Skip to contentSkip to conversation

Refactor the filter process code into a reusable module

PATCH Superseded Ben Peart 4 participants 15 messages Mar 24, 2017 – Mar 30, 2017

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Ben Peart

author
#0 lore raw

v2 cover Refactor the filter process code into a reusable module

We have a couple of patch series we�re working on (ObjectDB/Read-Object, Watchman integration) where we need the ability to have a background process running that can accept multiple commands thus avoiding the overhead of spawning a new process for every command.

The ability to do this was added in:

	Commit edcc85814c ("convert: add filter.<driver>.process option", 2016-10-16)
	keeps the external process running and processes all commands

but it is integrated into the convert code. This patch series takes the code from convert.c and refactors it into a separate "sub-process" module so that we can centralize and reuse this logic in other areas.

Ben Peart (2):
pkt-line: add packet_writel() and packet_read_line_gently()
sub-process: refactor the filter process code into a reusable module

 Documentation/technical/api-sub-process.txt |  54 ++++++++++
 Makefile                                    |   1 +
 convert.c                                   | 160 ++++++----------------------
 pkt-line.c                                  |  31 ++++++
 pkt-line.h                                  |  11 ++
 sub-process.c                               | 117 ++++++++++++++++++++
 sub-process.h                               |  46 ++++++++
 7 files changed, 290 insertions(+), 130 deletions(-)
 create mode 100644 Documentation/technical/api-sub-process.txt
 create mode 100644 sub-process.c
 create mode 100644 sub-process.h
Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
author
#1 lore raw

PATCH v2 1/2 pkt-line: add packet_writel() and packet_read_line_gently()

Add packet_writel() which writes multiple lines in a single call and then calls packet_flush_gently(). Add packet_read_line_gently() to enable reading a line without dying on EOF.

---
 pkt-line.c | 31 +++++++++++++++++++++++++++++++
 pkt-line.h | 11 +++++++++++
 2 files changed, 42 insertions(+)
pkt-line.c +31 -0
index d4b6bfe076..2788aa1af6 100644
@@ -171,6 +171,25 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
 	return status;
 }
 
+int packet_writel(int fd, const char *line, ...)
+{
+	va_list args;
+	int err;
+	va_start(args, line);
+	for (;;) {
+		if (!line)
+			break;
+		if (strlen(line) > LARGE_PACKET_DATA_MAX)
+			return -1;
+		err = packet_write_fmt_gently(fd, "%s\n", line);
+		if (err)
+			return err;
+		line = va_arg(args, const char*);
+	}
+	va_end(args);
+	return packet_flush_gently(fd);
+}
+
 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
 {
 	static char packet_write_buffer[LARGE_PACKET_MAX];
@@ -323,6 +342,18 @@ char *packet_read_line(int fd, int *len_p)
 	return packet_read_line_generic(fd, NULL, NULL, len_p);
 }
 
+int packet_read_line_gently(int fd, int *dst_len, char** dst_line)
+{
+	int len = packet_read(fd, NULL, NULL,
+		packet_buffer, sizeof(packet_buffer),
+		PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
+	if (dst_len)
+		*dst_len = len;
+	if (dst_line)
+		*dst_line = (len > 0) ? packet_buffer : NULL;
+	return len;
+}
+
 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
 {
 	return packet_read_line_generic(-1, src, src_len, dst_len);
pkt-line.h +11 -0
index 18eac64830..cb3eda9695 100644
@@ -25,6 +25,7 @@ void packet_buf_flush(struct strbuf *buf);
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
 int packet_flush_gently(int fd);
 int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
+int packet_writel(int fd, const char *line, ...);
 int write_packetized_from_fd(int fd_in, int fd_out);
 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out);
 
@@ -74,6 +75,16 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, char
 char *packet_read_line(int fd, int *size);
 
 /*
+ * Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF
+ * and CHOMP_NEWLINE options. The return value specifies the number of bytes
+ * read into the buffer or -1 on truncated input. if the *dst_line parameter
+ * is not NULL it will return NULL for a flush packet and otherwise points to
+ * a static buffer (that may be overwritten by subsequent calls). If the size
+ * parameter is not NULL, the length of the packet is written to it.
+ */
+int packet_read_line_gently(int fd, int *size, char** dst_line);
+
+/*
  * Same as packet_read_line, but read from a buf rather than a descriptor;
  * see packet_read for details on how src_* is used.
  */
Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
#2 lore raw

Re: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

Quoted 15 lines · Ben Peart

Add packet_writel() which writes multiple lines in a single call and then calls packet_flush_gently(). Add packet_read_line_gently() to enable reading a line without dying on EOF.

---
 pkt-line.c | 31 +++++++++++++++++++++++++++++++
 pkt-line.h | 11 +++++++++++
 2 files changed, 42 insertions(+)
pkt-line.c +0 -0
index d4b6bfe076..2788aa1af6 100644

pkt-line.c int packet_write_fmt_gently(int fd, const char *fmt, ...) · line 174

Quoted 4 lines
pkt-line.c +1 -0
@@ -171,6 +171,25 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
 	return status;
 }
 
