Skip to content

Commit 7ae0b0c

Browse files
author
Junio C Hamano
committed
git-log (internal): more options.
This ports the following options from rev-list based git-log implementation: * -<n>, -n<n>, and -n <n>. I am still wondering if we want this natively supported by setup_revisions(), which already takes --max-count. We may want to move them in the next round. Also I am not sure if we can get away with not setting revs->limited when we set max-count. The latest rev-list.c and revision.c in this series do not, so I left them as they are. * --pretty and --pretty=<fmt>. * --abbrev=<n> and --no-abbrev. The previous commit already handles time-based limiters (--since, --until and friends). The remaining things that rev-list based git-log happens to do are not useful in a pure log-viewing purposes, and not ported: * --bisect (obviously). * --header. I am actually in favor of doing the NUL terminated record format, but rev-list based one always passed --pretty, which defeated this option. Maybe next round. * --parents. I do not think of a reason a log viewer wants this. The flag is primarily for feeding squashed history via pipe to downstream tools. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd75166 commit 7ae0b0c

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

git.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,80 @@ static int cmd_log(int argc, char **argv, char **envp)
256256
struct rev_info rev;
257257
struct commit *commit;
258258
char *buf = xmalloc(LOGSIZE);
259+
static enum cmit_fmt commit_format = CMIT_FMT_DEFAULT;
260+
int abbrev = DEFAULT_ABBREV;
261+
int show_parents = 0;
262+
const char *commit_prefix = "commit ";
259263

260264
argc = setup_revisions(argc, argv, &rev, "HEAD");
265+
while (1 < argc) {
266+
char *arg = argv[1];
267+
/* accept -<digit>, like traditilnal "head" */
268+
if ((*arg == '-') && isdigit(arg[1])) {
269+
rev.max_count = atoi(arg + 1);
270+
}
271+
else if (!strcmp(arg, "-n")) {
272+
if (argc < 2)
273+
die("-n requires an argument");
274+
rev.max_count = atoi(argv[2]);
275+
argc--; argv++;
276+
}
277+
else if (!strncmp(arg,"-n",2)) {
278+
rev.max_count = atoi(arg + 2);
279+
}
280+
else if (!strncmp(arg, "--pretty", 8)) {
281+
commit_format = get_commit_format(arg + 8);
282+
if (commit_format == CMIT_FMT_ONELINE)
283+
commit_prefix = "";
284+
}
285+
else if (!strcmp(arg, "--parents")) {
286+
show_parents = 1;
287+
}
288+
else if (!strcmp(arg, "--no-abbrev")) {
289+
abbrev = 0;
290+
}
291+
else if (!strncmp(arg, "--abbrev=", 9)) {
292+
abbrev = strtoul(arg + 9, NULL, 10);
293+
if (abbrev && abbrev < MINIMUM_ABBREV)
294+
abbrev = MINIMUM_ABBREV;
295+
else if (40 < abbrev)
296+
abbrev = 40;
297+
}
298+
else
299+
die("unrecognized argument: %s", arg);
300+
argc--; argv++;
301+
}
302+
261303
prepare_revision_walk(&rev);
262304
setup_pager();
263305
while ((commit = get_revision(&rev)) != NULL) {
264-
pretty_print_commit(CMIT_FMT_DEFAULT, commit, ~0, buf, LOGSIZE, 18);
306+
printf("%s%s", commit_prefix,
307+
sha1_to_hex(commit->object.sha1));
308+
if (show_parents) {
309+
struct commit_list *parents = commit->parents;
310+
while (parents) {
311+
struct object *o = &(parents->item->object);
312+
parents = parents->next;
313+
if (o->flags & TMP_MARK)
314+
continue;
315+
printf(" %s", sha1_to_hex(o->sha1));
316+
o->flags |= TMP_MARK;
317+
}
318+
/* TMP_MARK is a general purpose flag that can
319+
* be used locally, but the user should clean
320+
* things up after it is done with them.
321+
*/
322+
for (parents = commit->parents;
323+
parents;
324+
parents = parents->next)
325+
parents->item->object.flags &= ~TMP_MARK;
326+
}
327+
if (commit_format == CMIT_FMT_ONELINE)
328+
putchar(' ');
329+
else
330+
putchar('\n');
331+
pretty_print_commit(commit_format, commit, ~0, buf,
332+
LOGSIZE, abbrev);
265333
printf("%s\n", buf);
266334
}
267335
free(buf);

rev-list.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
#include "diff.h"
88
#include "revision.h"
99

10-
/* bits #0-3 in revision.h */
10+
/* bits #0-4 in revision.h */
1111

12-
#define COUNTED (1u << 4)
13-
#define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
12+
#define COUNTED (1u<<5)
1413

1514
static const char rev_list_usage[] =
1615
"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"

revision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define UNINTERESTING (1u<<1)
66
#define TREECHANGE (1u<<2)
77
#define SHOWN (1u<<3)
8+
#define TMP_MARK (1u<<4) /* for isolated cases; clean after use */
89

910
struct rev_info {
1011
/* Starting list */

0 commit comments

Comments
 (0)