[cogito PATCH] Add "-c" (colorize) option to cg-diff

From: Catalin Marinas <catalin.marinas@arm.com>
Date: 2005-05-21 00:58:30
An option I found quite useful in quilt. I just copied the
corresponding code from quilt (released under GPLv2) to cg-diff.

Catalin


Add "-c" (colorize) option to cg-diff

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

Index: cg-diff
===================================================================
--- ca5fef50fb68a3afbb35e1a48ac622f7a964f021/cg-diff  (mode:100755)
+++ uncommitted/cg-diff  (mode:100755)
@@ -14,6 +14,8 @@
 # -p instead of one ID denotes a parent commit to the specified ID
 # (which must not be a tree, obviously).
 #
+# -c colorizes the diff output
+#
 # Outputs a diff converting the first tree to the second one.
 
 . ${COGITO_LIB}cg-Xlib
@@ -22,7 +24,17 @@
 id1=" "
 id2=" "
 parent=
+opt_color=
 
+setup_colors()
+{
+	local C=diff_hdr=32:diff_add=36:diff_mod=35:diff_rem=35:diff_hunk=33:diff_ctx=35:diff_cctx=33:patch_offs=33:patch_fuzz=35:patch_fail=31:clear=00
+	[ -n "$COGITO_COLORS" ] && C="$C:$COGITO_COLORS"
+
+	C=${C//=/=\'$'\e'[}
+	C=color_${C//:/m\'; color_}m\'
+	eval $C
+}
 
 # FIXME: The commandline parsing is awful.
 
@@ -31,6 +43,12 @@
 	parent=1
 fi
 
+if [ "$1" = "-c" ]; then
+	shift
+	opt_color=1
+	setup_colors
+fi
+
 if [ "$1" = "-r" ]; then
 	shift
 	id1=$(echo "$1": | cut -d : -f 1)
@@ -44,6 +62,32 @@
 	shift
 fi
 
+colorize() {
+	if [ "$opt_color" ]; then
+		/usr/bin/gawk '
+		{ if (/^(Index:|\-\-\-|\+\+\+|\*\*\*) /)
+		    print "'$color_diff_hdr'" $0 "'$color_clear'"
+		  else if (/^\+/)
+		    print "'$color_diff_add'" $0 "'$color_clear'"
+		  else if (/^-/)
+		    print "'$color_diff_rem'" $0 "'$color_clear'"
+		  else if (/^!/)
+		    print "'$color_diff_mod'" $0 "'$color_clear'"
+		  else if (/^@@ \-[0-9]+(,[0-9]+)? \+[0-9]+(,[0-9]+)? @@/)
+		    print gensub(/^(@@[^@]*@@)([ \t]*)(.*)/,
+		         "'$color_diff_hunk'" "\\1" "'$color_clear'" \
+			 "\\2" \
+			 "'$color_diff_ctx'" "\\3" "'$color_clear'", "")
+		  else if (/^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/)
+		    print "'$color_diff_cctx'" $0 "'$color_clear'"
+		  else
+		    print
+		}'
+	else
+		cat
+	fi
+}
+
 if [ "$parent" ]; then
 	id2="$id1"
 	id1=$(parent-id "$id2" | head -n 1)
@@ -71,7 +115,7 @@
 	# FIXME: Update ret based on what did we match. And take "$@"
 	# to account after all.
 	ret=
-	git-diff-cache -r -z $tree | xargs -0 ${COGITO_LIB}cg-Xdiffdo "$tree" uncommitted "$filter"
+	git-diff-cache -r -z $tree | xargs -0 ${COGITO_LIB}cg-Xdiffdo "$tree" uncommitted "$filter" | colorize
 
 	[ "$filter" ] && rm $filter
 
@@ -85,7 +129,7 @@
 
 [ "$id1" = "$id2" ] && die "trying to diff $id1 against itself"
 
-git-diff-tree -r -z $id1 $id2 | xargs -0 ${COGITO_LIB}cg-Xdiffdo $id1 $id2 "$filter"
+git-diff-tree -r -z $id1 $id2 | xargs -0 ${COGITO_LIB}cg-Xdiffdo $id1 $id2 "$filter" | colorize
 
 [ "$filter" ] && rm $filter
 exit 0
Index: cg-help
===================================================================
--- ca5fef50fb68a3afbb35e1a48ac622f7a964f021/cg-help  (mode:100755)
+++ uncommitted/cg-help  (mode:100755)
@@ -26,7 +26,7 @@
 	cg-cancel
 	cg-clone	[-s] SOURCE_LOC [DESTDIR]
 	cg-commit	[-m"Commit message"]... [-e | -E] [FILE]... < log message
-	cg-diff		[-p] [-r FROM_ID[:TO_ID]] [FILE]...
+	cg-diff		[-p] [-c] [-r FROM_ID[:TO_ID]] [FILE]...
 	cg-export	DEST [TREE_ID]
 	cg-help		[COMMAND]
 	cg-init


-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Received on Sat May 21 01:00:16 2005

This archive was generated by hypermail 2.1.8 : 2005-05-21 01:00:17 EST