Skip to contentSkip to conversation

Implement parsing for new core.whitespace.* options.

PATCH In review David Symonds 2 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

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

---
 cache.h       |   16 ++++++++++++++++
 config.c      |   28 ++++++++++++++++++++++++++++
 environment.c |    3 +++
 3 files changed, 47 insertions(+), 0 deletions(-)
cache.h +16 -0
index bfffa05..51e3982 100644
@@ -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 */
config.c +28 -0
index dc3148d..8e6f252 100644
@@ -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;
 }
environment.c +3 -0
index b5a6c69..71502fc 100644
@@ -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;
Signature · 1 line
1.5.3.1
#1 lore raw

PATCH 2/3 Act on WS_WARN for ws_mode_space_before_tab.

---
 diff.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
diff.c +6 -2
index a6aaaf7..6f9b624 100644
@@ -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;
Signature · 1 line
1.5.3.1
#2 lore raw

PATCH 3/3 Act on WS_WARN for ws_mode_trailing.

---
 diff.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)
diff.c +13 -8
index 6f9b624..ebcc0f3 100644
@@ -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);
Signature · 1 line
1.5.3.1
#3 lore raw

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

Quoted 4 lines · David Symonds
---
 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.

#4 lore raw
In reply to David Symonds
Quoted 6 lines · David Symonds

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