开发者

How do I find useful code previously deleted but still stored in source control?

Whenever someone asks what to do with code that is no longer needed the answer is usually "delete it, restore it from source control if you need it back".

Now how do I find that piece of s开发者_StackOverflow中文版ource code in the repository? Let's limit scope to SVN for simplicity - I suspect that using any other source control system will not make much difference in this aspect (correct me if I'm wrong).

If I delete that code and commit the changes it will no longer be in the latest revision. How do I find it without exporting each revision and searching thoroughly (which is nearly impossible)?


You need to use a difference tool, most svn repositories have them. Right click on the file you want to rollback and choose 'diff' or 'difference' or similar (spending on implementation) and then it will ask to compare the current version with an earlier version - if you know the approximate date of the earlier version then choose that version (the date/time stamps are usually obvious) and you will see the two versions side by side. You can then choose which bits are different (usually coloured red) right click on them and choose to integrate them.

The client tool you are using for SVN will determine the details.


Another way is to search the log messages - you presumably left a comment somewhat along the lines of:

Removed XYZ functionality

when you made the change.


The most correct answer is: you can't. There are methods to help you with that task, but it is impossible to do the automatically, because:

  • you don't know which files it is in
  • you don't know the source code or parts of it
  • you can't remember when the code removal happened.
  • ...

A computer cannot read our minds. That said, there are of course other tools that help you, such as the mentioned ones, or graphical front ends like trac (which I like very much for that purpose).


The way I do it is to think of a date when I knew the missing code was active, and fetch the entire source tree as it was on that date. If I get the date wrong, I'll go x day/weeks/months either side until I zero in on it.

svn checkout --revision {2002-02-17}


Presumably, the code comments or revision comments have keywords to target the code fragment you are looking for:

SVN Query - http://svnquery.tigris.org/

"SvnQuery is a fast full text search engine for subversion repositories. It searches every file in every revision. Not only the content of a file is indexed, but also its complete metadata like path, author, comments and properties. A simple, Google-like web frontend is provided, but it is expected that the query library and indexing mechanism can be reused in other frontends, e.g. a web service or a Visual Studio Plug-In.

SvnQuery is implemented in C# 3.0 and ASP.NET is used for the web frontend. The full text search is done by Lucene.Net which delivers an astonishing performance. ..."

The query language is described here:
http://svnquery.tigris.org/servlets/ProjectProcess?pageID=o0dpdE&freeformpage=SvnWebQueryHelp


I suspect that using any other source control system will not make much difference in this aspect (correct me if I'm wrong).

This is wrong. Distributed version control (e.g. git or hg) can do this relatively efficiently.

In git, for instance, to find a deleted function named foo, you could do git log -S foo, which will show all commits that add or remove lines that contain "foo".

If you want to stick with the arbitrary SVN limitation for some reason, clone the SVN repository with git-svn first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