Export files changed at revision from mercurial

Sometimes (more precisely - rarely) you need to export files changed at given revision as directory tree instead of patch file.

Mercurial vcs has archive command which let you to export selected files at given revision:

hg archive --type files --rev $REVISION -I list_of_files

To list files changed at revision:

hg log -r $REVISION --template '{files}\n' | sed 's/\n / -I /g'

Here end of line character is used to handle the case of using space character in filename.

Combine this snippets

#!/bin/bash

# dump all files changed at given revision into given directory

REVISION=$1
DESTINATION=$2

if [ -n "$REVISION" ] && [ -n "$DESTINATION" ]; then
    mkdir -p $DESTINATION
    hg archive --type files --rev $REVISION -I $(hg log -r $REVISION --template '{files}\n' | sed 's/\n / -I /g') $DESTINATION
else
    echo -e "`basename $0` revision outputdir"
    echo -e "\twhere revision - revision number or hash"
    echo -e "\toutputdir - destination directory (created if needed)"
fi