git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 80/83] run-command: make dup_devnull() non static
@ 2016-04-24 13:39 Christian Couder
  2016-04-24 13:39 ` [PATCH 81/83] apply: roll back index in case of error Christian Couder
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: Christian Couder @ 2016-04-24 13:39 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 run-command.c | 2 +-
 run-command.h | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/run-command.c b/run-command.c
index 8c7115a..29d2bda 100644
--- a/run-command.c
+++ b/run-command.c
@@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
 }
 
 #ifndef GIT_WINDOWS_NATIVE
-static inline void dup_devnull(int to)
+void dup_devnull(int to)
 {
 	int fd = open("/dev/null", O_RDWR);
 	if (fd < 0)
diff --git a/run-command.h b/run-command.h
index de1727e..3aa244a 100644
--- a/run-command.h
+++ b/run-command.h
@@ -200,4 +200,10 @@ int run_processes_parallel(int n,
 			   task_finished_fn,
 			   void *pp_cb);
 
+/**
+ * Misc helper functions
+ */
+
+void dup_devnull(int to);
+
 #endif
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 81/83] apply: roll back index in case of error
  2016-04-24 13:39 [PATCH 80/83] run-command: make dup_devnull() non static Christian Couder
@ 2016-04-24 13:39 ` Christian Couder
  2016-04-25 16:06   ` Johannes Schindelin
  2016-04-24 13:39 ` [PATCH 82/83] environment: add set_index_file() Christian Couder
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-04-24 13:39 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 apply.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/apply.c b/apply.c
index 86e0d20..7cee834 100644
--- a/apply.c
+++ b/apply.c
@@ -4718,8 +4718,11 @@ int apply_all_patches(struct apply_state *state,
 
 		if (!strcmp(arg, "-")) {
 			res = apply_patch(state, 0, "<stdin>", options);
-			if (res < 0)
+			if (res < 0) {
+				if (state->lock_file)
+					rollback_lock_file(state->lock_file);
 				return -1;
+			}
 			errs |= res;
 			read_stdin = 0;
 			continue;
@@ -4734,16 +4737,22 @@ int apply_all_patches(struct apply_state *state,
 		read_stdin = 0;
 		set_default_whitespace_mode(state);
 		res = apply_patch(state, fd, arg, options);
-		if (res < 0)
+		if (res < 0) {
+			if (state->lock_file)
+				rollback_lock_file(state->lock_file);
 			return -1;
+		}
 		errs |= res;
 		close(fd);
 	}
 	set_default_whitespace_mode(state);
 	if (read_stdin) {
 		res = apply_patch(state, 0, "<stdin>", options);
-		if (res < 0)
+		if (res < 0) {
+			if (state->lock_file)
+				rollback_lock_file(state->lock_file);
 			return -1;
+		}
 		errs |= res;
 	}
 
@@ -4757,11 +4766,14 @@ int apply_all_patches(struct apply_state *state,
 				   squelched),
 				squelched);
 		}
-		if (state->ws_error_action == die_on_ws_error)
+		if (state->ws_error_action == die_on_ws_error) {
+			if (state->lock_file)
+				rollback_lock_file(state->lock_file);
 			return error(Q_("%d line adds whitespace errors.",
 					"%d lines add whitespace errors.",
 					state->whitespace_error),
 				     state->whitespace_error);
+		}
 		if (state->applied_after_fixing_ws && state->apply)
 			warning("%d line%s applied after"
 				" fixing whitespace errors.",
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 82/83] environment: add set_index_file()
  2016-04-24 13:39 [PATCH 80/83] run-command: make dup_devnull() non static Christian Couder
  2016-04-24 13:39 ` [PATCH 81/83] apply: roll back index in case of error Christian Couder
@ 2016-04-24 13:39 ` Christian Couder
  2016-05-03 15:36   ` Junio C Hamano
  2016-04-24 13:39 ` [PATCH 83/83] builtin/am: use apply api in run_apply() Christian Couder
  2016-04-25 15:05 ` [PATCH 80/83] run-command: make dup_devnull() non static Johannes Schindelin
  3 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-04-24 13:39 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 cache.h       | 1 +
 environment.c | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/cache.h b/cache.h
index 2711048..7f36aa3 100644
--- a/cache.h
+++ b/cache.h
@@ -461,6 +461,7 @@ extern int is_inside_work_tree(void);
 extern const char *get_git_dir(void);
 extern const char *get_git_common_dir(void);
 extern char *get_object_directory(void);
+extern void set_index_file(char *index_file);
 extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
diff --git a/environment.c b/environment.c
index 57acb2f..5a57822 100644
--- a/environment.c
+++ b/environment.c
@@ -290,6 +290,11 @@ int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
 	return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 }
 
+void set_index_file(char *index_file)
+{
+	git_index_file = index_file;
+}
+
 char *get_index_file(void)
 {
 	if (!git_index_file)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 83/83] builtin/am: use apply api in run_apply()
  2016-04-24 13:39 [PATCH 80/83] run-command: make dup_devnull() non static Christian Couder
  2016-04-24 13:39 ` [PATCH 81/83] apply: roll back index in case of error Christian Couder
  2016-04-24 13:39 ` [PATCH 82/83] environment: add set_index_file() Christian Couder
@ 2016-04-24 13:39 ` Christian Couder
  2016-04-25 15:03   ` Johannes Schindelin
  2016-04-25 15:05 ` [PATCH 80/83] run-command: make dup_devnull() non static Johannes Schindelin
  3 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-04-24 13:39 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

This replaces run_apply() implementation with a new one that
uses the apply api that has been previously prepared in
apply.c and apply.h.

This shoud improve performance a lot in certain cases.

As the previous implementation was creating a new `git apply`
process to apply each patch, it could be slow on systems like
Windows where it is costly to create new processes.

