Commit 494ae940a07831fd1ce318f6fd0f04738bfc2fe5

nulltoken 2012-07-02T17:51:02

revparse: fix parsing of date specifiers

diff --git a/src/revparse.c b/src/revparse.c
index 774beef..8c15f46 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -267,31 +267,18 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char *
 					int numentries = git_reflog_entrycount(reflog);
 					int i;
 
-					/* TODO: clunky. Factor "now" into a utility */
-					git_signature *sig;
-					git_time as_of;
-
-					git_signature_now(&sig, "blah", "blah");
-					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--) {
+					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 ) {
+						if (commit_time.time - timestamp <= 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);
+					if (i ==  -1) {
+						/* Didn't find a match */
+						retcode = GIT_ENOTFOUND;
 					}
 
 					git_reflog_free(reflog);
diff --git a/src/util.h b/src/util.h
index eed2bc8..adc6650 100644
--- a/src/util.h
+++ b/src/util.h
@@ -209,13 +209,6 @@ 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 (int)(adjusted_a - b->time);
-}
-
 GIT_INLINE(bool) git__iswildcard(int c)
 {
 	return (c == '*' || c == '?' || c == '[');
diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c
index d28dfa5..e282cd7 100644
--- a/tests-clar/refs/revparse.c
+++ b/tests-clar/refs/revparse.c
@@ -155,14 +155,73 @@ void test_refs_revparse__revwalk(void)
 
 void test_refs_revparse__date(void)
 {
-	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 18:24 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
-	test_object("master@{2012-4-30 23:24 -0300}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+	/*
+	 * $ git reflog HEAD --date=iso
+	 * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master
+	 * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in
+	 * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2
+	 * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit:
+	 * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes
+	 *
+	 * $ git reflog HEAD --date=raw
+	 * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master
+	 * a4a7dce HEAD@{1335806617 -0900}: commit: checking in
+	 * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2
+	 * a65fedf HEAD@{1335806603 -0900}: commit:
+	 * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
+	 */
+	cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "HEAD@{10 years ago}"));
 
-	/* Core git gives a65fedf, because they don't take time zones into account. */
-	test_object("master@{1335806640}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
+	test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+	test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+	test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+
+	/*
+	 * $ git reflog master --date=iso
+	 * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in
+	 * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src...
+	 *
+	 * $ git reflog master --date=raw
+	 * a65fedf master@{1335806603 -0800}: commit: checking in
+	 * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso
+	 */
+
+
+	/*
+	 * $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}"
+	 * warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800.
+	 */
+	cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "master@{2012-04-30 17:22:42 +0000}"));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "master@{2012-04-30 09:22:42 -0800}"));
+
+	/*
+	 * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
+	 * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg
+	 */
+	test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
+	test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
+
+	/*
+	 * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}"
+	 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
+	 */
+	test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+
+	/*
+	 * $ git reflog -1 master@{2012-05-03}
+	 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
+	 */
+	test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+
+	/*
+	 * $ git reflog -1 "master@{1335806603}"
+	 * a65fedf
+	 *
+	 * $ git reflog -1 "master@{1335806602}"
+	 * be3563a
+	 */
+	test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+	test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
 }
 
 void test_refs_revparse__colon(void)