{"thread":{"id":"672","subject":"[cogito PATCH] Add \"-c\" (colorize) option to cg-diff","startedAt":"2005-05-20T14:58:30Z","lastAt":"2005-05-20T14:58:30Z","messageCount":1,"participants":["Catalin Marinas"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"3613","messageId":"tnxfywirlk9.fsf@arm.com","threadId":"672","inReplyTo":null,"subject":"[cogito PATCH] Add \"-c\" (colorize) option to cg-diff","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@arm.com","sentAt":"2005-05-20T14:58:30Z","receivedAt":"2005-05-20T14:58:30Z","isPatch":true,"sender":{"key":"catalin.marinas@arm.com","avatar":null},"body":"An option I found quite useful in quilt. I just copied the\ncorresponding code from quilt (released under GPLv2) to cg-diff.\n\nCatalin\n\n\n\nAdd \"-c\" (colorize) option to cg-diff\n\nSigned-off-by: Catalin Marinas <catalin.marinas@arm.com>\n\nIndex: cg-diff\n===================================================================\n--- ca5fef50fb68a3afbb35e1a48ac622f7a964f021/cg-diff  (mode:100755)\n+++ uncommitted/cg-diff  (mode:100755)\n@@ -14,6 +14,8 @@\n # -p instead of one ID denotes a parent commit to the specified ID\n # (which must not be a tree, obviously).\n #\n+# -c colorizes the diff output\n+#\n # Outputs a diff converting the first tree to the second one.\n \n . ${COGITO_LIB}cg-Xlib\n@@ -22,7 +24,17 @@\n id1=\" \"\n id2=\" \"\n parent=\n+opt_color=\n \n+setup_colors()\n+{\n+\tlocal 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+\t[ -n \"$COGITO_COLORS\" ] && C=\"$C:$COGITO_COLORS\"\n+\n+\tC=${C//=/=\\'$'\\e'[}\n+\tC=color_${C//:/m\\'; color_}m\\'\n+\teval $C\n+}\n \n # FIXME: The commandline parsing is awful.\n \n@@ -31,6 +43,12 @@\n \tparent=1\n fi\n \n+if [ \"$1\" = \"-c\" ]; then\n+\tshift\n+\topt_color=1\n+\tsetup_colors\n+fi\n+\n if [ \"$1\" = \"-r\" ]; then\n \tshift\n \tid1=$(echo \"$1\": | cut -d : -f 1)\n@@ -44,6 +62,32 @@\n \tshift\n fi\n \n+colorize() {\n+\tif [ \"$opt_color\" ]; then\n+\t\t/usr/bin/gawk '\n+\t\t{ if (/^(Index:|\\-\\-\\-|\\+\\+\\+|\\*\\*\\*) /)\n+\t\t    print \"'$color_diff_hdr'\" $0 \"'$color_clear'\"\n+\t\t  else if (/^\\+/)\n+\t\t    print \"'$color_diff_add'\" $0 \"'$color_clear'\"\n+\t\t  else if (/^-/)\n+\t\t    print \"'$color_diff_rem'\" $0 \"'$color_clear'\"\n+\t\t  else if (/^!/)\n+\t\t    print \"'$color_diff_mod'\" $0 \"'$color_clear'\"\n+\t\t  else if (/^@@ \\-[0-9]+(,[0-9]+)? \\+[0-9]+(,[0-9]+)? @@/)\n+\t\t    print gensub(/^(@@[^@]*@@)([ \\t]*)(.*)/,\n+\t\t         \"'$color_diff_hunk'\" \"\\\\1\" \"'$color_clear'\" \\\n+\t\t\t \"\\\\2\" \\\n+\t\t\t \"'$color_diff_ctx'\" \"\\\\3\" \"'$color_clear'\", \"\")\n+\t\t  else if (/^\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*/)\n+\t\t    print \"'$color_diff_cctx'\" $0 \"'$color_clear'\"\n+\t\t  else\n+\t\t    print\n+\t\t}'\n+\telse\n+\t\tcat\n+\tfi\n+}\n+\n if [ \"$parent\" ]; then\n \tid2=\"$id1\"\n \tid1=$(parent-id \"$id2\" | head -n 1)\n@@ -71,7 +115,7 @@\n \t# FIXME: Update ret based on what did we match. And take \"$@\"\n \t# to account after all.\n \tret=\n-\tgit-diff-cache -r -z $tree | xargs -0 ${COGITO_LIB}cg-Xdiffdo \"$tree\" uncommitted \"$filter\"\n+\tgit-diff-cache -r -z $tree | xargs -0 ${COGITO_LIB}cg-Xdiffdo \"$tree\" uncommitted \"$filter\" | colorize\n \n \t[ \"$filter\" ] && rm $filter\n \n@@ -85,7 +129,7 @@\n \n [ \"$id1\" = \"$id2\" ] && die \"trying to diff $id1 against itself\"\n \n-git-diff-tree -r -z $id1 $id2 | xargs -0 ${COGITO_LIB}cg-Xdiffdo $id1 $id2 \"$filter\"\n+git-diff-tree -r -z $id1 $id2 | xargs -0 ${COGITO_LIB}cg-Xdiffdo $id1 $id2 \"$filter\" | colorize\n \n [ \"$filter\" ] && rm $filter\n exit 0\nIndex: cg-help\n===================================================================\n--- ca5fef50fb68a3afbb35e1a48ac622f7a964f021/cg-help  (mode:100755)\n+++ uncommitted/cg-help  (mode:100755)\n@@ -26,7 +26,7 @@\n \tcg-cancel\n \tcg-clone\t[-s] SOURCE_LOC [DESTDIR]\n \tcg-commit\t[-m\"Commit message\"]... [-e | -E] [FILE]... < log message\n-\tcg-diff\t\t[-p] [-r FROM_ID[:TO_ID]] [FILE]...\n+\tcg-diff\t\t[-p] [-c] [-r FROM_ID[:TO_ID]] [FILE]...\n \tcg-export\tDEST [TREE_ID]\n \tcg-help\t\t[COMMAND]\n \tcg-init\n"}]}