Re: [PATCH 3/3] config: teach git config --file - to read from the standard input

2014-02-15 Thread Eric Sunshine
On Fri, Feb 14, 2014 at 6:37 PM, Kirill A. Shutemov
kir...@shutemov.name wrote:
 The patch extends git config --file interface to allow read config from
 stdin.

 Editing stdin or setting value in stdin is an error.

 Include by absolute path is allowed in stdin config, but not by relative
 path.

 Signed-off-by: Kirill A. Shutemov kir...@shutemov.name
 ---
 diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh
 index a70707620f14..fda6555024c5 100755
 --- a/t/t1305-config-include.sh
 +++ b/t/t1305-config-include.sh
 @@ -122,6 +122,20 @@ test_expect_success 'relative includes from command line 
 fail' '
 test_must_fail git -c include.path=one config test.one
  '

 +test_expect_success 'absolute includes from stdin work' '
 +   echo [test]one = 1 one 
 +   echo 1 expect 
 +   echo [include]path=\$PWD/one\ |

To be Windows-friendly, you may want to use $(pwd). Quoting from t/README:

   When a test checks for an absolute path that a git command generated,
   construct the expected value using $(pwd) rather than $PWD,
   $TEST_DIRECTORY, or $TRASH_DIRECTORY. It makes a difference on
   Windows, where the shell (MSYS bash) mangles absolute path names.
   For details, see the commit message of 4114156ae9.

 +   git config --file - test.one actual 
 +   test_cmp expect actual
 +'
 +
 +test_expect_success 'relative includes from stdin line fail' '
 +   echo [test]one = 1 one 
 +   echo [include]path=one |
 +   test_must_fail git config --file - test.one
 +'
 +
  test_expect_success 'include cycles are detected' '
 cat .gitconfig -\EOF 
 [test]value = gitconfig
 --
 1.8.5.2
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 3/3] config: teach git config --file - to read from the standard input

2014-02-14 Thread Kirill A. Shutemov
The patch extends git config --file interface to allow read config from
stdin.

Editing stdin or setting value in stdin is an error.

Include by absolute path is allowed in stdin config, but not by relative
path.

Signed-off-by: Kirill A. Shutemov kir...@shutemov.name
---
 builtin/config.c  | 11 +
 cache.h   |  1 +
 config.c  | 58 ---
 t/t1300-repo-config.sh| 17 --
 t/t1305-config-include.sh | 14 
 5 files changed, 81 insertions(+), 20 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index de41ba50e9ca..5677c942b693 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -360,6 +360,9 @@ static int get_colorbool(int print)
 
 static void check_write(void)
 {
+   if (given_config_source.use_stdin)
+   die(writing to stdin is not supported);
+
if (given_config_source.blob)
die(writing config blobs is not supported);
 }
@@ -472,6 +475,12 @@ int cmd_config(int argc, const char **argv, const char 
*prefix)
usage_with_options(builtin_config_usage, 
builtin_config_options);
}
 
+   if (given_config_source.file 
+   !strcmp(given_config_source.file, -)) {
+   given_config_source.file = NULL;
+   given_config_source.use_stdin = 1;
+   }
+
if (use_global_config) {
char *user_config = NULL;
char *xdg_config = NULL;
@@ -558,6 +567,8 @@ int cmd_config(int argc, const char **argv, const char 
*prefix)
check_argc(argc, 0, 0);
if (!given_config_source.file  nongit)
die(not in a git directory);
+   if (given_config_source.use_stdin)
+   die(editing stdin is not supported);
if (given_config_source.blob)
die(editing blobs is not supported);
git_config(git_default_config, NULL);
diff --git a/cache.h b/cache.h
index 9d94bd69f7db..4db19b537059 100644
--- a/cache.h
+++ b/cache.h
@@ -1147,6 +1147,7 @@ extern int update_server_info(int);
 #define CONFIG_GENERIC_ERROR 7
 
 struct git_config_source {
+   unsigned int use_stdin:1;
const char *file;
const char *blob;
 };
diff --git a/config.c b/config.c
index 6269da907964..7b42608f5c89 100644
--- a/config.c
+++ b/config.c
@@ -443,10 +443,20 @@ static int git_parse_source(config_fn_t fn, void *data)
if (get_value(fn, data, var)  0)
break;
}
-   if (cf-die_on_error)
-   die(bad config file line %d in %s, cf-linenr, cf-name);
-   else
-   return error(bad config file line %d in %s, cf-linenr, 
cf-name);
+   if (cf-die_on_error) {
+   if (cf-name)
+   die(bad config file line %d in %s,
+   cf-linenr, cf-name);
+   else
+   die(bad config file line %d, cf-linenr);
+
+   } else {
+   if (cf-name)
+   return error(bad config file line %d in %s,
+   cf-linenr, cf-name);
+   else
+   return error(bad config file line %d, cf-linenr);
+   }
 }
 
 static int parse_unit_factor(const char *end, uintmax_t *val)
@@ -1030,24 +1040,34 @@ static int do_config_from(struct config_source *top, 
config_fn_t fn, void *data)
return ret;
 }
 
-int git_config_from_file(config_fn_t fn, const char *filename, void *data)
+static int do_config_from_file(config_fn_t fn, const char *filename, FILE *f,
+  void *data)
 {
-   int ret;
-   FILE *f = fopen(filename, r);
+   struct config_source top;
 
-   ret = -1;
-   if (f) {
-   struct config_source top;
+   top.u.file = f;
+   top.name = filename;
+   top.die_on_error = 1;
+   top.do_fgetc = config_file_fgetc;
+   top.do_ungetc = config_file_ungetc;
+   top.do_ftell = config_file_ftell;
 
-   top.u.file = f;
-   top.name = filename;
-   top.die_on_error = 1;
-   top.do_fgetc = config_file_fgetc;
-   top.do_ungetc = config_file_ungetc;
-   top.do_ftell = config_file_ftell;
+   return do_config_from(top, fn, data);
+}
+
+static int git_config_from_stdin(config_fn_t fn, void *data)
+{
+   return do_config_from_file(fn, NULL, stdin, data);
+}
 
-   ret = do_config_from(top, fn, data);
+int git_config_from_file(config_fn_t fn, const char *filename, void *data)
+{
+   int ret = -1;
+   FILE *f;
 
+   f = fopen(filename, r);
+   if (f) {
+   ret = do_config_from_file(fn, filename, f, data);
fclose(f);
}
return ret;
@@ -1188,7 +1208,9 @@ int git_config_with_options(config_fn_t fn, void *data,
 *