@matheusbsbs / matheus-bagliano / commits / 904b194151
Fork 0

Include recent command history in fast-import crash reports

When we crash the frontend developer (or end-user) may need to know roughly around what part of the input stream we had a problem with and aborted on. Because line numbers aren't very useful in this sort of application we instead just keep the last 100 commands in a FIFO queue and print them as part of the crash report. Currently one problem with this design is a commit that has more than 100 modified files in it will flood the FIFO and any context regarding branch/from/committer/mark/comments will be lost. We really should save only the last few (10?) file changes for the current commit, ensuring we have some prior higher level commands in the FIFO when we crash on a file M/D/C/R command. Another issue with this approach is the FIFO only includes the commands, it does not include the commit messages. Yet having a commit message may be useful to help locate the relevant change in the source material. In practice I don't think this is going to be a major concern as the frontend can always embed its own source change set identifier as a comment (which will appear in the crash report) and the commit message(s) for the most recent commits of any given branch should be obtainable from the (packed) commit objects. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 3, 2007 at 04:47 UTC 904b1941514e78543bd636b4db71fbcf57898388
1 file changed +58 -4
fast-import.c
+58 -4
@@ -269,6 +269,13 @@ typedef enum {
269 WHENSPEC_NOW,
270 } whenspec_type;
271
272 +struct recent_command
273 +{
274 + struct recent_command *prev;
275 + struct recent_command *next;
276 + char *buf;
277 +};
278 +
279 /* Configured limits on output */
280 static unsigned long max_depth = 10;
281 static off_t max_packsize = (1LL << 32) - 1;
@@ -335,6 +342,10 @@ static struct tag *last_tag;
342 static whenspec_type whenspec = WHENSPEC_RAW;
343 static struct strbuf command_buf;
344 static int unread_command_buf;
345 +static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
346 +static struct recent_command *cmd_tail = &cmd_hist;
347 +static struct recent_command *rc_free;
348 +static unsigned int cmd_save = 100;
349 static uintmax_t next_mark;
350 static struct dbuf new_data;
351
@@ -370,6 +381,7 @@ static void write_crash_report(const char *err, va_list params)
381 FILE *rpt = fopen(loc, "w");
382 struct branch *b;
383 unsigned long lu;
384 + struct recent_command *rc;
385
386 if (!rpt) {
387 error("can't write crash report %s: %s", loc, strerror(errno));
@@ -388,6 +400,18 @@ static void write_crash_report(const char *err, va_list params)
400 vfprintf(rpt, err, params);
401 fputc('\n', rpt);
402
403 + fputc('\n', rpt);
404 + fputs("Most Recent Commands Before Crash\n", rpt);
405 + fputs("---------------------------------\n", rpt);
406 + for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
407 + if (rc->next == &cmd_hist)
408 + fputs("* ", rpt);
409 + else
410 + fputs(" ", rpt);
411 + fputs(rc->buf, rpt);
412 + fputc('\n', rpt);
413 + }
414 +
415 fputc('\n', rpt);
416 fputs("Active Branch LRU\n", rpt);
417 fputs("-----------------\n", rpt);
@@ -1563,11 +1587,35 @@ static void dump_marks(void)
1587 static void read_next_command(void)
1588 {
1589 do {
1566 - if (unread_command_buf)
1590 + if (unread_command_buf) {
1591 unread_command_buf = 0;
1568 - else
1592 + if (command_buf.eof)
1593 + return;
1594 + } else {
1595 + struct recent_command *rc;
1596 +
1597 + command_buf.buf = NULL;
1598 read_line(&command_buf, stdin, '\n');
1570 - } while (!command_buf.eof && command_buf.buf[0] == '#');
1599 + if (command_buf.eof)
1600 + return;
1601 +
1602 + rc = rc_free;
1603 + if (rc)
1604 + rc_free = rc->next;
1605 + else {
1606 + rc = cmd_hist.next;
1607 + cmd_hist.next = rc->next;
1608 + cmd_hist.next->prev = &cmd_hist;
1609 + free(rc->buf);
1610 + }
1611 +
1612 + rc->buf = command_buf.buf;
1613 + rc->prev = cmd_tail;
1614 + rc->next = cmd_hist.prev;
1615 + rc->prev->next = rc;
1616 + cmd_tail = rc;
1617 + }
1618 + } while (command_buf.buf[0] == '#');
1619 }
1620
1621 static void skip_optional_lf()
@@ -1600,6 +1648,7 @@ static void *cmd_data (size_t *size)
1648 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1649 length = 0;
1650 buffer = xmalloc(sz);
1651 + command_buf.buf = NULL;
1652 for (;;) {
1653 read_line(&command_buf, stdin, '\n');
1654 if (command_buf.eof)
@@ -2269,7 +2318,7 @@ static const char fast_import_usage[] =
2318
2319 int main(int argc, const char **argv)
2320 {
2272 - int i, show_stats = 1;
2321 + unsigned int i, show_stats = 1;
2322
2323 git_config(git_default_config);
2324 alloc_objects(object_entry_alloc);
@@ -2323,6 +2372,11 @@ int main(int argc, const char **argv)
2372 if (i != argc)
2373 usage(fast_import_usage);
2374
2375 + rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2376 + for (i = 0; i < (cmd_save - 1); i++)
2377 + rc_free[i].next = &rc_free[i + 1];
2378 + rc_free[cmd_save - 1].next = NULL;
2379 +
2380 prepare_packed_git();
2381 start_packfile();
2382 set_die_routine(die_nicely);