From e1a5a2cc22dee2f6406a5fc26b0c6e2ee7fd8ddf Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sun, 15 Sep 2019 14:50:08 -0600
Subject: [PATCH] Improve fetch git branch logic

---
 cook.sh | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/cook.sh b/cook.sh
index 105c6b101..72b36d446 100755
--- a/cook.sh
+++ b/cook.sh
@@ -119,14 +119,17 @@ function op {
                         git remote add upstream "$GIT_UPSTREAM"
                         git fetch upstream
                     fi
+
+                    ORIGIN_BRANCH="$(git branch --remotes | grep '^  origin/HEAD -> ' | cut -d ' ' -f 5-)"
                     if [ -n "$BRANCH" ]
                     then
-                        git checkout "$BRANCH"
-                    else
-                        #TODO: Find correct upstream default branch
-                        git checkout master
+                        ORIGIN_BRANCH="origin/$BRANCH"
+                    fi
+
+                    if [ "$(git rev-parse HEAD)" != "$(git rev-parse $ORIGIN_BRANCH)" ]
+                    then
+                        git checkout -B "$(echo "$ORIGIN_BRANCH" | cut -d / -f 2-)" "$ORIGIN_BRANCH"
                     fi
-                    git pull
                     git submodule sync --recursive
                     git submodule update --init --recursive
                     popd > /dev/null
-- 
GitLab