add-interactive: handle unborn branch in patch mode

The list_modified function already knows how to handle an unborn branch by diffing against the empty tree. However, the diff we perform to get the actual hunks does not. Let's use the same logic for both diffs. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 25, 2013 at 02:52 UTC 954312a3ff76652a954b730ae69a031f98293a99
1 file changed +13 -9
git-add--interactive.perl
+13 -9
@@ -263,6 +263,17 @@ sub get_empty_tree {
263 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
264 }
265
266 +sub get_diff_reference {
267 + my $ref = shift;
268 + if (defined $ref and $ref ne 'HEAD') {
269 + return $ref;
270 + } elsif (is_initial_commit()) {
271 + return get_empty_tree();
272 + } else {
273 + return 'HEAD';
274 + }
275 +}
276 +
277 # Returns list of hashes, contents of each of which are:
278 # VALUE: pathname
279 # BINARY: is a binary path
@@ -286,14 +297,7 @@ sub list_modified {
297 return if (!@tracked);
298 }
299
289 - my $reference;
290 - if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
291 - $reference = $patch_mode_revision;
292 - } elsif (is_initial_commit()) {
293 - $reference = get_empty_tree();
294 - } else {
295 - $reference = 'HEAD';
296 - }
300 + my $reference = get_diff_reference($patch_mode_revision);
301 for (run_cmd_pipe(qw(git diff-index --cached
302 --numstat --summary), $reference,
303 '--', @tracked)) {
@@ -737,7 +741,7 @@ sub parse_diff {
741 splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
742 }
743 if (defined $patch_mode_revision) {
740 - push @diff_cmd, $patch_mode_revision;
744 + push @diff_cmd, get_diff_reference($patch_mode_revision);
745 }
746 my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
747 my @colored = ();