Skip to content

Commit

Permalink
rev-parse: dedup, support ^ and ~
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Mou committed Apr 21, 2019
1 parent d5d472d commit 5a4fde1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
8 changes: 6 additions & 2 deletions cat-file
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ filename = '.zit/objects/{}/{}'.format(sha1[:2], sha1[2:])
decompressed = zlib.decompress(open(filename).read())
header, contents = decompressed.split('\0', 1)
actual_type, length = header.split(' ')
assert actual_type == required_type

assert int(length) == len(contents)
sys.stdout.write(contents)
if required_type == '-t':
print actual_type
else:
assert actual_type == required_type
sys.stdout.write(contents)
35 changes: 16 additions & 19 deletions rev-parse
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ import sys

# sha1_name.c:get_sha1
def get_sha1(name):
# TODO ^ and ~?
# TODO specialize for tree?
if '^' in name:
name, type = name.rsplit('^', 1)
assert type[0] == '{' and type[-1] == '}'
type = type[1:-1]
if name[-2] == '~':
name = name[:-2] + '^' * int(name[-1])
if name[-1] == '^':
sha1 = get_sha1(name[:-1])
contents = subprocess.check_output(['cat-file', 'commit', sha1])
parent = next(line for line in contents.splitlines()
if line.startswith('parent '))
return parent.split(' ')[1]
if '^{' in name:
name, requested_type = name.rsplit('^{', 1)
assert requested_type[-1] == '}'
requested_type = requested_type[:-1]
while True:
sha1 = get_sha1(name)

# TODO dedup w/ cat-file
import zlib
# sha1_file.c:sha1_file_name
filename = '.zit/objects/{}/{}'.format(sha1[:2], sha1[2:])
decompressed = zlib.decompress(open(filename).read())
header, contents = decompressed.split('\0', 1)
actual_type, length = header.split(' ')

if actual_type == type:
actual_type = subprocess.check_output(['cat-file', '-t', sha1]).strip()
if actual_type == requested_type:
return sha1
elif actual_type == 'commit':
assert int(length) == len(contents)
contents = subprocess.check_output(['cat-file', 'commit', sha1])
tree = next(line for line in contents.splitlines()
if line.startswith('tree '))
name = tree.split(' ')[1]
# TODO deref tags?
else:
raise Exception('wrong type')
else:
return subprocess.check_output(['get-sha1-basic', name])[:-1]
return subprocess.check_output(['get-sha1-basic', name]).strip()

_, name = sys.argv
print get_sha1(name)

0 comments on commit 5a4fde1

Please sign in to comment.