Rev-parse: proper error checking.
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
diff --git a/src/revparse.c b/src/revparse.c
index 0fea5ce..7cb96b8 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -169,32 +169,34 @@ static int walk_ref_history(git_object **out, git_repository *repo, const char *
giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec);
return GIT_ERROR;
}
- git_reference_lookup(&ref, repo, "HEAD");
- git_reflog_read(&reflog, ref);
- git_reference_free(ref);
- regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED);
- if (regex_error != 0) {
- giterr_set_regex(®ex, regex_error);
- } else {
- 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(®ex, 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;
+ if (!git_reference_lookup(&ref, repo, "HEAD")) {
+ if (!git_reflog_read(&reflog, ref)) {
+ regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED);
+ if (regex_error != 0) {
+ giterr_set_regex(®ex, regex_error);
+ } else {
+ 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(®ex, 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;
+ }
+ }
}
+ regfree(®ex);
}
}
- regfree(®ex);
+ git_reference_free(ref);
}
} else {
git_buf datebuf = GIT_BUF_INIT;