threads / discuss / 1465

Newbie question: equiv of: cvs co -p <filename> ?

Subject: Newbie question: equiv of: cvs co -p <filename> ?

## tl;dr

7 messages between Aug 9, 2005 and Aug 12, 2005.

replies: 6people: 6as markdown or json

John Ellson· Aug 9, 2005, 21:59 UTC · lore
How can we cat the latest committed state of a file to stdout?
I hacked this:
	#!/bin/bash
	ID=`git-ls-files -s | grep $1 | cut -d ' ' -f 2`
	TMP=`git-unpack-file $ID`
	cat $TMP
	rm $TMP
but its really ugly!   It must be easier than this?
John
Johannes Schindelin· Aug 9, 2005, 22:10 UTC · re: John Ellson · lore

Re: Newbie question: equiv of: cvs co -p <filename> ?

Hi,
On Tue, 9 Aug 2005, John Ellson wrote:
Show 6 quoted lines
> How can we cat the latest committed state of a file to stdout?
> 
> I hacked this:
> 
> 	#!/bin/bash
> 	ID=`git-ls-files -s | grep $1 | cut -d ' ' -f 2`
and now:
	git-cat-file blob $ID

Voila, Dscho

Rene Scharfe· Aug 9, 2005, 22:18 UTC · re: Johannes Schindelin · lore

Re: Newbie question: equiv of: cvs co -p <filename> ?

Johannes Schindelin wrote:
Show 12 quoted lines
> On Tue, 9 Aug 2005, John Ellson wrote:
>>How can we cat the latest committed state of a file to stdout?
>>
>>I hacked this:
>>
>>	#!/bin/bash
>>	ID=`git-ls-files -s | grep $1 | cut -d ' ' -f 2`
> 
> 
> and now:
> 
> 	git-cat-file blob $ID
Still not pretty.  How about this?
	#!/bin/sh
	git-ls-tree HEAD "$@" | while read mode type hash filename; do
		[ "$type" = "blob" ] && git-cat-file blob "$hash"
	done
Rene
Linus Torvalds· Aug 10, 2005, 00:36 UTC · re: John Ellson · lore

Re: Newbie question: equiv of: cvs co -p <filename> ?

On Tue, 9 Aug 2005, John Ellson wrote:
Show 5 quoted lines
> 
> I hacked this:
> 
> 	#!/bin/bash
> 	ID=`git-ls-files -s | grep $1 | cut -d ' ' -f 2`

No. "git-ls-files" shows the latest _index_ state, not the latest committed state.

Use "git-ls-tree HEAD pathname" to get the latest committed state for the pathname, and then pick out the SHA1 from there, use

	git-cat-file blob <sha1>
to cat the result.

Of course, this will work with any revision, not just HEAD. So you could do something like

	git-ls-tree $(git-rev-parse --default HEAD "$@") |
		while read mode type sha name
		do
			case "$type" in
			blob)
				git-cat-file blob "$sha"
				;;
			tree)
				git-ls-tree "$sha"
				;;
			*)
				exit 1
		done
(totally untested)
		Linus
John Ellson· Aug 10, 2005, 14:38 UTC · re: Linus Torvalds · lore

[PATCH] cg-cat [was: Re: Newbie question: equiv of: cvs co -p <filename> ? ]

Linus Torvalds wrote:
> 
> On Tue, 9 Aug 2005, John Ellson wrote:
> 
>>I hacked this:
> No. ...
> So you could do something like ...
> (totally untested)
> 
> 		Linus
Thanks Linus, also Rene and Johannes.

I applied a bit of polish and testing and now I'm ready to offer my first contribution to cogito.

	cg-cat [-r rev] FILE
I hope that this is useful to others.
Signed-off-by: John Ellson <ellson@research.att.com>
---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -14,11 +14,11 @@ INSTALL?=install
 
 SCRIPT=	commit-id tree-id parent-id cg-add cg-admin-lsobj cg-admin-uncommit \
 	cg-branch-add cg-branch-ls cg-reset cg-clone cg-commit cg-diff \
 	cg-export cg-help cg-init cg-log cg-merge cg-mkpatch cg-patch \
 	cg-pull cg-restore cg-rm cg-seek cg-status cg-tag cg-tag-ls cg-update \
