5

My project underwent multiple directory renames and directory splitting. gitk deals with that very nicely and is capable of tracking a file from head to all file revisions down to the very first one.

Emacs, however, seems to stumble upon the first rename. It only lists a few recent revisions when doing Ctrl-x v l (Tools > Version Control > Show History on the menu, vc-git-print-log function in vc-git.el):

commit 13172930ab094badeab81cd2e05119d2a4c1f58a
Author: WinWin <winwin@example.com>
Date:   Tue Jul 12 18:17:27 2011 -0600

    commit comment 

commit 0b52ca957a79a26e51bdd6f7193ef913c4abdc7b
Author: WinWin <winwin@example.com>
Date:   Tue Jul 10 10:39:12 2011 -0600

    commit comment 

commit 8ca577e532849203646867527921b66aa1162dbd
Author: WinWin <winwin@example.com>
Date:   Tue Jul 10 08:01:59 2011 -0600

    commit comment 

commit 9e623a23ad485d0937fe5409162f07434be8ca63
Author: WinWin <winwin@example.com>
Date:   Tue Jul 10 01:50:39 2011 -0600

    commit comment 

Since I am used to the way Emacs built-in support for Version Control works (finger habits from the CVS days...), I wonder whether it is possible to enable it to track the entire revision history, despite the renames, just as gitk does, without using a completely different package?

