3

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
10
  • You want git diff HEAD~1 HEAD ? That's the command you currently have. (Which is the same as git show) Commented Apr 24, 2018 at 7:56
  • @hek2mgl No, I want to compare two arbitrary different commits using a difftool and I don't want to copy the hashes... Commented Apr 24, 2018 at 7:57
  • Use git rev-list rather than git log: it's designed for scripting. Commented Apr 24, 2018 at 16:32
  • @torek: But git rev-list -S"new_tour <-" --pretty=format:"%h" does not work. Did I miss anything? Commented Apr 24, 2018 at 16:38
  • You had --all above; you'll need that, or HEAD, here (this is one significant difference from git log, which will assume HEAD if not given other revision specifiers to start from). Remove the --pretty argument entirely: rev-list's main job is to list revision hash IDs, so that's what it does by default. Commented Apr 24, 2018 at 16:53

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

You are welcome but please don't my answer in a way that introduces syntax errors. Feel free to modify it according to your needs, but do not change it in place
I am sorry - what went wrong? I checked it on my laptop and everything worked. Otherwise I would not have changed anything...
which shell are you using?
Git bash for Windows
Not sure what's under the hood of that. In a normal bash a function definition using def, like def foo() is a syntax error. Besides that, I would generally not modify answers more than probably fixing a little syntax error. What you do on your computer is your thing, sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.