3

Have a repo with lots of commits. I know the name of the file, and the thing (a string, some coefficients to be precise) I am looking for, but I don't know what commit it's in. It's no longer at the current one, that's for sure, nor is it in the few previous ones.

*How can I search time-wise, so to speak, for a certain thing within a specified file (or within the whole repo, I don't care), so it goes through all the commits until he finds it?*

Interested in both mercurial & git. Use both currently, and I don't really know whether this is possible in either one of them.

Rook
  • 60,248
  • 49
  • 165
  • 242

4 Answers4

3

Mercurial has the grep command. From the documentation:

hg grep [OPTION]... PATTERN [FILE]...

search for a pattern in specified files and revisions

Search revisions of files for a regular expression.

This command behaves differently than Unix grep. It only accepts
Python/Perl regexps. It searches repository history, not the working
directory. It always prints the revision number in which a match appears.

By default, grep only prints output for the first revision of a file in
which it finds a match. To get it to print every revision that contains a
change in match status ("-" for a match that becomes a non-match, or "+"
for a non-match that becomes a match), use the --all flag.

For Git, this related question seems relevant:

How to grep (search) committed code in the git history?

Community
  • 1
  • 1
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
2

Git has the grep command as well.

git grep regex $(git rev-list --all) file

See How to grep (search) committed code in the git history? for more ways to search.

Community
  • 1
  • 1
Oesor
  • 6,632
  • 2
  • 29
  • 56
1

With git you can use a pickaxe search. There's two interesting flags to git log:

-S<string>
    Look for differences that introduce or remove an instance of <string>.
    Note that this is different than the string simply appearing in diff
    output; see the pickaxe entry in gitdiffcore(7) for more details.

This lets you find the commits that add or remove a given string.

-G<regex>
    Look for differences whose added or removed line matches the given
    <regex>.

This lets you find commits where an added or removed line matches the regular expression.

You can run this on a specific file by filtering the output of git log with -- filename, e.g. git log -Scoeff -- myfile.c

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
0

Mercurial (fresh) have revsets, which will allow to find anything and more easy than using grep (in common)

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110