Created
March 31, 2021 00:55
-
-
Save paulie4/5762ad9851629b87f36d01375e3694c0 to your computer and use it in GitHub Desktop.
workaround for lack of "copy" in git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Since Git does not have a way to copy files, this implements the workaround from here: | |
# https://stackoverflow.com/a/46484848/2016290 | |
import argparse | |
import os | |
import subprocess | |
TMP_BRANCH = 'tmpToCopyFile' | |
def gitCopyFile(origPath, newPath): | |
def run(*args): | |
print(f'RUNNING {" ".join(args)}') | |
subprocess.run(args, shell=True, check=True) | |
print() | |
# verify that the path exists | |
run('git', 'show', origPath) | |
# create temp branch | |
run('git', 'checkout', '-b', TMP_BRANCH) | |
# move file to new name | |
run('git', 'mv', origPath, newPath) | |
# commit file rename | |
commitMsg = input('enter commit message for \'git mv\': ') | |
run('git', 'commit', '-m', commitMsg) | |
# bring back the original file and commit it | |
run('git', 'checkout', 'HEAD~', origPath) | |
run('git', 'commit', '-m', 'restore original file') | |
# switch back to the previous branch (just like `cd -` in most command line shells) | |
run('git', 'checkout', '-') | |
# merge the temp branch and delete it | |
run('git', 'merge', '--no-ff', TMP_BRANCH) | |
run('git', 'branch', '-d', TMP_BRANCH) | |
if __name__ == '__main__': | |
helpstr = 'to workaround git\'s lack of "copy", create a temp branch to rename a file and then merge it back' | |
parser = argparse.ArgumentParser(description=helpstr) | |
parser.add_argument('origPath') | |
parser.add_argument('newPath') | |
args = parser.parse_args() | |
gitCopyFile(args.origPath, args.newPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment