History of changes to a particular line of code in Subversion

Svn

Svn Problem Overview


Is it possible to see the history of changes to a particular line of code in a Subversion repository?

I'd like, for instance, to be able to see when a particular statement was added or when that statement was changed, even if its line number is not the same any more.

Svn Solutions


Solution 1 - Svn

I don't know a method for tracking statements through time in Subversion.

It is simple however to see when any particular line in a file was last changed using svn blame. Check the SVNBook: svn blame reference:

Synopsis

svn blame TARGET[@REV]...

Description

Show author and revision information in-line for the specified files or URLs. Each line of text is annotated at the beginning with the author (username) and the revision number for the last change to that line.

Solution 2 - Svn

In the TortoiseSVN client there is a very nice feature that lets you:

  • blame a file, displaying the last change for each line (this is standard)
  • "blame previous revision", after clicking on a particular line in the above view (this is the good one)

The second feature does what it says - it shows the annotated revision preceding the last modification to the line. By using this feature iteratively, you can trace back through the history of a particular line.

Solution 3 - Svn

I'd usually:

  1. Run svn blame FILE first.

  2. Note the last revision of the particular line.

  3. Do another query with the -r argument:

    svn blame FILE -r 1:REV
    
  4. Trace manually from there.

Solution 4 - Svn

This can be done in two stages:

  1. svn blame /path/to/your/file > blame.tmp
  2. grep "your_line_of_text" blame.tmp

You can delete blame.tmp file afterwards if you don't need it.

In principle, a simple script can be written in any scripting language that does roughly the same.

Solution 5 - Svn

In Eclipse you can know when each line of your code has been committed using the SVN annotate view, or right click on the file → TeamShow annotation....

Solution 6 - Svn

The key here is how much history is required. As others have pointed out, the short answer is: svn blame (see svn help blame for details). If you're reaching far back in history or dealing with significant changes, you will likely need more than just this one command.

I just had to do this myself, and found this (ye ole) thread here on SO. Here's what I did to solve it with just the CLI, specifically for my case where an API had changed (e.g. while porting someone's far outdated work (not on a branch, arrgh!) back into a feature branch based off of an up-to-date trunk). E.g. function names had changed enough to where it wasn't apparent which function needed to be called.

Step One

The following command allowed me to page through commits where things had changed in the file "fileName.h" and to see the corresponding revision number (note: you may have to alter the '10' for more or less context per your svn log text).

svn log | grep -C 10 "fileName.h" | less

This results in a list of revisions in which this file was modified.

Step Two

Then it was a simple matter of using blame (or as others have pointed out, annotate) to narrow down to the revisions of interest.

cd trunk
svn blame fileName.h@r35948 | less

E.g. found the revision of interest was 35948.

Step Three

Having found the revision(s) of interest via blame, a diff can be produced to leverage the SVN tool.

svn diff -r35948:PREV fileName.h

Conclusion

Having a visual diff made it much easier to identify the old API names with the newer/updated API names.

Solution 7 - Svn

svn annotate

The AKA SVN Blame from TortoiseSVN.

Solution 8 - Svn

svn blame shows you which checkin modified any line in a file the last time.

This works on old revisions too.

Solution 9 - Svn

A start is the command svn blame (or annotate,praise). It will show you when a line of code was last modified and by whom it was modified. e.g.:

  4564    wiemann # $Id$
  4564    wiemann # Author: David Goodger <[email protected]>
   778    goodger # Copyright: This module has been placed in the public domain.
   217    goodger 

Solution 10 - Svn

If you use Emacs, the built-in package vc can do this.

  1. Navigate to the file in question.
  2. Run the command vc-annotate with either M-x vc-annotate or C-xvg.
  3. Each line will show up with its revision, like a normal svn blame.
  4. Pressing a (vc-annotate-revision-previous-to-line) will navigate to the revision before the revision at the line you're on.

Solution 11 - Svn

The command you're looking for is svn blame.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlindelofView Question on Stackoverflow
Solution 1 - SvnmorechilliView Answer on Stackoverflow
Solution 2 - SvnRafał DowgirdView Answer on Stackoverflow
Solution 3 - SvnatedjaView Answer on Stackoverflow
Solution 4 - SvnSerge KutnyView Answer on Stackoverflow
Solution 5 - SvnYoussef NAITView Answer on Stackoverflow
Solution 6 - SvntnilesView Answer on Stackoverflow
Solution 7 - SvnMarcio AguiarView Answer on Stackoverflow
Solution 8 - SvnMaximilianView Answer on Stackoverflow
Solution 9 - SvnPeter HoffmannView Answer on Stackoverflow
Solution 10 - SvnHarrison McView Answer on Stackoverflow
Solution 11 - SvnBrad WilsonView Answer on Stackoverflow