add the ability to start the log at a particular commit
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
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)
{