Home > database >  Git diff command from shell script
Git diff command from shell script

Time:05-09

I have a script:

#!/bin/bash

git log -1 | grep -w '^commit' | cut -d ' ' -f2 | git show | grep -w '^index' | cut -d ' ' -f2 > tmp_out

while read -r arg
do
        arg1=${arg[@]:0:10}
        arg2=${arg[@]:23:10}

        cmd="git diff ${arg1}^ ${arg2}"
        echo $cmd
        $cmd
done <tmp_out

Which should in theory show all merge conflicts occurred during git merge. Script gives an error:

git diff <SHA1>^ <SHA2>
error: object <SHA1> is a blob, not a commit
error: object <SHA1> is a blob, not a commit
fatal: ambiguous argument '<SHA1>^': unknown revision or path not in the working tree.

(SHA1 and SHA2 are index hashes) But when I copy the command manually and run:

git diff <SHA1>^ <SHA2>

it works. So my question is: Why shell script can not execute git diff <SHA1>^ <SHA2>?

CodePudding user response:

The solution was to remove ^ completely. Answer from @torek in comments.

#!/bin/bash                                           
                                                        
git show | grep -w '^index' | cut -d ' ' -f2 > tmp_out
                                                      
while read -r arg                                     
do                                                    
        arg1=${arg[@]:0:10}                           
        arg2=${arg[@]:23:10}                          
                                                      
        cmd="git diff ${arg1} ${arg2}"              
        echo $cmd                                     
        $cmd                                          
done <tmp_out     

                                
  • Related