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

Re: [PATCH] contrib: added git-diffall

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 21, 2012, 21:51 UTC
Message-ID
<7vd397g8ic.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1329785969-828-1-git-send-email-tim.henigan@gmail.com>
Tim Henigan <tim.henigan@gmail.com> writes:
Show 7 quoted lines
> diff --git a/contrib/diffall/git-diffall b/contrib/diffall/git-diffall
> new file mode 100755
> index 0000000..ef01eda
> --- /dev/null
> +++ b/contrib/diffall/git-diffall
> @@ -0,0 +1,275 @@
> +#!/bin/bash -e

Does this have to be bash-only (iow, infested with bash-isms beyond repair), or is you wrote this merely from inertia?

The following is only after a cursory scanning, so there may be other things that needs fixing, but anyway:

 - Don't use "which" in scripts.  Its output is not machine parseable, and
   exit code is not reliable, across platforms.  It is only meant for
   consumption by human who can read English (or natural language in the
   current locale).
Show 6 quoted lines
> +				if test -z "$paths"
> +				then
> +					paths=$1
> +				else
> +					paths="$paths $1"
> +				fi

Just a style tip; if you are going to let shell $IFS split this list anyway, it is customary to write the above as

	paths="$paths$1 "
> +		git diff --name-only "$left"..."$right" -- $paths > "$tmp"/filelist
	git diff ... -- $paths >"$tmp/filelist"
Show 14 quoted lines
> +# Exit immediately if there are no diffs
> +if test ! -s "$tmp"/filelist
> +then
> +	exit 0
> +fi
> +
> +if test -n "$copy_back" && test "$right_dir" != "working_tree"
> +then
> +	echo "--copy-back is only valid when diff includes the working tree."
> +	exit 1
> +fi
> +
> +# Create the named tmp directories that will hold the files to be compared
> +mkdir -p "$tmp"/"$left_dir" "$tmp"/"$right_dir"
	mkdir -p "$tmp/$left_dir" "$tmp/$right_dir"
Show 12 quoted lines
> +		if test -n "$compare_staged"
> +		then
> +			ls_list=$(git ls-tree HEAD $name)
> +			if test -n "$ls_list"
> +			then
> +				mkdir -p "$tmp"/"$left_dir"/"$(dirname "$name")"
> +				git show HEAD:"$name" > "$tmp"/"$left_dir"/"$name"
> +				fi
> +			else
> +				mkdir -p "$tmp"/"$left_dir"/"$(dirname "$name")"
> +				git show :"$name" > "$tmp"/"$left_dir"/"$name"
> +		fi

That's misleadingly indented. First I thought "in what case would we want to switch the LHS between HEAD:$path and :$path when doing diff --cached?" but the overindented four lines starting from the funny "fi" is about non cached case.

Show 26 quoted lines
> +	fi
> +done < "$tmp"/filelist
> +
> +cd "$tmp"
> +LOCAL="$left_dir"
> +REMOTE="$right_dir"
> +
> +if test -n "$diff_tool"
> +then
> +	export BASE
> +	eval $diff_tool '"$LOCAL"' '"$REMOTE"'
> +else
> +	run_merge_tool "$merge_tool" false
> +fi
> +
> +# This function is called on exit
> +cleanup () {
> +	cd "$start_dir"
> +
> +	# Copy files back to the working dir, if requested
> +	if test -n "$copy_back" && test "$right_dir" = "working_tree"
> +	then
> +		git_top_dir=$(git rev-parse --show-toplevel)
> +		find "$tmp/$right_dir" -type f|while read file; do
> +			cp "$file" "$git_top_dir/${file#$tmp/$right_dir/}"
> +		done

Why is this loop written in such a dense way? Everything else (except for that misindented part) were almost to our CodingStyle and was fairly easy to read, though.

Show 7 quoted lines
> +	fi
> +
> +	# Remove the tmp directory
> +	rm -rf "$tmp"
> +}
> +
> +trap cleanup EXIT

Does this even trigger? This is not Perl that parses and runs set-up code before executing everything else, so I suspect this last line amounts to the same thing as writing just

	cleanup
without trap nor signal names.

If you are to set up temporary files or directories that you want to clean up, a good discipline is to follow this order:

  - define variable(s) to hold the temporary locations, e.g.
    tmpdir=$(mktemp ...)
  - set the trap before starting to use these temporary locations, e.g.
    trap 'rm -rf "$tmpdir' 0 1 2 3 15
  - and then start populating tmpdir and do whatever you want to do.
Previous: Tim HeniganNext: Tim Henigan
Message 2 of 8 in “contrib: added git-diffall”
  1. contrib: added git-diffallTim Henigan, Feb 21, 2012
  2. Junio C HamanoFeb 21, 2012
  3. Tim HeniganFeb 22, 2012
  4. Junio C HamanoFeb 22, 2012
  5. David AguilarFeb 22, 2012
  6. Tim HeniganFeb 22, 2012
  7. Junio C HamanoFeb 22, 2012
  8. Stefano LattariniFeb 22, 2012

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.