From dec4663129f72321a14efd6de63f14a7419e3ed2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 23 Nov 2010 09:14:17 +0100 Subject: Split ref implementation up into multiple files, to make room for the log implementation --- repo/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'repo/fun.py') diff --git a/repo/fun.py b/repo/fun.py index a0f66fe5..a684730b 100644 --- a/repo/fun.py +++ b/repo/fun.py @@ -1,5 +1,5 @@ """Package with general repository related functions""" - +import os from gitdb.exc import BadObject from git.refs import SymbolicReference from git.objects import Object -- cgit v1.2.3 From 5bd7d44ff7e51105e3e277aee109a45c42590572 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 23 Nov 2010 17:40:41 +0100 Subject: repo.rev_parse: Added support for simple log parsing - dates are not yet supported, mainly because I don't need it --- repo/fun.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'repo/fun.py') diff --git a/repo/fun.py b/repo/fun.py index a684730b..7b842c38 100644 --- a/repo/fun.py +++ b/repo/fun.py @@ -42,9 +42,13 @@ def short_to_long(odb, hexsha): # END exception handling -def name_to_object(repo, name): - """:return: object specified by the given name, hexshas ( short and long ) - as well as references are supported""" +def name_to_object(repo, name, return_ref=False): + """ + :return: object specified by the given name, hexshas ( short and long ) + as well as references are supported + :param return_ref: if name specifies a reference, we will return the reference + instead of the object. Otherwise it will raise BadObject + """ hexsha = None # is it a hexsha ? Try the most common ones, which is 7 to 40 @@ -59,12 +63,20 @@ def name_to_object(repo, name): for base in ('%s', 'refs/%s', 'refs/tags/%s', 'refs/heads/%s', 'refs/remotes/%s', 'refs/remotes/%s/HEAD'): try: hexsha = SymbolicReference.dereference_recursive(repo, base % name) + if return_ref: + return SymbolicReference(repo, base % name) + #END handle symbolic ref break except ValueError: pass # END for each base # END handle hexsha - + + # didn't find any ref, this is an error + if return_ref: + raise BadObject("Couldn't find reference named %r" % name) + #END handle return ref + # tried everything ? fail if hexsha is None: raise BadObject(name) @@ -101,9 +113,6 @@ def rev_parse(repo, rev): :note: Currently there is no access to the rev-log, rev-specs may only contain topological tokens such ~ and ^. :raise BadObject: if the given revision could not be found""" - if '@' in rev: - raise ValueError("There is no rev-log support yet") - # colon search mode ? if rev.startswith(':/'): @@ -112,22 +121,37 @@ def rev_parse(repo, rev): # END handle search obj = None + ref = None output_type = "commit" start = 0 parsed_to = 0 lr = len(rev) while start < lr: - if rev[start] not in "^~:": + if rev[start] not in "^~:@": start += 1 continue # END handle start + token = rev[start] + if obj is None: # token is a rev name - obj = name_to_object(repo, rev[:start]) + if start == 0: + ref = repo.head.ref + else: + if token == '@': + ref = name_to_object(repo, rev[:start], return_ref=True) + else: + obj = name_to_object(repo, rev[:start]) + #END handle token + #END handle refname + + if ref is not None: + obj = ref.commit + #END handle ref # END initialize obj on first token - token = rev[start] + start += 1 # try to parse {type} @@ -153,6 +177,24 @@ def rev_parse(repo, rev): # cannot do anything for non-tags pass # END handle tag + elif token == '@': + # try single int + assert ref is not None, "Requre Reference to access reflog" + revlog_index = None + try: + # transform reversed index into the format of our revlog + revlog_index = -(int(output_type)+1) + except ValueError: + # TODO: Try to parse the other date options, using parse_date + # maybe + raise NotImplementedError("Support for additional @{...} modes not implemented") + #END handle revlog index + + entry = ref.log()[revlog_index] + obj = Object.new_from_sha(repo, hex_to_bin(entry.newhexsha)) + + # make it pass the following checks + output_type = None else: raise ValueError("Invalid output type: %s ( in %s )" % (output_type, rev)) # END handle output type -- cgit v1.2.3 From 8dd51f1d63fa5ee704c2bdf4cb607bb6a71817d2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 24 Nov 2010 09:37:40 +0100 Subject: Improved refparse error handling in case of out-of-bound indices --- repo/fun.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'repo/fun.py') diff --git a/repo/fun.py b/repo/fun.py index 7b842c38..aa938477 100644 --- a/repo/fun.py +++ b/repo/fun.py @@ -190,7 +190,12 @@ def rev_parse(repo, rev): raise NotImplementedError("Support for additional @{...} modes not implemented") #END handle revlog index - entry = ref.log()[revlog_index] + try: + entry = ref.log()[revlog_index] + except IndexError: + raise BadObject("Invalid revlog index: %i" % revlog_index) + #END handle index out of bound + obj = Object.new_from_sha(repo, hex_to_bin(entry.newhexsha)) # make it pass the following checks -- cgit v1.2.3 From 98a313305f0d554a179b93695d333199feb5266c Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 24 Nov 2010 19:36:34 +0100 Subject: RefLog: added entry_at method, which is a faster way of reading single entries, including test --- repo/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'repo/fun.py') diff --git a/repo/fun.py b/repo/fun.py index aa938477..c523a3e1 100644 --- a/repo/fun.py +++ b/repo/fun.py @@ -191,7 +191,7 @@ def rev_parse(repo, rev): #END handle revlog index try: - entry = ref.log()[revlog_index] + entry = ref.log_entry(revlog_index) except IndexError: raise BadObject("Invalid revlog index: %i" % revlog_index) #END handle index out of bound -- cgit v1.2.3