简体   繁体   中英

How can I pipe output from git log in Git Bash?

Reason: I want to compare two arbitrary different commits using a difftool. I know the hashes from a search and I don't want to copy these hashes, thus I am looking for a command that does something like

$ log_str=$(git log --all -S"new_tour <-" --pretty=format:"%h")
$ git difftool -t kdiff3 log_str[1] log_str[2] myfile.txt
  • I would like to be able to address arbitrary indices - not always 1 and 2
  • It would be great if the answer also gives a hint, how to figure out, what the structure of log_str is. Is it a character? An array of characters? A list? ... using the Bash.

I found some related help here and here , but I can't make it work.
Now I do:

$ git log --pretty=format:"%h"
3f69dc7  
b8242c6  
01aa74f  
903c5aa  
069cfc5  

and

$ git difftool -t kdiff3 3f69dc7 b8242c6 myfile.txt

I would take a two step approach using a temporary file:

git log --all -S'SEARCH' --pretty=format:"%h" > tmp_out
git diff "$(sed -n '1p' tmp_out)" "$(sed -n '2p' tmp_out)" myfile.txt
rm tmp_out

sed is used to display line 1 and line 2 of the file.


With variables:

search="foo"
index_a="1"
index_b="2"
file="myfile.txt"
git log --all -S"${search}" --pretty=format:"%h" > tmp_out
git diff "$(sed -n "${index_a}p" tmp_out)" "$(sed -n "${index_b}p"  tmp_out)" "${file}"
rm tmp_out

in a bash function:

search_diff() {
    search="${1}"
    index_a="${2}"
    index_b="${3}"
    file="${4}"
    git log --all -S"${search}" --pretty=format:"%h" > tmp_out
    git diff "$(sed -n "${index_a}p" tmp_out)" "$(sed -n "${index_b}p" tmp_out)" "${file}"
    rm tmp_out
}

search_diff "foo" 2 3 myfile.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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