Skip to content

Commit afba63d

Browse files
jeffhostetlermjcheetham
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 6b0b19e commit afba63d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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,
@@ -119,6 +120,52 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
119120
return 0;
120121
}
121122

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