7 open($fh, '-|', @_) or die;
11 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
13 if (!defined $GIT_DIR) {
14 exit(1); # rev-parse would have already said "not a git repo"
20 open $fh, '-|', qw(git update-index --refresh)
23 ;# ignore 'needs update'
33 run_cmd_pipe
(qw(git ls-files --others
34 --exclude-per-directory=.gitignore),
35 "--exclude-from=$GIT_DIR/info/exclude",
39 my $status_fmt = '%12s %12s %s';
40 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
42 # Returns list of hashes, contents of each of which are:
43 # PRINT: print message
45 # BINARY: is a binary path
46 # INDEX: is index different from HEAD?
47 # FILE: is file different from index?
48 # INDEX_ADDDEL: is it add/delete between HEAD and index?
49 # FILE_ADDDEL: is it add/delete between index and file?
54 my ($add, $del, $adddel, $file);
56 for (run_cmd_pipe
(qw(git diff-index --cached
57 --numstat --summary HEAD))) {
58 if (($add, $del, $file) =
59 /^([-\d]+) ([-\d]+) (.*)/) {
61 if ($add eq '-' && $del eq '-') {
66 $change = "+$add/-$del";
74 elsif (($adddel, $file) =
75 /^ (create|delete) mode [0-7]+ (.*)$/) {
76 $data{$file}{INDEX_ADDDEL
} = $adddel;
80 for (run_cmd_pipe
(qw(git diff-files --numstat --summary))) {
81 if (($add, $del, $file) =
82 /^([-\d]+) ([-\d]+) (.*)/) {
83 if (!exists $data{$file}) {
90 if ($add eq '-' && $del eq '-') {
95 $change = "+$add/-$del";
97 $data{$file}{FILE
} = $change;
99 $data{$file}{BINARY
} = 1;
102 elsif (($adddel, $file) =
103 /^ (create|delete) mode [0-7]+ (.*)$/) {
104 $data{$file}{FILE_ADDDEL
} = $adddel;
108 for (sort keys %data) {
112 if ($only eq 'index-only') {
113 next if ($it->{INDEX
} eq 'unchanged');
115 if ($only eq 'file-only') {
116 next if ($it->{FILE
} eq 'nothing');
121 PRINT
=> (sprintf $status_fmt,
122 $it->{INDEX
}, $it->{FILE
}, $_),
130 my ($string, @stuff) = @_;
132 for (my $i = 0; $i < @stuff; $i++) {
136 if ((ref $it) eq 'ARRAY') {
144 if ($it =~ /^$string/) {
148 if (defined $hit && defined $found) {
158 sub list_and_choose
{
159 my ($opts, @stuff) = @_;
160 my (@chosen, @return);
167 if ($opts->{HEADER
}) {
168 if (!$opts->{LIST_FLAT
}) {
171 print "$opts->{HEADER}\n";
173 for ($i = 0; $i < @stuff; $i++) {
174 my $chosen = $chosen[$i] ?
'*' : ' ';
175 my $print = $stuff[$i];
177 if ((ref $print) eq 'ARRAY') {
178 $print = $print->[0];
181 $print = $print->{PRINT
};
184 printf("%s%2d: %s", $chosen, $i+1, $print);
185 if (($opts->{LIST_FLAT
}) &&
186 (($i + 1) % ($opts->{LIST_FLAT
}))) {
199 return if ($opts->{LIST_ONLY
});
201 print $opts->{PROMPT
};
202 if ($opts->{SINGLETON
}) {
211 my $donesomething = 0;
212 for my $choice (split(/[\s,]+/, $line)) {
216 # Input that begins with '-'; unchoose
217 if ($choice =~ s/^-//) {
220 # A range can be specified like 5-7
221 if ($choice =~ /^(\d+)-(\d+)$/) {
222 ($bottom, $top) = ($1, $2);
224 elsif ($choice =~ /^\d+$/) {
225 $bottom = $top = $choice;
227 elsif ($choice eq '*') {
232 $bottom = $top = find_unique
($choice, @stuff);
233 if (!defined $bottom) {
234 print "Huh ($choice)?\n";
238 if ($opts->{SINGLETON
} && $bottom != $top) {
239 print "Huh ($choice)?\n";
242 for ($i = $bottom-1; $i <= $top-1; $i++) {
243 next if (@stuff <= $i);
244 $chosen[$i] = $choose;
248 last if (!$donesomething || $opts->{IMMEDIATE
});
250 for ($i = 0; $i < @stuff; $i++) {
252 push @return, $stuff[$i];
259 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
269 print "$cnt paths\n";
277 my @mods = list_modified
('file-only');
280 my @update = list_and_choose
({ PROMPT
=> 'Update',
281 HEADER
=> $status_head, },
284 system(qw(git update-index --add --),
285 map { $_->{VALUE
} } @update);
286 say_n_paths
('updated', @update);
292 my @update = list_and_choose
({ PROMPT
=> 'Revert',
293 HEADER
=> $status_head, },
296 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
297 map { $_->{VALUE
} } @update);
299 open $fh, '|-', qw(git update-index --index-info)
306 if ($_->{INDEX_ADDDEL
} &&
307 $_->{INDEX_ADDDEL
} eq 'create') {
308 system(qw(git update-index --force-remove --),
310 print "note: $_->{VALUE} is untracked now.\n";
314 say_n_paths
('reverted', @update);
319 sub add_untracked_cmd
{
320 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
323 system(qw(git update-index --add --), @add);
324 say_n_paths
('added', @add);
331 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
332 my (@hunk) = { TEXT
=> [] };
336 push @hunk, { TEXT
=> [] };
338 push @
{$hunk[-1]{TEXT
}}, $_;
346 n
- do not stage this hunk
347 a
- stage this
and all the remaining hunks
348 d
- do not stage this hunk nor any of the remaining hunks
349 j
- leave this hunk undecided
, see
next undecided hunk
350 J
- leave this hunk undecided
, see
next hunk
351 k
- leave this hunk undecided
, see previous undecided hunk
352 K
- leave this hunk undecided
, see previous hunk
356 sub patch_update_cmd
{
357 my @mods = list_modified
('file-only');
358 @mods = grep { !($_->{BINARY
}) } @mods;
361 my ($it) = list_and_choose
({ PROMPT
=> 'Patch update',
364 HEADER
=> $status_head, },
369 my $path = $it->{VALUE
};
370 my ($head, @hunk) = parse_diff
($path);
371 for (@
{$head->{TEXT
}}) {
378 my ($prev, $next, $other, $undecided);
384 for (my $i = 0; $i < $ix; $i++) {
385 if (!defined $hunk[$i]{USE
}) {
394 for (my $i = $ix + 1; $i < $num; $i++) {
395 if (!defined $hunk[$i]{USE
}) {
401 if ($ix < $num - 1) {
404 for (my $i = 0; $i < $num; $i++) {
405 if (!defined $hunk[$i]{USE
}) {
410 last if (!$undecided);
412 for (@
{$hunk[$ix]{TEXT
}}) {
415 print "Stage this hunk [y/n/a/d$other/?]? ";
418 if ($line =~ /^y/i) {
421 elsif ($line =~ /^n/i) {
424 elsif ($line =~ /^a/i) {
426 if (!defined $hunk[$ix]{USE
}) {
433 elsif ($line =~ /^d/i) {
435 if (!defined $hunk[$ix]{USE
}) {
442 elsif ($other =~ /K/ && $line =~ /^K/) {
446 elsif ($other =~ /J/ && $line =~ /^J/) {
450 elsif ($other =~ /k/ && $line =~ /^k/) {
454 !defined $hunk[$ix]{USE
});
458 elsif ($other =~ /j/ && $line =~ /^j/) {
461 last if ($ix >= $num ||
462 !defined $hunk[$ix]{USE
});
467 help_patch_cmd
($other);
473 last if ($ix >= $num ||
474 !defined $hunk[$ix]{USE
});
482 my $text = $_->{TEXT
};
483 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
484 $text->[0] =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
486 # Adjust offset here.
499 open $fh, '|-', qw(git apply --cached);
500 for (@
{$head->{TEXT
}}, @result) {
511 my @mods = list_modified
('index-only');
512 @mods = grep { !($_->{BINARY
}) } @mods;
514 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
516 HEADER
=> $status_head, },
519 system(qw(git diff-index -p --cached HEAD --),
520 map { $_->{VALUE
} } @them);
530 status
- show paths with changes
531 update
- add working tree
state to the staged set of changes
532 revert
- revert staged set of changes back to the HEAD version
533 patch
- pick hunks
and update selectively
534 diff
- view diff between HEAD
and index
535 add untracked
- add contents of untracked files to the staged set of changes
540 my @cmd = ([ 'status', \
&status_cmd
, ],
541 [ 'update', \
&update_cmd
, ],
542 [ 'revert', \
&revert_cmd
, ],
543 [ 'add untracked', \
&add_untracked_cmd
, ],
544 [ 'patch', \
&patch_update_cmd
, ],
545 [ 'diff', \
&diff_cmd
, ],
546 [ 'quit', \
&quit_cmd
, ],
547 [ 'help', \
&help_cmd
, ],
550 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
553 HEADER
=> '*** Commands ***',
554 IMMEDIATE
=> 1 }, @cmd);