Also the new `git apply` process had to read the index from
disk, and when the process was done the calling process
discarded its own index and read back from disk the new
index that had been created by the `git apply` process.

This could be very inefficient with big repositories that
have big index files, especially when the system decided
that it was a good idea to run the `git apply` processes on
a different processor core.

Also eliminating index reads enables further performance
improvements by using:

`git update-index --split-index`

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/am.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 85 insertions(+), 18 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index d003939..85a77d7 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -28,6 +28,7 @@
 #include "rerere.h"
 #include "prompt.h"
 #include "mailinfo.h"
+#include "apply.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -1522,39 +1523,105 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
  */
 static int run_apply(const struct am_state *state, const char *index_file)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-
-	cp.git_cmd = 1;
-
-	if (index_file)
-		argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
+	struct argv_array apply_paths = ARGV_ARRAY_INIT;
+	struct argv_array apply_opts = ARGV_ARRAY_INIT;
+	struct apply_state apply_state;
+	int save_stdout_fd, save_stderr_fd;
+	int res, opts_left;
+	char *save_index_file;
+
+	struct option am_apply_options[] = {
+		{ OPTION_CALLBACK, 0, "whitespace", &apply_state, N_("action"),
+			N_("detect new or modified lines that have whitespace errors"),
+			0, apply_option_parse_whitespace },
+		{ OPTION_CALLBACK, 0, "ignore-space-change", &apply_state, NULL,
+			N_("ignore changes in whitespace when finding context"),
+			PARSE_OPT_NOARG, apply_option_parse_space_change },
+		{ OPTION_CALLBACK, 0, "ignore-whitespace", &apply_state, NULL,
+			N_("ignore changes in whitespace when finding context"),
+			PARSE_OPT_NOARG, apply_option_parse_space_change },
+		{ OPTION_CALLBACK, 0, "directory", &apply_state, N_("root"),
+			N_("prepend <root> to all filenames"),
+			0, apply_option_parse_directory },
+		{ OPTION_CALLBACK, 0, "exclude", &apply_state, N_("path"),
+			N_("don't apply changes matching the given path"),
+			0, apply_option_parse_exclude },
+		{ OPTION_CALLBACK, 0, "include", &apply_state, N_("path"),
+			N_("apply changes matching the given path"),
+			0, apply_option_parse_include },
+		OPT_INTEGER('C', NULL, &apply_state.p_context,
+				N_("ensure at least <n> lines of context match")),
+		{ OPTION_CALLBACK, 'p', NULL, &apply_state, N_("num"),
+			N_("remove <num> leading slashes from traditional diff paths"),
+			0, apply_option_parse_p },
+		OPT_BOOL(0, "reject", &apply_state.apply_with_reject,
+			N_("leave the rejected hunks in corresponding *.rej files")),
+		OPT_END()
+	};
 
 	/*
 	 * If we are allowed to fall back on 3-way merge, don't give false
 	 * errors during the initial attempt.
 	 */
+
 	if (state->threeway && !index_file) {
-		cp.no_stdout = 1;
-		cp.no_stderr = 1;
+		save_stdout_fd = dup(1);
+		dup_devnull(1);
+		save_stderr_fd = dup(2);
+		dup_devnull(2);
 	}
 
-	argv_array_push(&cp.args, "apply");
+	if (index_file) {
+		save_index_file = get_index_file();
+		set_index_file((char *)index_file);
+	}
 
-	argv_array_pushv(&cp.args, state->git_apply_opts.argv);
+	if (init_apply_state(&apply_state, NULL))
+		die("init_apply_state() failed");
+
+	argv_array_push(&apply_opts, "apply");
+	argv_array_pushv(&apply_opts, state->git_apply_opts.argv);
+
+	opts_left = parse_options(apply_opts.argc, apply_opts.argv,
+				  NULL, am_apply_options, NULL, 0);
+
+	if (opts_left != 0)
+		die("unknown option passed thru to git apply");
 
 	if (index_file)
-		argv_array_push(&cp.args, "--cached");
+		apply_state.cached = 1;
 	else
-		argv_array_push(&cp.args, "--index");
+		apply_state.check_index = 1;
 
-	argv_array_push(&cp.args, am_path(state, "patch"));
+	if (check_apply_state(&apply_state, 0))
+		die("check_apply_state() failed");
 
-	if (run_command(&cp))
-		return -1;
+	argv_array_push(&apply_paths, am_path(state, "patch"));
 
-	/* Reload index as git-apply will have modified it. */
-	discard_cache();
-	read_cache_from(index_file ? index_file : get_index_file());
+	res = apply_all_patches(&apply_state, apply_paths.argc, apply_paths.argv, 0);
+
+	/* Restore stdout and stderr */
+	if (state->threeway && !index_file) {
+		dup2(save_stdout_fd, 1);
+		close(save_stdout_fd);
+		dup2(save_stderr_fd, 2);
+		close(save_stderr_fd);
+	}
+
+	if (index_file)
+		set_index_file(save_index_file);
+
+	argv_array_clear(&apply_paths);
+	argv_array_clear(&apply_opts);
+
+	if (res)
+		return res;
+
+	if (index_file) {
+		/* Reload index as apply_all_patches() will have modified it. */
+		discard_cache();
+		read_cache_from(index_file);
+	}
 
 	return 0;
 }
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH 83/83] builtin/am: use apply api in run_apply()
  2016-04-24 13:39 ` [PATCH 83/83] builtin/am: use apply api in run_apply() Christian Couder
@ 2016-04-25 15:03   ` Johannes Schindelin
  2016-05-05 10:04     ` Christian Couder
  0 siblings, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-04-25 15:03 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sun, 24 Apr 2016, Christian Couder wrote:

> [...]
>  	/*
>  	 * If we are allowed to fall back on 3-way merge, don't give false
>  	 * errors during the initial attempt.
>  	 */
> +
>  	if (state->threeway && !index_file) {
> -		cp.no_stdout = 1;
> -		cp.no_stderr = 1;
> +		save_stdout_fd = dup(1);
> +		dup_devnull(1);
> +		save_stderr_fd = dup(2);
> +		dup_devnull(2);

I wonder. It should be possible to teach the apply function to be quiet by
default, yes? That would be more elegant than dup()ing back and forth.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-04-24 13:39 [PATCH 80/83] run-command: make dup_devnull() non static Christian Couder
                   ` (2 preceding siblings ...)
  2016-04-24 13:39 ` [PATCH 83/83] builtin/am: use apply api in run_apply() Christian Couder
@ 2016-04-25 15:05 ` Johannes Schindelin
  2016-05-05  9:50   ` Christian Couder
  3 siblings, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-04-25 15:05 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sun, 24 Apr 2016, Christian Couder wrote:

> diff --git a/run-command.c b/run-command.c
> index 8c7115a..29d2bda 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
>  }
>  
>  #ifndef GIT_WINDOWS_NATIVE
> -static inline void dup_devnull(int to)
> +void dup_devnull(int to)
>  {

The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 81/83] apply: roll back index in case of error
  2016-04-24 13:39 ` [PATCH 81/83] apply: roll back index in case of error Christian Couder
