{"thread":{"id":"15328","subject":"[RFC/PATCH] git-mergetool: allow using merge tools for staging changes","startedAt":"2008-09-02T07:25:49Z","lastAt":"2008-09-02T07:25:49Z","messageCount":1,"participants":["David Aguilar"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"89492","messageId":"ca72cec5020d847ac7d1577c586772ae22810bef.1220339887.git.davvid@gmail.com","threadId":"15328","inReplyTo":null,"subject":"[RFC/PATCH] git-mergetool: allow using merge tools for staging changes","fromName":"David Aguilar","fromEmail":"davvid@gmail.com","sentAt":"2008-09-02T07:25:49Z","receivedAt":"2008-09-02T07:25:49Z","isPatch":true,"sender":{"key":"davvid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/13196?v=4"},"body":"git-mergetool contains a lot of useful code for interfacing\nwith arbitrary diff/merge tools.  This code is only ever used\nwhen merge conflicts occur, though.\n\nThis change allows git-mergetool to be used for the common\ntask of comparing unstaged changes against the latest commit.\nBeing able to see side-by-side diffs in your favorite $mergetool\nis the benefit of these changes.\n\nWhen additional changes are staged in the index, those changes\nare provided as the $BASE merge file.\n\nSigned-off-by: David Aguilar <davvid@gmail.com>\n---\n\nNotes:\n\nI am aware that git-citool is a viable alternative in such a\nscenario, but being able to see a side-by-side diff is one of\nthe first things that CVS-using co-workers asked me about\nwhen using git.\n\nThis only affects the \"git mergetool -t <tool> <file(s)>\"\nscenario.  The behavior of the no-argument \"git mergetool\"\ninvocation is unchanged.\n\nStyle note:\n\nThis patch mixes tabs and spaces (indent: 4) to be consistent\nwith the rest of git-mergetool.sh.\n\n git-mergetool.sh |   32 ++++++++++++++++++++++++++------\n 1 files changed, 26 insertions(+), 6 deletions(-)\n\ndiff --git a/git-mergetool.sh b/git-mergetool.sh\nindex 94187c3..9e5cc68 100755\n--- a/git-mergetool.sh\n+++ b/git-mergetool.sh\n@@ -131,6 +131,13 @@ merge_file () {\n     MERGED=\"$1\"\n \n     f=`git ls-files -u -- \"$MERGED\"`\n+\n+    if test -z \"$f\" ; then\n+\tf=$(git ls-files --modified -- \"$MERGED\")\n+\ttest -z \"$f\" && f=$(git diff --name-only --cached -- \"$MERGED\")\n+\ttest -f \"$f\" && worktree_mode=1\n+    fi\n+\n     if test -z \"$f\" ; then\n \tif test ! -f \"$MERGED\" ; then\n \t    echo \"$MERGED: file not found\"\n@@ -149,13 +156,26 @@ merge_file () {\n     mv -- \"$MERGED\" \"$BACKUP\"\n     cp -- \"$BACKUP\" \"$MERGED\"\n \n-    base_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==1) print $1;}'`\n-    local_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==2) print $1;}'`\n-    remote_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==3) print $1;}'`\n+    if test -n \"$worktree_mode\" ; then\n+\tbase_mode=$(git diff --name-only --cached -- \"$MERGED\")\n+\tlocal_mode=1\n+\tremote_mode=1\n+\tcp \"$MERGED\" \"$LOCAL\"\n+\tgit show HEAD:\"$MERGED\" > \"$REMOTE\"\n+\tif base_present ; then\n+\t    # If changes are present in the index use them as $BASE\n+\t    git checkout-index --prefix=.remote- \"$MERGED\"\n+\t    mv .remote-\"$MERGED\" \"$BASE\"\n+\tfi\n+    else\n+\tbase_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==1) print $1;}'`\n+\tlocal_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==2) print $1;}'`\n+\tremote_mode=`git ls-files -u -- \"$MERGED\" | awk '{if ($3==3) print $1;}'`\n \n-    base_present   && git cat-file blob \":1:$prefix$MERGED\" >\"$BASE\" 2>/dev/null\n-    local_present  && git cat-file blob \":2:$prefix$MERGED\" >\"$LOCAL\" 2>/dev/null\n-    remote_present && git cat-file blob \":3:$prefix$MERGED\" >\"$REMOTE\" 2>/dev/null\n+\tbase_present   && git cat-file blob \":1:$prefix$MERGED\" >\"$BASE\" 2>/dev/null\n+\tlocal_present  && git cat-file blob \":2:$prefix$MERGED\" >\"$LOCAL\" 2>/dev/null\n+\tremote_present && git cat-file blob \":3:$prefix$MERGED\" >\"$REMOTE\" 2>/dev/null\n+    fi\n \n     if test -z \"$local_mode\" -o -z \"$remote_mode\"; then\n \techo \"Deleted merge conflict for '$MERGED':\"\n-- \n1.6.0.1.161.g7f314\n"}]}