annotate extra/fast-export/hg-fast-export.sh @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents 3ad53f43483d
children
rev   line source
chris@1544 1 #!/bin/sh
chris@1544 2
chris@1544 3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
chris@1544 4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
chris@1544 5
chris@1544 6 ROOT="$(dirname "$(which "$0")")"
chris@1544 7 REPO=""
chris@1544 8 PFX="hg2git"
chris@1544 9 SFX_MAPPING="mapping"
chris@1544 10 SFX_MARKS="marks"
chris@1544 11 SFX_HEADS="heads"
chris@1544 12 SFX_STATE="state"
chris@1544 13 GFI_OPTS=""
chris@1544 14 PYTHON=${PYTHON:-python}
chris@1544 15
chris@1544 16 USAGE="[--quiet] [-r <repo>] [--force] [-m <max>] [-s] [--hgtags] [-A <file>] [-B <file>] [-T <file>] [-M <name>] [-o <name>] [--hg-hash] [-e <encoding>]"
chris@1544 17 LONG_USAGE="Import hg repository <repo> up to either tip or <max>
chris@1544 18 If <repo> is omitted, use last hg repository as obtained from state file,
chris@1544 19 GIT_DIR/$PFX-$SFX_STATE by default.
chris@1544 20
chris@1544 21 Note: The argument order matters.
chris@1544 22
chris@1544 23 Options:
chris@1544 24 --quiet Passed to git-fast-import(1)
chris@1544 25 -r <repo> Mercurial repository to import
chris@1544 26 --force Ignore validation errors when converting, and pass --force
chris@1544 27 to git-fast-import(1)
chris@1544 28 -m <max> Maximum revision to import
chris@1544 29 -s Enable parsing Signed-off-by lines
chris@1544 30 --hgtags Enable exporting .hgtags files
chris@1544 31 -A <file> Read author map from file
chris@1544 32 (Same as in git-svnimport(1) and git-cvsimport(1))
chris@1544 33 -B <file> Read branch map from file
chris@1544 34 -T <file> Read tags map from file
chris@1544 35 -M <name> Set the default branch name (defaults to 'master')
chris@1544 36 -o <name> Use <name> as branch namespace to track upstream (eg 'origin')
chris@1544 37 --hg-hash Annotate commits with the hg hash as git notes in the
chris@1544 38 hg namespace.
chris@1544 39 -e <encoding> Assume commit and author strings retrieved from
chris@1544 40 Mercurial are encoded in <encoding>
chris@1544 41 --fe <filename_encoding> Assume filenames from Mercurial are encoded
chris@1544 42 in <filename_encoding>
chris@1544 43 "
chris@1544 44 case "$1" in
chris@1544 45 -h|--help)
chris@1544 46 echo "usage: $(basename "$0") $USAGE"
chris@1544 47 echo ""
chris@1544 48 echo "$LONG_USAGE"
chris@1544 49 exit 0
chris@1544 50 esac
Chris@1567 51
Chris@1567 52 IS_BARE=$(git rev-parse --is-bare-repository) \
Chris@1567 53 || (echo "Could not find git repo" ; exit 1)
Chris@1567 54 if test "z$IS_BARE" != ztrue; then
Chris@1567 55 # This is not a bare repo, cd to the toplevel
Chris@1567 56 TOPLEVEL=$(git rev-parse --show-toplevel) \
Chris@1567 57 || (echo "Could not find git repo toplevel" ; exit 1)
Chris@1567 58 cd $TOPLEVEL || exit 1
Chris@1567 59 fi
Chris@1567 60 GIT_DIR=$(git rev-parse --git-dir) || (echo "Could not find git repo" ; exit 1)
chris@1544 61
chris@1544 62 while case "$#" in 0) break ;; esac
chris@1544 63 do
chris@1544 64 case "$1" in
chris@1544 65 -r|--r|--re|--rep|--repo)
chris@1544 66 shift
chris@1544 67 REPO="$1"
chris@1544 68 ;;
chris@1544 69 --q|--qu|--qui|--quie|--quiet)
chris@1544 70 GFI_OPTS="$GFI_OPTS --quiet"
chris@1544 71 ;;
chris@1544 72 --force)
chris@1544 73 # pass --force to git-fast-import and hg-fast-export.py
chris@1544 74 GFI_OPTS="$GFI_OPTS --force"
chris@1544 75 break
chris@1544 76 ;;
chris@1544 77 -*)
chris@1544 78 # pass any other options down to hg2git.py
chris@1544 79 break
chris@1544 80 ;;
chris@1544 81 *)
chris@1544 82 break
chris@1544 83 ;;
chris@1544 84 esac
chris@1544 85 shift
chris@1544 86 done
chris@1544 87
chris@1544 88 # for convenience: get default repo from state file
chris@1544 89 if [ x"$REPO" = x -a -f "$GIT_DIR/$PFX-$SFX_STATE" ] ; then
chris@1544 90 REPO="`grep '^:repo ' "$GIT_DIR/$PFX-$SFX_STATE" | cut -d ' ' -f 2`"
chris@1544 91 echo "Using last hg repository \"$REPO\""
chris@1544 92 fi
chris@1544 93
chris@1544 94 if [ -z "$REPO" ]; then
chris@1544 95 echo "no repo given, use -r flag"
chris@1544 96 exit 1
chris@1544 97 fi
chris@1544 98
chris@1544 99 # make sure we have a marks cache
chris@1544 100 if [ ! -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
chris@1544 101 touch "$GIT_DIR/$PFX-$SFX_MARKS"
chris@1544 102 fi
chris@1544 103
chris@1544 104 # cleanup on exit
chris@1544 105 trap 'rm -f "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp"' 0
chris@1544 106
chris@1544 107 _err1=
chris@1544 108 _err2=
chris@1544 109 exec 3>&1
chris@1544 110 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
chris@1544 111 $(
chris@1544 112 exec 4>&3 3>&1 1>&4 4>&-
chris@1544 113 {
chris@1544 114 _e1=0
chris@1544 115 GIT_DIR="$GIT_DIR" $PYTHON "$ROOT/hg-fast-export.py" \
chris@1544 116 --repo "$REPO" \
chris@1544 117 --marks "$GIT_DIR/$PFX-$SFX_MARKS" \
chris@1544 118 --mapping "$GIT_DIR/$PFX-$SFX_MAPPING" \
chris@1544 119 --heads "$GIT_DIR/$PFX-$SFX_HEADS" \
chris@1544 120 --status "$GIT_DIR/$PFX-$SFX_STATE" \
chris@1544 121 "$@" 3>&- || _e1=$?
chris@1544 122 echo $_e1 >&3
chris@1544 123 } | \
chris@1544 124 {
chris@1544 125 _e2=0
chris@1544 126 git fast-import $GFI_OPTS --export-marks="$GIT_DIR/$PFX-$SFX_MARKS.tmp" 3>&- || _e2=$?
chris@1544 127 echo $_e2 >&3
chris@1544 128 }
chris@1544 129 )
chris@1544 130 EOT
chris@1544 131 exec 3>&-
chris@1544 132 [ "$_err1" = 0 -a "$_err2" = 0 ] || exit 1
chris@1544 133
chris@1544 134 # move recent marks cache out of the way...
chris@1544 135 if [ -f "$GIT_DIR/$PFX-$SFX_MARKS" ] ; then
chris@1544 136 mv "$GIT_DIR/$PFX-$SFX_MARKS" "$GIT_DIR/$PFX-$SFX_MARKS.old"
chris@1544 137 else
chris@1544 138 touch "$GIT_DIR/$PFX-$SFX_MARKS.old"
chris@1544 139 fi
chris@1544 140
chris@1544 141 # ...to create a new merged one
chris@1544 142 cat "$GIT_DIR/$PFX-$SFX_MARKS.old" "$GIT_DIR/$PFX-$SFX_MARKS.tmp" \
chris@1544 143 | uniq > "$GIT_DIR/$PFX-$SFX_MARKS"
chris@1544 144
chris@1544 145 # save SHA1s of current heads for incremental imports
chris@1544 146 # and connectivity (plus sanity checking)
chris@1544 147 for head in `git branch | sed 's#^..##'` ; do
chris@1544 148 id="`git rev-parse refs/heads/$head`"
chris@1544 149 echo ":$head $id"
chris@1544 150 done > "$GIT_DIR/$PFX-$SFX_HEADS"
chris@1544 151
chris@1544 152 # check diff with color:
chris@1544 153 # ( for i in `find . -type f | grep -v '\.git'` ; do diff -u $i $REPO/$i ; done | cdiff ) | less -r