Skip to contentSkip to conversation

Add git-config --remove-section, document --rename-section

PATCH In review Paolo Bonzini 5 participants 12 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add git-config --remove-section, document --rename-section

As per the subject. The existing --rename-section option is undocumented, while --remove-section is added by this patch based on the code I had written for the "git branch" patch.

Paolo

* git-config: document --rename-section, provide --remove-section

This patch documents the previously undocumented option --rename-section and adds a new option to zap an entire section.

Documentation/git-config.txt +8 -0
index 6624484..68de588 100644
@@ -16,6 +16,8 @@ SYNOPSIS
 'git-config' [--global] [type] --get-all name [value_regex]
 'git-config' [--global] [type] --unset name [value_regex]
 'git-config' [--global] [type] --unset-all name [value_regex]
+'git-config' [--global] [type] --rename-section old_name new_name
+'git-config' [--global] [type] --remove-section name
 'git-config' [--global] -l | --list
 
 DESCRIPTION
@@ -74,6 +76,12 @@ OPTIONS
 --global::
 	Use global ~/.gitconfig file rather than the repository .git/config.
 
+--remove-section::
+	Remove the given section from the configuration file.
+
+--rename-section::
+	Rename the given section to a new name.
+
 --unset::
 	Remove the line matching the key from config file.
 
builtin-config.c +14 -1
index f1433a4..6b12fa1 100644
@@ -2,7 +2,7 @@
 #include "cache.h"
 
 static const char git_config_set_usage[] =
-"git-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --list";
+"git-config [ --global ] [ --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";
 
 static char *key;
 static regex_t *key_regexp;
@@ -168,6 +168,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			}
 			return 0;
 		}
+		else if (!strcmp(argv[1], "--remove-section")) {
+			int ret;
+			if (argc != 3)
+				usage(git_config_set_usage);
+			ret = git_config_remove_section(argv[2]);
+			if (ret < 0)
+				return ret;
+			if (ret == 0) {
+				fprintf(stderr, "No such section!\n");
+				return 1;
+			}
+			return 0;
+		}
 		else
 			break;
 		argc--;
config.c +89 -32

138 lines not on this page · raw message

