Commit a346992f by Ben Straub

Rev-parse: @{time} syntax.

Ported date.c (for approxidate_careful) from git.git
revision aa39b85. Trimmed out the parts we're not
using.
parent 886f183a
......@@ -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"
......
This diff is collapsed. Click to expand it.
/*
* 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
......@@ -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<len; i++) {
if (str[i] < '0' || str[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,26 +212,74 @@ 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)) {
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);
git_buf_free(&buf);
return retcode;
......@@ -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;
}
......
......@@ -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__ */
......@@ -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");
}
0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git
be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0700 commit:
a65fedf39aefe402d3bb6e24df4d4f5fe4547750 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0700 checkout: moving from master to br2
c47800c7266a2be04c571c04d5a6614691ea99bd a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0700 commit: checking in
a4a7dce85cf63874e984719f4fdd239f5145052f a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806621 -0700 checkout: moving from br2 to master
be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0900 commit:
a65fedf39aefe402d3bb6e24df4d4f5fe4547750 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0900 checkout: moving from master to br2
c47800c7266a2be04c571c04d5a6614691ea99bd a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0900 commit: checking in
a4a7dce85cf63874e984719f4fdd239f5145052f a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806621 -0900 checkout: moving from br2 to master
0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git
be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0700 commit: checking in
0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0800 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git
be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0800 commit: checking in
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment