Re: [PATCH v2] Add global and system-wide gitattributes

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

Re: [PATCH v2] Add global and system-wide gitattributes

From: Matthieu Moy <hidden>
Date: 2016-06-15 22:49:24

Petr Onderka [off-list ref] writes:
Allow gitattributes to be set globally and system wide. This way, settings
for particular file types can be set in one place and apply for all user's
repositories.

The location of system-wide attributes file is $(prefix)/etc/gitattributes.
The location of the global file can be configured by setting
core.attributesfile.
Good, I like this :-)
Some parts of the code were copied from the implementation of the same
functionality in config.c.
(maybe you could say quickly which ones and why)
+		if (git_attr_global() && attributes_file) {
I tried this, and attributes_file was NULL here. I don't know how it
worked for you, but there should be a call to git_config here.
+			char *user_attr = xstrdup(attributes_file);
+
+			elem = read_attr_from_file(user_attr, 1);
+			free(user_attr);
Any reason for this xstrdup/free?

Patch follows for these two points + a test, to be squashed into
yours.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

[PATCH] core.attributesfile: a fix, a simplification, and a test

From: Matthieu Moy <hidden>
Date: 2016-06-15 22:49:24

Patch meant to be squashed into the core.attributesfile.

* attributes_file won't be set unless one calls git_config before => do
  this.

* There was a useless xstrdup/free in the code.

* This really deserves a test, so I added one in t0003-attributes.sh.
  (I've been too lazy to check the system-wide attributes file, though)

Signed-off-by: Matthieu Moy <redacted>
---
 attr.c                |    6 ++----
 t/t0003-attributes.sh |   14 ++++++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/attr.c b/attr.c
index 068e13b..342791a 100644
--- a/attr.c
+++ b/attr.c
@@ -500,11 +500,9 @@ static void bootstrap_attr_stack(void)
 			}
 		}
 
+		git_config(git_default_config, NULL);
 		if (git_attr_global() && attributes_file) {
-			char *user_attr = xstrdup(attributes_file);
-
-			elem = read_attr_from_file(user_attr, 1);
-			free(user_attr);
+			elem = read_attr_from_file(attributes_file, 1);
 			if (elem) {
 				elem->origin = NULL;
 				elem->prev = attr_stack;
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index de38c7f..24286e5 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -15,6 +15,7 @@ attr_check () {
 
 }
 
+HOME=$(pwd)
 
 test_expect_success 'setup' '
 
@@ -36,6 +37,9 @@ test_expect_success 'setup' '
 		echo "d/* test=a/b/d/*"
 		echo "d/yes notest"
 	) >a/b/.gitattributes
+	(
+		echo "global test=global"
+	) >$HOME/global-gitattributes
 
 '
 
@@ -57,6 +61,16 @@ test_expect_success 'attribute test' '
 
 '
 
+test_expect_success 'core.attributesfile' '
+	attr_check global unspecified &&
+	git config core.attributesfile "$HOME/global-gitattributes" &&
+	attr_check global global &&
+	git config core.attributesfile "~/global-gitattributes" &&
+	attr_check global global &&
+	echo "global test=precedence" >> .gitattributes &&
+	attr_check global precedence
+'
+
 test_expect_success 'attribute test: read paths from stdin' '
 
 	cat <<EOF > expect
-- 
1.7.2.2.175.ga619d.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help