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>
return len(__tree_status()) != 0
def get_head():
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
"""
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
"""
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
- """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
def add(names):
"""Add the files or recursively add the directory contents