Rook
Rook

Reputation: 62578

How to search history of a repo?

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.

Upvotes: 3

Views: 223

Answers (4)

Lazy Badger
Lazy Badger

Reputation: 97355

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

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185831

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

Upvotes: 1

Oesor
Oesor

Reputation: 6642

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.

Upvotes: 2

Tim Henigan
Tim Henigan

Reputation: 62218

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?

Upvotes: 3

Related Questions