Differences between revisions 2 and 19 (spanning 17 versions)
Revision 2 as of 2005-10-26 20:12:59
Size: 974
Editor: JanHudec
Comment:
Revision 19 as of 2006-05-12 22:42:40
Size: 5404
Editor: BrendanCully
Comment: Added shell function for upgrading a repository in place.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Available tips:
[[TableOfContents]]
Line 8: Line 11:
=== The same thing, but for a tagged release ===

{{{
hg clone --noupdate source export-tagged
cd export-tagged
hg update mytag
rm -rf .hg
}}}
Line 10: Line 22:
Make a private copy of the 'hgeditor' script provided with mercuiral and replace the call to the editor with following command: Make a private copy of the 'hgeditor' script provided with mercurial and replace the call to the editor with following command:
Line 19: Line 31:

=== See diffstat of pulled changes ===

Place the following script (named "pull-diffstat" here) somewhere in your $PATH:

{{{
#!/bin/sh
test -n "$NODE" || exit 0
PARENT=`hg parents $NODE | head -1 | awk -F':' '{print $3}'`
TIP=`hg tip | head -1 | awk -F':' '{print $3}'`
echo "diffstat for $PARENT to $TIP"
hg diff -r $PARENT -r tip | diffstat
}}}

Add a changegroup entry to the [hooks] section of hgrc:

{{{
[hooks]
changegroup = pull-diffstat
}}}

Now you will see a diffstat of the new changes to your repo every time you do "hg pull".

=== One liner to remove unknown files with a pattern ===
To make these work, replace the {{{ls -l}}} with the command you wish to execute (ie. {{{rm}}}). You can also tweak the parameters passed to {{{hg status}}} to filter by something other than unknown files (see {{{hg help status}}}).

{{{
hg status -nu0 | grep -z pattern | xargs -0r ls -l
}}}

The above command requires a current version of GNU grep. If you don't have one, you can use the following:
{{{
hg status -nu | grep pattern | tr '\n' '\0' | xargs -0r ls -l
}}}

=== Keyword expansion according to file revision ===

This is an example on how you can achieve filewise keyword expansion (similar
to CVS) with an [encode] filter and the pretxncommit-hook. Comes in handy
when you want to keep track of different file revisions in the same
repository.

For demonstration we use just one keyword: "Hg".

 * {{{$Hg$}}}

It will be expanded by the script "hgpretxncommit.sh" (see below) to:

 * {{{$Hg: <basename of file>,v <short hash> <date> $}}}

You have the choice between two date output formats:

 * Generic Mercurial output
 * YYYY/mm/dd

