git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Karl Hasselström" <kha@treskal.com>
To: Catalin Marinas <catalin.marinas@gmail.com>
Cc: git@vger.kernel.org
Subject: [StGit PATCH 3/6] Properly detect that HEAD is detached
Date: Mon, 08 Oct 2007 10:55:23 +0200	[thread overview]
Message-ID: <20071008085523.9734.44817.stgit@yoghurt> (raw)
In-Reply-To: <20071008085430.9734.75797.stgit@yoghurt>

We still error out on a lot of places we shouldn't, e.g. "stg branch"
when on a detached HEAD, but at least now we give a sane error
message.

Signed-off-by: Karl Hasselström <kha@treskal.com>

---

 stgit/git.py |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)


diff --git a/stgit/git.py b/stgit/git.py
index 812b77a..cc6acb1 100644
--- a/stgit/git.py
+++ b/stgit/git.py
@@ -246,11 +246,19 @@ def get_head():
         __head = rev_parse('HEAD')
     return __head
 
+class DetachedHeadException(GitException):
+    def __init__(self):
+        GitException.__init__(self, 'Not on any branch')
+
 def get_head_file():
-    """Returns the name of the file pointed to by the HEAD link
-    """
-    return strip_prefix('refs/heads/',
-                        GRun('git-symbolic-ref', 'HEAD').output_one_line())
+    """Return the name of the file pointed to by the HEAD symref.
+    Throw an exception if HEAD is detached."""
+    try:
+        return strip_prefix(
+            'refs/heads/', GRun('git-symbolic-ref', '-q', 'HEAD'
+                                ).output_one_line())
+    except GitRunException:
+        raise DetachedHeadException()
 
 def set_head_file(ref):
     """Resets HEAD to point to a new ref
@@ -385,8 +393,11 @@ def rename_ref(from_ref, to_ref):
 def rename_branch(from_name, to_name):
     """Rename a git branch."""
     rename_ref('refs/heads/%s' % from_name, 'refs/heads/%s' % to_name)
-    if get_head_file() == from_name:
-        set_head_file(to_name)
+    try:
+        if get_head_file() == from_name:
+            set_head_file(to_name)
+    except DetachedHeadException:
+        pass # detached HEAD, so the renamee can't be the current branch
     reflog_dir = os.path.join(basedir.get(), 'logs', 'refs', 'heads')
     if os.path.exists(reflog_dir) \
            and os.path.exists(os.path.join(reflog_dir, from_name)):

  parent reply	other threads:[~2007-10-08  8:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-08  8:55 [StGit PATCH 0/6] Survive capital punishment Karl Hasselström
2007-10-08  8:55 ` [StGit PATCH 1/6] Allow caller to customize title of error/warning message Karl Hasselström
2007-10-08  8:55 ` [StGit PATCH 2/6] Use our nice message printout wrapping system Karl Hasselström
2007-10-08  8:55 ` Karl Hasselström [this message]
2007-10-08  8:55 ` [StGit PATCH 4/6] Refactor crt_series creation Karl Hasselström
2007-10-08  8:55 ` [StGit PATCH 5/6] Don't have a global crt_series in stgit.commans.common Karl Hasselström
2007-10-08  8:55 ` [StGit PATCH 6/6] Let some commands work with detached HEAD Karl Hasselström
2007-10-08 15:34   ` Karl Hasselström
2007-10-09  4:42     ` [StGit PATCH] New test: "stg diff" Karl Hasselström
2007-10-09  5:12     ` [StGit PATCH 6/6] Let some commands work with detached HEAD Karl Hasselström
2007-10-08  8:58 ` [StGit PATCH 0/6] Survive capital punishment Karl Hasselström
2007-10-08  9:27   ` Karl Hasselström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071008085523.9734.44817.stgit@yoghurt \
    --to=kha@treskal.com \
    --cc=catalin.marinas@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).