-	cg cg-admin-ls cg-push cg-branch-chg
+	cg cg-admin-ls cg-push cg-branch-chg cg-cat
 
 LIB_SCRIPT=cg-Xlib cg-Xmergefile cg-Xnormid
 
 GEN_SCRIPT= cg-version
 
diff --git a/cg-cat b/cg-cat
new file mode 100755
--- /dev/null
+++ b/cg-cat
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# Cat a file(s) by filename from a GIT repository.
+
+# Initiated from a request from:		    erg@research.att.com
+#   for an equivalent to "cvs co -p <filename>"
+# Question posted with really bad initial solution: ellson@research.att.com
+# Suggestions offered by:			    Johannes.Schindelin@gmx.de
+#						    rene.scharfe@lsrfire.ath.cx
+# This solution based on posting from:		    torvalds@osdl.org
+# Polish and test by:				    ellson@research.att.com
+
+USAGE="cg-cat [-r rev] FILE"
+
+. ${COGITO_LIB}cg-Xlib
+
+default=HEAD
+while optparse; do
+	if optparse -r; then
+		default="$OPTARG"
+	else
+		optfail
+	fi
+done
+
+[ "$ARGS" ] || usage
+
+git-ls-tree $(git-rev-parse --default $default "${ARGS[@]}") |
+	while read mode type sha name
+	do
+		case "$type" in
+		blob)
+			git-cat-file blob "$sha"
+			;;
+		tree)
+			git-ls-tree "$sha"
+			;;
+		*)
+			exit 1
+			;;
+		esac
+	done
Petr Baudis· Aug 11, 2005, 23:03 UTC · re: John Ellson · lore

Re: [PATCH] cg-cat [was: Re: Newbie question: equiv of: cvs co -p <filename> ? ]

Dear diary, on Wed, Aug 10, 2005 at 04:38:08PM CEST, I got a letter where John Ellson <ellson@research.att.com> told me that...

Show 23 quoted lines
> Linus Torvalds wrote:
> >
> >On Tue, 9 Aug 2005, John Ellson wrote:
> >
> >>I hacked this:
> 
> >No. ...
> 
> >So you could do something like ...
> 
> >(totally untested)
> >
> >		Linus
> 
> 
> Thanks Linus, also Rene and Johannes.
> 
> I applied a bit of polish and testing and now I'm ready to offer
> my first contribution to cogito.
> 
> 	cg-cat [-r rev] FILE
> 
> I hope that this is useful to others.

Thanks, cleaned up and applied. I only renamed it to cg-admin-cat since I think it's indeed a bit obscure. Also, note the remarkable similarity:

        cg-admin-cat       [-r TREE_ID] FILE...
        cg-admin-ls        [-t TREE_ID] [PATH]

What about joining those to cg-admin-dig or something? (Catting a directory or ls'ing file contents just feels wrong, although the first one would make some sense to people not acquainted with UNIX.)

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
If you want the holes in your knowledge showing up try teaching
someone.  -- Alan Cox
Matthias Urlichs· Aug 12, 2005, 07:53 UTC · re: Petr Baudis · lore

Re: [PATCH] cg-cat [was: Re: Newbie question: equiv of: cvs co -p <filename> ? ]

Hi, Petr Baudis wrote:
>         cg-admin-cat       [-r TREE_ID] FILE...
>         cg-admin-ls        [-t TREE_ID] [PATH]

I don't know if these need to be merged, *but* please at least change one of the flag names so that the whole mess is slightly more consistent.

-- 
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  smurf@smurf.noris.de
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
 - -
The thought of 2000 thousand people munching celery at the same time
horrifies me.
					-- George Bernard Shaw

← back to recent threads