X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/blobdiff_plain/41a6d8591d5962dbfe8e372fff10c60e06718083..a622d90bbeccf29319dc2c3142a93ae1010648fe:/gitmergeonefile.py diff --git a/gitmergeonefile.py b/gitmergeonefile.py index 5b588dd..99882c8 100755 --- a/gitmergeonefile.py +++ b/gitmergeonefile.py @@ -20,8 +20,22 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ import sys, os + +# Try to detect where it is run from and set prefix and the search path. +# It is assumed that the user installed StGIT using the --prefix= option +prefix, bin = os.path.split(sys.path[0]) + +if bin == 'bin' and prefix != sys.prefix: + major, minor = sys.version_info[0:2] + local_path = [os.path.join(prefix, 'lib', 'python'), + os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor)), + os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor), + 'site-packages')] + sys.path = local_path + sys.path + from stgit.config import config from stgit.utils import append_string +from stgit.git import get_base_dir # @@ -39,15 +53,6 @@ else: keeporig = 'yes' -# -# Global variables -# -if 'GIT_DIR' in os.environ: - base_dir = os.environ['GIT_DIR'] -else: - base_dir = '.git' - - # # Utility functions # @@ -74,17 +79,17 @@ def __checkout_files(): orig = '%s.older' % path tmp = __output('git-unpack-file %s' % orig_hash) os.chmod(tmp, int(orig_mode, 8)) - os.rename(tmp, orig) + os.renames(tmp, orig) if file1_hash: src1 = '%s.local' % path tmp = __output('git-unpack-file %s' % file1_hash) os.chmod(tmp, int(file1_mode, 8)) - os.rename(tmp, src1) + os.renames(tmp, src1) if file2_hash: src2 = '%s.remote' % path tmp = __output('git-unpack-file %s' % file2_hash) os.chmod(tmp, int(file2_mode, 8)) - os.rename(tmp, src2) + os.renames(tmp, src2) def __remove_files(): """Remove any temporary files @@ -100,7 +105,7 @@ def __remove_files(): def __conflict(): """Write the conflict file for the 'path' variable and exit """ - append_string(os.path.join(base_dir, 'conflicts'), path) + append_string(os.path.join(get_base_dir(), 'conflicts'), path) sys.exit(1) @@ -108,10 +113,12 @@ def __conflict(): # $2 - file in branch1 SHA1 (or empty) # $3 - file in branch2 SHA1 (or empty) # $4 - pathname in repository -# $5 - orignal file mode (or empty) +# $5 - original file mode (or empty) # $6 - file in branch1 mode (or empty) # $7 - file in branch2 mode (or empty) -#print 'gitmerge.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % tuple(sys.argv[1:8]) +# +#print 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \ +# % tuple(sys.argv[1:8]) orig_hash, file1_hash, file2_hash, path, orig_mode, file1_mode, file2_mode = \ [__str2none(x) for x in sys.argv[1:8]] @@ -125,14 +132,14 @@ __checkout_files() if orig_hash: # modified in both if file1_hash and file2_hash: - # if modes are the same (git-read-tree probably dealed with it) + # if modes are the same (git-read-tree probably dealt with it) if file1_hash == file2_hash: - if os.system('git-update-cache --cacheinfo %s %s %s' + if os.system('git-update-index --cacheinfo %s %s %s' % (file1_mode, file1_hash, path)) != 0: - print >> sys.stderr, 'Error: git-update-cache failed' + print >> sys.stderr, 'Error: git-update-index failed' __conflict() - if os.system('git-checkout-cache -u -f -- %s' % path): - print >> sys.stderr, 'Error: git-checkout-cache failed' + if os.system('git-checkout-index -u -f -- %s' % path): + print >> sys.stderr, 'Error: git-checkout-index failed' __conflict() if file1_mode != file2_mode: print >> sys.stderr, \ @@ -146,14 +153,14 @@ if orig_hash: 'output': path }) == 0 if merge_ok: - os.system('git-update-cache %s' % path) + os.system('git-update-index -- %s' % path) __remove_files() sys.exit(0) else: print >> sys.stderr, \ 'Error: three-way merge tool failed for file "%s"' % path # reset the cache to the first branch - os.system('git-update-cache --cacheinfo %s %s %s' + os.system('git-update-index --cacheinfo %s %s %s' % (file1_mode, file1_hash, path)) if keeporig != 'yes': __remove_files() @@ -164,19 +171,43 @@ if orig_hash: if os.path.exists(path): os.remove(path) __remove_files() - sys.exit(os.system('git-update-cache --remove %s' % path)) + sys.exit(os.system('git-update-index --remove -- %s' % path)) + # file deleted in one and changed in the other + else: + # Do something here - we must at least merge the entry in the cache, + # instead of leaving it in U(nmerged) state. In fact, stg resolved + # does not handle that. + + # Do the same thing cogito does - remove the file in any case. + os.system('git-update-index --remove -- %s' % path) + + #if file1_hash: + ## file deleted upstream and changed in the patch. The patch is + ## probably going to move the changes elsewhere. + + #os.system('git-update-index --remove -- %s' % path) + #else: + ## file deleted in the patch and changed upstream. We could re-delete + ## it, but for now leave it there - and let the user check if he + ## still wants to remove the file. + + ## reset the cache to the first branch + #os.system('git-update-index --cacheinfo %s %s %s' + #% (file1_mode, file1_hash, path)) + __conflict() + # file does not exist in origin else: # file added in both if file1_hash and file2_hash: # files are the same if file1_hash == file2_hash: - if os.system('git-update-cache --add --cacheinfo %s %s %s' + if os.system('git-update-index --add --cacheinfo %s %s %s' % (file1_mode, file1_hash, path)) != 0: - print >> sys.stderr, 'Error: git-update-cache failed' + print >> sys.stderr, 'Error: git-update-index failed' __conflict() - if os.system('git-checkout-cache -u -f -- %s' % path): - print >> sys.stderr, 'Error: git-checkout-cache failed' + if os.system('git-checkout-index -u -f -- %s' % path): + print >> sys.stderr, 'Error: git-checkout-index failed' __conflict() if file1_mode != file2_mode: print >> sys.stderr, \ @@ -196,15 +227,15 @@ else: else: mode = file2_mode obj = file2_hash - if os.system('git-update-cache --add --cacheinfo %s %s %s' + if os.system('git-update-index --add --cacheinfo %s %s %s' % (mode, obj, path)) != 0: - print >> sys.stderr, 'Error: git-update-cache failed' + print >> sys.stderr, 'Error: git-update-index failed' __conflict() __remove_files() - sys.exit(os.system('git-checkout-cache -u -f -- %s' % path)) + sys.exit(os.system('git-checkout-index -u -f -- %s' % path)) -# Un-handled case -print >> sys.stderr, 'Error: Un-handled merge conflict' -print >> sys.stderr, 'gitmerge.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \ +# Unhandled case +print >> sys.stderr, 'Error: Unhandled merge conflict' +print >> sys.stderr, 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \ % tuple(sys.argv[1:8]) __conflict()