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