Search code examples
gitgit-diff

Git diff from script


I am trying to write a script which takes a regular expression for commit log messages and then displays the git diff across all the commits. I have the following in my script:

#!/bin/sh
ARGV=("$@")
dir=$(pwd)
commits=($(git --work-tree ${dir} log --grep ${ARGV[0]} | grep -o 'commit .*$' | cut -f2 -d' '))
echo $(git --work-tree ${dir} log --grep ${ARGV[0]})
for i in ${commits[@]}
do
    echo "command: git diff ${i}"
    echo $(git diff ${i})
 done 

When I run /path/to/script.sh <some-regex> I get the following output:

command: git diff 806e5e76c0b20eabda3a14b1442168149ad30155

fatal: ambiguous argument '806e5e76c0b20eabda3a14b1442168149ad30155': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'

However when I run the actual command that is output everything works fine. Any ideas?


Solution

  • a script which takes a regular expression for commit log messages then displays the git diff across all the commits

    Doesn’t this do that?

    git log --patch --grep=<pattern>
    

    explainshell.com - git log --patch --grep