Mr_and_Mrs_D
Mr_and_Mrs_D

Reputation: 34076

How do I pass a range of commits to git log?

Ok I give up, I am a Bash noob.

This:

$ git log --no-walk --oneline 6980e6ecede8e188f434f6da669c2297f28decfe 458567536c1f20498da033cb0e42da74439ef12e

prints:

4585675 NNN bethDataFiles, allBethFiles belong to game/game/constants.py
6980e6e NNN bethDataFiles, allBethFiles belong to game/game/constants.py

This:

git log -g --pretty=format:'%ai %H' | awk '$0 >= "2016-04-13" && $0 <= "2016-04-15"'| cut -d' ' -f 4 | awk '!a[$0]++' | tr '\n' ' '

prints me a range of commits:

ba4ee4b099d23642e6cad56d9f41974f6e767781 1daaede0f4e11cae0e0bb00b9ebb43bba4f5671 ...

Now why on earth piping this command to git log as in:

git log -g --pretty=format:'%ai %H' | awk '$0 >= "2016-04-13" && $0 <= "2016-04-15"'| cut -d' ' -f 4 | awk '!a[$0]++' | tr '\r\n' ' ' | git log --format='%h %s %ai' --no-walk

only shows me the first commit:

ba4ee4b _load_active_plugins pre BAPI code - superseded by games.py API: 2016-04-14 19:38:41 +0200

?

$ git --version
git version 2.6.1.windows.1

Upvotes: 1

Views: 4407

Answers (3)

torek
torek

Reputation: 489848

There's a general form with bash and Unix utilities:

... | ... | ... | xargs cmd

(xargs runs cmd more than once as needed after inserting stdin as arguments), or:

cmd $(... | ... | ...)

which runs the pipeline, then provides its output as arguments to cmd.

However, with git log you don't need this as it accepts --stdin to get it to read revision IDs from standard input.

I think we can simplify your pipeline (incidentally, I did not realize some versions of awk know how to compare dates directly; older awk did not):

git log -g --pretty=format:'%ai %H' | \
    awk '$0 >= "2016-04-13" && $0 <= "2016-04-15"' | \
    cut -d' ' -f 4 | awk '!a[$0]++' | tr '\n' ' '

We want the revisions to be one per line so there's no need for the tr. We can also use the original awk to check the uniqueness of the hash ID. So:

git log -g --pretty=format:'%ai %H' | \
    awk '$0 >= "2016-04-13" && $0 <= "2016-04-15" && !a[$4]++ { print $4 }' | \
    git log --no-walk --stdin

Incidentally, it's only because git log -g does a reflog walk (which may produce duplicate hashes) that we need the associative array a to discard repeat hashes.

(edited per comment, but still untested)

Upvotes: 3

Alderath
Alderath

Reputation: 3874

git log does not read any input from stdin. Try doing echo "abcd123" | git log --no-walk and you will notice that git log will ignore stdin. It will look for a commit hash specified as an argument, but there is none. Hence, it will use the default HEAD, and print the latest commit on your current branch.

To print ranges of commit, you could for example do:

If you know the first and last commit hash:

git log abcd123..4567abc

If you know the time interval:

git log --since="10 days ago" --until="5 days ago"

Or if you know the specific dates:

git log --since="2016-04-13" --until="2016-04-15"

Upvotes: 4

user3159253
user3159253

Reputation: 17455

  git log --format='%h %s %ai' --no-walk \
       $(git log -g --pretty=format:'%ai %H' \
             | awk '$0 >= "2016-04-13" && $0 <= "2016-04-15"'\
             | cut -d' ' -f 4 | awk '!a[$0]++' | tr '\r\n' ' '
        )

Or slightly better

  git log -g --pretty=format:'%ai %H' | \
        awk '$0 >= "2016-04-13" && $0 <= "2016-04-15"' |\
        cut -d' ' -f 4 | awk '!a[$0]++' | \
        xargs git log --format='%h %s %ai' --no-walk

But I wonder why don't you wish to use --since and --until options of git log ?

Upvotes: 2

Related Questions