Commit b9ebcc59 by Vicent Martí

Merge pull request #684 from benstraub/rev-parse

Rev parse
parents cddb8efe 327dc61f
......@@ -6,6 +6,7 @@ Alexei Sholik
Andreas Ericsson
Ankur Sethi
Ben Noordhuis
Ben Straub
Benjamin C Meyer
Brian Lopez
Carlos Martín Nieto
......
......@@ -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"
......
/*
* 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"
/**
* @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_END_DECL
#endif
This diff is collapsed. Click to expand it.
......@@ -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
......
This diff is collapsed. Click to expand it.
......@@ -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
utc_tm = p_gmtime_r(&now, &_utc);
local_tm = p_localtime_r(&now, &_local);
offset = mktime(local_tm) - mktime(utc_tm);
offset /= 60;
......
......@@ -209,6 +209,13 @@ 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 == '[');
......@@ -223,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__ */
......@@ -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 *
p_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 *
p_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 p_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;
}
#include "clar_libgit2.h"
#include "util.h"
void test_date_date__overflow(void)
{
#ifdef __LP64__
git_time_t d2038, d2039;
/* 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
}
......@@ -107,7 +107,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, 20);
}
void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void)
......@@ -121,7 +121,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, 20);
git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */
remote = NULL;
......
......@@ -30,22 +30,22 @@ static void assert_retrieval(unsigned int flags, unsigned int expected_count)
{
cl_git_pass(git_branch_list(&branch_list, repo, flags));
cl_assert_equal_i(expected_count, branch_list.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, 9);
}
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)
{
assert_retrieval(GIT_BRANCH_LOCAL, 6);
assert_retrieval(GIT_BRANCH_LOCAL, 7);
}
static void assert_branch_list_contains(git_strarray *branches, const char* expected_branch_name)
......
#include "clar_libgit2.h"
#include "git2/revparse.h"
static git_repository *g_repo;
static git_object *g_obj;
/* Helpers */
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_fmt(objstr, git_object_id(g_obj));
cl_assert_equal_s(objstr, expected_oid);
git_object_free(g_obj);
g_obj = NULL;
}
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__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)
{
test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
}
void test_refs_revparse__head(void)
{
test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__full_refs(void)
{
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)
{
test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f");
}
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)
{
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)
{
test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__to_type(void)
{
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)
{
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)
{
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)
{
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");
test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
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}"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}"));
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)
{
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");
}
void test_refs_revparse__colon(void)
{
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");
}
......@@ -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
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 -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 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0700 branch: Created from refs/remotes/origin/br2
a4a7dce85cf63874e984719f4fdd239f5145052f a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -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
0000000000000000000000000000000000000000 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1336761944 -0700 branch: Created from master
0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git
a65fedf39aefe402d3bb6e24df4d4f5fe4547750
be3563ae3f795b2b4353bcce3a527ad0a4f7f644
849a5e34a26815e821f865b8479f5815a47af0fe
849a5e34a26815e821f865b8479f5815a47af0fe
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