implement got log -l
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.1 b/got/got.1
index e776aa4..971ecdd 100644
--- a/got/got.1
+++ b/got/got.1
@@ -75,16 +75,21 @@ In this case, only files beneath the specified directory prefix will
be checked out.
.\".It Cm status
.\"Show current status of files.
-.It Cm log [ Fl p ] [ Fl c Ar commit ] [ Ar repository-path ]
+.It Cm log [ Fl p ] [ Fl c Ar commit ] [ Fl l Ar N ] [ Ar repository-path ]
Display history of the repository.
If the
.Fl p
-flag is given, display the patch of modifications made in each commit.
+option is given, display the patch of modifications made in each commit.
If a
.Ar commit
is specified with the
.Fl c
option, start traversing history at this commit.
+The
+.Fl l
+option limits the output to a number of
+.Ar N
+commits.
If the
.Ar repository path
is omitted, use the current working directory.
diff --git a/got/got.c b/got/got.c
index ba1a257..dfaf97f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -16,6 +16,7 @@
*/
#include <sys/queue.h>
+#include <sys/limits.h>
#include <err.h>
#include <errno.h>
@@ -317,7 +318,7 @@ struct commit_queue_entry {
static const struct got_error *
print_commits(struct got_object *root_obj, struct got_object_id *root_id,
- struct got_repository *repo, int show_patch)
+ struct got_repository *repo, int show_patch, int limit)
{
const struct got_error *err;
struct got_commit_object *root_commit;
@@ -358,6 +359,9 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
break;
}
+ if (limit && --limit == 0)
+ break;
+
SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
struct got_object *obj;
struct got_commit_object *pcommit;
@@ -404,8 +408,8 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
__dead void
usage_log(void)
{
- fprintf(stderr, "usage: %s log [-p] [-c commit] [repository-path]\n",
- getprogname());
+ fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
+ "[repository-path]\n", getprogname());
exit(1);
}
@@ -419,14 +423,15 @@ cmd_log(int argc, char *argv[])
char *repo_path = NULL;
char *start_commit = NULL;
int ch;
- int show_patch = 0;
+ int show_patch = 0, limit = 0;
+ const char *errstr;
#ifndef PROFILE
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
#endif
- while ((ch = getopt(argc, argv, "pc:")) != -1) {
+ while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
switch (ch) {
case 'p':
show_patch = 1;
@@ -434,6 +439,11 @@ cmd_log(int argc, char *argv[])
case 'c':
start_commit = optarg;
break;
+ case 'l':
+ limit = strtonum(optarg, 1, INT_MAX, &errstr);
+ if (errstr != NULL)
+ err(1, "-l option %s", errstr);
+ break;
default:
usage();
/* NOTREACHED */
@@ -477,7 +487,7 @@ cmd_log(int argc, char *argv[])
if (error != NULL)
return error;
if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
- error = print_commits(obj, id, repo, show_patch);
+ error = print_commits(obj, id, repo, show_patch, limit);
else
error = got_error(GOT_ERR_OBJ_TYPE);
got_object_close(obj);