Re: [git-users] Show all commits for a specific file where ONLY that file was updated

2018-07-26 Thread Konstantin Khomoutov
On Thu, Jul 26, 2018 at 04:42:02AM -0700, Rob Hills wrote:

Hi!

> I am trying to get a list of commits for a specific file where ONLY the 
> file I am interested in has been updated.
> 
> If I run
> 
> git log -- 
> 
> I get a list of commits that have updated that file.  This list of commits 
> includes some that only contain my file and some that contain a number of 
> files that have been updated in the same commit.  I am trying to filter out 
> the commits that contain only one file, the one I am interested in.
> 
> Unfortunately, neither my git-fu nor my bash-fu have been up to the task so 
> far.
> 
> Any tips would be very welcome.

This can be done by a combination of the `git log -- ` you've
mentioned with a bit of shell scripting around the `git show` command
which is able to "show" Git objects of various types, including commits.

The "--name-status" option of the `git show` command displays the
changes introduced by a commit using the

  FLAG  FILENAME

format with one line per name. The "M" flag stands for "modified", "A"
for "added", "D" for deleted and so on.
So the encantation of the form

  git show --name-status --pretty=oneline 

would display a summary of the commit as the first line (it will include
the commit's SHA-1 name and the title of its message) and the rest of
the output would be those "flags+name" pairs.

So, to script the stuff, you'd call something like this (untested):

  git rev-list HEAD -- "$filepath" | while read sha; do
n=`git show --name-status --pretty=oneline "$sha" |
  tail -n +2 | grep -v "^[MAD]\t$filepath" | wc -l`
test "$n" -ne 0 && continue
echo "$sha"
  done

It should print you SHA-1 names of the commits reachable from HEAD which
"touched" (added, deleted or modified) only the file $filepath, in
reverse chronological order.

The body of the `while` loop rolls like this:

1) It calls `git show` in a way explained above.
2) Skips the the first line of its output (which contains the status).
3) From the remailing lines, which are "flags+filename" pairs, the lines
   which indicate modifications to $filepath are filtered out.
4) The remaining lines are counted, and the number of them is checked:
   - If it is zero, the commit only touched the file of interest.
   - Otherwise it also touched some other files.


A final remark is that this approach does not consider detecting file
renames and/or copies.  In order to implement this, you might need to
drop `git rev-list` and play with the "--pretty=" command-line
option of the `git log` command, and also its "-M" and "-C" options.

-- 
You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to git-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[git-users] Show all commits for a specific file where ONLY that file was updated

2018-07-26 Thread Rob Hills
Hi All,

I am trying to get a list of commits for a specific file where ONLY the 
file I am interested in has been updated.

If I run

git log -- 

I get a list of commits that have updated that file.  This list of commits 
includes some that only contain my file and some that contain a number of 
files that have been updated in the same commit.  I am trying to filter out 
the commits that contain only one file, the one I am interested in.

Unfortunately, neither my git-fu nor my bash-fu have been up to the task so 
far.

Any tips would be very welcome.

Cheers,

Rob Hills
Waikiki, Western Australia

-- 
You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to git-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.