- unsigned char sha1[20];
- struct commit *commit;
- const char* filename;
- int i;
-
- setup_git_directory();
-
- if (argc != 3)
- die("Usage: blame commit-ish file");
-
- if (get_sha1(argv[1], sha1))
- die("get_sha1 failed");
-
- commit = lookup_commit_reference(sha1);
-
- filename = argv[2];
-
- struct commit_list* list = get_commit_list(commit, filename);
- sort_in_topological_order(&list, 1);
-
- if(fill_util_info(commit, filename)) {
- printf("%s not found in %s\n", filename, argv[1]);
- return 0;
- }
- alloc_line_map(commit);
-
- struct util_info* util = commit->object.util;
- num_blame_lines = util->num_lines;
- blame_lines = xmalloc(sizeof(struct commit*)*num_blame_lines);
-
-
- for(i = 0; i < num_blame_lines; i++) {
- blame_lines[i] = commit;
-
- ((struct util_info*) commit->object.util)->line_map[i] = i;
- }
-
- process_commits(list, filename);
-
- for(i = 0; i < num_blame_lines; i++) {
- printf("%d %s\n", i+1-1, sha1_to_hex(blame_lines[i]->object.sha1));
-// printf("%d %s\n", i+1-1, find_unique_abbrev(blame_lines[i]->object.sha1, 6));
- }
-
- if(DEBUG) {
- printf("num get patch: %d\n", num_get_patch);
- printf("num commits: %d\n", num_commits);
- }
-
- return 0;
+ int i;
+ struct commit *initial = NULL;
+ unsigned char sha1[20];
+
+ const char *filename = NULL, *commit = NULL;
+ char filename_buf[256];
+ int sha1_len = 8;
+ int compability = 0;
+ int options = 1;
+ struct commit* start_commit;
+
+ const char* args[10];
+ struct rev_info rev;
+
+ struct commit_info ci;
+ const char *buf;
+ int max_digits;
+ int longest_file, longest_author;
+ int found_rename;
+
+ const char* prefix = setup_git_directory();
+
+ for(i = 1; i < argc; i++) {
+ if(options) {
+ if(!strcmp(argv[i], "-h") ||
+ !strcmp(argv[i], "--help"))
+ usage(blame_usage);
+ else if(!strcmp(argv[i], "-l") ||
+ !strcmp(argv[i], "--long")) {
+ sha1_len = 40;
+ continue;
+ } else if(!strcmp(argv[i], "-c") ||
+ !strcmp(argv[i], "--compability")) {
+ compability = 1;
+ continue;
+ } else if(!strcmp(argv[i], "--")) {
+ options = 0;
+ continue;
+ } else if(argv[i][0] == '-')
+ usage(blame_usage);
+ else
+ options = 0;
+ }
+
+ if(!options) {
+ if(!filename)
+ filename = argv[i];
+ else if(!commit)
+ commit = argv[i];
+ else
+ usage(blame_usage);
+ }
+ }
+
+ if(!filename)
+ usage(blame_usage);
+ if(!commit)
+ commit = "HEAD";
+
+ if(prefix)
+ sprintf(filename_buf, "%s%s", prefix, filename);
+ else
+ strcpy(filename_buf, filename);
+ filename = filename_buf;
+
+ if (get_sha1(commit, sha1))
+ die("get_sha1 failed, commit '%s' not found", commit);
+ start_commit = lookup_commit_reference(sha1);
+ get_util(start_commit)->pathname = filename;
+ if (fill_util_info(start_commit)) {
+ printf("%s not found in %s\n", filename, commit);
+ return 1;
+ }
+
+
+ init_revisions(&rev);
+ rev.remove_empty_trees = 1;
+ rev.topo_order = 1;
+ rev.prune_fn = simplify_commit;
+ rev.topo_setter = topo_setter;
+ rev.topo_getter = topo_getter;
+ rev.limited = 1;
+
+ commit_list_insert(start_commit, &rev.commits);
+
+ args[0] = filename;
+ args[1] = NULL;
+ diff_tree_setup_paths(args);
+ prepare_revision_walk(&rev);
+ process_commits(&rev, filename, &initial);
+
+ buf = blame_contents;
+ for (max_digits = 1, i = 10; i <= num_blame_lines + 1; max_digits++)
+ i *= 10;
+
+ longest_file = 0;
+ longest_author = 0;
+ found_rename = 0;
+ for (i = 0; i < num_blame_lines; i++) {
+ struct commit *c = blame_lines[i];
+ struct util_info* u;
+ if (!c)
+ c = initial;
+ u = c->object.util;
+
+ if (!found_rename && strcmp(filename, u->pathname))
+ found_rename = 1;
+ if (longest_file < strlen(u->pathname))
+ longest_file = strlen(u->pathname);
+ get_commit_info(c, &ci);
+ if (longest_author < strlen(ci.author))
+ longest_author = strlen(ci.author);
+ }
+
+ for (i = 0; i < num_blame_lines; i++) {
+ struct commit *c = blame_lines[i];
+ struct util_info* u;
+
+ if (!c)
+ c = initial;
+
+ u = c->object.util;
+ get_commit_info(c, &ci);
+ fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
+ if(compability) {
+ printf("\t(%10s\t%10s\t%d)", ci.author,
+ format_time(ci.author_time, ci.author_tz), i+1);
+ } else {
+ if (found_rename)
+ printf(" %-*.*s", longest_file, longest_file,
+ u->pathname);
+ printf(" (%-*.*s %10s %*d) ",
+ longest_author, longest_author, ci.author,
+ format_time(ci.author_time, ci.author_tz),
+ max_digits, i+1);
+ }
+
+ if(i == num_blame_lines - 1) {
+ fwrite(buf, blame_len - (buf - blame_contents),
+ 1, stdout);
+ if(blame_contents[blame_len-1] != '\n')
+ putc('\n', stdout);
+ } else {
+ char* next_buf = index(buf, '\n') + 1;
+ fwrite(buf, next_buf - buf, 1, stdout);
+ buf = next_buf;
+ }
+ }
+
+ if (DEBUG) {
+ printf("num get patch: %d\n", num_get_patch);
+ printf("num commits: %d\n", num_commits);
+ printf("patch time: %f\n", patch_time / 1000000.0);
+ printf("initial: %s\n", sha1_to_hex(initial->object.sha1));
+ }
+
+ return 0;