Allow branches and tags to be remapped
[fast-export.git] / hg-fast-export.sh
blobfac4c857fb98f5e2cd0cb3b15d04e93a13494179
1 #!/bin/sh
3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
6 ROOT="$(dirname "$(which "$0")")"
7 REPO=""
8 PFX="hg2git"
9 SFX_MAPPING="mapping"
10 SFX_MARKS="marks"
11 SFX_HEADS="heads"
12 SFX_STATE="state"
13 GFI_OPTS=""
14 PYTHON=${PYTHON:-python}
16 USAGE="[--quiet] [-r <repo>] [--force] [-m <max>] [-s] [--hgtags] [-A <file>] [-B <file>] [-T <file>] [-M <name>] [-o <name>] [--hg-hash] [-e <encoding>]"
17 LONG_USAGE="Import hg repository <repo> up to either tip or <max>
18 If <repo> is omitted, use last hg repository as obtained from state file,
19 GIT_DIR/$PFX-$SFX_STATE by default.
21 Note: The argument order matters.
23 Options:
24 --quiet Passed to git-fast-import(1)
25 -r <repo> Mercurial repository to import
26 --force Ignore validation errors when converting, and pass --force
27 to git-fast-import(1)
28 -m <max> Maximum revision to import
29 -s Enable parsing Signed-off-by lines
30 --hgtags Enable exporting .hgtags files
31 -A <file> Read author map from file
32 (Same as in git-svnimport(1) and git-cvsimport(1))
33 -B <file> Read branch map from file
34 -T <file> Read tags map from file
35 -M <name> Set the default branch name (defaults to 'master')
36 -o <name> Use <name> as branch namespace to track upstream (eg 'origin')
37 --hg-hash Annotate commits with the hg hash as git notes in the
38 hg namespace.
39 -e <encoding> Assume commit and author strings retrieved from
40 Mercurial are encoded in <encoding>
42 case "$1" in
43 -h|--help)
44 echo "usage: $(basename "$0") $USAGE"
45 echo ""
46 echo "$LONG_USAGE"
47 exit 0
48 esac
49 . "$(git --exec-path)/git-sh-setup"
50 cd_to_toplevel
52 while case "$#" in 0) break ;; esac
54 case "$1" in
55 -r|--r|--re|--rep|--repo)
56 shift
57 REPO="$1"
59 --q|--qu|--qui|--quie|--quiet)
60 GFI_OPTS="$GFI_OPTS --quiet"
62 --force)
63 # pass --force to git-fast-import and hg-fast-export.py
64 GFI_OPTS="$GFI_OPTS --force"
65 break
67 -*)
68 # pass any other options down to hg2git.py
69 break
72 break
74 esac
75 shift
76 done
78 # for convenience: get default repo from state file
79 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
80 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
81 echo "Using last hg repository \"$REPO\""
84 if [ -z "$REPO" ]; then
85 echo "no repo given, use -r flag"
86 exit 1
89 # make sure we have a marks cache
90 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
91 touch "$GIT_DIR/$PFX-$SFX_MARKS"
94 # cleanup on exit
95 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
97 _err1=
98 _err2=
99 exec 3>&1
100 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
102 exec 4>&3 3>&1 1>&4 4>&-
104 _e1=0
105 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
106 --repo "$REPO" \
107 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
108 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
109 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
110 --status "$GIT_DIR/$PFX-$SFX_STATE" \
111 "$@" 3>&- || _e1=$?
112 echo $_e1 >&3
113 } | \
115 _e2=0
116 git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" 3>&- || _e2=$?
117 echo $_e2 >&3
121 exec 3>&-
122 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
124 # move recent marks cache out of the way...
125 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
126 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
127 else
128 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
131 # ...to create a new merged one
132 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
133 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
135 # save SHA1s of current heads for incremental imports
136 # and connectivity (plus sanity checking)
137 for head in `git branch | sed 's#^..##'` ; do
138 id="`git rev-parse refs/heads/$head`"
139 echo ":$head $id"
140 done > "$GIT_DIR/$PFX-$SFX_HEADS"
142 # check diff with color:
143 # ( for i in `find . -type f | grep -v '\.git'` ; do diff -u $i $REPO/$i ; done | cdiff ) | less -r