+int packet_writel(int fd, const char *line, ...)

The name packet_writel is hard to distinguish from packet_write. Would packet_write_lines make more sense ?

Quoted 8 lines
pkt-line.c +8 -0
+{
+	va_list args;
+	int err;
+	va_start(args, line);
+	for (;;) {
+		if (!line)
+			break;
+		if (strlen(line) > LARGE_PACKET_DATA_MAX)

pkt-line.c int packet_write_fmt_gently(int fd, const char *fmt, ...) · line 192

Quoted 10 lines
pkt-line.c +10 -0
+			return -1;
+		err = packet_write_fmt_gently(fd, "%s\n", line);
+		if (err)
+			return err;
+		line = va_arg(args, const char*);
+	}
+	va_end(args);
+	return packet_flush_gently(fd);
+}
+

I don't think that this va_start() is needed, even if it works.

int packet_write_line(int fd, const char *lines[]) |

	const char *line = *lines;
	int err;
	while (line) {
		if (strlen(line) > LARGE_PACKET_DATA_MAX)
			return -1;
		err = packet_write_fmt_gently(fd, "%s\n", line);
		if (err)
			return err;
		lines++;
		line = *lines;
	}
	return packet_flush_gently(fd);

]

#3 lore raw

RE: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

Quoted 26 lines

-----Original Message-----
From: Torsten Bögershausen [mailto:[email protected]]
Sent: Saturday, March 25, 2017 1:47 AM
To: Ben Peart <[email protected]>; [email protected]
Cc: Ben Peart <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

pkt-line.c int packet_write_fmt_gently(int fd, const char

Quoted 15 lines · Ben Peart

Add packet_writel() which writes multiple lines in a single call and then calls packet_flush_gently(). Add packet_read_line_gently() to enable reading a line without dying on EOF.

---
 pkt-line.c | 31 +++++++++++++++++++++++++++++++  pkt-line.h | 11
+++++++++++
 2 files changed, 42 insertions(+)
pkt-line.c +0 -0
index d4b6bfe076..2788aa1af6 100644
@@ -171,6 +171,25 @@ int packet_write_fmt_gently(int fd, const char

*fmt, ...)

Quoted 7 lines

pkt-line.c int packet_write_fmt_gently(int fd, const char *fmt, ...) · line 174

Quoted 4 lines
pkt-line.c +1 -0
 	return status;
 }
 
+int packet_writel(int fd, const char *line, ...)

The name packet_writel is hard to distinguish from packet_write. Would packet_write_lines make more sense ?

Just goes to prove that there are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. :) The feedback on V1 was:

"I am hesitant to take a function that does not take any "list"
type argument and yet calls itself "write_list". IOW, I'd expect something like these

	write_list(int fd, const char **lines);
	write_list(int fd, struct string_list *lines);

given that name, and not "varargs, each of which is a line". I am tempted to suggest

packet_writel(int fd, const char *line, ...);"

Quoted 34 lines
Quoted 8 lines
pkt-line.c +8 -0
+{
+	va_list args;
+	int err;
+	va_start(args, line);
+	for (;;) {
+		if (!line)
+			break;
+		if (strlen(line) > LARGE_PACKET_DATA_MAX)

pkt-line.c int packet_write_fmt_gently(int fd, const char *fmt, ...) · line 192

Quoted 10 lines
pkt-line.c +10 -0
+			return -1;
+		err = packet_write_fmt_gently(fd, "%s\n", line);
+		if (err)
+			return err;
+		line = va_arg(args, const char*);
+	}
+	va_end(args);
+	return packet_flush_gently(fd);
+}
+

