git_cl: Fix crashes when using --no-squash with detached HEAD

Bug: None
Change-Id: If69d4f558bd01c439a68dfdab76755487b58dca0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4183546
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Michael Cui <mlcui@google.com>
diff --git a/git_cl.py b/git_cl.py
index 77d4a10..6ea8498 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1517,14 +1517,15 @@
       #   bug_123
       #   fix-123
       #   fix-123-some-description
-      match = re.match(
-          r'^(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)([-_]|$)',
-          self.GetBranch())
-      if not bug and not fixed and match:
-        if match.group('type') == 'bug':
-          bug = match.group('bugnum')
-        else:
-          fixed = match.group('bugnum')
+      branch = self.GetBranch()
+      if branch is not None:
+        match = re.match(
+            r'^(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)([-_]|$)', branch)
+        if not bug and not fixed and match:
+          if match.group('type') == 'bug':
+            bug = match.group('bugnum')
+          else:
+            fixed = match.group('bugnum')
 
     change_description = ChangeDescription(description, bug, fixed)
 
@@ -1727,9 +1728,10 @@
     ret = self.CMDUploadChange(
         options, git_diff_args, custom_cl_base, change_desc)
     if not ret:
-      self._GitSetBranchConfigValue(
-          LAST_UPLOAD_HASH_CONFIG_KEY,
-          scm.GIT.ResolveCommit(settings.GetRoot(), 'HEAD'))
+      if self.GetBranch() is not None:
+        self._GitSetBranchConfigValue(
+            LAST_UPLOAD_HASH_CONFIG_KEY,
+            scm.GIT.ResolveCommit(settings.GetRoot(), 'HEAD'))
       # Run post upload hooks, if specified.
       if settings.GetRunPostUploadHook():
         self.RunPostUploadHook(options.verbose, base_branch,