Commit 04f57cb3f4f6793b5ade5115fc1227e0099c8859

Stefan Sperling 2019-07-25T10:56:59

reduce code duplication in handling '-c commit' options

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;
 	}