简体   繁体   English

来自 shell 脚本的 Git diff 命令

[英]Git diff command from shell script

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.理论上应该显示在 git 合并期间发生的所有合并冲突。 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: (SHA1 和 SHA2 是索引哈希)但是当我手动复制命令并运行时:

git diff <SHA1>^ <SHA2>

it works.有用。 So my question is: Why shell script can not execute git diff <SHA1>^ <SHA2> ?所以我的问题是:为什么 shell 脚本不能执行git diff <SHA1>^ <SHA2>

The solution was to remove ^ completely.解决方案是完全删除^ Answer from @torek in comments. @torek 在评论中的回答。

#!/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     

                                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM