From ac250c56c7d7bb11691c9dfbcd0dbf580d85e177 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 25 Apr 2012 16:24:22 -0700 Subject: [PATCH 01/45] First stab at implementation of rev-parse. This version supports refspecs of these kinds: - Full & partial SHAs - Output from "git describe" - "/refs/heads/master" (full ref names) - "master" (partial ref names) - "FETCH_HEAD" (named heads) --- include/git2/revparse.h | 22 +++++ src/revparse.c | 175 +++++++++++++++++++++++++++++++++++++ tests-clar/refs/revparse.c | 74 ++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 include/git2/revparse.h create mode 100644 src/revparse.c create mode 100644 tests-clar/refs/revparse.c diff --git a/include/git2/revparse.h b/include/git2/revparse.h new file mode 100644 index 000000000..3fd69d91d --- /dev/null +++ b/include/git2/revparse.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_revparse_h__ +#define INCLUDE_git_revparse_h__ + +#include "common.h" +#include "types.h" + + +GIT_BEGIN_DECL + +GIT_EXTERN(int) git_revparse_single(git_object **out, git_repository *repo, const char *spec); + +//GIT_EXTERN(int) git_revparse_multi(TODO); + +GIT_END_DECL + +#endif diff --git a/src/revparse.c b/src/revparse.c new file mode 100644 index 000000000..d6a7ebd19 --- /dev/null +++ b/src/revparse.c @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include + +#include "common.h" +#include "buffer.h" + +#include "git2/revparse.h" +#include "git2/object.h" +#include "git2/oid.h" +#include "git2/refs.h" + +GIT_BEGIN_DECL + +typedef enum { + REVPARSE_STATE_INIT, + + /* for parsing "@{...}" */ + REVPARSE_STATE_REF_A, + REVPARSE_STATE_REF_B, + + /* for "^{...}" and ^... */ + REVPARSE_STATE_PARENTS_A, + REVPARSE_STATE_PARENTS_B, + + /* For "~..." */ + REVPARSE_STATE_LIINEAR, + + /* For joining parents and linear, as in "master^2~3^2" */ + REVPARSE_STATE_JOIN, +} revparse_state; + +static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec) +{ + git_reference *ref; + git_object *obj = NULL; + + if (!git_reference_lookup(&ref, repo, spec)) { + git_reference *resolved_ref; + if (!git_reference_resolve(&resolved_ref, ref)) { + if (!git_object_lookup(&obj, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY)) { + *out = obj; + } + git_reference_free(resolved_ref); + } + git_reference_free(ref); + } + if (obj) { + return 0; + } + + return GIT_ERROR; +} + +static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) +{ + size_t speclen = strlen(spec); + git_object *obj = NULL; + git_oid oid; + git_buf refnamebuf = GIT_BUF_INIT; + static const char* formatters[] = { + "refs/%s", + "refs/tags/%s", + "refs/heads/%s", + "refs/remotes/%s", + "refs/remotes/%s/HEAD", + NULL + }; + unsigned int i; + const char *substr; + + /* "git describe" output; snip everything before/including "-g" */ + substr = strstr(spec, "-g"); + if (substr) { + spec = substr + 2; + speclen = strlen(spec); + } + + /* SHA or prefix */ + if (!git_oid_fromstrn(&oid, spec, speclen)) { + if (!git_object_lookup_prefix(&obj, repo, &oid, speclen, GIT_OBJ_ANY)) { + *out = obj; + return 0; + } + } + + /* Fully-named ref */ + if (!revparse_lookup_fully_qualifed_ref(&obj, repo, spec)) { + *out = obj; + return 0; + } + + /* Partially-named ref; match in this order: */ + for (i=0; formatters[i]; i++) { + git_buf_clear(&refnamebuf); + if (git_buf_printf(&refnamebuf, formatters[i], spec) < 0) { + return GIT_ERROR; + } + + if (!revparse_lookup_fully_qualifed_ref(&obj, repo, git_buf_cstr(&refnamebuf))) { + git_buf_free(&refnamebuf); + *out = obj; + return 0; + } + } + git_buf_free(&refnamebuf); + + giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); + return GIT_ERROR; +} + + +static void set_invalid_syntax_err(const char *spec) +{ + giterr_set(GITERR_INVALID, "Refspec '%s' is not valid.", spec); +} + + +int git_revparse_single(git_object **out, git_repository *repo, const char *spec) +{ + revparse_state current_state = REVPARSE_STATE_INIT; + revparse_state next_state = REVPARSE_STATE_INIT; + const char *spec_cur = spec; + git_object *obj = NULL; + git_buf specbuffer = GIT_BUF_INIT; + git_buf stepbuffer = GIT_BUF_INIT; + + assert(out && repo && spec); + + while (1) { + switch (current_state) { + case REVPARSE_STATE_INIT: + if (!*spec_cur) { + /* No operators, just a name. Find it and return. */ + return revparse_lookup_object(out, repo, spec); + } else if (*spec_cur == '@') { + next_state = REVPARSE_STATE_REF_A; + } + spec_cur++; + + if (current_state != next_state) { + /* Leaving INIT state, find the object specified and carry on */ + assert(!git_buf_set(&specbuffer, spec, spec_cur - spec)); + assert(!revparse_lookup_object(&obj, repo, git_buf_cstr(&specbuffer))); + } + break; + + case REVPARSE_STATE_REF_A: + /* Found '@', look for '{', fail otherwise */ + if (*spec_cur != '{') { + set_invalid_syntax_err(spec); + return GIT_ERROR; + } + spec_cur++; + next_state = REVPARSE_STATE_REF_B; + break; + + case REVPARSE_STATE_REF_B: + /* Found "@{", gather things until a '}' */ + break; + } + + current_state = next_state; + } + + return 0; +} + + +GIT_END_DECL diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c new file mode 100644 index 000000000..05c12b074 --- /dev/null +++ b/tests-clar/refs/revparse.c @@ -0,0 +1,74 @@ +#include "clar_libgit2.h" + +#include "git2/revparse.h" + +static git_repository *g_repo; +static git_object *g_obj; + + + +// Hepers +static void oid_str_cmp(const git_oid *oid, const char *str) +{ + git_oid oid2; + cl_git_pass(git_oid_fromstr(&oid2, str)); + cl_assert(0 == git_oid_cmp(oid, &oid2)); +} + + +void test_refs_revparse__initialize(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_refs_revparse__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_obj = NULL; +} + + +void test_refs_revparse__shas(void) +{ + // Full SHA should return a valid object + cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c7266a2be04c571c04d5a6614691ea99bd")); + oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c")); + oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); +} + +void test_refs_revparse__head(void) +{ + // Named head should return a valid object + cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD")); + oid_str_cmp(git_object_id(g_obj), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); +} + +void test_refs_revparse__full_refs(void) +{ + // Fully-qualified refs should return valid objects + cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/master")); + oid_str_cmp(git_object_id(g_obj), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/test")); + oid_str_cmp(git_object_id(g_obj), "e90810b8df3e80c413d903f631643c716887138d"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/tags/test")); + oid_str_cmp(git_object_id(g_obj), "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); +} + +void test_refs_revparse__partial_refs(void) +{ + // Partially-qualified refs should return valid objects + cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob")); + oid_str_cmp(git_object_id(g_obj), "1385f264afb75a56a5bec74243be9b367ba4ca08"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "packed-test")); + oid_str_cmp(git_object_id(g_obj), "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "br2")); + oid_str_cmp(git_object_id(g_obj), "a4a7dce85cf63874e984719f4fdd239f5145052f"); +} + +void test_refs_revparse__describe_output(void) +{ + cl_git_pass(git_revparse_single(&g_obj, g_repo, "blah-7-gc47800c")); + oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); +} + From 023c6f69edcacfefe36a2b7d606def32d5bc246c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 25 Apr 2012 19:08:17 -0700 Subject: [PATCH 02/45] Simpler states and initial structure. New tests for "foo^2" syntax, but they don't pass yet. Support for chaining these, i.e. "foo^2~3^{u}~1' is starting to shape up. --- src/revparse.c | 150 ++++++++++++++++++++++++++++++------- tests-clar/refs/revparse.c | 16 ++++ 2 files changed, 139 insertions(+), 27 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index d6a7ebd19..f9213d9be 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -20,21 +20,18 @@ GIT_BEGIN_DECL typedef enum { REVPARSE_STATE_INIT, - /* for parsing "@{...}" */ - REVPARSE_STATE_REF_A, - REVPARSE_STATE_REF_B, - /* for "^{...}" and ^... */ - REVPARSE_STATE_PARENTS_A, - REVPARSE_STATE_PARENTS_B, + REVPARSE_STATE_CARET, /* For "~..." */ - REVPARSE_STATE_LIINEAR, - - /* For joining parents and linear, as in "master^2~3^2" */ - REVPARSE_STATE_JOIN, + REVPARSE_STATE_LINEAR, } revparse_state; +static void set_invalid_syntax_err(const char *spec) +{ + giterr_set(GITERR_INVALID, "Refspec '%s' is not valid.", spec); +} + static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec) { git_reference *ref; @@ -115,20 +112,94 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const } -static void set_invalid_syntax_err(const char *spec) +static int walk_ref_history(git_object **out, const char *refspec, const char *reflogspec) { - giterr_set(GITERR_INVALID, "Refspec '%s' is not valid.", spec); + // TODO + + /* Empty refspec means current branch */ + + /* Possible syntaxes for reflogspec: + * "8" -> 8th prior value for the ref + * "-2" -> nth branch checked out before the current one (refspec must be empty) + * "yesterday", "1 month 2 weeks 3 days 4 hours 5 seconds ago", "1979-02-26 18:30:00" + * -> value of ref at given point in time + * "upstream" or "u" -> branch the ref is set to build on top of + * */ + return 0; } +static git_object* dereference_object(git_object *obj) +{ + git_otype type = git_object_type(obj); + git_object *newobj = NULL; + + switch (type) { + case GIT_OBJ_COMMIT: + break; + case GIT_OBJ_TREE: + break; + case GIT_OBJ_BLOB: + break; + case GIT_OBJ_TAG: + break; + case GIT_OBJ_OFS_DELTA: + break; + case GIT_OBJ_REF_DELTA: + break; + }} + +static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) +{ + git_otype this_type = git_object_type(obj); + + while (1) { + if (this_type == target_type) { + *out = obj; + return 0; + } + + /* Dereference once, if possible. */ + obj = dereference_object(obj); + + } +} + +static int handle_caret_syntax(git_object **out, git_object *start, const char *movement) +{ + git_object *obj; + + printf("Moving by '%s'\n", movement); + + if (*movement == '{') { + if (movement[strlen(movement)-1] != '}') { + set_invalid_syntax_err(movement); + return GIT_ERROR; + } + + // TODO + /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ + /* {} -> Dereference until we reach an object that isn't a tag. */ + /* {...} -> Dereference until we reach an object of a certain type. */ + } + + /* Dereference until we reach a commit. */ + if (dereference_to_type(&obj, start, GIT_OBJ_COMMIT) < 0) { + } + + /* Move to the Nth parent. */ + + return 0; +} int git_revparse_single(git_object **out, git_repository *repo, const char *spec) { revparse_state current_state = REVPARSE_STATE_INIT; revparse_state next_state = REVPARSE_STATE_INIT; const char *spec_cur = spec; - git_object *obj = NULL; + git_object *cur_obj = NULL; git_buf specbuffer = GIT_BUF_INIT; git_buf stepbuffer = GIT_BUF_INIT; + int retcode = 0; assert(out && repo && spec); @@ -139,36 +210,61 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec /* No operators, just a name. Find it and return. */ return revparse_lookup_object(out, repo, spec); } else if (*spec_cur == '@') { - next_state = REVPARSE_STATE_REF_A; + git_buf_puts(&stepbuffer, spec_cur); + retcode = walk_ref_history(out, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); + goto cleanup; + } else if (*spec_cur == '^') { + next_state = REVPARSE_STATE_CARET; + } else if (*spec_cur == '~') { + next_state = REVPARSE_STATE_LINEAR; + } else { + git_buf_putc(&specbuffer, *spec_cur); } spec_cur++; if (current_state != next_state) { - /* Leaving INIT state, find the object specified and carry on */ - assert(!git_buf_set(&specbuffer, spec, spec_cur - spec)); - assert(!revparse_lookup_object(&obj, repo, git_buf_cstr(&specbuffer))); + /* Leaving INIT state, find the object specified, in case that state needs it */ + assert(!revparse_lookup_object(&cur_obj, repo, git_buf_cstr(&specbuffer))); } break; - case REVPARSE_STATE_REF_A: - /* Found '@', look for '{', fail otherwise */ - if (*spec_cur != '{') { - set_invalid_syntax_err(spec); - return GIT_ERROR; + + case REVPARSE_STATE_CARET: + /* Gather characters until NULL, '~', or '^' */ + if (!*spec_cur) { + retcode = handle_caret_syntax(out, + cur_obj, + git_buf_cstr(&stepbuffer)); + goto cleanup; + } else if (*spec_cur == '~') { + retcode = handle_caret_syntax(&cur_obj, + cur_obj, + git_buf_cstr(&stepbuffer)); + if (retcode < 0) goto cleanup; + next_state = REVPARSE_STATE_LINEAR; + } else if (*spec_cur == '^') { + retcode = handle_caret_syntax(&cur_obj, + cur_obj, + git_buf_cstr(&stepbuffer)); + if (retcode < 0) goto cleanup; + next_state = REVPARSE_STATE_CARET; + } else { + git_buf_putc(&stepbuffer, *spec_cur); } spec_cur++; - next_state = REVPARSE_STATE_REF_B; break; - case REVPARSE_STATE_REF_B: - /* Found "@{", gather things until a '}' */ + case REVPARSE_STATE_LINEAR: break; } current_state = next_state; } - - return 0; + +cleanup: + git_buf_free(&specbuffer); + git_buf_free(&stepbuffer); + return retcode; } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 05c12b074..90aaf5423 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -72,3 +72,19 @@ void test_refs_revparse__describe_output(void) oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); } +void test_refs_revparse__nth_parent(void) +{ + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1")); + oid_str_cmp(git_object_id(g_obj), "9fd738e8f7967c078dceed8190330fc8648ee56a"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2")); + oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1^1")); + oid_str_cmp(git_object_id(g_obj), "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2^1")); + oid_str_cmp(git_object_id(g_obj), "5b5b025afb0b4c913b4c338a42934a3863bf3644"); +} + +void test_refs_revparse__reflog(void) +{ + // TODO: how to create a fixture for this? git_reflog_write? +} From f597ea8978bf10f51fabed6cbafbd9625603b09d Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 26 Apr 2012 13:06:46 -0700 Subject: [PATCH 03/45] Implemented partial caret syntax for rev-parse. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Supported forms: - "^n" - "^0" - "^" Still missing: all of the "^{…}" variants. --- src/revparse.c | 55 +++++++++++++++++++++++++++++++++----- tests-clar/refs/revparse.c | 4 +++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index f9213d9be..3a1cd0598 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -14,6 +14,8 @@ #include "git2/object.h" #include "git2/oid.h" #include "git2/refs.h" +#include "git2/tag.h" +#include "git2/commit.h" GIT_BEGIN_DECL @@ -146,7 +148,14 @@ static git_object* dereference_object(git_object *obj) break; case GIT_OBJ_REF_DELTA: break; - }} + + default: + break; + } + + /* Can't dereference some types */ + return NULL; +} static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) { @@ -158,6 +167,11 @@ static int dereference_to_type(git_object **out, git_object *obj, git_otype targ return 0; } + if (this_type == GIT_OBJ_TAG) { + git_tag_peel(&obj, (git_tag*)obj); + continue; + } + /* Dereference once, if possible. */ obj = dereference_object(obj); @@ -167,8 +181,8 @@ static int dereference_to_type(git_object **out, git_object *obj, git_otype targ static int handle_caret_syntax(git_object **out, git_object *start, const char *movement) { git_object *obj; - - printf("Moving by '%s'\n", movement); + git_commit *commit; + int n; if (*movement == '{') { if (movement[strlen(movement)-1] != '}') { @@ -184,10 +198,29 @@ static int handle_caret_syntax(git_object **out, git_object *start, const char * /* Dereference until we reach a commit. */ if (dereference_to_type(&obj, start, GIT_OBJ_COMMIT) < 0) { + /* Can't dereference to a commit; fail */ + return GIT_ERROR; } - /* Move to the Nth parent. */ + /* "^" is the same as "^1" */ + if (strlen(movement) == 0) { + n = 1; + } else { + git__strtol32(&n, movement, NULL, 0); + } + commit = (git_commit*)obj; + /* "^0" just returns the input */ + if (n == 0) { + *out = (git_object*)commit; + return 0; + } + + if (git_commit_parent(&commit, commit, n-1) < 0) { + return GIT_ERROR; + } + + *out = (git_object*)commit; return 0; } @@ -197,6 +230,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec revparse_state next_state = REVPARSE_STATE_INIT; const char *spec_cur = spec; git_object *cur_obj = NULL; + git_object *next_obj = NULL; git_buf specbuffer = GIT_BUF_INIT; git_buf stepbuffer = GIT_BUF_INIT; int retcode = 0; @@ -210,6 +244,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec /* No operators, just a name. Find it and return. */ return revparse_lookup_object(out, repo, spec); } else if (*spec_cur == '@') { + /* '@' syntax doesn't allow chaining */ git_buf_puts(&stepbuffer, spec_cur); retcode = walk_ref_history(out, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); goto cleanup; @@ -224,7 +259,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec if (current_state != next_state) { /* Leaving INIT state, find the object specified, in case that state needs it */ - assert(!revparse_lookup_object(&cur_obj, repo, git_buf_cstr(&specbuffer))); + assert(!revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer))); } break; @@ -237,15 +272,17 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec git_buf_cstr(&stepbuffer)); goto cleanup; } else if (*spec_cur == '~') { - retcode = handle_caret_syntax(&cur_obj, + retcode = handle_caret_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); if (retcode < 0) goto cleanup; next_state = REVPARSE_STATE_LINEAR; } else if (*spec_cur == '^') { - retcode = handle_caret_syntax(&cur_obj, + retcode = handle_caret_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); if (retcode < 0) goto cleanup; next_state = REVPARSE_STATE_CARET; } else { @@ -259,6 +296,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } current_state = next_state; + if (cur_obj != next_obj) { + git_object_free(cur_obj); + cur_obj = next_obj; + } } cleanup: diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 90aaf5423..9ff3bd1da 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -76,12 +76,16 @@ void test_refs_revparse__nth_parent(void) { cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1")); oid_str_cmp(git_object_id(g_obj), "9fd738e8f7967c078dceed8190330fc8648ee56a"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^")); + oid_str_cmp(git_object_id(g_obj), "9fd738e8f7967c078dceed8190330fc8648ee56a"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2")); oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1^1")); oid_str_cmp(git_object_id(g_obj), "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2^1")); oid_str_cmp(git_object_id(g_obj), "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^0")); + oid_str_cmp(git_object_id(g_obj), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); } void test_refs_revparse__reflog(void) From 9d7bdf7119fe7858fdaa6e79283dacbf98c4128c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 26 Apr 2012 18:15:43 -0700 Subject: [PATCH 04/45] Implemented rev-parse's "^{}" syntax. --- src/revparse.c | 78 ++++++++++++++---- tests-clar/refs/revparse.c | 57 ++++++++----- .../d7/eddec7b3610726791785bf839065953f10341b | Bin 0 -> 134 bytes .../resources/testrepo.git/refs/tags/hard_tag | Bin 0 -> 41 bytes .../testrepo.git/refs/tags/wrapped_tag | Bin 0 -> 41 bytes 5 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b create mode 100644 tests/resources/testrepo.git/refs/tags/hard_tag create mode 100644 tests/resources/testrepo.git/refs/tags/wrapped_tag diff --git a/src/revparse.c b/src/revparse.c index 3a1cd0598..da274f860 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -143,6 +143,9 @@ static git_object* dereference_object(git_object *obj) case GIT_OBJ_BLOB: break; case GIT_OBJ_TAG: + if (0 == git_tag_target(&newobj, (git_tag*)obj)) { + return newobj; + } break; case GIT_OBJ_OFS_DELTA: break; @@ -159,28 +162,36 @@ static git_object* dereference_object(git_object *obj) static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) { - git_otype this_type = git_object_type(obj); + git_object *obj1 = obj, *obj2 = obj; while (1) { + git_otype this_type = git_object_type(obj1); + if (this_type == target_type) { *out = obj; return 0; } - if (this_type == GIT_OBJ_TAG) { - git_tag_peel(&obj, (git_tag*)obj); - continue; - } - /* Dereference once, if possible. */ - obj = dereference_object(obj); - + obj2 = dereference_object(obj1); + if (obj2 != obj) { + git_object_free(obj2); + } + obj1 = obj2; } } -static int handle_caret_syntax(git_object **out, git_object *start, const char *movement) +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 handle_caret_syntax(git_object **out, git_object *obj, const char *movement) { - git_object *obj; git_commit *commit; int n; @@ -189,15 +200,40 @@ static int handle_caret_syntax(git_object **out, git_object *start, const char * set_invalid_syntax_err(movement); return GIT_ERROR; } - - // TODO - /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ + /* {} -> Dereference until we reach an object that isn't a tag. */ + if (strlen(movement) == 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 = newobj; + } + *out = newobj2; + return 0; + } + + /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ + if (movement[1] == '/') { + // TODO + return GIT_ERROR; + } + /* {...} -> Dereference until we reach an object of a certain type. */ + if (dereference_to_type(out, obj, parse_obj_type(movement)) < 0) { + giterr_set(GITERR_REFERENCE, "Can't dereference to type"); + return GIT_ERROR; + } + return 0; } /* Dereference until we reach a commit. */ - if (dereference_to_type(&obj, start, GIT_OBJ_COMMIT) < 0) { + if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { /* Can't dereference to a commit; fail */ return GIT_ERROR; } @@ -212,7 +248,7 @@ static int handle_caret_syntax(git_object **out, git_object *start, const char * /* "^0" just returns the input */ if (n == 0) { - *out = (git_object*)commit; + *out = obj; return 0; } @@ -259,7 +295,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec if (current_state != next_state) { /* Leaving INIT state, find the object specified, in case that state needs it */ - assert(!revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer))); + retcode = revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); + if (retcode < 0) { + goto cleanup; + } } break; @@ -292,6 +331,13 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec break; case REVPARSE_STATE_LINEAR: + if (!*spec_cur) { + } else if (*spec_cur == '~') { + } else if (*spec_cur == '^') { + } else { + git_buf_putc(&stepbuffer, *spec_cur); + } + spec_cur++; break; } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 9ff3bd1da..64aca4a70 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -8,11 +8,11 @@ static git_object *g_obj; // Hepers -static void oid_str_cmp(const git_oid *oid, const char *str) +static void oid_str_cmp(const git_object *obj, const char *expected) { - git_oid oid2; - cl_git_pass(git_oid_fromstr(&oid2, str)); - cl_assert(0 == git_oid_cmp(oid, &oid2)); + char objstr[64] = {0}; + git_oid_to_string(objstr, 64, git_object_id(obj)); + cl_assert_equal_s(objstr, expected); } @@ -28,64 +28,81 @@ void test_refs_revparse__cleanup(void) } +void test_refs_revparse__nonexistant_object(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist")); +} + void test_refs_revparse__shas(void) { // Full SHA should return a valid object cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c7266a2be04c571c04d5a6614691ea99bd")); - oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c")); - oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); } void test_refs_revparse__head(void) { // Named head should return a valid object cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD")); - oid_str_cmp(git_object_id(g_obj), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__full_refs(void) { // Fully-qualified refs should return valid objects cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/master")); - oid_str_cmp(git_object_id(g_obj), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/test")); - oid_str_cmp(git_object_id(g_obj), "e90810b8df3e80c413d903f631643c716887138d"); + oid_str_cmp(g_obj, "e90810b8df3e80c413d903f631643c716887138d"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/tags/test")); - oid_str_cmp(git_object_id(g_obj), "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + oid_str_cmp(g_obj, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); } void test_refs_revparse__partial_refs(void) { // Partially-qualified refs should return valid objects cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob")); - oid_str_cmp(git_object_id(g_obj), "1385f264afb75a56a5bec74243be9b367ba4ca08"); + oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "packed-test")); - oid_str_cmp(git_object_id(g_obj), "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + oid_str_cmp(g_obj, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "br2")); - oid_str_cmp(git_object_id(g_obj), "a4a7dce85cf63874e984719f4fdd239f5145052f"); + oid_str_cmp(g_obj, "a4a7dce85cf63874e984719f4fdd239f5145052f"); } void test_refs_revparse__describe_output(void) { cl_git_pass(git_revparse_single(&g_obj, g_repo, "blah-7-gc47800c")); - oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); } void test_refs_revparse__nth_parent(void) { cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1")); - oid_str_cmp(git_object_id(g_obj), "9fd738e8f7967c078dceed8190330fc8648ee56a"); + oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^")); - oid_str_cmp(git_object_id(g_obj), "9fd738e8f7967c078dceed8190330fc8648ee56a"); + oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2")); - oid_str_cmp(git_object_id(g_obj), "c47800c7266a2be04c571c04d5a6614691ea99bd"); + oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1^1")); - oid_str_cmp(git_object_id(g_obj), "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + oid_str_cmp(g_obj, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2^1")); - oid_str_cmp(git_object_id(g_obj), "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + oid_str_cmp(g_obj, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^0")); - oid_str_cmp(git_object_id(g_obj), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); +} + +void test_refs_revparse__not_tag(void) +{ + cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob^{}")); + oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); +} + +void test_refs_revparse__to_type(void) +{ } void test_refs_revparse__reflog(void) diff --git a/tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b b/tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b new file mode 100644 index 0000000000000000000000000000000000000000..32fc53202e7c7e9b3c9e6eedbd443f9efb03764a GIT binary patch literal 134 zcmV;10D1p-0UeA@4#FT1MqP7?xqybB0W>kjcm>@XC+|D2m`>sX}}4CI*9a-;9c`|zsrKFa63~ORuf2ZOT(z(Xsr+GMG8v* literal 0 HcmV?d00001 diff --git a/tests/resources/testrepo.git/refs/tags/wrapped_tag b/tests/resources/testrepo.git/refs/tags/wrapped_tag new file mode 100644 index 0000000000000000000000000000000000000000..1621c20d698c61eacb4c43f3967b4c693d51e117 GIT binary patch literal 41 ucmV~$K>+|D2m`>sX}}4CI*9a-;9c`|zsrKFa63~ORuf2ZOT(z(Xsr+GMG8v* literal 0 HcmV?d00001 From 387d01b8578ebedc665d8fa707b8897f9a04f380 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 27 Apr 2012 11:47:29 -0700 Subject: [PATCH 05/45] Implemented rev-parse "^{type}" syntax. --- src/revparse.c | 28 ++++++++++++++-------------- tests-clar/refs/revparse.c | 14 ++++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index da274f860..472cc7587 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -116,7 +116,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const static int walk_ref_history(git_object **out, const char *refspec, const char *reflogspec) { - // TODO + /* TODO */ /* Empty refspec means current branch */ @@ -134,25 +134,25 @@ static git_object* dereference_object(git_object *obj) { git_otype type = git_object_type(obj); git_object *newobj = NULL; + git_tree *tree = NULL; switch (type) { case GIT_OBJ_COMMIT: - break; - case GIT_OBJ_TREE: - break; - case GIT_OBJ_BLOB: + if (0 == git_commit_tree(&tree, (git_commit*)obj)) { + return (git_object*)tree; + } break; case GIT_OBJ_TAG: if (0 == git_tag_target(&newobj, (git_tag*)obj)) { return newobj; } break; - case GIT_OBJ_OFS_DELTA: - break; - case GIT_OBJ_REF_DELTA: - break; default: + case GIT_OBJ_TREE: + case GIT_OBJ_BLOB: + case GIT_OBJ_OFS_DELTA: + case GIT_OBJ_REF_DELTA: break; } @@ -168,14 +168,14 @@ static int dereference_to_type(git_object **out, git_object *obj, git_otype targ git_otype this_type = git_object_type(obj1); if (this_type == target_type) { - *out = obj; + *out = obj1; return 0; } /* Dereference once, if possible. */ obj2 = dereference_object(obj1); - if (obj2 != obj) { - git_object_free(obj2); + if (obj1 != obj) { + git_object_free(obj1); } obj1 = obj2; } @@ -212,7 +212,7 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo giterr_set(GITERR_REFERENCE, "Couldn't find object of target type."); return GIT_ERROR; } - newobj = newobj; + newobj = newobj2; } *out = newobj2; return 0; @@ -220,7 +220,7 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ if (movement[1] == '/') { - // TODO + /* TODO */ return GIT_ERROR; } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 64aca4a70..c1be32f18 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -7,7 +7,7 @@ static git_object *g_obj; -// Hepers +/* Helpers */ static void oid_str_cmp(const git_object *obj, const char *expected) { char objstr[64] = {0}; @@ -35,7 +35,6 @@ void test_refs_revparse__nonexistant_object(void) void test_refs_revparse__shas(void) { - // Full SHA should return a valid object cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c7266a2be04c571c04d5a6614691ea99bd")); oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c")); @@ -44,14 +43,12 @@ void test_refs_revparse__shas(void) void test_refs_revparse__head(void) { - // Named head should return a valid object cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD")); oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__full_refs(void) { - // Fully-qualified refs should return valid objects cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/master")); oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/test")); @@ -62,7 +59,6 @@ void test_refs_revparse__full_refs(void) void test_refs_revparse__partial_refs(void) { - // Partially-qualified refs should return valid objects cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob")); oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "packed-test")); @@ -103,9 +99,15 @@ void test_refs_revparse__not_tag(void) void test_refs_revparse__to_type(void) { + cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{commit}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{tree}")); + oid_str_cmp(g_obj, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob^{blob}")); + oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); } void test_refs_revparse__reflog(void) { - // TODO: how to create a fixture for this? git_reflog_write? + /* TODO: how to create a fixture for this? git_reflog_write? */ } From 7149a6252c6124a505d377fe36054e120ee2299b Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 27 Apr 2012 13:53:28 -0700 Subject: [PATCH 06/45] Returning error if dereferencing operation fails. --- src/revparse.c | 5 ++++- tests-clar/refs/revparse.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/revparse.c b/src/revparse.c index 472cc7587..7d57ad7dd 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -174,6 +174,10 @@ static int dereference_to_type(git_object **out, git_object *obj, git_otype targ /* Dereference once, if possible. */ obj2 = dereference_object(obj1); + if (!obj2) { + giterr_set(GITERR_REFERENCE, "Can't dereference to type"); + return GIT_ERROR; + } if (obj1 != obj) { git_object_free(obj1); } @@ -226,7 +230,6 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo /* {...} -> Dereference until we reach an object of a certain type. */ if (dereference_to_type(out, obj, parse_obj_type(movement)) < 0) { - giterr_set(GITERR_REFERENCE, "Can't dereference to type"); return GIT_ERROR; } return 0; diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index c1be32f18..e34646b11 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -105,6 +105,8 @@ void test_refs_revparse__to_type(void) oid_str_cmp(g_obj, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob^{blob}")); oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + + cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); } void test_refs_revparse__reflog(void) From e0887d81786a6e9479cdcd06c9f59748fed17d9e Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 27 Apr 2012 14:10:03 -0700 Subject: [PATCH 07/45] Removed goto from state machine loop. --- src/revparse.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 7d57ad7dd..ffa3e6c0d 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -21,12 +21,9 @@ GIT_BEGIN_DECL typedef enum { REVPARSE_STATE_INIT, - - /* for "^{...}" and ^... */ REVPARSE_STATE_CARET, - - /* For "~..." */ REVPARSE_STATE_LINEAR, + REVPARSE_STATE_DONE, } revparse_state; static void set_invalid_syntax_err(const char *spec) @@ -272,11 +269,12 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec git_object *next_obj = NULL; git_buf specbuffer = GIT_BUF_INIT; git_buf stepbuffer = GIT_BUF_INIT; + int keep_looping = 1; int retcode = 0; assert(out && repo && spec); - while (1) { + while (keep_looping) { switch (current_state) { case REVPARSE_STATE_INIT: if (!*spec_cur) { @@ -286,7 +284,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec /* '@' syntax doesn't allow chaining */ git_buf_puts(&stepbuffer, spec_cur); retcode = walk_ref_history(out, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); - goto cleanup; + next_state = REVPARSE_STATE_DONE; } else if (*spec_cur == '^') { next_state = REVPARSE_STATE_CARET; } else if (*spec_cur == '~') { @@ -300,7 +298,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec /* Leaving INIT state, find the object specified, in case that state needs it */ retcode = revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); if (retcode < 0) { - goto cleanup; + next_state = REVPARSE_STATE_DONE; } } break; @@ -309,24 +307,18 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec case REVPARSE_STATE_CARET: /* Gather characters until NULL, '~', or '^' */ if (!*spec_cur) { - retcode = handle_caret_syntax(out, - cur_obj, - git_buf_cstr(&stepbuffer)); - goto cleanup; + retcode = handle_caret_syntax(out, cur_obj, git_buf_cstr(&stepbuffer)); + next_state = REVPARSE_STATE_DONE; } else if (*spec_cur == '~') { - retcode = handle_caret_syntax(&next_obj, - cur_obj, - git_buf_cstr(&stepbuffer)); + retcode = handle_caret_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); git_buf_clear(&stepbuffer); - if (retcode < 0) goto cleanup; - next_state = REVPARSE_STATE_LINEAR; + next_state = !retcode ? REVPARSE_STATE_LINEAR : REVPARSE_STATE_DONE; } else if (*spec_cur == '^') { - retcode = handle_caret_syntax(&next_obj, - cur_obj, - git_buf_cstr(&stepbuffer)); + retcode = handle_caret_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); git_buf_clear(&stepbuffer); - if (retcode < 0) goto cleanup; - next_state = REVPARSE_STATE_CARET; + if (retcode < 0) { + next_state = REVPARSE_STATE_DONE; + } } else { git_buf_putc(&stepbuffer, *spec_cur); } @@ -342,6 +334,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } spec_cur++; break; + + case REVPARSE_STATE_DONE: + keep_looping = 0; + break; } current_state = next_state; @@ -351,7 +347,6 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } } -cleanup: git_buf_free(&specbuffer); git_buf_free(&stepbuffer); return retcode; From 38533d5acf6abc2c4266ea5cf5eb80bb819794f8 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 27 Apr 2012 14:11:12 -0700 Subject: [PATCH 08/45] Implementing rev-parse's "ref~2" syntax. Also extended the test suite to include chaining operators, e.g. "master^2~3^4". --- src/revparse.c | 49 ++++++++++++++++++++++++++++++++++++++ tests-clar/refs/revparse.c | 22 +++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/revparse.c b/src/revparse.c index ffa3e6c0d..7a07e0c38 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -260,6 +260,45 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo return 0; } +static int handle_linear_syntax(git_object **out, git_object *obj, const char *movement) +{ + git_commit *commit1, *commit2; + int i, n; + + /* Dereference until we reach a commit. */ + if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { + /* Can't dereference to a commit; fail */ + return GIT_ERROR; + } + + /* "~" is the same as "~1" */ + if (strlen(movement) == 0) { + n = 1; + } else { + git__strtol32(&n, movement, NULL, 0); + } + commit1 = (git_commit*)obj; + + /* "~0" just returns the input */ + if (n == 0) { + *out = obj; + return 0; + } + + for (i=0; i Date: Mon, 30 Apr 2012 09:58:43 -0700 Subject: [PATCH 09/45] Adding comment documentation for rev-parse api. --- include/git2/revparse.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/include/git2/revparse.h b/include/git2/revparse.h index 3fd69d91d..4567027e5 100644 --- a/include/git2/revparse.h +++ b/include/git2/revparse.h @@ -11,12 +11,26 @@ #include "types.h" +/** + * @file git2/revparse.h + * @brief Git revision parsing routines + * @defgroup git_revparse Git revision parsing routines + * @ingroup Git + * @{ + */ GIT_BEGIN_DECL +/** + * Find an object, as specified by a revision string. See `man gitrevisions`, or the documentation + * for `git rev-parse` for information on the syntax accepted. + * + * @param out pointer to output object + * @param repo the repository to search in + * @param spec the textual specification for an object + * @return on success, GIT_ERROR otherwise (use git_error_last for information about the error) + */ GIT_EXTERN(int) git_revparse_single(git_object **out, git_repository *repo, const char *spec); -//GIT_EXTERN(int) git_revparse_multi(TODO); - +/** @} */ GIT_END_DECL - #endif From a51bdbcfa17c46578700006eea2ef1f2dea03cf5 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 30 Apr 2012 20:21:45 -0700 Subject: [PATCH 10/45] Implementing rev-parse's ref@{n} and @{-n} syntaxes. Added some reflags to the test repo to support unit tests. --- src/revparse.c | 108 +++++++++++++++--- tests-clar/refs/revparse.c | 26 ++++- tests/resources/testrepo.git/logs/HEAD | Bin 0 -> 820 bytes .../testrepo.git/logs/refs/heads/br2 | Bin 0 -> 327 bytes .../testrepo.git/logs/refs/heads/master | Bin 0 -> 346 bytes .../logs/refs/remotes/origin/HEAD | Bin 0 -> 195 bytes 6 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 tests/resources/testrepo.git/logs/HEAD create mode 100644 tests/resources/testrepo.git/logs/refs/heads/br2 create mode 100644 tests/resources/testrepo.git/logs/refs/heads/master create mode 100644 tests/resources/testrepo.git/logs/refs/remotes/origin/HEAD diff --git a/src/revparse.c b/src/revparse.c index 7a07e0c38..61a9abc34 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -16,6 +16,8 @@ #include "git2/refs.h" #include "git2/tag.h" #include "git2/commit.h" +#include "git2/reflog.h" +#include "git2/refs.h" GIT_BEGIN_DECL @@ -111,20 +113,97 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const } -static int walk_ref_history(git_object **out, const char *refspec, const char *reflogspec) +static int walk_ref_history(git_object **out, git_repository *repo, const char *refspec, const char *reflogspec) { - /* TODO */ + git_reference *ref; + git_reflog *reflog = NULL; + int n, retcode = GIT_ERROR; + size_t i, refloglen; + const git_reflog_entry *entry; + git_buf buf = GIT_BUF_INIT; - /* Empty refspec means current branch */ + if (git__prefixcmp(reflogspec, "@{") != 0 || + git__suffixcmp(reflogspec, "}") != 0) { + giterr_set(GITERR_INVALID, "Bad reflogspec '%s'", reflogspec); + return GIT_ERROR; + } - /* Possible syntaxes for reflogspec: - * "8" -> 8th prior value for the ref - * "-2" -> nth branch checked out before the current one (refspec must be empty) - * "yesterday", "1 month 2 weeks 3 days 4 hours 5 seconds ago", "1979-02-26 18:30:00" - * -> value of ref at given point in time - * "upstream" or "u" -> branch the ref is set to build on top of - * */ - return 0; + /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ + if (strlen(refspec) == 0 && !git__prefixcmp(reflogspec, "@{-")) { + if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 || + n < 1) { + giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec); + return GIT_ERROR; + } + git_reference_lookup(&ref, repo, "HEAD"); + git_reflog_read(&reflog, ref); + git_reference_free(ref); + + refloglen = git_reflog_entrycount(reflog); + for (i=0; i < refloglen; i++) { + const char *msg; + entry = git_reflog_entry_byindex(reflog, i); + + msg = git_reflog_entry_msg(entry); + if (!git__prefixcmp(msg, "checkout: moving")) { + n--; + if (!n) { + char *branchname = strrchr(msg, ' ') + 1; + git_buf_printf(&buf, "refs/heads/%s", branchname); + retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); + break; + } + } + } + } else { + if (!strlen(refspec)) { + /* Empty refspec means current branch */ + /* Get the target of HEAD */ + git_reference_lookup(&ref, repo, "HEAD"); + git_buf_puts(&buf, git_reference_target(ref)); + git_reference_free(ref); + + /* Get the reflog for that ref */ + git_reference_lookup(&ref, repo, git_buf_cstr(&buf)); + git_reflog_read(&reflog, ref); + git_reference_free(ref); + } else { + if (git__prefixcmp(refspec, "refs/heads/") != 0) { + git_buf_printf(&buf, "refs/heads/%s", refspec); + } else { + git_buf_puts(&buf, refspec); + } + } + + /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ + if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { + /* TODO */ + } + + /* @{N} -> Nth prior value for the ref (from reflog) */ + else if (!git__strtol32(&n, reflogspec+2, NULL, 0)) { + if (n == 0) { + retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); + } else if (!git_reference_lookup(&ref, repo, git_buf_cstr(&buf))) { + if (!git_reflog_read(&reflog, ref)) { + 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); + } + git_reference_free(ref); + } + } + + /* @{Anything else} -> try to parse the expression into a date, and get the value of the ref as it + was then. */ + else { + /* TODO */ + } + } + + if (reflog) git_reflog_free(reflog); + git_buf_free(&buf); + return retcode; } static git_object* dereference_object(git_object *obj) @@ -322,7 +401,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } else if (*spec_cur == '@') { /* '@' syntax doesn't allow chaining */ git_buf_puts(&stepbuffer, spec_cur); - retcode = walk_ref_history(out, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); + 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; @@ -335,10 +414,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec if (current_state != next_state) { /* Leaving INIT state, find the object specified, in case that state needs it */ - retcode = revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); - if (retcode < 0) { - next_state = REVPARSE_STATE_DONE; - } + revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); } break; diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 08cf406f6..d8013abbd 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -133,5 +133,29 @@ void test_refs_revparse__chaining(void) void test_refs_revparse__reflog(void) { - /* TODO: how to create a fixture for this? git_reflog_write? */ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}")); + + cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{-2}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{-1}")); + oid_str_cmp(g_obj, "a4a7dce85cf63874e984719f4fdd239f5145052f"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{0}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{1}")); + oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{0}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{1}")); + oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + /* Not ready yet + cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD@{100 years ago}")); + oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{2012-4-30 10:23:20}")); + oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{upstream}")); + oid_str_cmp(g_obj, "???"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{u}")); + oid_str_cmp(g_obj, "???"); + */ } diff --git a/tests/resources/testrepo.git/logs/HEAD b/tests/resources/testrepo.git/logs/HEAD new file mode 100644 index 0000000000000000000000000000000000000000..5bdcb7ce7ab5bc3258f7624098383de7176fbbf5 GIT binary patch literal 820 zcma))O>V;=5JuPaDY!u60R}KpRaL!0)dTP|q*h~70q*(hBu!Ucz-rKF_})AL(07<8 zskC$?ld;MRk5Wk=0!gQYaS{t)u=%(#?l0=+-R z9>D-SgmYf$$WqA*8{aFHI(k~iTEcjq{XQzjMzz(kP|aHjRIm3!U-__ihi)>bRic=r zb(9D}MCrXIfiYpkwD(Yf*8g3J&7=^S+ qaa5p0>c-^ej`^!BTyQU#^Ez~=b!+bYj9TzJ*1z_4XBTxhiNg<$a`IFF literal 0 HcmV?d00001 diff --git a/tests/resources/testrepo.git/logs/refs/heads/br2 b/tests/resources/testrepo.git/logs/refs/heads/br2 new file mode 100644 index 0000000000000000000000000000000000000000..4e27f6b8df19d6996a7212b5355d8cea8ca9d2c6 GIT binary patch literal 327 zcma)%%WcCj5CzY17280fCAIt+K@iv_xB_yyl!!nSK+*jt2wZ^PX%2Ikhj?zlR6xYgZJ$V)I1xH)}n`Zbck=M z$Hwaw?p@-L45jZIbScNwW!r}w)86Y++myP#M2ar6c*Dic-jM`PPK81xXFZ2v6d5$S az-jSpuX3;6zZY8_SKw9nr?v%ZyZi##k6i%( literal 0 HcmV?d00001 diff --git a/tests/resources/testrepo.git/logs/refs/heads/master b/tests/resources/testrepo.git/logs/refs/heads/master new file mode 100644 index 0000000000000000000000000000000000000000..c41e87a3f97753e6e32e6d22bb45cf7d2b99063f GIT binary patch literal 346 zcma)1L2d&f5IZ-om=7dSW`HKDqN-o0`Tz!o1T_m4u>bEWho09bTb3wN$gv<`z{ z_Gin62`zGrg25Uek`9!8c0}+g6~1NZG9vA;b!Z0pj35rM5wFx<6GX;J)tZ^bYaA01LT}f>83k zAwJXMMsmf2{DW*m4`TT?}=nj)H)5=A#I^3Bb Date: Tue, 1 May 2012 14:34:05 -0700 Subject: [PATCH 11/45] Rev-parse chaining: adding the longest chain in the test repo. --- tests-clar/refs/revparse.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index d8013abbd..f302e2c53 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -129,6 +129,8 @@ void test_refs_revparse__chaining(void) oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); cl_git_pass(git_revparse_single(&g_obj, g_repo, "master^1^2~1")); oid_str_cmp(g_obj, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + cl_git_pass(git_revparse_single(&g_obj, g_repo, "master^1^1^1^1^1")); + oid_str_cmp(g_obj, "8496071c1b46c854b31185ea97743be6a8774479"); } void test_refs_revparse__reflog(void) From e88b8bd593c765db9190fbdfc0a5583f179ac6c8 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 1 May 2012 14:37:11 -0700 Subject: [PATCH 12/45] Incorporating feedback from @tanoku. Removed repeated strlen's, and unnecessary loop-termination variable. --- src/revparse.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 61a9abc34..9579d450d 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -121,6 +121,8 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * size_t i, refloglen; const git_reflog_entry *entry; git_buf buf = GIT_BUF_INIT; + size_t refspeclen = strlen(refspec); + size_t reflogspeclen = strlen(reflogspec); if (git__prefixcmp(reflogspec, "@{") != 0 || git__suffixcmp(reflogspec, "}") != 0) { @@ -129,7 +131,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * } /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ - if (strlen(refspec) == 0 && !git__prefixcmp(reflogspec, "@{-")) { + if (refspeclen == 0 && !git__prefixcmp(reflogspec, "@{-")) { if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 || n < 1) { giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec); @@ -156,7 +158,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * } } } else { - if (!strlen(refspec)) { + if (!refspeclen) { /* Empty refspec means current branch */ /* Get the target of HEAD */ git_reference_lookup(&ref, repo, "HEAD"); @@ -273,16 +275,17 @@ static git_otype parse_obj_type(const char *str) static int handle_caret_syntax(git_object **out, git_object *obj, const char *movement) { git_commit *commit; + size_t movementlen = strlen(movement); int n; if (*movement == '{') { - if (movement[strlen(movement)-1] != '}') { + if (movement[movementlen-1] != '}') { set_invalid_syntax_err(movement); return GIT_ERROR; } /* {} -> Dereference until we reach an object that isn't a tag. */ - if (strlen(movement) == 2) { + if (movementlen == 2) { git_object *newobj = obj; git_object *newobj2 = newobj; while (git_object_type(newobj2) == GIT_OBJ_TAG) { @@ -318,7 +321,7 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo } /* "^" is the same as "^1" */ - if (strlen(movement) == 0) { + if (movementlen == 0) { n = 1; } else { git__strtol32(&n, movement, NULL, 0); @@ -387,12 +390,11 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec git_object *next_obj = NULL; git_buf specbuffer = GIT_BUF_INIT; git_buf stepbuffer = GIT_BUF_INIT; - int keep_looping = 1; int retcode = 0; assert(out && repo && spec); - while (keep_looping) { + while (current_state != REVPARSE_STATE_DONE) { switch (current_state) { case REVPARSE_STATE_INIT: if (!*spec_cur) { @@ -461,7 +463,6 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec break; case REVPARSE_STATE_DONE: - keep_looping = 0; break; } From 27ee848397facd2a3744f3a7505bdda24216cb90 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 2 May 2012 14:41:19 -0700 Subject: [PATCH 13/45] Rev-parse: plugging (most) memory leaks. --- src/revparse.c | 45 +++++++------ tests-clar/refs/revparse.c | 128 ++++++++++++++----------------------- 2 files changed, 71 insertions(+), 102 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 9579d450d..ea33e3a2d 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -122,7 +122,6 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * const git_reflog_entry *entry; git_buf buf = GIT_BUF_INIT; size_t refspeclen = strlen(refspec); - size_t reflogspeclen = strlen(reflogspec); if (git__prefixcmp(reflogspec, "@{") != 0 || git__suffixcmp(reflogspec, "}") != 0) { @@ -164,11 +163,6 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * git_reference_lookup(&ref, repo, "HEAD"); git_buf_puts(&buf, git_reference_target(ref)); git_reference_free(ref); - - /* Get the reflog for that ref */ - git_reference_lookup(&ref, repo, git_buf_cstr(&buf)); - git_reflog_read(&reflog, ref); - git_reference_free(ref); } else { if (git__prefixcmp(refspec, "refs/heads/") != 0) { git_buf_printf(&buf, "refs/heads/%s", refspec); @@ -211,18 +205,22 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * static git_object* dereference_object(git_object *obj) { git_otype type = git_object_type(obj); - git_object *newobj = NULL; - git_tree *tree = NULL; switch (type) { case GIT_OBJ_COMMIT: - if (0 == git_commit_tree(&tree, (git_commit*)obj)) { - return (git_object*)tree; + { + git_tree *tree = NULL; + if (0 == git_commit_tree(&tree, (git_commit*)obj)) { + return (git_object*)tree; + } } break; case GIT_OBJ_TAG: - if (0 == git_tag_target(&newobj, (git_tag*)obj)) { - return newobj; + { + git_object *newobj = NULL; + if (0 == git_tag_target(&newobj, (git_tag*)obj)) { + return newobj; + } } break; @@ -383,13 +381,10 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m int git_revparse_single(git_object **out, git_repository *repo, const char *spec) { - revparse_state current_state = REVPARSE_STATE_INIT; - revparse_state next_state = REVPARSE_STATE_INIT; + revparse_state current_state = REVPARSE_STATE_INIT, next_state = REVPARSE_STATE_INIT; const char *spec_cur = spec; - git_object *cur_obj = NULL; - git_object *next_obj = NULL; - git_buf specbuffer = GIT_BUF_INIT; - git_buf stepbuffer = GIT_BUF_INIT; + git_object *cur_obj = NULL, *next_obj = NULL; + git_buf specbuffer = GIT_BUF_INIT, stepbuffer = GIT_BUF_INIT; int retcode = 0; assert(out && repo && spec); @@ -399,7 +394,8 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec case REVPARSE_STATE_INIT: if (!*spec_cur) { /* No operators, just a name. Find it and return. */ - return revparse_lookup_object(out, repo, spec); + 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); @@ -414,7 +410,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } spec_cur++; - if (current_state != next_state) { + if (current_state != next_state && next_state != REVPARSE_STATE_DONE) { /* Leaving INIT state, find the object specified, in case that state needs it */ revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); } @@ -463,16 +459,23 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec break; case REVPARSE_STATE_DONE: + if (cur_obj && *out != cur_obj) git_object_free(cur_obj); + if (next_obj && *out != next_obj) git_object_free(next_obj); break; } current_state = next_state; if (cur_obj != next_obj) { - git_object_free(cur_obj); + if (cur_obj) git_object_free(cur_obj); cur_obj = next_obj; } } + if (!retcode) { + if (*out != cur_obj) git_object_free(cur_obj); + if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); + } + git_buf_free(&specbuffer); git_buf_free(&stepbuffer); return retcode; diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index f302e2c53..c5cbb8745 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -8,11 +8,16 @@ static git_object *g_obj; /* Helpers */ -static void oid_str_cmp(const git_object *obj, const char *expected) +static void test_object(const char *spec, const char *expected_oid) { char objstr[64] = {0}; - git_oid_to_string(objstr, 64, git_object_id(obj)); - cl_assert_equal_s(objstr, expected); + + cl_git_pass(git_revparse_single(&g_obj, g_repo, spec)); + git_oid_to_string(objstr, 64, git_object_id(g_obj)); + cl_assert_equal_s(objstr, expected_oid); + + git_object_free(g_obj); + g_obj = NULL; } @@ -35,102 +40,73 @@ void test_refs_revparse__nonexistant_object(void) void test_refs_revparse__shas(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c7266a2be04c571c04d5a6614691ea99bd")); - oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "c47800c")); - oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); } void test_refs_revparse__head(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__full_refs(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/master")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/heads/test")); - oid_str_cmp(g_obj, "e90810b8df3e80c413d903f631643c716887138d"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "refs/tags/test")); - oid_str_cmp(g_obj, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d"); + test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); } void test_refs_revparse__partial_refs(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob")); - oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "packed-test")); - oid_str_cmp(g_obj, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "br2")); - oid_str_cmp(g_obj, "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f"); } void test_refs_revparse__describe_output(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "blah-7-gc47800c")); - oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); } void test_refs_revparse__nth_parent(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1")); - oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^")); - oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2")); - oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^1^1")); - oid_str_cmp(g_obj, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^2^1")); - oid_str_cmp(g_obj, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "be3563a^0")); - oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); } void test_refs_revparse__not_tag(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob^{}")); - oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__to_type(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{commit}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{tree}")); - oid_str_cmp(g_obj, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "point_to_blob^{blob}")); - oid_str_cmp(g_obj, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); } void test_refs_revparse__linear_history(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~0")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~1")); - oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~2")); - oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~1~1")); - oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); } void test_refs_revparse__chaining(void) { - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~1^1")); - oid_str_cmp(g_obj, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master~1^2")); - oid_str_cmp(g_obj, "c47800c7266a2be04c571c04d5a6614691ea99bd"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master^1^2~1")); - oid_str_cmp(g_obj, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master^1^1^1^1^1")); - oid_str_cmp(g_obj, "8496071c1b46c854b31185ea97743be6a8774479"); + test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479"); } void test_refs_revparse__reflog(void) @@ -138,26 +114,16 @@ void test_refs_revparse__reflog(void) cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}")); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{-2}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{-1}")); - oid_str_cmp(g_obj, "a4a7dce85cf63874e984719f4fdd239f5145052f"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{0}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{1}")); - oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{0}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "@{1}")); - oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); /* Not ready yet - cl_git_pass(git_revparse_single(&g_obj, g_repo, "HEAD@{100 years ago}")); - oid_str_cmp(g_obj, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{2012-4-30 10:23:20}")); - oid_str_cmp(g_obj, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{upstream}")); - oid_str_cmp(g_obj, "???"); - cl_git_pass(git_revparse_single(&g_obj, g_repo, "master@{u}")); - oid_str_cmp(g_obj, "???"); + test_object("HEAD@{100 years ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{2012-4-30 10:23:20}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{upstream}", "???"); + test_object("master@{u}", "???"); */ } From 65bc26d54aaf307d0e7eb71d6c5752f88fdca5de Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 3 May 2012 10:29:41 -0700 Subject: [PATCH 14/45] Fixed last 2 memory leaks in rev-parse. --- src/revparse.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index ea33e3a2d..9719093f6 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -238,27 +238,29 @@ static git_object* dereference_object(git_object *obj) static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) { + int retcode = 1; git_object *obj1 = obj, *obj2 = obj; - while (1) { + while (retcode > 0) { git_otype this_type = git_object_type(obj1); if (this_type == target_type) { *out = obj1; - return 0; - } - - /* Dereference once, if possible. */ - obj2 = dereference_object(obj1); - if (!obj2) { - giterr_set(GITERR_REFERENCE, "Can't dereference to type"); - return GIT_ERROR; + retcode = 0; + } else { + /* Dereference once, if possible. */ + obj2 = dereference_object(obj1); + if (!obj2) { + giterr_set(GITERR_REFERENCE, "Can't dereference to type"); + retcode = GIT_ERROR; + } } if (obj1 != obj) { git_object_free(obj1); } obj1 = obj2; } + return retcode; } static git_otype parse_obj_type(const char *str) @@ -471,10 +473,8 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec } } - if (!retcode) { - if (*out != cur_obj) git_object_free(cur_obj); - if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); - } + if (*out != cur_obj) git_object_free(cur_obj); + if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); git_buf_free(&specbuffer); git_buf_free(&stepbuffer); From a6346302e6805aea0f1c353bb62944f1ae7352af Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 3 May 2012 13:58:46 -0700 Subject: [PATCH 15/45] Rev-parse: "ref@{upstream}" syntax. Added tracking configuration to the test repo's config to support unit tests. --- src/revparse.c | 24 +++++++++++++++++- tests-clar/refs/revparse.c | 4 +-- tests-clar/resources/testrepo.git/config | 4 +++ .../testrepo.git/refs/remotes/test/master | Bin 0 -> 41 bytes 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/resources/testrepo.git/refs/remotes/test/master diff --git a/src/revparse.c b/src/revparse.c index 9719093f6..13778eb11 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -18,6 +18,8 @@ #include "git2/commit.h" #include "git2/reflog.h" #include "git2/refs.h" +#include "git2/repository.h" +#include "git2/config.h" GIT_BEGIN_DECL @@ -173,7 +175,27 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { - /* TODO */ + git_config *cfg; + if (!git_repository_config(&cfg, repo)) { + /* Is the ref a tracking branch? */ + const char *remote; + git_buf_clear(&buf); + git_buf_printf(&buf, "branch.%s.remote", refspec); + if (!git_config_get_string(cfg, git_buf_cstr(&buf), &remote)) { + /* Yes. Find the first merge target name. */ + const char *mergetarget; + git_buf_clear(&buf); + git_buf_printf(&buf, "branch.%s.merge", refspec); + if (!git_config_get_string(cfg, git_buf_cstr(&buf), &mergetarget) && + !git__prefixcmp(mergetarget, "refs/heads/")) { + /* Success. Look up the target and fetch the object. */ + git_buf_clear(&buf); + git_buf_printf(&buf, "refs/remotes/%s/%s", remote, mergetarget+11); + retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); + } + } + git_config_free(cfg); + } } /* @{N} -> Nth prior value for the ref (from reflog) */ diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index c5cbb8745..944a18b9c 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -120,10 +120,10 @@ void test_refs_revparse__reflog(void) test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); /* Not ready yet test_object("HEAD@{100 years ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("master@{2012-4-30 10:23:20}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{upstream}", "???"); - test_object("master@{u}", "???"); */ } diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config index 1a5aacdfa..b4fdac6c2 100644 --- a/tests-clar/resources/testrepo.git/config +++ b/tests-clar/resources/testrepo.git/config @@ -6,3 +6,7 @@ [remote "test"] url = git://github.com/libgit2/libgit2 fetch = +refs/heads/*:refs/remotes/test/* + +[branch "master"] + remote = test + merge = refs/heads/master diff --git a/tests/resources/testrepo.git/refs/remotes/test/master b/tests/resources/testrepo.git/refs/remotes/test/master new file mode 100644 index 0000000000000000000000000000000000000000..9536ad89cfb3b156e2ad6bafe06c6a708d7d2b73 GIT binary patch literal 41 ucmV~$!4Uu;2m`Rc(|EMVIZi>?e*}|k_< Date: Fri, 4 May 2012 10:31:57 -0700 Subject: [PATCH 16/45] Rev-parse: fixing double-freeing. Thanks, Visual Studio! --- src/revparse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revparse.c b/src/revparse.c index 13778eb11..e03e3338c 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -277,7 +277,7 @@ static int dereference_to_type(git_object **out, git_object *obj, git_otype targ retcode = GIT_ERROR; } } - if (obj1 != obj) { + if (obj1 != obj && obj1 != obj2) { git_object_free(obj1); } obj1 = obj2; From 886f183ac3dc43c774700825ba32b7b6ffbfc3c3 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 7 May 2012 14:26:40 -0700 Subject: [PATCH 17/45] Rev-parse: "ref^{/regex}" syntax. --- src/revparse.c | 53 +++++++++++++++++++++++++++++++++----- tests-clar/refs/revparse.c | 15 +++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index e03e3338c..547426449 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -20,6 +20,7 @@ #include "git2/refs.h" #include "git2/repository.h" #include "git2/config.h" +#include "git2/revwalk.h" GIT_BEGIN_DECL @@ -294,7 +295,7 @@ static git_otype parse_obj_type(const char *str) return GIT_OBJ_BAD; } -static int handle_caret_syntax(git_object **out, git_object *obj, const char *movement) +static int handle_caret_syntax(git_object **out, git_repository *repo, git_object *obj, const char *movement) { git_commit *commit; size_t movementlen = strlen(movement); @@ -325,8 +326,48 @@ static int handle_caret_syntax(git_object **out, git_object *obj, const char *mo /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ if (movement[1] == '/') { - /* TODO */ - return GIT_ERROR; + int retcode = GIT_ERROR; + git_revwalk *walk; + if (!git_revwalk_new(&walk, repo)) { + git_oid oid; + regex_t preg; + git_buf buf = GIT_BUF_INIT; + + git_revwalk_sorting(walk, GIT_SORT_TIME); + git_revwalk_push(walk, git_object_id(obj)); + + /* Extract the regex from the movement string */ + git_buf_put(&buf, movement+2, strlen(movement)-3); + + if (!regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED)) { + while(!git_revwalk_next(&oid, walk)) { + git_object *walkobj; + char str[41]; + git_oid_fmt(str, &oid); + str[40] = 0; + + /* 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); + } + } + regfree(&preg); + } + + git_buf_free(&buf); + git_revwalk_free(walk); + } + return retcode; } /* {...} -> Dereference until we reach an object of a certain type. */ @@ -444,14 +485,14 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec case REVPARSE_STATE_CARET: /* Gather characters until NULL, '~', or '^' */ if (!*spec_cur) { - retcode = handle_caret_syntax(out, cur_obj, git_buf_cstr(&stepbuffer)); + retcode = handle_caret_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); next_state = REVPARSE_STATE_DONE; } else if (*spec_cur == '~') { - retcode = handle_caret_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); + 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, cur_obj, git_buf_cstr(&stepbuffer)); + 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; diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 944a18b9c..e967f7a48 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -122,6 +122,21 @@ void test_refs_revparse__reflog(void) test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); +} + +void test_refs_revparse__revwalk(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); + + test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a"); +} + +void test_refs_revparse__date(void) +{ /* Not ready yet test_object("HEAD@{100 years ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("master@{2012-4-30 10:23:20}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); From a346992f7e7812416651eb5c2b52129adec5eacf Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 09:47:14 -0700 Subject: [PATCH 18/45] Rev-parse: @{time} syntax. Ported date.c (for approxidate_careful) from git.git revision aa39b85. Trimmed out the parts we're not using. --- include/git2.h | 1 + src/date.c | 871 ++++++++++++++++++ src/date.h | 12 + src/revparse.c | 131 ++- src/util.h | 7 + tests-clar/refs/revparse.c | 15 +- tests/resources/testrepo.git/logs/HEAD | Bin 820 -> 820 bytes .../testrepo.git/logs/refs/heads/master | Bin 346 -> 346 bytes 8 files changed, 999 insertions(+), 38 deletions(-) create mode 100644 src/date.c create mode 100644 src/date.h diff --git a/include/git2.h b/include/git2.h index d75387318..2630709e2 100644 --- a/include/git2.h +++ b/include/git2.h @@ -25,6 +25,7 @@ #include "git2/merge.h" #include "git2/refs.h" #include "git2/reflog.h" +#include "git2/revparse.h" #include "git2/object.h" #include "git2/blob.h" diff --git a/src/date.c b/src/date.c new file mode 100644 index 000000000..b4708b8ef --- /dev/null +++ b/src/date.c @@ -0,0 +1,871 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ + +#include "date.h" +#include "cache.h" + +#include +#include + +typedef enum { + DATE_NORMAL = 0, + DATE_RELATIVE, + DATE_SHORT, + DATE_LOCAL, + DATE_ISO8601, + DATE_RFC2822, + DATE_RAW +} date_mode; + +/* + * This is like mktime, but without normalization of tm_wday and tm_yday. + */ +static time_t tm_to_time_t(const struct tm *tm) +{ + static const int mdays[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + int year = tm->tm_year - 70; + int month = tm->tm_mon; + int day = tm->tm_mday; + + if (year < 0 || year > 129) /* algo only works for 1970-2099 */ + return -1; + if (month < 0 || month > 11) /* array bounds */ + return -1; + if (month < 2 || (year + 2) % 4) + day--; + if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) + return -1; + return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + + tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; +} + +static const char *month_names[] = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December" +}; + +static const char *weekday_names[] = { + "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" +}; + + + +/* + * Check these. And note how it doesn't do the summer-time conversion. + * + * In my world, it's always summer, and things are probably a bit off + * in other ways too. + */ +static const struct { + const char *name; + int offset; + int dst; +} timezone_names[] = { + { "IDLW", -12, 0, }, /* International Date Line West */ + { "NT", -11, 0, }, /* Nome */ + { "CAT", -10, 0, }, /* Central Alaska */ + { "HST", -10, 0, }, /* Hawaii Standard */ + { "HDT", -10, 1, }, /* Hawaii Daylight */ + { "YST", -9, 0, }, /* Yukon Standard */ + { "YDT", -9, 1, }, /* Yukon Daylight */ + { "PST", -8, 0, }, /* Pacific Standard */ + { "PDT", -8, 1, }, /* Pacific Daylight */ + { "MST", -7, 0, }, /* Mountain Standard */ + { "MDT", -7, 1, }, /* Mountain Daylight */ + { "CST", -6, 0, }, /* Central Standard */ + { "CDT", -6, 1, }, /* Central Daylight */ + { "EST", -5, 0, }, /* Eastern Standard */ + { "EDT", -5, 1, }, /* Eastern Daylight */ + { "AST", -3, 0, }, /* Atlantic Standard */ + { "ADT", -3, 1, }, /* Atlantic Daylight */ + { "WAT", -1, 0, }, /* West Africa */ + + { "GMT", 0, 0, }, /* Greenwich Mean */ + { "UTC", 0, 0, }, /* Universal (Coordinated) */ + { "Z", 0, 0, }, /* Zulu, alias for UTC */ + + { "WET", 0, 0, }, /* Western European */ + { "BST", 0, 1, }, /* British Summer */ + { "CET", +1, 0, }, /* Central European */ + { "MET", +1, 0, }, /* Middle European */ + { "MEWT", +1, 0, }, /* Middle European Winter */ + { "MEST", +1, 1, }, /* Middle European Summer */ + { "CEST", +1, 1, }, /* Central European Summer */ + { "MESZ", +1, 1, }, /* Middle European Summer */ + { "FWT", +1, 0, }, /* French Winter */ + { "FST", +1, 1, }, /* French Summer */ + { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ + { "EEST", +2, 1, }, /* Eastern European Daylight */ + { "WAST", +7, 0, }, /* West Australian Standard */ + { "WADT", +7, 1, }, /* West Australian Daylight */ + { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ + { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ + { "EAST", +10, 0, }, /* Eastern Australian Standard */ + { "EADT", +10, 1, }, /* Eastern Australian Daylight */ + { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ + { "NZT", +12, 0, }, /* New Zealand */ + { "NZST", +12, 0, }, /* New Zealand Standard */ + { "NZDT", +12, 1, }, /* New Zealand Daylight */ + { "IDLE", +12, 0, }, /* International Date Line East */ +}; + +static int match_string(const char *date, const char *str) +{ + int i = 0; + + for (i = 0; *date; date++, str++, i++) { + if (*date == *str) + continue; + if (toupper(*date) == toupper(*str)) + continue; + if (!isalnum(*date)) + break; + return 0; + } + return i; +} + +static int skip_alpha(const char *date) +{ + int i = 0; + do { + i++; + } while (isalpha(date[i])); + return i; +} + +/* +* Parse month, weekday, or timezone name +*/ +static int match_alpha(const char *date, struct tm *tm, int *offset) +{ + unsigned int i; + + for (i = 0; i < 12; i++) { + int match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + return match; + } + } + + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { + tm->tm_wday = i; + return match; + } + } + + for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { + int match = match_string(date, timezone_names[i].name); + if (match >= 3 || match == (int)strlen(timezone_names[i].name)) { + int off = timezone_names[i].offset; + + /* This is bogus, but we like summer */ + off += timezone_names[i].dst; + + /* Only use the tz name offset if we don't have anything better */ + if (*offset == -1) + *offset = 60*off; + + return match; + } + } + + if (match_string(date, "PM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 12; + return 2; + } + + if (match_string(date, "AM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 0; + return 2; + } + + /* BAD CRAP */ + return skip_alpha(date); +} + +static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) +{ + if (month > 0 && month < 13 && day > 0 && day < 32) { + struct tm check = *tm; + struct tm *r = (now_tm ? &check : tm); + time_t specified; + + r->tm_mon = month - 1; + r->tm_mday = day; + if (year == -1) { + if (!now_tm) + return 1; + r->tm_year = now_tm->tm_year; + } + else if (year >= 1970 && year < 2100) + r->tm_year = year - 1900; + else if (year > 70 && year < 100) + r->tm_year = year; + else if (year < 38) + r->tm_year = year + 100; + else + return 0; + if (!now_tm) + return 1; + + specified = tm_to_time_t(r); + + /* Be it commit time or author time, it does not make + * sense to specify timestamp way into the future. Make + * sure it is not later than ten days from now... + */ + if (now + 10*24*3600 < specified) + return 0; + tm->tm_mon = r->tm_mon; + tm->tm_mday = r->tm_mday; + if (year != -1) + tm->tm_year = r->tm_year; + return 1; + } + return 0; +} + +static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) +{ + time_t now; + struct tm now_tm; + struct tm *refuse_future; + long num2, num3; + + num2 = strtol(end+1, &end, 10); + num3 = -1; + if (*end == c && isdigit(end[1])) + num3 = strtol(end+1, &end, 10); + + /* Time? Date? */ + switch (c) { + case ':': + if (num3 < 0) + num3 = 0; + if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { + tm->tm_hour = num; + tm->tm_min = num2; + tm->tm_sec = num3; + break; + } + return 0; + + case '-': + case '/': + case '.': + now = time(NULL); + refuse_future = NULL; + if (gmtime_r(&now, &now_tm)) + refuse_future = &now_tm; + + if (num > 70) { + /* yyyy-mm-dd? */ + if (is_date(num, num2, num3, refuse_future, now, tm)) + break; + /* yyyy-dd-mm? */ + if (is_date(num, num3, num2, refuse_future, now, tm)) + break; + } + /* Our eastern European friends say dd.mm.yy[yy] + * is the norm there, so giving precedence to + * mm/dd/yy[yy] form only when separator is not '.' + */ + if (c != '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ + if (is_date(num3, num2, num, refuse_future, now, tm)) + break; + /* Funny European mm.dd.yy */ + if (c == '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + return 0; + } + return end - date; +} + +/* + * Have we filled in any part of the time/date yet? + * We just do a binary 'and' to see if the sign bit + * is set in all the values. + */ +static inline int nodate(struct tm *tm) +{ + return (tm->tm_year & + tm->tm_mon & + tm->tm_mday & + tm->tm_hour & + tm->tm_min & + tm->tm_sec) < 0; +} + +/* + * We've seen a digit. Time? Year? Date? + */ +static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) +{ + int n; + char *end; + unsigned long num; + + num = strtoul(date, &end, 10); + + /* + * Seconds since 1970? We trigger on that for any numbers with + * more than 8 digits. This is because we don't want to rule out + * numbers like 20070606 as a YYYYMMDD date. + */ + if (num >= 100000000 && nodate(tm)) { + time_t time = num; + if (gmtime_r(&time, tm)) { + *tm_gmt = 1; + return end - date; + } + } + + /* + * Check for special formats: num[-.:/]num[same]num + */ + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (isdigit(end[1])) { + int match = match_multi_number(num, *end, date, end, tm); + if (match) + return match; + } + } + + /* + * None of the special formats? Try to guess what + * the number meant. We use the number of digits + * to make a more educated guess.. + */ + n = 0; + do { + n++; + } while (isdigit(date[n])); + + /* Four-digit year or a timezone? */ + if (n == 4) { + if (num <= 1400 && *offset == -1) { + unsigned int minutes = num % 100; + unsigned int hours = num / 100; + *offset = hours*60 + minutes; + } else if (num > 1900 && num < 2100) + tm->tm_year = num - 1900; + return n; + } + + /* + * Ignore lots of numerals. We took care of 4-digit years above. + * Days or months must be one or two digits. + */ + if (n > 2) + return n; + + /* + * NOTE! We will give precedence to day-of-month over month or + * year numbers in the 1-12 range. So 05 is always "mday 5", + * unless we already have a mday.. + * + * IOW, 01 Apr 05 parses as "April 1st, 2005". + */ + if (num > 0 && num < 32 && tm->tm_mday < 0) { + tm->tm_mday = num; + return n; + } + + /* Two-digit year? */ + if (n == 2 && tm->tm_year < 0) { + if (num < 10 && tm->tm_mday >= 0) { + tm->tm_year = num + 100; + return n; + } + if (num >= 70) { + tm->tm_year = num; + return n; + } + } + + if (num > 0 && num < 13 && tm->tm_mon < 0) + tm->tm_mon = num-1; + + return n; +} + +static int match_tz(const char *date, int *offp) +{ + char *end; + int hour = strtoul(date + 1, &end, 10); + int n = end - (date + 1); + int min = 0; + + if (n == 4) { + /* hhmm */ + min = hour % 100; + hour = hour / 100; + } else if (n != 2) { + min = 99; /* random crap */ + } else if (*end == ':') { + /* hh:mm? */ + min = strtoul(end + 1, &end, 10); + if (end - (date + 1) != 5) + min = 99; /* random crap */ + } /* otherwise we parsed "hh" */ + + /* + * Don't accept any random crap. Even though some places have + * offset larger than 12 hours (e.g. Pacific/Kiritimati is at + * UTC+14), there is something wrong if hour part is much + * larger than that. We might also want to check that the + * minutes are divisible by 15 or something too. (Offset of + * Kathmandu, Nepal is UTC+5:45) + */ + if (min < 60 && hour < 24) { + int offset = hour * 60 + min; + if (*date == '-') + offset = -offset; + *offp = offset; + } + return end - date; +} + +/* + * Parse a string like "0 +0000" as ancient timestamp near epoch, but + * only when it appears not as part of any other string. + */ +static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset) +{ + char *end; + unsigned long stamp; + int ofs; + + if (*date < '0' || '9' <= *date) + return -1; + stamp = strtoul(date, &end, 10); + if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) + return -1; + date = end + 2; + ofs = strtol(date, &end, 10); + if ((*end != '\0' && (*end != '\n')) || end != date + 4) + return -1; + ofs = (ofs / 100) * 60 + (ofs % 100); + if (date[-1] == '-') + ofs = -ofs; + *timestamp = stamp; + *offset = ofs; + return 0; +} + +/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 + (i.e. English) day/month names, and it doesn't work correctly with %z. */ +static int parse_date_basic(const char *date, unsigned long *timestamp, int *offset) +{ + struct tm tm; + int tm_gmt; + unsigned long dummy_timestamp; + int dummy_offset; + + if (!timestamp) + timestamp = &dummy_timestamp; + if (!offset) + offset = &dummy_offset; + + memset(&tm, 0, sizeof(tm)); + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + tm.tm_isdst = -1; + tm.tm_hour = -1; + tm.tm_min = -1; + tm.tm_sec = -1; + *offset = -1; + tm_gmt = 0; + + if (*date == '@' && + !match_object_header_date(date + 1, timestamp, offset)) + return 0; /* success */ + for (;;) { + int match = 0; + unsigned char c = *date; + + /* Stop at end of string or newline */ + if (!c || c == '\n') + break; + + if (isalpha(c)) + match = match_alpha(date, &tm, offset); + else if (isdigit(c)) + match = match_digit(date, &tm, offset, &tm_gmt); + else if ((c == '-' || c == '+') && isdigit(date[1])) + match = match_tz(date, offset); + + if (!match) { + /* BAD CRAP */ + match = 1; + } + + date += match; + } + + /* mktime uses local timezone */ + *timestamp = tm_to_time_t(&tm); + if (*offset == -1) + *offset = ((time_t)*timestamp - mktime(&tm)) / 60; + + if (*timestamp == (unsigned long)-1) + return -1; + + if (!tm_gmt) + *timestamp -= *offset * 60; + return 0; /* success */ +} + + +/* + * Relative time update (eg "2 days ago"). If we haven't set the time + * yet, we need to set it from current time. + */ +static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec) +{ + time_t n; + + if (tm->tm_mday < 0) + tm->tm_mday = now->tm_mday; + if (tm->tm_mon < 0) + tm->tm_mon = now->tm_mon; + if (tm->tm_year < 0) { + tm->tm_year = now->tm_year; + if (tm->tm_mon > now->tm_mon) + tm->tm_year--; + } + + n = mktime(tm) - sec; + localtime_r(&n, tm); + return n; +} + +static void date_now(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 0); +} + +static void date_yesterday(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 24*60*60); +} + +static void date_time(struct tm *tm, struct tm *now, int hour) +{ + if (tm->tm_hour < hour) + date_yesterday(tm, now, NULL); + tm->tm_hour = hour; + tm->tm_min = 0; + tm->tm_sec = 0; +} + +static void date_midnight(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 0); +} + +static void date_noon(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 12); +} + +static void date_tea(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 17); +} + +static void date_pm(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(now); + int hour, n = *num; + *num = 0; + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12) + 12; +} + +static void date_am(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(now); + int hour, n = *num; + *num = 0; + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12); +} + +static void date_never(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(now); + GIT_UNUSED(num); + time_t n = 0; + localtime_r(&n, tm); +} + +static const struct special { + const char *name; + void (*fn)(struct tm *, struct tm *, int *); +} special[] = { + { "yesterday", date_yesterday }, + { "noon", date_noon }, + { "midnight", date_midnight }, + { "tea", date_tea }, + { "PM", date_pm }, + { "AM", date_am }, + { "never", date_never }, + { "now", date_now }, + { NULL } +}; + +static const char *number_name[] = { + "zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine", "ten", +}; + +static const struct typelen { + const char *type; + int length; +} typelen[] = { + { "seconds", 1 }, + { "minutes", 60 }, + { "hours", 60*60 }, + { "days", 24*60*60 }, + { "weeks", 7*24*60*60 }, + { NULL } +}; + +static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) +{ + const struct typelen *tl; + const struct special *s; + const char *end = date; + int i; + + while (isalpha(*++end)); + ; + + for (i = 0; i < 12; i++) { + int match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + *touched = 1; + return end; + } + } + + for (s = special; s->name; s++) { + int len = strlen(s->name); + if (match_string(date, s->name) == len) { + s->fn(tm, now, num); + *touched = 1; + return end; + } + } + + if (!*num) { + for (i = 1; i < 11; i++) { + int len = strlen(number_name[i]); + if (match_string(date, number_name[i]) == len) { + *num = i; + *touched = 1; + return end; + } + } + if (match_string(date, "last") == 4) { + *num = 1; + *touched = 1; + } + return end; + } + + tl = typelen; + while (tl->type) { + int len = strlen(tl->type); + if (match_string(date, tl->type) >= len-1) { + update_tm(tm, now, tl->length * *num); + *num = 0; + *touched = 1; + return end; + } + tl++; + } + + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { + int diff, n = *num -1; + *num = 0; + + diff = tm->tm_wday - i; + if (diff <= 0) + n++; + diff += 7*n; + + update_tm(tm, now, diff * 24 * 60 * 60); + *touched = 1; + return end; + } + } + + if (match_string(date, "months") >= 5) { + int n; + update_tm(tm, now, 0); /* fill in date fields if needed */ + n = tm->tm_mon - *num; + *num = 0; + while (n < 0) { + n += 12; + tm->tm_year--; + } + tm->tm_mon = n; + *touched = 1; + return end; + } + + if (match_string(date, "years") >= 4) { + update_tm(tm, now, 0); /* fill in date fields if needed */ + tm->tm_year -= *num; + *num = 0; + *touched = 1; + return end; + } + + return end; +} + +static const char *approxidate_digit(const char *date, struct tm *tm, int *num) +{ + char *end; + unsigned long number = strtoul(date, &end, 10); + + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (isdigit(end[1])) { + int match = match_multi_number(number, *end, date, end, tm); + if (match) + return date + match; + } + } + + /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ + if (date[0] != '0' || end - date <= 2) + *num = number; + return end; +} + +/* + * Do we have a pending number at the end, or when + * we see a new one? Let's assume it's a month day, + * as in "Dec 6, 1992" + */ +static void pending_number(struct tm *tm, int *num) +{ + int number = *num; + + if (number) { + *num = 0; + if (tm->tm_mday < 0 && number < 32) + tm->tm_mday = number; + else if (tm->tm_mon < 0 && number < 13) + tm->tm_mon = number-1; + else if (tm->tm_year < 0) { + if (number > 1969 && number < 2100) + tm->tm_year = number - 1900; + else if (number > 69 && number < 100) + tm->tm_year = number; + else if (number < 38) + tm->tm_year = 100 + number; + /* We screw up for number = 00 ? */ + } + } +} + +static unsigned long approxidate_str(const char *date, + const struct timeval *tv, + int *error_ret) +{ + int number = 0; + int touched = 0; + struct tm tm, now; + time_t time_sec; + + time_sec = tv->tv_sec; + localtime_r(&time_sec, &tm); + now = tm; + + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + + for (;;) { + unsigned char c = *date; + if (!c) + break; + date++; + if (isdigit(c)) { + pending_number(&tm, &number); + date = approxidate_digit(date-1, &tm, &number); + touched = 1; + continue; + } + if (isalpha(c)) + date = approxidate_alpha(date-1, &tm, &now, &number, &touched); + } + pending_number(&tm, &number); + if (!touched) + *error_ret = 1; + return update_tm(&tm, &now, 0); +} + +unsigned long approxidate_careful(const char *date, int *error_ret) +{ + struct timeval tv; + unsigned long timestamp; + int offset; + int dummy = 0; + if (!error_ret) + error_ret = &dummy; + + if (!parse_date_basic(date, ×tamp, &offset)) { + *error_ret = 0; + return timestamp; + } + + gettimeofday(&tv, NULL); + return approxidate_str(date, &tv, error_ret); +} diff --git a/src/date.h b/src/date.h new file mode 100644 index 000000000..80df47aeb --- /dev/null +++ b/src/date.h @@ -0,0 +1,12 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_date_h__ +#define INCLUDE_date_h__ + +unsigned long approxidate_careful(const char *date, int *error_ret); + +#endif diff --git a/src/revparse.c b/src/revparse.c index 547426449..8b2787348 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -9,18 +9,9 @@ #include "common.h" #include "buffer.h" +#include "date.h" -#include "git2/revparse.h" -#include "git2/object.h" -#include "git2/oid.h" -#include "git2/refs.h" -#include "git2/tag.h" -#include "git2/commit.h" -#include "git2/reflog.h" -#include "git2/refs.h" -#include "git2/repository.h" -#include "git2/config.h" -#include "git2/revwalk.h" +#include "git2.h" GIT_BEGIN_DECL @@ -116,6 +107,36 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const } +static int all_chars_are_digits(const char *str, size_t len) +{ + size_t i=0; + for (i=0; i '9') return 0; + } + return 1; +} + +static void normalize_maybe_empty_refname(git_buf *buf, git_repository *repo, const char *refspec, size_t refspeclen) +{ + git_reference *ref; + + if (!refspeclen) { + /* Empty refspec means current branch (target of HEAD) */ + git_reference_lookup(&ref, repo, "HEAD"); + git_buf_puts(buf, git_reference_target(ref)); + git_reference_free(ref); + } else if (strstr(refspec, "HEAD")) { + /* Explicit head */ + git_buf_puts(buf, refspec); + }else { + if (git__prefixcmp(refspec, "refs/heads/") != 0) { + git_buf_printf(buf, "refs/heads/%s", refspec); + } else { + git_buf_puts(buf, refspec); + } + } +} + static int walk_ref_history(git_object **out, git_repository *repo, const char *refspec, const char *reflogspec) { git_reference *ref; @@ -125,6 +146,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * const git_reflog_entry *entry; git_buf buf = GIT_BUF_INIT; size_t refspeclen = strlen(refspec); + size_t reflogspeclen = strlen(reflogspec); if (git__prefixcmp(reflogspec, "@{") != 0 || git__suffixcmp(reflogspec, "}") != 0) { @@ -153,26 +175,16 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * n--; if (!n) { char *branchname = strrchr(msg, ' ') + 1; - git_buf_printf(&buf, "refs/heads/%s", branchname); - retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); + retcode = revparse_lookup_object(out, repo, branchname); break; } } } } else { - if (!refspeclen) { - /* Empty refspec means current branch */ - /* Get the target of HEAD */ - git_reference_lookup(&ref, repo, "HEAD"); - git_buf_puts(&buf, git_reference_target(ref)); - git_reference_free(ref); - } else { - if (git__prefixcmp(refspec, "refs/heads/") != 0) { - git_buf_printf(&buf, "refs/heads/%s", refspec); - } else { - git_buf_puts(&buf, refspec); - } - } + git_buf datebuf = GIT_BUF_INIT; + git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); + int date_error = 0; + time_t timestamp = approxidate_careful(git_buf_cstr(&datebuf), &date_error); /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { @@ -200,24 +212,72 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * } /* @{N} -> Nth prior value for the ref (from reflog) */ - else if (!git__strtol32(&n, reflogspec+2, NULL, 0)) { + else if (all_chars_are_digits(reflogspec+2, reflogspeclen-3) && + !git__strtol32(&n, reflogspec+2, NULL, 0) && + n <= 100000000) { /* Allow integer time */ + normalize_maybe_empty_refname(&buf, repo, refspec, refspeclen); + if (n == 0) { retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); } else if (!git_reference_lookup(&ref, repo, git_buf_cstr(&buf))) { if (!git_reflog_read(&reflog, ref)) { - 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); + int numentries = git_reflog_entrycount(reflog); + if (numentries < n) { + giterr_set(GITERR_REFERENCE, "Reflog for '%s' has only %d entries, asked for %d", + git_buf_cstr(&buf), numentries, n); + retcode = GIT_ERROR; + } else { + 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); + } } git_reference_free(ref); } } - /* @{Anything else} -> try to parse the expression into a date, and get the value of the ref as it - was then. */ - else { - /* TODO */ + else if (!date_error) { + /* Ref as it was on a certain date */ + normalize_maybe_empty_refname(&buf, repo, refspec, refspeclen); + + if (!git_reference_lookup(&ref, repo, git_buf_cstr(&buf))) { + git_reflog *reflog; + if (!git_reflog_read(&reflog, ref)) { + /* Keep walking until we find an entry older than the given date */ + int numentries = git_reflog_entrycount(reflog); + int i; + + /* TODO: clunky. Factor "now" into a utility */ + git_signature *sig; + git_signature_now(&sig, "blah", "blah"); + git_time as_of = sig->when; + git_signature_free(sig); + + as_of.time = (timestamp > 0) + ? timestamp + : sig->when.time + timestamp; + + for (i=numentries-1; i>0; i--) { + const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, i); + git_time commit_time = git_reflog_entry_committer(entry)->when; + if (git__time_cmp(&commit_time, &as_of) <= 0 ) { + retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); + break; + } + } + + if (!i) { + /* Didn't find a match. Use the oldest revision in the reflog. */ + const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, 0); + retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); + } + } + + git_reference_free(ref); + } } + + git_buf_free(&datebuf); } if (reflog) git_reflog_free(reflog); @@ -367,6 +427,9 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec git_buf_free(&buf); git_revwalk_free(walk); } + if (retcode < 0) { + giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", movement); + } return retcode; } diff --git a/src/util.h b/src/util.h index 2081f29f9..4d1ee680d 100644 --- a/src/util.h +++ b/src/util.h @@ -209,4 +209,11 @@ GIT_INLINE(bool) git__isspace(int c) return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v'); } +GIT_INLINE(int) git__time_cmp(const git_time *a, const git_time *b) +{ + /* Adjust for time zones. Times are in seconds, offsets are in minutes. */ + git_time_t adjusted_a = a->time + ((b->offset - a->offset) * 60); + return adjusted_a - b->time; +} + #endif /* INCLUDE_util_h__ */ diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index e967f7a48..d7affafe9 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -113,6 +113,7 @@ void test_refs_revparse__reflog(void) { 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, "@{1000}")); test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); @@ -137,8 +138,14 @@ void test_refs_revparse__revwalk(void) void test_refs_revparse__date(void) { - /* Not ready yet - test_object("HEAD@{100 years ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{2012-4-30 10:23:20}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - */ + test_object("HEAD@{10 years ago}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{2012-4-30 10:23:20 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{2012-4-30 10:24 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{2012-4-30 16:24 -0200}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{1335806600}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{1335816640}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + + /* Core git gives a65fedf, because they don't take time zones into account. */ + test_object("master@{1335806640}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); } diff --git a/tests/resources/testrepo.git/logs/HEAD b/tests/resources/testrepo.git/logs/HEAD index 5bdcb7ce7ab5bc3258f7624098383de7176fbbf5..b16c9b313a04b8330fbf2dc06802d588bef18b0d 100644 GIT binary patch delta 35 lcmdnOwuNnj6C)0|3b*3Zei2 delta 35 lcmdnOwuNnj6C)0|3aA3Yq`_ diff --git a/tests/resources/testrepo.git/logs/refs/heads/master b/tests/resources/testrepo.git/logs/refs/heads/master index c41e87a3f97753e6e32e6d22bb45cf7d2b99063f..e1c729a45fffcd5c751fdb694f21b57f818b315d 100644 GIT binary patch delta 36 rcmcb`bc<<1J)_0MhC>n-1_qqT`MJ57B~}W_8L7$HnR)37nR#3Q^z;m^ delta 36 rcmcb`bc<<1J)`-=hC>qO1_qqT`MJ57B~}W_8L7$HnR)37nR#3Q^uG+N From d13c1a8b60eb8f45cb14373cfd8e42f5f4d44749 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 09:48:14 -0700 Subject: [PATCH 19/45] Fixing broken tests. --- tests-clar/network/remotelocal.c | 4 ++-- tests-clar/refs/branches/listall.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c index 35fa072ef..86461b1e5 100644 --- a/tests-clar/network/remotelocal.c +++ b/tests-clar/network/remotelocal.c @@ -98,7 +98,7 @@ void test_network_remotelocal__retrieve_advertised_references(void) cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ + cl_assert_equal_i(how_many_refs, 19); } void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) @@ -112,7 +112,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ + cl_assert_equal_i(how_many_refs, 19); git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ remote = NULL; diff --git a/tests-clar/refs/branches/listall.c b/tests-clar/refs/branches/listall.c index 391177368..b65378e35 100644 --- a/tests-clar/refs/branches/listall.c +++ b/tests-clar/refs/branches/listall.c @@ -30,17 +30,17 @@ static void assert_retrieval(unsigned int flags, unsigned int expected_count) { cl_git_pass(git_branch_list(&branch_list, repo, flags)); - cl_assert(branch_list.count == expected_count); + cl_assert_equal_i(branch_list.count, expected_count); } void test_refs_branches_listall__retrieve_all_branches(void) { - assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 6 + 1); + assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 8); } void test_refs_branches_listall__retrieve_remote_branches(void) { - assert_retrieval(GIT_BRANCH_REMOTE, 1); + assert_retrieval(GIT_BRANCH_REMOTE, 2); } void test_refs_branches_listall__retrieve_local_branches(void) From ec6a632a1bdc5e14c14964005946f35ad61c0259 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 13:21:58 -0700 Subject: [PATCH 20/45] Simplifying revparse_lookup_fully_qualified_ref. --- src/revparse.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 8b2787348..4c03fdf38 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -29,24 +29,12 @@ static void set_invalid_syntax_err(const char *spec) static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec) { - git_reference *ref; - git_object *obj = NULL; + git_oid resolved; - if (!git_reference_lookup(&ref, repo, spec)) { - git_reference *resolved_ref; - if (!git_reference_resolve(&resolved_ref, ref)) { - if (!git_object_lookup(&obj, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY)) { - *out = obj; - } - git_reference_free(resolved_ref); - } - git_reference_free(ref); - } - if (obj) { - return 0; - } + if (git_reference_name_to_oid(&resolved, repo, spec) < 0) + return GIT_ERROR; - return GIT_ERROR; + return git_object_lookup(out, repo, &resolved, GIT_OBJ_ANY); } static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) From 46c2ead05d289b4ed1ad96c63b75a228f02dd74c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 13:39:34 -0700 Subject: [PATCH 21/45] Now properly handling branches with "-g" in their names. --- src/revparse.c | 6 +++--- tests-clar/refs/revparse.c | 1 + tests/resources/testrepo.git/refs/heads/not-good | Bin 0 -> 41 bytes 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 tests/resources/testrepo.git/refs/heads/not-good diff --git a/src/revparse.c b/src/revparse.c index 4c03fdf38..c22bd98d8 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -56,9 +56,9 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const /* "git describe" output; snip everything before/including "-g" */ substr = strstr(spec, "-g"); - if (substr) { - spec = substr + 2; - speclen = strlen(spec); + if (substr && + !revparse_lookup_object(out, repo, substr+2)) { + return 0; } /* SHA or prefix */ diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index d7affafe9..4c9eeb4c3 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -66,6 +66,7 @@ void test_refs_revparse__partial_refs(void) void test_refs_revparse__describe_output(void) { test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__nth_parent(void) diff --git a/tests/resources/testrepo.git/refs/heads/not-good b/tests/resources/testrepo.git/refs/heads/not-good new file mode 100644 index 0000000000000000000000000000000000000000..3d8f0a402b492c248b12ac72b6a66eebe941023f GIT binary patch literal 41 vcmYc^GfhiPNi()gOifEQF)&IoPD(OMH8M#_Gf6Q?1In40nwXoL8gKys2{Q}F literal 0 HcmV?d00001 From 2b35c45f1be2c5384100625ae7c2a31b5f75acb4 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 13:40:53 -0700 Subject: [PATCH 22/45] Rev-parse: now @{-N} syntax searches in the right direction! --- src/revparse.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index c22bd98d8..53cc7a347 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -130,7 +130,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * git_reference *ref; git_reflog *reflog = NULL; int n, retcode = GIT_ERROR; - size_t i, refloglen; + int i, refloglen; const git_reflog_entry *entry; git_buf buf = GIT_BUF_INIT; size_t refspeclen = strlen(refspec); @@ -144,6 +144,8 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ if (refspeclen == 0 && !git__prefixcmp(reflogspec, "@{-")) { + regex_t regex; + if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 || n < 1) { giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec); @@ -153,18 +155,22 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * git_reflog_read(&reflog, ref); git_reference_free(ref); - refloglen = git_reflog_entrycount(reflog); - for (i=0; i < refloglen; i++) { - const char *msg; - entry = git_reflog_entry_byindex(reflog, i); + if (!regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED)) { + regmatch_t regexmatches[2]; - msg = git_reflog_entry_msg(entry); - if (!git__prefixcmp(msg, "checkout: moving")) { - n--; - if (!n) { - char *branchname = strrchr(msg, ' ') + 1; - retcode = revparse_lookup_object(out, repo, branchname); - break; + refloglen = git_reflog_entrycount(reflog); + for (i=refloglen-1; i >= 0; i--) { + const char *msg; + entry = git_reflog_entry_byindex(reflog, i); + + msg = git_reflog_entry_msg(entry); + if (!regexec(®ex, msg, 2, regexmatches, 0)) { + n--; + if (!n) { + 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; + } } } } From c8a33547a0442576baff7cbf683076ec89c68dbb Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 14:12:30 -0700 Subject: [PATCH 23/45] Rev-parse: now capturing and reporting regex errors. --- src/revparse.c | 19 ++++++++++++++----- tests-clar/refs/revparse.c | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 53cc7a347..98285f0d0 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -145,6 +145,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ if (refspeclen == 0 && !git__prefixcmp(reflogspec, "@{-")) { regex_t regex; + int regex_error; if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 || n < 1) { @@ -155,7 +156,10 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * git_reflog_read(&reflog, ref); git_reference_free(ref); - if (!regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED)) { + regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED); + if (regex_error != 0) { + giterr_set_regex(®ex, regex_error); + } else { regmatch_t regexmatches[2]; refloglen = git_reflog_entrycount(reflog); @@ -173,6 +177,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * } } } + regfree(®ex); } } else { git_buf datebuf = GIT_BUF_INIT; @@ -385,6 +390,7 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec 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); @@ -393,7 +399,10 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec /* Extract the regex from the movement string */ git_buf_put(&buf, movement+2, strlen(movement)-3); - if (!regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED)) { + reg_error = regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED); + if (reg_error != 0) { + giterr_set_regex(&preg, reg_error); + } else { while(!git_revwalk_next(&oid, walk)) { git_object *walkobj; char str[41]; @@ -415,15 +424,15 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec 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); git_revwalk_free(walk); } - if (retcode < 0) { - giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", movement); - } return retcode; } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 4c9eeb4c3..c069846e7 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -130,6 +130,8 @@ void test_refs_revparse__revwalk(void) { cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}")); + cl_assert(strstr(git_lasterror(), "parentheses not balanced") != NULL); test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); From b41384b473e9ea4cfaea6f42b106e805bae8ab93 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 14:14:09 -0700 Subject: [PATCH 24/45] Plugging memory leak. --- src/revparse.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/revparse.c b/src/revparse.c index 98285f0d0..5f527443c 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -270,6 +270,8 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, 0); retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); } + + git_reflog_free(reflog); } git_reference_free(ref); From 7e79d389a400952a24a5619a8bee88b7b40bee72 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 15:05:19 -0700 Subject: [PATCH 25/45] Rev-parse: regex check for "git describe" output. --- src/revparse.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/revparse.c b/src/revparse.c index 5f527443c..0fea5ce8d 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -37,6 +37,22 @@ static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository * 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(®ex, ".+-[0-9]+-g[0-9a-fA-F]+", REG_EXTENDED); + if (regex_error != 0) { + giterr_set_regex(®ex, regex_error); + return 1; /* To be safe */ + } + retcode = regexec(®ex, spec, 0, NULL, 0); + regfree(®ex); + return retcode == 0; +} + static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) { size_t speclen = strlen(spec); @@ -57,6 +73,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const /* "git describe" output; snip everything before/including "-g" */ substr = strstr(spec, "-g"); if (substr && + spec_looks_like_describe_output(spec) && !revparse_lookup_object(out, repo, substr+2)) { return 0; } From 94952ded3aed856d8fd41ca9a65fcbe59a301efa Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 May 2012 15:07:04 -0700 Subject: [PATCH 26/45] Rev-parse: proper error checking. --- src/revparse.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 0fea5ce8d..7cb96b806 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -169,32 +169,34 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec); return GIT_ERROR; } - git_reference_lookup(&ref, repo, "HEAD"); - git_reflog_read(&reflog, ref); - git_reference_free(ref); - regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED); - if (regex_error != 0) { - giterr_set_regex(®ex, regex_error); - } else { - regmatch_t regexmatches[2]; + if (!git_reference_lookup(&ref, repo, "HEAD")) { + if (!git_reflog_read(&reflog, ref)) { + regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED); + if (regex_error != 0) { + giterr_set_regex(®ex, regex_error); + } else { + regmatch_t regexmatches[2]; - refloglen = git_reflog_entrycount(reflog); - for (i=refloglen-1; i >= 0; i--) { - const char *msg; - entry = git_reflog_entry_byindex(reflog, i); + refloglen = git_reflog_entrycount(reflog); + for (i=refloglen-1; i >= 0; i--) { + const char *msg; + entry = git_reflog_entry_byindex(reflog, i); - msg = git_reflog_entry_msg(entry); - if (!regexec(®ex, msg, 2, regexmatches, 0)) { - n--; - if (!n) { - 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; + msg = git_reflog_entry_msg(entry); + if (!regexec(®ex, msg, 2, regexmatches, 0)) { + n--; + if (!n) { + 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(®ex); } } - regfree(®ex); + git_reference_free(ref); } } else { git_buf datebuf = GIT_BUF_INIT; From 92ad5a5cda94c04b24d84c20ede235d69bedb988 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 11 May 2012 11:50:54 -0700 Subject: [PATCH 27/45] Rebasing onto libgit2/development: cleanup. --- tests-clar/network/remotelocal.c | 4 ++-- tests-clar/refs/branches/listall.c | 4 ++-- tests-clar/refs/revparse.c | 4 ++-- .../resources/testrepo.git/logs/HEAD | Bin .../resources/testrepo.git/logs/refs/heads/br2 | Bin .../resources/testrepo.git/logs/refs/heads/master | Bin .../testrepo.git/logs/refs/heads/not-good | 1 + .../testrepo.git/logs/refs/remotes/origin/HEAD | Bin .../84/9a5e34a26815e821f865b8479f5815a47af0fe | 2 ++ .../resources/testrepo.git/refs/heads/not-good | Bin .../testrepo.git/refs/remotes/test/master | Bin .../resources/testrepo.git/refs/tags/hard_tag | 1 + .../resources/testrepo.git/refs/tags/wrapped_tag | 1 + .../d7/eddec7b3610726791785bf839065953f10341b | Bin 134 -> 0 bytes tests/resources/testrepo.git/refs/tags/hard_tag | Bin 41 -> 0 bytes .../resources/testrepo.git/refs/tags/wrapped_tag | Bin 41 -> 0 bytes 16 files changed, 11 insertions(+), 6 deletions(-) rename {tests => tests-clar}/resources/testrepo.git/logs/HEAD (100%) rename {tests => tests-clar}/resources/testrepo.git/logs/refs/heads/br2 (100%) rename {tests => tests-clar}/resources/testrepo.git/logs/refs/heads/master (100%) create mode 100644 tests-clar/resources/testrepo.git/logs/refs/heads/not-good rename {tests => tests-clar}/resources/testrepo.git/logs/refs/remotes/origin/HEAD (100%) create mode 100644 tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe rename {tests => tests-clar}/resources/testrepo.git/refs/heads/not-good (100%) rename {tests => tests-clar}/resources/testrepo.git/refs/remotes/test/master (100%) create mode 100644 tests-clar/resources/testrepo.git/refs/tags/hard_tag create mode 100644 tests-clar/resources/testrepo.git/refs/tags/wrapped_tag delete mode 100644 tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b delete mode 100644 tests/resources/testrepo.git/refs/tags/hard_tag delete mode 100644 tests/resources/testrepo.git/refs/tags/wrapped_tag diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c index 86461b1e5..4bf9fb6d8 100644 --- a/tests-clar/network/remotelocal.c +++ b/tests-clar/network/remotelocal.c @@ -98,7 +98,7 @@ void test_network_remotelocal__retrieve_advertised_references(void) cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert_equal_i(how_many_refs, 19); + cl_assert_equal_i(how_many_refs, 20); } void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) @@ -112,7 +112,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert_equal_i(how_many_refs, 19); + cl_assert_equal_i(how_many_refs, 20); git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ remote = NULL; diff --git a/tests-clar/refs/branches/listall.c b/tests-clar/refs/branches/listall.c index b65378e35..8eb228d31 100644 --- a/tests-clar/refs/branches/listall.c +++ b/tests-clar/refs/branches/listall.c @@ -35,7 +35,7 @@ static void assert_retrieval(unsigned int flags, unsigned int expected_count) void test_refs_branches_listall__retrieve_all_branches(void) { - assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 8); + assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); } void test_refs_branches_listall__retrieve_remote_branches(void) @@ -45,5 +45,5 @@ void test_refs_branches_listall__retrieve_remote_branches(void) void test_refs_branches_listall__retrieve_local_branches(void) { - assert_retrieval(GIT_BRANCH_LOCAL, 6); + assert_retrieval(GIT_BRANCH_LOCAL, 7); } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index c069846e7..b8318923d 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -13,7 +13,7 @@ static void test_object(const char *spec, const char *expected_oid) char objstr[64] = {0}; cl_git_pass(git_revparse_single(&g_obj, g_repo, spec)); - git_oid_to_string(objstr, 64, git_object_id(g_obj)); + git_oid_fmt(objstr, git_object_id(g_obj)); cl_assert_equal_s(objstr, expected_oid); git_object_free(g_obj); @@ -131,7 +131,7 @@ void test_refs_revparse__revwalk(void) cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}")); - cl_assert(strstr(git_lasterror(), "parentheses not balanced") != NULL); + cl_assert(strstr(giterr_last()->message, "parentheses not balanced") != NULL); test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); diff --git a/tests/resources/testrepo.git/logs/HEAD b/tests-clar/resources/testrepo.git/logs/HEAD similarity index 100% rename from tests/resources/testrepo.git/logs/HEAD rename to tests-clar/resources/testrepo.git/logs/HEAD diff --git a/tests/resources/testrepo.git/logs/refs/heads/br2 b/tests-clar/resources/testrepo.git/logs/refs/heads/br2 similarity index 100% rename from tests/resources/testrepo.git/logs/refs/heads/br2 rename to tests-clar/resources/testrepo.git/logs/refs/heads/br2 diff --git a/tests/resources/testrepo.git/logs/refs/heads/master b/tests-clar/resources/testrepo.git/logs/refs/heads/master similarity index 100% rename from tests/resources/testrepo.git/logs/refs/heads/master rename to tests-clar/resources/testrepo.git/logs/refs/heads/master diff --git a/tests-clar/resources/testrepo.git/logs/refs/heads/not-good b/tests-clar/resources/testrepo.git/logs/refs/heads/not-good new file mode 100644 index 000000000..bfbeacb8a --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/refs/heads/not-good @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub 1336761944 -0700 branch: Created from master diff --git a/tests/resources/testrepo.git/logs/refs/remotes/origin/HEAD b/tests-clar/resources/testrepo.git/logs/refs/remotes/origin/HEAD similarity index 100% rename from tests/resources/testrepo.git/logs/refs/remotes/origin/HEAD rename to tests-clar/resources/testrepo.git/logs/refs/remotes/origin/HEAD diff --git a/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe b/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe new file mode 100644 index 000000000..71019a636 --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe @@ -0,0 +1,2 @@ +xM F]s41x(IKݽ/_P@!8)es + N&FGSƄh{+CZzvF7Z-kx\[P8GK/^ l>.4 \ No newline at end of file diff --git a/tests/resources/testrepo.git/refs/heads/not-good b/tests-clar/resources/testrepo.git/refs/heads/not-good similarity index 100% rename from tests/resources/testrepo.git/refs/heads/not-good rename to tests-clar/resources/testrepo.git/refs/heads/not-good diff --git a/tests/resources/testrepo.git/refs/remotes/test/master b/tests-clar/resources/testrepo.git/refs/remotes/test/master similarity index 100% rename from tests/resources/testrepo.git/refs/remotes/test/master rename to tests-clar/resources/testrepo.git/refs/remotes/test/master diff --git a/tests-clar/resources/testrepo.git/refs/tags/hard_tag b/tests-clar/resources/testrepo.git/refs/tags/hard_tag new file mode 100644 index 000000000..59ce65649 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/tags/hard_tag @@ -0,0 +1 @@ +849a5e34a26815e821f865b8479f5815a47af0fe diff --git a/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag b/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag new file mode 100644 index 000000000..59ce65649 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag @@ -0,0 +1 @@ +849a5e34a26815e821f865b8479f5815a47af0fe diff --git a/tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b b/tests/resources/testrepo.git/objects/d7/eddec7b3610726791785bf839065953f10341b deleted file mode 100644 index 32fc53202e7c7e9b3c9e6eedbd443f9efb03764a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 134 zcmV;10D1p-0UeA@4#FT1MqP7?xqybB0W>kjcm>@XC+|D2m`>sX}}4CI*9a-;9c`|zsrKFa63~ORuf2ZOT(z(Xsr+GMG8v* diff --git a/tests/resources/testrepo.git/refs/tags/wrapped_tag b/tests/resources/testrepo.git/refs/tags/wrapped_tag deleted file mode 100644 index 1621c20d698c61eacb4c43f3967b4c693d51e117..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 41 ucmV~$K>+|D2m`>sX}}4CI*9a-;9c`|zsrKFa63~ORuf2ZOT(z(Xsr+GMG8v* From 72b86bae50f3dc0ec1a9df0b62c70ab9bdb37c77 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 11 May 2012 11:58:02 -0700 Subject: [PATCH 28/45] Rev-parse: better error handling for chaining. Fixed an error where "nonexistant^N" or similar would fall into an assert. This now properly returns an error. --- src/revparse.c | 5 ++++- tests-clar/refs/revparse.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/revparse.c b/src/revparse.c index 7cb96b806..3487f5638 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -564,7 +564,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec if (current_state != next_state && next_state != REVPARSE_STATE_DONE) { /* Leaving INIT state, find the object specified, in case that state needs it */ - revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)); + if (revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)) < 0) { + retcode = GIT_ERROR; + next_state = REVPARSE_STATE_DONE; + } } break; diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index b8318923d..31a832aca 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -36,6 +36,8 @@ void test_refs_revparse__cleanup(void) void test_refs_revparse__nonexistant_object(void) { cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist^1")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist~2")); } void test_refs_revparse__shas(void) From 7c22e72ba65fa4ac9c2989d115435aa60788136c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 11 May 2012 12:21:58 -0700 Subject: [PATCH 29/45] Removing test whose results are platform-dependent. --- tests-clar/refs/revparse.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 31a832aca..783b03b3a 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -133,7 +133,6 @@ void test_refs_revparse__revwalk(void) cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}")); - cl_assert(strstr(giterr_last()->message, "parentheses not balanced") != NULL); test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); From 1ce4cc0164fbc14fcb7f686483aa581bc946bfdb Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 15 May 2012 15:41:05 -0700 Subject: [PATCH 30/45] Fix date.c build in msvc. Ported the win32 implementations of gmtime_r, localtime_r, and gettimeofday to be part of the posix compatibility layer, and fixed git_signature_now to use them. --- src/date.c | 19 +++++++---- src/revparse.c | 12 ++++--- src/signature.c | 12 ------- src/util.h | 2 +- src/win32/posix.h | 4 +++ src/win32/posix_w32.c | 76 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 23 deletions(-) diff --git a/src/date.c b/src/date.c index b4708b8ef..90f2f149e 100644 --- a/src/date.c +++ b/src/date.c @@ -4,11 +4,18 @@ * Copyright (C) Linus Torvalds, 2005 */ +#include "common.h" + +#ifndef GIT_WIN32 +#include +#endif + #include "date.h" #include "cache.h" +#include "posix.h" #include -#include +#include typedef enum { DATE_NORMAL = 0, @@ -299,7 +306,7 @@ static int match_multi_number(unsigned long num, char c, const char *date, char * We just do a binary 'and' to see if the sign bit * is set in all the values. */ -static inline int nodate(struct tm *tm) +static int nodate(struct tm *tm) { return (tm->tm_year & tm->tm_mon & @@ -599,9 +606,9 @@ static void date_tea(struct tm *tm, struct tm *now, int *num) static void date_pm(struct tm *tm, struct tm *now, int *num) { - GIT_UNUSED(now); int hour, n = *num; *num = 0; + GIT_UNUSED(now); hour = tm->tm_hour; if (n) { @@ -614,9 +621,9 @@ static void date_pm(struct tm *tm, struct tm *now, int *num) static void date_am(struct tm *tm, struct tm *now, int *num) { - GIT_UNUSED(now); int hour, n = *num; *num = 0; + GIT_UNUSED(now); hour = tm->tm_hour; if (n) { @@ -629,9 +636,9 @@ static void date_am(struct tm *tm, struct tm *now, int *num) static void date_never(struct tm *tm, struct tm *now, int *num) { + time_t n = 0; GIT_UNUSED(now); GIT_UNUSED(num); - time_t n = 0; localtime_r(&n, tm); } @@ -821,7 +828,7 @@ static unsigned long approxidate_str(const char *date, { int number = 0; int touched = 0; - struct tm tm, now; + struct tm tm = {0}, now; time_t time_sec; time_sec = tv->tv_sec; diff --git a/src/revparse.c b/src/revparse.c index 3487f5638..8eb5c11ae 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -199,10 +199,12 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * git_reference_free(ref); } } else { - git_buf datebuf = GIT_BUF_INIT; - git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); int date_error = 0; - time_t timestamp = approxidate_careful(git_buf_cstr(&datebuf), &date_error); + time_t timestamp; + git_buf datebuf = GIT_BUF_INIT; + + git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); + timestamp = approxidate_careful(git_buf_cstr(&datebuf), &date_error); /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { @@ -267,8 +269,10 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * /* TODO: clunky. Factor "now" into a utility */ git_signature *sig; + git_time as_of; + git_signature_now(&sig, "blah", "blah"); - git_time as_of = sig->when; + as_of = sig->when; git_signature_free(sig); as_of.time = (timestamp > 0) diff --git a/src/signature.c b/src/signature.c index 4d6d11c70..74ef84376 100644 --- a/src/signature.c +++ b/src/signature.c @@ -113,26 +113,14 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema time_t offset; struct tm *utc_tm, *local_tm; git_signature *sig; - -#ifndef GIT_WIN32 struct tm _utc, _local; -#endif *sig_out = NULL; time(&now); - /** - * On Win32, `gmtime_r` doesn't exist but - * `gmtime` is threadsafe, so we can use that - */ -#ifdef GIT_WIN32 - utc_tm = gmtime(&now); - local_tm = localtime(&now); -#else utc_tm = gmtime_r(&now, &_utc); local_tm = localtime_r(&now, &_local); -#endif offset = mktime(local_tm) - mktime(utc_tm); offset /= 60; diff --git a/src/util.h b/src/util.h index 4d1ee680d..9003c08ad 100644 --- a/src/util.h +++ b/src/util.h @@ -213,7 +213,7 @@ GIT_INLINE(int) git__time_cmp(const git_time *a, const git_time *b) { /* Adjust for time zones. Times are in seconds, offsets are in minutes. */ git_time_t adjusted_a = a->time + ((b->offset - a->offset) * 60); - return adjusted_a - b->time; + return (int)(adjusted_a - b->time); } #endif /* INCLUDE_util_h__ */ diff --git a/src/win32/posix.h b/src/win32/posix.h index 2666fccb4..55732f5ac 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -52,4 +52,8 @@ extern int p_rename(const char *from, const char *to); extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags); extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags); +extern struct tm * localtime_r (const time_t *timer, struct tm *result); +extern struct tm * gmtime_r (const time_t *timer, struct tm *result); +extern int gettimeofday(struct timeval *tv, struct timezone *tz); + #endif diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 10de70da8..092bafed0 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -470,3 +470,79 @@ int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) return send(socket, buffer, (int)length, flags); } + +/** + * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html + * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that + */ +struct tm * +localtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = localtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} +struct tm * +gmtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = gmtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} From dd9e4abc1ba1701efac0c3af3b1ceede2bd561a4 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 30 May 2012 11:46:42 -0700 Subject: [PATCH 31/45] Approxidate: use libgit2 naming/calling conventions. Also use git_time_t (64-bit integer) for time values, although the 2038 problem is still present on 32-bit machines. --- src/date.c | 34 ++++++++++++++++------------------ src/date.h | 4 +++- src/revparse.c | 4 ++-- tests-clar/date/date.c | 13 +++++++++++++ 4 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 tests-clar/date/date.c diff --git a/src/date.c b/src/date.c index 90f2f149e..a2f36982a 100644 --- a/src/date.c +++ b/src/date.c @@ -30,7 +30,7 @@ typedef enum { /* * This is like mktime, but without normalization of tm_wday and tm_yday. */ -static time_t tm_to_time_t(const struct tm *tm) +static git_time_t tm_to_time_t(const struct tm *tm) { static const int mdays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 @@ -454,7 +454,7 @@ static int match_tz(const char *date, int *offp) * Parse a string like "0 +0000" as ancient timestamp near epoch, but * only when it appears not as part of any other string. */ -static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset) +static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) { char *end; unsigned long stamp; @@ -479,11 +479,11 @@ static int match_object_header_date(const char *date, unsigned long *timestamp, /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 (i.e. English) day/month names, and it doesn't work correctly with %z. */ -static int parse_date_basic(const char *date, unsigned long *timestamp, int *offset) +static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) { struct tm tm; int tm_gmt; - unsigned long dummy_timestamp; + git_time_t dummy_timestamp; int dummy_offset; if (!timestamp) @@ -533,7 +533,7 @@ static int parse_date_basic(const char *date, unsigned long *timestamp, int *off if (*offset == -1) *offset = ((time_t)*timestamp - mktime(&tm)) / 60; - if (*timestamp == (unsigned long)-1) + if (*timestamp == (git_time_t)-1) return -1; if (!tm_gmt) @@ -546,7 +546,7 @@ static int parse_date_basic(const char *date, unsigned long *timestamp, int *off * Relative time update (eg "2 days ago"). If we haven't set the time * yet, we need to set it from current time. */ -static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec) +static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) { time_t n; @@ -822,9 +822,9 @@ static void pending_number(struct tm *tm, int *num) } } -static unsigned long approxidate_str(const char *date, - const struct timeval *tv, - int *error_ret) +static git_time_t approxidate_str(const char *date, + const struct timeval *tv, + int *error_ret) { int number = 0; int touched = 0; @@ -859,20 +859,18 @@ static unsigned long approxidate_str(const char *date, return update_tm(&tm, &now, 0); } -unsigned long approxidate_careful(const char *date, int *error_ret) +int git__date_parse(git_time_t *out, const char *date) { struct timeval tv; - unsigned long timestamp; - int offset; - int dummy = 0; - if (!error_ret) - error_ret = &dummy; + git_time_t timestamp; + int offset, error_ret=0; if (!parse_date_basic(date, ×tamp, &offset)) { - *error_ret = 0; - return timestamp; + *out = timestamp; + return 0; } gettimeofday(&tv, NULL); - return approxidate_str(date, &tv, error_ret); + *out = approxidate_str(date, &tv, &error_ret); + return error_ret; } diff --git a/src/date.h b/src/date.h index 80df47aeb..6859ee5e6 100644 --- a/src/date.h +++ b/src/date.h @@ -7,6 +7,8 @@ #ifndef INCLUDE_date_h__ #define INCLUDE_date_h__ -unsigned long approxidate_careful(const char *date, int *error_ret); +#include "git2/types.h" + +int git__date_parse(git_time_t *out, const char *date); #endif diff --git a/src/revparse.c b/src/revparse.c index 8eb5c11ae..3615ac519 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -200,11 +200,11 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * } } else { int date_error = 0; - time_t timestamp; + git_time_t timestamp; git_buf datebuf = GIT_BUF_INIT; git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); - timestamp = approxidate_careful(git_buf_cstr(&datebuf), &date_error); + date_error = git__date_parse(×tamp, git_buf_cstr(&datebuf)); /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { diff --git a/tests-clar/date/date.c b/tests-clar/date/date.c new file mode 100644 index 000000000..1018a1383 --- /dev/null +++ b/tests-clar/date/date.c @@ -0,0 +1,13 @@ +#include "clar_libgit2.h" + +#include "date.h" + +void test_date_date__overflow(void) +{ + git_time_t d2038, d2039; + + /* This fails on a 32-bit machine. */ + cl_git_pass(git__date_parse(&d2038, "2038-1-1")); + cl_git_pass(git__date_parse(&d2039, "2039-1-1")); + cl_assert(d2038 < d2039); +} From 244d2f6b80161978cce5bacc3ac6e663b530bb65 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 30 May 2012 16:52:11 -0700 Subject: [PATCH 32/45] Rev-parse: add "tag:README" syntax. --- src/revparse.c | 60 ++++++++++++++++++++++++++++++++++++++ tests-clar/refs/revparse.c | 10 +++++++ 2 files changed, 70 insertions(+) diff --git a/src/revparse.c b/src/revparse.c index 3615ac519..1e78d7623 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -10,6 +10,7 @@ #include "common.h" #include "buffer.h" #include "date.h" +#include "tree.h" #include "git2.h" @@ -19,6 +20,7 @@ typedef enum { REVPARSE_STATE_INIT, REVPARSE_STATE_CARET, REVPARSE_STATE_LINEAR, + REVPARSE_STATE_COLON, REVPARSE_STATE_DONE, } revparse_state; @@ -535,6 +537,45 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m return 0; } +static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repository *repo, const char *path) +{ + char *str = git__strdup(path); + char *tok; + git_tree *tree2 = tree; + const git_tree_entry *entry; + + while ((tok = git__strtok(&str, "/\\")) != NULL) { + entry = git_tree_entry_byname(tree2, tok); + if (tree2 != tree) git_tree_free(tree2); + if (entry_is_tree(entry)) { + if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { + return NULL; + } + } + } + + return entry; +} + +static int handle_colon_syntax(git_object **out, + git_repository *repo, + git_object *obj, + const char *path) +{ + git_tree *tree; + const git_tree_entry *entry; + + /* Dereference until we reach a tree. */ + if (dereference_to_type(&obj, obj, GIT_OBJ_TREE) < 0) { + return GIT_ERROR; + } + tree = (git_tree*)obj; + + /* Find the blob at the given path. */ + entry = git_tree_entry_bypath(tree, repo, path); + return git_tree_entry_2object(out, repo, entry); +} + 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; @@ -545,6 +586,13 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec assert(out && repo && spec); + if (spec[0] == ':') { + /* Either a global grep (":/foo") or a merge-stage path lookup (":2:Makefile"). + Neither of these are handled just yet. */ + giterr_set(GITERR_INVALID, "Unimplemented"); + return GIT_ERROR; + } + while (current_state != REVPARSE_STATE_DONE) { switch (current_state) { case REVPARSE_STATE_INIT: @@ -561,6 +609,8 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec next_state = REVPARSE_STATE_CARET; } else if (*spec_cur == '~') { next_state = REVPARSE_STATE_LINEAR; + } else if (*spec_cur == ':') { + next_state = REVPARSE_STATE_COLON; } else { git_buf_putc(&specbuffer, *spec_cur); } @@ -617,6 +667,16 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec spec_cur++; break; + case REVPARSE_STATE_COLON: + if (*spec_cur) { + 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; + case REVPARSE_STATE_DONE: if (cur_obj && *out != cur_obj) git_object_free(cur_obj); if (next_obj && *out != next_obj) git_object_free(next_obj); diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 783b03b3a..2da857415 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -153,3 +153,13 @@ void test_refs_revparse__date(void) /* Core git gives a65fedf, because they don't take time zones into account. */ test_object("master@{1335806640}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); } + +void test_refs_revparse__colon(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/foo")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); + + test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); + test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); +} From b183a92fc2f030acf4c8e0cd76a5259d8f65669a Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 31 May 2012 13:42:58 -0700 Subject: [PATCH 33/45] Rev-parse: Plug memory leaks. --- src/revparse.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/revparse.c b/src/revparse.c index 1e78d7623..60e819c00 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -541,6 +541,7 @@ static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repositor { char *str = git__strdup(path); char *tok; + void *alloc = str; git_tree *tree2 = tree; const git_tree_entry *entry; @@ -549,11 +550,13 @@ static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repositor if (tree2 != tree) git_tree_free(tree2); if (entry_is_tree(entry)) { if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { + free(alloc); return NULL; } } } + free(alloc); return entry; } @@ -573,6 +576,7 @@ static int handle_colon_syntax(git_object **out, /* Find the blob at the given path. */ entry = git_tree_entry_bypath(tree, repo, path); + git_tree_free(tree); return git_tree_entry_2object(out, repo, entry); } From 2497106f91f1eea363c173a53b17416e3191c9d7 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 1 Jun 2012 11:41:54 -0700 Subject: [PATCH 34/45] Rev-parse: add test with deeper path. --- tests-clar/refs/revparse.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 2da857415..9ecdb8265 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -160,6 +160,7 @@ void test_refs_revparse__colon(void) cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); + test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); } From 734efe4b8e8f6cfc820c7f88b3fb4e4221cec0ab Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 1 Jun 2012 14:18:52 -0700 Subject: [PATCH 35/45] Rev-parse: implement ":/foo" syntax. --- src/revparse.c | 52 ++++++++++++++++++++++++++++++++++---- tests-clar/refs/revparse.c | 6 ++++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 60e819c00..6d88a1d38 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -432,9 +432,6 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec } else { while(!git_revwalk_next(&oid, walk)) { git_object *walkobj; - char str[41]; - git_oid_fmt(str, &oid); - str[40] = 0; /* Fetch the commit object, and check for matches in the message */ if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { @@ -580,6 +577,49 @@ static int handle_colon_syntax(git_object **out, return git_tree_entry_2object(out, repo, entry); } +static int git__revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) +{ + git_revwalk *walk; + int retcode = GIT_ERROR; + + if (!git_revwalk_new(&walk, repo)) { + regex_t preg; + int reg_error; + git_oid oid; + + git_revwalk_sorting(walk, GIT_SORT_TIME); + git_revwalk_push_glob(walk, "refs/heads/*"); + + reg_error = regcomp(&preg, pattern, REG_EXTENDED); + if (reg_error != 0) { + giterr_set_regex(&preg, reg_error); + } else { + git_object *walkobj = NULL, *resultobj = NULL; + while(!git_revwalk_next(&oid, walk)) { + /* Fetch the commit object, and check for matches in the message */ + if (walkobj != resultobj) git_object_free(walkobj); + if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { + if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { + /* Match! */ + resultobj = walkobj; + retcode = 0; + break; + } + } + } + if (!resultobj) { + giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern); + } else { + *out = resultobj; + } + regfree(&preg); + git_revwalk_free(walk); + } + } + + return retcode; +} + 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; @@ -591,8 +631,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec assert(out && repo && spec); if (spec[0] == ':') { - /* Either a global grep (":/foo") or a merge-stage path lookup (":2:Makefile"). - Neither of these are handled just yet. */ + if (spec[1] == '/') { + return git__revparse_global_grep(out, repo, spec+2); + } + /* TODO: support merge-stage path lookup (":2:Makefile"). */ giterr_set(GITERR_INVALID, "Unimplemented"); return GIT_ERROR; } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 9ecdb8265..0bd6ad704 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -156,11 +156,15 @@ void test_refs_revparse__date(void) void test_refs_revparse__colon(void) { - cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/foo")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/not found in any commit")); cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); + test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"); } From e267c9fc1a4910d1081e97b4d7a411658ddc0def Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 4 Jun 2012 06:03:08 -0700 Subject: [PATCH 36/45] Complete the AUTHORS list. --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 03904ff55..9f621b990 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Alexei Sholik Andreas Ericsson Ankur Sethi Ben Noordhuis +Ben Straub Benjamin C Meyer Brian Lopez Carlos Martín Nieto From 2c2cde47b8241e66cacd535d5f8c269d623a47f0 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 08:41:39 -0700 Subject: [PATCH 37/45] Fix signatures for tree calls. --- src/revparse.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 6d88a1d38..a6f2a159f 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -216,12 +216,12 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char * const char *remote; git_buf_clear(&buf); git_buf_printf(&buf, "branch.%s.remote", refspec); - if (!git_config_get_string(cfg, git_buf_cstr(&buf), &remote)) { + if (!git_config_get_string(&remote, cfg, git_buf_cstr(&buf))) { /* Yes. Find the first merge target name. */ const char *mergetarget; git_buf_clear(&buf); git_buf_printf(&buf, "branch.%s.merge", refspec); - if (!git_config_get_string(cfg, git_buf_cstr(&buf), &mergetarget) && + if (!git_config_get_string(&mergetarget, cfg, git_buf_cstr(&buf)) && !git__prefixcmp(mergetarget, "refs/heads/")) { /* Success. Look up the target and fetch the object. */ git_buf_clear(&buf); @@ -545,7 +545,7 @@ static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repositor while ((tok = git__strtok(&str, "/\\")) != NULL) { entry = git_tree_entry_byname(tree2, tok); if (tree2 != tree) git_tree_free(tree2); - if (entry_is_tree(entry)) { + if (git_tree_entry__is_tree(entry)) { if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { free(alloc); return NULL; @@ -574,7 +574,7 @@ static int handle_colon_syntax(git_object **out, /* Find the blob at the given path. */ entry = git_tree_entry_bypath(tree, repo, path); git_tree_free(tree); - return git_tree_entry_2object(out, repo, entry); + return git_tree_entry_to_object(out, repo, entry); } static int git__revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) From 8a385c0482aff009738f509892863707592434b9 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 12:25:22 -0700 Subject: [PATCH 38/45] Move git__date_parse declaration to util.h. --- src/date.c | 2 +- src/date.h | 14 -------------- src/revparse.c | 1 - src/util.h | 10 ++++++++++ tests-clar/date/date.c | 2 +- 5 files changed, 12 insertions(+), 17 deletions(-) delete mode 100644 src/date.h diff --git a/src/date.c b/src/date.c index a2f36982a..5529dc2f6 100644 --- a/src/date.c +++ b/src/date.c @@ -10,7 +10,7 @@ #include #endif -#include "date.h" +#include "util.h" #include "cache.h" #include "posix.h" diff --git a/src/date.h b/src/date.h deleted file mode 100644 index 6859ee5e6..000000000 --- a/src/date.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) 2009-2012 the libgit2 contributors - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_date_h__ -#define INCLUDE_date_h__ - -#include "git2/types.h" - -int git__date_parse(git_time_t *out, const char *date); - -#endif diff --git a/src/revparse.c b/src/revparse.c index a6f2a159f..3312345bb 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -9,7 +9,6 @@ #include "common.h" #include "buffer.h" -#include "date.h" #include "tree.h" #include "git2.h" diff --git a/src/util.h b/src/util.h index c4a55f524..eed2bc80c 100644 --- a/src/util.h +++ b/src/util.h @@ -230,4 +230,14 @@ GIT_INLINE(bool) git__iswildcard(int c) */ extern int git__parse_bool(int *out, const char *value); +/* + * Parse a string into a value as a git_time_t. + * + * Sample valid input: + * - "yesterday" + * - "July 17, 2003" + * - "2003-7-17 08:23" + */ +int git__date_parse(git_time_t *out, const char *date); + #endif /* INCLUDE_util_h__ */ diff --git a/tests-clar/date/date.c b/tests-clar/date/date.c index 1018a1383..d44ef85c8 100644 --- a/tests-clar/date/date.c +++ b/tests-clar/date/date.c @@ -1,6 +1,6 @@ #include "clar_libgit2.h" -#include "date.h" +#include "util.h" void test_date_date__overflow(void) { From 19d35d528cefe8ffe7b066648b7233cf7db3dce8 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 12:31:48 -0700 Subject: [PATCH 39/45] Prefer git__free() to free(). --- src/revparse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revparse.c b/src/revparse.c index 3312345bb..c406b7bfb 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -552,7 +552,7 @@ static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repositor } } - free(alloc); + git__free(alloc); return entry; } From 36c0802245e2c041db19d65173587c1a39cfb742 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 12:39:29 -0700 Subject: [PATCH 40/45] Omit failing test on 32-bit machines. This test is intended to verify that 64-bit machines can handle parsing dates in 2039 and beyond, and fails on 32-bit machines. It is now omitted when run on a 32-bit machine to eliminate an expected failure. --- tests-clar/date/date.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests-clar/date/date.c b/tests-clar/date/date.c index d44ef85c8..88881d1e1 100644 --- a/tests-clar/date/date.c +++ b/tests-clar/date/date.c @@ -4,10 +4,12 @@ void test_date_date__overflow(void) { +#ifdef __LP64__ git_time_t d2038, d2039; - /* This fails on a 32-bit machine. */ + /* This is expected to fail on a 32-bit machine. */ cl_git_pass(git__date_parse(&d2038, "2038-1-1")); cl_git_pass(git__date_parse(&d2039, "2039-1-1")); cl_assert(d2038 < d2039); +#endif } From d6391a626f0bac4880adda74528e3d6f5054c571 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 13:00:12 -0700 Subject: [PATCH 41/45] Rev-parse: stop referencing freed memory. Converted an internal utility to return an oid, rather than a tree entry (whose lifetime is tied to the parent tree, which was freed before returning). --- src/revparse.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index c406b7bfb..8775b80ee 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -533,7 +533,7 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m return 0; } -static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repository *repo, const char *path) +static int tree_entry_bypath(git_oid *out, git_tree *tree, git_repository *repo, const char *path) { char *str = git__strdup(path); char *tok; @@ -547,13 +547,14 @@ static const git_tree_entry* git_tree_entry_bypath(git_tree *tree, git_repositor if (git_tree_entry__is_tree(entry)) { if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { free(alloc); - return NULL; + return GIT_ERROR; } } } + git_oid_cpy(out, git_tree_entry_id(entry)); git__free(alloc); - return entry; + return 0; } static int handle_colon_syntax(git_object **out, @@ -562,7 +563,7 @@ static int handle_colon_syntax(git_object **out, const char *path) { git_tree *tree; - const git_tree_entry *entry; + git_oid oid; /* Dereference until we reach a tree. */ if (dereference_to_type(&obj, obj, GIT_OBJ_TREE) < 0) { @@ -571,9 +572,9 @@ static int handle_colon_syntax(git_object **out, tree = (git_tree*)obj; /* Find the blob at the given path. */ - entry = git_tree_entry_bypath(tree, repo, path); + tree_entry_bypath(&oid, tree, repo, path); git_tree_free(tree); - return git_tree_entry_to_object(out, repo, entry); + return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); } static int git__revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) From 1a728066c3b307a71c04f746bb6f322dacd50938 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 13:04:08 -0700 Subject: [PATCH 42/45] Remove 'git__' prefix from a static function. --- src/revparse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 8775b80ee..102e27a1f 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -577,7 +577,7 @@ static int handle_colon_syntax(git_object **out, return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); } -static int git__revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) +static int revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) { git_revwalk *walk; int retcode = GIT_ERROR; @@ -632,7 +632,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec if (spec[0] == ':') { if (spec[1] == '/') { - return git__revparse_global_grep(out, repo, spec+2); + return revparse_global_grep(out, repo, spec+2); } /* TODO: support merge-stage path lookup (":2:Makefile"). */ giterr_set(GITERR_INVALID, "Unimplemented"); From 9ecf860d48b39241a2dce61dd33455816064d56b Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 6 Jun 2012 13:24:25 -0700 Subject: [PATCH 43/45] Rename posix wrappers with 'p_' prefix. --- src/date.c | 12 ++++++------ src/posix.h | 8 ++++++++ src/signature.c | 4 ++-- src/win32/posix.h | 4 ---- src/win32/posix_w32.c | 6 +++--- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/date.c b/src/date.c index 5529dc2f6..658b09eb5 100644 --- a/src/date.c +++ b/src/date.c @@ -271,7 +271,7 @@ static int match_multi_number(unsigned long num, char c, const char *date, char case '.': now = time(NULL); refuse_future = NULL; - if (gmtime_r(&now, &now_tm)) + if (p_gmtime_r(&now, &now_tm)) refuse_future = &now_tm; if (num > 70) { @@ -334,7 +334,7 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt */ if (num >= 100000000 && nodate(tm)) { time_t time = num; - if (gmtime_r(&time, tm)) { + if (p_gmtime_r(&time, tm)) { *tm_gmt = 1; return end - date; } @@ -561,7 +561,7 @@ static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) } n = mktime(tm) - sec; - localtime_r(&n, tm); + p_localtime_r(&n, tm); return n; } @@ -639,7 +639,7 @@ static void date_never(struct tm *tm, struct tm *now, int *num) time_t n = 0; GIT_UNUSED(now); GIT_UNUSED(num); - localtime_r(&n, tm); + p_localtime_r(&n, tm); } static const struct special { @@ -832,7 +832,7 @@ static git_time_t approxidate_str(const char *date, time_t time_sec; time_sec = tv->tv_sec; - localtime_r(&time_sec, &tm); + p_localtime_r(&time_sec, &tm); now = tm; tm.tm_year = -1; @@ -870,7 +870,7 @@ int git__date_parse(git_time_t *out, const char *date) return 0; } - gettimeofday(&tv, NULL); + p_gettimeofday(&tv, NULL); *out = approxidate_str(date, &tv, &error_ret); return error_ret; } diff --git a/src/posix.h b/src/posix.h index d020d94ac..3f52f9b72 100644 --- a/src/posix.h +++ b/src/posix.h @@ -59,9 +59,17 @@ extern int p_rename(const char *from, const char *to); typedef int GIT_SOCKET; #define INVALID_SOCKET -1 +#define p_localtime_r localtime_r +#define p_gmtime_r gmtime_r +#define p_gettimeofday gettimeofday + #else typedef SOCKET GIT_SOCKET; +extern struct tm * p_localtime_r (const time_t *timer, struct tm *result); +extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result); +extern int p_gettimeofday(struct timeval *tv, struct timezone *tz); + #endif diff --git a/src/signature.c b/src/signature.c index 6b28a3e4c..332bdf65f 100644 --- a/src/signature.c +++ b/src/signature.c @@ -119,8 +119,8 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema time(&now); - utc_tm = gmtime_r(&now, &_utc); - local_tm = localtime_r(&now, &_local); + utc_tm = p_gmtime_r(&now, &_utc); + local_tm = p_localtime_r(&now, &_local); offset = mktime(local_tm) - mktime(utc_tm); offset /= 60; diff --git a/src/win32/posix.h b/src/win32/posix.h index c38caa8ee..baa4a3b4e 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -52,8 +52,4 @@ extern int p_rename(const char *from, const char *to); extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags); extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags); -extern struct tm * localtime_r (const time_t *timer, struct tm *result); -extern struct tm * gmtime_r (const time_t *timer, struct tm *result); -extern int gettimeofday(struct timeval *tv, struct timezone *tz); - #endif diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 092bafed0..f0441df97 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -476,7 +476,7 @@ int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that */ struct tm * -localtime_r (const time_t *timer, struct tm *result) +p_localtime_r (const time_t *timer, struct tm *result) { struct tm *local_result; local_result = localtime (timer); @@ -488,7 +488,7 @@ localtime_r (const time_t *timer, struct tm *result) return result; } struct tm * -gmtime_r (const time_t *timer, struct tm *result) +p_gmtime_r (const time_t *timer, struct tm *result) { struct tm *local_result; local_result = gmtime (timer); @@ -512,7 +512,7 @@ struct timezone int tz_dsttime; /* type of dst correction */ }; -int gettimeofday(struct timeval *tv, struct timezone *tz) +int p_gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; unsigned __int64 tmpres = 0; From 31dda6471687f0ec6985f3db2536e93a8c012df2 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 7 Jun 2012 12:16:39 -0700 Subject: [PATCH 44/45] Rename internal function. --- src/revparse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/revparse.c b/src/revparse.c index 102e27a1f..6bcdeb3a5 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -533,7 +533,7 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m return 0; } -static int tree_entry_bypath(git_oid *out, git_tree *tree, git_repository *repo, const char *path) +static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, const char *path) { char *str = git__strdup(path); char *tok; @@ -572,7 +572,7 @@ static int handle_colon_syntax(git_object **out, tree = (git_tree*)obj; /* Find the blob at the given path. */ - tree_entry_bypath(&oid, tree, repo, path); + oid_for_tree_path(&oid, tree, repo, path); git_tree_free(tree); return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); } From 327dc61f132a4999e006d8d8bd2080c1f5a34bf0 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 7 Jun 2012 12:28:08 -0700 Subject: [PATCH 45/45] Prefer git__free (again). --- src/revparse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revparse.c b/src/revparse.c index 6bcdeb3a5..62c193bf2 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -546,7 +546,7 @@ static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, if (tree2 != tree) git_tree_free(tree2); if (git_tree_entry__is_tree(entry)) { if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { - free(alloc); + git__free(alloc); return GIT_ERROR; } }