@ 2016-04-25 16:06   ` Johannes Schindelin
  2016-05-02  7:07     ` Johannes Schindelin
  2016-05-03 12:57     ` Christian Couder
  0 siblings, 2 replies; 24+ messages in thread
From: Johannes Schindelin @ 2016-04-25 16:06 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sun, 24 Apr 2016, Christian Couder wrote:

> @@ -4734,16 +4737,22 @@ int apply_all_patches(struct apply_state *state,
>  		read_stdin = 0;
>  		set_default_whitespace_mode(state);
>  		res = apply_patch(state, fd, arg, options);
> -		if (res < 0)
> +		if (res < 0) {
> +			if (state->lock_file)
> +				rollback_lock_file(state->lock_file);
>  			return -1;
> +		}
>  		errs |= res;
>  		close(fd);

In case of error, this leaves fd open, which in the end will prevent the
"patch" file, and hence the "rebase-apply/" directory from being removed
on Windows. This triggered a failure of t4014 here (and possibly more, but
it took me quite a while to track this down, what with builtin/am.c's
am_destroy() not bothering at all to check the return value of
remove_dir_recursively(), resulting in the error to be caught only much,
much later).

Could you please review all open()/close() and fopen()/fclose() calls in
your patch series, to make sure that there are no mistakes? A passing test
suite does not really make me confident here, as our code coverage is not
quite 100%.

Thanks,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 81/83] apply: roll back index in case of error
  2016-04-25 16:06   ` Johannes Schindelin
@ 2016-05-02  7:07     ` Johannes Schindelin
  2016-05-02  7:18       ` Eric Sunshine
  2016-05-03 12:57     ` Christian Couder
  1 sibling, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-05-02  7:07 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi,

apparently this mail never made it to the list???

On Mon, 25 Apr 2016, Johannes Schindelin wrote:

> Hi Chris,
> 
> On Sun, 24 Apr 2016, Christian Couder wrote:
> 
> > @@ -4734,16 +4737,22 @@ int apply_all_patches(struct apply_state *state,
> >  		read_stdin = 0;
> >  		set_default_whitespace_mode(state);
> >  		res = apply_patch(state, fd, arg, options);
> > -		if (res < 0)
> > +		if (res < 0) {
> > +			if (state->lock_file)
> > +				rollback_lock_file(state->lock_file);
> >  			return -1;
> > +		}
> >  		errs |= res;
> >  		close(fd);
> 
> In case of error, this leaves fd open, which in the end will prevent the
> "patch" file, and hence the "rebase-apply/" directory from being removed
> on Windows. This triggered a failure of t4014 here (and possibly more, but
> it took me quite a while to track this down, what with builtin/am.c's
> am_destroy() not bothering at all to check the return value of
> remove_dir_recursively(), resulting in the error to be caught only much,
> much later).
> 
> Could you please review all open()/close() and fopen()/fclose() calls in
> your patch series, to make sure that there are no mistakes? A passing test
> suite does not really make me confident here, as our code coverage is not
> quite 100%.
> 
> Thanks,
> Dscho
> 

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 81/83] apply: roll back index in case of error
  2016-05-02  7:07     ` Johannes Schindelin
@ 2016-05-02  7:18       ` Eric Sunshine
  0 siblings, 0 replies; 24+ messages in thread
From: Eric Sunshine @ 2016-05-02  7:18 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Christian Couder, Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, May 2, 2016 at 3:07 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> apparently this mail never made it to the list???

It made it to the list[1].

[1]: http://git.661346.n2.nabble.com/PATCH-80-83-run-command-make-dup-devnull-non-static-tp7654127p7654233.html

> On Mon, 25 Apr 2016, Johannes Schindelin wrote:
>
>> Hi Chris,
>>
>> On Sun, 24 Apr 2016, Christian Couder wrote:
>>
>> > @@ -4734,16 +4737,22 @@ int apply_all_patches(struct apply_state *state,
>> >             read_stdin = 0;
>> >             set_default_whitespace_mode(state);
>> >             res = apply_patch(state, fd, arg, options);
>> > -           if (res < 0)
>> > +           if (res < 0) {
>> > +                   if (state->lock_file)
>> > +                           rollback_lock_file(state->lock_file);
>> >                     return -1;
>> > +           }
>> >             errs |= res;
>> >             close(fd);
>>
>> In case of error, this leaves fd open, which in the end will prevent the
>> "patch" file, and hence the "rebase-apply/" directory from being removed
>> on Windows. This triggered a failure of t4014 here (and possibly more, but
>> it took me quite a while to track this down, what with builtin/am.c's
>> am_destroy() not bothering at all to check the return value of
>> remove_dir_recursively(), resulting in the error to be caught only much,
>> much later).
>>
>> Could you please review all open()/close() and fopen()/fclose() calls in
>> your patch series, to make sure that there are no mistakes? A passing test
>> suite does not really make me confident here, as our code coverage is not
>> quite 100%.
>>
>> Thanks,
>> Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 81/83] apply: roll back index in case of error
  2016-04-25 16:06   ` Johannes Schindelin
  2016-05-02  7:07     ` Johannes Schindelin
@ 2016-05-03 12:57     ` Christian Couder
  1 sibling, 0 replies; 24+ messages in thread
