Update README with note about no working directory
[fast-export.git] / hg-fast-export.sh
blob9c1077c106f272fb3279881348fbcfac6b389d7a
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] [-A <file>] [-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 -A <file> Read author map from file
32 (Same as in git-svnimport(1) and git-cvsimport(1))
33 -M <name> Set the default branch name (defaults to 'master')
34 -o <name> Use <name> as branch namespace to track upstream (eg 'origin')
35 --hg-hash Annotate commits with the hg hash as git notes in the
36 hg namespace.
38 case "$1" in
39 -h|--help)
40 echo "usage: $(basename "$0") $USAGE"
41 echo ""
42 echo "$LONG_USAGE"
43 exit 0
44 esac
45 . "$(git --exec-path)/git-sh-setup"
46 cd_to_toplevel
48 while case "$#" in 0) break ;; esac
50 case "$1" in
51 -r|--r|--re|--rep|--repo)
52 shift
53 REPO="$1"
55 --q|--qu|--qui|--quie|--quiet)
56 GFI_OPTS="$GFI_OPTS --quiet"
58 --force)
59 # pass --force to git-fast-import and hg-fast-export.py
60 GFI_OPTS="$GFI_OPTS --force"
61 break
63 -*)
64 # pass any other options down to hg2git.py
65 break
68 break
70 esac
71 shift
72 done
74 # for convenience: get default repo from state file
75 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
76 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
77 echo "Using last hg repository \"$REPO\""
80 if [ -z "$REPO" ]; then
81 echo "no repo given, use -r flag"
82 exit 1
85 # make sure we have a marks cache
86 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
87 touch "$GIT_DIR/$PFX-$SFX_MARKS"
90 # cleanup on exit
91 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
93 _err1=
94 _err2=
95 exec 3>&1
96 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
98 exec 4>&3 3>&1 1>&4 4>&-
100 _e1=0
101 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
102 --repo "$REPO" \
103 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
104 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
105 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
106 --status "$GIT_DIR/$PFX-$SFX_STATE" \
107 "$@" 3>&- || _e1=$?
108 echo $_e1 >&3
109 } | \
111 _e2=0
112 git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" 3>&- || _e2=$?
113 echo $_e2 >&3
117 exec 3>&-
118 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
120 # move recent marks cache out of the way...
121 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
122 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
123 else
124 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
127 # ...to create a new merged one
128 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
129 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
131 # save SHA1s of current heads for incremental imports
132 # and connectivity (plus sanity checking)
133 for head in `git branch | sed 's#^..##'` ; do
134 id="`git rev-parse $head`"
135 echo ":$head $id"
136 done > "$GIT_DIR/$PFX-$SFX_HEADS"
138 # check diff with color:
139 # ( for i in `find . -type f | grep -v '\.git'` ; do diff -u $i $REPO/$i ; done | cdiff ) | less -r