I don't think that this va_start() is needed, even if it works.

int packet_write_line(int fd, const char *lines[]) |

	const char *line = *lines;
	int err;
	while (line) {
		if (strlen(line) > LARGE_PACKET_DATA_MAX)
			return -1;
		err = packet_write_fmt_gently(fd, "%s\n", line);
		if (err)
			return err;
		lines++;
		line = *lines;
	}
	return packet_flush_gently(fd);
Quoted 2 lines

]

This is a helper function to simply the common pattern of writing out a variable number of lines followed by a flush. The usage of the function as currently implemented is:

packet_writel(fd, "line one", "line two", "line n");

which requires the use of variable number of arguments. With your proposal that convenience is lost as you have to create an array of strings and pass that instead. The usage just isn't as simple as the current model.

#4 lore raw

Re: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

[snip]

Quoted 52 lines
Quoted 2 lines

Would packet_write_lines make more sense ?

Just goes to prove that there are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. :) The feedback on V1 was:

"I am hesitant to take a function that does not take any "list"
type argument and yet calls itself "write_list". IOW, I'd expect something like these

	write_list(int fd, const char **lines);
	write_list(int fd, struct string_list *lines);

given that name, and not "varargs, each of which is a line". I am tempted to suggest

packet_writel(int fd, const char *line, ...);"

Quoted 34 lines
Quoted 8 lines
pkt-line.c +8 -0
+{
+	va_list args;
+	int err;
+	va_start(args, line);
+	for (;;) {
+		if (!line)
+			break;
+		if (strlen(line) > LARGE_PACKET_DATA_MAX)

pkt-line.c int packet_write_fmt_gently(int fd, const char *fmt, ...) · line 192

Quoted 10 lines
pkt-line.c +10 -0
+			return -1;
+		err = packet_write_fmt_gently(fd, "%s\n", line);
+		if (err)
+			return err;
+		line = va_arg(args, const char*);
+	}
+	va_end(args);
+	return packet_flush_gently(fd);
+}
+

I don't think that this va_start() is needed, even if it works.

int packet_write_line(int fd, const char *lines[]) |

	const char *line = *lines;
	int err;
	while (line) {
		if (strlen(line) > LARGE_PACKET_DATA_MAX)
			return -1;
		err = packet_write_fmt_gently(fd, "%s\n", line);
		if (err)
			return err;
		lines++;
		line = *lines;
	}
	return packet_flush_gently(fd);
Quoted 2 lines

]

Quoted 3 lines

This is a helper function to simply the common pattern of writing out a variable number of lines followed by a flush. The usage of the function as currently implemented is:

packet_writel(fd, "line one", "line two", "line n");

Does this work ?
I would have expected
packet_writel(fd, "line one", "line two", "line n"), NULL;

Quoted 3 lines

which requires the use of variable number of arguments. With your proposal that convenience is lost as you have to create an array of strings and pass that instead. The usage just isn't as simple as the current model.

What is wrong with

int packet_write_fmt_gently(int fd, const char *fmt, ...)
and we use it like this:
if packet_write_fmt_gently(fd, "%s%s%s", "line one", "line two", "line n")

(Or do we need another one like)
int packet_write_fmt_gently_flush(int fd, const char *fmt, ...)

Sorry if I am lost here

#5 lore raw

RE: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

Quoted 7 lines

From: Torsten Bögershausen [mailto:[email protected]]

Does this work ?
I would have expected
packet_writel(fd, "line one", "line two", "line n"), NULL;

No, that's actually not valid C syntax.

Quoted 11 lines
Quoted 2 lines

which requires the use of variable number of arguments. With your

proposal that convenience is lost as you have to create an array of strings and pass that instead. The usage just isn't as simple as the current model.

Quoted 1 line

What is wrong with

int packet_write_fmt_gently(int fd, const char *fmt, ...) and we use it like this:
if packet_write_fmt_gently(fd, "%s%s%s", "line one", "line two", "line n")

Packets are not just strings; see pkt-line.h for more details but basically they are a length packet, followed by the data (in this particular case a string). The packet_writel function is a convenience function to write out a variable number of packetized strings followed by a flush packet. You're sample above would simply concatenate the three strings and then write a single packet. A very different outcome. :)