From: Christian Couder @ 2016-05-03 12:57 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Dscho,

On Mon, Apr 25, 2016 at 6:06 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Sun, 24 Apr 2016, Christian Couder wrote:
>
>> @@ -4734,16 +4737,22 @@ int apply_all_patches(struct apply_state *state,
>>               read_stdin = 0;
>>               set_default_whitespace_mode(state);
>>               res = apply_patch(state, fd, arg, options);
>> -             if (res < 0)
>> +             if (res < 0) {
>> +                     if (state->lock_file)
>> +                             rollback_lock_file(state->lock_file);
>>                       return -1;
>> +             }
>>               errs |= res;
>>               close(fd);
>
> In case of error, this leaves fd open, which in the end will prevent the
> "patch" file, and hence the "rebase-apply/" directory from being removed
> on Windows. This triggered a failure of t4014 here (and possibly more, but
> it took me quite a while to track this down, what with builtin/am.c's
> am_destroy() not bothering at all to check the return value of
> remove_dir_recursively(), resulting in the error to be caught only much,
> much later).

Sorry about that and thanks for tracking down the source of the test failure.
I fixed this by moving the "close(fd)" call just after the "apply_patch()" call.

> Could you please review all open()/close() and fopen()/fclose() calls in
> your patch series, to make sure that there are no mistakes? A passing test
> suite does not really make me confident here, as our code coverage is not
> quite 100%.

Ok, I will have another look at the 2 other places where there are
open()/close() or fopen()/fclose() calls.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 82/83] environment: add set_index_file()
  2016-04-24 13:39 ` [PATCH 82/83] environment: add set_index_file() Christian Couder
@ 2016-05-03 15:36   ` Junio C Hamano
  2016-05-04 11:50     ` Christian Couder
  0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2016-05-03 15:36 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> +void set_index_file(char *index_file)
> +{
> +	git_index_file = index_file;
> +}

What's the rationale for this change, and more importantly, the
ownership rule for the string?  When you call this function, does
the caller still own that piece of memory?  When you call this
twice, where does the old value go and who is responsible for
taking care of not leaking it?

Cannot guess any of the above with no proposed log message (that
comment applies most of your patches in this series).

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 82/83] environment: add set_index_file()
  2016-05-03 15:36   ` Junio C Hamano
@ 2016-05-04 11:50     ` Christian Couder
  0 siblings, 0 replies; 24+ messages in thread
From: Christian Couder @ 2016-05-04 11:50 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Tue, May 3, 2016 at 5:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> +void set_index_file(char *index_file)
>> +{
>> +     git_index_file = index_file;
>> +}
>
> What's the rationale for this change, and more importantly, the
> ownership rule for the string?  When you call this function, does
> the caller still own that piece of memory?  When you call this
> twice, where does the old value go and who is responsible for
> taking care of not leaking it?
>
> Cannot guess any of the above with no proposed log message (that
> comment applies most of your patches in this series).

Yeah, I agree that I could have been more verbose on this one, and
some other ones too.

The reason for this is that run_apply() in builtin/am.c has a "const
char *index_file" argument.
The current version of run_apply() does:

        if (index_file)
                argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
index_file);

to pass that parameter to the `git apply` process that it launches.

I couldn't find a good way other than doing:

    if (index_file) {
        save_index_file = get_index_file();
        set_index_file((char *)index_file);
    }

    /* Call libified apply functions */
    ...

    if (index_file)
        set_index_file(save_index_file);

to do the equivalent of what was done previously.

So I guess I could have written something like the following in the
commit message of the commit that introduces set_index_file():

    Introduce set_index_file() to be able to temporarily change the index file.

    It should be used like:

        /* Save current index file */
        old_index_file = get_index_file();
        set_index_file((char *)tmp_index_file);

        /* Do stuff that will use tmp_index_file as the index file */
        ...

        /* When finished reset the index file */
        set_index_file(old_index_file);

Maybe I could also add a comment just before the function.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-04-25 15:05 ` [PATCH 80/83] run-command: make dup_devnull() non static Johannes Schindelin
@ 2016-05-05  9:50   ` Christian Couder
  2016-05-05 20:07     ` Johannes Sixt
  0 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-05-05  9:50 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Sun, 24 Apr 2016, Christian Couder wrote:
>
>> diff --git a/run-command.c b/run-command.c
>> index 8c7115a..29d2bda 100644
>> --- a/run-command.c
>> +++ b/run-command.c
>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
>>  }
>>
>>  #ifndef GIT_WINDOWS_NATIVE
>> -static inline void dup_devnull(int to)
>> +void dup_devnull(int to)
>>  {
>
> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.

Yeah, but I must say that I don't know what I should do about this.
Do you have a suggestion? Should I try to implement the same function
for Windows?

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 83/83] builtin/am: use apply api in run_apply()
  2016-04-25 15:03   ` Johannes Schindelin
@ 2016-05-05 10:04     ` Christian Couder
  0 siblings, 0 replies; 24+ messages in thread
