2010-01-26 11:39:36 +00:00
|
|
|
#
|
2010-01-26 21:18:36 +00:00
|
|
|
# git-flow -- A collection of Git extensions to provide high-level
|
|
|
|
# repository operations for Vincent Driessen's branching model.
|
2010-01-26 11:39:36 +00:00
|
|
|
#
|
|
|
|
# Original blog post presenting this model is found at:
|
|
|
|
# http://nvie.com/archives/323
|
|
|
|
#
|
|
|
|
# Feel free to contribute to this project at:
|
|
|
|
# http://github.com/nvie/gitflow
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 by Vincent Driessen
|
|
|
|
# Copyright (c) 2010 by Benedikt Böhm
|
|
|
|
#
|
|
|
|
|
2010-01-27 23:51:15 +00:00
|
|
|
PREFIX=$(git config --get gitflow.prefix.feature || echo feature/)
|
|
|
|
|
2010-01-26 11:39:36 +00:00
|
|
|
usage() {
|
2010-02-01 21:00:39 +00:00
|
|
|
echo "usage: git flow feature [list] [-v]"
|
|
|
|
echo " git flow feature start [-Fv] <name> [<base>]"
|
|
|
|
echo " git flow feature finish [-rsFv] <name|nameprefix> [<base>]"
|
2010-01-27 22:48:39 +00:00
|
|
|
echo " git flow feature publish <name>"
|
|
|
|
echo " git flow feature track <name>"
|
2010-01-29 15:56:29 +00:00
|
|
|
echo " git flow feature diff <name|nameprefix>"
|
2010-01-26 11:39:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 22:48:39 +00:00
|
|
|
cmd_default() {
|
2010-01-28 00:01:53 +00:00
|
|
|
cmd_list "$@"
|
2010-01-26 11:39:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-30 15:28:20 +00:00
|
|
|
max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }
|
|
|
|
|
2010-01-28 00:01:53 +00:00
|
|
|
cmd_list() {
|
2010-02-01 14:51:43 +00:00
|
|
|
DEFINE_boolean verbose 0 'verbose (more) output' v
|
|
|
|
parse_args "$@"
|
|
|
|
|
2010-01-27 22:48:39 +00:00
|
|
|
FEATURE_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
|
|
|
|
if [ -z "$FEATURE_BRANCHES" ]; then
|
|
|
|
warn "No feature branches exist."
|
|
|
|
exit 0
|
|
|
|
fi
|
2010-02-01 14:57:48 +00:00
|
|
|
|
2010-02-01 18:43:46 +00:00
|
|
|
CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
|
2010-02-01 14:57:48 +00:00
|
|
|
SHORT_NAMES=$(echo "$FEATURE_BRANCHES" | sed "s?^$PREFIX??g")
|
2010-02-01 18:43:46 +00:00
|
|
|
# determine column width first
|
|
|
|
width=0
|
|
|
|
for branch in $SHORT_NAMES; do
|
|
|
|
len=$(($(echo "$branch" | wc -c) - 1))
|
|
|
|
width=$(max $width $len)
|
|
|
|
done
|
|
|
|
width=$(($width + 3))
|
2010-01-30 15:28:20 +00:00
|
|
|
|
2010-02-01 18:43:46 +00:00
|
|
|
for branch in $SHORT_NAMES; do
|
|
|
|
fullname="$PREFIX$branch"
|
|
|
|
base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
|
|
|
|
develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
|
|
|
|
branch_sha=$(git rev-parse "$fullname")
|
|
|
|
if [ "$fullname" = "$CURRENT_BRANCH" ]; then
|
|
|
|
printf "* "
|
|
|
|
else
|
|
|
|
printf " "
|
|
|
|
fi
|
|
|
|
if [ $FLAGS_verbose -eq 1 ]; then
|
2010-01-30 15:28:20 +00:00
|
|
|
printf "%-${width}s" "$branch"
|
2010-02-01 14:57:48 +00:00
|
|
|
if [ "$branch_sha" = "$develop_sha" ]; then
|
|
|
|
printf "(no commits yet)"
|
|
|
|
elif [ "$base" = "$branch_sha" ]; then
|
|
|
|
printf "(is behind develop, may ff)"
|
|
|
|
elif [ "$base" = "$develop_sha" ]; then
|
|
|
|
printf "(based on latest develop)"
|
|
|
|
else
|
|
|
|
printf "(may be rebased)"
|
|
|
|
fi
|
2010-02-01 18:43:46 +00:00
|
|
|
else
|
|
|
|
printf "%s" "$branch"
|
|
|
|
fi
|
|
|
|
echo
|
|
|
|
done
|
2010-01-27 22:48:39 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 11:39:36 +00:00
|
|
|
cmd_help() {
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2010-01-29 15:43:37 +00:00
|
|
|
resolve_name_by_prefix() {
|
2010-01-29 14:18:13 +00:00
|
|
|
# first, check if there is a perfect match
|
2010-01-29 15:43:37 +00:00
|
|
|
if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
|
|
|
|
echo "$1"
|
|
|
|
return 0
|
2010-01-29 14:18:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
|
|
|
|
NUM_MATCHES=$(echo "$MATCHES" | wc -l)
|
2010-02-01 14:51:43 +00:00
|
|
|
if [ -z "$MATCHES" ]; then
|
2010-01-29 14:18:13 +00:00
|
|
|
# no prefix match, so take it literally
|
|
|
|
echo "$1"
|
|
|
|
else
|
2010-02-01 14:51:43 +00:00
|
|
|
if [ $NUM_MATCHES -eq 1 ]; then
|
|
|
|
# sed arg looks a bit weird, but $PREFIX should not contain spaces,
|
|
|
|
# so this one is safe
|
|
|
|
echo "$MATCHES" | sed "s $PREFIX g"
|
|
|
|
else
|
|
|
|
# multiple matches, cannot decide
|
|
|
|
warn "Multiple branches match for prefix '$1':"
|
|
|
|
for match in $MATCHES; do
|
|
|
|
warn "- $match"
|
|
|
|
done
|
|
|
|
die "Aborting. Use an unambiguous prefix."
|
|
|
|
fi
|
2010-01-29 14:18:13 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-02-01 14:51:43 +00:00
|
|
|
require_name() {
|
|
|
|
if [ "$NAME" = "" ]; then
|
|
|
|
echo "Missing argument <name>"
|
|
|
|
usage
|
2010-01-29 14:18:13 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-02-01 14:51:43 +00:00
|
|
|
expand_name_arg_prefix() {
|
|
|
|
require_name
|
|
|
|
NAME=$(resolve_name_by_prefix "$NAME")
|
|
|
|
if echo "$NAME" | grep -q "^[ \t\n\r]+$"; then
|
2010-01-29 14:18:13 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
BRANCH=$PREFIX$NAME
|
|
|
|
}
|
|
|
|
|
2010-01-29 15:56:29 +00:00
|
|
|
parse_args() {
|
2010-02-01 14:51:43 +00:00
|
|
|
# parse options
|
|
|
|
FLAGS "$@" || exit $?
|
|
|
|
eval set -- "${FLAGS_ARGV}"
|
|
|
|
|
|
|
|
# read arguments into global variables
|
2010-01-28 00:01:53 +00:00
|
|
|
NAME="$1"
|
2010-02-01 14:51:43 +00:00
|
|
|
BASE="${2:-$DEVELOP_BRANCH}"
|
2010-02-01 20:58:37 +00:00
|
|
|
BRANCH=$PREFIX$NAME
|
2010-01-28 00:01:53 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 11:39:36 +00:00
|
|
|
cmd_start() {
|
2010-02-01 14:51:43 +00:00
|
|
|
DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
|
2010-01-29 15:56:29 +00:00
|
|
|
parse_args "$@"
|
2010-02-01 14:51:43 +00:00
|
|
|
require_name
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# sanity checks
|
2010-01-29 09:30:40 +00:00
|
|
|
gitflow_require_clean_working_tree
|
2010-01-26 11:39:36 +00:00
|
|
|
gitflow_require_branch_absent $BRANCH
|
2010-01-29 11:10:44 +00:00
|
|
|
|
|
|
|
# update the local repo with remote changes, if asked
|
2010-02-01 14:51:43 +00:00
|
|
|
if [ $FLAGS_fetch -eq 1 ]; then
|
2010-01-26 13:46:56 +00:00
|
|
|
git fetch -q $ORIGIN $DEVELOP_BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
fi
|
|
|
|
|
2010-01-29 11:10:44 +00:00
|
|
|
gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
|
|
|
|
|
2010-01-26 11:39:36 +00:00
|
|
|
# create branch
|
|
|
|
git checkout -b $BRANCH $BASE
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Summary of actions:"
|
|
|
|
echo "- A new branch '$BRANCH' was created, based on '$BASE'"
|
|
|
|
echo "- You are now on branch '$BRANCH'"
|
|
|
|
echo ""
|
|
|
|
echo "Now, start committing on your feature. When done, use:"
|
|
|
|
echo ""
|
|
|
|
echo " git flow finish feature $NAME"
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_finish() {
|
2010-02-01 14:51:43 +00:00
|
|
|
DEFINE_boolean fetch 0 'fetch from origin before performing local operation' F
|
|
|
|
DEFINE_boolean rebase 0 'rebase instead of merge' r
|
|
|
|
DEFINE_boolean squash 0 'squash all commits when rebasing (implies --rebase)' s
|
|
|
|
parse_args "$@"
|
|
|
|
expand_name_arg_prefix
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# sanity checks
|
|
|
|
gitflow_require_branch $BRANCH
|
2010-01-29 11:10:44 +00:00
|
|
|
|
2010-01-28 11:40:33 +00:00
|
|
|
# detect if we're restoring from a merge conflict
|
|
|
|
if [ -f "$GIT_DIR/.gitflow/MERGE_BASE" ]; then
|
|
|
|
#
|
|
|
|
# TODO: detect that we're working on the correct branch here!
|
|
|
|
# The user need not necessarily have given the same $NAME twice here
|
|
|
|
# (although he/she should).
|
|
|
|
#
|
2010-01-29 09:28:08 +00:00
|
|
|
|
|
|
|
# TODO: gitflow_test_clean_working_tree() should provide an alternative
|
|
|
|
# exit code for "unmerged changes in working tree", which we should
|
|
|
|
# actually be testing for here
|
|
|
|
if gitflow_test_clean_working_tree; then
|
|
|
|
FINISH_BASE="$(cat "$GIT_DIR/.gitflow/MERGE_BASE")"
|
|
|
|
|
|
|
|
# Since the working tree is now clean, either the user did a
|
|
|
|
# succesfull merge manually, or the merge was cancelled.
|
|
|
|
# We detect this using gitflow_is_branch_merged_into()
|
|
|
|
if gitflow_is_branch_merged_into $BRANCH $FINISH_BASE; then
|
|
|
|
rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
|
|
|
|
helper_finish_cleanup
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
# If the user cancelled the merge and decided to wait until later,
|
|
|
|
# that's fine. But we have to acknowledge this by removing the
|
|
|
|
# MERGE_BASE file and continuing normal execution of the finish
|
|
|
|
rm -f "$GIT_DIR/.gitflow/MERGE_BASE"
|
|
|
|
fi
|
2010-01-28 11:40:33 +00:00
|
|
|
else
|
|
|
|
echo
|
|
|
|
echo "Merge conflicts not resolved yet, use:"
|
|
|
|
echo " git mergetool"
|
|
|
|
echo " git commit"
|
|
|
|
echo
|
|
|
|
echo "You can then complete the finish by running it again:"
|
|
|
|
echo " git flow feature finish $NAME"
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-01-29 09:28:08 +00:00
|
|
|
# sanity checks
|
|
|
|
gitflow_require_clean_working_tree
|
|
|
|
|
2010-01-29 11:10:44 +00:00
|
|
|
# update local repo with remote changes first, if asked
|
2010-02-01 14:51:43 +00:00
|
|
|
if [ $FLAGS_fetch -eq 1 ]; then
|
2010-01-29 11:10:44 +00:00
|
|
|
git fetch -q $ORIGIN $BRANCH
|
|
|
|
fi
|
|
|
|
|
2010-01-26 12:05:05 +00:00
|
|
|
if has $ORIGIN/$BRANCH $REMOTE_BRANCHES; then
|
|
|
|
gitflow_require_branches_equal $BRANCH $ORIGIN/$BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
fi
|
2010-01-26 11:59:27 +00:00
|
|
|
if [ "$BASE" = "$DEVELOP_BRANCH" ]; then
|
2010-01-26 12:05:05 +00:00
|
|
|
gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# merge into BASE
|
|
|
|
git checkout $BASE
|
|
|
|
if [ "$(git rev-list -n2 $BASE..$BRANCH | wc -l)" = "1" ]; then
|
|
|
|
git merge --ff $BRANCH
|
|
|
|
else
|
|
|
|
git merge --no-ff $BRANCH
|
|
|
|
fi
|
|
|
|
|
2010-01-28 11:40:33 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
# oops.. we have a merge conflict!
|
|
|
|
# write the given $BASE to a temporary file (we need it later)
|
2010-01-29 09:28:08 +00:00
|
|
|
mkdir -p "$GIT_DIR/.gitflow"
|
2010-01-28 11:40:33 +00:00
|
|
|
echo "$BASE" > "$GIT_DIR/.gitflow/MERGE_BASE"
|
|
|
|
echo
|
|
|
|
echo "There were merge conflicts. To resolve the merge conflict manually, use:"
|
|
|
|
echo " git mergetool"
|
|
|
|
echo " git commit"
|
|
|
|
echo
|
|
|
|
echo "You can then complete the finish by running it again:"
|
|
|
|
echo " git flow feature finish $NAME"
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# when no merge conflict is detected, just clean up the feature branch
|
|
|
|
helper_finish_cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
helper_finish_cleanup() {
|
2010-01-29 09:28:08 +00:00
|
|
|
# sanity checks
|
|
|
|
gitflow_require_branch $BRANCH
|
2010-01-29 16:36:21 +00:00
|
|
|
gitflow_require_clean_working_tree
|
2010-01-29 09:28:08 +00:00
|
|
|
|
2010-01-26 11:39:36 +00:00
|
|
|
# delete branch
|
2010-01-26 12:05:05 +00:00
|
|
|
git push $ORIGIN :refs/heads/$BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
git branch -d $BRANCH
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Summary of actions:"
|
|
|
|
echo "- The feature branch '$BRANCH' was merged into '$BASE'"
|
|
|
|
#echo "- Merge conflicts were resolved" # TODO: Add this line when it's supported
|
|
|
|
echo "- Feature branch '$BRANCH' has been removed"
|
|
|
|
echo "- You are now on branch '$BASE'"
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_publish() {
|
2010-01-29 15:56:29 +00:00
|
|
|
parse_args "$@"
|
2010-02-01 14:51:43 +00:00
|
|
|
expand_name_arg_prefix
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# sanity checks
|
2010-01-29 09:30:40 +00:00
|
|
|
gitflow_require_clean_working_tree
|
2010-01-26 11:39:36 +00:00
|
|
|
gitflow_require_branch $BRANCH
|
2010-01-26 13:46:56 +00:00
|
|
|
git fetch -q $ORIGIN
|
2010-01-26 12:05:05 +00:00
|
|
|
gitflow_require_branch_absent $ORIGIN/$BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# create remote branch
|
2010-01-26 12:05:05 +00:00
|
|
|
git push $ORIGIN $BRANCH:refs/heads/$BRANCH
|
2010-01-26 13:46:56 +00:00
|
|
|
git fetch -q $ORIGIN
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# configure remote tracking
|
2010-01-26 12:05:05 +00:00
|
|
|
git config branch.$BRANCH.remote $ORIGIN
|
2010-01-26 11:39:36 +00:00
|
|
|
git config branch.$BRANCH.merge refs/heads/$BRANCH
|
|
|
|
git checkout $BRANCH
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Summary of actions:"
|
|
|
|
echo "- A new remote branch '$BRANCH' was created"
|
|
|
|
echo "- The local branch '$BRANCH' was configured to track the remote branch"
|
|
|
|
echo "- You are now on branch '$BRANCH'"
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_track() {
|
2010-01-29 15:56:29 +00:00
|
|
|
parse_args "$@"
|
2010-02-01 14:51:43 +00:00
|
|
|
require_name
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# sanity checks
|
2010-01-29 09:30:40 +00:00
|
|
|
gitflow_require_clean_working_tree
|
2010-01-26 11:39:36 +00:00
|
|
|
gitflow_require_branch_absent $BRANCH
|
2010-01-26 13:46:56 +00:00
|
|
|
git fetch -q $ORIGIN
|
2010-01-26 12:05:05 +00:00
|
|
|
gitflow_require_branch $ORIGIN/$BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
# create tracking branch
|
2010-01-26 12:05:05 +00:00
|
|
|
git checkout -b $BRANCH $ORIGIN/$BRANCH
|
2010-01-26 11:39:36 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Summary of actions:"
|
|
|
|
echo "- A new remote tracking branch '$BRANCH' was created"
|
|
|
|
echo "- You are now on branch '$BRANCH'"
|
|
|
|
echo
|
|
|
|
}
|
2010-01-27 12:57:15 +00:00
|
|
|
|
|
|
|
cmd_diff() {
|
2010-02-01 14:51:43 +00:00
|
|
|
parse_args "$@"
|
2010-02-02 05:29:51 +00:00
|
|
|
CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
|
|
|
|
if [ "$NAME" != "" ]; then
|
|
|
|
expand_name_arg_prefix
|
|
|
|
elif [ "$CURRENT_BRANCH" != "" ]; then
|
|
|
|
BRANCH="$CURRENT_BRANCH"
|
|
|
|
else
|
|
|
|
warn "The current HEAD is no feature branch."
|
|
|
|
warn "To diff a feature, specify a <name> argument."
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
2010-02-01 14:51:43 +00:00
|
|
|
|
2010-01-27 12:57:15 +00:00
|
|
|
# TODO: if this feature has been based on a non-develop branch, we really
|
|
|
|
# should not be comparing to $DEVELOP. How to deal with this?
|
2010-01-28 15:29:34 +00:00
|
|
|
git diff $DEVELOP_BRANCH..$BRANCH
|
2010-01-27 12:57:15 +00:00
|
|
|
}
|