Revert "Make bashism conditional on the shell being bash"
[fast-export.git] / hg-fast-export.sh
blob7dd918cb9ea454a409b522ad0bc29a12b7d16889
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 set -o pipefail
8 ROOT="`dirname $0`"
9 REPO=""
10 PFX="hg2git"
11 SFX_MAPPING="mapping"
12 SFX_MARKS="marks"
13 SFX_HEADS="heads"
14 SFX_STATE="state"
15 GFI_OPTS=""
16 PYTHON=${PYTHON:-python}
18 USAGE="[--quiet] [-r <repo>] [--force] [-m <max>] [-s] [-A <file>] [-M <name>] [-o <name>]"
19 LONG_USAGE="Import hg repository <repo> up to either tip or <max>
20 If <repo> is omitted, use last hg repository as obtained from state file,
21 GIT_DIR/$PFX-$SFX_STATE by default.
23 Note: The argument order matters.
25 Options:
26 -m Maximum revision to import
27 --quiet Passed to git-fast-import(1)
28 -s Enable parsing Signed-off-by lines
29 -A Read author map from file
30 (Same as in git-svnimport(1) and git-cvsimport(1))
31 -r Mercurial repository to import
32 -M Set the default branch name (default to 'master')
33 -o Use <name> as branch namespace to track upstream (eg 'origin')
34 --force Ignore validation errors when converting, and pass --force
35 to git-fast-import(1)
38 . "$(git --exec-path)/git-sh-setup"
39 cd_to_toplevel
41 while case "$#" in 0) break ;; esac
43 case "$1" in
44 -r|--r|--re|--rep|--repo)
45 shift
46 REPO="$1"
48 --q|--qu|--qui|--quie|--quiet)
49 GFI_OPTS="$GFI_OPTS --quiet"
51 --force)
52 # pass --force to git-fast-import and hg-fast-export.py
53 GFI_OPTS="$GFI_OPTS --force"
54 break
56 -*)
57 # pass any other options down to hg2git.py
58 break
61 break
63 esac
64 shift
65 done
67 # for convenience: get default repo from state file
68 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
69 REPO="`egrep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
70 echo "Using last hg repository \"$REPO\""
73 if [ -z "$REPO" ]; then
74 echo "no repo given, use -r flag"
75 exit 1
78 # make sure we have a marks cache
79 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
80 touch "$GIT_DIR/$PFX-$SFX_MARKS"
83 # cleanup on exit
84 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
86 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
87 --repo "$REPO" \
88 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
89 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
90 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
91 --status "$GIT_DIR/$PFX-$SFX_STATE" \
92 "$@" \
93 | git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" || exit 1
95 # move recent marks cache out of the way...
96 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
97 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
98 else
99 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
102 # ...to create a new merged one
103 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
104 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
106 # save SHA1s of current heads for incremental imports
107 # and connectivity (plus sanity checking)
108 for head in `git branch | sed 's#^..##'` ; do
109 id="`git rev-parse $head`"
110 echo ":$head $id"
111 done > "$GIT_DIR/$PFX-$SFX_HEADS"
113 # check diff with color:
114 # ( for i in `find . -type f | grep -v '\.git'` ; do diff -u $i $REPO/$i ; done | cdiff ) | less -r