'hg log: how to get latest commit by a particular user?

I have a repo with multiple users. I need to make a python script to retrieve a particular user's latest commits. How do I do that with mercurial?

I was thinking of calling an hg log command thru Python's subprocess call. The problem is how I should call the hg log command such that it tells me a user's latest logs.



Solution 1:[1]

Easy (but not efficient or elegant way)

  • hg log -u USERNAME

or (with revsets)

  • hg log -r "author(USERNAME)"

with added value

If string starts with "re:", the remainder of the string is treated as a regular expression. To match a user that actually contains "re:", use the prefix "literal:".

Solution 2:[2]

I think you should be able to get a single user's latest commit by calling something like this with a system call. (Command-line version follows.)

$ hg log -u your-user-name | head -5

The head -5 gives you the whole abbreviated hg log output for the latest changeset for your-user-name, which includes five lines: changeset, tag, user, date, and summary. If you want only the changeset, you could use something along these lines. (Command-line version, again.)

$ hg log -u [email protected] | head -1 | awk '{print $2}'

Comments suggest this makes unwarranted assumptions about the output format. I agree.


Based on comments, this seems to be the best expression for getting the last commit from a user.

hg log -r "last(author('[email protected]'))" 
hg log -r "last(author('Fred Flintstone'))"

To get the last three . . .

hg log -r "last(author('[email protected]'), 3)" 
hg log -r "last(author('Fred Flintstone'), 3)"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2