Quoted 6 lines

(Or do we need another one like)
int packet_write_fmt_gently_flush(int fd, const char *fmt, ...)

Sorry if I am lost here

#6 lore raw

Re: [PATCH v2 1/2] pkt-line: add packet_writel() and packet_read_line_gently()

Quoted 6 lines · Ben Peart
Quoted 6 lines

From: Torsten Bögershausen [mailto:[email protected]]

Does this work ?
I would have expected
packet_writel(fd, "line one", "line two", "line n"), NULL;

Typo.
Should have been:
packet_writel(fd, "line one", "line two", "line n", NULL);

Quoted 16 lines
Quoted 1 line

No, that's actually not valid C syntax.

Quoted 11 lines
Quoted 2 lines

which requires the use of variable number of arguments. With your

proposal that convenience is lost as you have to create an array of strings and pass that instead. The usage just isn't as simple as the current model.

Quoted 1 line

What is wrong with

int packet_write_fmt_gently(int fd, const char *fmt, ...) and we use it like this:
if packet_write_fmt_gently(fd, "%s%s%s", "line one", "line two", "line n")

Quoted 6 lines

Packets are not just strings; see pkt-line.h for more details-
but basically they are a length packet, followed by the data (in this particular case a string).
The packet_writel function is a convenience function to write out a variable number of packetized strings followed by a flush packet.
You're sample above would simply concatenate the three strings and then write a single packet. A very different outcome. :)

Got it.

author
#7 lore raw

PATCH v2 1/4 t7800: remove whitespace before redirect

In reply to Ben Peart

From: David Aguilar <[email protected]>

---
 t/t7800-difftool.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t7800-difftool.sh +1 -1
index 25241f4096..e1ec292718 100755
@@ -428,7 +428,7 @@ run_dir_diff_test 'difftool --dir-diff branch from subdirectory' '
 		git difftool --dir-diff $symlinks --extcmd ls branch >output &&
 		# "sub" must only exist in "right"
 		# "file" and "file2" must be listed in both "left" and "right"
-		grep sub output > sub-output &&
+		grep sub output >sub-output &&
 		test_line_count = 1 sub-output &&
 		grep file"$" output >file-output &&
 		test_line_count = 2 file-output &&
Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
#8 lore raw

RE: [PATCH v2 1/4] t7800: remove whitespace before redirect

Please disregard. I apologize - somehow my format-patch/send-email went awry.

Thanks,

Ben

Quoted 34 lines

-----Original Message-----
From: Ben Peart [mailto:[email protected]]
Sent: Friday, March 24, 2017 11:27 AM
To: [email protected]
Cc: Ben Peart <[email protected]>; [email protected]; [email protected]; David Aguilar <[email protected]>; Junio C Hamano <[email protected]>
Subject: [PATCH v2 1/4] t7800: remove whitespace before redirect

From: David Aguilar <[email protected]>

---
 t/t7800-difftool.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t7800-difftool.sh index renamed from t/t7800-difftool.sh +0 -0

25241f4096..e1ec292718 100755

t/t7800-difftool.sh +0 -0
@@ -428,7 +428,7 @@ run_dir_diff_test 'difftool --dir-diff branch from

subdirectory' '

t/t7800-difftool.sh +1 -2
 		git difftool --dir-diff $symlinks --extcmd ls branch >output &&
 		# "sub" must only exist in "right"
 		# "file" and "file2" must be listed in both "left" and "right"
-		grep sub output > sub-output &&
+		grep sub output >sub-output &&
 		test_line_count = 1 sub-output &&
 		grep file"$" output >file-output &&
 		test_line_count = 2 file-output &&
--

2.12.0.gvfs.1.43.g876ba2a

author
#9 lore raw

PATCH v2 2/4 t7800: cleanup cruft left behind by tests

In reply to Ben Peart

From: David Aguilar <[email protected]>

---
 t/t7800-difftool.sh | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
t/t7800-difftool.sh +4 -1
index e1ec292718..e0e65df8de 100755
@@ -591,6 +591,7 @@ test_expect_success 'difftool --no-symlinks detects conflict ' '
 '
 
 test_expect_success 'difftool properly honors gitlink and core.worktree' '
+	test_when_finished rm -rf submod/ule &&
 	git submodule add ./. submod/ule &&
 	test_config -C submod/ule diff.tool checktrees &&
 	test_config -C submod/ule difftool.checktrees.cmd '\''
@@ -600,11 +601,13 @@ test_expect_success 'difftool properly honors gitlink and core.worktree' '
 		cd submod/ule &&
 		echo good >expect &&
 		git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
-		test_cmp expect actual
+		test_cmp expect actual &&
+		rm -f expect actual
 	)
 '
 
 test_expect_success SYMLINKS 'difftool --dir-diff symlinked directories' '
+	test_when_finished git reset --hard &&
 	git init dirlinks &&
 	(
 		cd dirlinks &&
Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
author
#10 lore raw

PATCH v2 3/4 difftool: handle modified symlinks in dir-diff mode

In reply to Ben Peart

From: David Aguilar <[email protected]>

Detect the null object ID for symlinks in dir-diff so that difftool can detect when symlinks are modified in the worktree.

Previously, a null symlink object ID would crash difftool. Handle null object IDs as unknown content that must be read from the worktree.

---
 builtin/difftool.c  | 51 ++++++++++++++++++++++++++++++++++++++++-----
 t/t7800-difftool.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 5 deletions(-)
builtin/difftool.c +46 -5
index d13350ce83..25e54ad3ed 100644
@@ -254,6 +254,49 @@ static int ensure_leading_directories(char *path)
 	}
 }
 
+/*
+ * Unconditional writing of a plain regular file is what
+ * "git difftool --dir-diff" wants to do for symlinks.  We are preparing two
+ * temporary directories to be fed to a Git-unaware tool that knows how to
+ * show a diff of two directories (e.g. "diff -r A B").
+ *
+ * Because the tool is Git-unaware, if a symbolic link appears in either of
+ * these temporary directories, it will try to dereference and show the
+ * difference of the target of the symbolic link, which is not what we want,
+ * as the goal of the dir-diff mode is to produce an output that is logically
+ * equivalent to what "git diff" produces.
+ *
+ * Most importantly, we want to get textual comparison of the result of the
+ * readlink(2).  get_symlink() provides that---it returns the contents of
+ * the symlink that gets written to a regular file to force the external tool
+ * to compare the readlink(2) result as text, even on a filesystem that is
+ * capable of doing a symbolic link.
+ */
+static char *get_symlink(const struct object_id *oid, const char *path)
+{
+	char *data;
+	if (is_null_oid(oid)) {
+		/* The symlink is unknown to Git so read from the filesystem */
+		struct strbuf link = STRBUF_INIT;
+		if (has_symlinks) {
+			if (strbuf_readlink(&link, path, strlen(path)))
+				die(_("could not read symlink %s"), path);
+		} else if (strbuf_read_file(&link, path, 128))
+			die(_("could not read symlink file %s"), path);
+
+		data = strbuf_detach(&link, NULL);
+	} else {
+		enum object_type type;
+		unsigned long size;
+		data = read_sha1_file(oid->hash, &type, &size);
+		if (!data)
+			die(_("could not read object %s for symlink %s"),
+				oid_to_hex(oid), path);
+	}
+
+	return data;
+}
+
 static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 			int argc, const char **argv)
 {
@@ -270,8 +313,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	struct hashmap working_tree_dups, submodules, symlinks2;
 	struct hashmap_iter iter;
 	struct pair_entry *entry;
-	enum object_type type;
-	unsigned long size;
 	struct index_state wtindex;
 	struct checkout lstate, rstate;
 	int rc, flags = RUN_GIT_CMD, err = 0;
@@ -377,13 +418,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 		}
 
 		if (S_ISLNK(lmode)) {
-			char *content = read_sha1_file(loid.hash, &type, &size);
+			char *content = get_symlink(&loid, src_path);
 			add_left_or_right(&symlinks2, src_path, content, 0);
 			free(content);
 		}
 
 		if (S_ISLNK(rmode)) {
-			char *content = read_sha1_file(roid.hash, &type, &size);
+			char *content = get_symlink(&roid, dst_path);
 			add_left_or_right(&symlinks2, dst_path, content, 1);
 			free(content);
 		}
