hg-fast-export.sh/hg-reset.sh: replace egrep with grep
[fast-export.git] / hg-fast-export.sh
bloba21c61f8e13bcf35decc57a6035ac9fb4cde7e02
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>]"
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')
36 case "$1" in
37 -h|--help)
38 echo "usage: $(basename "$0") $USAGE"
39 echo ""
40 echo "$LONG_USAGE"
41 exit 0
42 esac
43 . "$(git --exec-path)/git-sh-setup"
44 cd_to_toplevel
46 while case "$#" in 0) break ;; esac
48 case "$1" in
49 -r|--r|--re|--rep|--repo)
50 shift
51 REPO="$1"
53 --q|--qu|--qui|--quie|--quiet)
54 GFI_OPTS="$GFI_OPTS --quiet"
56 --force)
57 # pass --force to git-fast-import and hg-fast-export.py
58 GFI_OPTS="$GFI_OPTS --force"
59 break
61 -*)
62 # pass any other options down to hg2git.py
63 break
66 break
68 esac
69 shift
70 done
72 # for convenience: get default repo from state file
73 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
74 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
75 echo "Using last hg repository \"$REPO\""
78 if [ -z "$REPO" ]; then
79 echo "no repo given, use -r flag"
80 exit 1
83 # make sure we have a marks cache
84 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
85 touch "$GIT_DIR/$PFX-$SFX_MARKS"
88 # cleanup on exit
89 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
91 _err1=
92 _err2=
93 exec 3>&1
94 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
96 exec 4>&3 3>&1 1>&4 4>&-
98 _e1=0
99 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
100 --repo "$REPO" \
101 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
102 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
103 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
104 --status "$GIT_DIR/$PFX-$SFX_STATE" \
105 "$@" 3>&- || _e1=$?
106 echo $_e1 >&3
107 } | \
109 _e2=0
110 git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" 3>&- || _e2=$?
111 echo $_e2 >&3
115 exec 3>&-
116 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
118 # move recent marks cache out of the way...
119 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
120 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
121 else
122 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
125 # ...to create a new merged one
126 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
127 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
129 # save SHA1s of current heads for incremental imports
130 # and connectivity (plus sanity checking)
131 for head in `git branch | sed 's#^..##'` ; do
132 id="`git rev-parse $head`"
133 echo ":$head $id"
134 done > "$GIT_DIR/$PFX-$SFX_HEADS"
136 # check diff with color:
137 # ( for i in `find . -type f | grep -v '\.git'` ; do diff -u $i $REPO/$i ; done | cdiff ) | less -r