commit 3235492e3fcec3882427de904e5be159b6380edf from: Stefan Sperling date: Sun Apr 01 16:17:18 2018 UTC add the ability to start the log at a particular commit commit - 2acfca77da819090c3caac77635cb2c2bc4ebe14 commit + 3235492e3fcec3882427de904e5be159b6380edf blob - fb1724c0ddbc3fe5a9dd001ccb99e8ff4acc3093 blob + a55e7d9e6f0317755b1aa6df1c5f7673ad9a6e0c --- got/got.c +++ got/got.c @@ -400,7 +400,7 @@ print_commits(struct got_object *root_obj, struct got_ __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; } blob - 93541c410d6bcbd0f98b87d10418b2f3138911f4 blob + 2b3af389c809b5bfa2e2d23457718e1de4f0b696 --- include/got_object.h +++ 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 *); blob - 7f38f5a672366753baf1915c00cc6a8d37a515e4 blob + 47ed4b5ba38c568f77882ab7bc2cabc23917edc3 --- lib/object.c +++ lib/object.c @@ -87,6 +87,12 @@ got_object_id_dup(struct got_object_id *id1) return NULL; memcpy(id2, id1, sizeof(*id2)); return id2; +} + +struct got_object_id * +got_object_get_id(struct got_object *obj) +{ + return got_object_id_dup(&obj->id); } int