forked from mozilla/moz-git-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-push-to-hg
executable file
·120 lines (97 loc) · 3.39 KB
/
git-push-to-hg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
set -e
# Push some commits from git to the git-temp qqueue in a given hg repository.
# Note that this nukes the git-temp qqueue, if it exists.
#
# If no commit range is specified, we push
#
# $(git merge-base origin/master HEAD)..HEAD.
#
# Otherwise, we push the commit(s) given.
PATH="$(dirname $0):$PATH"
$(dirname $0)/check-for-updates
# Read git version and note if it's less than 1.7.6, because older versions do
# not support git-format-patch --quiet properly.
function git_version() {
# Get the $1'th part of the git version.
git version | cut -f 3 -d ' ' | cut -f $1 -d '.'
}
# "Old git" means less than 1.7.5. Earlier versions do not work well with git
# diff --quiet. See https://github.com/jlebar/moz-git-tools/issues/1.
if (( ($(git_version 1) == 1 && $(git_version 2) == 7 && $(git_version 3) < 5) ||
($(git_version 1) == 1 && $(git_version 2) < 7) )); then
old_git=1
else
old_git=0
fi
push_to_tip=0
if [[ "$1" == "-t" || "$1" == "--tip" ]]; then
push_to_tip=1
shift
fi
hg_repo="$1"
if [[ "$1" == "" ]]; then
echo "Usage: $(basename $0) [-t/--tip] path-to-hg-repo [git-revs]" 1>& 2
exit 255
fi
revs="$2"
if [[ "$revs" == "" ]]; then
revs="$(git merge-base HEAD origin/master)..HEAD"
fi
# If revs doesn't contain "..", add "$revs^.." to the beginning.
# git-format-patch needs this, otherwise it'll format all patches since $revs.
if ! echo "$revs" | fgrep ".."; then
revs="$revs^..$revs"
fi
echo "On branch $(git branch | grep '^\*' | cut -f 2 -d ' ')."
git log --reverse --date-order --pretty=oneline --abbrev-commit $revs
git_status=$(git status --porcelain)
if [[ "$git_status" != "" ]]; then
echo ""
echo "Warning; tree is not clean!"
echo "$git_status"
read -sn1 -p "Press any key to continue, or <ctrl-c> to quit. "
echo ""
fi
function hg_cmd() {
#echo "hg "$@"" >&2
hg -R "$hg_repo" -q $@ > /dev/null
}
first_rev=$(echo "$revs" | sed -e 's/\.\..*//')
git_parent_rev=$(git merge-base $first_rev origin/master)
# Run git-to-hg-commit, and only run hg pull if it fails.
if [[ "$push_to_tip" == "0" ]]; then
hg_parent_rev=$(git-to-hg-commit "$hg_repo" $git_parent_rev 2> /dev/null || \
(hg_cmd pull && \
git-to-hg-commit "$hg_repo" $git_parent_rev 2> /dev/null || \
(echo "No matching commit found. (Try pushing with --tip.)" 1>& 2 && \
exit 2)))
else
hg_parent_rev="tip"
fi
hg_cmd up --rev "$hg_parent_rev"
# Run qpop -a only if there are patches applied, so we don't see "no patches
# applied". (We could qpop -a and ignore stderr, but then we could ignore a
# real error!)
if [[ $(hg -R "$hg_repo" qapplied) != "" ]]; then
hg_cmd qpop -a
fi
# Switch to the patches queue (which we assume exists) so we can delete
# git-temp.
hg_cmd qqueue patches
hg_cmd qqueue -q --purge git-temp || true
hg_cmd qqueue -q --create git-temp
# Pass --quiet only for not-old-git.
if [[ "$old_git" == 0 ]]; then
git_format_quiet='--quiet'
fi
git format-patch $git_format_quiet -M -C -pk $revs -o ""$hg_repo"/.hg/patches-git-temp"
pushd ""$hg_repo"/.hg/patches-git-temp" > /dev/null
find . -name '*.patch' | sort -g > series
# There might not be any patches here, in which case git-patch-to-hg-patch
# will hang!
if [[ "$(cat series)" != "" ]]; then
git-patch-to-hg-patch $(cat series)
fi
popd > /dev/null
hg_cmd qpush -a