Cause error when date parsing is failed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
diff --git a/src/date.c b/src/date.c
index 2297ee6..af6694e 100644
--- a/src/date.c
+++ b/src/date.c
@@ -853,7 +853,7 @@ static git_time_t approxidate_str(const char *date,
}
pending_number(&tm, &number);
if (!touched)
- *error_ret = 1;
+ *error_ret = -1;
return update_tm(&tm, &now, 0);
}
@@ -872,7 +872,7 @@ int git__date_parse(git_time_t *out, const char *date)
return -1;
*out = approxidate_str(date, time_sec, &error_ret);
- return error_ret;
+ return error_ret;
}
int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date)
diff --git a/src/revparse.c b/src/revparse.c
index 8be22d6..2815bcf 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -348,8 +348,10 @@ static int handle_at_syntax(git_object **out, git_reference **ref, const char *s
goto cleanup;
}
- if (git__date_parse(×tamp, curly_braces_content) < 0)
+ if (git__date_parse(×tamp, curly_braces_content) < 0) {
+ error = GIT_EINVALIDSPEC;
goto cleanup;
+ }
error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), (size_t)timestamp);
diff --git a/tests/date/date.c b/tests/date/date.c
index 88881d1..ae787c0 100644
--- a/tests/date/date.c
+++ b/tests/date/date.c
@@ -13,3 +13,10 @@ void test_date_date__overflow(void)
cl_assert(d2038 < d2039);
#endif
}
+
+void test_date_date__invalid_date(void)
+{
+ git_time_t d;
+ cl_git_fail(git__date_parse(&d, ""));
+ cl_git_fail(git__date_parse(&d, "NEITHER_INTEGER_NOR_DATETIME"));
+}
diff --git a/tests/refs/revparse.c b/tests/refs/revparse.c
index d6ec802..8d3a7e0 100644
--- a/tests/refs/revparse.c
+++ b/tests/refs/revparse.c
@@ -460,6 +460,19 @@ void test_refs_revparse__date(void)
test_object("with-empty-log@{2 days ago}", NULL);
}
+void test_refs_revparse__invalid_date(void)
+{
+ /*
+ * $ git rev-parse HEAD@{} --
+ * fatal: bad revision 'HEAD@{}'
+ *
+ * $ git rev-parse HEAD@{NEITHER_INTEGER_NOR_DATETIME} --
+ * fatal: bad revision 'HEAD@{NEITHER_INTEGER_NOR_DATETIME}'
+ */
+ test_object("HEAD@{}", NULL);
+ test_object("HEAD@{NEITHER_INTEGER_NOR_DATETIME}", NULL);
+}
+
void test_refs_revparse__colon(void)
{
assert_invalid_single_spec(":/");