Search code examples
gitmercurialgit-loghg-log

How can I limit the Git/Mercurial log to commits whose logs contain at least one of two strings?


Is there a way where I can send two choices for git/Mercurial commands?

I want to search the history of git repository and Mercurial repository for specific word, say "commit". However, I am not sure how is it written in commit message "commit" or "com-mit"? So, I want to send these options with the command. I know that if I want to search I will type:

In git:

git log --grep="commit"

In mercurial:

hg log --keyword commit

Any help?


Solution

  • The most flexible way to do this with Mercurial is to use revsets. For your example:

    hg log -r 'grep("\\b(com-mit|commit)\\b")'
    

    In Python regex syntax, \b indicates a word boundary, and the backslash needs to be escaped. The (a|b) syntax is used to match one or the other. The single quotes surrounding the argument are there so that the shell won't interpret the metacharacters itself.

    You can also simplify the regular expression by combining revsets with and:

    hg log -r 'grep("\\bcom-mit\\b") or grep("\\bcommit\\b")'
    

    If you don't need to match words exactly (e.g. if you want to match "committer" and "committed" too, not just "commit"), you can use the keyword revsets:

    hg log -r 'keyword("com-mit") or keyword("commit")'
    

    By using and instead of or, you can find revisions that match both rather than one of them; also, you can use reverse(...) to list matches in the reverse order, e.g.:

    hg log -r 'reverse(keyword("com-mit") or keyword("commit"))'