Merge branch 'master' into girocco
[girocco-hg-fast-export.git] / hg-fast-export.sh
blobc462ba5df063d161e0586a0a76604dd0e5cdd7e0
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 "$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] [--flatten] [-A <file>] [-U <addr>] [-M <name>] [-o <name>] [--hg-hash]"
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 --flatten Create one-level ref names (convert '/' to '_')
32 -A <file> Read author map from file
33 (Same as in git-svnimport(1) and git-cvsimport(1))
34 -U <addr> Use <addr> as unknown email instead of devnull@localhost
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.
40 case "$1" in
41 -h|--help)
42 echo "usage: $(basename "$0") $USAGE"
43 echo ""
44 echo "$LONG_USAGE"
45 exit 0
46 esac
47 . "$(git --exec-path)/git-sh-setup"
48 cd_to_toplevel
50 while case "$#" in 0) break ;; esac
52 case "$1" in
53 -r|--r|--re|--rep|--repo)
54 shift
55 REPO="$1"
57 --q|--qu|--qui|--quie|--quiet)
58 GFI_OPTS="$GFI_OPTS --quiet"
60 --force)
61 # pass --force to git-fast-import and hg-fast-export.py
62 GFI_OPTS="$GFI_OPTS --force"
63 break
65 -*)
66 # pass any other options down to hg2git.py
67 break
70 break
72 esac
73 shift
74 done
76 # for convenience: get default repo from state file
77 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
78 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
79 echo "Using last hg repository \"$REPO\""
82 if [ -z "$REPO" ]; then
83 echo "no repo given, use -r flag"
84 exit 1
87 # make sure we have a marks cache
88 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
89 touch "$GIT_DIR/$PFX-$SFX_MARKS"
92 # cleanup on exit
93 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
95 _err1=
96 _err2=
97 exec 3>&1
98 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
100 exec 4>&3 3>&1 1>&4 4>&-
102 _e1=0
103 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
104 --repo "$REPO" \
105 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
106 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
107 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
108 --status "$GIT_DIR/$PFX-$SFX_STATE" \
109 "$@" 3>&- || _e1=$?
110 echo $_e1 >&3
111 } | \
113 _e2=0
114 git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" 3>&- || _e2=$?
115 echo $_e2 >&3
119 exec 3>&-
120 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
122 # move recent marks cache out of the way...
123 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
124 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
125 else
126 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
129 # ...to create a new merged one
130 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
131 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
133 # save SHA1s of current heads for incremental imports
134 # and connectivity (plus sanity checking)
136 git branch --no-color | \
137 while IFS= read -r head; do
138 echo ":${head#??} $(git rev-parse "refs/heads/${head#??}")"
139 done > "$GIT_DIR/$PFX-$SFX_HEADS"