From: Chuck Lever Date: Mon, 3 Oct 2005 17:03:31 +0000 (-0400) Subject: Use "git-rev-parse" to get the HEAD commit X-Git-Tag: v0.8~85 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/commitdiff_plain/3097799dcde97cc3e1e3d6c87138f300df0ec2b6 Use "git-rev-parse" to get the HEAD commit 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 --- diff --git a/stgit/git.py b/stgit/git.py index 56374bc..dbc44c8 100644 --- a/stgit/git.py +++ b/stgit/git.py @@ -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