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
  • 57,389
  • 47
  • 162
  • 240

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
  • 57,640
  • 11
  • 83
  • 76
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,602
  • 2
  • 26
  • 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
  • 176,187
  • 29
  • 372
  • 338
0

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

Lazy Badger
  • 91,325
  • 7
  • 76
  • 105