chiark / gitweb /
Merge branch 'master' into refs/top-bases/debian/locations
[topgit.git] / tg-update.sh
index 5caf6910a17f7c39906fe6456bd550faf4b537c2..73280c65af79af2da50204582c7029a00cfcef34 100644 (file)
@@ -9,7 +9,7 @@ name=
 ## Parse options
 
 if [ -n "$1" ]; then
-       echo "Usage: tg update" >&2
+       echo "Usage: tg [...] update" >&2
        exit 1
 fi
 
@@ -43,9 +43,10 @@ if [ -s "$depcheck" ]; then
                        dep="$(echo "$depline" | cut -c 2-)"
 
                        # We do not distinguish between dependencies out-of-date
-                       # and base out-of-date cases for $dep here,
-                       # but thanks to needs_update returning : for the latter,
-                       # we do correctly recurse here in both cases.
+                       # and base/remote out-of-date cases for $dep here,
+                       # but thanks to needs_update returning : or %
+                       # for the latter, we do correctly recurse here
+                       # in both cases.
 
                        if [ x"$action" = x+ ]; then
                                info "Recursing to $dep..."
@@ -53,12 +54,12 @@ if [ -s "$depcheck" ]; then
                                (
                                export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
                                export PS1="[$dep] $PS1"
-                               while ! tg update; do
+                               while ! $tg update; do
                                        # The merge got stuck! Let the user fix it up.
                                        info "You are in a subshell. If you abort the merge,"
                                        info "use \`exit 1\` to abort the recursive update altogether."
                                        if ! sh -i </dev/tty; then
-                                               info "Ok, you aborated the merge. Now, you just need to"
+                                               info "Ok, you aborted the merge. Now, you just need to"
                                                info "switch back to some sane branch using \`git checkout\`."
                                                exit 3
                                        fi
@@ -67,16 +68,14 @@ if [ -s "$depcheck" ]; then
                                switch_to_base "$name"
                        fi
 
-                       # This will always be a proper topic branch
-                       # (not a base or remote), since for deep updates
-                       # we recurse and immediate dependencies
-                       # are always proper. (branch_needs_update() is called
+                       # This will be either a proper topic branch
+                       # or a remote base.  (branch_needs_update() is called
                        # only on the _dependencies_, not our branch itself!)
 
                        info "Updating base with $dep changes..."
                        if ! git merge "$dep"; then
                                if [ -z "$TG_RECURSIVE" ]; then
-                                       resume='`tg update` again'
+                                       resume="\`git checkout $name && $tg update\` again"
                                else # subshell
                                        resume='exit'
                                fi
@@ -88,30 +87,60 @@ if [ -s "$depcheck" ]; then
                                exit 2
                        fi
                done
-
-       # Home, sweet home...
-       git checkout -q "$name"
 else
        info "The base is up-to-date."
 fi
 rm "$depcheck"
 
+# Home, sweet home...
+# (We want to always switch back, in case we were on the base from failed
+# previous merge.)
+git checkout -q "$name"
+
+merge_with="refs/top-bases/$name"
+
+
+## Second, update our head with the remote branch
+
+if has_remote "$name"; then
+       rname="refs/remotes/$base_remote/$name"
+       if branch_contains "$name" "$rname"; then
+               info "The $name head is up-to-date wrt. its remote branch."
+       else
+               info "Reconciling remote branch updates with $name base..."
+               # *DETACH* our HEAD now!
+               git checkout -q "refs/top-bases/$name"
+               if ! git merge "$rname"; then
+                       info "Oops, you will need to help me out here a bit."
+                       info "Please commit merge resolution and call:"
+                       info "git checkout $name && git merge <commitid>"
+                       info "It is also safe to abort this operation using: git reset --hard $name"
+                       exit 3
+               fi
+               # Go back but remember we want to merge with this, not base
+               merge_with="$(git rev-parse HEAD)"
+               git checkout -q "$name"
+       fi
+fi
 
-## Second, update our head with the base
 
-if branch_contains "$name" "refs/top-bases/$name"; then
+## Third, update our head with the base
+
+if branch_contains "$name" "$merge_with"; then
        info "The $name head is up-to-date wrt. the base."
        exit 0
 fi
 info "Updating $name against new base..."
-if ! git merge "refs/top-bases/$name"; then
+if ! git merge "$merge_with"; then
        if [ -z "$TG_RECURSIVE" ]; then
                info "Please commit merge resolution. No need to do anything else"
                info "You can abort this operation using \`git reset --hard\` now"
-               info "and retry this merge later using \`tg update\`."
+               info "and retry this merge later using \`$tg update\`."
        else # subshell
                info "Please commit merge resolution and call exit."
                info "You can abort this operation using \`git reset --hard\`."
        fi
        exit 3
 fi
+
+# vim:noet