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)"
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.
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
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
43 echo "usage: $(basename "$0") $USAGE"
48 PATH
="$(git --exec-path):$PATH" && export PATH
52 while case "$#" in 0) break ;; esac
55 -r|
--r|
--re|
--rep|
--repo)
59 --q|
--qu|
--qui|
--quie|
--quiet)
60 GFI_OPTS
="$GFI_OPTS --quiet"
63 # pass --force to git-fast-import and hg-fast-export.py
64 GFI_OPTS
="$GFI_OPTS --force"
68 # pass any other options down to hg2git.py
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"
90 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.new"' 0
92 rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.new"
93 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" -a -s "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
94 # remove any marks that are no longer valid (e.g. gc'd "unnamed" heads)
95 LC_ALL
=C
sed 's/^:\([^ ][^ ]*\) \([^ ][^ ]*\)$/\2 \1/' <"$GIT_DIR/$PFX-$SFX_MARKS" | \
96 git cat-file
--batch-check=':%(rest) %(objectname)' | \
97 LC_ALL
=C
sed '/ missing$/d' >"$GIT_DIR/$PFX-$SFX_MARKS.old"
99 for arg
; do if [ "$arg" = "--hg-hash" ]; then notes
=1; break; fi; done
100 # for --hg-hash run "notes prune" on refs/notes/hg if it exists and any marks were missing
101 if [ -n "$notes" ] && \
102 git rev-parse
--quiet --verify refs
/notes
/hg
>/dev
/null
&& \
103 ! LC_ALL
=C
cmp -s "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"; then
105 GIT_AUTHOR_NAME
="$nm" GIT_COMMITTER_NAME
="$nm" \
106 GIT_AUTHOR_EMAIL
="$nm" GIT_COMMITTER_EMAIL
="$nm" \
107 git notes
--ref=refs
/notes
/hg prune
110 # make sure we have a marks cache
111 >"$GIT_DIR/$PFX-$SFX_MARKS.old"
117 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
119 exec 4>&3 3>&1 1>&4 4>&-
122 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
124 --marks "$GIT_DIR/$PFX-$SFX_MARKS.old" \
125 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
126 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
127 --status "$GIT_DIR/$PFX-$SFX_STATE" \
134 --import-marks="$GIT_DIR/$PFX-$SFX_MARKS.old" \
135 --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.new" \
136 $GFI_OPTS 3>&- || _e2=$?
142 [ "$_err1" = 0 -a "$_err2" = 0 ] ||
exit 1
143 mv -f "$GIT_DIR/$PFX-$SFX_MARKS.new" "$GIT_DIR/$PFX-$SFX_MARKS"
144 rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old"
146 # save SHA1s of current heads for incremental imports
147 # and connectivity (plus sanity checking)
149 git for-each-ref
--format='%(refname) %(objectname)' refs
/heads | \
150 LC_ALL
=C
sed -e 's,^refs/heads/,:,' > "$GIT_DIR/$PFX-$SFX_HEADS"