From: Christian Couder @ 2016-05-05 10:04 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Dscho,

On Mon, Apr 25, 2016 at 5:03 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Sun, 24 Apr 2016, Christian Couder wrote:
>
>> [...]
>>       /*
>>        * If we are allowed to fall back on 3-way merge, don't give false
>>        * errors during the initial attempt.
>>        */
>> +
>>       if (state->threeway && !index_file) {
>> -             cp.no_stdout = 1;
>> -             cp.no_stderr = 1;
>> +             save_stdout_fd = dup(1);
>> +             dup_devnull(1);
>> +             save_stderr_fd = dup(2);
>> +             dup_devnull(2);
>
> I wonder. It should be possible to teach the apply function to be quiet by
> default, yes? That would be more elegant than dup()ing back and forth.

Yes, it could be possible, but it could mean many changes not only in
the apply functions, but in possibly many other places as well. I
didn't check, but for example if an apply function calls a function
from another part of git and this function uses error(...) in case of
error, I would have to change this function too.

I could also introduce a hack like a global variable that would tell
error() to shut up, but I am not sure that would be more elegant.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-05  9:50   ` Christian Couder
@ 2016-05-05 20:07     ` Johannes Sixt
  2016-05-06 13:56       ` Christian Couder
  0 siblings, 1 reply; 24+ messages in thread
From: Johannes Sixt @ 2016-05-05 20:07 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Schindelin, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Am 05.05.2016 um 11:50 schrieb Christian Couder:
> On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
>> Hi Chris,
>>
>> On Sun, 24 Apr 2016, Christian Couder wrote:
>>
>>> diff --git a/run-command.c b/run-command.c
>>> index 8c7115a..29d2bda 100644
>>> --- a/run-command.c
>>> +++ b/run-command.c
>>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
>>>   }
>>>
>>>   #ifndef GIT_WINDOWS_NATIVE
>>> -static inline void dup_devnull(int to)
>>> +void dup_devnull(int to)
>>>   {
>>
>> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.
>
> Yeah, but I must say that I don't know what I should do about this.
> Do you have a suggestion? Should I try to implement the same function
> for Windows?

No, just remove the #ifndef brackets. There is already code in 
compat/mingw.c that treats the file name "/dev/null" specially.

-- Hannes

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-05 20:07     ` Johannes Sixt
@ 2016-05-06 13:56       ` Christian Couder
  2016-05-06 15:34         ` Johannes Schindelin
  0 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-05-06 13:56 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: Johannes Schindelin, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, May 5, 2016 at 10:07 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 05.05.2016 um 11:50 schrieb Christian Couder:
>>
>> On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
>> <Johannes.Schindelin@gmx.de> wrote:
>>>
>>> Hi Chris,
>>>
>>> On Sun, 24 Apr 2016, Christian Couder wrote:
>>>
>>>> diff --git a/run-command.c b/run-command.c
>>>> index 8c7115a..29d2bda 100644
>>>> --- a/run-command.c
>>>> +++ b/run-command.c
>>>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
>>>>   }
>>>>
>>>>   #ifndef GIT_WINDOWS_NATIVE
>>>> -static inline void dup_devnull(int to)
>>>> +void dup_devnull(int to)
>>>>   {
>>>
>>>
>>> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.
>>
>>
>> Yeah, but I must say that I don't know what I should do about this.
>> Do you have a suggestion? Should I try to implement the same function
>> for Windows?
>
> No, just remove the #ifndef brackets. There is already code in
> compat/mingw.c that treats the file name "/dev/null" specially.

Ok, I will do that in the same patch though the "#ifndef
GIT_WINDOWS_NATIVE" was already there before.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-06 13:56       ` Christian Couder
@ 2016-05-06 15:34         ` Johannes Schindelin
  2016-05-07 10:12           ` Christian Couder
  0 siblings, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-05-06 15:34 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Fri, 6 May 2016, Christian Couder wrote:

> On Thu, May 5, 2016 at 10:07 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> > Am 05.05.2016 um 11:50 schrieb Christian Couder:
> >>
> >> On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
> >> <Johannes.Schindelin@gmx.de> wrote:
> >>>
> >>> Hi Chris,
> >>>
> >>> On Sun, 24 Apr 2016, Christian Couder wrote:
> >>>
> >>>> diff --git a/run-command.c b/run-command.c
> >>>> index 8c7115a..29d2bda 100644
> >>>> --- a/run-command.c
> >>>> +++ b/run-command.c
> >>>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
> >>>>   }
> >>>>
> >>>>   #ifndef GIT_WINDOWS_NATIVE
> >>>> -static inline void dup_devnull(int to)
> >>>> +void dup_devnull(int to)
> >>>>   {
> >>>
> >>>
> >>> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.
> >>
> >>
> >> Yeah, but I must say that I don't know what I should do about this.
> >> Do you have a suggestion? Should I try to implement the same function
> >> for Windows?

No, you should change the code that requires that ugly dup()ing so that it
can be configured to shut up.

> > No, just remove the #ifndef brackets. There is already code in
> > compat/mingw.c that treats the file name "/dev/null" specially.
> 
> Ok, I will do that in the same patch though the "#ifndef
> GIT_WINDOWS_NATIVE" was already there before.

The idea was that compat/mingw.c is *really* only for the MINGW version,
not for the MSVC version.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-06 15:34         ` Johannes Schindelin
@ 2016-05-07 10:12           ` Christian Couder
  2016-05-07 12:13             ` Johannes Schindelin
  0 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-05-07 10:12 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Dscho,

On Fri, May 6, 2016 at 5:34 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Fri, 6 May 2016, Christian Couder wrote:
>
>> On Thu, May 5, 2016 at 10:07 PM, Johannes Sixt <j6t@kdbg.org> wrote:
>> > Am 05.05.2016 um 11:50 schrieb Christian Couder:
>> >>
>> >> On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
>> >> <Johannes.Schindelin@gmx.de> wrote:
>> >>>
>> >>> Hi Chris,
>> >>>
>> >>> On Sun, 24 Apr 2016, Christian Couder wrote:
>> >>>
>> >>>> diff --git a/run-command.c b/run-command.c
>> >>>> index 8c7115a..29d2bda 100644
>> >>>> --- a/run-command.c
>> >>>> +++ b/run-command.c
>> >>>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
>> >>>>   }
>> >>>>
>> >>>>   #ifndef GIT_WINDOWS_NATIVE
>> >>>> -static inline void dup_devnull(int to)
>> >>>> +void dup_devnull(int to)
>> >>>>   {
>> >>>
>> >>>
>> >>> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.
>> >>
>> >>
>> >> Yeah, but I must say that I don't know what I should do about this.
>> >> Do you have a suggestion? Should I try to implement the same function
>> >> for Windows?
>
> No, you should change the code that requires that ugly dup()ing so that it
> can be configured to shut up.

After taking a look, it looks like a routine that does nothing could
be passed to set_error_routine() and that could do part of the trick.

This part might not be too ugly, but it would anyway be more complex,
less close to what the code is doing now and more error prone, as one
also need to make sure that for example no warning() or
fprintf(stderr, ...) are called and nothing is printed on stdout.

By the way I took a look and there are 11 calls to fprintf(stderr,
...) and 10 calls to warning() in different places in builtin/apply.c.
There might also be such calls in functions outside builtin/apply.c
that are called by the functions in builtin/apply.c.

So I'd much rather keep doing what I am doing now. If you or someone
else want to contribute patches on top of the series to do it in
another way, maybe they might be integrated at the same time by Junio,
so that the whole thing would appear in the same release and there
would be no feature discrepancy between Windows and the other
platforms, and you wouldn't need to implement anything special for
Windows.

But anyway, even though I don't know much about Windows, I think if
you have some code already in compat/mingw.c to handle redirections,
it might be easier and safer overall to just implement the
redirections in Windows.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-07 10:12           ` Christian Couder
@ 2016-05-07 12:13             ` Johannes Schindelin
  2016-05-07 13:46               ` Christian Couder
  0 siblings, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-05-07 12:13 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sat, 7 May 2016, Christian Couder wrote:

> On Fri, May 6, 2016 at 5:34 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> >
> > On Fri, 6 May 2016, Christian Couder wrote:
> >
> >> On Thu, May 5, 2016 at 10:07 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> >> > Am 05.05.2016 um 11:50 schrieb Christian Couder:
> >> >>
> >> >> On Mon, Apr 25, 2016 at 5:05 PM, Johannes Schindelin
> >> >> <Johannes.Schindelin@gmx.de> wrote:
> >> >>>
> >> >>> Hi Chris,
> >> >>>
> >> >>> On Sun, 24 Apr 2016, Christian Couder wrote:
> >> >>>
> >> >>>> diff --git a/run-command.c b/run-command.c
> >> >>>> index 8c7115a..29d2bda 100644
> >> >>>> --- a/run-command.c
> >> >>>> +++ b/run-command.c
> >> >>>> @@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
> >> >>>>   }
> >> >>>>
> >> >>>>   #ifndef GIT_WINDOWS_NATIVE
> >> >>>> -static inline void dup_devnull(int to)
> >> >>>> +void dup_devnull(int to)
> >> >>>>   {
> >> >>>
> >> >>>
> >> >>> The #ifndef GIT_WINDOWS_NATIVE rings very, very loud alarm bells.
> >> >>
> >> >>
> >> >> Yeah, but I must say that I don't know what I should do about this.
> >> >> Do you have a suggestion? Should I try to implement the same function
> >> >> for Windows?
> >
> > No, you should change the code that requires that ugly dup()ing so that it
> > can be configured to shut up.
> 
> After taking a look, it looks like a routine that does nothing could
> be passed to set_error_routine() and that could do part of the trick.
> 
> This part might not be too ugly, but it would anyway be more complex,
> less close to what the code is doing now and more error prone, as one
> also need to make sure that for example no warning() or
> fprintf(stderr, ...) are called and nothing is printed on stdout.

I am afraid that you *have* to do that, though, if you truly want to
libify the code.

Of course you can go with really ugly workarounds instead. Something like
a global flag that die() and error() and warning() respect. It would
incur some technical debt, but it would make your life easier in the short
run.

Both the real solution and the workaround would be better than the current
version of the patches that dup() back and forth, just to avoid addressing
the real problem.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-07 12:13             ` Johannes Schindelin
@ 2016-05-07 13:46               ` Christian Couder
  2016-05-08  6:33                 ` Johannes Schindelin
  0 siblings, 1 reply; 24+ messages in thread
From: Christian Couder @ 2016-05-07 13:46 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Dscho,

On Sat, May 7, 2016 at 2:13 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Sat, 7 May 2016, Christian Couder wrote:
>
>> On Fri, May 6, 2016 at 5:34 PM, Johannes Schindelin
>> <Johannes.Schindelin@gmx.de> wrote:
>> >
>> > No, you should change the code that requires that ugly dup()ing so that it
>> > can be configured to shut up.
>>
>> After taking a look, it looks like a routine that does nothing could
>> be passed to set_error_routine() and that could do part of the trick.
>>
>> This part might not be too ugly, but it would anyway be more complex,
>> less close to what the code is doing now and more error prone, as one
>> also need to make sure that for example no warning() or
>> fprintf(stderr, ...) are called and nothing is printed on stdout.
>
> I am afraid that you *have* to do that, though, if you truly want to
> libify the code.
>
> Of course you can go with really ugly workarounds instead. Something like
> a global flag that die() and error() and warning() respect. It would
> incur some technical debt, but it would make your life easier in the short
> run.
>
> Both the real solution and the workaround would be better than the current
> version of the patches that dup() back and forth, just to avoid addressing
> the real problem.

The code that is now in master in builtin/am.c does:

    if (state->threeway && !index_file) {
        cp.no_stdout = 1;
        cp.no_stderr = 1;
    }

and in run-command.c there is already:

        if (cmd->no_stdout)
            dup_devnull(1);
        [...]
        if (cmd->no_stderr)
            dup_devnull(2);

for Linux and the following for Windows:

    if (cmd->no_stderr)
        fherr = open("/dev/null", O_RDWR);
        [...]
    if (cmd->no_stdout)
        fhout = open("/dev/null", O_RDWR);

so the current code is already using dup_devnull() for Linux that you
don't want me to use, and it looks like there is already a simple way
to do that on Windows.

So what's the problem? Isn't it just that you don't want a
dup_devnull() for Windows that would be a few lines long?

You keep saying that what is done in this patch is "ugly" or that
there is a "real problem", but frankly I don't see why. Could you
explain exactly why?
Because the more I look at it, the more it looks to me like the
solution that is the simplest (even for Windows), the safest and the
closest to what the current code is doing.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-07 13:46               ` Christian Couder
@ 2016-05-08  6:33                 ` Johannes Schindelin
  2016-05-08 10:15                   ` Duy Nguyen
  0 siblings, 1 reply; 24+ messages in thread
From: Johannes Schindelin @ 2016-05-08  6:33 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sat, 7 May 2016, Christian Couder wrote:

> On Sat, May 7, 2016 at 2:13 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> >
> > On Sat, 7 May 2016, Christian Couder wrote:
> >
> >> On Fri, May 6, 2016 at 5:34 PM, Johannes Schindelin
> >> <Johannes.Schindelin@gmx.de> wrote:
> >> >
> >> > No, you should change the code that requires that ugly dup()ing so
> >> > that it can be configured to shut up.
> >>
> >> After taking a look, it looks like a routine that does nothing could
> >> be passed to set_error_routine() and that could do part of the trick.
> >>
> >> This part might not be too ugly, but it would anyway be more complex,
> >> less close to what the code is doing now and more error prone, as one
> >> also need to make sure that for example no warning() or
> >> fprintf(stderr, ...) are called and nothing is printed on stdout.
> >
> > I am afraid that you *have* to do that, though, if you truly want to
> > libify the code.
> >
> > Of course you can go with really ugly workarounds instead. Something
> > like a global flag that die() and error() and warning() respect. It
> > would incur some technical debt, but it would make your life easier in
> > the short run.
> >
> > Both the real solution and the workaround would be better than the
> > current version of the patches that dup() back and forth, just to
> > avoid addressing the real problem.
> 
> The code that is now in master in builtin/am.c does:
> 
>     if (state->threeway && !index_file) {
>         cp.no_stdout = 1;
>         cp.no_stderr = 1;
>     }
> 
> and in run-command.c there is already:
> 
>         if (cmd->no_stdout)
>             dup_devnull(1);
>         [...]
>         if (cmd->no_stderr)
>             dup_devnull(2);

Of course it does that. Because there is no other way, that's why: you
cannot change the code that is spawned by start_command().

> for Linux and the following for Windows:
> 
>     if (cmd->no_stderr)
>         fherr = open("/dev/null", O_RDWR);
>         [...]
>     if (cmd->no_stdout)
>         fhout = open("/dev/null", O_RDWR);

And it is very well contained on Windows. No other callers. The code
is limited to run_command.c.

> so the current code is already using dup_devnull() for Linux that you
> don't want me to use, and it looks like there is already a simple way
> to do that on Windows.

The difference between the code in master and what your patches try to do
is that in the latter case, you want to dup() just for a while, to shut up
a code path that is not only known very well, but our very own code that
is easily changed, only to dup() it *back* in the end.

The claim is that this libifies the procedure. But it makes the code
really nasty for use as a library: if this is run in a thread (and you
know that we are going to have to do this in the near future, for
performance reasons), it will completely mess up all the other threads
because it messes with the global file descriptors.

And that is why it is ugly: it incurs an enormous technical debt that will
make code changes substantially more complicated down the road.

In essence, you save yourself a little time by sloppily dup()ing back and
forth. At the expense of making the life much harder for the developer who
needs to use your code as a library function.

And actually, hiding even fatal errors might be an ugly side effect of the
current implementation, an unfortunate implementation detail, really, not
something we want to preserve when libifying the code. And actually,
dup()ing the *caller's* stdout is not exactly preserving the current
behavior that dup()s the *called process'* stdout.

So yes, I find the proposed patch inelegant.

If others are okay with this, I will shut up. But I have to point out that
it is ugly code, plain and simple, that silences an entire global file
descriptor, just temporarily, only to avoid a careful set of patches that
introduces a silent mode to the library functions that need to be called,
which might even facilitate other libifying efforts.

I hope you do not take this as a personal attack. It is not intended as
such. It is intended to help end up with the best possible code quality.

Ciao,
Johannes

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-08  6:33                 ` Johannes Schindelin
@ 2016-05-08 10:15                   ` Duy Nguyen
  2016-05-09 15:05                     ` Johannes Schindelin
  2016-05-09 17:40                     ` Junio C Hamano
  0 siblings, 2 replies; 24+ messages in thread
From: Duy Nguyen @ 2016-05-08 10:15 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Christian Couder, Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Stefan Beller, Matthieu Moy, Christian Couder

On Sun, May 8, 2016 at 1:33 PM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> The claim is that this libifies the procedure. But it makes the code
> really nasty for use as a library: if this is run in a thread (and you
> know that we are going to have to do this in the near future, for
> performance reasons), it will completely mess up all the other threads
> because it messes with the global file descriptors.

I vote one step at a time, leave multi-thread support for future.
There's a lot more shared state than file descriptors anyway, at least
there are object db and index access and probably a couple of hidden
static variables somewhere. And I'm not sure if multi-thread really
helps here. Are we really CPU-bound? If object inflation causes that
(wild guess), can we just inflate ahead in some separate process and
pass the result back?
-- 
Duy

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-08 10:15                   ` Duy Nguyen
@ 2016-05-09 15:05                     ` Johannes Schindelin
  2016-05-09 17:40                     ` Junio C Hamano
  1 sibling, 0 replies; 24+ messages in thread
From: Johannes Schindelin @ 2016-05-09 15:05 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Christian Couder, Johannes Sixt, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Stefan Beller, Matthieu Moy, Christian Couder

Hi Duy,

On Sun, 8 May 2016, Duy Nguyen wrote:

> On Sun, May 8, 2016 at 1:33 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> > The claim is that this libifies the procedure. But it makes the code
> > really nasty for use as a library: if this is run in a thread (and you
> > know that we are going to have to do this in the near future, for
> > performance reasons), it will completely mess up all the other threads
> > because it messes with the global file descriptors.
> 
> I vote one step at a time, leave multi-thread support for future.

Oh, but I never said that we have to do that now!

All I said was that using this dup() dance instead of truly libifying the
functions would slam the door almost shut for future multi-threading.
Which is a strong hint in my book that we should *not* do that dup()
dance, but fix our code by introducing a silent mode.

> There's a lot more shared state than file descriptors anyway, at least
> there are object db and index access and probably a couple of hidden
> static variables somewhere.

Sure. And do we change those shared states temporarily in our functions?
No, we don't. The object db is not made temporarily inaccessible while a
certain function runs. The index access is not temporarily disabled while
a certain function runs.

And this is what that dup() dance does: it disables *all* output, not only
from the current thread.

> And I'm not sure if multi-thread really helps here. Are we really
> CPU-bound? If object inflation causes that (wild guess), can we just
> inflate ahead in some separate process and pass the result back?

Again. I did *not* suggest to introduce multi-threading. I was making a
case for *avoiding* that ugly dup() to /dev/null and then dup() it back to
the original state. That would just ask for unintended side effects.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 80/83] run-command: make dup_devnull() non static
  2016-05-08 10:15                   ` Duy Nguyen
  2016-05-09 15:05                     ` Johannes Schindelin
@ 2016-05-09 17:40                     ` Junio C Hamano
  1 sibling, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2016-05-09 17:40 UTC (permalink / raw)
  To: Christian Couder, Duy Nguyen
  Cc: Johannes Schindelin, Johannes Sixt, git, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Stefan Beller, Matthieu Moy, Christian Couder

Duy Nguyen <pclouds@gmail.com> writes:

> I vote one step at a time, leave multi-thread support for future.
> There's a lot more shared state than file descriptors anyway, at least
> there are object db and index access and probably a couple of hidden
> static variables somewhere. And I'm not sure if multi-thread really
> helps here. Are we really CPU-bound? If object inflation causes that
> (wild guess), can we just inflate ahead in some separate process and
> pass the result back?

I do not particularly care about multi-thread issues, but I have to
agree with Dscho that the updated code that claims to be "libified"
that futz with the file descriptors like the way this patch does is
not a proper libification.

Unfortunately, the anticipated caller of this code that does "this
may fail and it is OK because it is merely one of the attempts, so
let's not show the errors" is not something we call only when we are
falling back, so "why not do this rare codepath via the usual
run_command() interface to spawn 'apply' as a separate process?",
which would be the most sensible "one step at a time" suggestion if
it were the case, would not apply.

As you will be passing the apply state structure throughout the
callchain, would it be a viable and reasonable endgame state to have
a strbuf in it that accumulates the errors?  That is, instead of
dup()ing the standard error stream out, you would accumulate the
errors for a caller that asks their errors not directly sent to
the standard error stream, so that it can choose to either show it
at the end, or ignore it altogether.

How far can you go with just set-error-routine?  Are there things,
other than the file descriptors, that you need to futz with in order
to covert that "we'd fallback, so this early round must be silent"
codepath?

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2016-05-09 17:40 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-24 13:39 [PATCH 80/83] run-command: make dup_devnull() non static Christian Couder
2016-04-24 13:39 ` [PATCH 81/83] apply: roll back index in case of error Christian Couder
2016-04-25 16:06   ` Johannes Schindelin
2016-05-02  7:07     ` Johannes Schindelin
2016-05-02  7:18       ` Eric Sunshine
2016-05-03 12:57     ` Christian Couder
2016-04-24 13:39 ` [PATCH 82/83] environment: add set_index_file() Christian Couder
2016-05-03 15:36   ` Junio C Hamano
2016-05-04 11:50     ` Christian Couder
2016-04-24 13:39 ` [PATCH 83/83] builtin/am: use apply api in run_apply() Christian Couder
2016-04-25 15:03   ` Johannes Schindelin
2016-05-05 10:04     ` Christian Couder
2016-04-25 15:05 ` [PATCH 80/83] run-command: make dup_devnull() non static Johannes Schindelin
2016-05-05  9:50   ` Christian Couder
2016-05-05 20:07     ` Johannes Sixt
2016-05-06 13:56       ` Christian Couder
2016-05-06 15:34         ` Johannes Schindelin
2016-05-07 10:12           ` Christian Couder
2016-05-07 12:13             ` Johannes Schindelin
2016-05-07 13:46               ` Christian Couder
2016-05-08  6:33                 ` Johannes Schindelin
2016-05-08 10:15                   ` Duy Nguyen
2016-05-09 15:05                     ` Johannes Schindelin
2016-05-09 17:40                     ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).