Ævar Arnfjörð Bjarmason | 3328ace | 2010-09-24 20:00:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 2 | |
Ævar Arnfjörð Bjarmason | d48b284 | 2010-09-24 20:00:52 +0000 | [diff] [blame] | 3 | use 5.008; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 4 | use strict; |
Ævar Arnfjörð Bjarmason | 3328ace | 2010-09-24 20:00:53 +0000 | [diff] [blame] | 5 | use warnings; |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 6 | use Git; |
| 7 | |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 8 | binmode(STDOUT, ":raw"); |
| 9 | |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 10 | my $repo = Git->repository(); |
| 11 | |
Jeff King | f87e310 | 2008-01-04 03:35:21 -0500 | [diff] [blame] | 12 | my $menu_use_color = $repo->get_colorbool('color.interactive'); |
| 13 | my ($prompt_color, $header_color, $help_color) = |
| 14 | $menu_use_color ? ( |
| 15 | $repo->get_color('color.interactive.prompt', 'bold blue'), |
| 16 | $repo->get_color('color.interactive.header', 'bold'), |
| 17 | $repo->get_color('color.interactive.help', 'red bold'), |
| 18 | ) : (); |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 19 | my $error_color = (); |
| 20 | if ($menu_use_color) { |
Stephan Beyer | 9aad6cb | 2009-02-08 18:40:39 +0100 | [diff] [blame] | 21 | my $help_color_spec = ($repo->config('color.interactive.help') or |
| 22 | 'red bold'); |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 23 | $error_color = $repo->get_color('color.interactive.error', |
| 24 | $help_color_spec); |
| 25 | } |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 26 | |
Jeff King | f87e310 | 2008-01-04 03:35:21 -0500 | [diff] [blame] | 27 | my $diff_use_color = $repo->get_colorbool('color.diff'); |
| 28 | my ($fraginfo_color) = |
| 29 | $diff_use_color ? ( |
| 30 | $repo->get_color('color.diff.frag', 'cyan'), |
| 31 | ) : (); |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 32 | my ($diff_plain_color) = |
| 33 | $diff_use_color ? ( |
| 34 | $repo->get_color('color.diff.plain', ''), |
| 35 | ) : (); |
| 36 | my ($diff_old_color) = |
| 37 | $diff_use_color ? ( |
| 38 | $repo->get_color('color.diff.old', 'red'), |
| 39 | ) : (); |
| 40 | my ($diff_new_color) = |
| 41 | $diff_use_color ? ( |
| 42 | $repo->get_color('color.diff.new', 'green'), |
| 43 | ) : (); |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 44 | |
Jeff King | f87e310 | 2008-01-04 03:35:21 -0500 | [diff] [blame] | 45 | my $normal_color = $repo->get_color("", "reset"); |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 46 | |
John Keeping | 2cc0f53 | 2013-06-12 19:44:10 +0100 | [diff] [blame] | 47 | my $diff_algorithm = $repo->config('diff.algorithm'); |
| 48 | |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 49 | my $use_readkey = 0; |
Thomas Rast | b5cc003 | 2011-05-17 17:19:08 +0200 | [diff] [blame] | 50 | my $use_termcap = 0; |
| 51 | my %term_escapes; |
| 52 | |
Thomas Rast | 748aa68 | 2009-02-06 20:30:01 +0100 | [diff] [blame] | 53 | sub ReadMode; |
| 54 | sub ReadKey; |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 55 | if ($repo->config_bool("interactive.singlekey")) { |
| 56 | eval { |
Thomas Rast | 748aa68 | 2009-02-06 20:30:01 +0100 | [diff] [blame] | 57 | require Term::ReadKey; |
| 58 | Term::ReadKey->import; |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 59 | $use_readkey = 1; |
| 60 | }; |
Thomas Rast | b5cc003 | 2011-05-17 17:19:08 +0200 | [diff] [blame] | 61 | eval { |
| 62 | require Term::Cap; |
| 63 | my $termcap = Term::Cap->Tgetent; |
| 64 | foreach (values %$termcap) { |
| 65 | $term_escapes{$_} = 1 if /^\e/; |
| 66 | } |
| 67 | $use_termcap = 1; |
| 68 | }; |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 71 | sub colored { |
| 72 | my $color = shift; |
| 73 | my $string = join("", @_); |
| 74 | |
Jeff King | f87e310 | 2008-01-04 03:35:21 -0500 | [diff] [blame] | 75 | if (defined $color) { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 76 | # Put a color code at the beginning of each line, a reset at the end |
| 77 | # color after newlines that are not at the end of the string |
| 78 | $string =~ s/(\n+)(.)/$1$color$2/g; |
| 79 | # reset before newlines |
| 80 | $string =~ s/(\n+)/$normal_color$1/g; |
| 81 | # codes at beginning and end (if necessary): |
| 82 | $string =~ s/^/$color/; |
| 83 | $string =~ s/$/$normal_color/ unless $string =~ /\n$/; |
| 84 | } |
| 85 | return $string; |
| 86 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 87 | |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 88 | # command line options |
| 89 | my $patch_mode; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 90 | my $patch_mode_revision; |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 91 | |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 92 | sub apply_patch; |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 93 | sub apply_patch_for_checkout_commit; |
Thomas Rast | dda1f2a | 2009-08-13 14:29:44 +0200 | [diff] [blame] | 94 | sub apply_patch_for_stash; |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 95 | |
| 96 | my %patch_modes = ( |
| 97 | 'stage' => { |
| 98 | DIFF => 'diff-files -p', |
| 99 | APPLY => sub { apply_patch 'apply --cached', @_; }, |
| 100 | APPLY_CHECK => 'apply --cached', |
| 101 | VERB => 'Stage', |
| 102 | TARGET => '', |
| 103 | PARTICIPLE => 'staging', |
| 104 | FILTER => 'file-only', |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 105 | IS_REVERSE => 0, |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 106 | }, |
Thomas Rast | dda1f2a | 2009-08-13 14:29:44 +0200 | [diff] [blame] | 107 | 'stash' => { |
| 108 | DIFF => 'diff-index -p HEAD', |
| 109 | APPLY => sub { apply_patch 'apply --cached', @_; }, |
| 110 | APPLY_CHECK => 'apply --cached', |
| 111 | VERB => 'Stash', |
| 112 | TARGET => '', |
| 113 | PARTICIPLE => 'stashing', |
| 114 | FILTER => undef, |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 115 | IS_REVERSE => 0, |
Thomas Rast | dda1f2a | 2009-08-13 14:29:44 +0200 | [diff] [blame] | 116 | }, |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 117 | 'reset_head' => { |
| 118 | DIFF => 'diff-index -p --cached', |
| 119 | APPLY => sub { apply_patch 'apply -R --cached', @_; }, |
| 120 | APPLY_CHECK => 'apply -R --cached', |
| 121 | VERB => 'Unstage', |
| 122 | TARGET => '', |
| 123 | PARTICIPLE => 'unstaging', |
| 124 | FILTER => 'index-only', |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 125 | IS_REVERSE => 1, |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 126 | }, |
| 127 | 'reset_nothead' => { |
| 128 | DIFF => 'diff-index -R -p --cached', |
| 129 | APPLY => sub { apply_patch 'apply --cached', @_; }, |
| 130 | APPLY_CHECK => 'apply --cached', |
| 131 | VERB => 'Apply', |
| 132 | TARGET => ' to index', |
| 133 | PARTICIPLE => 'applying', |
| 134 | FILTER => 'index-only', |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 135 | IS_REVERSE => 0, |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 136 | }, |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 137 | 'checkout_index' => { |
| 138 | DIFF => 'diff-files -p', |
| 139 | APPLY => sub { apply_patch 'apply -R', @_; }, |
| 140 | APPLY_CHECK => 'apply -R', |
| 141 | VERB => 'Discard', |
| 142 | TARGET => ' from worktree', |
| 143 | PARTICIPLE => 'discarding', |
| 144 | FILTER => 'file-only', |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 145 | IS_REVERSE => 1, |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 146 | }, |
| 147 | 'checkout_head' => { |
| 148 | DIFF => 'diff-index -p', |
| 149 | APPLY => sub { apply_patch_for_checkout_commit '-R', @_ }, |
| 150 | APPLY_CHECK => 'apply -R', |
| 151 | VERB => 'Discard', |
| 152 | TARGET => ' from index and worktree', |
| 153 | PARTICIPLE => 'discarding', |
| 154 | FILTER => undef, |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 155 | IS_REVERSE => 1, |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 156 | }, |
| 157 | 'checkout_nothead' => { |
| 158 | DIFF => 'diff-index -R -p', |
| 159 | APPLY => sub { apply_patch_for_checkout_commit '', @_ }, |
| 160 | APPLY_CHECK => 'apply', |
| 161 | VERB => 'Apply', |
| 162 | TARGET => ' to index and worktree', |
| 163 | PARTICIPLE => 'applying', |
| 164 | FILTER => undef, |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 165 | IS_REVERSE => 0, |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 166 | }, |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 167 | ); |
| 168 | |
| 169 | my %patch_mode_flavour = %{$patch_modes{stage}}; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 170 | |
| 171 | sub run_cmd_pipe { |
Johannes Sixt | df17e77 | 2013-09-04 09:24:47 +0200 | [diff] [blame] | 172 | if ($^O eq 'MSWin32') { |
Alex Riesen | 21e9757 | 2007-08-01 14:57:43 +0200 | [diff] [blame] | 173 | my @invalid = grep {m/[":*]/} @_; |
| 174 | die "$^O does not support: @invalid\n" if @invalid; |
| 175 | my @args = map { m/ /o ? "\"$_\"": $_ } @_; |
| 176 | return qx{@args}; |
| 177 | } else { |
| 178 | my $fh = undef; |
| 179 | open($fh, '-|', @_) or die; |
| 180 | return <$fh>; |
| 181 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir)); |
| 185 | |
| 186 | if (!defined $GIT_DIR) { |
| 187 | exit(1); # rev-parse would have already said "not a git repo" |
| 188 | } |
| 189 | chomp($GIT_DIR); |
| 190 | |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 191 | my %cquote_map = ( |
| 192 | "b" => chr(8), |
| 193 | "t" => chr(9), |
| 194 | "n" => chr(10), |
| 195 | "v" => chr(11), |
| 196 | "f" => chr(12), |
| 197 | "r" => chr(13), |
| 198 | "\\" => "\\", |
| 199 | "\042" => "\042", |
| 200 | ); |
| 201 | |
| 202 | sub unquote_path { |
| 203 | local ($_) = @_; |
| 204 | my ($retval, $remainder); |
| 205 | if (!/^\042(.*)\042$/) { |
| 206 | return $_; |
| 207 | } |
| 208 | ($_, $retval) = ($1, ""); |
| 209 | while (/^([^\\]*)\\(.*)$/) { |
| 210 | $remainder = $2; |
| 211 | $retval .= $1; |
| 212 | for ($remainder) { |
| 213 | if (/^([0-3][0-7][0-7])(.*)$/) { |
| 214 | $retval .= chr(oct($1)); |
| 215 | $_ = $2; |
| 216 | last; |
| 217 | } |
| 218 | if (/^([\\\042btnvfr])(.*)$/) { |
| 219 | $retval .= $cquote_map{$1}; |
| 220 | $_ = $2; |
| 221 | last; |
| 222 | } |
| 223 | # This is malformed -- just return it as-is for now. |
| 224 | return $_[0]; |
| 225 | } |
| 226 | $_ = $remainder; |
| 227 | } |
| 228 | $retval .= $_; |
| 229 | return $retval; |
| 230 | } |
| 231 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 232 | sub refresh { |
| 233 | my $fh; |
Alex Riesen | 21e9757 | 2007-08-01 14:57:43 +0200 | [diff] [blame] | 234 | open $fh, 'git update-index --refresh |' |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 235 | or die; |
| 236 | while (<$fh>) { |
| 237 | ;# ignore 'needs update' |
| 238 | } |
| 239 | close $fh; |
| 240 | } |
| 241 | |
| 242 | sub list_untracked { |
| 243 | map { |
| 244 | chomp $_; |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 245 | unquote_path($_); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 246 | } |
Wincent Colaiuta | 4c84168 | 2007-11-22 02:36:24 +0100 | [diff] [blame] | 247 | run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | my $status_fmt = '%12s %12s %s'; |
| 251 | my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path'); |
| 252 | |
Jeff King | 18bc761 | 2008-02-13 05:50:51 -0500 | [diff] [blame] | 253 | { |
| 254 | my $initial; |
| 255 | sub is_initial_commit { |
| 256 | $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0 |
| 257 | unless defined $initial; |
| 258 | return $initial; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | sub get_empty_tree { |
| 263 | return '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; |
| 264 | } |
| 265 | |
Jeff King | 954312a | 2013-10-25 02:52:30 -0400 | [diff] [blame] | 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 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 277 | # Returns list of hashes, contents of each of which are: |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 278 | # VALUE: pathname |
| 279 | # BINARY: is a binary path |
| 280 | # INDEX: is index different from HEAD? |
| 281 | # FILE: is file different from index? |
| 282 | # INDEX_ADDDEL: is it add/delete between HEAD and index? |
| 283 | # FILE_ADDDEL: is it add/delete between index and file? |
Jeff King | 4066bd6 | 2012-04-05 08:30:08 -0400 | [diff] [blame] | 284 | # UNMERGED: is the path unmerged |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 285 | |
| 286 | sub list_modified { |
| 287 | my ($only) = @_; |
| 288 | my (%data, @return); |
| 289 | my ($add, $del, $adddel, $file); |
Wincent Colaiuta | 4c84168 | 2007-11-22 02:36:24 +0100 | [diff] [blame] | 290 | my @tracked = (); |
| 291 | |
| 292 | if (@ARGV) { |
| 293 | @tracked = map { |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 294 | chomp $_; |
| 295 | unquote_path($_); |
Pauli Virtanen | b145b21 | 2009-10-10 18:51:45 +0300 | [diff] [blame] | 296 | } run_cmd_pipe(qw(git ls-files --), @ARGV); |
Wincent Colaiuta | 4c84168 | 2007-11-22 02:36:24 +0100 | [diff] [blame] | 297 | return if (!@tracked); |
| 298 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 299 | |
Jeff King | 954312a | 2013-10-25 02:52:30 -0400 | [diff] [blame] | 300 | my $reference = get_diff_reference($patch_mode_revision); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 301 | for (run_cmd_pipe(qw(git diff-index --cached |
Jeff King | 18bc761 | 2008-02-13 05:50:51 -0500 | [diff] [blame] | 302 | --numstat --summary), $reference, |
| 303 | '--', @tracked)) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 304 | if (($add, $del, $file) = |
| 305 | /^([-\d]+) ([-\d]+) (.*)/) { |
| 306 | my ($change, $bin); |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 307 | $file = unquote_path($file); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 308 | if ($add eq '-' && $del eq '-') { |
| 309 | $change = 'binary'; |
| 310 | $bin = 1; |
| 311 | } |
| 312 | else { |
| 313 | $change = "+$add/-$del"; |
| 314 | } |
| 315 | $data{$file} = { |
| 316 | INDEX => $change, |
| 317 | BINARY => $bin, |
| 318 | FILE => 'nothing', |
| 319 | } |
| 320 | } |
| 321 | elsif (($adddel, $file) = |
| 322 | /^ (create|delete) mode [0-7]+ (.*)$/) { |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 323 | $file = unquote_path($file); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 324 | $data{$file}{INDEX_ADDDEL} = $adddel; |
| 325 | } |
| 326 | } |
| 327 | |
Jeff King | 4066bd6 | 2012-04-05 08:30:08 -0400 | [diff] [blame] | 328 | for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 329 | if (($add, $del, $file) = |
| 330 | /^([-\d]+) ([-\d]+) (.*)/) { |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 331 | $file = unquote_path($file); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 332 | my ($change, $bin); |
| 333 | if ($add eq '-' && $del eq '-') { |
| 334 | $change = 'binary'; |
| 335 | $bin = 1; |
| 336 | } |
| 337 | else { |
| 338 | $change = "+$add/-$del"; |
| 339 | } |
| 340 | $data{$file}{FILE} = $change; |
| 341 | if ($bin) { |
| 342 | $data{$file}{BINARY} = 1; |
| 343 | } |
| 344 | } |
| 345 | elsif (($adddel, $file) = |
| 346 | /^ (create|delete) mode [0-7]+ (.*)$/) { |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 347 | $file = unquote_path($file); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 348 | $data{$file}{FILE_ADDDEL} = $adddel; |
| 349 | } |
Jeff King | 4066bd6 | 2012-04-05 08:30:08 -0400 | [diff] [blame] | 350 | elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) { |
| 351 | $file = unquote_path($2); |
| 352 | if (!exists $data{$file}) { |
| 353 | $data{$file} = +{ |
| 354 | INDEX => 'unchanged', |
| 355 | BINARY => 0, |
| 356 | }; |
| 357 | } |
| 358 | if ($1 eq 'U') { |
| 359 | $data{$file}{UNMERGED} = 1; |
| 360 | } |
| 361 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | for (sort keys %data) { |
| 365 | my $it = $data{$_}; |
| 366 | |
| 367 | if ($only) { |
| 368 | if ($only eq 'index-only') { |
| 369 | next if ($it->{INDEX} eq 'unchanged'); |
| 370 | } |
| 371 | if ($only eq 'file-only') { |
| 372 | next if ($it->{FILE} eq 'nothing'); |
| 373 | } |
| 374 | } |
| 375 | push @return, +{ |
| 376 | VALUE => $_, |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 377 | %$it, |
| 378 | }; |
| 379 | } |
| 380 | return @return; |
| 381 | } |
| 382 | |
| 383 | sub find_unique { |
| 384 | my ($string, @stuff) = @_; |
| 385 | my $found = undef; |
| 386 | for (my $i = 0; $i < @stuff; $i++) { |
| 387 | my $it = $stuff[$i]; |
| 388 | my $hit = undef; |
| 389 | if (ref $it) { |
| 390 | if ((ref $it) eq 'ARRAY') { |
| 391 | $it = $it->[0]; |
| 392 | } |
| 393 | else { |
| 394 | $it = $it->{VALUE}; |
| 395 | } |
| 396 | } |
| 397 | eval { |
| 398 | if ($it =~ /^$string/) { |
| 399 | $hit = 1; |
| 400 | }; |
| 401 | }; |
| 402 | if (defined $hit && defined $found) { |
| 403 | return undef; |
| 404 | } |
| 405 | if ($hit) { |
| 406 | $found = $i + 1; |
| 407 | } |
| 408 | } |
| 409 | return $found; |
| 410 | } |
| 411 | |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 412 | # inserts string into trie and updates count for each character |
| 413 | sub update_trie { |
| 414 | my ($trie, $string) = @_; |
| 415 | foreach (split //, $string) { |
| 416 | $trie = $trie->{$_} ||= {COUNT => 0}; |
| 417 | $trie->{COUNT}++; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | # returns an array of tuples (prefix, remainder) |
| 422 | sub find_unique_prefixes { |
| 423 | my @stuff = @_; |
| 424 | my @return = (); |
| 425 | |
| 426 | # any single prefix exceeding the soft limit is omitted |
| 427 | # if any prefix exceeds the hard limit all are omitted |
| 428 | # 0 indicates no limit |
| 429 | my $soft_limit = 0; |
| 430 | my $hard_limit = 3; |
| 431 | |
| 432 | # build a trie modelling all possible options |
| 433 | my %trie; |
| 434 | foreach my $print (@stuff) { |
| 435 | if ((ref $print) eq 'ARRAY') { |
| 436 | $print = $print->[0]; |
| 437 | } |
Wincent Colaiuta | 6332098 | 2007-12-02 14:44:11 +0100 | [diff] [blame] | 438 | elsif ((ref $print) eq 'HASH') { |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 439 | $print = $print->{VALUE}; |
| 440 | } |
| 441 | update_trie(\%trie, $print); |
| 442 | push @return, $print; |
| 443 | } |
| 444 | |
| 445 | # use the trie to find the unique prefixes |
| 446 | for (my $i = 0; $i < @return; $i++) { |
| 447 | my $ret = $return[$i]; |
| 448 | my @letters = split //, $ret; |
| 449 | my %search = %trie; |
| 450 | my ($prefix, $remainder); |
| 451 | my $j; |
| 452 | for ($j = 0; $j < @letters; $j++) { |
| 453 | my $letter = $letters[$j]; |
| 454 | if ($search{$letter}{COUNT} == 1) { |
| 455 | $prefix = substr $ret, 0, $j + 1; |
| 456 | $remainder = substr $ret, $j + 1; |
| 457 | last; |
| 458 | } |
| 459 | else { |
| 460 | my $prefix = substr $ret, 0, $j; |
| 461 | return () |
| 462 | if ($hard_limit && $j + 1 > $hard_limit); |
| 463 | } |
| 464 | %search = %{$search{$letter}}; |
| 465 | } |
Junio C Hamano | 8851f48 | 2009-02-16 22:43:43 -0800 | [diff] [blame] | 466 | if (ord($letters[0]) > 127 || |
| 467 | ($soft_limit && $j + 1 > $soft_limit)) { |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 468 | $prefix = undef; |
| 469 | $remainder = $ret; |
| 470 | } |
| 471 | $return[$i] = [$prefix, $remainder]; |
| 472 | } |
| 473 | return @return; |
| 474 | } |
| 475 | |
Wincent Colaiuta | 6332098 | 2007-12-02 14:44:11 +0100 | [diff] [blame] | 476 | # filters out prefixes which have special meaning to list_and_choose() |
| 477 | sub is_valid_prefix { |
| 478 | my $prefix = shift; |
| 479 | return (defined $prefix) && |
| 480 | !($prefix =~ /[\s,]/) && # separators |
| 481 | !($prefix =~ /^-/) && # deselection |
| 482 | !($prefix =~ /^\d+/) && # selection |
Wincent Colaiuta | 7e018be | 2007-12-03 09:09:43 +0100 | [diff] [blame] | 483 | ($prefix ne '*') && # "all" wildcard |
| 484 | ($prefix ne '?'); # prompt help |
Wincent Colaiuta | 6332098 | 2007-12-02 14:44:11 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 487 | # given a prefix/remainder tuple return a string with the prefix highlighted |
| 488 | # for now use square brackets; later might use ANSI colors (underline, bold) |
| 489 | sub highlight_prefix { |
| 490 | my $prefix = shift; |
| 491 | my $remainder = shift; |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 492 | |
| 493 | if (!defined $prefix) { |
| 494 | return $remainder; |
| 495 | } |
| 496 | |
| 497 | if (!is_valid_prefix($prefix)) { |
| 498 | return "$prefix$remainder"; |
| 499 | } |
| 500 | |
Jeff King | f87e310 | 2008-01-04 03:35:21 -0500 | [diff] [blame] | 501 | if (!$menu_use_color) { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 502 | return "[$prefix]$remainder"; |
| 503 | } |
| 504 | |
| 505 | return "$prompt_color$prefix$normal_color$remainder"; |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 508 | sub error_msg { |
| 509 | print STDERR colored $error_color, @_; |
| 510 | } |
| 511 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 512 | sub list_and_choose { |
| 513 | my ($opts, @stuff) = @_; |
| 514 | my (@chosen, @return); |
| 515 | my $i; |
Wincent Colaiuta | 14cb503 | 2007-11-29 13:00:38 +0100 | [diff] [blame] | 516 | my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY}; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 517 | |
| 518 | TOPLOOP: |
| 519 | while (1) { |
| 520 | my $last_lf = 0; |
| 521 | |
| 522 | if ($opts->{HEADER}) { |
| 523 | if (!$opts->{LIST_FLAT}) { |
| 524 | print " "; |
| 525 | } |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 526 | print colored $header_color, "$opts->{HEADER}\n"; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 527 | } |
| 528 | for ($i = 0; $i < @stuff; $i++) { |
| 529 | my $chosen = $chosen[$i] ? '*' : ' '; |
| 530 | my $print = $stuff[$i]; |
Wincent Colaiuta | 6332098 | 2007-12-02 14:44:11 +0100 | [diff] [blame] | 531 | my $ref = ref $print; |
| 532 | my $highlighted = highlight_prefix(@{$prefixes[$i]}) |
| 533 | if @prefixes; |
| 534 | if ($ref eq 'ARRAY') { |
| 535 | $print = $highlighted || $print->[0]; |
| 536 | } |
| 537 | elsif ($ref eq 'HASH') { |
| 538 | my $value = $highlighted || $print->{VALUE}; |
| 539 | $print = sprintf($status_fmt, |
| 540 | $print->{INDEX}, |
| 541 | $print->{FILE}, |
| 542 | $value); |
| 543 | } |
| 544 | else { |
| 545 | $print = $highlighted || $print; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 546 | } |
| 547 | printf("%s%2d: %s", $chosen, $i+1, $print); |
| 548 | if (($opts->{LIST_FLAT}) && |
| 549 | (($i + 1) % ($opts->{LIST_FLAT}))) { |
| 550 | print "\t"; |
| 551 | $last_lf = 0; |
| 552 | } |
| 553 | else { |
| 554 | print "\n"; |
| 555 | $last_lf = 1; |
| 556 | } |
| 557 | } |
| 558 | if (!$last_lf) { |
| 559 | print "\n"; |
| 560 | } |
| 561 | |
| 562 | return if ($opts->{LIST_ONLY}); |
| 563 | |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 564 | print colored $prompt_color, $opts->{PROMPT}; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 565 | if ($opts->{SINGLETON}) { |
| 566 | print "> "; |
| 567 | } |
| 568 | else { |
| 569 | print ">> "; |
| 570 | } |
| 571 | my $line = <STDIN>; |
Jean-Luc Herren | c95c024 | 2007-09-26 15:56:19 +0200 | [diff] [blame] | 572 | if (!$line) { |
| 573 | print "\n"; |
| 574 | $opts->{ON_EOF}->() if $opts->{ON_EOF}; |
| 575 | last; |
| 576 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 577 | chomp $line; |
Jean-Luc Herren | 6a6eb3d | 2007-09-26 16:05:01 +0200 | [diff] [blame] | 578 | last if $line eq ''; |
Wincent Colaiuta | 7e018be | 2007-12-03 09:09:43 +0100 | [diff] [blame] | 579 | if ($line eq '?') { |
| 580 | $opts->{SINGLETON} ? |
| 581 | singleton_prompt_help_cmd() : |
| 582 | prompt_help_cmd(); |
| 583 | next TOPLOOP; |
| 584 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 585 | for my $choice (split(/[\s,]+/, $line)) { |
| 586 | my $choose = 1; |
| 587 | my ($bottom, $top); |
| 588 | |
| 589 | # Input that begins with '-'; unchoose |
| 590 | if ($choice =~ s/^-//) { |
| 591 | $choose = 0; |
| 592 | } |
Ciaran McCreesh | 1e5aaa6 | 2008-07-14 19:29:37 +0100 | [diff] [blame] | 593 | # A range can be specified like 5-7 or 5-. |
| 594 | if ($choice =~ /^(\d+)-(\d*)$/) { |
| 595 | ($bottom, $top) = ($1, length($2) ? $2 : 1 + @stuff); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 596 | } |
| 597 | elsif ($choice =~ /^\d+$/) { |
| 598 | $bottom = $top = $choice; |
| 599 | } |
| 600 | elsif ($choice eq '*') { |
| 601 | $bottom = 1; |
| 602 | $top = 1 + @stuff; |
| 603 | } |
| 604 | else { |
| 605 | $bottom = $top = find_unique($choice, @stuff); |
| 606 | if (!defined $bottom) { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 607 | error_msg "Huh ($choice)?\n"; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 608 | next TOPLOOP; |
| 609 | } |
| 610 | } |
| 611 | if ($opts->{SINGLETON} && $bottom != $top) { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 612 | error_msg "Huh ($choice)?\n"; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 613 | next TOPLOOP; |
| 614 | } |
| 615 | for ($i = $bottom-1; $i <= $top-1; $i++) { |
Jean-Luc Herren | 6a6eb3d | 2007-09-26 16:05:01 +0200 | [diff] [blame] | 616 | next if (@stuff <= $i || $i < 0); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 617 | $chosen[$i] = $choose; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
Junio C Hamano | 12db334 | 2007-11-22 01:47:13 -0800 | [diff] [blame] | 620 | last if ($opts->{IMMEDIATE} || $line eq '*'); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 621 | } |
| 622 | for ($i = 0; $i < @stuff; $i++) { |
| 623 | if ($chosen[$i]) { |
| 624 | push @return, $stuff[$i]; |
| 625 | } |
| 626 | } |
| 627 | return @return; |
| 628 | } |
| 629 | |
Wincent Colaiuta | 7e018be | 2007-12-03 09:09:43 +0100 | [diff] [blame] | 630 | sub singleton_prompt_help_cmd { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 631 | print colored $help_color, <<\EOF ; |
Wincent Colaiuta | 7e018be | 2007-12-03 09:09:43 +0100 | [diff] [blame] | 632 | Prompt help: |
| 633 | 1 - select a numbered item |
| 634 | foo - select item based on unique prefix |
| 635 | - (empty) select nothing |
| 636 | EOF |
| 637 | } |
| 638 | |
| 639 | sub prompt_help_cmd { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 640 | print colored $help_color, <<\EOF ; |
Wincent Colaiuta | 7e018be | 2007-12-03 09:09:43 +0100 | [diff] [blame] | 641 | Prompt help: |
| 642 | 1 - select a single item |
| 643 | 3-5 - select a range of items |
| 644 | 2-3,6-9 - select multiple ranges |
| 645 | foo - select item based on unique prefix |
| 646 | -... - unselect specified items |
| 647 | * - choose all items |
| 648 | - (empty) finish selecting |
| 649 | EOF |
| 650 | } |
| 651 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 652 | sub status_cmd { |
| 653 | list_and_choose({ LIST_ONLY => 1, HEADER => $status_head }, |
| 654 | list_modified()); |
| 655 | print "\n"; |
| 656 | } |
| 657 | |
| 658 | sub say_n_paths { |
| 659 | my $did = shift @_; |
| 660 | my $cnt = scalar @_; |
| 661 | print "$did "; |
| 662 | if (1 < $cnt) { |
| 663 | print "$cnt paths\n"; |
| 664 | } |
| 665 | else { |
| 666 | print "one path\n"; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | sub update_cmd { |
| 671 | my @mods = list_modified('file-only'); |
| 672 | return if (!@mods); |
| 673 | |
| 674 | my @update = list_and_choose({ PROMPT => 'Update', |
| 675 | HEADER => $status_head, }, |
| 676 | @mods); |
| 677 | if (@update) { |
Junio C Hamano | a4f7112 | 2007-02-07 10:56:38 -0800 | [diff] [blame] | 678 | system(qw(git update-index --add --remove --), |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 679 | map { $_->{VALUE} } @update); |
| 680 | say_n_paths('updated', @update); |
| 681 | } |
| 682 | print "\n"; |
| 683 | } |
| 684 | |
| 685 | sub revert_cmd { |
| 686 | my @update = list_and_choose({ PROMPT => 'Revert', |
| 687 | HEADER => $status_head, }, |
| 688 | list_modified()); |
| 689 | if (@update) { |
Jeff King | 18bc761 | 2008-02-13 05:50:51 -0500 | [diff] [blame] | 690 | if (is_initial_commit()) { |
| 691 | system(qw(git rm --cached), |
| 692 | map { $_->{VALUE} } @update); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 693 | } |
Jeff King | 18bc761 | 2008-02-13 05:50:51 -0500 | [diff] [blame] | 694 | else { |
| 695 | my @lines = run_cmd_pipe(qw(git ls-tree HEAD --), |
| 696 | map { $_->{VALUE} } @update); |
| 697 | my $fh; |
| 698 | open $fh, '| git update-index --index-info' |
| 699 | or die; |
| 700 | for (@lines) { |
| 701 | print $fh $_; |
| 702 | } |
| 703 | close($fh); |
| 704 | for (@update) { |
| 705 | if ($_->{INDEX_ADDDEL} && |
| 706 | $_->{INDEX_ADDDEL} eq 'create') { |
| 707 | system(qw(git update-index --force-remove --), |
| 708 | $_->{VALUE}); |
| 709 | print "note: $_->{VALUE} is untracked now.\n"; |
| 710 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | refresh(); |
| 714 | say_n_paths('reverted', @update); |
| 715 | } |
| 716 | print "\n"; |
| 717 | } |
| 718 | |
| 719 | sub add_untracked_cmd { |
| 720 | my @add = list_and_choose({ PROMPT => 'Add untracked' }, |
| 721 | list_untracked()); |
| 722 | if (@add) { |
| 723 | system(qw(git update-index --add --), @add); |
| 724 | say_n_paths('added', @add); |
| 725 | } |
| 726 | print "\n"; |
| 727 | } |
| 728 | |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 729 | sub run_git_apply { |
| 730 | my $cmd = shift; |
| 731 | my $fh; |
Junio C Hamano | 933e44d | 2011-04-06 14:20:57 -0700 | [diff] [blame] | 732 | open $fh, '| git ' . $cmd . " --recount --allow-overlap"; |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 733 | print $fh @_; |
| 734 | return close $fh; |
| 735 | } |
| 736 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 737 | sub parse_diff { |
| 738 | my ($path) = @_; |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 739 | my @diff_cmd = split(" ", $patch_mode_flavour{DIFF}); |
John Keeping | 2cc0f53 | 2013-06-12 19:44:10 +0100 | [diff] [blame] | 740 | if (defined $diff_algorithm) { |
Junio C Hamano | e5c2909 | 2013-06-23 12:19:05 -0700 | [diff] [blame] | 741 | splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}"; |
John Keeping | 2cc0f53 | 2013-06-12 19:44:10 +0100 | [diff] [blame] | 742 | } |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 743 | if (defined $patch_mode_revision) { |
Jeff King | 954312a | 2013-10-25 02:52:30 -0400 | [diff] [blame] | 744 | push @diff_cmd, get_diff_reference($patch_mode_revision); |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 745 | } |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 746 | my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path); |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 747 | my @colored = (); |
| 748 | if ($diff_use_color) { |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 749 | @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path); |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 750 | } |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 751 | my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' }; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 752 | |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 753 | for (my $i = 0; $i < @diff; $i++) { |
| 754 | if ($diff[$i] =~ /^@@ /) { |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 755 | push @hunk, { TEXT => [], DISPLAY => [], |
| 756 | TYPE => 'hunk' }; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 757 | } |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 758 | push @{$hunk[-1]{TEXT}}, $diff[$i]; |
| 759 | push @{$hunk[-1]{DISPLAY}}, |
| 760 | ($diff_use_color ? $colored[$i] : $diff[$i]); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 761 | } |
| 762 | return @hunk; |
| 763 | } |
| 764 | |
Jeff King | b717a62 | 2008-03-27 03:30:43 -0400 | [diff] [blame] | 765 | sub parse_diff_header { |
| 766 | my $src = shift; |
| 767 | |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 768 | my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' }; |
| 769 | my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' }; |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 770 | my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' }; |
Jeff King | b717a62 | 2008-03-27 03:30:43 -0400 | [diff] [blame] | 771 | |
| 772 | for (my $i = 0; $i < @{$src->{TEXT}}; $i++) { |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 773 | my $dest = |
| 774 | $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode : |
| 775 | $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion : |
| 776 | $head; |
Jeff King | b717a62 | 2008-03-27 03:30:43 -0400 | [diff] [blame] | 777 | push @{$dest->{TEXT}}, $src->{TEXT}->[$i]; |
| 778 | push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i]; |
| 779 | } |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 780 | return ($head, $mode, $deletion); |
Jeff King | b717a62 | 2008-03-27 03:30:43 -0400 | [diff] [blame] | 781 | } |
| 782 | |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 783 | sub hunk_splittable { |
| 784 | my ($text) = @_; |
| 785 | |
| 786 | my @s = split_hunk($text); |
| 787 | return (1 < @s); |
| 788 | } |
| 789 | |
| 790 | sub parse_hunk_header { |
| 791 | my ($line) = @_; |
| 792 | my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = |
Jean-Luc Herren | 7288ed8 | 2007-10-09 21:29:26 +0200 | [diff] [blame] | 793 | $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/; |
| 794 | $o_cnt = 1 unless defined $o_cnt; |
| 795 | $n_cnt = 1 unless defined $n_cnt; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 796 | return ($o_ofs, $o_cnt, $n_ofs, $n_cnt); |
| 797 | } |
| 798 | |
| 799 | sub split_hunk { |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 800 | my ($text, $display) = @_; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 801 | my @split = (); |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 802 | if (!defined $display) { |
| 803 | $display = $text; |
| 804 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 805 | # If there are context lines in the middle of a hunk, |
| 806 | # it can be split, but we would need to take care of |
| 807 | # overlaps later. |
| 808 | |
Jean-Luc Herren | 7b40a45 | 2007-10-09 21:34:17 +0200 | [diff] [blame] | 809 | my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]); |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 810 | my $hunk_start = 1; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 811 | |
| 812 | OUTER: |
| 813 | while (1) { |
| 814 | my $next_hunk_start = undef; |
| 815 | my $i = $hunk_start - 1; |
| 816 | my $this = +{ |
| 817 | TEXT => [], |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 818 | DISPLAY => [], |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 819 | TYPE => 'hunk', |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 820 | OLD => $o_ofs, |
| 821 | NEW => $n_ofs, |
| 822 | OCNT => 0, |
| 823 | NCNT => 0, |
| 824 | ADDDEL => 0, |
| 825 | POSTCTX => 0, |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 826 | USE => undef, |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 827 | }; |
| 828 | |
| 829 | while (++$i < @$text) { |
| 830 | my $line = $text->[$i]; |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 831 | my $display = $display->[$i]; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 832 | if ($line =~ /^ /) { |
| 833 | if ($this->{ADDDEL} && |
| 834 | !defined $next_hunk_start) { |
| 835 | # We have seen leading context and |
| 836 | # adds/dels and then here is another |
| 837 | # context, which is trailing for this |
| 838 | # split hunk and leading for the next |
| 839 | # one. |
| 840 | $next_hunk_start = $i; |
| 841 | } |
| 842 | push @{$this->{TEXT}}, $line; |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 843 | push @{$this->{DISPLAY}}, $display; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 844 | $this->{OCNT}++; |
| 845 | $this->{NCNT}++; |
| 846 | if (defined $next_hunk_start) { |
| 847 | $this->{POSTCTX}++; |
| 848 | } |
| 849 | next; |
| 850 | } |
| 851 | |
| 852 | # add/del |
| 853 | if (defined $next_hunk_start) { |
| 854 | # We are done with the current hunk and |
| 855 | # this is the first real change for the |
| 856 | # next split one. |
| 857 | $hunk_start = $next_hunk_start; |
| 858 | $o_ofs = $this->{OLD} + $this->{OCNT}; |
| 859 | $n_ofs = $this->{NEW} + $this->{NCNT}; |
| 860 | $o_ofs -= $this->{POSTCTX}; |
| 861 | $n_ofs -= $this->{POSTCTX}; |
| 862 | push @split, $this; |
| 863 | redo OUTER; |
| 864 | } |
| 865 | push @{$this->{TEXT}}, $line; |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 866 | push @{$this->{DISPLAY}}, $display; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 867 | $this->{ADDDEL}++; |
| 868 | if ($line =~ /^-/) { |
| 869 | $this->{OCNT}++; |
| 870 | } |
| 871 | else { |
| 872 | $this->{NCNT}++; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | push @split, $this; |
| 877 | last; |
| 878 | } |
| 879 | |
| 880 | for my $hunk (@split) { |
| 881 | $o_ofs = $hunk->{OLD}; |
| 882 | $n_ofs = $hunk->{NEW}; |
Jean-Luc Herren | 7b40a45 | 2007-10-09 21:34:17 +0200 | [diff] [blame] | 883 | my $o_cnt = $hunk->{OCNT}; |
| 884 | my $n_cnt = $hunk->{NCNT}; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 885 | |
| 886 | my $head = ("@@ -$o_ofs" . |
| 887 | (($o_cnt != 1) ? ",$o_cnt" : '') . |
| 888 | " +$n_ofs" . |
| 889 | (($n_cnt != 1) ? ",$n_cnt" : '') . |
| 890 | " @@\n"); |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 891 | my $display_head = $head; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 892 | unshift @{$hunk->{TEXT}}, $head; |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 893 | if ($diff_use_color) { |
| 894 | $display_head = colored($fraginfo_color, $head); |
| 895 | } |
| 896 | unshift @{$hunk->{DISPLAY}}, $display_head; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 897 | } |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 898 | return @split; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Junio C Hamano | 7a26e65 | 2009-05-16 10:48:23 -0700 | [diff] [blame] | 901 | sub find_last_o_ctx { |
| 902 | my ($it) = @_; |
| 903 | my $text = $it->{TEXT}; |
| 904 | my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]); |
| 905 | my $i = @{$text}; |
| 906 | my $last_o_ctx = $o_ofs + $o_cnt; |
| 907 | while (0 < --$i) { |
| 908 | my $line = $text->[$i]; |
| 909 | if ($line =~ /^ /) { |
| 910 | $last_o_ctx--; |
| 911 | next; |
| 912 | } |
| 913 | last; |
| 914 | } |
| 915 | return $last_o_ctx; |
| 916 | } |
| 917 | |
| 918 | sub merge_hunk { |
| 919 | my ($prev, $this) = @_; |
| 920 | my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) = |
| 921 | parse_hunk_header($prev->{TEXT}[0]); |
| 922 | my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) = |
| 923 | parse_hunk_header($this->{TEXT}[0]); |
| 924 | |
| 925 | my (@line, $i, $ofs, $o_cnt, $n_cnt); |
| 926 | $ofs = $o0_ofs; |
| 927 | $o_cnt = $n_cnt = 0; |
| 928 | for ($i = 1; $i < @{$prev->{TEXT}}; $i++) { |
| 929 | my $line = $prev->{TEXT}[$i]; |
| 930 | if ($line =~ /^\+/) { |
| 931 | $n_cnt++; |
| 932 | push @line, $line; |
| 933 | next; |
| 934 | } |
| 935 | |
| 936 | last if ($o1_ofs <= $ofs); |
| 937 | |
| 938 | $o_cnt++; |
| 939 | $ofs++; |
| 940 | if ($line =~ /^ /) { |
| 941 | $n_cnt++; |
| 942 | } |
| 943 | push @line, $line; |
| 944 | } |
| 945 | |
| 946 | for ($i = 1; $i < @{$this->{TEXT}}; $i++) { |
| 947 | my $line = $this->{TEXT}[$i]; |
| 948 | if ($line =~ /^\+/) { |
| 949 | $n_cnt++; |
| 950 | push @line, $line; |
| 951 | next; |
| 952 | } |
| 953 | $ofs++; |
| 954 | $o_cnt++; |
| 955 | if ($line =~ /^ /) { |
| 956 | $n_cnt++; |
| 957 | } |
| 958 | push @line, $line; |
| 959 | } |
| 960 | my $head = ("@@ -$o0_ofs" . |
| 961 | (($o_cnt != 1) ? ",$o_cnt" : '') . |
| 962 | " +$n0_ofs" . |
| 963 | (($n_cnt != 1) ? ",$n_cnt" : '') . |
| 964 | " @@\n"); |
| 965 | @{$prev->{TEXT}} = ($head, @line); |
| 966 | } |
| 967 | |
| 968 | sub coalesce_overlapping_hunks { |
| 969 | my (@in) = @_; |
| 970 | my @out = (); |
| 971 | |
| 972 | my ($last_o_ctx, $last_was_dirty); |
| 973 | |
| 974 | for (grep { $_->{USE} } @in) { |
Thomas Rast | 3d79216 | 2009-08-15 15:56:39 +0200 | [diff] [blame] | 975 | if ($_->{TYPE} ne 'hunk') { |
| 976 | push @out, $_; |
| 977 | next; |
| 978 | } |
Junio C Hamano | 7a26e65 | 2009-05-16 10:48:23 -0700 | [diff] [blame] | 979 | my $text = $_->{TEXT}; |
| 980 | my ($o_ofs) = parse_hunk_header($text->[0]); |
| 981 | if (defined $last_o_ctx && |
| 982 | $o_ofs <= $last_o_ctx && |
| 983 | !$_->{DIRTY} && |
| 984 | !$last_was_dirty) { |
| 985 | merge_hunk($out[-1], $_); |
| 986 | } |
| 987 | else { |
| 988 | push @out, $_; |
| 989 | } |
| 990 | $last_o_ctx = find_last_o_ctx($out[-1]); |
| 991 | $last_was_dirty = $_->{DIRTY}; |
| 992 | } |
| 993 | return @out; |
| 994 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 995 | |
Jeff King | e1327ed | 2010-02-22 20:05:44 -0500 | [diff] [blame] | 996 | sub reassemble_patch { |
| 997 | my $head = shift; |
| 998 | my @patch; |
| 999 | |
| 1000 | # Include everything in the header except the beginning of the diff. |
| 1001 | push @patch, (grep { !/^[-+]{3}/ } @$head); |
| 1002 | |
| 1003 | # Then include any headers from the hunk lines, which must |
| 1004 | # come before any actual hunk. |
| 1005 | while (@_ && $_[0] !~ /^@/) { |
| 1006 | push @patch, shift; |
| 1007 | } |
| 1008 | |
| 1009 | # Then begin the diff. |
| 1010 | push @patch, grep { /^[-+]{3}/ } @$head; |
| 1011 | |
| 1012 | # And then the actual hunks. |
| 1013 | push @patch, @_; |
| 1014 | |
| 1015 | return @patch; |
| 1016 | } |
| 1017 | |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1018 | sub color_diff { |
| 1019 | return map { |
| 1020 | colored((/^@/ ? $fraginfo_color : |
| 1021 | /^\+/ ? $diff_new_color : |
| 1022 | /^-/ ? $diff_old_color : |
| 1023 | $diff_plain_color), |
| 1024 | $_); |
| 1025 | } @_; |
| 1026 | } |
| 1027 | |
| 1028 | sub edit_hunk_manually { |
| 1029 | my ($oldtext) = @_; |
| 1030 | |
| 1031 | my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff"; |
| 1032 | my $fh; |
| 1033 | open $fh, '>', $hunkfile |
| 1034 | or die "failed to open hunk edit file for writing: " . $!; |
| 1035 | print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n"; |
| 1036 | print $fh @$oldtext; |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1037 | my $participle = $patch_mode_flavour{PARTICIPLE}; |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 1038 | my $is_reverse = $patch_mode_flavour{IS_REVERSE}; |
| 1039 | my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-'); |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1040 | print $fh <<EOF; |
| 1041 | # --- |
Jonathan "Duke" Leto | 7b8c705 | 2010-10-27 17:49:20 -0700 | [diff] [blame] | 1042 | # To remove '$remove_minus' lines, make them ' ' lines (context). |
| 1043 | # To remove '$remove_plus' lines, delete them. |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1044 | # Lines starting with # will be removed. |
| 1045 | # |
| 1046 | # If the patch applies cleanly, the edited hunk will immediately be |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1047 | # marked for $participle. If it does not apply cleanly, you will be given |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1048 | # an opportunity to edit again. If all lines of the hunk are removed, |
| 1049 | # then the edit is aborted and the hunk is left unchanged. |
| 1050 | EOF |
| 1051 | close $fh; |
| 1052 | |
Jonathan Nieder | b4479f0 | 2009-10-30 20:42:34 -0500 | [diff] [blame] | 1053 | chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR))); |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1054 | system('sh', '-c', $editor.' "$@"', $editor, $hunkfile); |
| 1055 | |
Deskin Miller | 1d398a0 | 2009-02-12 00:19:41 -0500 | [diff] [blame] | 1056 | if ($? != 0) { |
| 1057 | return undef; |
| 1058 | } |
| 1059 | |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1060 | open $fh, '<', $hunkfile |
| 1061 | or die "failed to open hunk edit file for reading: " . $!; |
| 1062 | my @newtext = grep { !/^#/ } <$fh>; |
| 1063 | close $fh; |
| 1064 | unlink $hunkfile; |
| 1065 | |
| 1066 | # Abort if nothing remains |
| 1067 | if (!grep { /\S/ } @newtext) { |
| 1068 | return undef; |
| 1069 | } |
| 1070 | |
| 1071 | # Reinsert the first hunk header if the user accidentally deleted it |
| 1072 | if ($newtext[0] !~ /^@/) { |
| 1073 | unshift @newtext, $oldtext->[0]; |
| 1074 | } |
| 1075 | return \@newtext; |
| 1076 | } |
| 1077 | |
| 1078 | sub diff_applies { |
Junio C Hamano | 9dce832 | 2011-04-06 14:12:34 -0700 | [diff] [blame] | 1079 | return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check', |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1080 | map { @{$_->{TEXT}} } @_); |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1083 | sub _restore_terminal_and_die { |
| 1084 | ReadMode 'restore'; |
| 1085 | print "\n"; |
| 1086 | exit 1; |
| 1087 | } |
| 1088 | |
| 1089 | sub prompt_single_character { |
| 1090 | if ($use_readkey) { |
| 1091 | local $SIG{TERM} = \&_restore_terminal_and_die; |
| 1092 | local $SIG{INT} = \&_restore_terminal_and_die; |
| 1093 | ReadMode 'cbreak'; |
| 1094 | my $key = ReadKey 0; |
| 1095 | ReadMode 'restore'; |
Thomas Rast | b5cc003 | 2011-05-17 17:19:08 +0200 | [diff] [blame] | 1096 | if ($use_termcap and $key eq "\e") { |
| 1097 | while (!defined $term_escapes{$key}) { |
| 1098 | my $next = ReadKey 0.5; |
| 1099 | last if (!defined $next); |
| 1100 | $key .= $next; |
| 1101 | } |
| 1102 | $key =~ s/\e/^[/; |
| 1103 | } |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1104 | print "$key" if defined $key; |
| 1105 | print "\n"; |
| 1106 | return $key; |
| 1107 | } else { |
| 1108 | return <STDIN>; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1112 | sub prompt_yesno { |
| 1113 | my ($prompt) = @_; |
| 1114 | while (1) { |
| 1115 | print colored $prompt_color, $prompt; |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1116 | my $line = prompt_single_character; |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1117 | return 0 if $line =~ /^n/i; |
| 1118 | return 1 if $line =~ /^y/i; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | sub edit_hunk_loop { |
| 1123 | my ($head, $hunk, $ix) = @_; |
| 1124 | my $text = $hunk->[$ix]->{TEXT}; |
| 1125 | |
| 1126 | while (1) { |
| 1127 | $text = edit_hunk_manually($text); |
| 1128 | if (!defined $text) { |
| 1129 | return undef; |
| 1130 | } |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1131 | my $newhunk = { |
| 1132 | TEXT => $text, |
| 1133 | TYPE => $hunk->[$ix]->{TYPE}, |
Junio C Hamano | 7a26e65 | 2009-05-16 10:48:23 -0700 | [diff] [blame] | 1134 | USE => 1, |
| 1135 | DIRTY => 1, |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1136 | }; |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1137 | if (diff_applies($head, |
| 1138 | @{$hunk}[0..$ix-1], |
| 1139 | $newhunk, |
| 1140 | @{$hunk}[$ix+1..$#{$hunk}])) { |
| 1141 | $newhunk->{DISPLAY} = [color_diff(@{$text})]; |
| 1142 | return $newhunk; |
| 1143 | } |
| 1144 | else { |
| 1145 | prompt_yesno( |
| 1146 | 'Your edited hunk does not apply. Edit again ' |
| 1147 | . '(saying "no" discards!) [y/n]? ' |
| 1148 | ) or return undef; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1153 | sub help_patch_cmd { |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1154 | my $verb = lc $patch_mode_flavour{VERB}; |
| 1155 | my $target = $patch_mode_flavour{TARGET}; |
| 1156 | print colored $help_color, <<EOF ; |
| 1157 | y - $verb this hunk$target |
| 1158 | n - do not $verb this hunk$target |
Jonathan Nieder | 74e42ce | 2010-06-12 22:32:51 -0500 | [diff] [blame] | 1159 | q - quit; do not $verb this hunk nor any of the remaining ones |
| 1160 | a - $verb this hunk and all later hunks in the file |
| 1161 | d - do not $verb this hunk nor any of the later hunks in the file |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1162 | g - select a hunk to go to |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1163 | / - search for a hunk matching the given regex |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1164 | j - leave this hunk undecided, see next undecided hunk |
| 1165 | J - leave this hunk undecided, see next hunk |
| 1166 | k - leave this hunk undecided, see previous undecided hunk |
| 1167 | K - leave this hunk undecided, see previous hunk |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1168 | s - split the current hunk into smaller hunks |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1169 | e - manually edit the current hunk |
Ralf Wildenhues | 280e50c | 2007-11-28 19:21:42 +0100 | [diff] [blame] | 1170 | ? - print help |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1171 | EOF |
| 1172 | } |
| 1173 | |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1174 | sub apply_patch { |
| 1175 | my $cmd = shift; |
Junio C Hamano | 9dce832 | 2011-04-06 14:12:34 -0700 | [diff] [blame] | 1176 | my $ret = run_git_apply $cmd, @_; |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1177 | if (!$ret) { |
| 1178 | print STDERR @_; |
| 1179 | } |
| 1180 | return $ret; |
| 1181 | } |
| 1182 | |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1183 | sub apply_patch_for_checkout_commit { |
| 1184 | my $reverse = shift; |
Junio C Hamano | 9dce832 | 2011-04-06 14:12:34 -0700 | [diff] [blame] | 1185 | my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_; |
| 1186 | my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_; |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1187 | |
| 1188 | if ($applies_worktree && $applies_index) { |
Junio C Hamano | 9dce832 | 2011-04-06 14:12:34 -0700 | [diff] [blame] | 1189 | run_git_apply 'apply '.$reverse.' --cached', @_; |
| 1190 | run_git_apply 'apply '.$reverse, @_; |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1191 | return 1; |
| 1192 | } elsif (!$applies_index) { |
| 1193 | print colored $error_color, "The selected hunks do not apply to the index!\n"; |
| 1194 | if (prompt_yesno "Apply them to the worktree anyway? ") { |
Junio C Hamano | 9dce832 | 2011-04-06 14:12:34 -0700 | [diff] [blame] | 1195 | return run_git_apply 'apply '.$reverse, @_; |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1196 | } else { |
| 1197 | print colored $error_color, "Nothing was applied.\n"; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | } else { |
| 1201 | print STDERR @_; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1206 | sub patch_update_cmd { |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1207 | my @all_mods = list_modified($patch_mode_flavour{FILTER}); |
Jeff King | 4066bd6 | 2012-04-05 08:30:08 -0400 | [diff] [blame] | 1208 | error_msg "ignoring unmerged: $_->{VALUE}\n" |
| 1209 | for grep { $_->{UNMERGED} } @all_mods; |
| 1210 | @all_mods = grep { !$_->{UNMERGED} } @all_mods; |
| 1211 | |
Thomas Rast | 9fe7a64 | 2008-10-26 20:37:06 +0100 | [diff] [blame] | 1212 | my @mods = grep { !($_->{BINARY}) } @all_mods; |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1213 | my @them; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1214 | |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1215 | if (!@mods) { |
Thomas Rast | 9fe7a64 | 2008-10-26 20:37:06 +0100 | [diff] [blame] | 1216 | if (@all_mods) { |
| 1217 | print STDERR "Only binary files changed.\n"; |
| 1218 | } else { |
| 1219 | print STDERR "No changes.\n"; |
| 1220 | } |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | if ($patch_mode) { |
| 1224 | @them = @mods; |
| 1225 | } |
| 1226 | else { |
| 1227 | @them = list_and_choose({ PROMPT => 'Patch update', |
| 1228 | HEADER => $status_head, }, |
| 1229 | @mods); |
| 1230 | } |
Junio C Hamano | 12db334 | 2007-11-22 01:47:13 -0800 | [diff] [blame] | 1231 | for (@them) { |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1232 | return 0 if patch_update_file($_->{VALUE}); |
Junio C Hamano | 12db334 | 2007-11-22 01:47:13 -0800 | [diff] [blame] | 1233 | } |
Wincent Colaiuta | a7d9da6 | 2007-11-21 13:36:38 +0100 | [diff] [blame] | 1234 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1235 | |
William Pursell | 3f6aff6 | 2008-12-04 10:00:24 +0000 | [diff] [blame] | 1236 | # Generate a one line summary of a hunk. |
| 1237 | sub summarize_hunk { |
| 1238 | my $rhunk = shift; |
| 1239 | my $summary = $rhunk->{TEXT}[0]; |
| 1240 | |
| 1241 | # Keep the line numbers, discard extra context. |
| 1242 | $summary =~ s/@@(.*?)@@.*/$1 /s; |
| 1243 | $summary .= " " x (20 - length $summary); |
| 1244 | |
| 1245 | # Add some user context. |
| 1246 | for my $line (@{$rhunk->{TEXT}}) { |
| 1247 | if ($line =~ m/^[+-].*\w/) { |
| 1248 | $summary .= $line; |
| 1249 | last; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | chomp $summary; |
| 1254 | return substr($summary, 0, 80) . "\n"; |
| 1255 | } |
| 1256 | |
| 1257 | |
| 1258 | # Print a one-line summary of each hunk in the array ref in |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 1259 | # the first argument, starting with the index in the 2nd. |
William Pursell | 3f6aff6 | 2008-12-04 10:00:24 +0000 | [diff] [blame] | 1260 | sub display_hunks { |
| 1261 | my ($hunks, $i) = @_; |
| 1262 | my $ctr = 0; |
| 1263 | $i ||= 0; |
| 1264 | for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) { |
| 1265 | my $status = " "; |
| 1266 | if (defined $hunks->[$i]{USE}) { |
| 1267 | $status = $hunks->[$i]{USE} ? "+" : "-"; |
| 1268 | } |
| 1269 | printf "%s%2d: %s", |
| 1270 | $status, |
| 1271 | $i + 1, |
| 1272 | summarize_hunk($hunks->[$i]); |
| 1273 | } |
| 1274 | return $i; |
| 1275 | } |
| 1276 | |
Wincent Colaiuta | a7d9da6 | 2007-11-21 13:36:38 +0100 | [diff] [blame] | 1277 | sub patch_update_file { |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1278 | my $quit = 0; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1279 | my ($ix, $num); |
Wincent Colaiuta | a7d9da6 | 2007-11-21 13:36:38 +0100 | [diff] [blame] | 1280 | my $path = shift; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1281 | my ($head, @hunk) = parse_diff($path); |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 1282 | ($head, my $mode, my $deletion) = parse_diff_header($head); |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 1283 | for (@{$head->{DISPLAY}}) { |
| 1284 | print; |
| 1285 | } |
Jeff King | ca72468 | 2008-03-27 03:32:25 -0400 | [diff] [blame] | 1286 | |
| 1287 | if (@{$mode->{TEXT}}) { |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1288 | unshift @hunk, $mode; |
Jeff King | ca72468 | 2008-03-27 03:32:25 -0400 | [diff] [blame] | 1289 | } |
Jeff King | 8947fdd | 2009-12-08 02:49:35 -0500 | [diff] [blame] | 1290 | if (@{$deletion->{TEXT}}) { |
| 1291 | foreach my $hunk (@hunk) { |
| 1292 | push @{$deletion->{TEXT}}, @{$hunk->{TEXT}}; |
| 1293 | push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}}; |
| 1294 | } |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 1295 | @hunk = ($deletion); |
| 1296 | } |
Jeff King | ca72468 | 2008-03-27 03:32:25 -0400 | [diff] [blame] | 1297 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1298 | $num = scalar @hunk; |
| 1299 | $ix = 0; |
| 1300 | |
| 1301 | while (1) { |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1302 | my ($prev, $next, $other, $undecided, $i); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1303 | $other = ''; |
| 1304 | |
| 1305 | if ($num <= $ix) { |
| 1306 | $ix = 0; |
| 1307 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1308 | for ($i = 0; $i < $ix; $i++) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1309 | if (!defined $hunk[$i]{USE}) { |
| 1310 | $prev = 1; |
William Pursell | 57886bc | 2008-11-27 04:07:52 +0000 | [diff] [blame] | 1311 | $other .= ',k'; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1312 | last; |
| 1313 | } |
| 1314 | } |
| 1315 | if ($ix) { |
William Pursell | 57886bc | 2008-11-27 04:07:52 +0000 | [diff] [blame] | 1316 | $other .= ',K'; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1317 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1318 | for ($i = $ix + 1; $i < $num; $i++) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1319 | if (!defined $hunk[$i]{USE}) { |
| 1320 | $next = 1; |
William Pursell | 57886bc | 2008-11-27 04:07:52 +0000 | [diff] [blame] | 1321 | $other .= ',j'; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1322 | last; |
| 1323 | } |
| 1324 | } |
| 1325 | if ($ix < $num - 1) { |
William Pursell | 57886bc | 2008-11-27 04:07:52 +0000 | [diff] [blame] | 1326 | $other .= ',J'; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1327 | } |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1328 | if ($num > 1) { |
Thomas Rast | 4404b2e | 2009-02-02 22:46:28 +0100 | [diff] [blame] | 1329 | $other .= ',g'; |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1330 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1331 | for ($i = 0; $i < $num; $i++) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1332 | if (!defined $hunk[$i]{USE}) { |
| 1333 | $undecided = 1; |
| 1334 | last; |
| 1335 | } |
| 1336 | } |
| 1337 | last if (!$undecided); |
| 1338 | |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1339 | if ($hunk[$ix]{TYPE} eq 'hunk' && |
| 1340 | hunk_splittable($hunk[$ix]{TEXT})) { |
William Pursell | 57886bc | 2008-11-27 04:07:52 +0000 | [diff] [blame] | 1341 | $other .= ',s'; |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1342 | } |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1343 | if ($hunk[$ix]{TYPE} eq 'hunk') { |
| 1344 | $other .= ',e'; |
| 1345 | } |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 1346 | for (@{$hunk[$ix]{DISPLAY}}) { |
| 1347 | print; |
| 1348 | } |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1349 | print colored $prompt_color, $patch_mode_flavour{VERB}, |
Jeff King | 24ab81a | 2009-10-27 20:52:57 -0400 | [diff] [blame] | 1350 | ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' : |
| 1351 | $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' : |
| 1352 | ' this hunk'), |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1353 | $patch_mode_flavour{TARGET}, |
Wincent Colaiuta | a2fc8d6 | 2009-04-20 11:42:52 +0200 | [diff] [blame] | 1354 | " [y,n,q,a,d,/$other,?]? "; |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1355 | my $line = prompt_single_character; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1356 | if ($line) { |
| 1357 | if ($line =~ /^y/i) { |
| 1358 | $hunk[$ix]{USE} = 1; |
| 1359 | } |
| 1360 | elsif ($line =~ /^n/i) { |
| 1361 | $hunk[$ix]{USE} = 0; |
| 1362 | } |
| 1363 | elsif ($line =~ /^a/i) { |
| 1364 | while ($ix < $num) { |
| 1365 | if (!defined $hunk[$ix]{USE}) { |
| 1366 | $hunk[$ix]{USE} = 1; |
| 1367 | } |
| 1368 | $ix++; |
| 1369 | } |
| 1370 | next; |
| 1371 | } |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1372 | elsif ($other =~ /g/ && $line =~ /^g(.*)/) { |
| 1373 | my $response = $1; |
| 1374 | my $no = $ix > 10 ? $ix - 10 : 0; |
| 1375 | while ($response eq '') { |
| 1376 | my $extra = ""; |
| 1377 | $no = display_hunks(\@hunk, $no); |
| 1378 | if ($no < $num) { |
| 1379 | $extra = " (<ret> to see more)"; |
| 1380 | } |
| 1381 | print "go to which hunk$extra? "; |
| 1382 | $response = <STDIN>; |
Thomas Rast | 68c02d7 | 2009-02-02 22:46:29 +0100 | [diff] [blame] | 1383 | if (!defined $response) { |
| 1384 | $response = ''; |
| 1385 | } |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1386 | chomp $response; |
| 1387 | } |
| 1388 | if ($response !~ /^\s*\d+\s*$/) { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1389 | error_msg "Invalid number: '$response'\n"; |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1390 | } elsif (0 < $response && $response <= $num) { |
| 1391 | $ix = $response - 1; |
| 1392 | } else { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1393 | error_msg "Sorry, only $num hunks available.\n"; |
William Pursell | 070434d | 2008-12-04 10:22:40 +0000 | [diff] [blame] | 1394 | } |
| 1395 | next; |
| 1396 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1397 | elsif ($line =~ /^d/i) { |
| 1398 | while ($ix < $num) { |
| 1399 | if (!defined $hunk[$ix]{USE}) { |
| 1400 | $hunk[$ix]{USE} = 0; |
| 1401 | } |
| 1402 | $ix++; |
| 1403 | } |
| 1404 | next; |
| 1405 | } |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1406 | elsif ($line =~ /^q/i) { |
Junio C Hamano | f5ea3f2 | 2011-04-29 15:12:32 -0700 | [diff] [blame] | 1407 | for ($i = 0; $i < $num; $i++) { |
| 1408 | if (!defined $hunk[$i]{USE}) { |
| 1409 | $hunk[$i]{USE} = 0; |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1410 | } |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1411 | } |
| 1412 | $quit = 1; |
Junio C Hamano | f5ea3f2 | 2011-04-29 15:12:32 -0700 | [diff] [blame] | 1413 | last; |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1414 | } |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1415 | elsif ($line =~ m|^/(.*)|) { |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1416 | my $regex = $1; |
| 1417 | if ($1 eq "") { |
| 1418 | print colored $prompt_color, "search for regex? "; |
| 1419 | $regex = <STDIN>; |
| 1420 | if (defined $regex) { |
| 1421 | chomp $regex; |
| 1422 | } |
| 1423 | } |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1424 | my $search_string; |
| 1425 | eval { |
Thomas Rast | ca6ac7f | 2009-02-05 09:28:26 +0100 | [diff] [blame] | 1426 | $search_string = qr{$regex}m; |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1427 | }; |
| 1428 | if ($@) { |
| 1429 | my ($err,$exp) = ($@, $1); |
| 1430 | $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//; |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1431 | error_msg "Malformed search regexp $exp: $err\n"; |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1432 | next; |
| 1433 | } |
| 1434 | my $iy = $ix; |
| 1435 | while (1) { |
| 1436 | my $text = join ("", @{$hunk[$iy]{TEXT}}); |
| 1437 | last if ($text =~ $search_string); |
| 1438 | $iy++; |
| 1439 | $iy = 0 if ($iy >= $num); |
| 1440 | if ($ix == $iy) { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1441 | error_msg "No hunk matches the given pattern\n"; |
William Pursell | dd971cc | 2008-11-27 04:07:57 +0000 | [diff] [blame] | 1442 | last; |
| 1443 | } |
| 1444 | } |
| 1445 | $ix = $iy; |
| 1446 | next; |
| 1447 | } |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1448 | elsif ($line =~ /^K/) { |
| 1449 | if ($other =~ /K/) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1450 | $ix--; |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1451 | } |
| 1452 | else { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1453 | error_msg "No previous hunk\n"; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1454 | } |
| 1455 | next; |
| 1456 | } |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1457 | elsif ($line =~ /^J/) { |
| 1458 | if ($other =~ /J/) { |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1459 | $ix++; |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1460 | } |
| 1461 | else { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1462 | error_msg "No next hunk\n"; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1463 | } |
| 1464 | next; |
| 1465 | } |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1466 | elsif ($line =~ /^k/) { |
| 1467 | if ($other =~ /k/) { |
| 1468 | while (1) { |
| 1469 | $ix--; |
| 1470 | last if (!$ix || |
| 1471 | !defined $hunk[$ix]{USE}); |
| 1472 | } |
| 1473 | } |
| 1474 | else { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1475 | error_msg "No previous hunk\n"; |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1476 | } |
| 1477 | next; |
| 1478 | } |
| 1479 | elsif ($line =~ /^j/) { |
| 1480 | if ($other !~ /j/) { |
Thomas Rast | a301973 | 2009-02-05 09:28:27 +0100 | [diff] [blame] | 1481 | error_msg "No next hunk\n"; |
William Pursell | ace30ba | 2008-11-27 04:08:03 +0000 | [diff] [blame] | 1482 | next; |
| 1483 | } |
| 1484 | } |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1485 | elsif ($other =~ /s/ && $line =~ /^s/) { |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 1486 | my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY}); |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1487 | if (1 < @split) { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 1488 | print colored $header_color, "Split into ", |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1489 | scalar(@split), " hunks.\n"; |
| 1490 | } |
Wincent Colaiuta | 4af756f | 2007-12-07 13:35:10 +0100 | [diff] [blame] | 1491 | splice (@hunk, $ix, 1, @split); |
Junio C Hamano | 835b2ae | 2006-12-11 17:09:26 -0800 | [diff] [blame] | 1492 | $num = scalar @hunk; |
| 1493 | next; |
| 1494 | } |
Jeff King | 0392513 | 2009-04-16 03:14:15 -0400 | [diff] [blame] | 1495 | elsif ($other =~ /e/ && $line =~ /^e/) { |
Thomas Rast | ac083c4 | 2008-07-03 00:00:00 +0200 | [diff] [blame] | 1496 | my $newhunk = edit_hunk_loop($head, \@hunk, $ix); |
| 1497 | if (defined $newhunk) { |
| 1498 | splice @hunk, $ix, 1, $newhunk; |
| 1499 | } |
| 1500 | } |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1501 | else { |
| 1502 | help_patch_cmd($other); |
| 1503 | next; |
| 1504 | } |
| 1505 | # soft increment |
| 1506 | while (1) { |
| 1507 | $ix++; |
| 1508 | last if ($ix >= $num || |
| 1509 | !defined $hunk[$ix]{USE}); |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
Junio C Hamano | 7a26e65 | 2009-05-16 10:48:23 -0700 | [diff] [blame] | 1514 | @hunk = coalesce_overlapping_hunks(@hunk); |
| 1515 | |
Jean-Luc Herren | 7b40a45 | 2007-10-09 21:34:17 +0200 | [diff] [blame] | 1516 | my $n_lofs = 0; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1517 | my @result = (); |
| 1518 | for (@hunk) { |
Thomas Rast | 8cbd431 | 2008-07-02 23:59:16 +0200 | [diff] [blame] | 1519 | if ($_->{USE}) { |
| 1520 | push @result, @{$_->{TEXT}}; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | if (@result) { |
Jeff King | e1327ed | 2010-02-22 20:05:44 -0500 | [diff] [blame] | 1525 | my @patch = reassemble_patch($head->{TEXT}, @result); |
Thomas Rast | 8f0bef6 | 2009-08-13 14:29:39 +0200 | [diff] [blame] | 1526 | my $apply_routine = $patch_mode_flavour{APPLY}; |
| 1527 | &$apply_routine(@patch); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1528 | refresh(); |
| 1529 | } |
| 1530 | |
| 1531 | print "\n"; |
Matthieu Moy | 9a7a1e0 | 2009-04-10 16:57:01 +0200 | [diff] [blame] | 1532 | return $quit; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | sub diff_cmd { |
| 1536 | my @mods = list_modified('index-only'); |
| 1537 | @mods = grep { !($_->{BINARY}) } @mods; |
| 1538 | return if (!@mods); |
| 1539 | my (@them) = list_and_choose({ PROMPT => 'Review diff', |
| 1540 | IMMEDIATE => 1, |
| 1541 | HEADER => $status_head, }, |
| 1542 | @mods); |
| 1543 | return if (!@them); |
Jeff King | 18bc761 | 2008-02-13 05:50:51 -0500 | [diff] [blame] | 1544 | my $reference = is_initial_commit() ? get_empty_tree() : 'HEAD'; |
| 1545 | system(qw(git diff -p --cached), $reference, '--', |
| 1546 | map { $_->{VALUE} } @them); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | sub quit_cmd { |
| 1550 | print "Bye.\n"; |
| 1551 | exit(0); |
| 1552 | } |
| 1553 | |
| 1554 | sub help_cmd { |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 1555 | print colored $help_color, <<\EOF ; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1556 | status - show paths with changes |
| 1557 | update - add working tree state to the staged set of changes |
| 1558 | revert - revert staged set of changes back to the HEAD version |
| 1559 | patch - pick hunks and update selectively |
| 1560 | diff - view diff between HEAD and index |
| 1561 | add untracked - add contents of untracked files to the staged set of changes |
| 1562 | EOF |
| 1563 | } |
| 1564 | |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1565 | sub process_args { |
| 1566 | return unless @ARGV; |
| 1567 | my $arg = shift @ARGV; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 1568 | if ($arg =~ /--patch(?:=(.*))?/) { |
| 1569 | if (defined $1) { |
| 1570 | if ($1 eq 'reset') { |
| 1571 | $patch_mode = 'reset_head'; |
| 1572 | $patch_mode_revision = 'HEAD'; |
| 1573 | $arg = shift @ARGV or die "missing --"; |
| 1574 | if ($arg ne '--') { |
| 1575 | $patch_mode_revision = $arg; |
| 1576 | $patch_mode = ($arg eq 'HEAD' ? |
| 1577 | 'reset_head' : 'reset_nothead'); |
| 1578 | $arg = shift @ARGV or die "missing --"; |
| 1579 | } |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1580 | } elsif ($1 eq 'checkout') { |
| 1581 | $arg = shift @ARGV or die "missing --"; |
| 1582 | if ($arg eq '--') { |
| 1583 | $patch_mode = 'checkout_index'; |
| 1584 | } else { |
| 1585 | $patch_mode_revision = $arg; |
| 1586 | $patch_mode = ($arg eq 'HEAD' ? |
| 1587 | 'checkout_head' : 'checkout_nothead'); |
| 1588 | $arg = shift @ARGV or die "missing --"; |
| 1589 | } |
Thomas Rast | dda1f2a | 2009-08-13 14:29:44 +0200 | [diff] [blame] | 1590 | } elsif ($1 eq 'stage' or $1 eq 'stash') { |
| 1591 | $patch_mode = $1; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 1592 | $arg = shift @ARGV or die "missing --"; |
| 1593 | } else { |
| 1594 | die "unknown --patch mode: $1"; |
| 1595 | } |
| 1596 | } else { |
| 1597 | $patch_mode = 'stage'; |
| 1598 | $arg = shift @ARGV or die "missing --"; |
| 1599 | } |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1600 | die "invalid argument $arg, expecting --" |
| 1601 | unless $arg eq "--"; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 1602 | %patch_mode_flavour = %{$patch_modes{$patch_mode}}; |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1603 | } |
| 1604 | elsif ($arg ne "--") { |
| 1605 | die "invalid argument $arg, expecting --"; |
| 1606 | } |
| 1607 | } |
| 1608 | |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1609 | sub main_loop { |
| 1610 | my @cmd = ([ 'status', \&status_cmd, ], |
| 1611 | [ 'update', \&update_cmd, ], |
| 1612 | [ 'revert', \&revert_cmd, ], |
| 1613 | [ 'add untracked', \&add_untracked_cmd, ], |
| 1614 | [ 'patch', \&patch_update_cmd, ], |
| 1615 | [ 'diff', \&diff_cmd, ], |
| 1616 | [ 'quit', \&quit_cmd, ], |
| 1617 | [ 'help', \&help_cmd, ], |
| 1618 | ); |
| 1619 | while (1) { |
| 1620 | my ($it) = list_and_choose({ PROMPT => 'What now', |
| 1621 | SINGLETON => 1, |
| 1622 | LIST_FLAT => 4, |
| 1623 | HEADER => '*** Commands ***', |
Jean-Luc Herren | c95c024 | 2007-09-26 15:56:19 +0200 | [diff] [blame] | 1624 | ON_EOF => \&quit_cmd, |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1625 | IMMEDIATE => 1 }, @cmd); |
| 1626 | if ($it) { |
| 1627 | eval { |
| 1628 | $it->[1]->(); |
| 1629 | }; |
| 1630 | if ($@) { |
| 1631 | print "$@"; |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1637 | process_args(); |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 1638 | refresh(); |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 1639 | if ($patch_mode) { |
| 1640 | patch_update_cmd(); |
| 1641 | } |
| 1642 | else { |
| 1643 | status_cmd(); |
| 1644 | main_loop(); |
| 1645 | } |