From: Phillip Wood <phillip.wood@xxxxxxxxxxxxx> If there are fewer than ten changes in a hunk then make spaces optional when selecting individual lines. This means that for short hunks one can just type 1-357 to stage lines 1, 2, 3, 5 & 7. Signed-off-by: Phillip Wood <phillip.wood@xxxxxxxxxxxxx> --- Notes: changes since v2: - removed code that handled the default lower bound now that '-...' means invert the selection. Documentation/git-add.txt | 9 +++++---- git-add--interactive.perl | 26 ++++++++++++++++++++++++++ t/t3701-add-interactive.sh | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt index 01ff4d7d24..f3c81dfb11 100644 --- a/Documentation/git-add.txt +++ b/Documentation/git-add.txt @@ -340,10 +340,11 @@ patch:: If you press "l" then the hunk will be reprinted with each insertion or deletion labelled with a number and you will be prompted to enter which lines you wish to select. Individual line numbers should be separated by -a space or comma, to specify a range of lines use a dash between -them. If the upper bound of a range of lines is omitted it defaults to -the last line. To invert the selection prefix it with "-" so "-3-5,8" -will select everything except lines 3, 4, 5 and 8. +a space or comma (these can be omitted if there are fewer than ten +labelled lines), to specify a range of lines use a dash between them. If +the upper bound of a range of lines is omitted it defaults to the last +line. To invert the selection prefix it with "-" so "-3-5,8" will select +everything except lines 3, 4, 5 and 8. + After deciding the fate for all hunks, if there is any hunk that was chosen, the index is updated with the selected hunks. diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 54fbe114f3..d65ad7c26d 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1082,6 +1082,29 @@ sub check_hunk_label { return 1; } +sub split_hunk_selection { + local $_; + my @fields = @_; + my @ret; + for (@fields) { + while ($_ ne '') { + if (/^[0-9]-$/) { + push @ret, $_; + last; + } elsif (/^([0-9](?:-[0-9])?)(.*)/) { + push @ret, $1; + $_ = $2; + } else { + error_msg sprintf + __("invalid hunk line '%s'\n"), + substr($_, 0, 1); + return (); + } + } + } + return @ret; +} + sub parse_hunk_selection { local $_; my ($hunk, $line) = @_; @@ -1100,6 +1123,9 @@ sub parse_hunk_selection { } } } + if ($max_label < 10) { + @fields = split_hunk_selection(@fields) or return undef; + } for (@fields) { if (my ($lo, $hi) = /^([0-9]+)-([0-9]*)$/) { if ($hi eq '') { diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 46814babf3..467c6eff0e 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -410,7 +410,7 @@ test_expect_success 'setup expected diff' ' ' test_expect_success 'can reset individual lines of patch' ' - printf "%s\n" l "-1 3" | + printf "%s\n" l -13 | EDITOR=: git reset -p 2>error && test_must_be_empty error && git diff --cached HEAD >actual && -- 2.16.2