Commit 3235492e3fcec3882427de904e5be159b6380edf

Stefan Sperling 2018-04-01T18:17:18

add the ability to start the log at a particular commit

diff --git a/got/got.c b/got/got.c
index fb1724c..a55e7d9 100644
--- a/got/got.c
+++ b/got/got.c
@@ -400,7 +400,7 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
 __dead void
 usage_log(void)
 {
-	fprintf(stderr, "usage: %s log [-p] [repository-path]\n",
+	fprintf(stderr, "usage: %s log [-p] [repository-path] [commit]\n",
 	    getprogname());
 	exit(1);
 }
@@ -410,10 +410,10 @@ cmd_log(int argc, char *argv[])
 {
 	const struct got_error *error;
 	struct got_repository *repo;
-	struct got_reference *head_ref;
-	struct got_object_id *id;
+	struct got_object_id *id = NULL;
 	struct got_object *obj;
 	char *repo_path = NULL;
+	char *commit_id_str = NULL;
 	int ch;
 	int show_patch = 0;
 
@@ -440,22 +440,36 @@ cmd_log(int argc, char *argv[])
 		repo_path = getcwd(NULL, 0);
 		if (repo_path == NULL)
 			err(1, "getcwd");
-	} else if (argc == 1)
+	} else if (argc == 1) {
 		repo_path = argv[0];
-	else
+	} else if (argc == 2) {
+		repo_path = argv[0];
+		commit_id_str = argv[1];
+	} else
 		usage_log();
 
 	error = got_repo_open(&repo, repo_path);
 	if (error != NULL)
 		return error;
-	error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
-	if (error != NULL)
-		return error;
-	error = got_ref_resolve(&id, repo, head_ref);
-	if (error != NULL)
-		return error;
 
-	error = got_object_open(&obj, repo, id);
+	if (commit_id_str == NULL) {
+		struct got_reference *head_ref;
+		error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
+		if (error != NULL)
+			return error;
+		error = got_ref_resolve(&id, repo, head_ref);
+		got_ref_close(head_ref);
+		if (error != NULL)
+			return error;
+		error = got_object_open(&obj, repo, id);
+	} else {
+		error = got_object_open_by_id_str(&obj, repo, commit_id_str);
+		if (error == NULL) {
+			id = got_object_get_id(obj);
+			if (id == NULL)
+				error = got_error_from_errno();
+		}
+	}
 	if (error != NULL)
 		return error;
 	if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
@@ -464,7 +478,6 @@ cmd_log(int argc, char *argv[])
 		error = got_error(GOT_ERR_OBJ_TYPE);
 	got_object_close(obj);
 	free(id);
-	got_ref_close(head_ref);
 	got_repo_close(repo);
 	return error;
 }
diff --git a/include/got_object.h b/include/got_object.h
index 93541c4..2b3af38 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -58,6 +58,7 @@ struct got_repository;
 const struct got_error *got_object_id_str(char **, struct got_object_id *);
 int got_object_id_cmp(struct got_object_id *, struct got_object_id *);
 struct got_object_id *got_object_id_dup(struct got_object_id *);
+struct got_object_id *got_object_get_id(struct got_object *);
 int got_object_get_type(struct got_object *);
 const struct got_error *got_object_open(struct got_object **,
     struct got_repository *, struct got_object_id *);
diff --git a/lib/object.c b/lib/object.c
index 7f38f5a..47ed4b5 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -89,6 +89,12 @@ got_object_id_dup(struct got_object_id *id1)
 	return id2;
 }
 
+struct got_object_id *
+got_object_get_id(struct got_object *obj)
+{
+	return got_object_id_dup(&obj->id);
+}
+
 int
 got_object_get_type(struct got_object *obj)
 {