rev-list: allow -<n> as shorthand for --max-count=<n>
[git.git] / git-merge-recursive.py
index 767b13c..b17c8e5 100755 (executable)
@@ -205,11 +205,16 @@ def mergeFile(oPath, oSha, oMode, aPath, aSha, aMode, bPath, bSha, bMode,
             orig = runProgram(['git-unpack-file', oSha]).rstrip()
             src1 = runProgram(['git-unpack-file', aSha]).rstrip()
             src2 = runProgram(['git-unpack-file', bSha]).rstrip()
-            [out, code] = runProgram(['merge',
-                                      '-L', branch1Name + '/' + aPath,
-                                      '-L', 'orig/' + oPath,
-                                      '-L', branch2Name + '/' + bPath,
-                                      src1, orig, src2], returnCode=True)
+            try:
+                [out, code] = runProgram(['merge',
+                                          '-L', branch1Name + '/' + aPath,
+                                          '-L', 'orig/' + oPath,
+                                          '-L', branch2Name + '/' + bPath,
+                                          src1, orig, src2], returnCode=True)
+            except ProgramError, e:
+                print >>sys.stderr, e
+                die("Failed to execute 'merge'. merge(1) is used as the "
+                    "file-level merge tool. Is 'merge' in your path?")
 
             sha = runProgram(['git-hash-object', '-t', 'blob', '-w',
                               src1]).rstrip()
@@ -283,18 +288,20 @@ def updateFileExt(sha, mode, path, updateCache, updateWd):
 def setIndexStages(path,
                    oSHA1, oMode,
                    aSHA1, aMode,
-                   bSHA1, bMode):
-    prog = ['git-update-index', '-z', '--index-info']
-    proc = subprocess.Popen(prog, stdin=subprocess.PIPE)
-    pipe = proc.stdin
-    # Clear stages first.
-    pipe.write("0 " + ("0" * 40) + "\t" + path + "\0")
-    # Set stages
-    pipe.write("%o %s %d\t%s\0" % (oMode, oSHA1, 1, path))
-    pipe.write("%o %s %d\t%s\0" % (aMode, aSHA1, 2, path))
-    pipe.write("%o %s %d\t%s\0" % (bMode, bSHA1, 3, path))
-    pipe.close()
-    proc.wait()
+                   bSHA1, bMode,
+                   clear=True):
+    istring = []
+    if clear:
+        istring.append("0 " + ("0" * 40) + "\t" + path + "\0")
+    if oMode:
+        istring.append("%o %s %d\t%s\0" % (oMode, oSHA1, 1, path))
+    if aMode:
+        istring.append("%o %s %d\t%s\0" % (aMode, aSHA1, 2, path))
+    if bMode:
+        istring.append("%o %s %d\t%s\0" % (bMode, bSHA1, 3, path))
+
+    runProgram(['git-update-index', '-z', '--index-info'],
+               input="".join(istring))
 
 def removeFile(clean, path):
     updateCache = cacheOnly or clean
@@ -576,7 +583,7 @@ def processRenames(renamesA, renamesB, branchNameA, branchNameB):
             continue
 
         ren1.processed = True
-        removeFile(True, ren1.srcName)
+
         if ren2:
             # Renamed in 1 and renamed in 2
             assert(ren1.srcName == ren2.srcName)
@@ -604,13 +611,19 @@ def processRenames(renamesA, renamesB, branchNameA, branchNameB):
                            'adding as', dstName2, 'instead.')
                     removeFile(False, ren2.dstName)
                 else:
-                    dstName2 = ren1.dstName
+                    dstName2 = ren2.dstName
+                setIndexStages(dstName1,
+                               None, None,
+                               ren1.dstSha, ren1.dstMode,
+                              None, None)
+                setIndexStages(dstName2,
+                               None, None,
+                               None, None,
+                               ren2.dstSha, ren2.dstMode)
 
-                # NEEDSWORK: place dstNameA at stage 2 and dstNameB at stage 3
-                # What about other stages???
-                updateFile(False, ren1.dstSha, ren1.dstMode, dstName1)
-                updateFile(False, ren2.dstSha, ren2.dstMode, dstName2)
             else:
+                removeFile(True, ren1.srcName)
+
                 [resSha, resMode, clean, merge] = \
                          mergeFile(ren1.srcName, ren1.srcSha, ren1.srcMode,
                                    ren1.dstName, ren1.dstSha, ren1.dstMode,
@@ -636,6 +649,8 @@ def processRenames(renamesA, renamesB, branchNameA, branchNameB):
 
                 updateFile(clean, resSha, resMode, ren1.dstName)
         else:
+            removeFile(True, ren1.srcName)
+
             # Renamed in 1, maybe changed in 2
             if renamesA == renames1:
                 stage = 3
@@ -724,7 +739,6 @@ def processRenames(renamesA, renamesB, branchNameA, branchNameB):
                     cleanMerge = False
 
                     if not cacheOnly:
-                        # Stuff stage1/2/3
                         setIndexStages(ren1.dstName,
                                        oSHA1, oMode,
                                        aSHA1, aMode,