git-rebase--am.sh 2.39 KB
Newer Older
1 2
# This shell script fragment is sourced by git-rebase to implement
# its default, fast, patch-based, non-interactive mode.
3 4 5 6
#
# Copyright (c) 2010 Junio C Hamano.
#

7 8 9 10 11 12 13 14 15 16 17
# The whole contents of this file is run by dot-sourcing it from
# inside a shell function.  It used to be that "return"s we see
# below were not inside any function, and expected to return
# to the function that dot-sourced us.
#
# However, FreeBSD /bin/sh misbehaves on such a construct and
# continues to run the statements that follow such a "return".
# As a work-around, we introduce an extra layer of a function
# here, and immediately call it after defining it.
git_rebase__am () {

18 19
case "$action" in
continue)
20 21
	git am --resolved --resolvemsg="$resolvemsg" \
		${gpg_sign_opt:+"$gpg_sign_opt"} &&
22
	move_to_original_branch
23
	return
24 25
	;;
skip)
26
	git am --skip --resolvemsg="$resolvemsg" &&
27
	move_to_original_branch
28
	return
29 30 31
	;;
esac

32 33 34 35 36 37 38
if test -z "$rebase_root"
	# this is now equivalent to ! -z "$upstream"
then
	revisions=$upstream...$orig_head
else
	revisions=$onto...$orig_head
fi
39

40
ret=0
41 42 43 44 45 46
if test -n "$keep_empty"
then
	# we have to do this the hard way.  git format-patch completely squashes
	# empty commits and even if it didn't the format doesn't really lend
	# itself well to recording empty patches.  fortunately, cherry-pick
	# makes this easy
47
	git cherry-pick ${gpg_sign_opt:+"$gpg_sign_opt"} --allow-empty \
48 49
		--right-only "$revisions" \
		${restrict_revision+^$restrict_revision}
50
	ret=$?
51
else
52 53
	rm -f "$GIT_DIR/rebased-patches"

54
	git format-patch -k --stdout --full-index --cherry-pick --right-only \
55
		--src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter \
56 57
		"$revisions" ${restrict_revision+^$restrict_revision} \
		>"$GIT_DIR/rebased-patches"
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	ret=$?

	if test 0 != $ret
	then
		rm -f "$GIT_DIR/rebased-patches"
		case "$head_name" in
		refs/heads/*)
			git checkout -q "$head_name"
			;;
		*)
			git checkout -q "$orig_head"
			;;
		esac

		cat >&2 <<-EOF

		git encountered an error while preparing the patches to replay
		these revisions:

		    $revisions

		As a result, git cannot rebase them.
		EOF
81
		return $?
82 83
	fi

84 85
	git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" \
		${gpg_sign_opt:+"$gpg_sign_opt"} <"$GIT_DIR/rebased-patches"
86 87 88 89 90 91 92 93
	ret=$?

	rm -f "$GIT_DIR/rebased-patches"
fi

if test 0 != $ret
then
	test -d "$state_dir" && write_basic_state
94
	return $ret
95
fi
96

97
move_to_original_branch
98 99 100 101

}
# ... and then we call the whole thing.
git_rebase__am