3 # Copyright (C) 2014 Felipe Contreras
4 # Copyright (C) 2005 Junio C Hamano
6 # Fetch upstream and update the current branch.
8 USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]ff] [--[no-]rebase|--rebase=preserve] [-s strategy]...'
9 LONG_USAGE='Fetch upstram and update the current branch.'
14 set_reflog_action "update${1+ $*}"
15 require_work_tree_exists
20 printf >&2 'warning: %s\n' "$*"
24 git diff-index --cached --name-status -r --ignore-submodules HEAD --
25 die "$(gettext "Update is not possible because you have instaged files.")"
29 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
32 test -z "$(git ls-files -u)" || die_conflict
33 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
35 bool_or_string_config () {
36 git config --bool "$1" 2>/dev/null || git config "$1"
39 strategy_args= diffstat= no_commit= no_ff= ff_only=
40 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
41 merge_args= edit= rebase_args=
42 curr_branch=$(git symbolic-ref -q HEAD)
43 curr_branch_short="${curr_branch#refs/heads/}"
44 mode=$(git config branch.${curr_branch_short}.updatemode)
47 mode=$(git config update.mode)
50 merge|rebase|ff-only|'')
54 rebase_args="--preserve-merges"
57 echo "Invalid value for 'mode'"
62 # compatibility with pull configuration
65 rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
68 rebase=$(bool_or_string_config pull.rebase)
71 test -z "$mode" && mode=ff-only
77 verbosity="$verbosity -q" ;;
79 verbosity="$verbosity -v" ;;
81 progress=--progress ;;
83 progress=--no-progress ;;
84 -n|--no-stat|--no-summary)
95 no_commit=--no-commit ;;
104 -s=*|--strategy=*|-s|--strategy)
107 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
114 strategy_args="${strategy_args}-s $strategy "
121 xx="-X $(git rev-parse --sq-quote "$2")"
124 xx=$(git rev-parse --sq-quote "$1") ;;
126 merge_args="$merge_args$xx "
137 --recurse-submodules)
138 recurse_submodules=--recurse-submodules
140 --recurse-submodules=*)
141 recurse_submodules="$1"
143 --no-recurse-submodules)
144 recurse_submodules=--no-recurse-submodules
147 verify_signatures=--verify-signatures
149 --no-verify-signatures)
150 verify_signatures=--no-verify-signatures
159 # Pass thru anything that may be meant for fetch.
177 rebase_args=--preserve-merges
180 echo "Invalid value for --rebase, should be true, false, or preserve"
187 test -z "$curr_branch" &&
188 die "$(gettext "You are not currently on a branch.")"
190 get_remote_branch () {
193 ref=$(git rev-parse --symbolic-full-name $1)
195 git config --get-regexp 'remote.*.fetch' | while read key value
197 remote=${key#remote.}
198 remote=${remote%.fetch}
203 branch=${ref#$prefix}
204 echo "remote=$remote branch=$branch"
213 branch=$(git config "branch.$curr_branch_short.merge")
214 remote=$(git config "branch.$curr_branch_short.remote")
216 test -z "$branch" && branch=$curr_branch
217 test -z "$remote" && remote="origin"
223 eval $(get_remote_branch $1)
231 branch="${branch#refs/heads/}"
233 test "$mode" = rebase && {
234 require_clean_work_tree "update with rebase" "Please commit or stash them."
235 oldremoteref=$(git merge-base --fork-point $branch $curr_branch 2>/dev/null)
237 orig_head=$(git rev-parse -q --verify HEAD)
239 if test $remote = "."
244 git fetch $verbosity $progress $dry_run $recurse_submodules $remote $branch $extra || exit 1
246 test -z "$dry_run" || exit 0
248 merge_head=$(sed -e '/ not-for-merge /d' -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD)
250 test -z "$merge_head" &&
251 die "$(gettext "Couldnot fetch branch '${branch#refs/heads/}'.")"
253 if test "$mode" = 'ff-only' && test -z "$no_ff$ff_only"
255 # check if a non-fast-forward merge would be needed
256 if ! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
257 ! git merge-base --is-ancestor "$merge_head" "$orig_head"
259 die "$(gettext "The update was not fast-forward, please either merge or rebase.
260 If unsure, run 'git update --merge'.")"
265 if test "$mode" = rebase
267 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
268 test "$oldremoteref" = "$o" && unset oldremoteref
272 if test "$curr_branch_short" = "$branch"
274 echo "Update branch '$curr_branch_short'"
277 msg="${msg} branch '$curr_branch_short'"
278 test "$branch" != "master" && msg="${msg} into $branch"
290 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
291 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
295 merge_msg=$(git fmt-merge-msg $log_arg < "$GIT_DIR"/FETCH_HEAD | fix_msg) || exit
296 eval="git-merge $diffstat $no_commit $verify_signatures $edit $no_ff $ff_only"
297 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
298 eval="$eval --reverse-parents -m \"\$merge_msg\" $merge_head"