You need an [encode] filter that "reverts" the expansion in your hgrc (unless
you don't care about the expanded keyword showing up in every "hg diff").

Simple example hgrc for a repository containing python files:

{{{
[encode]
*.py = sed 's/[$]Hg[^$]*[$]/\$Hg\$/'
[hooks]
pretxncommit = hgpretxncommit.sh
}}}

In "hgpretxncommit.sh" you have to tweak the 'excl' and 'slash' variables
according to your needs. The script doesn't look at files matching $excl.
Whereas files not matching $excl but matching $slash will have the commit
date expanded as YYYY/mm/dd (like the python files in this example).

{{{
#!/bin/sh
set -e
test $? -eq 0 || exit 1
excl='^\.hg\|\.\(p\(df\|ng\)\|jpg\)$'
slash='\.\(\(te\|dt\)x\|s\(ty\|h\)\|c\(fg\|ls\)\|bib\|py\|awk\)$'
cset="${HG_NODE:0:12}"
tipv=`hg tip --verbose`
manifest=`hg manifest | cut -d ' ' -f 3`
slashdate=`echo "$tipv" |
awk '/^date: +/ { printf "%d/%s/%0.2d %s %s", $6, $3, $4, $5, $7 }'`
m=0
for M in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec; do
    let $((++m))
    slashdate=${slashdate/$M/`printf %0.2d $m`}
done
longdate=`echo "$tipv" | awk -F ': +' '/^date: +/ { print $2 }'`
for f in `echo "$tipv" | awk -F ': +' '/^files: +/ { print $2 }'`; do
    if echo "$f" | grep -v -q "$excl" && echo "$manifest" | grep -x -q "$f"
    then
        bn="${f##*/}"
        if echo "$f" | grep -q "$slash"; then
            hgdate="$slashdate"
        else
            hgdate="$longdate"
        fi
        sed -i,hg -e "s!\([$]Hg\)[^$]*[$]!\1: ${bn},v $cset $hgdate \$!" "$f" &&
        rm "${f},hg"
    fi
done
exit $?
}}}

=== hg diff does not support -foo option like gnu diff does ===

I use the following bash function to put the diff options I like most

{{{
hgdi ()
{
  for i in `hg status -marn "$@"`
  do
    diff -ubwd <(hg cat "$i") "$i"
  done
}
}}}

=== upgrading a repository to revlogng in place ===

Here's a quick and dirty script to upgrade a mercurial repository in place.
Note: the current undo information will be lost.

{{{
function upgradehg () (
  if test -n "$1"
  then
    local repo="$1"
    local tmprepo="$1".tmp
  else
    local repo="."
    local tmprepo="../hgupgrade.tmp"
  fi

  set -e
  hg clone --pull -U "$repo" "$tmprepo"
  cp "$repo"/.hg/{hgrc,dirstate} "$tmprepo"/.hg
  mv "$repo"/.hg "$repo"/.hg.orig
  mv "$tmprepo"/.hg "$repo"/.hg
  rmdir "$tmprepo"
  echo "Upgrade complete. A backup of your original repository is now in $repo/\.hg.orig"
)
}}}

Available tips: TableOfContents

Make a clean copy of a source tree, like CVS export

hg clone source export
rm -rf export/.hg

The same thing, but for a tagged release

hg clone --noupdate source export-tagged
cd export-tagged
hg update mytag
rm -rf .hg

See diffs when editing commit message with VIM

Make a private copy of the 'hgeditor' script provided with mercurial and replace the call to the editor with following command:

vim "+e $HGTMP/diff" '+set buftype=help filetype=diff' "+vsplit $HGTMP/msg" || exit $?

This will start up VIM in vertical split mode with commit message in the left pane and diff in the right pane. The buftype=help setting for diff window tells vim to exit when all other windows are closed, so when you write and quit the log with :x (:wq - they are equivalent), vim exits. If you have syntax highlight set up, the diff will be properly highlighted.

This setting is suitable for wide terminals. If you have a narrow terminal, you may want to replace the +vsplit above with +split or add nowrap to the +set.

See diffstat of pulled changes

Place the following script (named "pull-diffstat" here) somewhere in your $PATH:

test -n "$NODE" || exit 0
PARENT=`hg parents $NODE | head -1 | awk -F':' '{print $3}'`
TIP=`hg tip | head -1 | awk -F':' '{print $3}'`
echo "diffstat for $PARENT to $TIP"
hg diff -r $PARENT -r tip | diffstat

Add a changegroup entry to the [hooks] section of hgrc:

[hooks]
changegroup = pull-diffstat

Now you will see a diffstat of the new changes to your repo every time you do "hg pull".

One liner to remove unknown files with a pattern

To make these work, replace the ls -l with the command you wish to execute (ie. rm). You can also tweak the parameters passed to hg status to filter by something other than unknown files (see hg help status).

hg status -nu0 | grep -z pattern | xargs -0r ls -l

The above command requires a current version of GNU grep. If you don't have one, you can use the following:

hg status -nu | grep pattern | tr '\n' '\0' | xargs -0r ls -l

Keyword expansion according to file revision

This is an example on how you can achieve filewise keyword expansion (similar to CVS) with an [encode] filter and the pretxncommit-hook. Comes in handy when you want to keep track of different file revisions in the same repository.

For demonstration we use just one keyword: "Hg".

  • $Hg$

It will be expanded by the script "hgpretxncommit.sh" (see below) to:

  • $Hg: <basename of file>,v <short hash> <date> $

You have the choice between two date output formats:

  • Generic Mercurial output
  • YYYY/mm/dd

You need an [encode] filter that "reverts" the expansion in your hgrc (unless you don't care about the expanded keyword showing up in every "hg diff").

Simple example hgrc for a repository containing python files:

[encode]
*.py = sed 's/[$]Hg[^$]*[$]/\$Hg\$/'
[hooks]
pretxncommit = hgpretxncommit.sh

In "hgpretxncommit.sh" you have to tweak the 'excl' and 'slash' variables according to your needs. The script doesn't look at files matching $excl. Whereas files not matching $excl but matching $slash will have the commit date expanded as YYYY/mm/dd (like the python files in this example).

set -e
test $? -eq 0 || exit 1
excl='^\.hg\|\.\(p\(df\|ng\)\|jpg\)$'
slash='\.\(\(te\|dt\)x\|s\(ty\|h\)\|c\(fg\|ls\)\|bib\|py\|awk\)$'
cset="${HG_NODE:0:12}"
tipv=`hg tip --verbose`
manifest=`hg manifest | cut -d ' ' -f 3`
slashdate=`echo "$tipv" |
awk '/^date:  +/ { printf "%d/%s/%0.2d %s %s", $6, $3, $4, $5, $7 }'`
m=0
for M in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec; do
    let $((++m))
    slashdate=${slashdate/$M/`printf %0.2d $m`}
done
longdate=`echo "$tipv" | awk -F ': +' '/^date:  +/ { print $2 }'`
for f in `echo "$tipv" | awk -F ': +' '/^files:  +/ { print $2 }'`; do
    if echo "$f" | grep -v -q "$excl" && echo "$manifest" | grep -x -q "$f"
    then
        bn="${f##*/}"
        if echo "$f" | grep -q "$slash"; then
            hgdate="$slashdate"
        else
            hgdate="$longdate"
        fi
        sed -i,hg -e "s!\([$]Hg\)[^$]*[$]!\1: ${bn},v $cset $hgdate \$!" "$f" &&
        rm "${f},hg"
    fi
done
exit $?

hg diff does not support -foo option like gnu diff does

I use the following bash function to put the diff options I like most

hgdi ()
{
  for i in `hg status -marn "$@"`
  do
    diff -ubwd <(hg cat "$i") "$i"
  done
}

upgrading a repository to revlogng in place

Here's a quick and dirty script to upgrade a mercurial repository in place. Note: the current undo information will be lost.

function upgradehg () (
  if test -n "$1"
  then
    local repo="$1"
    local tmprepo="$1".tmp
  else
    local repo="."
    local tmprepo="../hgupgrade.tmp"
  fi

  set -e
  hg clone --pull -U "$repo" "$tmprepo"
  cp "$repo"/.hg/{hgrc,dirstate} "$tmprepo"/.hg
  mv "$repo"/.hg "$repo"/.hg.orig
  mv "$tmprepo"/.hg "$repo"/.hg
  rmdir "$tmprepo"
  echo "Upgrade complete. A backup of your original repository is now in $repo/\.hg.orig"
)

TipsAndTricks (last edited 2016-12-05 11:14:36 by ArneBab)