Skip to content

Commit 91d3c70

Browse files
jeffhostetlerdscho
authored andcommitted
sub-process: add subprocess_start_argv()
Add function to start a subprocess with an argv. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
1 parent acfeba2 commit 91d3c70

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎sub-process.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "sub-process.h"
66
#include "sigchain.h"
77
#include "pkt-line.h"
8+
#include "quote.h"
89

910
int cmd2process_cmp(const void *cmp_data UNUSED,
1011
const struct hashmap_entry *eptr,
@@ -116,6 +117,52 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
116117
return 0;
117118
}
118119

120+
int subprocess_start_strvec(struct hashmap *hashmap,
121+
struct subprocess_entry *entry,
122+
int is_git_cmd,
123+
const struct strvec *argv,
124+
subprocess_start_fn startfn)
125+
{
126+
int err;
127+
size_t k;
128+
struct child_process *process;
129+
struct strbuf quoted = STRBUF_INIT;
130+
131+
process = &entry->process;
132+
133+
child_process_init(process);
134+
for (k = 0; k < argv->nr; k++)
135+
strvec_push(&process->args, argv->v[k]);
136+
process->use_shell = 1;
137+
process->in = -1;
138+
process->out = -1;
139+
process->git_cmd = is_git_cmd;
140+
process->clean_on_exit = 1;
141+
process->clean_on_exit_handler = subprocess_exit_handler;
142+
process->trace2_child_class = "subprocess";
143+
144+
sq_quote_argv_pretty(&quoted, argv->v);
145+
entry->cmd = strbuf_detach(&quoted, NULL);
146+
147+
err = start_command(process);
148+
if (err) {
149+
error("cannot fork to run subprocess '%s'", entry->cmd);
150+
return err;
151+
}
152+
153+
hashmap_entry_init(&entry->ent, strhash(entry->cmd));
154+
155+
err = startfn(entry);
156+
if (err) {
157+
error("initialization for subprocess '%s' failed", entry->cmd);
158+
subprocess_stop(hashmap, entry);
159+
return err;
160+
}
161+
162+
hashmap_add(hashmap, &entry->ent);
163+
return 0;
164+
}
165+
119166
static int handshake_version(struct child_process *process,
120167
const char *welcome_prefix, int *versions,
121168
int *chosen_version)

‎sub-process.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
5656
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
5757
subprocess_start_fn startfn);
5858

59+
int subprocess_start_strvec(struct hashmap *hashmap,
60+
struct subprocess_entry *entry,
61+
int is_git_cmd,
62+
const struct strvec *argv,
63+
subprocess_start_fn startfn);
64+
5965
/* Kill a subprocess and remove it from the subprocess hashmap. */
6066
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);
6167

0 commit comments

Comments
 (0)