# This shell script fragment is sourced by git-rebase to implement # its merge-based non-interactive mode that copes well with renamed # files. # # Copyright (c) 2010 Junio C Hamano. # setglobal prec = '4' proc read_state { setglobal onto_name = $[cat "$state_dir"/onto_name] && setglobal end = $[cat "$state_dir"/end] && setglobal msgnum = $[cat "$state_dir"/msgnum] } proc continue_merge { test -d $state_dir || die "$state_dir directory does not exist" setglobal unmerged = $[git ls-files -u] if test -n $unmerged { echo "You still have unmerged paths in your index" echo "did you forget to use git add?" die $resolvemsg } setglobal cmt = $[cat "$state_dir/current] if ! git diff-index --quiet --ignore-submodules HEAD -- { if ! git commit $(gpg_sign_opt:+"$gpg_sign_opt") --no-verify -C $cmt { echo "Commit failed, please do not call \"git commit\"" echo "directly, but instead do one of the following: " die $resolvemsg } if test -z $GIT_QUIET { printf "Committed: %0$(prec)d " $msgnum } echo "$cmt $[git rev-parse HEAD^0]" >> "$state_dir/rewritten" } else { if test -z $GIT_QUIET { printf "Already applied: %0$(prec)d " $msgnum } } test -z $GIT_QUIET && env GIT_PAGER='' git log --format=%s -1 $cmt # onto the next patch: setglobal msgnum = $shExpr('$msgnum + 1') echo $msgnum >"$state_dir/msgnum" } proc call_merge { setglobal msgnum = $1 echo $msgnum >"$state_dir/msgnum" setglobal cmt = $[cat "$state_dir/cmt.$msgnum] echo $cmt > "$state_dir/current" setglobal hd = $[git rev-parse --verify HEAD] setglobal cmt_name = $[git symbolic-ref HEAD !2 > /dev/null || echo HEAD] eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"' eval GITHEAD_$hd='$onto_name' export GITHEAD_$cmt GITHEAD_$hd if test -n $GIT_QUIET { setglobal GIT_MERGE_VERBOSITY = '1' && export GIT_MERGE_VERBOSITY } test -z $strategy && setglobal strategy = 'recursive' # If cmt doesn't have a parent, don't include it as a base setglobal base = $[git rev-parse --verify --quiet $cmt^] eval 'git-merge-$strategy' $strategy_opts $base ' -- "$hd" "$cmt"' setglobal rv = $Status match $rv { with 0 unset GITHEAD_$cmt GITHEAD_$hd return with 1 git rerere $allow_rerere_autoupdate die $resolvemsg with 2 echo "Strategy: $strategy failed, try another" !1 > !2 die $resolvemsg with * die "Unknown exit code ($rv) from command:" \ "git-merge-$strategy $cmt^ -- HEAD $cmt" } } proc finish_rb_merge { move_to_original_branch if test -s "$state_dir"/rewritten { git notes copy --for-rewrite=rebase <"$state_dir"/rewritten setglobal hook = $[git rev-parse --git-path hooks/post-rewrite] test -x $hook && $hook rebase <"$state_dir"/rewritten } say All done. } # 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, older (9.x) versions of FreeBSD /bin/sh misbehave on such a # construct and continue 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. proc git_rebase__merge { match $action { with continue read_state continue_merge while test "$msgnum" -le "$end" { call_merge $msgnum continue_merge } finish_rb_merge return with skip read_state git rerere clear setglobal msgnum = $shExpr('$msgnum + 1') while test "$msgnum" -le "$end" { call_merge $msgnum continue_merge } finish_rb_merge return } mkdir -p $state_dir echo $onto_name > "$state_dir/onto_name" write_basic_state setglobal msgnum = '0' for cmt in [$[git rev-list --reverse --no-merges $revisions]] { setglobal msgnum = $shExpr('$msgnum + 1') echo $cmt > "$state_dir/cmt.$msgnum" } echo 1 >"$state_dir/msgnum" echo $msgnum >"$state_dir/end" setglobal end = $msgnum setglobal msgnum = '1' while test "$msgnum" -le "$end" { call_merge $msgnum continue_merge } finish_rb_merge } # ... and then we call the whole thing. git_rebase__merge