Skip to content

Commit 80d35ca

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 402596a commit 80d35ca

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

builtin/log.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,24 +1311,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13111311
rev.show_root_diff = 1;
13121312

13131313
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;
13321314
if (!branch_name)
13331315
branch_name = find_branch_name(&rev);
13341316
}
@@ -1364,6 +1346,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13641346
list = xrealloc(list, nr * sizeof(list[0]));
13651347
list[nr - 1] = commit;
13661348
}
1349+
if (nr == 0)
1350+
/* nothing to do */
1351+
return 0;
1352+
head = list[0];
13671353
total = nr;
13681354
if (!keep_subject && auto_number && total > 1)
13691355
numbered = 1;

t/t4014-format-patch.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,4 +1284,9 @@ test_expect_success 'cover letter using branch description (6)' '
12841284
grep hello actual >/dev/null
12851285
'
12861286

1287+
test_expect_success 'cover letter with nothing' '
1288+
git format-patch --stdout --cover-letter >actual &&
1289+
test_line_count = 0 actual
1290+
'
1291+
12871292
test_done

0 commit comments

Comments
 (0)