hg-fast-export.py: include unnamed head's branch
[fast-export/rorcz.git] / hg-fast-export.sh
blob54c627c98a802bbeb40f3f14513eb490b24f0e73
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="$(cd "$(dirname "$0")" && pwd -P)"
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 'unknown'
35 (previous default was 'devnull@localhost')
36 -M <name> Set the default branch name (defaults to 'master')
37 -o <name> Use <name> as branch namespace to track upstream (eg 'origin')
38 --hg-hash Annotate commits with the hg hash as git notes in the
39 hg namespace.
41 case "$1" in
42 -h|--help)
43 echo "usage: $(basename "$0") $USAGE"
44 echo ""
45 echo "$LONG_USAGE"
46 exit 0
47 esac
48 . "$(git --exec-path)/git-sh-setup"
49 cd_to_toplevel
51 while case "$#" in 0) break ;; esac
53 case "$1" in
54 -r|--r|--re|--rep|--repo)
55 shift
56 REPO="$1"
58 --q|--qu|--qui|--quie|--quiet)
59 GFI_OPTS="$GFI_OPTS --quiet"
61 --force)
62 # pass --force to git-fast-import and hg-fast-export.py
63 GFI_OPTS="$GFI_OPTS --force"
64 break
66 -*)
67 # pass any other options down to hg2git.py
68 break
71 break
73 esac
74 shift
75 done
77 # for convenience: get default repo from state file
78 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
79 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
80 echo "Using last hg repository \"$REPO\""
83 if [ -z "$REPO" ]; then
84 echo "no repo given, use -r flag"
85 exit 1
88 # make sure we have a marks cache
89 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
90 touch "$GIT_DIR/$PFX-$SFX_MARKS"
92 rm -f "$GIT_DIR/$PFX-$SFX_MARKS.new"
94 # cleanup on exit
95 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.new"' 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 \
117 --import-marks="$GIT_DIR/$PFX-$SFX_MARKS" \
118 --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.new" \
119 $GFI_OPTS 3>&- || _e2=$?
120 echo $_e2 >&3
124 exec 3>&-
125 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
126 mv -f "$GIT_DIR/$PFX-$SFX_MARKS.new" "$GIT_DIR/$PFX-$SFX_MARKS"
128 # save SHA1s of current heads for incremental imports
129 # and connectivity (plus sanity checking)
131 git for-each-ref --format='%(refname) %(objectname)' refs/heads | \
132 LC_ALL=C sed -e 's,^refs/heads/,:,' > "$GIT_DIR/$PFX-$SFX_HEADS"