git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: summaries in git add --patch

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 28, 2008, 07:24 UTC
Message-ID
<7v8wr48g98.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<492F92C9.7030301@gmail.com>
William Pursell <bill.pursell@gmail.com> writes:
> Here's a new patch.  Instead of displaying the summary and then
> the current hunk, it implements a 'goto' command.
I take it that this is for discussion not for immediate inclusion.
Show 8 quoted lines
> @@ -799,6 +801,7 @@ sub help_patch_cmd {
>  y - stage this hunk
>  n - do not stage this hunk
>  a - stage this and all the remaining hunks in the file
> +g - select a hunk to jump to
>  d - do not stage this hunk nor any of the remaining hunks in the file
>  j - leave this hunk undecided, see next undecided hunk
>  J - leave this hunk undecided, see next hunk

Since you took 'g' after "go to", help text should also say "go to", instead of "jump to" for the mnemonics value, iow, to help people remember.

Show 14 quoted lines
> @@ -836,6 +839,27 @@ sub patch_update_cmd {
>  	}
>  }
>
> +sub select_new_hunk {
> +	my $ri = shift;
> +	my @hunk = @_;
> +	my ($i, $response);
> +	print "   '+' stage, '-' don't stage\n";
> +	for ( $i = 0; $i < @hunk; $i++ ) {
> +		my $status = " ";
> +		if( defined $hunk[$i]{USE} ) {
> +			$status = $hunk[$i]{USE} ? "+" : "-";
> +		}
Style.
    (1) SP between language construct and open parenthesis, as opposed to
        no extra SP between function name and open parenthesis;
    (2) No extra SP around what is enclosed in parentheses.
Show 5 quoted lines
> +		printf "%s%3d: %s",
> +			$status,
> +			$i + 1,
> +			$hunk[$i]{SUMMARY};
> +	}

I think this "for ()" loop part, including the comment about +/- notation, should be separated into a function so that you can implement a separate "l"ist command like you did in the other patch, using the same function.

> +	printf "goto which hunk? ";
> +	$response = <STDIN>;
> +	chomp $response;
> +	$$ri = $response - 1;

What happens when $response is (1) a non number, (2) outside range (both negative and positive), or (3) EOF?

Sending ref to scalar and returning the value by assigning is a bad taste. Why shouldn't this function just return an integer to be assigned to $ix by the caller? If you want to use pass-by-ref to show off your Perl-fu, I think \@hunk would be what you would want to for performance reasons.

Show 6 quoted lines
> @@ -919,7 +943,7 @@ sub patch_update_file {
>  		for (@{$hunk[$ix]{DISPLAY}}) {
>  			print;
>  		}
> -		print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
> +		print colored $prompt_color, "Stage this hunk [y/n/a/d/g$other/?]? ";

When there is only one hunk, we do not give j nor k. Should we give g in such a case? Why?

Show 12 quoted lines
> @@ -937,6 +961,16 @@ sub patch_update_file {
>  				}
>  				next;
>  			}
> +			elsif ($line =~ /^g/) {
> +				chomp ($line);
> +				if ($line =~ /^g$/) {
> +					select_new_hunk (\$ix, @hunk);
> +				}
> +				else {
> +					$ix = (substr $line, 1) - 1;
> +				}
The same "input validation" issue exists here.  it would make sense to:
 - Make choose_hunk(@hunk) that calls list_hunks(@hunk) that gives the
   summary, reads one line, and returns that line;
 - Make the caller here to look like this:
	elsif ($line =~ s/^g//) {
        	chomp($line);
                if ($line eq '') {
                	$line = choose_hunk(@hunk);
		}
		if ($line !~ /^\d+$/) {
			print STDERR "Eh '$line', what number is that?\n";
                        next;
		} elsif (0 < $line && $line <= $num) {
			$ix = $line - 1;
                } else {
                	print STDERR "Sorry, you have only $num hunks\n";
                }
	}
Show 9 quoted lines
> +				next;
> +			}
>  			elsif ($line =~ /^d/i) {
>  				while ($ix < $num) {
>  					if (!defined $hunk[$ix]{USE}) {
>
>
> -- 
> William Pursell
Previous: William PursellNext: William Pursell
Message 6 of 19 in “summaries in git add --patch”
  1. William PursellNov 27, 2008
  2. Jakub NarebskiNov 27, 2008
  3. Junio C HamanoNov 28, 2008
  4. William PursellNov 28, 2008
  5. William PursellNov 28, 2008
  6. Junio C HamanoNov 28, 2008
  7. William PursellNov 29, 2008
  8. Junio C HamanoDec 3, 2008
  9. William PursellDec 3, 2008
  10. Junio C HamanoDec 3, 2008
  11. William PursellDec 4, 2008
  12. Junio C HamanoDec 4, 2008
  13. William PursellDec 4, 2008
  14. Junio C HamanoDec 5, 2008
  15. William PursellDec 3, 2008
  16. Junio C HamanoDec 3, 2008
  17. William PursellDec 4, 2008
  18. Junio C HamanoDec 4, 2008
  19. William PursellDec 4, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.