# "git cat" and "git ls"

2 messages from 2006-01-18 to 2006-01-18. Participants: Santi Béjar, Junio C Hamano.
Thread: https://gitlist.dev/t/3097

## Santi Béjar, 2006-01-18 15:10

Subject: "git cat" and "git ls"
Message-ID: <87irsh6087.fsf@gmail.com>
URL: https://gitlist.dev/e/87irsh6087.fsf%40gmail.com

```
Hello,

        following the "git diff" wrapper I've done two new wrappers:

git cat: USAGE='<tag|commit|blob> | [<ent>|-1|-2|-3] -- <file>'

    It shows the content of the giventag/commit/blob.

    You can also ask for a file in a tree or in the index (stage
    optional).

git ls: USAGE='[<ent>] [--] <path>'

    It lists the files in <path> in the <ent> or in the index.

  Santi



#!/bin/sh
#

USAGE='<tag|commit|blob> | [<ent>|-1|-2|-3] -- <file>'
SUBDIRECTORY_OK='Yes'
. git-sh-setup

rev=$(git-rev-parse --revs-only --no-flags --sq "$@") || exit
flags=$(git-rev-parse --no-revs --flags "$@")
files=$(git-rev-parse --no-revs --no-flags --sq "$@")

case "$rev,$files" in
?*' '?*,*|*,?*' '?*)
	usage
	;;
?*,?*)
	sha1=$(eval "git-ls-tree $rev $files" | cut -f 1 | cut -d " " -f 3)
	;;
?*,)
	sha1=$rev
	;;
,?*)
	# Only one file
	[ $(eval "git-ls-files $files | wc -l") != 1 ] && usage
	stg=0
	flag=${flags#-}
	case $flag in
	    1|2|3) stg=$flag;;
	esac
	sha1=$(eval "git-ls-files -s $files" | while read -r mode sha stage name ; do
		[ $stage -eq $stg ] && echo $sha && break ; done)
	;;
*)
	usage
	;;
esac

[ -z $sha1 ] && echo "$(basename $0): $files: Not found" && exit 1
type=$(eval "git-cat-file -t $sha1")

case $type in
    tree)
	usage
	;;
    tag|blob)
	cmd="git-cat-file $type $sha1"
	;;
    commit)
	cmd="git-rev-list --max-count=1 $sha1 --pretty=fuller"
	;;
esac

eval "$cmd"
exit


#!/bin/sh
#

USAGE='[<ent>] [--] <path>'
SUBDIRECTORY_OK='Yes'
. git-sh-setup

tree=$(git-rev-parse --verify $1^{tree} 2>/dev/null)
[ -n "$tree" ] && shift
files=$(git-rev-parse --no-revs --no-flags --sq "$@")

case "$tree" in
"")
        cmd="git-ls-files -s $files"
	;;
?*)
	cmd="git-ls-tree $tree $files"
	;;
esac

eval "$cmd"
exit

```

## Junio C Hamano, 2006-01-18 17:14

Subject: Re: "git cat" and "git ls"
Message-ID: <7vlkxdxxtp.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vlkxdxxtp.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <87irsh6087.fsf@gmail.com>

```
Santi Béjar <sbejar@gmail.com> writes:

>         following the "git diff" wrapper I've done two new wrappers:
>
> git cat: USAGE='<tag|commit|blob> | [<ent>|-1|-2|-3] -- <file>'
> git ls: USAGE='[<ent>] [--] <path>'

Not interested; sorry.

The only thing I find lacking from the current set of tools is a
way to prettyprint an annotated tag to make its date human
readable.

```
