Differences between revisions 13 and 14
Revision 13 as of 2006-02-04 04:20:28
Size: 2274
Editor: BrendanCully
Comment: Removed FileMerge strategy - a variant is included in hgmerge as of 0.8
Revision 14 as of 2006-04-11 16:36:15
Size: 5035
Comment:
Deletions are marked like this. Additions are marked like this.
Line 65: Line 65:

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

This is an example on how you can achieve filewise keyword expansion (similar
to CVS) with [decode] and [encode] filters, and the pretxncommit-hook. This
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". As [decode] and [encode] work
only on a stream you'll have to include the filename ''manually'' once.

Accepted formats are:

 * {{{$Hg$}}} (without filename "expansion")
 * {{{$Hg: <filename>,v$}}}

You also have the choice between two date output formats:

 * Generic Mercurial output
 * YYYY/mm/dd (called 'slashday' here)

With the scripts called "hgfilter.sh" and "hgpretxncommit.sh" put in your
$PATH a simple setup could have the following entries in hgrc:

{{{
[decode]
*.txt = hgfilter.sh decode
*.py = hgfilter.sh decode slashday
[encode]
*.txt = hgfilter.sh
*.py = hgfilter.sh
[hooks]
pretxncommit = hgpretxncommit.sh
}}}

Here's the filter script "hgfilter.sh":

{{{
#!/bin/sh
set -e
decode=
slashday=
for i; do
    case "$i" in
        decode) decode=true; shift ;;
        slashday) slashday=true; shift ;;
    esac
done
if [ "$decode" ]; then
    if (($# == 0)); then
        tip=`hg tip`
        cset=`echo "$tip" | awk -F ':' '/^changeset: +/ { print $3 }'`
    else
        tip="$@"
        cset="${HG_NODE:0:12}"
    fi
    if [ "$slashday" ]; then
        hgdate=`echo "$tip" |
        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))
            hgdate=${hgdate/$M/`printf %0.2d $m`}
        done
    else
        hgdate=`echo "$tip" | awk -F ': +' '/^date: +/ { print $2 }'`
    fi
    sed -e "s!\([$]Hg\)\(:\( [^,]\{1,\},v\)\)\{0,1\}[$]!\1:\3 $cset $hgdate \$!"
else
    sed -e 's!\([$]Hg\(: [^,]\{1,\},v\)\{0,1\}\)[^$]*[$]!\1\$!'
fi
exit $?
}}}

In "hgpretxncommit.sh" you have to tweak the 'excl' and 'slash' variables
according to your needs. Supposing above [decode] settings in hgrc the 'slash'
pattern includes files with suffix ".py".

{{{
#!/bin/sh
set -e
test $? -eq 0 || exit 1
sl=""
excl='^\.hg\|\.\(p\(df\|ng\)\|jpg\)$'
slash='\.\(tex\|s\(ty\|h\)\|c\(fg\|ls\)\|bib\|py\|awk\)$'
tipv=`hg tip --verbose`
manifest=`hg manifest | cut -d ' ' -f 3`
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
        echo "$f" | grep -q "$slash" && sl='slashday'
        hg cat "$f" | hgfilter.sh decode $sl "$tipv" > "$f"
    fi
done
exit $?
}}}

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 [decode] and [encode] filters, and the pretxncommit-hook. This 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". As [decode] and [encode] work only on a stream you'll have to include the filename manually once.

Accepted formats are:

  • $Hg$ (without filename "expansion")

  • $Hg: <filename>,v$

You also have the choice between two date output formats:

  • Generic Mercurial output
  • YYYY/mm/dd (called 'slashday' here)

With the scripts called "hgfilter.sh" and "hgpretxncommit.sh" put in your $PATH a simple setup could have the following entries in hgrc:

[decode]
*.txt = hgfilter.sh decode
*.py = hgfilter.sh decode slashday
[encode]
*.txt = hgfilter.sh
*.py = hgfilter.sh 
[hooks]
pretxncommit = hgpretxncommit.sh

Here's the filter script "hgfilter.sh":

set -e
decode=
slashday=
for i; do
    case "$i" in
        decode) decode=true; shift ;;
        slashday) slashday=true; shift ;;
    esac
done
if [ "$decode" ]; then
    if (($# == 0)); then
        tip=`hg tip`
        cset=`echo "$tip" | awk -F ':' '/^changeset:  +/ { print $3 }'`
    else
        tip="$@"
        cset="${HG_NODE:0:12}"
    fi
    if [ "$slashday" ]; then
        hgdate=`echo "$tip" |
        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))
            hgdate=${hgdate/$M/`printf %0.2d $m`}
        done
    else
        hgdate=`echo "$tip" | awk -F ': +' '/^date:  +/ { print $2 }'`
    fi
    sed -e "s!\([$]Hg\)\(:\( [^,]\{1,\},v\)\)\{0,1\}[$]!\1:\3 $cset $hgdate \$!"
else
    sed -e 's!\([$]Hg\(: [^,]\{1,\},v\)\{0,1\}\)[^$]*[$]!\1\$!'
fi
exit $?

In "hgpretxncommit.sh" you have to tweak the 'excl' and 'slash' variables according to your needs. Supposing above [decode] settings in hgrc the 'slash' pattern includes files with suffix ".py".

set -e
test $? -eq 0 || exit 1
sl=""
excl='^\.hg\|\.\(p\(df\|ng\)\|jpg\)$'
slash='\.\(tex\|s\(ty\|h\)\|c\(fg\|ls\)\|bib\|py\|awk\)$'
tipv=`hg tip --verbose`
manifest=`hg manifest | cut -d ' ' -f 3`
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
        echo "$f" | grep -q "$slash" && sl='slashday'
        hg cat "$f" | hgfilter.sh decode $sl "$tipv" > "$f"
    fi
done
exit $?

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