-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ff-to
44 lines (36 loc) · 1.22 KB
/
git-ff-to
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
#!/bin/bash
if [ $# -eq 0 ] || [ "$1" == "--help" ] || [ "$1" == "-?" ]; then
cat <<USAGE_HERE_DOCUMENT
usage: git ff-to [merge-options] <commitish>
Fast forwards the current branch one commit towards the specified commit.
Given the following history:
A--B--C--D--E
\--X--/
If HEAD is at commit A, then:
$ git ff-to E # HEAD == B
$ git ff-to E # HEAD == C
$ git ff-to E # HEAD == D
$ git ff-to E # HEAD == E
USAGE_HERE_DOCUMENT
exit
fi
MERGE_ARG_LEN=$(($#-1))
MERGE_ARGS=${@:1:$MERGE_ARG_LEN}
TARGET=${@:$#}
CURREV=$(git rev-parse --revs-only --short HEAD) || exit
echo "# Currently at: $(git log -n 1 --oneline --decorate HEAD)"
TARGETREV=$(git rev-parse --revs-only --short "$TARGET") || exit
echo "# FF towards: $(git log -n 1 --oneline --decorate $TARGET)"
if [ "$CURREV" == "$TARGETREV" ]; then
echo "Already at target commit!" 1>&2
exit
fi
NEXTCOMMIT=$(git next-commit "$TARGET")
if [ $? -ne 0 ] || [ -z $NEXTCOMMIT ]; then
echo "Could not determine next commit!" 1>&2
exit
fi
echo "# Next commit: $(git log -n 1 --oneline --decorate $NEXTCOMMIT)"
if [ -z $MERGEARGS ]; then git merge --ff-only $NEXTCOMMIT
else git merge --ff-only $MERGE_ARGS $NEXTCOMMIT
fi