[PATCH 1/3] Implement parsing for new core.whitespace.* options.

Subsystems: the rest

DORMANTno replies

5 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH 1/3] Implement parsing for new core.whitespace.* options.

From: David Symonds <hidden>
Date: 2016-06-15 22:43:46

Each of the new core.whitespace.* options (enumerated below) can be set to one
of:
	* okay (default): Whitespace of this type is okay
	* warn: Whitespace of this type should be warned about
	* error: Whitespace of this type should raise an error
	* autofix: Whitespace of this type should be automatically fixed

The initial options are:
	* trailing: Whitespace at the end of a line
	* space-before-tab: SP HT sequence in the initial whitespace of a line
	* space-indent: At least 8 spaces in a row at the start of a line

Example usage:
	[core "whitespace"]
		trailing = autofix
		space-before-tab = error
		space-indent = warn

Signed-off-by: David Symonds <redacted>
---
 cache.h       |   16 ++++++++++++++++
 config.c      |   28 ++++++++++++++++++++++++++++
 environment.c |    3 +++
 3 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index bfffa05..51e3982 100644
--- a/cache.h
+++ b/cache.h
@@ -602,4 +602,20 @@ extern int diff_auto_refresh_index;
 /* match-trees.c */
 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
 
+/*
+ * whitespace rules.
+ * used by both diff and apply
+ */
+enum whitespace_mode {
+	WS_OKAY = 0,
+	WS_WARN,
+	WS_ERROR,
+	WS_AUTOFIX
+};
+extern enum whitespace_mode ws_mode_trailing;
+extern enum whitespace_mode ws_mode_space_before_tab;
+extern enum whitespace_mode ws_mode_space_indent;
+extern enum whitespace_mode git_config_whitespace_mode(const char *, const char *);
+
+
 #endif /* CACHE_H */
diff --git a/config.c b/config.c
index dc3148d..8e6f252 100644
--- a/config.c
+++ b/config.c
@@ -297,6 +297,19 @@ int git_config_bool(const char *name, const char *value)
 	return git_config_int(name, value) != 0;
 }
 
+enum whitespace_mode git_config_whitespace_mode(const char *name, const char *value)
+{
+	if (!strcasecmp(value, "okay"))
+		return WS_OKAY;
+	if (!strcasecmp(value, "warn"))
+		return WS_WARN;
+	if (!strcasecmp(value, "error"))
+		return WS_ERROR;
+	if (!strcasecmp(value, "autofix"))
+		return WS_AUTOFIX;
+	die("bad config value for '%s' in %s", name, config_file_name);
+}
+
 int git_default_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -431,6 +444,21 @@ int git_default_config(const char *var, const char *value)
 		return 0;
 	}
 
+	if (!strcmp(var, "core.whitespace.trailing")) {
+		ws_mode_trailing = git_config_whitespace_mode(var, value);
+		return 0;
+	}
+
+	if (!strcmp(var, "core.whitespace.space-before-tab")) {
+		ws_mode_space_before_tab = git_config_whitespace_mode(var, value);
+		return 0;
+	}
+
+	if (!strcmp(var, "core.whitespace.space-indent")) {
+		ws_mode_space_indent = git_config_whitespace_mode(var, value);
+		return 0;
+	}
+
 	/* Add other config variables here and to Documentation/config.txt. */
 	return 0;
 }
diff --git a/environment.c b/environment.c
index b5a6c69..71502fc 100644
--- a/environment.c
+++ b/environment.c
@@ -35,6 +35,9 @@ int pager_in_use;
 int pager_use_color = 1;
 char *editor_program;
 int auto_crlf = 0;	/* 1: both ways, -1: only when adding git objects */
+enum whitespace_mode ws_mode_trailing = WS_OKAY;
+enum whitespace_mode ws_mode_space_before_tab = WS_OKAY;
+enum whitespace_mode ws_mode_space_indent = WS_OKAY;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
-- 
1.5.3.1

[PATCH 3/3] Act on WS_WARN for ws_mode_trailing.

From: David Symonds <hidden>
Date: 2016-06-15 22:43:46

Signed-off-by: David Symonds <redacted>
---
 diff.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/diff.c b/diff.c
index 6f9b624..ebcc0f3 100644
--- a/diff.c
+++ b/diff.c
@@ -544,17 +544,22 @@ static void emit_line_with_ws(int nparents,
 	tail = len - 1;
 	if (line[tail] == '\n' && i < tail)
 		tail--;
-	while (i < tail) {
-		if (!isspace(line[tail]))
-			break;
-		tail--;
+	if (ws_mode_trailing != WS_OKAY) {
+		while (i < tail) {
+			if (!isspace(line[tail]))
+				break;
+			tail--;
+		}
 	}
 	if ((i < tail && line[tail + 1] != '\n')) {
 		/* This has whitespace between tail+1..len */
-		fputs(set, stdout);
-		fwrite(line + i, tail - i + 1, 1, stdout);
-		fputs(reset, stdout);
-		emit_line(ws, reset, line + tail + 1, len - tail - 1);
+		if (ws_mode_trailing == WS_WARN) {
+			fputs(set, stdout);
+			fwrite(line + i, tail - i + 1, 1, stdout);
+			fputs(reset, stdout);
+			emit_line(ws, reset, line + tail + 1, len - tail - 1);
+		}
+		/* TODO: handle WS_ERROR and WS_AUTOFIX */
 	}
 	else
 		emit_line(set, reset, line + i, len - i);
-- 
1.5.3.1

[PATCH 2/3] Act on WS_WARN for ws_mode_space_before_tab.

From: David Symonds <hidden>
Date: 2016-06-15 22:43:46

Signed-off-by: David Symonds <redacted>
---
 diff.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/diff.c b/diff.c
index a6aaaf7..6f9b624 100644
--- a/diff.c
+++ b/diff.c
@@ -508,8 +508,12 @@ static void emit_line_with_ws(int nparents,
 	for (i = col0; i < len; i++) {
 		if (line[i] == '\t') {
 			last_tab_in_indent = i;
-			if (0 <= last_space_in_indent)
-				need_highlight_leading_space = 1;
+			if ((ws_mode_space_before_tab != WS_OKAY) &&
+			    (0 <= last_space_in_indent)) {
+				if (ws_mode_space_before_tab == WS_WARN)
+					need_highlight_leading_space = 1;
+				/* TODO: handle WS_ERROR and WS_AUTOFIX */
+			}
 		}
 		else if (line[i] == ' ')
 			last_space_in_indent = i;
-- 
1.5.3.1

Re: [PATCH 3/3] Act on WS_WARN for ws_mode_trailing.

From: David Symonds <hidden>
Date: 2016-06-15 22:43:46

On 11/3/07, David Symonds [off-list ref] wrote:
Signed-off-by: David Symonds <redacted>
---
 diff.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)
Silly me; I somehow forgot I was only in diff.c. I'll fix and repost the series.


Dave.

Re: [PATCH 1/3] Implement parsing for new core.whitespace.* options.

From: Baz <hidden>
Date: 2016-06-15 22:43:46

On 02/11/2007, David Symonds [off-list ref] wrote:
Each of the new core.whitespace.* options (enumerated below) can be set to one
of:
        * okay (default): Whitespace of this type is okay
        * warn: Whitespace of this type should be warned about
        * error: Whitespace of this type should raise an error
        * autofix: Whitespace of this type should be automatically fixed
Sorry, this is a bit bikesheddy, but shouldn't that be 'fix' rather
than autofix? Otherwise you might want to rename the others
'autowarn', 'autookay' etc... since the computer does them
automatically too.

Cheers,
Baz
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help