cache.h +1 -0
index 8bbc142..585a9b4 100644
@@ -438,6 +439,7 @@ extern int git_config_bool(const char *, const char *);
 extern int git_config_set(const char *, const char *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
+extern int git_config_remove_section(const char *);
 extern int check_repository_format_version(const char *var, const char *value);
 
 #define MAX_GITNAME (1000)
#1 lore raw
Quoted 3 lines · Paolo Bonzini

As per the subject. The existing --rename-section option is undocumented, while --remove-section is added by this patch based on the code I had written for the "git branch" patch.

You desperately need tests for this code.

#2 lore raw

PATCH 1/1 add test for git-config --remove-section

This adds a simple test if a section is properly removed, even if the section header is repeated in the config.

---
On Thu, 1 Mar 2007, Alex Riesen wrote:

> On 3/1/07, Paolo Bonzini <[email protected]> wrote:
> > As per the subject. The existing --rename-section option is > > undocumented, while --remove-section is added by this patch > > based on the code I had written for the "git branch" patch. >
> You desperately need tests for this code.

Something like this? Totally untested (of course)...

 t/t1300-repo-config.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
t/t1300-repo-config.sh +16 -0
index 3753e9f..6b15a3e 100755
@@ -391,6 +391,22 @@ EOF
 
 test_expect_success "rename succeeded" "diff -u expect .git/config"
 
+test_expect_success "remove section" "git config --remove-section branch.zwei"
+
+cat > expect << EOF
+# Hallo
+	#Bello
+[branch "zwei"]
+	x = 1
+[branch "zwei"]
+	y = 1
+[branch "drei"]
+weird
+EOF
+
+test_expect_success "section was removed properly" \
+	"diff -u expect .git/config"
+
 test_expect_success numbers '
 
 	git-config kilo.gram 1k &&
#4 lore raw
In reply to Paolo Bonzini

Hi,

Quoted 3 lines · Paolo Bonzini

As per the subject. The existing --rename-section option is undocumented, while --remove-section is added by this patch based on the code I had written for the "git branch" patch.

Am I right assuming that you copied rename_section(), and modified it to be remove_section()?

How about modifying rename_section() so that if new_name == NULL, it removes the section?

But I like the way you extracted section_name_match(). Makes the code much more readable.

Ciao,
Dscho

#5 lore raw
Quoted 3 lines

Am I right assuming that you copied rename_section(), and modified it to be remove_section()?

Yes, after extracting section_name_match.

Quoted 2 lines

How about modifying rename_section() so that if new_name == NULL, it removes the section?

It's not so immediate, because rename_section has to print all the non-section lines anyway, while remove_section has to remove them too. I agree though that there is some duplicate code in getting the filename to open.

Thanks for writing the test, having an example will make my future work easier.

Paolo

#6 lore raw

Hi,

Quoted 5 lines · Paolo Bonzini
Quoted 2 lines

How about modifying rename_section() so that if new_name == NULL, it removes the section?

It's not so immediate, because rename_section has to print all the non-section lines anyway, while remove_section has to remove them too.

Of course you would have to move "int removing" and the related two lines to rename_section(), too.

The diff would be shorter, and if you then rename the function to rename_or_remove_section() it becomes all clearer.

Ciao,
Dscho

Quoted 2 lines

Thanks for writing the test, having an example will make my future work easier.

You're welcome. Knowing how useful tests are is one of the good lessons of Extreme Programming...

Ciao,
Dscho

#7 lore raw
Quoted 6 lines

Of course you would have to move "int removing" and the related two lines to rename_section(), too.

The diff would be shorter, and if you then rename the function to rename_or_remove_section() it becomes all clearer.

It's more different than it looks like. For example, remove_section needs this,

        if (buf[i] == '[') {
                if (section_name_match (&buf[i + 1], name)) {
		} else
			...
	}

while rename_section can use

        if (buf[i] == '[' && section_name_match (&buf[i+1], old_name)) {

and has more indenting in it. The amount of indentation in the combined function is not makign things easier to read.

Paolo

#8 lore raw

Hi,

Quoted 14 lines · Paolo Bonzini
Quoted 5 lines

Of course you would have to move "int removing" and the related two lines to rename_section(), too.

The diff would be shorter, and if you then rename the function to rename_or_remove_section() it becomes all clearer.

It's more different than it looks like. For example, remove_section needs this,

        if (buf[i] == '[') {
                if (section_name_match (&buf[i + 1], name)) {
		} else
			...
	}
Quoted 3 lines

while rename_section can use

        if (buf[i] == '[' && section_name_match (&buf[i+1], old_name)) {

But it does not have to.

	if (buf[i] == '[') {
		if (section_name_match(&buf[i + 1], old_name) {
			if (new_name == NULL)
				remove = 1;
			else
				/* write new section */
			continue;
		}
		remove = 0;
	}
	if (!remove)
		/* write buf */

Ciao,
Dscho

#9 lore raw
Quoted 4 lines
	if (buf[i] == '[') {
		if (section_name_match(&buf[i + 1], old_name) {
			if (new_name == NULL)
				remove = 1;
Quoted 8 lines
			else
				/* write new section */
			continue;
		}
		remove = 0;
	}
	if (!remove)
		/* write buf */

Try doing this and you'll see that the 8-character indentation makes it pretty hard. :-)

Paolo

#10 lore raw

PATCH 1/1 git-config: document --rename-section, provide --remove-section

From: Paolo Bonzini <[email protected]>

This patch documents the previously undocumented option --rename-section and adds a new option to zap an entire section.

---

I just made the changes to git_rename_section() myself, and added the test case (which I made a little nastier, too), then made sure that everything is fine.

Paolo, do you agree with this patch? I left you as the author, because you did all the hard work.

 Documentation/git-config.txt |    8 +++++
 builtin-config.c             |   15 ++++++++++-
 config.c                     |   60 +++++++++++++++++++++++++----------------
 t/t1300-repo-config.sh       |   16 +++++++++++
 4 files changed, 74 insertions(+), 25 deletions(-)
Documentation/git-config.txt +8 -0
index 6624484..68de588 100644
@@ -16,6 +16,8 @@ SYNOPSIS
 'git-config' [--global] [type] --get-all name [value_regex]
 'git-config' [--global] [type] --unset name [value_regex]
 'git-config' [--global] [type] --unset-all name [value_regex]
+'git-config' [--global] [type] --rename-section old_name new_name
+'git-config' [--global] [type] --remove-section name
 'git-config' [--global] -l | --list
 
 DESCRIPTION
@@ -74,6 +76,12 @@ OPTIONS
 --global::
 	Use global ~/.gitconfig file rather than the repository .git/config.
 
+--remove-section::
+	Remove the given section from the configuration file.
+
+--rename-section::
+	Rename the given section to a new name.
+
 --unset::
 	Remove the line matching the key from config file.
 
builtin-config.c +14 -1
index f1433a4..dfa403b 100644
@@ -2,7 +2,7 @@
 #include "cache.h"
 
 static const char git_config_set_usage[] =
-"git-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --list";
+"git-config [ --global ] [ --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";
 
 static char *key;
 static regex_t *key_regexp;
@@ -168,6 +168,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			}
 			return 0;
 		}
+		else if (!strcmp(argv[1], "--remove-section")) {
+			int ret;
+			if (argc != 3)
+				usage(git_config_set_usage);
+			ret = git_config_rename_section(argv[2], NULL);
+			if (ret < 0)
+				return ret;
+			if (ret == 0) {
+				fprintf(stderr, "No such section!\n");
+				return 1;
+			}
+			return 0;
+		}
 		else
 			break;
 		argc--;
config.c +36 -24

83 lines not on this page · raw message

t/t1300-repo-config.sh +16 -0
index 3753e9f..655d1e6 100755
@@ -391,6 +391,22 @@ EOF
 
 test_expect_success "rename succeeded" "git diff expect .git/config"
 
+cat >> .git/config << EOF
+  [branch "zwei"] a = 1 [branch "vier"]
+EOF
+
+test_expect_success "remove section" "git config --remove-section branch.zwei"
+
+cat > expect << EOF
+# Hallo
+	#Bello
+[branch "drei"]
+weird
+EOF
+
+test_expect_success "section was removed properly" \
+	"diff -u expect .git/config"
+
 test_expect_success numbers '
 
 	git-config kilo.gram 1k &&
Signature · 1 line
1.5.0.2.2488.gdffb-dirty