Commit 5b786963 by Vicent Martí

Merge pull request #818 from nulltoken/rework

Revparse rework
parents 5d9cfa07 e2c81fca
...@@ -167,6 +167,23 @@ GIT_EXTERN(int) git_object_typeisloose(git_otype type); ...@@ -167,6 +167,23 @@ GIT_EXTERN(int) git_object_typeisloose(git_otype type);
*/ */
GIT_EXTERN(size_t) git_object__size(git_otype type); GIT_EXTERN(size_t) git_object__size(git_otype type);
/**
* Recursively peel an object until an object of the specified
* type is met
*
* The retrieved `peeled` object is owned by the repository
* and should be closed with the `git_object_free` method.
*
* @param peeled Pointer to the peeled git_object
* @param object The object to be processed
* @param target_type The type of the requested object
* @return 0 or an error code
*/
GIT_EXTERN(int) git_object_peel(
git_object **peeled,
git_object *object,
git_otype target_type);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -333,3 +333,72 @@ int git_object__resolve_to_type(git_object **obj, git_otype type) ...@@ -333,3 +333,72 @@ int git_object__resolve_to_type(git_object **obj, git_otype type)
*obj = scan; *obj = scan;
return error; return error;
} }
static int dereference_object(git_object **dereferenced, git_object *obj)
{
git_otype type = git_object_type(obj);
switch (type) {
case GIT_OBJ_COMMIT:
return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);
break;
case GIT_OBJ_TAG:
return git_tag_target(dereferenced, (git_tag*)obj);
break;
default:
return GIT_ENOTFOUND;
break;
}
}
static int peel_error(int error, const char* msg)
{
giterr_set(GITERR_INVALID, "The given object cannot be peeled - %s", msg);
return error;
}
int git_object_peel(
git_object **peeled,
git_object *object,
git_otype target_type)
{
git_object *source, *deref = NULL;
assert(object);
if (git_object_type(object) == target_type)
return git_object__dup(peeled, object);
if (target_type == GIT_OBJ_BLOB
|| target_type == GIT_OBJ_ANY)
return peel_error(GIT_EAMBIGUOUS, "Ambiguous target type");
if (git_object_type(object) == GIT_OBJ_BLOB)
return peel_error(GIT_ERROR, "A blob cannot be dereferenced");
source = object;
while (true) {
if (dereference_object(&deref, source) < 0)
goto cleanup;
if (source != object)
git_object_free(source);
if (git_object_type(deref) == target_type) {
*peeled = deref;
return 0;
}
source = deref;
deref = NULL;
}
cleanup:
if (source != object)
git_object_free(source);
git_object_free(deref);
return -1;
}
...@@ -98,6 +98,13 @@ struct git_repository { ...@@ -98,6 +98,13 @@ struct git_repository {
* export */ * export */
void git_object__free(void *object); void git_object__free(void *object);
GIT_INLINE(int) git_object__dup(git_object **dest, git_object *source)
{
git_cached_obj_incref(source);
*dest = source;
return 0;
}
int git_object__resolve_to_type(git_object **obj, git_otype type); int git_object__resolve_to_type(git_object **obj, git_otype type);
int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header); int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
......
...@@ -13,48 +13,12 @@ ...@@ -13,48 +13,12 @@
#include "git2.h" #include "git2.h"
typedef enum {
REVPARSE_STATE_INIT,
REVPARSE_STATE_CARET,
REVPARSE_STATE_LINEAR,
REVPARSE_STATE_COLON,
REVPARSE_STATE_DONE,
} revparse_state;
static int revspec_error(const char *revspec) static int revspec_error(const char *revspec)
{ {
giterr_set(GITERR_INVALID, "Failed to parse revision specifier - Invalid pattern '%s'", revspec); giterr_set(GITERR_INVALID, "Failed to parse revision specifier - Invalid pattern '%s'", revspec);
return -1; return -1;
} }
static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec)
{
git_oid resolved;
int error;
if ((error = git_reference_name_to_oid(&resolved, repo, spec)) < 0)
return error;
return git_object_lookup(out, repo, &resolved, GIT_OBJ_ANY);
}
/* Returns non-zero if yes */
static int spec_looks_like_describe_output(const char *spec)
{
regex_t regex;
int regex_error, retcode;
regex_error = regcomp(&regex, ".+-[0-9]+-g[0-9a-fA-F]+", REG_EXTENDED);
if (regex_error != 0) {
giterr_set_regex(&regex, regex_error);
return regex_error;
}
retcode = regexec(&regex, spec, 0, NULL, 0);
regfree(&regex);
return retcode == 0;
}
static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname) static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname)
{ {
int error, i; int error, i;
...@@ -75,7 +39,7 @@ static int disambiguate_refname(git_reference **out, git_repository *repo, const ...@@ -75,7 +39,7 @@ static int disambiguate_refname(git_reference **out, git_repository *repo, const
if (*refname) if (*refname)
git_buf_puts(&name, refname); git_buf_puts(&name, refname);
else { else {
git_buf_puts(&name, "HEAD"); git_buf_puts(&name, GIT_HEAD_FILE);
fallbackmode = false; fallbackmode = false;
} }
...@@ -115,21 +79,43 @@ static int maybe_sha_or_abbrev(git_object**out, git_repository *repo, const char ...@@ -115,21 +79,43 @@ static int maybe_sha_or_abbrev(git_object**out, git_repository *repo, const char
return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY); return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY);
} }
static int build_regex(regex_t *regex, const char *pattern)
{
int error;
if (*pattern == '\0') {
giterr_set(GITERR_REGEX, "Empty pattern");
return -1;
}
error = regcomp(regex, pattern, REG_EXTENDED);
if (!error)
return 0;
giterr_set_regex(regex, error);
regfree(regex);
return -1;
}
static int maybe_describe(git_object**out, git_repository *repo, const char *spec) static int maybe_describe(git_object**out, git_repository *repo, const char *spec)
{ {
const char *substr; const char *substr;
int match; int error;
regex_t regex;
/* "git describe" output; snip everything before/including "-g" */
substr = strstr(spec, "-g"); substr = strstr(spec, "-g");
if (substr == NULL) if (substr == NULL)
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
if ((match = spec_looks_like_describe_output(spec)) < 0) if (build_regex(&regex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0)
return match; return -1;
error = regexec(&regex, spec, 0, NULL, 0);
regfree(&regex);
if (!match) if (error)
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
return maybe_sha_or_abbrev(out, repo, substr+2); return maybe_sha_or_abbrev(out, repo, substr+2);
...@@ -168,373 +154,308 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const ...@@ -168,373 +154,308 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
} }
static int all_chars_are_digits(const char *str, size_t len) static int try_parse_numeric(int *n, const char *curly_braces_content)
{ {
size_t i = 0; int content;
const char *end_ptr;
for (i = 0; i < len; i++) if (git__strtol32(&content, curly_braces_content, &end_ptr, 10) < 0)
if (!git__isdigit(str[i])) return 0; return -1;
if (*end_ptr != '\0')
return -1;
return 1; *n = content;
return 0;
} }
static int walk_ref_history(git_object **out, git_repository *repo, const char *refspec, const char *reflogspec) static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *spec, const char *identifier, unsigned int position)
{ {
git_reference *disambiguated = NULL; git_reference *ref = NULL;
git_reflog *reflog = NULL; git_reflog *reflog = NULL;
int n, retcode = GIT_ERROR; regex_t preg;
int i, refloglen; int numentries, i, cur, error = -1;
const git_reflog_entry *entry; const git_reflog_entry *entry;
const char *msg;
regmatch_t regexmatches[2];
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
size_t refspeclen = strlen(refspec);
size_t reflogspeclen = strlen(reflogspec);
if (git__prefixcmp(reflogspec, "@{") != 0 || cur = position;
git__suffixcmp(reflogspec, "}") != 0)
return revspec_error(reflogspec);
/* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ if (*identifier != '\0' || *base_ref != NULL)
if (!git__prefixcmp(reflogspec, "@{-")) { return revspec_error(spec);
regex_t regex;
int regex_error;
if (refspeclen > 0) if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0)
return revspec_error(reflogspec); return -1;
if (git__strtol32(&n, reflogspec+3, NULL, 10) < 0 || n < 1) if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
return revspec_error(reflogspec); goto cleanup;
if (!git_reference_lookup(&disambiguated, repo, "HEAD")) { if (git_reflog_read(&reflog, ref) < 0)
if (!git_reflog_read(&reflog, disambiguated)) { goto cleanup;
regex_error = regcomp(&regex, "checkout: moving from (.*) to .*", REG_EXTENDED);
if (regex_error != 0) {
giterr_set_regex(&regex, regex_error);
} else {
regmatch_t regexmatches[2];
retcode = GIT_ENOTFOUND; numentries = git_reflog_entrycount(reflog);
refloglen = git_reflog_entrycount(reflog); for (i = numentries - 1; i >= 0; i--) {
for (i=refloglen-1; i >= 0; i--) {
const char *msg;
entry = git_reflog_entry_byindex(reflog, i); entry = git_reflog_entry_byindex(reflog, i);
msg = git_reflog_entry_msg(entry); msg = git_reflog_entry_msg(entry);
if (!regexec(&regex, msg, 2, regexmatches, 0)) {
n--; if (regexec(&preg, msg, 2, regexmatches, 0))
if (!n) { continue;
cur--;
if (cur > 0)
continue;
git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so);
retcode = revparse_lookup_object(out, repo, git_buf_cstr(&buf));
break;
}
}
}
regfree(&regex);
}
}
}
} else {
int date_error = 0, result;
git_time_t timestamp;
git_buf datebuf = GIT_BUF_INIT;
result = disambiguate_refname(&disambiguated, repo, refspec); if ((error = disambiguate_refname(base_ref, repo, git_buf_cstr(&buf))) == 0)
goto cleanup;
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
error = maybe_sha_or_abbrev(out, repo, git_buf_cstr(&buf));
if (result < 0) {
retcode = result;
goto cleanup; goto cleanup;
} }
git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); error = GIT_ENOTFOUND;
date_error = git__date_parse(&timestamp, git_buf_cstr(&datebuf));
/* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ cleanup:
if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { git_reference_free(ref);
git_reference *tracking; git_buf_free(&buf);
regfree(&preg);
git_reflog_free(reflog);
return error;
}
if (!(retcode = git_reference_remote_tracking_from_branch(&tracking, disambiguated))) { static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned int identifier)
retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_reference_name(tracking)); {
git_reference_free(tracking); git_reflog *reflog;
} int error = -1;
} unsigned int numentries;
const git_reflog_entry *entry;
bool search_by_pos = (identifier <= 100000000);
/* @{N} -> Nth prior value for the ref (from reflog) */ if (git_reflog_read(&reflog, ref) < 0)
else if (all_chars_are_digits(reflogspec+2, reflogspeclen-3) && return -1;
!git__strtol32(&n, reflogspec+2, NULL, 10) &&
n <= 100000000) { /* Allow integer time */ numentries = git_reflog_entrycount(reflog);
git_buf_puts(&buf, git_reference_name(disambiguated)); if (search_by_pos) {
if (numentries < identifier + 1) {
if (n == 0) giterr_set(
retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); GITERR_REFERENCE,
else if (!git_reflog_read(&reflog, disambiguated)) { "Reflog for '%s' has only %d entries, asked for %d",
int numentries = git_reflog_entrycount(reflog); git_reference_name(ref),
if (numentries < n + 1) { numentries,
giterr_set(GITERR_REFERENCE, "Reflog for '%s' has only %d entries, asked for %d", identifier);
git_buf_cstr(&buf), numentries, n);
retcode = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
} else { goto cleanup;
const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, n);
const git_oid *oid = git_reflog_entry_oidold(entry);
retcode = git_object_lookup(out, repo, oid, GIT_OBJ_ANY);
}
}
} }
else if (!date_error) { entry = git_reflog_entry_byindex(reflog, identifier);
/* Ref as it was on a certain date */ git_oid_cpy(oid, git_reflog_entry_oidold(entry));
git_reflog *reflog; error = 0;
if (!git_reflog_read(&reflog, disambiguated)) { goto cleanup;
/* Keep walking until we find an entry older than the given date */
int numentries = git_reflog_entrycount(reflog); } else {
int i; int i;
git_time commit_time;
for (i = numentries - 1; i >= 0; i--) { for (i = numentries - 1; i >= 0; i--) {
const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, i); entry = git_reflog_entry_byindex(reflog, i);
git_time commit_time = git_reflog_entry_committer(entry)->when; commit_time = git_reflog_entry_committer(entry)->when;
if (commit_time.time - timestamp <= 0) {
retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY);
break;
}
}
if (i == -1) { if (commit_time.time - identifier > 0)
/* Didn't find a match */ continue;
retcode = GIT_ENOTFOUND;
}
git_reflog_free(reflog); git_oid_cpy(oid, git_reflog_entry_oidnew(entry));
} error = 0;
goto cleanup;
} }
git_buf_free(&datebuf); error = GIT_ENOTFOUND;
} }
cleanup: cleanup:
if (reflog)
git_reflog_free(reflog); git_reflog_free(reflog);
git_buf_free(&buf); return error;
git_reference_free(disambiguated);
return retcode;
} }
static git_object* dereference_object(git_object *obj) static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, unsigned int position)
{ {
git_otype type = git_object_type(obj); git_reference *ref;
git_oid oid;
int error = -1;
switch (type) { if (*base_ref == NULL) {
case GIT_OBJ_COMMIT: if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
{ return error;
git_tree *tree = NULL; } else {
if (0 == git_commit_tree(&tree, (git_commit*)obj)) { ref = *base_ref;
return (git_object*)tree; *base_ref = NULL;
}
}
break;
case GIT_OBJ_TAG:
{
git_object *newobj = NULL;
if (0 == git_tag_target(&newobj, (git_tag*)obj)) {
return newobj;
}
} }
break;
default: if (position == 0) {
case GIT_OBJ_TREE: error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY);
case GIT_OBJ_BLOB: goto cleanup;
case GIT_OBJ_OFS_DELTA:
case GIT_OBJ_REF_DELTA:
break;
} }
/* Can't dereference some types */ if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0)
return NULL; goto cleanup;
error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY);
cleanup:
git_reference_free(ref);
return error;
} }
static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo)
{ {
int retcode = 1; git_reference *tracking, *ref;
git_object *obj1 = obj, *obj2 = obj; int error = -1;
while (retcode > 0) {
git_otype this_type = git_object_type(obj1);
if (this_type == target_type) { if (*base_ref == NULL) {
*out = obj1; if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
retcode = 0; return error;
} else { } else {
/* Dereference once, if possible. */ ref = *base_ref;
obj2 = dereference_object(obj1); *base_ref = NULL;
if (!obj2) {
giterr_set(GITERR_REFERENCE, "Can't dereference to type");
retcode = GIT_ERROR;
}
}
if (obj1 != obj && obj1 != obj2) {
git_object_free(obj1);
} }
obj1 = obj2;
}
return retcode;
}
static git_otype parse_obj_type(const char *str) if ((error = git_reference_remote_tracking_from_branch(&tracking, ref)) < 0)
{ goto cleanup;
if (!strcmp(str, "{commit}")) return GIT_OBJ_COMMIT;
if (!strcmp(str, "{tree}")) return GIT_OBJ_TREE; *base_ref = tracking;
if (!strcmp(str, "{blob}")) return GIT_OBJ_BLOB;
if (!strcmp(str, "{tag}")) return GIT_OBJ_TAG; cleanup:
return GIT_OBJ_BAD; git_reference_free(ref);
return error;
} }
static int handle_caret_syntax(git_object **out, git_repository *repo, git_object *obj, const char *movement) static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, int identifier_len, git_repository* repo, const char *curly_braces_content)
{ {
git_commit *commit; bool is_numeric;
size_t movementlen = strlen(movement); int parsed, error = -1;
int n; git_buf identifier = GIT_BUF_INIT;
git_time_t timestamp;
if (*movement == '{') {
if (movement[movementlen-1] != '}')
return revspec_error(movement);
/* {} -> Dereference until we reach an object that isn't a tag. */
if (movementlen == 2) {
git_object *newobj = obj;
git_object *newobj2 = newobj;
while (git_object_type(newobj2) == GIT_OBJ_TAG) {
newobj2 = dereference_object(newobj);
if (newobj != obj) git_object_free(newobj);
if (!newobj2) {
giterr_set(GITERR_REFERENCE, "Couldn't find object of target type.");
return GIT_ERROR;
}
newobj = newobj2;
}
*out = newobj2;
return 0;
}
/* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ assert(*out == NULL);
if (movement[1] == '/') {
int retcode = GIT_ERROR;
git_revwalk *walk;
if (!git_revwalk_new(&walk, repo)) {
git_oid oid;
regex_t preg;
int reg_error;
git_buf buf = GIT_BUF_INIT;
git_revwalk_sorting(walk, GIT_SORT_TIME); if (git_buf_put(&identifier, spec, identifier_len) < 0)
git_revwalk_push(walk, git_object_id(obj)); return -1;
/* Extract the regex from the movement string */ is_numeric = !try_parse_numeric(&parsed, curly_braces_content);
git_buf_put(&buf, movement+2, strlen(movement)-3);
reg_error = regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED); if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) {
if (reg_error != 0) { error = revspec_error(spec);
giterr_set_regex(&preg, reg_error); goto cleanup;
} else {
while(!git_revwalk_next(&oid, walk)) {
git_object *walkobj;
/* Fetch the commit object, and check for matches in the message */
if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) {
if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) {
/* Found it! */
retcode = 0;
*out = walkobj;
if (obj == walkobj) {
/* Avoid leaking an object */
git_object_free(walkobj);
}
break;
}
git_object_free(walkobj);
}
}
if (retcode < 0) {
giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", movement);
}
regfree(&preg);
} }
git_buf_free(&buf); if (is_numeric) {
git_revwalk_free(walk); if (parsed < 0)
} error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, spec, git_buf_cstr(&identifier), -parsed);
return retcode; else
} error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed);
/* {...} -> Dereference until we reach an object of a certain type. */ goto cleanup;
if (dereference_to_type(out, obj, parse_obj_type(movement)) < 0) {
return GIT_ERROR;
}
return 0;
} }
/* Dereference until we reach a commit. */ if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) {
if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo);
/* Can't dereference to a commit; fail */
return GIT_ERROR;
}
/* "^" is the same as "^1" */ goto cleanup;
if (movementlen == 0) {
n = 1;
} else {
git__strtol32(&n, movement, NULL, 10);
} }
commit = (git_commit*)obj;
/* "^0" just returns the input */ if (git__date_parse(&timestamp, curly_braces_content) < 0)
goto cleanup;
error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (unsigned int)timestamp);
cleanup:
git_buf_free(&identifier);
return error;
}
static git_otype parse_obj_type(const char *str)
{
if (!strcmp(str, "commit"))
return GIT_OBJ_COMMIT;
if (!strcmp(str, "tree"))
return GIT_OBJ_TREE;
if (!strcmp(str, "blob"))
return GIT_OBJ_BLOB;
if (!strcmp(str, "tag"))
return GIT_OBJ_TAG;
return GIT_OBJ_BAD;
}
static int dereference_to_non_tag(git_object **out, git_object *obj)
{
if (git_object_type(obj) == GIT_OBJ_TAG)
return git_tag_peel(out, (git_tag *)obj);
return git_object__dup(out, obj);
}
static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n)
{
git_object *temp_commit = NULL;
int error;
if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
return -1;
if (n == 0) { if (n == 0) {
*out = obj; *out = temp_commit;
return 0; return 0;
} }
if (git_commit_parent(&commit, commit, n-1) < 0) { error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1);
return GIT_ENOTFOUND;
}
*out = (git_object*)commit; git_object_free(temp_commit);
return 0; return error;
} }
static int handle_linear_syntax(git_object **out, git_object *obj, const char *movement) static int handle_linear_syntax(git_object **out, git_object *obj, int n)
{ {
int n; git_object *temp_commit = NULL;
int error;
/* Dereference until we reach a commit. */ if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { return -1;
/* Can't dereference to a commit; fail */
return GIT_ERROR;
}
/* "~" is the same as "~1" */ error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
if (*movement == '\0') {
n = 1;
} else if (git__strtol32(&n, movement, NULL, 10) < 0) {
return GIT_ERROR;
}
return git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)obj, n); git_object_free(temp_commit);
return error;
} }
static int handle_colon_syntax(git_object **out, static int handle_colon_syntax(
git_repository *repo, git_object **out,
git_object *obj, git_object *obj,
const char *path) const char *path)
{ {
git_object *tree = obj; git_object *tree;
int error = -1; int error = -1;
git_tree_entry *entry = NULL; git_tree_entry *entry = NULL;
/* Dereference until we reach a tree. */ if (git_object_peel(&tree, obj, GIT_OBJ_TREE) < 0)
if (dereference_to_type(&tree, obj, GIT_OBJ_TREE) < 0) return -1;
return GIT_ERROR;
if (*path == '\0') if (*path == '\0') {
return git_object_lookup(out, repo, git_object_id(tree), GIT_OBJ_TREE); *out = tree;
return 0;
}
/* /*
* TODO: Handle the relative path syntax * TODO: Handle the relative path syntax
...@@ -543,188 +464,367 @@ static int handle_colon_syntax(git_object **out, ...@@ -543,188 +464,367 @@ static int handle_colon_syntax(git_object **out,
if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0)
goto cleanup; goto cleanup;
error = git_tree_entry_to_object(out, repo, entry); error = git_tree_entry_to_object(out, git_object_owner(tree), entry);
cleanup: cleanup:
git_tree_entry_free(entry); git_tree_entry_free(entry);
if (tree != obj)
git_object_free(tree); git_object_free(tree);
return error; return error;
} }
static int revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex)
{ {
git_revwalk *walk; int error;
int retcode = GIT_ERROR; git_oid oid;
git_object *obj;
if (!pattern[0]) { while (!(error = git_revwalk_next(&oid, walk))) {
giterr_set(GITERR_REGEX, "Empty pattern");
return GIT_ERROR; if ((error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT) < 0) &&
(error != GIT_ENOTFOUND))
return -1;
if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) {
*out = obj;
return 0;
} }
if (!git_revwalk_new(&walk, repo)) { git_object_free(obj);
}
if (error < 0 && error == GIT_REVWALKOVER)
error = GIT_ENOTFOUND;
return error;
}
static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern)
{
regex_t preg; regex_t preg;
int reg_error; git_revwalk *walk = NULL;
git_oid oid; int error = -1;
if (build_regex(&preg, pattern) < 0)
return -1;
if (git_revwalk_new(&walk, repo) < 0)
goto cleanup;
git_revwalk_sorting(walk, GIT_SORT_TIME); git_revwalk_sorting(walk, GIT_SORT_TIME);
git_revwalk_push_glob(walk, "refs/heads/*");
reg_error = regcomp(&preg, pattern, REG_EXTENDED); if (spec_oid == NULL) {
if (reg_error != 0) { // TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails
giterr_set_regex(&preg, reg_error); if (git_revwalk_push_glob(walk, "refs/heads/*") < 0)
} else { goto cleanup;
git_object *walkobj = NULL, *resultobj = NULL; } else if (git_revwalk_push(walk, spec_oid) < 0)
while(!git_revwalk_next(&oid, walk)) { goto cleanup;
/* Fetch the commit object, and check for matches in the message */
if (walkobj != resultobj) git_object_free(walkobj); error = walk_and_search(out, walk, &preg);
if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) {
if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { cleanup:
/* Match! */
resultobj = walkobj;
retcode = 0;
break;
}
}
}
if (!resultobj) {
giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern);
retcode = GIT_ENOTFOUND;
git_object_free(walkobj);
} else {
*out = resultobj;
}
regfree(&preg); regfree(&preg);
git_revwalk_free(walk); git_revwalk_free(walk);
return error;
}
static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content)
{
git_otype expected_type;
if (*curly_braces_content == '\0')
return dereference_to_non_tag(out, obj);
if (*curly_braces_content == '/')
return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1);
expected_type = parse_obj_type(curly_braces_content);
if (expected_type == GIT_OBJ_BAD)
return -1;
return git_object_peel(out, obj, expected_type);
}
static int extract_curly_braces_content(git_buf *buf, const char *spec, int *pos)
{
git_buf_clear(buf);
assert(spec[*pos] == '^' || spec[*pos] == '@');
(*pos)++;
if (spec[*pos] == '\0' || spec[*pos] != '{')
return revspec_error(spec);
(*pos)++;
while (spec[*pos] != '}') {
if (spec[*pos] == '\0')
return revspec_error(spec);
git_buf_putc(buf, spec[(*pos)++]);
} }
(*pos)++;
return 0;
}
static int extract_path(git_buf *buf, const char *spec, int *pos)
{
git_buf_clear(buf);
assert(spec[*pos] == ':');
(*pos)++;
if (git_buf_puts(buf, spec + *pos) < 0)
return -1;
*pos += git_buf_len(buf);
return 0;
}
static int extract_how_many(int *n, const char *spec, int *pos)
{
const char *end_ptr;
int parsed, accumulated;
char kind = spec[*pos];
assert(spec[*pos] == '^' || spec[*pos] == '~');
accumulated = 0;
do {
do {
(*pos)++;
accumulated++;
} while (spec[(*pos)] == kind && kind == '~');
if (git__isdigit(spec[*pos])) {
if ((git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0) < 0)
return revspec_error(spec);
accumulated += (parsed - 1);
*pos = end_ptr - spec;
} }
return retcode; } while (spec[(*pos)] == kind && kind == '~');
*n = accumulated;
return 0;
}
static int object_from_reference(git_object **object, git_reference *reference)
{
git_reference *resolved = NULL;
int error;
if (git_reference_resolve(&resolved, reference) < 0)
return -1;
error = git_object_lookup(object, reference->owner, git_reference_oid(resolved), GIT_OBJ_ANY);
git_reference_free(resolved);
return error;
}
static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, int identifier_len, git_repository *repo, bool allow_empty_identifier)
{
int error;
git_buf identifier = GIT_BUF_INIT;
if (*object != NULL)
return 0;
if (*reference != NULL) {
if ((error = object_from_reference(object, *reference)) < 0)
return error;
git_reference_free(*reference);
*reference = NULL;
return 0;
}
if (!allow_empty_identifier && identifier_len == 0)
return revspec_error(spec);
if (git_buf_put(&identifier, spec, identifier_len) < 0)
return -1;
error = revparse_lookup_object(object, repo, git_buf_cstr(&identifier));
git_buf_free(&identifier);
return error;
}
static int ensure_base_rev_is_not_known_yet(git_object *object, const char *spec)
{
if (object == NULL)
return 0;
return revspec_error(spec);
}
static bool any_left_hand_identifier(git_object *object, git_reference *reference, int identifier_len)
{
if (object != NULL)
return true;
if (reference != NULL)
return true;
if (identifier_len > 0)
return true;
return false;
}
static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference, const char *spec)
{
if (!ensure_base_rev_is_not_known_yet(object, spec) && reference == NULL)
return 0;
return revspec_error(spec);
} }
int git_revparse_single(git_object **out, git_repository *repo, const char *spec) int git_revparse_single(git_object **out, git_repository *repo, const char *spec)
{ {
revparse_state current_state = REVPARSE_STATE_INIT, next_state = REVPARSE_STATE_INIT; int pos = 0, identifier_len = 0;
const char *spec_cur = spec; int error = -1, n;
git_object *cur_obj = NULL, *next_obj = NULL; git_buf buf = GIT_BUF_INIT;
git_buf specbuffer = GIT_BUF_INIT, stepbuffer = GIT_BUF_INIT;
int retcode = 0; git_reference *reference = NULL;
git_object *base_rev = NULL;
assert(out && repo && spec); assert(out && repo && spec);
if (spec[0] == ':') { *out = NULL;
if (spec[1] == '/') {
return revparse_global_grep(out, repo, spec+2); do {
} switch (spec[pos]) {
/* TODO: support merge-stage path lookup (":2:Makefile"). */ case '^':
giterr_set(GITERR_INVALID, "Unimplemented"); if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
return GIT_ERROR; goto cleanup;
}
if (spec[pos+1] == '{') {
git_object *temp_object = NULL;
if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
goto cleanup;
if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
goto cleanup;
while (current_state != REVPARSE_STATE_DONE) { git_object_free(base_rev);
switch (current_state) { base_rev = temp_object;
case REVPARSE_STATE_INIT:
if (!*spec_cur) {
/* No operators, just a name. Find it and return. */
retcode = revparse_lookup_object(out, repo, spec);
next_state = REVPARSE_STATE_DONE;
} else if (*spec_cur == '@') {
/* '@' syntax doesn't allow chaining */
git_buf_puts(&stepbuffer, spec_cur);
retcode = walk_ref_history(out, repo, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer));
next_state = REVPARSE_STATE_DONE;
} else if (*spec_cur == '^') {
next_state = REVPARSE_STATE_CARET;
} else if (*spec_cur == '~') {
next_state = REVPARSE_STATE_LINEAR;
} else if (*spec_cur == ':') {
next_state = REVPARSE_STATE_COLON;
} else { } else {
git_buf_putc(&specbuffer, *spec_cur); git_object *temp_object = NULL;
}
spec_cur++; if ((error = extract_how_many(&n, spec, &pos)) < 0)
goto cleanup;
if (current_state != next_state && next_state != REVPARSE_STATE_DONE) { if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0)
/* Leaving INIT state, find the object specified, in case that state needs it */ goto cleanup;
if ((retcode = revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer))) < 0)
next_state = REVPARSE_STATE_DONE; git_object_free(base_rev);
base_rev = temp_object;
} }
break; break;
case '~':
{
git_object *temp_object = NULL;
if ((error = extract_how_many(&n, spec, &pos)) < 0)
goto cleanup;
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
goto cleanup;
case REVPARSE_STATE_CARET: if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0)
/* Gather characters until NULL, '~', or '^' */ goto cleanup;
if (!*spec_cur) {
retcode = handle_caret_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); git_object_free(base_rev);
next_state = REVPARSE_STATE_DONE; base_rev = temp_object;
} else if (*spec_cur == '~') { break;
retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer));
git_buf_clear(&stepbuffer);
next_state = !retcode ? REVPARSE_STATE_LINEAR : REVPARSE_STATE_DONE;
} else if (*spec_cur == '^') {
retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer));
git_buf_clear(&stepbuffer);
if (retcode < 0) {
next_state = REVPARSE_STATE_DONE;
} }
} else if (*spec_cur == ':') {
retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); case ':':
git_buf_clear(&stepbuffer); {
next_state = !retcode ? REVPARSE_STATE_COLON : REVPARSE_STATE_DONE; git_object *temp_object = NULL;
if ((error = extract_path(&buf, spec, &pos)) < 0)
goto cleanup;
if (any_left_hand_identifier(base_rev, reference, identifier_len)) {
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0)
goto cleanup;
if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
goto cleanup;
} else {
if (*git_buf_cstr(&buf) == '/') {
if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0)
goto cleanup;
} else { } else {
git_buf_putc(&stepbuffer, *spec_cur);
}
spec_cur++;
break;
case REVPARSE_STATE_LINEAR: /*
if (!*spec_cur) { * TODO: support merge-stage path lookup (":2:Makefile")
retcode = handle_linear_syntax(out, cur_obj, git_buf_cstr(&stepbuffer)); * and plain index blob lookup (:i-am/a/blob)
next_state = REVPARSE_STATE_DONE; */
} else if (*spec_cur == '~') { giterr_set(GITERR_INVALID, "Unimplemented");
retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); error = GIT_ERROR;
git_buf_clear(&stepbuffer); goto cleanup;
if (retcode < 0) {
next_state = REVPARSE_STATE_DONE;
} }
} else if (*spec_cur == '^') {
retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer));
git_buf_clear(&stepbuffer);
next_state = !retcode ? REVPARSE_STATE_CARET : REVPARSE_STATE_DONE;
} else {
git_buf_putc(&stepbuffer, *spec_cur);
} }
spec_cur++;
break;
case REVPARSE_STATE_COLON: git_object_free(base_rev);
if (*spec_cur) { base_rev = temp_object;
git_buf_putc(&stepbuffer, *spec_cur);
} else {
retcode = handle_colon_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer));
next_state = REVPARSE_STATE_DONE;
}
spec_cur++;
break; break;
}
case '@':
{
git_object *temp_object = NULL;
if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
goto cleanup;
if ((error = ensure_base_rev_is_not_known_yet(base_rev, spec)) < 0)
goto cleanup;
case REVPARSE_STATE_DONE: if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0)
if (cur_obj && *out != cur_obj) git_object_free(cur_obj); goto cleanup;
if (next_obj && *out != next_obj) git_object_free(next_obj);
if (temp_object != NULL)
base_rev = temp_object;
break; break;
} }
current_state = next_state; default:
if (cur_obj != next_obj) { if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference, spec)) < 0)
if (cur_obj) git_object_free(cur_obj); goto cleanup;
cur_obj = next_obj;
} pos++;
identifier_len++;
} }
} while (spec[pos]);
if (*out != cur_obj) git_object_free(cur_obj); if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0)
if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); goto cleanup;
*out = base_rev;
error = 0;
git_buf_free(&specbuffer); cleanup:
git_buf_free(&stepbuffer); if (error)
return retcode; git_object_free(base_rev);
git_reference_free(reference);
git_buf_free(&buf);
return error;
} }
#include "clar_libgit2.h"
static git_repository *g_repo;
void test_object_peel__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
}
void test_object_peel__cleanup(void)
{
git_repository_free(g_repo);
}
static void assert_peel(const char* expected_sha, const char *sha, git_otype requested_type)
{
git_oid oid, expected_oid;
git_object *obj;
git_object *peeled;
cl_git_pass(git_oid_fromstr(&oid, sha));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
cl_git_pass(git_object_peel(&peeled, obj, requested_type));
cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
cl_assert_equal_i(0, git_oid_cmp(&expected_oid, git_object_id(peeled)));
git_object_free(peeled);
git_object_free(obj);
}
static void assert_peel_error(int error, const char *sha, git_otype requested_type)
{
git_oid oid;
git_object *obj;
git_object *peeled;
cl_git_pass(git_oid_fromstr(&oid, sha));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type));
git_object_free(obj);
}
void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void)
{
assert_peel("e90810b8df3e80c413d903f631643c716887138d", "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TAG);
assert_peel("53fc32d17276939fc79ed05badaef2db09990016", "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_TREE);
assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_BLOB);
}
void test_object_peel__can_peel_a_tag(void)
{
assert_peel("e90810b8df3e80c413d903f631643c716887138d", "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_COMMIT);
assert_peel("53fc32d17276939fc79ed05badaef2db09990016", "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJ_TREE);
}
void test_object_peel__can_peel_a_commit(void)
{
assert_peel("53fc32d17276939fc79ed05badaef2db09990016", "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_TREE);
}
void test_object_peel__cannot_peel_a_tree(void)
{
assert_peel_error(GIT_EAMBIGUOUS, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJ_BLOB);
}
void test_object_peel__cannot_peel_a_blob(void)
{
assert_peel_error(GIT_ERROR, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJ_COMMIT);
}
void test_object_peel__cannot_target_any_object(void)
{
assert_peel_error(GIT_EAMBIGUOUS, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_ANY);
}
...@@ -74,7 +74,11 @@ void test_refs_revparse__shas(void) ...@@ -74,7 +74,11 @@ void test_refs_revparse__shas(void)
void test_refs_revparse__head(void) void test_refs_revparse__head(void)
{ {
test_object("", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
} }
void test_refs_revparse__full_refs(void) void test_refs_revparse__full_refs(void)
...@@ -99,12 +103,18 @@ void test_refs_revparse__describe_output(void) ...@@ -99,12 +103,18 @@ void test_refs_revparse__describe_output(void)
void test_refs_revparse__nth_parent(void) void test_refs_revparse__nth_parent(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "be3563a^-1"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "^"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "be3563a^{tree}^"));
test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("be3563a^^", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("be3563a^{commit}^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^42", NULL); test_object("be3563a^42", NULL);
} }
...@@ -113,34 +123,56 @@ void test_refs_revparse__not_tag(void) ...@@ -113,34 +123,56 @@ void test_refs_revparse__not_tag(void)
{ {
test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master^{tree}^{}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
test_object("tags/e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
test_object("e908^{}", "e90810b8df3e80c413d903f631643c716887138d");
} }
void test_refs_revparse__to_type(void) void test_refs_revparse__to_type(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{trip}"));
test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("master^{commit}^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
} }
void test_refs_revparse__linear_history(void) void test_refs_revparse__linear_history(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "~"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "foo~bar")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "foo~bar"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~bar")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~bar"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~-1"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~0bar"));
test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a"); test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~~", "9fd738e8f7967c078dceed8190330fc8648ee56a");
} }
void test_refs_revparse__chaining(void) void test_refs_revparse__chaining(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{0}@{0}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{u}@{-1}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-1}@{-1}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-3}@{0}"));
test_object("master@{0}~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("@{u}@{0}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("@{-1}@{0}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
test_object("@{-4}@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("master^^2^", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479"); test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479");
test_object("master^^1^2^1", NULL);
} }
void test_refs_revparse__upstream(void) void test_refs_revparse__upstream(void)
...@@ -159,6 +191,10 @@ void test_refs_revparse__ordinal(void) ...@@ -159,6 +191,10 @@ void test_refs_revparse__ordinal(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{-2}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{-2}"));
/* TODO: make the test below actually fail
* cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{1a}"));
*/
test_object("nope@{0}", NULL); test_object("nope@{0}", NULL);
test_object("master@{31415}", NULL); test_object("master@{31415}", NULL);
test_object("@{1000}", NULL); test_object("@{1000}", NULL);
...@@ -177,6 +213,7 @@ void test_refs_revparse__previous_head(void) ...@@ -177,6 +213,7 @@ void test_refs_revparse__previous_head(void)
{ {
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-1b}"));
test_object("@{-42}", NULL); test_object("@{-42}", NULL);
...@@ -353,6 +390,7 @@ void test_refs_revparse__colon(void) ...@@ -353,6 +390,7 @@ void test_refs_revparse__colon(void)
test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd"); test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"); test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
test_object("test/master@{1}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
} }
void test_refs_revparse__disambiguation(void) void test_refs_revparse__disambiguation(void)
......
0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git 0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git
be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0900 commit: be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0900 commit:
a65fedf39aefe402d3bb6e24df4d4f5fe4547750 5b5b025afb0b4c913b4c338a42934a3863bf3644 Ben Straub <bstraub@github.com> 1335806604 -0900 checkout: moving from master to 5b5b025
5b5b025afb0b4c913b4c338a42934a3863bf3644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806605 -0900 checkout: moving from 5b5b025 to master
a65fedf39aefe402d3bb6e24df4d4f5fe4547750 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0900 checkout: moving from master to br2 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0900 checkout: moving from master to br2
c47800c7266a2be04c571c04d5a6614691ea99bd a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0900 commit: checking in c47800c7266a2be04c571c04d5a6614691ea99bd a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0900 commit: checking in
a4a7dce85cf63874e984719f4fdd239f5145052f a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806621 -0900 checkout: moving from br2 to master a4a7dce85cf63874e984719f4fdd239f5145052f a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806621 -0900 checkout: moving from br2 to master
0000000000000000000000000000000000000000 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806565 -0800 update by push
a65fedf39aefe402d3bb6e24df4d4f5fe4547750 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806688 -0800 update by push
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment