Skip to contentSkip to conversation

push: submodule support

PATCH Superseded Heiko Voigt 4 participants 17 messages Jun 15, 2016

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover push: submodule support

I have rewritten and cleaned up the recursive submodule on-demand push patch from Fredrik. Since this involved changing large parts of it I have taken over the ownership. To pay credit that this is built upon Fredriks work I have left the signed-off and mentored-by footers the same. I hope that this is the proper way to handle such cases. If someone comes up with a better idea I am happy to change things.

Cheers Heiko

The first iteration of this patch series can be found here:
http://thread.gmane.org/gmane.comp.version-control.git/176328/focus=176327

The second iteration of this patch series can be found here:
http://thread.gmane.org/gmane.comp.version-control.git/177992

The third iteration of this patch series can be found here:
http://thread.gmane.org/gmane.comp.version-control.git/179037/focus=179048

The fourth iteration of this patch series can be found here:
http://thread.gmane.org/gmane.comp.version-control.git/179731

Heiko Voigt (3):
Teach revision walking machinery to walk multiple times sequencially Refactor submodule push check to use string list instead of integer
push: teach --recurse-submodules the on-demand option

 .gitignore                                       |    1 +
 Documentation/git-push.txt                       |   14 +++-
 Documentation/technical/api-revision-walking.txt |    5 +
 Makefile                                         |    1 +
 builtin/push.c                                   |    7 ++
 object.c                                         |   11 +++
 object.h                                         |    2 +
 revision.c                                       |    5 +
 revision.h                                       |    1 +
 submodule.c                                      |   70 ++++++++++++++---
 submodule.h                                      |    4 +-
 t/t0062-revision-walking.sh                      |   33 ++++++++
 t/t5531-deep-submodule-push.sh                   |   94 ++++++++++++++++++++++
 test-revision-walking.c                          |   66 +++++++++++++++
 transport.c                                      |   41 +++++++++-
 transport.h                                      |    1 +
 16 files changed, 338 insertions(+), 18 deletions(-)
 create mode 100755 t/t0062-revision-walking.sh
 create mode 100644 test-revision-walking.c
Signature · 1 line
1.7.9.114.gead08
#1 lore raw

PATCH v5 1/3 Teach revision walking machinery to walk multiple times sequencially

Previously it was not possible to iterate revisions twice using the revision walking api. We add a reset_revision_walk() which clears the used flags. This allows us to do multiple sequencial revision walks.

---
 .gitignore                                       |    1 +
 Documentation/technical/api-revision-walking.txt |    5 ++
 Makefile                                         |    1 +
 object.c                                         |   11 ++++
 object.h                                         |    2 +
 revision.c                                       |    5 ++
 revision.h                                       |    1 +
 submodule.c                                      |    2 +
 t/t0062-revision-walking.sh                      |   33 +++++++++++
 test-revision-walking.c                          |   66 ++++++++++++++++++++++
 10 files changed, 127 insertions(+), 0 deletions(-)
 create mode 100755 t/t0062-revision-walking.sh
 create mode 100644 test-revision-walking.c
.gitignore +1 -0
index 3b7680e..2f09842 100644
@@ -184,6 +184,7 @@
 /test-obj-pool
 /test-parse-options
 /test-path-utils
+/test-revision-walking
 /test-run-command
 /test-sha1
 /test-sigchain
Documentation/technical/api-revision-walking.txt +5 -0
index 996da05..b7d0d9a 100644
@@ -56,6 +56,11 @@ function.
 	returning a `struct commit *` each time you call it. The end of the
 	revision list is indicated by returning a NULL pointer.
 
+`reset_revision_walk`::
+
+	Reset the flags used by the revision walking api. You can use
+	this to do multiple sequencial revision walks.
+
 Data structures
 ---------------
 
Makefile +1 -0
index c457c34..bff686f 100644
@@ -472,6 +472,7 @@ TEST_PROGRAMS_NEED_X += test-mktemp
 TEST_PROGRAMS_NEED_X += test-obj-pool
 TEST_PROGRAMS_NEED_X += test-parse-options
 TEST_PROGRAMS_NEED_X += test-path-utils
+TEST_PROGRAMS_NEED_X += test-revision-walking
 TEST_PROGRAMS_NEED_X += test-run-command
 TEST_PROGRAMS_NEED_X += test-sha1
 TEST_PROGRAMS_NEED_X += test-sigchain
object.c +11 -0
index 6b06297..6291ce9 100644
@@ -275,3 +275,14 @@ void object_array_remove_duplicates(struct object_array *array)
 		array->nr = dst;
 	}
 }
+
+void clear_object_flags(unsigned flags)
+{
+	int i;
+	struct object *obj;
+
+	for (i=0; i < obj_hash_size; i++) {
+		if ((obj = obj_hash[i]) && obj->flags & flags)
+			obj->flags &= ~flags;
+	}
+}
object.h +2 -0
index b6618d9..6a97b6b 100644
@@ -76,4 +76,6 @@ void add_object_array(struct object *obj, const char *name, struct object_array
 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
 void object_array_remove_duplicates(struct object_array *);
 
+void clear_object_flags(unsigned flags);
+
 #endif /* OBJECT_H */
revision.c +5 -0
index c97d834..77ce6bd 100644
@@ -2061,6 +2061,11 @@ static void set_children(struct rev_info *revs)
 	}
 }
 
+void reset_revision_walk()
+{
+	clear_object_flags(SEEN | ADDED | SHOWN);
+}
+
 int prepare_revision_walk(struct rev_info *revs)
 {
 	int nr = revs->pending.nr;
revision.h +1 -0
index b8e9223..3535733 100644
@@ -192,6 +192,7 @@ extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ct
 				 const char * const usagestr[]);
 extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
 
+extern void reset_revision_walk();
 extern int prepare_revision_walk(struct rev_info *revs);
 extern struct commit *get_revision(struct rev_info *revs);
 extern char *get_revision_mark(const struct rev_info *revs, const struct commit *commit);
submodule.c +2 -0
index 9a28060..645ff5d 100644
@@ -404,6 +404,7 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	while ((commit = get_revision(&rev)) && !needs_pushing)
 		commit_need_pushing(commit, &needs_pushing);
 
+	reset_revision_walk();
 	free(sha1_copy);
 	strbuf_release(&remotes_arg);
 
@@ -741,6 +742,7 @@ static int find_first_merges(struct object_array *result, const char *path,
 		if (in_merge_bases(b, &commit, 1))
 			add_object_array(o, NULL, &merges);
 	}
+	reset_revision_walk();
 
 	/* Now we've got all merges that contain a and b. Prune all
 	 * merges that contain another found merge and save them in
t/t0062-revision-walking.sh new file +33 -0
new file mode 100755
index 0000000..3d98eb8
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Heiko Voigt
+#
+
+test_description='Test revision walking api'
+
+. ./test-lib.sh
+
+cat >run_twice_expected <<-EOF
+1st
+ > add b
+ > add a
+2nd
+ > add b
+ > add a
+EOF
+
+test_expect_success 'setup' '
+	echo a > a &&
+	git add a &&
+	git commit -m "add a" &&
+	echo b > b &&
+	git add b &&
+	git commit -m "add b"
+'
+
+test_expect_success 'revision walking can be done twice' '
+	test-revision-walking run-twice > run_twice_actual
+	test_cmp run_twice_expected run_twice_actual
+'
+
+test_done
test-revision-walking.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
1.7.9.114.gead08
#5 lore raw

PATCH v5 2/3 Refactor submodule push check to use string list instead of integer

In reply to Heiko Voigt

This allows us to tell the user which submodules have not been pushed. Additionally this is helpful when we want to automatically try to push submodules that have not been pushed.

---
 submodule.c |   20 ++++++++++----------
 submodule.h |    3 ++-
 transport.c |   24 ++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 13 deletions(-)
submodule.c +10 -10
index 645ff5d..3c714c2 100644
@@ -357,21 +357,20 @@ static void collect_submodules_from_diff(struct diff_queue_struct *q,
 					 void *data)
 {
 	int i;
-	int *needs_pushing = data;
+	struct string_list *needs_pushing = data;
 
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 		if (!S_ISGITLINK(p->two->mode))
 			continue;
 		if (submodule_needs_pushing(p->two->path, p->two->sha1)) {
-			*needs_pushing = 1;
-			break;
+			if (!string_list_has_string(needs_pushing, p->two->path))
+				string_list_insert(needs_pushing, p->two->path);
 		}
 	}
 }
 
-
-static void commit_need_pushing(struct commit *commit, int *needs_pushing)
+static void commit_need_pushing(struct commit *commit, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 
@@ -382,14 +381,15 @@ static void commit_need_pushing(struct commit *commit, int *needs_pushing)
 	diff_tree_combined_merge(commit, 1, &rev);
 }
 
-int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
+int check_submodule_needs_pushing(unsigned char new_sha1[20],
+		const char *remotes_name, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 	struct commit *commit;
 	const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
 	int argc = ARRAY_SIZE(argv) - 1;
 	char *sha1_copy;
-	int needs_pushing = 0;
+
 	struct strbuf remotes_arg = STRBUF_INIT;
 
 	strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
@@ -401,14 +401,14 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	if (prepare_revision_walk(&rev))
 		die("revision walk setup failed");
 
-	while ((commit = get_revision(&rev)) && !needs_pushing)
-		commit_need_pushing(commit, &needs_pushing);
+	while ((commit = get_revision(&rev)) != NULL)
+		commit_need_pushing(commit, needs_pushing);
 
 	reset_revision_walk();
 	free(sha1_copy);
 	strbuf_release(&remotes_arg);
 
-	return needs_pushing;
+	return needs_pushing->nr;
 }
 
 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
submodule.h +2 -1
index 80e04f3..ddd1941 100644
@@ -29,6 +29,7 @@ int fetch_populated_submodules(int num_options, const char **options,
 unsigned is_submodule_modified(const char *path, int ignore_untracked);
 int merge_submodule(unsigned char result[20], const char *path, const unsigned char base[20],
 		    const unsigned char a[20], const unsigned char b[20], int search);
-int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name);
+int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name,
+		struct string_list *needs_pushing);
 
 #endif
transport.c +22 -2
index cac0c06..d13bd4a 100644
@@ -11,6 +11,7 @@
 #include "branch.h"
 #include "url.h"
 #include "submodule.h"
+#include "string-list.h"
 
 /* rsync support */
 
@@ -1000,6 +1001,20 @@ void transport_set_verbosity(struct transport *transport, int verbosity,
 	transport->progress = force_progress || (verbosity >= 0 && isatty(2));
 }
 
+static void die_with_unpushed_submodules(struct string_list *needs_pushing)
+{
+	int i;
+
+	fprintf(stderr, "The following submodule paths contain changes that can\n"
+			"not be found on any remote:\n");
+	for (i = 0; i < needs_pushing->nr; i++)
+		printf("  %s\n", needs_pushing->items[i].string);
+
+	string_list_clear(needs_pushing, 0);
+
+	die("Aborting.");
+}
+
 int transport_push(struct transport *transport,
 		   int refspec_nr, const char **refspec, int flags,
 		   int *nonfastforward)
@@ -1040,10 +1055,15 @@ int transport_push(struct transport *transport,
 
 		if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
 			struct ref *ref = remote_refs;
+			struct string_list needs_pushing;
+
+			memset(&needs_pushing, 0, sizeof(struct string_list));
+			needs_pushing.strdup_strings = 1;
 			for (; ref; ref = ref->next)
 				if (!is_null_sha1(ref->new_sha1) &&
-				    check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
-					die("There are unpushed submodules, aborting.");
+				    check_submodule_needs_pushing(ref->new_sha1,
+					    transport->remote->name, &needs_pushing))
+					die_with_unpushed_submodules(&needs_pushing);
 		}
 
 		push_ret = transport->push_refs(transport, remote_refs, flags);
Signature · 1 line
1.7.9.114.gead08
#10 lore raw

PATCH v5 3/3 push: teach --recurse-submodules the on-demand option

In reply to Heiko Voigt

When using this option git will search for all submodules that have changed in the revisions to be send. It will then try to push the currently checked out branch of each submodule.

This helps when a user has finished working on a change which involves submodules and just wants to push everything in one go.

---
 Documentation/git-push.txt     |   14 ++++--
 builtin/push.c                 |    7 +++
 submodule.c                    |   48 ++++++++++++++++++++
 submodule.h                    |    1 +
 t/t5531-deep-submodule-push.sh |   94 ++++++++++++++++++++++++++++++++++++++++
 transport.c                    |   17 +++++++-
 transport.h                    |    1 +
 7 files changed, 177 insertions(+), 5 deletions(-)
Documentation/git-push.txt +10 -4
index aede488..649ee3a 100644
@@ -162,10 +162,16 @@ useful if you write an alias or script around 'git push'.
 	is specified. This flag forces progress status even if the
 	standard error stream is not directed to a terminal.
 
---recurse-submodules=check::
-	Check whether all submodule commits used by the revisions to be
-	pushed are available on a remote tracking branch. Otherwise the
-	push will be aborted and the command will exit with non-zero status.
+--recurse-submodules=<check|on-demand>::
+	Make sure all submodule commits used by the revisions to be
+	pushed are available on a remote tracking branch. If check is
+	used it will be checked that all submodule commits that changed
+	in the revisions to be pushed are available on a remote.
+	Otherwise the push will be aborted and exit with non-zero
+	status. If on-demand is used all submodules that changed in the
+	revisions to be pushed will be pushed. If on-demand was not able
+	to push all necessary revisions it will also be aborted and exit
+	with non-zero status.
 
 
 include::urls-remotes.txt[]
builtin/push.c +7 -0
index 35cce53..f2ef8dd 100644
@@ -224,9 +224,16 @@ static int option_parse_recurse_submodules(const struct option *opt,
 				   const char *arg, int unset)
 {
 	int *flags = opt->value;
+
+	if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
+		      TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
+		die("%s can only be used once.", opt->long_name);
+
 	if (arg) {
 		if (!strcmp(arg, "check"))
 			*flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
+		else if (!strcmp(arg, "on-demand"))
+			*flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
 		else
 			die("bad %s argument: %s", opt->long_name, arg);
 	} else
submodule.c +48 -0

54 lines not on this page · raw message

submodule.h +1 -0
index ddd1941..af74941 100644
@@ -31,5 +31,6 @@ int merge_submodule(unsigned char result[20], const char *path, const unsigned c
 		    const unsigned char a[20], const unsigned char b[20], int search);
 int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name,
 		struct string_list *needs_pushing);
+int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);
 
 #endif
t/t5531-deep-submodule-push.sh +94 -0

98 lines not on this page · raw message

transport.c +16 -1
index d13bd4a..8c0fec0 100644
@@ -1009,6 +1009,11 @@ static void die_with_unpushed_submodules(struct string_list *needs_pushing)
 			"not be found on any remote:\n");
 	for (i = 0; i < needs_pushing->nr; i++)
 		printf("  %s\n", needs_pushing->items[i].string);
+	fprintf(stderr, "\nPlease try\n\n"
+			"	git push --recurse-submodules=on-demand\n\n"
+			"or cd to the path and use\n\n"
+			"	git push\n\n"
+			"to push them to a remote.\n\n");
 
 	string_list_clear(needs_pushing, 0);
 
@@ -1053,7 +1058,17 @@ int transport_push(struct transport *transport,
 			flags & TRANSPORT_PUSH_MIRROR,
 			flags & TRANSPORT_PUSH_FORCE);
 
-		if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
+		if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
+			struct ref *ref = remote_refs;
+			for (; ref; ref = ref->next)
+				if (!is_null_sha1(ref->new_sha1) &&
+				    !push_unpushed_submodules(ref->new_sha1,
+					    transport->remote->name))
+				    die ("Failed to push all needed submodules!");
+		}
+
+		if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
+			      TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
 			struct ref *ref = remote_refs;
 			struct string_list needs_pushing;
 
transport.h +1 -0
index 059b330..9d19c78 100644
@@ -102,6 +102,7 @@ struct transport {
 #define TRANSPORT_PUSH_PORCELAIN 16
 #define TRANSPORT_PUSH_SET_UPSTREAM 32
 #define TRANSPORT_RECURSE_SUBMODULES_CHECK 64
+#define TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND 128
 
 #define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
 
Signature · 1 line
1.7.9.114.gead08
#2 lore raw

Re: [PATCH v5 1/3] Teach revision walking machinery to walk multiple times sequencially

In reply to Heiko Voigt
Quoted 5 lines · Heiko Voigt

Previously it was not possible to iterate revisions twice using the revision walking api. We add a reset_revision_walk() which clears the used flags. This allows us to do multiple sequencial revision walks.

I am kind of surprised that this is already its 5th round.

Quoted 9 lines
object.c +1 -0
index 6b06297..6291ce9 100644
@@ -275,3 +275,14 @@ void object_array_remove_duplicates(struct object_array *array)
 		array->nr = dst;
 	}
 }
+

object.c void object_array_remove_duplicates(struct object_array *array) · line 288

Quoted 10 lines
object.c +10 -0
+void clear_object_flags(unsigned flags)
+{
+	int i;
+	struct object *obj;
+
+	for (i=0; i < obj_hash_size; i++) {
+		if ((obj = obj_hash[i]) && obj->flags & flags)
+			obj->flags &= ~flags;
+	}
+}

Minimally,

        void clear_object_flags(unsigned flags)
        {
                int i;

                for (i = 0; i < obj_hash_size; i++) {
                        struct object *obj = obj_hash[i];
                        if (obj && (obj->flags & flags))
                                obj->flags &= ~flags;
                }
        }

I am not sure if the "If there is any bit set we care about, drop them" buys us anything, though.

revision.c static void set_children(struct rev_info *revs) · line 2064

Quoted 9 lines
revision.c +1 -0
index c97d834..77ce6bd 100644
@@ -2061,6 +2061,11 @@ static void set_children(struct rev_info *revs)
 	}
 }
 
