commit 04f57cb3f4f6793b5ade5115fc1227e0099c8859 from: Stefan Sperling date: Thu Jul 25 08:56:59 2019 UTC reduce code duplication in handling '-c commit' options commit - 30837e32b8c05d75e429f5587a658d6df7ddfda8 commit + 04f57cb3f4f6793b5ade5115fc1227e0099c8859 blob - a16ed7b70ecc012e088c02a4df0d6f13e4e268a6 blob + 10849ff6cdd8efadcc63584ba98f7628c71d6211 --- got/got.c +++ got/got.c @@ -755,6 +755,26 @@ done: free(head_commit_id); if (!err && !is_same_branch) err = got_error(GOT_ERR_ANCESTRY); + return err; +} + +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; } @@ -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; }