31

I'm customizing my git log to be all in 1 line. Specifically, I added the following alias:

lg = log --graph --pretty=format:'%Cred%h%Creset - %C(yellow)%an%Creset - %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

So, when I run git lg, I see the following:

* 41a49ad - zain - commit 1 message here (3 hours ago)
* 6087812 - zain - commit 2 message here (5 hours ago)
* 74842dd - zain - commit 3 message here (6 hours ago)

However, I want to add the SVN revision number in there too, so it looks something like:

* 41a49ad - r1593 - zain - commit 1 message here (3 hours ago)

The normal git log shows you the SVN revision number, so I'm sure this must be possible. How do I do this?

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
Zain
  • 639
  • 1
  • 7
  • 13

4 Answers4

31

Consider the command git svn

  • it has a similar log function than git log: git svn log
  • it has the find-rev option (to retrieve the SVN revision from a SHA1 key) (introduced in git 1.6.0)

I am not sure of you can combine those two options in one command line though.
A script (a bit like this one which is not exactly what you want but still can give some idea) might be in order.


sdaau adds in the comments:

An example of this:

git svn find-rev $(git log --max-count 1 --pretty=format:%H)

Adversus adds in the comments:

Note that find-rev searches in the current branch, so if you're in master and rxyz happened in a branch, find-rev will not find it.
You can give -B (before) or -A (after) options for a wider search, see git svn find-rev man page section.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    An example of this: `git svn find-rev $(git log --max-count 1 --pretty=format:%H)` – sdaau Jul 21 '14 at 02:49
  • @sdaau Thank you. I have included your example in the answer for more visibility. – VonC Jul 21 '14 at 05:55
  • Those links to the mailing lists are broken. Could you replace them with a more appropriate link? E.g. the web archive http://web.archive.org/web/20070615164322/http://lists-archives.org/git/423371-git-svn-add-find-rev-command.html – Rob W Dec 20 '14 at 11:27
  • @RobW no need for web.archive.org ;) I have restored the links. – VonC Dec 20 '14 at 18:04
  • How do you add the result of this command to a git log ? I have an alias `lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit`, how can I insert the SVN revision ? – thomasb Oct 18 '16 at 14:33
  • @thomasb 6 years later, not too sure: try and ask a new question for all to see. – VonC Oct 18 '16 at 15:19
  • Note that `find-rev` searches in the current branch, so if you're in master and rxyz happened in a branch `find-rev` will not find it. You can give `-B` or `-A` options for a wider search, see [find-rev](https://git-scm.com/docs/git-svn#Documentation/git-svn.txt-emfind-revem) – Adversus Sep 30 '19 at 12:30
  • @Adversus Thank you (for commenting on this 9-years old question). I have included your comment in the answer for more visibility. – VonC Sep 30 '19 at 13:04
  • @VonC thanks for the 9 years old answer ;) Just trying to help other poor souls stuck in the git-svn vcs limbo. – Adversus Oct 01 '19 at 14:09
15

Run:

git rev-parse HEAD

which gives you git commit hash.

Then use that commit hash to run:

git svn find-rev <commit_hash>

Which gives you svn revision.

kakyo
  • 10,460
  • 14
  • 76
  • 140
  • I like this answer best, because it seems most direct. Here's my take on a one-line version: ```echo "r$(git svn find-rev $(git rev-parse HEAD))"```. This works as an alias of course: ```rev = !echo "r$(git svn find-rev $(git rev-parse HEAD))"``` – floer32 Jul 17 '13 at 12:43
  • I like this answer best, because it seems most direct. Here's my take on a one-line version: ```echo "r$(git svn find-rev $(git rev-parse HEAD))"```. This works as an alias of course: ```svnrev = !echo "r$(git svn find-rev $(git rev-parse HEAD))"```. This also makes it so you can mimic svn export even better ```export = !git archive --format zip --output /tmp/$(git svnrev) master``` – floer32 Jul 17 '13 at 12:50
  • `git svn find-rev` also works the other way round, just specify the SVN revision number prefixed with an r: `git svn find-rev r63919`. – Flimm Oct 01 '13 at 09:02
  • 9
    Maybe this wasn't true when you submitted the answer (or I have special git/svn version), but for me it also works to simply write `git svn find-rev HEAD`. – yngve Oct 02 '13 at 08:52
  • Actually the right command seems to be `git svn find-rev master` as you could be currently in another branch and svn in syncronized with 'master' branch if I'm not wrong – Jaime Hablutzel Dec 04 '13 at 22:23
  • 3
    This only works if you don't have any local commits you haven't pushed to SVN. Otherwise it will come back blank! – DavidGamba Apr 22 '14 at 18:49
10

When you say that "the normal git log shows you the SVN revision number", I guess you mean that you are dealing with a repository handled by git svn, which by default adds a line like this at the end of the synchronized commits:

git-svn-id: svn://path/to/repository@###### <domain>

Now, as far as git is concerned, this is just random text, so I doubt that you can find a % accessor to read the ###### revision number from there.

At this point your best option would be to just parse the output of plain git log by yourself. Here's a crude starting point:

git log -z | tr '\n\0' ' \n' | sed 's/\(commit \S*\) .*git-svn-id: svn:[^@]*@\([0-9]*\) .*/\1 r\2/'
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
8

Ended up with something like this:

git svn log --oneline -1 | cut -d '|' -f1

That gives the last revision from that repo (you can tweak git svn log parameters for showing another revision, but keep --oneline and -1), but with a trailing whitespace (something like "r9441 ") that I think should be easy to strip out.

Hope it helps...

user1264366
  • 81
  • 1
  • 1