[PATCHv5 2/8] commit.c: new function for looking up a comit by name
Signed-off-by: Pat Notz <patnotz@gmail.com>
---
builtin/commit.c | 7 +------
commit.c | 13 +++++++++++++
commit.h | 1 +
3 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index bf9fcc1..9fe4bdc 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -896,17 +896,12 @@ static int parse_and_validate_options(int argc, const char *argv[],
if (!use_message && renew_authorship)
die("--reset-author can be used only with -C, -c or --amend.");
if (use_message) {
- unsigned char sha1[20];
static char utf8[] = "UTF-8";
const char *out_enc;
char *enc;
struct commit *commit;
- if (get_sha1(use_message, sha1))
- die("could not lookup commit %s", use_message);
- commit = lookup_commit_reference(sha1);
- if (!commit || parse_commit(commit))
- die("could not parse commit %s", use_message);
+ commit = lookup_commit_reference_by_name(use_message);
enc = get_header(commit, "encoding");
enc = enc ? enc : utf8;
diff --git a/commit.c b/commit.c
index 0094ec1..f51098a 100644
--- a/commit.c
+++ b/commit.c
@@ -49,6 +49,19 @@ struct commit *lookup_commit(const unsigned char *sha1)
return check_commit(obj, sha1, 0);
}
+struct commit *lookup_commit_reference_by_name(const char *name)
+{
+ unsigned char sha1[20];
+ struct commit *commit;
+
+ if (get_sha1(name, sha1))
+ die("could not lookup commit %s", name);
+ commit = lookup_commit_reference(sha1);
+ if (!commit || parse_commit(commit))
+ die("could not parse commit %s", name);
+ return commit;
+}
+
static unsigned long parse_commit_date(const char *buf, const char *tail)
{
const char *dateptr;
diff --git a/commit.h b/commit.h
index c246c94..6f4b586 100644
--- a/commit.h
+++ b/commit.h
@@ -36,6 +36,7 @@ struct commit *lookup_commit(const unsigned char *sha1);
struct commit *lookup_commit_reference(const unsigned char *sha1);
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
int quiet);
+struct commit *lookup_commit_reference_by_name(const char *name);
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
--
1.7.3.1