sys.path.append('@@GIT_PYTHON_PATH@@')
from gitMergeCommon import *
-alwaysWriteTree = False
-
# The actual merge code
# ---------------------
+originalIndexFile = os.environ.get('GIT_INDEX_FILE',
+ os.environ.get('GIT_DIR', '.git') + '/index')
+temporaryIndexFile = os.environ.get('GIT_DIR', '.git') + \
+ '/merge-recursive-tmp-index'
+def setupIndex(temporary):
+ try:
+ os.unlink(temporaryIndexFile)
+ except OSError:
+ pass
+ if temporary:
+ newIndex = temporaryIndexFile
+ os.environ
+ else:
+ newIndex = originalIndexFile
+ os.environ['GIT_INDEX_FILE'] = newIndex
+
def merge(h1, h2, branch1Name, branch2Name, graph, callDepth=0):
'''Merge the commits h1 and h2, return the resulting virtual
commit object and a flag indicating the cleaness of the merge.'''
assert(isinstance(Ms, Commit))
if callDepth == 0:
- if len(ca) > 1:
- runProgram(['git-read-tree', h1.tree()])
- runProgram(['git-update-cache', '-q', '--refresh'])
- # Use the original index if we only have one common ancestor
-
- updateWd = True
- if alwaysWriteTree:
- cleanCache = True
- else:
- cleanCache = False
+ setupIndex(False)
+ cleanCache = False
else:
+ setupIndex(True)
runProgram(['git-read-tree', h1.tree()])
- updateWd = False
cleanCache = True
[shaRes, clean] = mergeTrees(h1.tree(), h2.tree(), Ms.tree(),
branch1Name, branch2Name,
- cleanCache, updateWd)
+ cleanCache)
if clean or cleanCache:
res = Commit(None, [h1, h2], tree=shaRes)
return [res, clean]
-getFilesRE = re.compile('([0-9]+) ([a-z0-9]+) ([0-9a-f]{40})\t(.*)')
+getFilesRE = re.compile(r'^([0-7]+) (\S+) ([0-9a-f]{40})\t(.*)$', re.S)
def getFilesAndDirs(tree):
files = Set()
dirs = Set()
self.stages = [Stage(), Stage(), Stage()]
self.path = path
-unmergedRE = re.compile('^([0-9]+) ([0-9a-f]{40}) ([1-3])\t(.*)$')
+unmergedRE = re.compile(r'^([0-7]+) ([0-9a-f]{40}) ([1-3])\t(.*)$', re.S)
def unmergedCacheEntries():
'''Create a dictionary mapping file names to CacheEntry
objects. The dictionary contains one entry for every path with a
return res
def mergeTrees(head, merge, common, branch1Name, branch2Name,
- cleanCache, updateWd):
+ cleanCache):
'''Merge the trees 'head' and 'merge' with the common ancestor
'common'. The name of the head branch is 'branch1Name' and the name of
the merge branch is 'branch2Name'. Return a tuple (tree, cleanMerge)
print 'Already uptodate!'
return [head, True]
- if updateWd:
- updateArg = '-u'
- else:
+ if cleanCache:
updateArg = '-i'
- runProgram(['git-read-tree', updateArg, '-m', common, head, merge])
+ else:
+ updateArg = '-u'
+
+ [out, code] = runProgram(['git-read-tree', updateArg, '-m', common, head, merge], returnCode = True)
+ if code != 0:
+ die('git-read-tree:', out)
+
cleanMerge = True
[tree, code] = runProgram('git-write-tree', returnCode=True)
entries = unmergedCacheEntries()
for name in entries:
if not processEntry(entries[name], branch1Name, branch2Name,
- files, dirs, cleanCache, updateWd):
+ files, dirs, cleanCache):
cleanMerge = False
if cleanMerge or cleanCache:
return [tree, cleanMerge]
-def processEntry(entry, branch1Name, branch2Name, files, dirs,
- cleanCache, updateWd):
+def processEntry(entry, branch1Name, branch2Name, files, dirs, cleanCache):
'''Merge one cache entry. 'files' is a Set with the files in both of
the heads that we are going to merge. 'dirs' contains the
corresponding data for directories. If 'cleanCache' is True no
non-zero stages will be left in the cache for the path
corresponding to the entry 'entry'.'''
-# cleanCache == True => Don't leave any non-stage 0 entries in the cache.
-# False => Leave unmerged entries
-
-# updateWd == True => Update the working directory to correspond to the cache
-# False => Leave the working directory unchanged
+# cleanCache == True => Don't leave any non-stage 0 entries in the cache and
+# don't update the working directory
+# False => Leave unmerged entries and update the working directory
# clean == True => non-conflict case
# False => conflict case
# If cleanCache == False then the cache shouldn't be updated if clean == False
- def updateFile(clean, sha, mode, path):
- if cleanCache or (not cleanCache and clean):
- runProgram(['git-update-cache', '--add', '--cacheinfo',
- '0%o' % mode, sha, path])
+ def updateFile(clean, sha, mode, path, onlyWd=False):
+ updateCache = not onlyWd and (cleanCache or (not cleanCache and clean))
+ updateWd = onlyWd or (not cleanCache and clean)
if updateWd:
prog = ['git-cat-file', 'blob', sha]
os.symlink(linkTarget, path)
else:
assert(False)
- runProgram(['git-update-cache', '--', path])
+
+ if updateWd and updateCache:
+ runProgram(['git-update-index', '--add', '--', path])
+ elif updateCache:
+ runProgram(['git-update-index', '--add', '--cacheinfo',
+ '0%o' % mode, sha, path])
def removeFile(clean, path):
if cleanCache or (not cleanCache and clean):
- runProgram(['git-update-cache', '--force-remove', '--', path])
+ runProgram(['git-update-index', '--force-remove', '--', path])
- if updateWd:
+ if not cleanCache and clean:
try:
os.unlink(path)
except OSError, e:
files.add(newPath)
return newPath
- debug('processing', entry.path, 'clean cache:', cleanCache,
- 'wd:', updateWd)
+ debug('processing', entry.path, 'clean cache:', cleanCache)
cleanMerge = True
if aMode != bMode:
cleanMerge = False
print 'CONFLICT: File "' + path + \
- '" added identically in both branches,'
- print 'CONFLICT: but permissions conflict', '0%o' % aMode, \
- '->', '0%o' % bMode
+ '" added identically in both branches,', \
+ 'but permissions conflict', '0%o' % aMode, '->', \
+ '0%o' % bMode
print 'CONFLICT: adding with permission:', '0%o' % aMode
updateFile(False, aSha, aMode, path)
newPath1 = uniquePath(path, branch1Name)
newPath2 = uniquePath(path, branch2Name)
print 'CONFLICT (add/add): File "' + path + \
- '" added non-identically in both branches.', \
- 'Adding "' + newPath1 + '" and "' + newPath2 + '" instead.'
+ '" added non-identically in both branches.'
removeFile(False, path)
updateFile(False, aSha, aMode, newPath1)
updateFile(False, bSha, bMode, newPath2)
if ret != 0:
cleanMerge = False
print 'CONFLICT (content): Merge conflict in "' + path + '".'
- updateFile(False, sha, mode, path)
+
+ if cleanCache:
+ updateFile(False, sha, mode, path)
+ else:
+ updateFile(True, aSha, aMode, path)
+ updateFile(False, sha, mode, path, True)
else:
updateFile(True, sha, mode, path)
print ''
except:
- traceback.print_exc(None, sys.stderr)
- sys.exit(2)
+ if isinstance(sys.exc_info()[1], SystemExit):
+ raise
+ else:
+ traceback.print_exc(None, sys.stderr)
+ sys.exit(2)
if clean:
sys.exit(0)
else:
- print 'Automatic merge failed, fix up by hand'
sys.exit(1)