chiark / gitweb /
tg summary: Show info about remote mates
[topgit.git] / tg-update.sh
index ec14bf68049d1dcffcf8b68b3d5ec36226c847af..34fa6dcb10f59799e0df977e98544483cff3fc30 100644 (file)
@@ -21,8 +21,10 @@ base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)"
 
 ## First, take care of our base
 
-depcheck="$(mktemp)"
-needs_update "$name" >"$depcheck"
+depcheck="$(mktemp -t tg-depcheck.XXXXXX)"
+missing_deps=
+needs_update "$name" >"$depcheck" || :
+[ -z "$missing_deps" ] || die "some dependencies are missing: $missing_deps"
 if [ -s "$depcheck" ]; then
        # We need to switch to the base branch
        # ...but only if we aren't there yet (from failed previous merge)
@@ -37,13 +39,14 @@ if [ -s "$depcheck" ]; then
                sed 's/^\([^+]\)/-\1/' | # now each line is +branch or -branch (+ == recurse)
                uniq -s 1 | # fold branch lines; + always comes before - and thus wins within uniq
                while read depline; do
-                       action="${depline:0:1}"
-                       dep="${depline:1}"
+                       action="$(echo "$depline" | cut -c 1)"
+                       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..."
@@ -65,6 +68,10 @@ if [ -s "$depcheck" ]; then
                                switch_to_base "$name"
                        fi
 
+                       # 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
@@ -88,15 +95,41 @@ else
 fi
 rm "$depcheck"
 
+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
+## Third, update our head with the base
 
-if branch_contains "$name" "refs/top-bases/$name"; then
+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"