git-blame: Make the output human readable
The default output mode is slightly different from git-annotate's. However, git-annotate's output mode can be obtained by using the '-c' flag. Signed-off-by: Fredrik Kuivinen <freku045@student.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>
Fredrik Kuivinen committed
Mar 5, 2006 at 12:03 UTC
ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7
2 files changed
+148
-18
Makefile
+4
@@ -534,6 +534,10 @@ git-rev-list$X: rev-list.o $(LIB_FILE)
534
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
535
$(LIBS) $(OPENSSL_LIBSSL)
536
537
+git-blame$X: blame.o $(LIB_FILE)
538
+ $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
539
+ $(LIBS) -lm
540
+
541
init-db.o: init-db.c
542
$(CC) -c $(ALL_CFLAGS) \
543
-DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $*.c
blame.c
+144
-18
@@ -5,6 +5,7 @@
5
#include <assert.h>
6
#include <time.h>
7
#include <sys/time.h>
8
+#include <math.h>
9
10
#include "cache.h"
11
#include "refs.h"
@@ -17,8 +18,15 @@
18
19
#define DEBUG 0
20
20
-struct commit **blame_lines;
21
-int num_blame_lines;
21
+static const char blame_usage[] = "[-c] [-l] [--] file [commit]\n"
22
+ " -c, --compability Use the same output mode as git-annotate (Default: off)\n"
23
+ " -l, --long Show long commit SHA1 (Default: off)\n"
24
+ " -h, --help This message";
25
+
26
+static struct commit **blame_lines;
27
+static int num_blame_lines;
28
+static char* blame_contents;
29
+static int blame_len;
30
31
struct util_info {
32
int *line_map;
@@ -390,9 +398,8 @@ static void init_first_commit(struct commit* commit, const char* filename)
398
alloc_line_map(commit);
399
400
util = commit->object.util;
393
- num_blame_lines = util->num_lines;
401
395
- for (i = 0; i < num_blame_lines; i++)
402
+ for (i = 0; i < util->num_lines; i++)
403
util->line_map[i] = i;
404
}
405
@@ -414,6 +421,9 @@ static void process_commits(struct rev_info *rev, const char *path,
421
util = commit->object.util;
422
num_blame_lines = util->num_lines;
423
blame_lines = xmalloc(sizeof(struct commit *) * num_blame_lines);
424
+ blame_contents = util->buf;
425
+ blame_len = util->size;
426
+
427
for (i = 0; i < num_blame_lines; i++)
428
blame_lines[i] = NULL;
429
@@ -501,32 +511,128 @@ static void process_commits(struct rev_info *rev, const char *path,
511
} while ((commit = get_revision(rev)) != NULL);
512
}
513
514
+struct commit_info
515
+{
516
+ char* author;
517
+ char* author_mail;
518
+ unsigned long author_time;
519
+ char* author_tz;
520
+};
521
+
522
+static void get_commit_info(struct commit* commit, struct commit_info* ret)
523
+{
524
+ int len;
525
+ char* tmp;
526
+ static char author_buf[1024];
527
+
528
+ tmp = strstr(commit->buffer, "\nauthor ") + 8;
529
+ len = index(tmp, '\n') - tmp;
530
+ ret->author = author_buf;
531
+ memcpy(ret->author, tmp, len);
532
+
533
+ tmp = ret->author;
534
+ tmp += len;
535
+ *tmp = 0;
536
+ while(*tmp != ' ')
537
+ tmp--;
538
+ ret->author_tz = tmp+1;
539
+
540
+ *tmp = 0;
541
+ while(*tmp != ' ')
542
+ tmp--;
543
+ ret->author_time = strtoul(tmp, NULL, 10);
544
+
545
+ *tmp = 0;
546
+ while(*tmp != ' ')
547
+ tmp--;
548
+ ret->author_mail = tmp + 1;
549
+
550
+ *tmp = 0;
551
+}
552
+
553
+char* format_time(unsigned long time, const char* tz)
554
+{
555
+ static char time_buf[128];
556
+ time_t t = time;
557
+
558
+ strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", gmtime(&t));
559
+ strcat(time_buf, tz);
560
+ return time_buf;
561
+}
562
+
563
int main(int argc, const char **argv)
564
{
565
int i;
566
struct commit *initial = NULL;
567
unsigned char sha1[20];
509
- const char* filename;
568
+
569
+ const char *filename = NULL, *commit = NULL;
570
+ char filename_buf[256];
571
+ int sha1_len = 8;
572
+ int compability = 0;
573
+ int options = 1;
574
+
575
int num_args;
576
const char* args[10];
577
struct rev_info rev;
578
514
- setup_git_directory();
579
+ struct commit_info ci;
580
+ const char *buf;
581
+ int max_digits;
582
516
- if (argc != 3)
517
- die("Usage: blame commit-ish file");
583
+ const char* prefix = setup_git_directory();
584
585
+ for(i = 1; i < argc; i++) {
586
+ if(options) {
587
+ if(!strcmp(argv[i], "-h") ||
588
+ !strcmp(argv[i], "--help"))
589
+ usage(blame_usage);
590
+ else if(!strcmp(argv[i], "-l") ||
591
+ !strcmp(argv[i], "--long")) {
592
+ sha1_len = 20;
593
+ continue;
594
+ } else if(!strcmp(argv[i], "-c") ||
595
+ !strcmp(argv[i], "--compability")) {
596
+ compability = 1;
597
+ continue;
598
+ } else if(!strcmp(argv[i], "--")) {
599
+ options = 0;
600
+ continue;
601
+ } else if(argv[i][0] == '-')
602
+ usage(blame_usage);
603
+ else
604
+ options = 0;
605
+ }
606
520
- filename = argv[2];
607
+ if(!options) {
608
+ if(!filename)
609
+ filename = argv[i];
610
+ else if(!commit)
611
+ commit = argv[i];
612
+ else
613
+ usage(blame_usage);
614
+ }
615
+ }
616
+
617
+ if(!filename)
618
+ usage(blame_usage);
619
+ if(!commit)
620
+ commit = "HEAD";
621
+
622
+ if(prefix)
623
+ sprintf(filename_buf, "%s%s", prefix, filename);
624
+ else
625
+ strcpy(filename_buf, filename);
626
+ filename = filename_buf;
627
628
{
523
- struct commit* commit;
524
- if (get_sha1(argv[1], sha1))
525
- die("get_sha1 failed");
526
- commit = lookup_commit_reference(sha1);
629
+ struct commit* c;
630
+ if (get_sha1(commit, sha1))
631
+ die("get_sha1 failed, commit '%s' not found", commit);
632
+ c = lookup_commit_reference(sha1);
633
528
- if (fill_util_info(commit, filename)) {
529
- printf("%s not found in %s\n", filename, argv[1]);
634
+ if (fill_util_info(c, filename)) {
635
+ printf("%s not found in %s\n", filename, commit);
636
return 1;
637
}
638
}
@@ -535,7 +641,7 @@ int main(int argc, const char **argv)
641
args[num_args++] = NULL;
642
args[num_args++] = "--topo-order";
643
args[num_args++] = "--remove-empty";
538
- args[num_args++] = argv[1];
644
+ args[num_args++] = commit;
645
args[num_args++] = "--";
646
args[num_args++] = filename;
647
args[num_args] = NULL;
@@ -544,13 +650,33 @@ int main(int argc, const char **argv)
650
prepare_revision_walk(&rev);
651
process_commits(&rev, filename, &initial);
652
653
+ buf = blame_contents;
654
+ max_digits = 1 + log(num_blame_lines+1)/log(10);
655
for (i = 0; i < num_blame_lines; i++) {
656
struct commit *c = blame_lines[i];
657
if (!c)
658
c = initial;
659
552
- printf("%d %.8s\n", i, sha1_to_hex(c->object.sha1));
553
-// printf("%d %s\n", i, find_unique_abbrev(blame_lines[i]->object.sha1, 6));
660
+ get_commit_info(c, &ci);
661
+ fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
662
+ if(compability)
663
+ printf("\t(%10s\t%10s\t%d)", ci.author,
664
+ format_time(ci.author_time, ci.author_tz), i+1);
665
+ else
666
+ printf(" (%-15.15s %10s %*d) ", ci.author,
667
+ format_time(ci.author_time, ci.author_tz),
668
+ max_digits, i+1);
669
+
670
+ if(i == num_blame_lines - 1) {
671
+ fwrite(buf, blame_len - (buf - blame_contents),
672
+ 1, stdout);
673
+ if(blame_contents[blame_len-1] != '\n')
674
+ putc('\n', stdout);
675
+ } else {
676
+ char* next_buf = index(buf, '\n') + 1;
677
+ fwrite(buf, next_buf - buf, 1, stdout);
678
+ buf = next_buf;
679
+ }
680
}
681
682
if (DEBUG) {