reduce code duplication in handling '-c commit' options
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
diff --git a/got/got.c b/got/got.c
index a16ed7b..10849ff 100644
--- a/got/got.c
+++ b/got/got.c
@@ -759,6 +759,26 @@ done:
}
static const struct got_error *
+resolve_commit_arg(struct got_object_id **commit_id,
+ const char *commit_id_arg, struct got_repository *repo)
+{
+ const struct got_error *err;
+ struct got_reference *ref;
+
+ err = got_ref_open(&ref, repo, commit_id_arg, 0);
+ if (err == NULL) {
+ err = got_ref_resolve(commit_id, repo, ref);
+ got_ref_close(ref);
+ } else {
+ if (err->code != GOT_ERR_NOT_REF)
+ return err;
+ err = got_repo_match_object_id_prefix(commit_id,
+ commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
+ }
+ return err;
+}
+
+static const struct got_error *
cmd_checkout(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -900,18 +920,8 @@ cmd_checkout(int argc, char *argv[])
}
if (commit_id_str) {
- struct got_object_id *commit_id = NULL;
- struct got_reference *ref;
- error = got_ref_open(&ref, repo, commit_id_str, 0);
- if (error == NULL) {
- error = got_ref_resolve(&commit_id, repo, ref);
- got_ref_close(ref);
- } else {
- if (error->code != GOT_ERR_NOT_REF)
- goto done;
- error = got_repo_match_object_id_prefix(&commit_id,
- commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
- }
+ struct got_object_id *commit_id;
+ error = resolve_commit_arg(&commit_id, commit_id_str, repo);
if (error)
goto done;
error = check_linear_ancestry(commit_id,
@@ -1117,21 +1127,10 @@ cmd_update(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- struct got_reference *ref;
- error = got_ref_open(&ref, repo, commit_id_str, 0);
- if (error == NULL) {
- error = got_ref_resolve(&commit_id, repo, ref);
- got_ref_close(ref);
- }
- else {
- if (error->code != GOT_ERR_NOT_REF)
- goto done;
- error = got_repo_match_object_id_prefix(&commit_id,
- commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
- }
+ error = resolve_commit_arg(&commit_id, commit_id_str, repo);
+ free(commit_id_str);
if (error)
goto done;
- free(commit_id_str);
error = got_object_id_str(&commit_id_str, commit_id);
if (error)
goto done;
@@ -2034,17 +2033,7 @@ cmd_blame(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- struct got_reference *ref;
- error = got_ref_open(&ref, repo, commit_id_str, 0);
- if (error == NULL) {
- error = got_ref_resolve(&commit_id, repo, ref);
- got_ref_close(ref);
- } else {
- if (error->code != GOT_ERR_NOT_REF)
- goto done;
- error = got_repo_match_object_id_prefix(&commit_id,
- commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
- }
+ error = resolve_commit_arg(&commit_id, commit_id_str, repo);
if (error)
goto done;
}
@@ -2275,17 +2264,7 @@ cmd_tree(int argc, char *argv[])
if (error != NULL)
goto done;
} else {
- struct got_reference *ref;
- error = got_ref_open(&ref, repo, commit_id_str, 0);
- if (error == NULL) {
- error = got_ref_resolve(&commit_id, repo, ref);
- got_ref_close(ref);
- } else {
- if (error->code != GOT_ERR_NOT_REF)
- goto done;
- error = got_repo_match_object_id_prefix(&commit_id,
- commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
- }
+ error = resolve_commit_arg(&commit_id, commit_id_str, repo);
if (error)
goto done;
}