+void reset_revision_walk()

void reset_revision_walk(void)

revision.c static void set_children(struct rev_info *revs) · line 2067

Quoted 3 lines
revision.c +3 -0
+{
+	clear_object_flags(SEEN | ADDED | SHOWN);
+}

But is this really the right API? After a particular program finishes using the revision walker, wouldn't it want to clear both the set of these standard flag bits used by the traversal machinery, as well as whatever program specific bits it used to mark the objects with?

revision.h extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ct · line 195

Quoted 7 lines
revision.h +1 -0
index b8e9223..3535733 100644
@@ -192,6 +192,7 @@ extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ct
...
+extern void reset_revision_walk();

Likewise, "extern void reset_revision_walk(void);".

Quoted 13 lines
submodule.c +1 -0
index 9a28060..645ff5d 100644
@@ -404,6 +404,7 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	while ((commit = get_revision(&rev)) && !needs_pushing)
 		commit_need_pushing(commit, &needs_pushing);
 
+	reset_revision_walk();
 	free(sha1_copy);
 	strbuf_release(&remotes_arg);
 

submodule.c static int find_first_merges(struct object_array *result, const char *path, · line 748

Quoted 7 lines
submodule.c +1 -0
@@ -741,6 +742,7 @@ static int find_first_merges(struct object_array *result, const char *path,
 		if (in_merge_bases(b, &commit, 1))
 			add_object_array(o, NULL, &merges);
 	}
+	reset_revision_walk();
 
 	/* Now we've got all merges that contain a and b. Prune all
 	 * merges that contain another found merge and save them in

These two hunk look like a *BUGFIX* to me (certainly it does not look like this is an addition of any new feature).

What bug does this fix, and how is the current submodule code broken without this patch? Can you describe the problem in the log message, and add a test to demonstrate the existing breakage?

#6 lore raw

Re: [PATCH v5 2/3] Refactor submodule push check to use string list instead of integer

In reply to Heiko Voigt
Quoted 3 lines · Heiko Voigt

This allows us to tell the user which submodules have not been pushed. Additionally this is helpful when we want to automatically try to push submodules that have not been pushed.

Makes sense.

Quoted 10 lines
submodule.c +1 -1
index 645ff5d..3c714c2 100644
@@ -357,21 +357,20 @@ static void collect_submodules_from_diff(struct diff_queue_struct *q,
 					 void *data)
 {
 	int i;
-	int *needs_pushing = data;
+	struct string_list *needs_pushing = data;

submodule.c static void collect_submodules_from_diff(struct diff_queue_struct *q, · line 368

Quoted 10 lines
submodule.c +2 -2
 
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 		if (!S_ISGITLINK(p->two->mode))
 			continue;
 		if (submodule_needs_pushing(p->two->path, p->two->sha1)) {
-			*needs_pushing = 1;
-			break;
+			if (!string_list_has_string(needs_pushing, p->two->path))
+				string_list_insert(needs_pushing, p->two->path);

Does string_list API have "look for this and insert if it doesn't exist but otherwise don't do anything"? Running get_entry_index() to answer has_string() once and then calling it again to find where to insert to respond to insert() looks a bit wasteful.

Just wondering.

Quoted 29 lines
submodule.c +4 -4
 		}
 	}
 }
 
-
-static void commit_need_pushing(struct commit *commit, int *needs_pushing)
+static void commit_need_pushing(struct commit *commit, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 
@@ -382,14 +381,15 @@ static void commit_need_pushing(struct commit *commit, int *needs_pushing)
 	diff_tree_combined_merge(commit, 1, &rev);
 }
 
-int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
+int check_submodule_needs_pushing(unsigned char new_sha1[20],
+		const char *remotes_name, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 	struct commit *commit;
 	const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
 	int argc = ARRAY_SIZE(argv) - 1;
 	char *sha1_copy;
-	int needs_pushing = 0;
+
 	struct strbuf remotes_arg = STRBUF_INIT;
 
 	strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);

submodule.c int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote · line 405

Quoted 7 lines
submodule.c +2 -2
@@ -401,14 +401,14 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	if (prepare_revision_walk(&rev))
 		die("revision walk setup failed");
 
-	while ((commit = get_revision(&rev)) && !needs_pushing)
-		commit_need_pushing(commit, &needs_pushing);
+	while ((commit = get_revision(&rev)) != NULL)
+		commit_need_pushing(commit, needs_pushing);

Now the helper function to find list of submodules that need pushing given one commit starting to look more and more misnamed. It used to be "learn if something needs pushing", but now it is "find what needs pushing".

Can somebody think of a good adjective to describe a submodule (or a set of submodules) in this state, so that we can name this helper function find_blue_submodules(), if the adjective were "blue"?

"Unpushed" submodule is the word used in the later part of the patch.

#11 lore raw

Re: [PATCH v5 3/3] push: teach --recurse-submodules the on-demand option

In reply to Heiko Voigt
Quoted 20 lines · Heiko Voigt
submodule.c +12 -0
index 3c714c2..ff0cfd8 100644
@@ -411,6 +411,54 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20],
 	return needs_pushing->nr;
 }
 
+static int push_submodule(const char *path)
+{
+	if (add_submodule_odb(path))
+		return 1;
+
+	if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
+		struct child_process cp;
+		const char *argv[] = {"push", NULL};
+
+		memset(&cp, 0, sizeof(cp));
+		cp.argv = argv;
+		cp.env = local_repo_env;

submodule.c int check_submodule_needs_pushing(unsigned char new_sha1[20], · line 435

Quoted 10 lines
submodule.c +10 -0
+		cp.git_cmd = 1;
+		cp.no_stdin = 1;
+		cp.dir = path;
+		if (run_command(&cp))
+			return 0;
+		close(cp.out);
+	}
+
+	return 1;
+}

Hmm, this makes me wonder if we fire subprocesses and have them run in parallel (to a reasonably limited parallelism), it might make the overall user experience more pleasant, and if we did the same on the fetching side, it would be even nicer.

We would need to keep track of children and after firing a handful of them we would need to start waiting for some to finish and collect their exit status before firing more, and at the end we would need to wait for the remaining ones and find how each one of them did before returning from push_unpushed_submodules(). If we were to do so, what are the missing support we would need from the run_command() subsystem?

Quoted 19 lines
submodule.c +19 -0
+int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
+{
+	int i, ret = 1;
+	struct string_list needs_pushing;
+
+	memset(&needs_pushing, 0, sizeof(struct string_list));
+	needs_pushing.strdup_strings = 1;
+
+	if (!check_submodule_needs_pushing(new_sha1, remotes_name, &needs_pushing))
+		return 1;
+
+	for (i = 0; i < needs_pushing.nr; i++) {
+		const char *path = needs_pushing.items[i].string;
+		fprintf(stderr, "Pushing submodule '%s'\n", path);
+		if (!push_submodule(path)) {
+			fprintf(stderr, "Unable to push submodule '%s'\n", path);
+			ret = 0;
+		}
+	}
#12 lore raw

Re: [PATCH v5 3/3] push: teach --recurse-submodules the on-demand option

Am 14.02.2012 04:34, schrieb Junio C Hamano:

Quoted 29 lines
Quoted 27 lines · Heiko Voigt
submodule.c +19 -0
index 3c714c2..ff0cfd8 100644
@@ -411,6 +411,54 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20],
 	return needs_pushing->nr;
 }
 
+static int push_submodule(const char *path)
+{
+	if (add_submodule_odb(path))
+		return 1;
+
+	if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
+		struct child_process cp;
+		const char *argv[] = {"push", NULL};
+
+		memset(&cp, 0, sizeof(cp));
+		cp.argv = argv;
+		cp.env = local_repo_env;
+		cp.git_cmd = 1;
+		cp.no_stdin = 1;
+		cp.dir = path;
+		if (run_command(&cp))
+			return 0;
+		close(cp.out);
+	}
Quoted 8 lines

submodule.c int check_submodule_needs_pushing(unsigned char new_sha1[20], · line 435

Quoted 3 lines
submodule.c +3 -0
+
+	return 1;
+}

Hmm, this makes me wonder if we fire subprocesses and have them run in parallel (to a reasonably limited parallelism), it might make the overall user experience more pleasant, and if we did the same on the fetching side, it would be even nicer.

Yeah, I had the same idea and did some experiments when working on fetch some time ago.

Quoted 6 lines

We would need to keep track of children and after firing a handful of them we would need to start waiting for some to finish and collect their exit status before firing more, and at the end we would need to wait for the remaining ones and find how each one of them did before returning from push_unpushed_submodules(). If we were to do so, what are the missing support we would need from the run_command() subsystem?

We would not only have to collect the exit status but also the output lines. You don't want to see the output of multiple fetches or pushes mixed together, so it makes sense to just defer that until the command exited and then print everything at once. The interesting part I couldn't come up with an easy solution for is to preserve the output order between the stdout and stdin lines, as they contain different parts of the progress which would look strange when shuffled around.

And I saw that sometimes parallel fetches took way longer than doing them sequentially (in my case because of strange DNS behavior of my DSL router), so we would definitely want a config option for that (maybe setting the maximum number of simultaneous threads to be used).

But don't get me wrong, I'm all for having that feature! :-)

#3 lore raw

Re: Re: [PATCH v5 1/3] Teach revision walking machinery to walk multiple times sequencially

In reply to Junio C Hamano

Hi,

sorry that my reply to this took quite some time.

Quoted 9 lines · Junio C Hamano
Quoted 5 lines · Heiko Voigt

Previously it was not possible to iterate revisions twice using the revision walking api. We add a reset_revision_walk() which clears the used flags. This allows us to do multiple sequencial revision walks.

I am kind of surprised that this is already its 5th round.

Well, not this patch in particular but the series so I chose to it for the whole series.

Quoted 13 lines

Minimally,

        void clear_object_flags(unsigned flags)
        {
                int i;

                for (i = 0; i < obj_hash_size; i++) {
                        struct object *obj = obj_hash[i];
                        if (obj && (obj->flags & flags))
                                obj->flags &= ~flags;
                }
        }
Quoted 2 lines

I am not sure if the "If there is any bit set we care about, drop them" buys us anything, though.

Ok done. And also dropped the "if there is any bit set" condition.

Quoted 3 lines
Quoted 1 line

+void reset_revision_walk()

void reset_revision_walk(void)

Done.

Quoted 8 lines

revision.c static void set_children(struct rev_info *revs) · line 2067

Quoted 3 lines
revision.c +3 -0
+{
+	clear_object_flags(SEEN | ADDED | SHOWN);
+}

But is this really the right API? After a particular program finishes using the revision walker, wouldn't it want to clear both the set of these standard flag bits used by the traversal machinery, as well as whatever program specific bits it used to mark the objects with?

Well if a program uses extra flags on objects it should clear the flags it set by using the clear_objects_flags() function itself. For example if the program wants to reuse those extra flags in a second revision walk it would not be possible if reset_revision_walk() would clear all flags.

Quoted 9 lines

revision.h extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ct · line 195

Quoted 7 lines
revision.h +1 -0
index b8e9223..3535733 100644
@@ -192,6 +192,7 @@ extern void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ct
...
+extern void reset_revision_walk();

Likewise, "extern void reset_revision_walk(void);".

Done.

Quoted 25 lines
Quoted 14 lines
submodule.c +1 -0
index 9a28060..645ff5d 100644
@@ -404,6 +404,7 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	while ((commit = get_revision(&rev)) && !needs_pushing)
 		commit_need_pushing(commit, &needs_pushing);
 
+	reset_revision_walk();
 	free(sha1_copy);
 	strbuf_release(&remotes_arg);
 

submodule.c static int find_first_merges(struct object_array *result, const char *path, · line 748

Quoted 7 lines
submodule.c +1 -0
@@ -741,6 +742,7 @@ static int find_first_merges(struct object_array *result, const char *path,
 		if (in_merge_bases(b, &commit, 1))
 			add_object_array(o, NULL, &merges);
 	}
+	reset_revision_walk();
 
 	/* Now we've got all merges that contain a and b. Prune all
 	 * merges that contain another found merge and save them in

These two hunk look like a *BUGFIX* to me (certainly it does not look like this is an addition of any new feature).

Quoted 3 lines

What bug does this fix, and how is the current submodule code broken without this patch? Can you describe the problem in the log message, and add a test to demonstrate the existing breakage?

There is no breakage I know of. Its rather a cleanup which allows to call these functions multiple times. I did this to avoid surprises. Should I put these in a separate patch or just add an explanation into the current one?

Cheers Heiko

#4 lore raw

Re: [PATCH v5 1/3] Teach revision walking machinery to walk multiple times sequencially

Quoted 9 lines · Heiko Voigt
Quoted 8 lines

revision.c static void set_children(struct rev_info *revs) · line 2067

Quoted 3 lines
revision.c +3 -0
+{
+	clear_object_flags(SEEN | ADDED | SHOWN);
+}

But is this really the right API? After a particular program finishes using the revision walker, wouldn't it want to clear both the set of these standard flag bits used by the traversal machinery, as well as whatever program specific bits it used to mark the objects with?

Quoted 4 lines

Well if a program uses extra flags on objects it should clear the flags it set by using the clear_objects_flags() function itself. For example if the program wants to reuse those extra flags in a second revision walk it would not be possible if reset_revision_walk() would clear all flags.

OK.

Quoted 9 lines
Quoted 6 lines

These two hunk look like a *BUGFIX* to me (certainly it does not look like this is an addition of any new feature).

What bug does this fix, and how is the current submodule code broken without this patch? Can you describe the problem in the log message, and add a test to demonstrate the existing breakage?

There is no breakage I know of. Its rather a cleanup which allows to call these functions multiple times. I did this to avoid surprises.

OK.

Thanks.

#7 lore raw

Re: [PATCH v5 2/3] Refactor submodule push check to use string list instead of integer

In reply to Junio C Hamano
Quoted 27 lines · Junio C Hamano
Quoted 10 lines · Heiko Voigt
submodule.c +1 -1
index 645ff5d..3c714c2 100644
@@ -357,21 +357,20 @@ static void collect_submodules_from_diff(struct diff_queue_struct *q,
 					 void *data)
 {
 	int i;
-	int *needs_pushing = data;
+	struct string_list *needs_pushing = data;

submodule.c static void collect_submodules_from_diff(struct diff_queue_struct *q, · line 368

Quoted 10 lines
submodule.c +2 -2
 
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 		if (!S_ISGITLINK(p->two->mode))
 			continue;
 		if (submodule_needs_pushing(p->two->path, p->two->sha1)) {
-			*needs_pushing = 1;
-			break;
+			if (!string_list_has_string(needs_pushing, p->two->path))
+				string_list_insert(needs_pushing, p->two->path);

Does string_list API have "look for this and insert if it doesn't exist but otherwise don't do anything"? Running get_entry_index() to answer has_string() once and then calling it again to find where to insert to respond to insert() looks a bit wasteful.

Quoted 1 line

Just wondering.

If I see correctly currently it has no such functionality. I can have a look at adding another patch implementing this.

Quoted 37 lines
Quoted 25 lines
submodule.c +4 -4
-
-static void commit_need_pushing(struct commit *commit, int *needs_pushing)
+static void commit_need_pushing(struct commit *commit, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 
@@ -382,14 +381,15 @@ static void commit_need_pushing(struct commit *commit, int *needs_pushing)
 	diff_tree_combined_merge(commit, 1, &rev);
 }
 
-int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
+int check_submodule_needs_pushing(unsigned char new_sha1[20],
+		const char *remotes_name, struct string_list *needs_pushing)
 {
 	struct rev_info rev;
 	struct commit *commit;
 	const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
 	int argc = ARRAY_SIZE(argv) - 1;
 	char *sha1_copy;
-	int needs_pushing = 0;
+
 	struct strbuf remotes_arg = STRBUF_INIT;
 
 	strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);

submodule.c int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote · line 405

Quoted 7 lines
submodule.c +2 -2
@@ -401,14 +401,14 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remote
 	if (prepare_revision_walk(&rev))
 		die("revision walk setup failed");
 
-	while ((commit = get_revision(&rev)) && !needs_pushing)
-		commit_need_pushing(commit, &needs_pushing);
+	while ((commit = get_revision(&rev)) != NULL)
+		commit_need_pushing(commit, needs_pushing);

Now the helper function to find list of submodules that need pushing given one commit starting to look more and more misnamed. It used to be "learn if something needs pushing", but now it is "find what needs pushing".

Quoted 5 lines

Can somebody think of a good adjective to describe a submodule (or a set of submodules) in this state, so that we can name this helper function find_blue_submodules(), if the adjective were "blue"?

"Unpushed" submodule is the word used in the later part of the patch.

So why not use unpushed as the adjective? find_unpushed_submodules() sound good to me. Will change the patch accordingly. Since we are already discussing renaming should I also rename commit_need_pushing() to find_unpushed_submodule_commits()? I think that would make the whole naming more consistent.

Cheers Heiko

#8 lore raw

Re: [PATCH v5 2/3] Refactor submodule push check to use string list instead of integer

Quoted 12 lines · Heiko Voigt
Quoted 11 lines · Junio C Hamano

submodule.c static void collect_submodules_from_diff(struct diff_queue_struct *q, · line 368

Quoted 4 lines · Heiko Voigt
submodule.c +2 -2
-			*needs_pushing = 1;
-			break;
+			if (!string_list_has_string(needs_pushing, p->two->path))
+				string_list_insert(needs_pushing, p->two->path);

Does string_list API have "look for this and insert if it doesn't exist but otherwise don't do anything"? Running get_entry_index() to answer has_string() once and then calling it again to find where to insert to respond to insert() looks a bit wasteful.

Quoted 4 lines
Quoted 1 line

Just wondering.

If I see correctly currently it has no such functionality. I can have a look at adding another patch implementing this.

It seems my guess was wrong. As far as I read in the code string_list_insert() already skips inserting existing strings and just returns the existing items in the list. So it should be fine to remove the string_list_has_string() call.

Cheers Heiko

#9 lore raw

Re: [PATCH v5 2/3] Refactor submodule push check to use string list instead of integer

Quoted 4 lines · Heiko Voigt

It seems my guess was wrong. As far as I read in the code string_list_insert() already skips inserting existing strings and just returns the existing items in the list. So it should be fine to remove the string_list_has_string() call.

Sounds sane. Thanks.

#13 lore raw

Re: [PATCH v5 3/3] push: teach --recurse-submodules the on-demand option

In reply to Jens Lehmann
Quoted 57 lines · Jens Lehmann

Am 14.02.2012 04:34, schrieb Junio C Hamano:

Quoted 29 lines
Quoted 27 lines · Heiko Voigt
submodule.c +19 -0
index 3c714c2..ff0cfd8 100644
@@ -411,6 +411,54 @@ int check_submodule_needs_pushing(unsigned char new_sha1[20],
 	return needs_pushing->nr;
 }
 
+static int push_submodule(const char *path)
+{
+	if (add_submodule_odb(path))
+		return 1;
+
+	if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
+		struct child_process cp;
+		const char *argv[] = {"push", NULL};
+
+		memset(&cp, 0, sizeof(cp));
+		cp.argv = argv;
+		cp.env = local_repo_env;
+		cp.git_cmd = 1;
+		cp.no_stdin = 1;
+		cp.dir = path;
+		if (run_command(&cp))
+			return 0;
+		close(cp.out);
+	}
Quoted 8 lines

submodule.c int check_submodule_needs_pushing(unsigned char new_sha1[20], · line 435

Quoted 3 lines
submodule.c +3 -0
+
+	return 1;
+}

Hmm, this makes me wonder if we fire subprocesses and have them run in parallel (to a reasonably limited parallelism), it might make the overall user experience more pleasant, and if we did the same on the fetching side, it would be even nicer.

Yeah, I had the same idea and did some experiments when working on fetch some time ago.

Quoted 6 lines

We would need to keep track of children and after firing a handful of them we would need to start waiting for some to finish and collect their exit status before firing more, and at the end we would need to wait for the remaining ones and find how each one of them did before returning from push_unpushed_submodules(). If we were to do so, what are the missing support we would need from the run_command() subsystem?

We would not only have to collect the exit status but also the output lines. You don't want to see the output of multiple fetches or pushes mixed together, so it makes sense to just defer that until the command exited and then print everything at once. The interesting part I couldn't come up with an easy solution for is to preserve the output order between the stdout and stdin lines, as they contain different parts of the progress which would look strange when shuffled around.

Quoted 6 lines

And I saw that sometimes parallel fetches took way longer than doing them sequentially (in my case because of strange DNS behavior of my DSL router), so we would definitely want a config option for that (maybe setting the maximum number of simultaneous threads to be used).

But don't get me wrong, I'm all for having that feature! :-)

Me too, but I would suggest that we first implement the basic recursive push functionality and then start to do optimizations like this. I think we will definitely be interested in parallel pushing once we start using it.

Cheers Heiko

#15 lore raw

Re: [PATCH v5 3/3] push: teach --recurse-submodules the on-demand option

In reply to Heiko Voigt
Quoted 11 lines · Heiko Voigt

When using this option git will search for all submodules that have changed in the revisions to be send. It will then try to push the currently checked out branch of each submodule.

This helps when a user has finished working on a change which involves submodules and just wants to push everything in one go.

---recurse-submodules=check::

Documentation/git-push.txt +0 -3
-	Check whether all submodule commits used by the revisions to be
-	pushed are available on a remote tracking branch. Otherwise the
-	push will be aborted and the command will exit with non-zero status.

Documentation/git-push.txt useful if you write an alias or script around 'git push'. · line 174

Quoted 10 lines
Documentation/git-push.txt +10 -0
+--recurse-submodules=<check|on-demand>::
+	Make sure all submodule commits used by the revisions to be
+	pushed are available on a remote tracking branch. If check is
+	used it will be checked that all submodule commits that changed
+	in the revisions to be pushed are available on a remote.
+	Otherwise the push will be aborted and exit with non-zero
+	status. If on-demand is used all submodules that changed in the
+	revisions to be pushed will be pushed. If on-demand was not able
+	to push all necessary revisions it will also be aborted and exit
+	with non-zero status.

Hi,
this desciption seems awkward. Not sure how to improve it, but:
- the argument 'check' is changed to '<check|on-demand>', i.e. brackets are added. This changes the meaning, because brackets are used around a name for a value provided by the user. So here brackets shouldn't be used, because 'check' and 'on-demand' are literals.
- s/if check is used it will be checked/if check is used git will verify that/
- s/a remote/the remote/
- s/Otherwise the push/If any commits are missing the push/ (because 'Otherwise' could refer to 'If check is used'.)

Zbyszek

#16 lore raw

Re: [PATCH v5 3/3] push: teach --recurse-submodules the on-demand option

Quoted 25 lines · Zbigniew J??drzejewski-Szmek
Quoted 11 lines · Heiko Voigt

When using this option git will search for all submodules that have changed in the revisions to be send. It will then try to push the currently checked out branch of each submodule.

This helps when a user has finished working on a change which involves submodules and just wants to push everything in one go.

---recurse-submodules=check::

Documentation/git-push.txt +0 -3
-	Check whether all submodule commits used by the revisions to be
-	pushed are available on a remote tracking branch. Otherwise the
-	push will be aborted and the command will exit with non-zero status.

Documentation/git-push.txt useful if you write an alias or script around 'git push'. · line 174

Quoted 10 lines
Documentation/git-push.txt +10 -0
+--recurse-submodules=<check|on-demand>::
+	Make sure all submodule commits used by the revisions to be
+	pushed are available on a remote tracking branch. If check is
+	used it will be checked that all submodule commits that changed
+	in the revisions to be pushed are available on a remote.
+	Otherwise the push will be aborted and exit with non-zero
+	status. If on-demand is used all submodules that changed in the
+	revisions to be pushed will be pushed. If on-demand was not able
+	to push all necessary revisions it will also be aborted and exit
+	with non-zero status.

Hi,
this desciption seems awkward. Not sure how to improve it, but:
- the argument 'check' is changed to '<check|on-demand>', i.e. brackets

Quoted 8 lines

are added. This changes the meaning, because brackets are used around a name for a value provided by the user. So here brackets shouldn't be used, because 'check' and 'on-demand' are literals.
- s/if check is used it will be checked/if check is used git will verify that/
- s/a remote/the remote/
- s/Otherwise the push/If any commits are missing the push/ (because 'Otherwise' could refer to 'If check is used'.)

I agree on all of these except

s/a remote/the remote/

which I changed to

s/a remote/at least one remote of the submodule/

since it really just checks whether the commits are on some remote. It it not strictly the one defined in .gitmodules. Doing it this way allows to work with forks without the need to change .gitmodules.

Cheers Heiko

#14 lore raw

[RFC/PATCH] read from 2 filedescriptors simultaneously into one strbuf

In reply to Jens Lehmann

Hi,

Quoted 16 lines · Jens Lehmann

Am 14.02.2012 04:34, schrieb Junio C Hamano:

Quoted 4 lines

Hmm, this makes me wonder if we fire subprocesses and have them run in parallel (to a reasonably limited parallelism), it might make the overall user experience more pleasant, and if we did the same on the fetching side, it would be even nicer.

Yeah, I had the same idea and did some experiments when working on fetch some time ago.

Quoted 6 lines

We would need to keep track of children and after firing a handful of them we would need to start waiting for some to finish and collect their exit status before firing more, and at the end we would need to wait for the remaining ones and find how each one of them did before returning from push_unpushed_submodules(). If we were to do so, what are the missing support we would need from the run_command() subsystem?

Quoted 7 lines

We would not only have to collect the exit status but also the output lines. You don't want to see the output of multiple fetches or pushes mixed together, so it makes sense to just defer that until the command exited and then print everything at once. The interesting part I couldn't come up with an easy solution for is to preserve the output order between the stdout and stdin lines, as they contain different parts of the progress which would look strange when shuffled around.

I had a go at a possible implementation for deferred output of parallel subprocesses today. The idea is to use two concurrent threads simultaneously reading from the two filedescriptors and writing the result into one strbuf. Below is the diff for the current code I have[1].

One current limitation is that it only works with pthreads enabled. I first tried to use the same pipe for the two threads to avoid the need for locking and shared memory. I am not sure but it seems that concurrents writes into the same pipe are not allowed.

What do you think of this approach? Is it the correct place or should it go into another module? Also the function naming might need some polishing.

Cheers Heiko

P.S.: We can of course get rid of the second thread and just read in the main thread for one of the filedescriptors but this was easier while thinking about it.

[1]

run-command.c +56 -0
index 2a1041e..48c9e07 100644
@@ -1,6 +1,8 @@
 #include "cache.h"
 #include "run-command.h"
 #include "exec_cmd.h"
+#include "strbuf.h"
+#include <pthread.h>
 
 static inline void close_pair(int fd[2])
 {
@@ -407,6 +409,60 @@ fail_pipe:
 	return 0;
 }
 
+#ifndef NO_PTHREADS
+struct fd2fd_data {
+	pthread_mutex_t mutex;
+	struct strbuf *output;
+};
+
+static int fd2fd(int in, int out, void *data)
+{
+	struct fd2fd_data *me = data;
+	struct strbuf line;
+
+	FILE *fin = xfdopen(in, "r");
+
+	while (strbuf_getwholeline(&line, fin, '\n') != EOF) {
+		pthread_mutex_lock(&me->mutex);
+		strbuf_addbuf(me->output, &line);
+		pthread_mutex_unlock(&me->mutex);
+	}
+
+	strbuf_release(&line);
+	return 0;
+}
+
+void read_2_fds_into_strbuf(int fd1, int fd2, struct strbuf *output)
+{
+	struct async err_async;
+	struct async out_async;
+	struct fd2fd_data async_data;
+
+	pthread_mutex_init(&async_data.mutex, NULL);
+	async_data.output = output;
+
+	/* start two threads to read fd1 and fd2 simultaneously
+	 * into one strbuf */
+	memset(&out_async, 0, sizeof(out_async));
+	out_async.proc = fd2fd;
+	out_async.data = &async_data;
+	out_async.in = fd1;
+
+	memset(&err_async, 0, sizeof(err_async));
+	err_async.proc = fd2fd;
+	err_async.data = &async_data;
+	err_async.in = fd2;
+
+	start_async(&out_async);
+	start_async(&err_async);
+
+	finish_async(&out_async);
+	finish_async(&err_async);
+
+	pthread_mutex_destroy(&async_data.mutex);
+}
+#endif /* NO_PTHREADS */
+
 int finish_command(struct child_process *cmd)
 {
 	return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
run-command.h +6 -0
index 56491b9..a65debb 100644
@@ -5,6 +5,8 @@
 #include <pthread.h>
 #endif
 
+struct strbuf;
+
 struct child_process {
 	const char **argv;
 	pid_t pid;
@@ -90,4 +92,8 @@ struct async {
 int start_async(struct async *async);
 int finish_async(struct async *async);
 
+#ifndef NO_PTHREADS
+void read_2_fds_into_strbuf(int fd1, int fd2, struct strbuf *output);
+#endif
+
 #endif
t/t0061-run-command.sh +6 -0
index 10b26e4..774807b 100755
@@ -11,4 +11,10 @@ test_expect_success 'start_command reports ENOENT' '
 	test-run-command start-command-ENOENT ./does-not-exist
 '
 
+test_expect_success 'read_2_fds_into_strbuf basic behavior' '
+	test-run-command out2strbuf >actual &&
+	grep "^Hallo Stdout$" actual &&
+	grep "^Hallo Stderr$" actual
+'
+
 test_done
test-run-command.c +48 -2

74 lines not on this page · raw message