Skip to content

Commit 776a6a8

Browse files
committed
Return the oldest reflog entry on revparse when older time is given
For better compatibility with git command which returns the oldest log entry with a warning message.
1 parent 49ebc8a commit 776a6a8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/revparse.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t ide
207207
{
208208
git_reflog *reflog;
209209
size_t numentries;
210-
const git_reflog_entry *entry;
210+
const git_reflog_entry *entry = NULL;
211211
bool search_by_pos = (identifier <= 100000000);
212212

213213
if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0)
@@ -236,8 +236,12 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t ide
236236
break;
237237
}
238238

239-
if (i == numentries)
240-
goto notfound;
239+
if (i == numentries) {
240+
if (entry == NULL)
241+
goto notfound;
242+
243+
git_oid_cpy(oid, git_reflog_entry_id_new(entry));
244+
}
241245
}
242246

243247
git_reflog_free(reflog);

tests/refs/revparse.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ void test_refs_revparse__date(void)
399399
* a65fedf HEAD@{1335806603 -0900}: commit:
400400
* be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
401401
*/
402-
test_object("HEAD@{10 years ago}", NULL);
402+
test_object("HEAD@{10 years ago}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
403403

404404
test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
405405
test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
@@ -417,11 +417,12 @@ void test_refs_revparse__date(void)
417417

418418

419419
/*
420-
* $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}"
421-
* warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800.
420+
* $ git rev-parse "master@{2012-04-30 17:22:42 +0000}"
421+
* warning: log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800
422+
* be3563ae3f795b2b4353bcce3a527ad0a4f7f644
422423
*/
423-
test_object("master@{2012-04-30 17:22:42 +0000}", NULL);
424-
test_object("master@{2012-04-30 09:22:42 -0800}", NULL);
424+
test_object("master@{2012-04-30 17:22:42 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
425+
test_object("master@{2012-04-30 09:22:42 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
425426

426427
/*
427428
* $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
@@ -451,6 +452,12 @@ void test_refs_revparse__date(void)
451452
*/
452453
test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
453454
test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
455+
456+
/*
457+
* $ git rev-parse "with-empty-log@{2 days ago}" --
458+
* fatal: log for refs/heads/with-empty-log is empty
459+
*/
460+
test_object("with-empty-log@{2 days ago}", NULL);
454461
}
455462

456463
void test_refs_revparse__colon(void)

0 commit comments

Comments
 (0)