Search code examples
mercurialhg-log

How to change default behaviour of hg log to only show ancestors?


Is it possible to change the default behaviour of hg log to show only the current ancestors?

Ie this:

hg log --rev "reverse(ancestors(.))"

The thing is, I always want hg log to do that.

I know that I could do something like this in my .bashrc but I was wondering if there was a mercurial way to do this.

hg () {

    if [[ $2 -eq "log" ]]
    then
        # TODO: Append other arguments to this...
        hg log --rev "reverse(ancestors(.))" 
    else
        # Run the command
    fi
}

Solution

  • The alias function of hg is the right tool for this. While you can create aliases that modify the default behavior of built-in commands, hg config (section "alias") has the following to say:

    Note:
       It is possible to create aliases with the same names as existing
       commands, which will then override the original definitions. This is
       almost always a bad idea!
    

    The recommended practice is to create an alias alog and simply train your fingers to type hg alog instead of hg log. Placing this in ~/.hgrc (or equivalent) will do it:

    [alias]
    alog = log --rev "reverse(ancestors(.))"
    

    Why is this a better solution? Not only does an alias for log block you from accessing the original behavior (requiring another alias that claws back the original meaning); more importantly, once you come to expect the non-standard behavior, sooner or later you'll get bit when you type hg log in an another account or context that's not controlled by your .hgrc. (Typing hg alog in the same circumstances will only incur an "unknown command" error).