Skip to contentSkip to conversation

colored diff.

PATCH In review Junio C Hamano 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 colored diff.

With configuration option diff.usecolor, this colorizes the diff output.

---
* I am not happy with the initialization, especially the part that calls git_config(git_diff_config) from git.c, and I am not that interested in this myself, but some people seem to like colors, so this will be in "pu" for now.

 cache.h    |    3 +-
 diff-lib.c |  101 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 diff.h     |    2 +
 git.c      |    3 +-
 pager.c    |   17 ++++++----
 5 files changed, 104 insertions(+), 22 deletions(-)
cache.h +1 -2
index 69801b0..e6aa297 100644
@@ -169,7 +169,6 @@ extern int trust_executable_bit;
 extern int assume_unchanged;
 extern int only_use_symrefs;
 extern int warn_ambiguous_refs;
-extern int diff_rename_limit_default;
 extern int shared_repository;
 extern const char *apply_default_whitespace;
 
@@ -359,6 +358,6 @@ extern int receive_unpack_pack(int fd[2]
 extern int receive_keep_pack(int fd[2], const char *me, int quiet);
 
 /* pager.c */
-extern void setup_pager(void);
+extern int setup_pager(void);
 
 #endif /* CACHE_H */
diff-lib.c +89 -12

179 lines not on this page · raw message

diff.h +2 -0
index 52fff66..7cb5708 100644
@@ -168,4 +168,6 @@ #define DIFF_STATUS_FILTER_BROKEN	'B'
 
 extern const char *diff_unique_abbrev(const unsigned char *, int);
 
+extern void diff_setup_colors(void);
+
 #endif /* DIFF_H */
git.c +2 -1
index 40b7e42..6742de7 100644
@@ -290,7 +290,8 @@ static int cmd_log_wc(int argc, const ch
 		die("unrecognized argument: %s", argv[1]);
 
 	prepare_revision_walk(rev);
-	setup_pager();
+	if (!setup_pager())
+		diff_setup_colors();
 	while ((commit = get_revision(rev)) != NULL) {
 		log_tree_commit(rev, commit);
 		free(commit->buffer);
pager.c +10 -7
index b063353..1fb7103 100644
@@ -10,26 +10,29 @@ static void run_pager(const char *pager)
 	execlp(pager, pager, NULL);
 }
 
-void setup_pager(void)
+/* Returns 0 if the caller can assume the output stream
+ * is like a terminal (either bare terminal or a pager.
+ */
+int setup_pager(void)
 {
 	pid_t pid;
 	int fd[2];
 	const char *pager = getenv("PAGER");
 
 	if (!isatty(1))
-		return;
+		return 1;
 	if (!pager)
 		pager = "less";
 	else if (!*pager || !strcmp(pager, "cat"))
-		return;
+		return 0;
 
 	if (pipe(fd) < 0)
-		return;
+		return -1;
 	pid = fork();
 	if (pid < 0) {
 		close(fd[0]);
 		close(fd[1]);
-		return;
+		return -1;
 	}
 
 	/* return in the child */
@@ -37,7 +40,7 @@ void setup_pager(void)
 		dup2(fd[1], 1);
 		close(fd[0]);
 		close(fd[1]);
-		return;
+		return 0;
 	}
 
 	/* The original process turns into the PAGER */
@@ -45,7 +48,7 @@ void setup_pager(void)
 	close(fd[0]);
 	close(fd[1]);
 
-	setenv("LESS", "-S", 0);
+	setenv("LESS", "-RS", 0);
 	run_pager(pager);
 	exit(255);
 }
#1 lore raw

Hello Junio,

Quoted 2 lines · Junio C Hamano

With configuration option diff.usecolor, this colorizes the diff output.

I use a long time the following combination, which served me and a few others very well:

        bourne shell function:

        BLACK="^[[0;30m"
        RED="^[[0;31m"
        GREEN="^[[0;32m"
        YELLO="^[[0;33m"
        BLUE="^[[0;34m"
        PURPLE="^[[0;35m"
        CYAN="^[[0;36m"
        WHITE="^[[0;37m"
        END="^[[0m"
        REVERSE="^[[7m"

        cdiff()
        {
                cat $1 | \
                sed -e " \
                        s/^\(diff-tree\)\(.*\)/\1${RED}\2${END}/; \
                        s/^+.*/$RED&$END/;  \
                        s/^-.*/$BLUE&$END/;  \
                        s/^@.*/$GREEN&$END/;  \
                        s/^Date.*/$RED&$END/;  \
                        s/^Author.*/$RED&$END/; \
                        " | less -R -i -p "^diff-tree"

                        # This would highlight manpages but the last line clashes with cdiff
                        # --tg 00:52 05-05-26
                        #s/^[A-Z]\+[A-Z ]\+$/$RED&$END/; \
                        #s/[A-Z]\+([0-9])/$RED&$END/g; \
                        # s/\W--\?[0-9a-zA-Z=-]\+/$BLUE&$END/g; \
        }
# with 'n' you simply jump to the next commit
# with 'N' to the previous

git-whatchanged -p | cdiff

screenshot: http://wwwcip.informatik.uni-erlangen.de/~sithglan/shot.png

Thanks for the enegery you put in git,
Thomas