summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Straub <bstraub@github.com>2012-05-10 13:40:53 -0700
committerBen Straub <bstraub@github.com>2012-05-11 11:35:50 -0700
commit2b35c45f1be2c5384100625ae7c2a31b5f75acb4 (patch)
treeb7e37335df5080ae78faecac32977f015483a3ba
parent46c2ead05d289b4ed1ad96c63b75a228f02dd74c (diff)
downloadlibgit2-2b35c45f1be2c5384100625ae7c2a31b5f75acb4.tar.gz
Rev-parse: now @{-N} syntax searches in the right direction!
-rw-r--r--src/revparse.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/revparse.c b/src/revparse.c
index c22bd98d8..53cc7a347 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -130,7 +130,7 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char *
git_reference *ref;
git_reflog *reflog = NULL;
int n, retcode = GIT_ERROR;
- size_t i, refloglen;
+ int i, refloglen;
const git_reflog_entry *entry;
git_buf buf = GIT_BUF_INIT;
size_t refspeclen = strlen(refspec);
@@ -144,6 +144,8 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char *
/* "@{-N}" form means walk back N checkouts. That means the HEAD log. */
if (refspeclen == 0 && !git__prefixcmp(reflogspec, "@{-")) {
+ regex_t regex;
+
if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 ||
n < 1) {
giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec);
@@ -153,18 +155,22 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char *
git_reflog_read(&reflog, ref);
git_reference_free(ref);
- refloglen = git_reflog_entrycount(reflog);
- for (i=0; i < refloglen; i++) {
- const char *msg;
- entry = git_reflog_entry_byindex(reflog, i);
-
- msg = git_reflog_entry_msg(entry);
- if (!git__prefixcmp(msg, "checkout: moving")) {
- n--;
- if (!n) {
- char *branchname = strrchr(msg, ' ') + 1;
- retcode = revparse_lookup_object(out, repo, branchname);
- break;
+ if (!regcomp(&regex, "checkout: moving from (.*) to .*", REG_EXTENDED)) {
+ regmatch_t regexmatches[2];
+
+ refloglen = git_reflog_entrycount(reflog);
+ for (i=refloglen-1; i >= 0; i--) {
+ const char *msg;
+ entry = git_reflog_entry_byindex(reflog, i);
+
+ msg = git_reflog_entry_msg(entry);
+ if (!regexec(&regex, msg, 2, regexmatches, 0)) {
+ n--;
+ if (!n) {
+ git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so);
+ retcode = revparse_lookup_object(out, repo, git_buf_cstr(&buf));
+ break;
+ }
}
}
}