format-patch: improve head calculation for cover-letter
authorFelipe Contreras <felipe.contreras@gmail.com>
Sun, 7 Apr 2013 17:46:20 +0000 (12:46 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sun, 7 Apr 2013 20:32:41 +0000 (13:32 -0700)
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>
builtin/log.c
t/t4014-format-patch.sh
index 0f318107e5732a08bc6f979633829ea958d8f9b0..679282706add9afefa86c80f542685849784338e 100644 (file)
@@ -1311,24 +1311,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        rev.show_root_diff = 1;
 
        if (cover_letter) {
-               /*
-                * NEEDSWORK:randomly pick one positive commit to show
-                * diffstat; this is often the tip and the command
-                * happens to do the right thing in most cases, but a
-                * complex command like "--cover-letter a b c ^bottom"
-                * picks "c" and shows diffstat between bottom..c
-                * which may not match what the series represents at
-                * all and totally broken.
-                */
-               int i;
-               for (i = 0; i < rev.pending.nr; i++) {
-                       struct object *o = rev.pending.objects[i].item;
-                       if (!(o->flags & UNINTERESTING))
-                               head = (struct commit *)o;
-               }
-               /* There is nothing to show; it is not an error, though. */
-               if (!head)
-                       return 0;
                if (!branch_name)
                        branch_name = find_branch_name(&rev);
        }
@@ -1364,6 +1346,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                list = xrealloc(list, nr * sizeof(list[0]));
                list[nr - 1] = commit;
        }
+       if (nr == 0)
+               /* nothing to do */
+               return 0;
+       head = list[0];
        total = nr;
        if (!keep_subject && auto_number && total > 1)
                numbered = 1;
index b993dae64574cd2828fc636d3afc15b2c0c2e5a0..0ada8c78e7af875f371121fff388fd911a3e3974 100755 (executable)
@@ -1284,4 +1284,9 @@ test_expect_success 'cover letter using branch description (6)' '
        grep hello actual >/dev/null
 '
 
+test_expect_success 'cover letter with nothing' '
+       git format-patch --stdout --cover-letter >actual &&
+       test_line_count = 0 actual
+'
+
 test_done