hg-{fast-export,reset}.sh: add git --exec-path to PATH
[girocco-hg-fast-export.git] / hg-fast-export.sh
blob5f874349e1f7e878d1746631bb9247f4ece09e6a
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 PATH="$(git --exec-path):$PATH" && export PATH
49 . 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 # cleanup on exit
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"
98 notes=
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
104 nm='hg-fast-export'
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
109 else
110 # make sure we have a marks cache
111 >"$GIT_DIR/$PFX-$SFX_MARKS.old"
114 _err1=
115 _err2=
116 exec 3>&1
117 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
119 exec 4>&3 3>&1 1>&4 4>&-
121 _e1=0
122 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
123 --repo "$REPO" \
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" \
128 "$@" 3>&- || _e1=$?
129 echo $_e1 >&3
130 } | \
132 _e2=0
133 git fast-import \
134 --import-marks="$GIT_DIR/$PFX-$SFX_MARKS.old" \
135 --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.new" \
136 $GFI_OPTS 3>&- || _e2=$?
137 echo $_e2 >&3
141 exec 3>&-
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"