Skip to content

Commit 6dc242d

Browse files
jeffhostetlerderrickstolee
authored andcommitted
sub-process: add subprocess_start_argv()
Add function to start a subprocess with an argv. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent a1a53f1 commit 6dc242d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

sub-process.c

+47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "sub-process.h"
55
#include "sigchain.h"
66
#include "pkt-line.h"
7+
#include "quote.h"
78

89
int cmd2process_cmp(const void *unused_cmp_data,
910
const struct hashmap_entry *eptr,
@@ -118,6 +119,52 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
118119
return 0;
119120
}
120121

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

sub-process.h

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
5757
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
5858
subprocess_start_fn startfn);
5959

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

0 commit comments

Comments
 (0)