Use "git-rev-parse" to get the HEAD commit
authorChuck Lever <cel@netapp.com>
Mon, 3 Oct 2005 17:03:31 +0000 (13:03 -0400)
committerCatalin Marinas <catalin.marinas@gmail.com>
Tue, 4 Oct 2005 19:56:17 +0000 (20:56 +0100)
Traditionally, HEAD was just a link to a refs file.  Recently, however,
support for HEADs of the form "ref: " was added to git.  In addition,
it's safer to verify the HEAD commit id before using it.

Change git.py to use "git-rev-parse" to derive the HEAD commit instead of
reading the HEAD link directly.  If there are any problems with the HEAD
commit id, git.get_head() now raises an exception.

And, use "git-update-ref" to update the HEAD for similar reasons.

Signed-off-by: Chuck Lever <cel@netapp.com>
stgit/git.py

index 56374bc..dbc44c8 100644 (file)
@@ -215,28 +215,25 @@ def local_changes():
     return len(__tree_status()) != 0
 
 def get_head():
-    """Returns a string representing the HEAD
+    """Verifies the HEAD and returns the SHA1 id that represents it
     """
-    return read_string(head_link)
+    return rev_parse('HEAD')
 
 def get_head_file():
     """Returns the name of the file pointed to by the HEAD link
     """
-    # valid link
-    if os.path.islink(head_link) and os.path.isfile(head_link):
-        return os.path.basename(os.readlink(head_link))
-    else:
-        raise GitException, 'Invalid .git/HEAD link. Git tree not initialised?'
+    return os.path.basename(_output_one_line('git-symbolic-ref HEAD'))
 
 def __set_head(val):
     """Sets the HEAD value
     """
-    write_string(head_link, val)
+    if __run('git-update-ref HEAD', [val]) != 0:
+        raise GitException, 'Could not update HEAD to "%s".' % val
 
 def rev_parse(git_id):
-    """Parse the string and return an SHA1 id
+    """Parse the string and return a verified SHA1 id
     """
-    return _output(['git-rev-parse', git_id]).strip()
+    return _output_one_line(['git-rev-parse', '--verify', git_id])
 
 def add(names):
     """Add the files or recursively add the directory contents