@matheusbsbs / matheus-bagliano / commits / 717d1462ba
Fork 0

git-blame --incremental

This adds --incremental option to help GUI porcelains to show the result from git-blame incrementally. The output gives the origin information in the same format as the porcelain format. The first line has commit object name, the line number of the first line in the group in the original file, the line number of that file in the final image, and number of lines in the group. Then subsequent lines show the metainformation for the commit when the commit is shown for the first time, except the filename information is always shown (we cannot even make it conditional to -C option as blame always follows the renaming of the file wholesale). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Jan 28, 2007 at 01:34 UTC 717d1462ba0ebc430775eccaae65eb9b8234eb32
1 file changed +107 -66
builtin-blame.c
+107 -66
@@ -13,6 +13,7 @@
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "revision.h"
16 +#include "quote.h"
17 #include "xdiff-interface.h"
18
19 static char blame_usage[] =
@@ -27,6 +28,7 @@ static char blame_usage[] =
28 " -p, --porcelain Show in a format designed for machine consumption\n"
29 " -L n,m Process only line range n,m, counting from 1\n"
30 " -M, -C Find line movements within and across files\n"
31 +" --incremental Show blame entries as we find them, incrementally\n"
32 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
33
34 static int longest_file;
@@ -36,6 +38,7 @@ static int max_digits;
38 static int max_score_digits;
39 static int show_root;
40 static int blank_boundary;
41 +static int incremental;
42
43 #ifndef DEBUG
44 #define DEBUG 0
@@ -1069,72 +1072,6 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1072 origin_decref(parent_origin[i]);
1073 }
1074
1072 -static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1073 -{
1074 - while (1) {
1075 - struct blame_entry *ent;
1076 - struct commit *commit;
1077 - struct origin *suspect = NULL;
1078 -
1079 - /* find one suspect to break down */
1080 - for (ent = sb->ent; !suspect && ent; ent = ent->next)
1081 - if (!ent->guilty)
1082 - suspect = ent->suspect;
1083 - if (!suspect)
1084 - return; /* all done */
1085 -
1086 - origin_incref(suspect);
1087 - commit = suspect->commit;
1088 - if (!commit->object.parsed)
1089 - parse_commit(commit);
1090 - if (!(commit->object.flags & UNINTERESTING) &&
1091 - !(revs->max_age != -1 && commit->date < revs->max_age))
1092 - pass_blame(sb, suspect, opt);
1093 - else {
1094 - commit->object.flags |= UNINTERESTING;
1095 - if (commit->object.parsed)
1096 - mark_parents_uninteresting(commit);
1097 - }
1098 - /* treat root commit as boundary */
1099 - if (!commit->parents && !show_root)
1100 - commit->object.flags |= UNINTERESTING;
1101 -
1102 - /* Take responsibility for the remaining entries */
1103 - for (ent = sb->ent; ent; ent = ent->next)
1104 - if (!cmp_suspect(ent->suspect, suspect))
1105 - ent->guilty = 1;
1106 - origin_decref(suspect);
1107 -
1108 - if (DEBUG) /* sanity */
1109 - sanity_check_refcnt(sb);
1110 - }
1111 -}
1112 -
1113 -static const char *format_time(unsigned long time, const char *tz_str,
1114 - int show_raw_time)
1115 -{
1116 - static char time_buf[128];
1117 - time_t t = time;
1118 - int minutes, tz;
1119 - struct tm *tm;
1120 -
1121 - if (show_raw_time) {
1122 - sprintf(time_buf, "%lu %s", time, tz_str);
1123 - return time_buf;
1124 - }
1125 -
1126 - tz = atoi(tz_str);
1127 - minutes = tz < 0 ? -tz : tz;
1128 - minutes = (minutes / 100)*60 + (minutes % 100);
1129 - minutes = tz < 0 ? -minutes : minutes;
1130 - t = time + minutes * 60;
1131 - tm = gmtime(&t);
1132 -
1133 - strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1134 - strcat(time_buf, tz_str);
1135 - return time_buf;
1136 -}
1137 -
1075 struct commit_info
1076 {
1077 char *author;
@@ -1245,6 +1182,105 @@ static void get_commit_info(struct commit *commit,
1182 summary_buf[len] = 0;
1183 }
1184
1185 +static void found_guilty_entry(struct blame_entry *ent)
1186 +{
1187 + if (ent->guilty)
1188 + return;
1189 + ent->guilty = 1;
1190 + if (incremental) {
1191 + struct origin *suspect = ent->suspect;
1192 +
1193 + printf("%s %d %d %d\n",
1194 + sha1_to_hex(suspect->commit->object.sha1),
1195 + ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1196 + if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1197 + struct commit_info ci;
1198 + suspect->commit->object.flags |= METAINFO_SHOWN;
1199 + get_commit_info(suspect->commit, &ci, 1);
1200 + printf("author %s\n", ci.author);
1201 + printf("author-mail %s\n", ci.author_mail);
1202 + printf("author-time %lu\n", ci.author_time);
1203 + printf("author-tz %s\n", ci.author_tz);
1204 + printf("committer %s\n", ci.committer);
1205 + printf("committer-mail %s\n", ci.committer_mail);
1206 + printf("committer-time %lu\n", ci.committer_time);
1207 + printf("committer-tz %s\n", ci.committer_tz);
1208 + printf("summary %s\n", ci.summary);
1209 + if (suspect->commit->object.flags & UNINTERESTING)
1210 + printf("boundary\n");
1211 + }
1212 + printf("filename ");
1213 + write_name_quoted(NULL, 0, suspect->path, 1, stdout);
1214 + putchar('\n');
1215 + }
1216 +}
1217 +
1218 +static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1219 +{
1220 + while (1) {
1221 + struct blame_entry *ent;
1222 + struct commit *commit;
1223 + struct origin *suspect = NULL;
1224 +
1225 + /* find one suspect to break down */
1226 + for (ent = sb->ent; !suspect && ent; ent = ent->next)
1227 + if (!ent->guilty)
1228 + suspect = ent->suspect;
1229 + if (!suspect)
1230 + return; /* all done */
1231 +
1232 + origin_incref(suspect);
1233 + commit = suspect->commit;
1234 + if (!commit->object.parsed)
1235 + parse_commit(commit);
1236 + if (!(commit->object.flags & UNINTERESTING) &&
1237 + !(revs->max_age != -1 && commit->date < revs->max_age))
1238 + pass_blame(sb, suspect, opt);
1239 + else {
1240 + commit->object.flags |= UNINTERESTING;
1241 + if (commit->object.parsed)
1242 + mark_parents_uninteresting(commit);
1243 + }
1244 + /* treat root commit as boundary */
1245 + if (!commit->parents && !show_root)
1246 + commit->object.flags |= UNINTERESTING;
1247 +
1248 + /* Take responsibility for the remaining entries */
1249 + for (ent = sb->ent; ent; ent = ent->next)
1250 + if (!cmp_suspect(ent->suspect, suspect))
1251 + found_guilty_entry(ent);
1252 + origin_decref(suspect);
1253 +
1254 + if (DEBUG) /* sanity */
1255 + sanity_check_refcnt(sb);
1256 + }
1257 +}
1258 +
1259 +static const char *format_time(unsigned long time, const char *tz_str,
1260 + int show_raw_time)
1261 +{
1262 + static char time_buf[128];
1263 + time_t t = time;
1264 + int minutes, tz;
1265 + struct tm *tm;
1266 +
1267 + if (show_raw_time) {
1268 + sprintf(time_buf, "%lu %s", time, tz_str);
1269 + return time_buf;
1270 + }
1271 +
1272 + tz = atoi(tz_str);
1273 + minutes = tz < 0 ? -tz : tz;
1274 + minutes = (minutes / 100)*60 + (minutes % 100);
1275 + minutes = tz < 0 ? -minutes : minutes;
1276 + t = time + minutes * 60;
1277 + tm = gmtime(&t);
1278 +
1279 + strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1280 + strcat(time_buf, tz_str);
1281 + return time_buf;
1282 +}
1283 +
1284 #define OUTPUT_ANNOTATE_COMPAT 001
1285 #define OUTPUT_LONG_OBJECT_NAME 002
1286 #define OUTPUT_RAW_TIMESTAMP 004
@@ -1717,6 +1753,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
1753 die("More than one '-L n,m' option given");
1754 bottomtop = arg;
1755 }
1756 + else if (!strcmp("--incremental", arg))
1757 + incremental = 1;
1758 else if (!strcmp("--score-debug", arg))
1759 output_option |= OUTPUT_SHOW_SCORE;
1760 else if (!strcmp("-f", arg) ||
@@ -1907,6 +1945,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
1945
1946 assign_blame(&sb, &revs, opt);
1947
1948 + if (incremental)
1949 + return 0;
1950 +
1951 coalesce(&sb);
1952
1953 if (!(output_option & OUTPUT_PORCELAIN))