7832d6efb9
git specific and git-flow specific functions: gitflow_current_branch -> git_current_branch gitflow_is_branch_merged_into -> git_is_branch_merged_into gitflow_local_branch_exists -> git_local_branch_exists gitflow_local_branches -> git_local_branches gitflow_remote_branches -> git_remote_branches gitflow_require_branch -> require_branch gitflow_require_branch_absent -> require_branch_absent gitflow_require_branches_equal -> require_branches_equal gitflow_require_clean_working_tree -> require_clean_working_tree gitflow_require_git_repo -> require_git_repo gitflow_require_git_repo -> require_git_repo gitflow_require_initialized -> require_gitflow_initialized gitflow_require_initialized -> require_gitflow_initialized gitflow_require_local_branch -> require_local_branch gitflow_require_remote_branch -> require_remote_branch gitflow_require_tag_absent -> require_tag_absent gitflow_tag_exists -> git_tag_exists gitflow_test_branches_equal -> git_compare_branches gitflow_test_clean_working_tree -> git_is_clean_working_tree resolve_nameprefix -> gitflow_resolve_nameprefix
379 lines
9.5 KiB
Text
379 lines
9.5 KiB
Text
#
|
|
# git-flow -- A collection of Git extensions to provide high-level
|
|
# repository operations for Vincent Driessen's branching model.
|
|
#
|
|
# 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
|
|
#
|
|
|
|
require_git_repo
|
|
require_gitflow_initialized
|
|
gitflow_load_settings
|
|
PREFIX=$(git config --get gitflow.prefix.feature)
|
|
|
|
usage() {
|
|
echo "usage: git flow feature [list] [-v]"
|
|
echo " git flow feature start [-Fv] <name> [<base>]"
|
|
echo " git flow feature finish [-rsFv] <name|nameprefix>"
|
|
echo " git flow feature publish <name>"
|
|
echo " git flow feature track <name>"
|
|
echo " git flow feature diff <name|nameprefix>"
|
|
echo " git flow feature rebase [-i] <name|nameprefix>"
|
|
}
|
|
|
|
cmd_default() {
|
|
cmd_list "$@"
|
|
}
|
|
|
|
cmd_list() {
|
|
DEFINE_boolean verbose false 'verbose (more) output' v
|
|
parse_args "$@"
|
|
|
|
local feature_branches
|
|
local current_branch
|
|
local short_names
|
|
feature_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
|
|
if [ -z "$feature_branches" ]; then
|
|
warn "No feature branches exist."
|
|
exit 0
|
|
fi
|
|
current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
|
|
short_names=$(echo "$feature_branches" | sed "s ^$PREFIX g")
|
|
|
|
# determine column width first
|
|
local width=0
|
|
local branch
|
|
for branch in $short_names; do
|
|
local len=${#branch}
|
|
width=$(max $width $len)
|
|
done
|
|
width=$(($width+3))
|
|
|
|
local branch
|
|
for branch in $short_names; do
|
|
local fullname=$PREFIX$branch
|
|
local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
|
|
local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
|
|
local branch_sha=$(git rev-parse "$fullname")
|
|
if [ "$fullname" = "$current_branch" ]; then
|
|
printf "* "
|
|
else
|
|
printf " "
|
|
fi
|
|
if flag verbose; then
|
|
printf "%-${width}s" "$branch"
|
|
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
|
|
else
|
|
printf "%s" "$branch"
|
|
fi
|
|
echo
|
|
done
|
|
}
|
|
|
|
cmd_help() {
|
|
usage
|
|
exit 0
|
|
}
|
|
|
|
require_name_arg() {
|
|
if [ "$NAME" = "" ]; then
|
|
warn "Missing argument <name>"
|
|
usage
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
expand_nameprefix_arg() {
|
|
require_name_arg
|
|
|
|
local expanded_name
|
|
local exitcode
|
|
expanded_name=$(gitflow_resolve_nameprefix "$NAME" "$PREFIX")
|
|
exitcode=$?
|
|
case $exitcode in
|
|
0) NAME=$expanded_name
|
|
BRANCH=$PREFIX$NAME
|
|
;;
|
|
*) exit 1 ;;
|
|
esac
|
|
}
|
|
|
|
expand_nameprefix_arg_or_current() {
|
|
if [ "$NAME" != "" ]; then
|
|
expand_nameprefix_arg
|
|
require_branch "$PREFIX$NAME"
|
|
else
|
|
local current_branch=$(git_current_branch)
|
|
if startswith "$current_branch" "$PREFIX"; then
|
|
BRANCH=$current_branch
|
|
NAME=${BRANCH#$PREFIX}
|
|
else
|
|
warn "The current HEAD is no feature branch."
|
|
warn "To diff a feature, specify a <name> argument."
|
|
usage
|
|
exit 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
parse_args() {
|
|
# parse options
|
|
FLAGS "$@" || exit $?
|
|
eval set -- "${FLAGS_ARGV}"
|
|
|
|
# read arguments into global variables
|
|
NAME=$1
|
|
BRANCH=$PREFIX$NAME
|
|
}
|
|
|
|
cmd_start() {
|
|
DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
|
|
DEFINE_boolean force false 'force creation of feature branch (ignores dirty working tree)' f
|
|
parse_args "$@"
|
|
BASE=${2:-$DEVELOP_BRANCH}
|
|
require_name_arg
|
|
|
|
# sanity checks
|
|
if noflag force; then
|
|
require_clean_working_tree
|
|
fi
|
|
require_branch_absent "$BRANCH"
|
|
|
|
# update the local repo with remote changes, if asked
|
|
if flag fetch; then
|
|
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
|
|
fi
|
|
|
|
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
|
|
|
|
# create branch
|
|
if ! git checkout -b "$BRANCH" "$BASE"; then
|
|
die "Could not create feature branch '$BRANCH'"
|
|
fi
|
|
|
|
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() {
|
|
DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
|
|
DEFINE_boolean rebase false 'rebase instead of merge' r
|
|
DEFINE_boolean squash false 'squash all commits when rebasing (implies --rebase)' s
|
|
parse_args "$@"
|
|
expand_nameprefix_arg
|
|
|
|
# sanity checks
|
|
require_branch "$BRANCH"
|
|
|
|
# detect if we're restoring from a merge conflict
|
|
if [ -f "$DOT_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).
|
|
#
|
|
|
|
# TODO: git_is_clean_working_tree() should provide an alternative
|
|
# exit code for "unmerged changes in working tree", which we should
|
|
# actually be testing for here
|
|
if git_is_clean_working_tree; then
|
|
FINISH_BASE=$(cat "$DOT_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 git_is_branch_merged_into()
|
|
if git_is_branch_merged_into "$BRANCH" "$FINISH_BASE"; then
|
|
rm -f "$DOT_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 "$DOT_GIT_DIR/.gitflow/MERGE_BASE"
|
|
fi
|
|
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
|
|
|
|
# sanity checks
|
|
require_clean_working_tree
|
|
|
|
# update local repo with remote changes first, if asked
|
|
if flag fetch; then
|
|
git fetch -q "$ORIGIN" "$BRANCH"
|
|
fi
|
|
|
|
if has "$ORIGIN/$BRANCH" "$(git_remote_branches)"; then
|
|
require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
|
|
fi
|
|
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
|
|
|
|
# if the user wants to rebase, do that first
|
|
if flag rebase; then
|
|
if ! git flow feature rebase "$NAME" "$DEVELOP_BRANCH"; then
|
|
warn "Finish was aborted due to conflicts during rebase."
|
|
warn "Please finish the rebase manually now."
|
|
warn "When finished, re-run:"
|
|
warn " git flow feature finish '$NAME' '$DEVELOP_BRANCH'"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# merge into BASE
|
|
git checkout "$DEVELOP_BRANCH"
|
|
if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then
|
|
git merge --ff "$BRANCH"
|
|
else
|
|
git merge --no-ff "$BRANCH"
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
|
# oops.. we have a merge conflict!
|
|
# write the given $DEVELOP_BRANCH to a temporary file (we need it later)
|
|
mkdir -p "$DOT_GIT_DIR/.gitflow"
|
|
echo "$DEVELOP_BRANCH" > "$DOT_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() {
|
|
# sanity checks
|
|
require_branch "$BRANCH"
|
|
require_clean_working_tree
|
|
|
|
# delete branch
|
|
if flag fetch; then
|
|
git push "$ORIGIN" ":refs/heads/$BRANCH"
|
|
fi
|
|
git branch -d "$BRANCH"
|
|
|
|
echo
|
|
echo "Summary of actions:"
|
|
echo "- The feature branch '$BRANCH' was merged into '$DEVELOP_BRANCH'"
|
|
#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 '$DEVELOP_BRANCH'"
|
|
echo
|
|
}
|
|
|
|
cmd_publish() {
|
|
parse_args "$@"
|
|
expand_nameprefix_arg
|
|
|
|
# sanity checks
|
|
require_clean_working_tree
|
|
require_branch "$BRANCH"
|
|
git fetch -q "$ORIGIN"
|
|
require_branch_absent "$ORIGIN/$BRANCH"
|
|
|
|
# create remote branch
|
|
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
|
|
git fetch -q "$ORIGIN"
|
|
|
|
# configure remote tracking
|
|
git config "branch.$BRANCH.remote" "$ORIGIN"
|
|
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() {
|
|
parse_args "$@"
|
|
require_name_arg
|
|
|
|
# sanity checks
|
|
require_clean_working_tree
|
|
require_branch_absent "$BRANCH"
|
|
git fetch -q "$ORIGIN"
|
|
require_branch "$ORIGIN/$BRANCH"
|
|
|
|
# create tracking branch
|
|
git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
|
|
|
|
echo
|
|
echo "Summary of actions:"
|
|
echo "- A new remote tracking branch '$BRANCH' was created"
|
|
echo "- You are now on branch '$BRANCH'"
|
|
echo
|
|
}
|
|
|
|
cmd_diff() {
|
|
parse_args "$@"
|
|
|
|
if [ "$NAME" != "" ]; then
|
|
expand_nameprefix_arg
|
|
BASE=$(git merge-base "$DEVELOP_BRANCH" "$BRANCH")
|
|
git diff "$BASE..$BRANCH"
|
|
else
|
|
if ! git_current_branch | grep -q "^$PREFIX"; then
|
|
die "Not on a feature branch. Name one explicitly."
|
|
fi
|
|
|
|
BASE=$(git merge-base "$DEVELOP_BRANCH" HEAD)
|
|
git diff "$BASE"
|
|
fi
|
|
}
|
|
|
|
cmd_rebase() {
|
|
DEFINE_boolean interactive false 'do an interactive rebase' i
|
|
parse_args "$@"
|
|
expand_nameprefix_arg_or_current
|
|
warn "Will try to rebase '$NAME'..."
|
|
require_clean_working_tree
|
|
require_branch "$BRANCH"
|
|
|
|
git checkout -q "$BRANCH"
|
|
local OPTS=
|
|
if flag interactive; then
|
|
OPTS="$OPTS -i"
|
|
fi
|
|
git rebase $OPTS "$DEVELOP_BRANCH"
|
|
}
|