@@ -397,7 +438,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 				return error("could not write '%s'", src_path);
 		}
 
-		if (rmode) {
+		if (rmode && !S_ISLNK(rmode)) {
 			struct working_tree_entry *entry;
 
 			/* Avoid duplicate working_tree entries */
t/t7800-difftool.sh +60 -0

64 lines not on this page · raw message

Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
author
#11 lore raw

PATCH v2 4/4 Git 2.12.1

In reply to Ben Peart

From: Junio C Hamano <[email protected]>

---
 Documentation/git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/git.txt +2 -1
index aa895da4a5..25560f69ff 100644
@@ -44,9 +44,10 @@ unreleased) version of Git, that is available from the 'master'
 branch of the `git.git` repository.
 Documentation for older releases are available here:
 
-* link:v2.12.0/git.html[documentation for release 2.12.0]
+* link:v2.12.1/git.html[documentation for release 2.12.1]
 
 * release notes for
+  link:RelNotes/2.12.1.txt[2.12.1].
   link:RelNotes/2.12.0.txt[2.12].
 
 * link:v2.11.1/git.html[documentation for release 2.11.1]
Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
author
#12 lore raw

PATCH v2 2/2 sub-process: refactor the filter process code into a reusable module

In reply to Ben Peart

Refactor the filter.<driver>.process code into a separate sub-process module that can be used to reduce the cost of starting up a sub-process for multiple commands. It does this by keeping the external process running and processing all commands by communicating over standard input and standard output using the packet format (pkt-line) based protocol. Full documentation is in Documentation/technical/api-sub-process.txt.

This code is refactored from:

	Commit edcc85814c ("convert: add filter.<driver>.process option", 2016-10-16)
	keeps the external process running and processes all commands
---
 Documentation/technical/api-sub-process.txt |  54 ++++++++++
 Makefile                                    |   1 +
 convert.c                                   | 160 ++++++----------------------
 sub-process.c                               | 117 ++++++++++++++++++++
 sub-process.h                               |  46 ++++++++
 5 files changed, 248 insertions(+), 130 deletions(-)
 create mode 100644 Documentation/technical/api-sub-process.txt
 create mode 100644 sub-process.c
 create mode 100644 sub-process.h
Documentation/technical/api-sub-process.txt new file +54 -0
new file mode 100644
index 0000000000..eb5005aa72
@@ -0,0 +1,54 @@
+sub-process API
+===============
+
+The sub-process API makes it possible to run background sub-processes
+that should run until the git command exits and communicate with it
+through stdin and stdout.  This reduces the overhead of having to fork
+a new process each time it needs to be communicated with.
+
+The sub-processes are kept in a hashmap by command name and looked up
+via the subprocess_find_entry function.  If an existing instance can not
+be found then a new process should be created and started.  When the
+parent git command terminates, all sub-processes are also terminated.
+
+This API is based on the run-command API.
+
+Data structures
+---------------
+
+* `struct subprocess_entry`
+
+The sub-process structure.  Members should not be accessed directly.
+
+Types
+-----
+
+'int(*subprocess_start_fn)(struct subprocess_entry *entry)'::
+
+	User-supplied function to initialize the sub-process.  This is
+	typically used to negoiate the interface version and capabilities.
+
+
+Functions
+---------
+
+`subprocess_start`::
+
+	Start a subprocess and add it to the subprocess hashmap.
+
+`subprocess_stop`::
+
+	Kill a subprocess and remove it from the subprocess hashmap.
+
+`subprocess_find_entry`::
+
+	Find a subprocess in the subprocess hashmap.
+
+`subprocess_get_child_process`::
+
+	Get the underlying `struct child_process` from a subprocess.
+
+`subprocess_read_status`::
+
+	Helper function to read packets looking for the last "status=<foo>"
+	key/value pair.
Makefile +1 -0
index a5a11e721a..8afe733092 100644
@@ -830,6 +830,7 @@ LIB_OBJS += streaming.o
 LIB_OBJS += string-list.o
 LIB_OBJS += submodule.o
 LIB_OBJS += submodule-config.o
+LIB_OBJS += sub-process.o
 LIB_OBJS += symlinks.o
 LIB_OBJS += tag.o
 LIB_OBJS += tempfile.o
convert.c +30 -130

239 lines not on this page · raw message

sub-process.c new file +117 -0

117 lines not on this page · raw message

sub-process.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.12.0.gvfs.1.43.g876ba2a
#13 lore raw

Re: [PATCH v2 2/2] sub-process: refactor the filter process code into a reusable module

Quoted 6 lines · Ben Peart

Refactor the filter.<driver>.process code into a separate sub-process module that can be used to reduce the cost of starting up a sub-process for multiple commands. It does this by keeping the external process running and processing all commands by communicating over standard input and standard output using the packet format (pkt-line) based protocol. Full documentation is in Documentation/technical/api-sub-process.txt.

Thanks - this looks like something useful to have.

When you create a "struct subprocess_entry" to be entered into the system, it is not a true "struct subprocess_entry" - it is a "struct subprocess_entry" plus some extra variables at the end. Since the sub-process hashmap is keyed solely on the command, what happens if another component uses the same trick (but with different extra variables) when using a sub-process with the same command?

I can think of at least two ways to solve this: (i) each component can have its own sub-process hashmap, or (ii) add a component key to the hashmap. (i) seems more elegant to me, but I'm not sure what the code will look like.

Also, I saw some minor code improvements possible (e.g. using "starts_with" when you're checking for the "status=<foo>" line) but I'll comment on those and look into the code more thoroughly once the questions in this e-mail are resolved.

Quoted 15 lines
sub-process.h new file +9 -0
new file mode 100644
index 0000000000..d1492f476d
@@ -0,0 +1,46 @@
+#ifndef SUBPROCESS_H
+#define SUBPROCESS_H
+
+#include "git-compat-util.h"
+#include "hashmap.h"
+#include "run-command.h"
+
+/*
+ * Generic implementation of background process infrastructure.

sub-process.h line 19

Quoted 10 lines
sub-process.h new file +10 -0
+ * See Documentation/technical/api-background-process.txt.
+ */
+
+ /* data structures */
+
+struct subprocess_entry {
+	struct hashmap_entry ent; /* must be the first member! */
+	struct child_process process;
+	const char *cmd;
+};

I notice from the documentation (and from "subprocess_get_child_process" below) that this is meant to be opaque, but I think this can be non-opaque (like "run-command").

Also, I would prefer adding a "util" pointer here instead of using it as an embedded struct. There is no clue here that it is embeddable or meant to be embedded.

sub-process.h line 25

Quoted 6 lines
sub-process.h new file +6 -0
+
+/* subprocess functions */
+
+typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
+int subprocess_start(struct subprocess_entry *entry, const char *cmd,
+		subprocess_start_fn startfn);

I'm not sure if it is useful to take a callback here - I think the caller of this function can just run whatever it wants after a successful subprocess_start.

Alternatively, if you add the "util" pointer (as I described above), then it makes sense to add a subprocess_get_or_start() function (and now it makes sense to take the callback). This way, the data structure will own, create, and destroy all the "struct subprocess_entry" that it needs, creating a nice separation of concerns.

sub-process.h line 27

Quoted 2 lines
sub-process.h new file +2 -0
+
+void subprocess_stop(struct subprocess_entry *entry);

(continued from above) And it would be clear that this would free "entry", for example.

Quoted 20 lines
sub-process.h new file +19 -0
+
+struct subprocess_entry *subprocess_find_entry(const char *cmd);
+
+/* subprocess helper functions */
+
+static inline struct child_process *subprocess_get_child_process(
+		struct subprocess_entry *entry)
+{
+	return &entry->process;
+}
+
+/*
+ * Helper function that will read packets looking for "status=<foo>"
+ * key/value pairs and return the value from the last "status" packet
+ */
+
+int subprocess_read_status(int fd, struct strbuf *status);
+
+#endif
#14 lore raw

RE: [PATCH v2 2/2] sub-process: refactor the filter process code into a reusable module

Quoted 14 lines

-----Original Message-----
From: Jonathan Tan [mailto:[email protected]]
Sent: Monday, March 27, 2017 3:00 PM
To: Ben Peart <[email protected]>; [email protected]
Cc: Ben Peart <[email protected]>
Subject: Re: [PATCH v2 2/2] sub-process: refactor the filter process code into a reusable module

Quoted 5 lines · Ben Peart

Refactor the filter.<driver>.process code into a separate sub-process module that can be used to reduce the cost of starting up a sub-process for multiple commands. It does this by keeping the external process running and processing all commands by communicating over standard input and standard output using the packet format (pkt-line)

Quoted 4 lines

based protocol.

Quoted 1 line

Full documentation is in Documentation/technical/api-sub-process.txt.

Thanks - this looks like something useful to have.

Thanks for the review and feedback.

Quoted 7 lines

When you create a "struct subprocess_entry" to be entered into the system, it is not a true "struct subprocess_entry" - it is a "struct subprocess_entry" plus some extra variables at the end. Since the sub-process hashmap is keyed solely on the command, what happens if another component uses the same trick (but with different extra
variables) when using a sub-process with the same command?

Having the command be the unique key is sufficient because it gets executed as a process by run_command and there can't be multiple different processes by the same name.

Quoted 39 lines

I can think of at least two ways to solve this: (i) each component can have its own sub-process hashmap, or (ii) add a component key to the hashmap. (i) seems more elegant to me, but I'm not sure what the code will look like.

Also, I saw some minor code improvements possible (e.g. using "starts_with" when you're checking for the "status=<foo>" line) but I'll comment on those and look into the code more thoroughly once the questions in this e-mail are resolved.

Quoted 14 lines
sub-process.h new file mode 100644 index renamed from sub-process.h +0 -0

0000000000..d1492f476d

sub-process.h new file +9 -0
@@ -0,0 +1,46 @@
+#ifndef SUBPROCESS_H
+#define SUBPROCESS_H
+
+#include "git-compat-util.h"
+#include "hashmap.h"
+#include "run-command.h"
+
+/*
+ * Generic implementation of background process infrastructure.

sub-process.h line 19

Quoted 10 lines
sub-process.h new file +10 -0
+ * See Documentation/technical/api-background-process.txt.
+ */
+
+ /* data structures */
+
+struct subprocess_entry {
+	struct hashmap_entry ent; /* must be the first member! */
+	struct child_process process;
+	const char *cmd;
+};

I notice from the documentation (and from "subprocess_get_child_process" below) that this is meant to be opaque, but I think this can be non-opaque (like "run-command").

Quoted 4 lines

Also, I would prefer adding a "util" pointer here instead of using it as an embedded struct. There is no clue here that it is embeddable or meant to be embedded.

The structure is intentionally opaque to provide the benefits of encapsulation. Obviously, the "C" language doesn't provide any enforcement of that design principal but we do what we can.

The embedded struct is following the same design pattern as elsewhere in git (hashmap for example) simply for consistency.

Quoted 9 lines

sub-process.h line 25

Quoted 6 lines
sub-process.h new file +6 -0
+
+/* subprocess functions */
+
+typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
+int subprocess_start(struct subprocess_entry *entry, const char *cmd,
+		subprocess_start_fn startfn);

I'm not sure if it is useful to take a callback here - I think the caller of this function can just run whatever it wants after a successful subprocess_start.

The purpose of doing the subprocess specific initialization via a callback is so that if it encounters an error (for example, it can't negotiate a common interface version) the subprocess_start function can detect that and ensure the hashmap does not contain the invalid/unusable subprocess.

Quoted 33 lines

Alternatively, if you add the "util" pointer (as I described above), then it makes sense to add a subprocess_get_or_start() function (and now it makes sense to take the callback). This way, the data structure will own, create, and destroy all the "struct subprocess_entry" that it needs, creating a nice separation of concerns.

sub-process.h line 27

Quoted 2 lines
sub-process.h new file +2 -0
+
+void subprocess_stop(struct subprocess_entry *entry);

(continued from above) And it would be clear that this would free "entry", for example.

Quoted 20 lines
sub-process.h new file +19 -0
+
+struct subprocess_entry *subprocess_find_entry(const char *cmd);
+
+/* subprocess helper functions */
+
+static inline struct child_process *subprocess_get_child_process(
+		struct subprocess_entry *entry)
+{
+	return &entry->process;
+}
+
+/*
+ * Helper function that will read packets looking for "status=<foo>"
+ * key/value pairs and return the value from the last "status" packet
+ */
+
+int subprocess_read_status(int fd, struct strbuf *status);
+
+#endif