WinWin
  • 7,493
  • 10
  • 44
  • 53
  • 1
    how do you track a file with gitk? – Paŭlo Ebermann Jul 13 '11 at 00:57
  • 1
    @Paulo: just `gitk ` seems to do the trick, though you might need to do the usual `--` separator if there is a branch with the same name. – Tyler Jul 13 '11 at 02:01
  • @MatrixFrog Paŭlo Ebermann is correct to ask this question. I just tried `gitk ` on a file that its **full** path has been labeled with branch/tag about 20 times, but `gitk` only shows 4 commits. This suggests that `gitk` is not really tracking renames. I am disappointed. – WinWin Jul 15 '11 at 19:01
  • 1
    @Paŭlo Ebermann Oh wow! I am no longer disappointed. :)) I just discovered that gitk tracks a file incredibly well all the way down to the first ever commit! The trick is to specify the `--follow` switch, i.e. `gitk --follow `. – WinWin Jul 15 '11 at 19:22
  • @WinWin: Thanks. I'm not totally sure how this works ... it gives some strange results here. Anyway, `--follow` seems to be simply passed on to `git log`. (But this gets off topic here.) – Paŭlo Ebermann Jul 15 '11 at 19:36
  • @Paŭlo Ebermann It turns out that the `--follow` option is not even listed in the [gitk man page](ftp://ftp.kernel.org/pub/software/scm/git/docs/gitk.html). No wonder you were asking about that. Git seems to be ingenious but the more I learn about it the more I realize that I need to be a genius to understand it. – WinWin Jul 15 '11 at 20:04
  • [`gitk`'s manpage](http://www.kernel.org/pub/software/scm/git/docs/gitk.html) says *the command takes options applicable to the git rev-list command*. But in fact it calls `git log`, which [says the same thing](http://www.kernel.org/pub/software/scm/git/docs/git-log.html). – Paŭlo Ebermann Jul 15 '11 at 20:15
  • @Paŭlo Ebermann I just discovered that although `gitk --follow` does go very deep to older commits, it doesn't go all the way down to the first one like `git log --follow` does. Is that what you meant by "strange results"? – WinWin Jul 15 '11 at 20:44
  • No. Here is a screenshot: http://i.stack.imgur.com/25Ohz.png The blue circles relate to commits which changed the file (including some, but not all before the rename), while the white circles are their parent commits (which did not change the file). – Paŭlo Ebermann Jul 15 '11 at 20:57
  • (We can continue in chat, if you want.) – Paŭlo Ebermann Jul 15 '11 at 20:57

1 Answers1

2

You have other packages for integrating git with Emacs, as described here (maggit, git-emacs or egg, an old fork from maggit).

What you need to check, in git.el or in those other implementation, is how they implement git log:
Only a git log -M -C will find renames and copies of a file(*) (or at least a git log --follow).
(Also, check your git version)

(*): Not exactly for one file: --follow is the right option when displaying the log for one file. See the last part of this answer to know why.

That being said, without changing anything to your current package, you can check your local git config, and try:

git config diff.renames copies

and see if that changes the default git.el log result.


The OP WinWin reports:

  • the --follow is the right option use for rename and copy detection for one given file.
  • that modifying C:\emacs-23.2\lisp\vc-git.el (and deleting vc-git.elc in same folder, this is important!), adding "--follow" to the (defun vc-git-print-log ... part is enough for said option to be active.

Note: to understand the difference between -C and -M options, and --follow, see this thread

Jakub Narębski mentioned in May 2010 (and that still seems accurate in July 2011):

'-M/-C' is useful with "git diff" without pathspec, including e.g. "git show -C".

The problem with "git log -M -- <filename>" is that history simplification, which is required for good performance, happens before diff mechanism has a chance to perform rename detection. Before there was '--follow' option to git-log (which supports only the case of single file, and doesn't work that well with more complicated history), you were forced to do:

$ git log -M -- <filename> <oldname> <oldername>...

Also, is there a way to set this as the default for 'git log'?

I don't think so. Note also that '--follow' works only with single file, and does not work for example (currently) with directory pathspec.

To which, Eli Barzilay adds:

OK, so just to clear this up:
-C and -M (and --find-copies-harder) are for 'diff', and --follow is for 'log' with a single file (and each will pass it on to the other)?

Jeff King answers:

Yes (well, diff can never pass --follow to log, since diff never invokes log, but yes, the per-commit diff shown by log uses the -C and -M given to log).


About the config settings, Jeff King mentions:

copy detection can be really* slow. There is a reason it isn't on by default.
Try "git log -1000 -p" versus "git log -1000 -p -C -M --find-copies-harder" in some repository.
In a simple git.git test, it is almost 5x slower (about 1 second versus 5 seconds on my machine).
For large repositories, it can be much worse, because now each diff is O(size of repository) instead of O(size of changes).

Still, I see your point that you might want it on all the time, if you have a sufficiently small repo. There is "diff.renames" to turn on rename detection all the time.
But I think a log.follow option doesn't make sense at this point:

$ git config log.follow true
$ git log foo.c ;# ok, follow foo.c
$ git log foo.c bar.c ;# uh oh, now what?

Does the last one just barf, and make you say "git log --no-follow foo.c bar.c"?
Does it quietly turn off --follow, making the user guess why "git log foo.c" finds some history that "git log foo.c bar.c" doesn't?

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank for this great answer. Unfortunately, I just tried `git log -M -C ` on a file that its name never changed during the course of numerous (20 or more) directory splitting (as a project re-org) but `git log -M -C` was only able to track the last 4 commits. This is very disappointing as I thought that git is much smarter than that. :-/ – WinWin Jul 15 '11 at 19:13
  • Oh wow! I was too quick to judge git: It turns out that `git log --follow` does track a file incredibly well all the way down to the first ever commit! Now I need to see how to utilized that in Emcas. – WinWin Jul 15 '11 at 19:18
  • **UPDATE:** `git config diff.renames copies` doesn't change anything in Emacs's `Ctrl-x v l` (AKA `Show History`) behavior. :-/ – WinWin Jul 15 '11 at 21:02
  • @WinWin: I will be interested by the result of your tests. For the record, a `config log.follow` option were discussed in this thread, which is a good reference for the difference between `git log -C -M` and `git log --follow`: http://git.661346.n2.nabble.com/git-log-M-filename-is-not-working-td5020652.html. – VonC Jul 15 '11 at 21:41
  • **SUCCESS!** I just modified `C:\emacs-23.2\lisp\vc-git.el` (**and** deleted `vc-git.elc` in same folder, this is important!) by adding `"--follow"` to the `(defun vc-git-print-log ...` . Thanks so much @VonC. – WinWin Jul 15 '11 at 21:49
  • @WinWin: excellent. I have included your result in the answer, as well as a detailed explanation about the difference of `--follow` and '`-M / -C`' options for `git log`. – VonC Jul 16 '11 at 08:33
  • adding --follow works well to show the list. But I can't view the file by pressing 'f' there. `Running git cat-file blob hash.... Failed (status 128). I guess because file name is not the same as the current buffer's? – eugene May 15 '15 at 03:18