Git development
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas@arm.com>
To: git@vger.kernel.org
Subject: [cogito PATCH] Add "-c" (colorize) option to cg-diff
Date: Fri, 20 May 2005 15:58:30 +0100	[thread overview]
Message-ID: <tnxfywirlk9.fsf@arm.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 134 bytes --]

An option I found quite useful in quilt. I just copied the
corresponding code from quilt (released under GPLv2) to cg-diff.

Catalin


[-- Attachment #2: patch-color-diff --]
[-- Type: text/plain, Size: 3131 bytes --]

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

                 reply	other threads:[~2005-05-20 14:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tnxfywirlk9.fsf@arm.com \
    --to=catalin.marinas@arm.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox