format-patch: improve head calculation for cover-letter

If we do it after the revision traversal we can be sure that this is indeed a commit that will be processed (i.e. not a merge) and it's the top most one (thus removing the NEEDSWORK comment, at least we show the same as 'git diff --stat' output that appears in the cover-letter). While we are at it, since we know there's nothing to generate, exit sooner in all cases, like --cover-letter currently does. Also, if there's nothing to generate and cover-letter is specified, a different code-path might be triggered that is not currently covered in the test-case, so add a test for it. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Apr 7, 2013 at 12:46 UTC 80d35ca0aaeca654562974be6ea528e22bc76e8c
2 files changed +9 -18
builtin/log.c
+4 -18
@@ -1311,24 +1311,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1311 rev.show_root_diff = 1;
1312
1313 if (cover_letter) {
1314 - /*
1315 - * NEEDSWORK:randomly pick one positive commit to show
1316 - * diffstat; this is often the tip and the command
1317 - * happens to do the right thing in most cases, but a
1318 - * complex command like "--cover-letter a b c ^bottom"
1319 - * picks "c" and shows diffstat between bottom..c
1320 - * which may not match what the series represents at
1321 - * all and totally broken.
1322 - */
1323 - int i;
1324 - for (i = 0; i < rev.pending.nr; i++) {
1325 - struct object *o = rev.pending.objects[i].item;
1326 - if (!(o->flags & UNINTERESTING))
1327 - head = (struct commit *)o;
1328 - }
1329 - /* There is nothing to show; it is not an error, though. */
1330 - if (!head)
1331 - return 0;
1314 if (!branch_name)
1315 branch_name = find_branch_name(&rev);
1316 }
@@ -1364,6 +1346,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1346 list = xrealloc(list, nr * sizeof(list[0]));
1347 list[nr - 1] = commit;
1348 }
1349 + if (nr == 0)
1350 + /* nothing to do */
1351 + return 0;
1352 + head = list[0];
1353 total = nr;
1354 if (!keep_subject && auto_number && total > 1)
1355 numbered = 1;
t/t4014-format-patch.sh
+5
@@ -1284,4 +1284,9 @@ test_expect_success 'cover letter using branch description (6)' '
1284 grep hello actual >/dev/null
1285 '
1286
1287 +test_expect_success 'cover letter with nothing' '
1288 + git format-patch --stdout --cover-letter >actual &&
1289 + test_line_count = 0 actual
1290 +'
1291 +
1292 test_done