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

237 views Asked by At

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
}
2

There are 2 answers

0
alexis On BEST ANSWER

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).

3
Reimer Behrends On

While you can accomplish that with an alias (see below), the cleanest approach here is an extension:

from mercurial import extensions, commands

testedwith = "3.5"

default_log_rev = "reverse(ancestors(.))"

def override_log(original_cmd, ui, repo, *pats, **opts):
  have_rev = False
  for opt in ["rev", "branch"]:
    if opts.has_key(opt) and opts[opt]:
      have_rev = True
  if not have_rev:
    opts["rev"] = [default_log_rev]
  return original_cmd(ui, repo, *pats, **opts)

def uisetup(ui):
  extensions.wrapcommand(commands.table, "log", override_log)

This will only use your new default if neither the --rev nor the --branch options (or their abbreviations -r and -b) of log are set, thus preserving the original behavior if one of them is provided.

You can install such an extension in the usual way, i.e. by adding something like the following to your ~/.hgrc (assuming that the above code is in /path/to/logdefault.py):

[extensions]
logdefault = /path/to/logdefault.py

If an extension is to heavyweight for you, you can also create an alias in your ~/.hgrc:

[alias]
log = log --rev 'reverse(ancestors(.))'
rawlog = !$HG --config alias.log=log log "$@"

The second alias (rawlog) exists so that you can still access the original log functionality.