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

From: John Ellson <[email-masked]>
Date: Wed, 10 Aug 2005 10:38:08 -0400

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 <[email-masked]>

---
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:		    [email-masked]
+#   for an equivalent to "cvs co -p <filename>"
+# Question posted with really bad initial solution: [email-masked]
+# Suggestions offered by:			    [email-masked]
+#						    [email-masked]
+# This solution based on posting from:		    [email-masked]
+# Polish and test by:				    [email-masked]
+
+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