git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Frank Lichtenheld <frank@lichtenheld.de>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <junkio@cox.net>,
	Frank Lichtenheld <frank@lichtenheld.de>
Subject: [PATCH] config: Add --quoted option to produce machine-parsable output
Date: Mon, 21 May 2007 19:46:59 +0200	[thread overview]
Message-ID: <11797696193384-git-send-email-frank@lichtenheld.de> (raw)
In-Reply-To: <20070520225953.GK4085@planck.djpig.de>

This option will enclose key names in quotes (") if they
contain a subsection and then escape " and \. It will also
escape line breaks in values. Together this should produce
an easily parsable output.

Affects --list and --get-*

Signed-off-by: Frank Lichtenheld <frank@lichtenheld.de>
---
 builtin-config.c |   92 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 83 insertions(+), 9 deletions(-)

 Will add asciidoc documentation and test cases if people think that this is
 a good idea.

 I'm writing C about once a year, so I really don't mind being told if it's
 crap ;)

diff --git a/builtin-config.c b/builtin-config.c
index b2515f7..454cf4e 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -2,7 +2,7 @@
 #include "cache.h"
 
 static const char git_config_set_usage[] =
-"git-config [ --global | --system ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list";
+"git-config [ --global | --system ] [ --bool | --int ] [--quoted] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list";
 
 static char *key;
 static regex_t *key_regexp;
@@ -12,14 +12,73 @@ static int use_key_regexp;
 static int do_all;
 static int do_not_match;
 static int seen;
+static int quoted;
 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
 
+static char* quote_key(const char *key_)
+{
+	char *pos1, *pos2;
+	char* key_quot;
+
+	pos1 = strchr(key_, '.');
+	pos2 = strrchr(key_, '.');
+	if (pos1 != pos2) { /* has subsection */
+		char* key;
+		key = xmalloc(strlen(key_)*2 + 1);
+		key_quot = key;
+		*key++ = '"';
+		while (*key_) {
+			if (*key_ == '"' || *key_ == '\\')
+				*key++ = '\\';
+			*key++ = *key_++;
+		}
+		*key++ = '"';
+		*key = 0;
+	} else {
+		key_quot = xstrdup(key_);
+	}
+	return key_quot;
+}
+
+static char* quote_value(const char* value_)
+{
+	char *val_quot, *val;
+	val = xmalloc(strlen(value_)*2 + 1);
+	val_quot = val;
+
+	while (*value_) {
+		if (*value_ == '\n') {
+			*val++ = '\\';
+			*val++ = 'n';
+			value_ += 2;
+		} else
+			*val++ = *value_++;
+	}
+	*val = 0;
+
+	return val_quot;
+}
+
 static int show_all_config(const char *key_, const char *value_)
 {
-	if (value_)
-		printf("%s=%s\n", key_, value_);
-	else
-		printf("%s\n", key_);
+	if (!quoted) {
+		if (value_)
+			printf("%s=%s\n", key_, value_);
+		else
+			printf("%s\n", key_);
+	} else {
+		char* key_quot = quote_key(key_);
+
+		if (value_) {
+			char* val_quot = quote_value(value_);
+			printf("%s=%s\n", key_quot, val_quot);
+			free(val_quot);
+		} else
+			printf("%s\n", key_quot);
+
+		free(key_quot);
+	}
+
 	return 0;
 }
 
@@ -38,16 +97,26 @@ static int show_config(const char* key_, const char* value_)
 			  regexec(regexp, (value_?value_:""), 0, NULL, 0)))
 		return 0;
 
-	if (show_keys)
-		printf("%s ", key_);
+	if (show_keys) {
+		if (quoted) {
+			char* key = quote_key(key_);
+			printf("%s ", key);
+			free(key);
+		} else
+			printf("%s ", key_);
+	}
 	if (seen && !do_all)
 		dup_error = 1;
 	if (type == T_INT)
 		sprintf(value, "%d", git_config_int(key_, value_?value_:""));
 	else if (type == T_BOOL)
 		vptr = git_config_bool(key_, value_) ? "true" : "false";
-	else
-		vptr = value_?value_:"";
+	else {
+		if (quoted)
+			vptr = value_?quote_value(value_):"";
+		else
+			vptr = value_?value_:"";
+	}
 	seen++;
 	if (dup_error) {
 		error("More than one value for the key %s: %s",
@@ -56,6 +125,9 @@ static int show_config(const char* key_, const char* value_)
 	else
 		printf("%s\n", vptr);
 
+	if (quoted && value_)
+		free((char *)vptr);
+
 	return 0;
 }
 
@@ -141,6 +213,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			type = T_INT;
 		else if (!strcmp(argv[1], "--bool"))
 			type = T_BOOL;
+		else if (!strcmp(argv[1], "--quoted"))
+			quoted = 1;
 		else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
 			return git_config(show_all_config);
 		else if (!strcmp(argv[1], "--global")) {
-- 
1.5.2-rc3.GIT

  parent reply	other threads:[~2007-05-21 17:47 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-20 22:59 [RFC] Implementing git config handling in Git.pm Frank Lichtenheld
2007-05-20 23:14 ` Petr Baudis
2007-05-21 17:46 ` Frank Lichtenheld [this message]
2007-05-21 18:03   ` [PATCH] config: Add --quoted option to produce machine-parsable output Junio C Hamano
2007-05-21 18:46     ` Johannes Schindelin
2007-05-21 21:18       ` Junio C Hamano
2007-05-21 21:53         ` Johannes Schindelin
2007-05-21 19:54   ` Jan Hudec
2007-05-21 20:58     ` Frank Lichtenheld
2007-05-21 22:37       ` Jakub Narebski
2007-06-17 23:25         ` [PATCH/RFC] config: Add --null/-z option for null-delimted output Frank Lichtenheld
2007-06-19  0:55           ` Johannes Schindelin
2007-06-19  1:16             ` Jakub Narebski
2007-06-19  1:17             ` Frank Lichtenheld
2007-06-19  1:32               ` Johannes Schindelin
2007-06-19  1:37             ` Junio C Hamano
2007-06-19  2:12               ` Frank Lichtenheld
2007-06-19 11:09                 ` Johannes Schindelin
2007-06-19 11:19                   ` David Kastrup
2007-06-19 11:50                   ` Jakub Narebski
2007-06-19 15:21                   ` Frank Lichtenheld
2007-06-19 15:57                     ` Johannes Schindelin
2007-06-19 17:26                       ` Frank Lichtenheld
2007-06-20 10:31                         ` Johannes Schindelin
2007-06-20 16:54                           ` Jakub Narebski
2007-06-21 23:56           ` Jakub Narebski
2007-06-22 12:02             ` Frank Lichtenheld
2007-06-25 14:03             ` [PATCH 1/3] config: Complete documentation of --get-regexp Frank Lichtenheld
2007-06-25 14:03             ` [PATCH 2/3] config: Change output of --get-regexp for valueless keys Frank Lichtenheld
2007-06-27  2:14               ` Junio C Hamano
2007-06-25 14:03             ` [PATCH 3/3] config: Add --null/-z option for null-delimted output Frank Lichtenheld
2007-06-25 23:29               ` Jakub Narebski
2007-06-26 10:47                 ` Frank Lichtenheld
2007-06-27  2:14                 ` Junio C Hamano

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=11797696193384-git-send-email-frank@lichtenheld.de \
    --to=frank@lichtenheld.de \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /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;
as well as URLs for NNTP newsgroup(s).