summaryrefslogtreecommitdiff
path: root/update-from-lore.sh
blob: 7842c6a87a72daaf50228b12131bd3141c7d1633 (plain)
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
#!/bin/sh

afresh () {
	URL="${1?URL}"
	shift
	if test $# != 0
	then
		branch="$1"
		shift
	fi
	if test $# = 1
	then
		git checkout --detach "$1"
		shift
	elif git symbolic-ref -q HEAD
	then
		echo >&2 "HEAD not detached"
		exit 1
	fi

	if test $# != 0
	then
		echo >&2 "$0 URL topic-name [base]"
		exit 1
	fi

	b4 am -o- -t "$URL" |
	tee ./+b4am.mbx |
	git am -s3 && rm -f ./+b4am.mbx
	status=$?

	if test $status = 0 && test -n "$branch"
	then
		git checkout -b "$branch"
	fi
	exit $?
}

redo_or_afresh () {
	if git show-ref --quiet --verify "refs/heads/$1"
	then
		git checkout "$1" || exit
		return
	else
		afresh "$@"
		return
	fi
}

force=
while case "$#" in 0) break;; esac
do
	case "$1" in
	-f | --force)
		force=--force ;;
	-*)
		echo >&2 "$0: unknown option '$1'"
		exit 1 ;;
	*)
		break ;;
	esac
	shift
done

case $# in
0)	: happy ;;
1|2|3)	redo_or_afresh "$@" ;;
*)	echo >&2 "$0: extra arguments" ;;
esac

above_master=$(git rev-list --first-parent master.. | wc -l)
above_next=$(git rev-list --first-parent next.. | wc -l)
if test "$above_master" != "$above_next"
then
	if test "$force" = "--force"
	then
		echo >&2 warning: some patches are already in next
	else
		echo >&2 fatal: some patches are already in next
		exit 1
	fi
fi

MID=
OLD=$(git rev-parse HEAD)
git detach ${force+--force} || exit
git rev-list HEAD..$OLD |
while read commit
do
	git notes --ref=amlog show $commit
done |
sed -n -e '/^Message-Id: /{
	s///p
	q
}' |
xargs -n1 b4 am -o- -T |
tee ./+b4am.mbx |
git am -s3 && rm -f ./+b4am.mbx &&

git range-diff --notes=amlog --crea=999 @{-1}...
echo -n >&2 "Update [y/n]? "
while read yesno
do
	case "$yesno" in
	[Yy]*)
		git co -B @{-1}
		exit 0
		;;
	[Nn]*)
		exit 1
		;;
	esac
done