2017-05-05 17:28:01 +02:00
|
|
|
#ifndef SUBPROCESS_H
|
|
|
|
#define SUBPROCESS_H
|
|
|
|
|
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "hashmap.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
|
|
|
|
/*
|
2017-07-26 20:17:28 +02:00
|
|
|
* The sub-process API makes it possible to run background sub-processes
|
|
|
|
* for the entire lifetime of a Git invocation. If Git needs to communicate
|
|
|
|
* with an external process multiple times, then this can reduces the process
|
|
|
|
* invocation overhead. Git and the sub-process communicate through stdin and
|
|
|
|
* stdout.
|
|
|
|
*
|
|
|
|
* 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.
|
2017-05-05 17:28:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* data structures */
|
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/* Members should not be accessed directly. */
|
2017-05-05 17:28:01 +02:00
|
|
|
struct subprocess_entry {
|
|
|
|
struct hashmap_entry ent; /* must be the first member! */
|
|
|
|
const char *cmd;
|
|
|
|
struct child_process process;
|
|
|
|
};
|
|
|
|
|
2017-07-26 20:17:29 +02:00
|
|
|
struct subprocess_capability {
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subprocess_handshake will "|=" this value to supported_capabilities
|
|
|
|
* if the server reports that it supports this capability.
|
|
|
|
*/
|
|
|
|
unsigned int flag;
|
|
|
|
};
|
|
|
|
|
2017-05-05 17:28:01 +02:00
|
|
|
/* subprocess functions */
|
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/* Function to test two subprocess hashmap entries for equality. */
|
2017-06-30 21:14:05 +02:00
|
|
|
extern int cmd2process_cmp(const void *unused_cmp_data,
|
|
|
|
const struct subprocess_entry *e1,
|
|
|
|
const struct subprocess_entry *e2,
|
|
|
|
const void *unused_keydata);
|
2017-05-05 17:28:01 +02:00
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/*
|
|
|
|
* User-supplied function to initialize the sub-process. This is
|
|
|
|
* typically used to negotiate the interface version and capabilities.
|
|
|
|
*/
|
2017-05-05 17:28:01 +02:00
|
|
|
typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
|
2017-07-26 20:17:28 +02:00
|
|
|
|
|
|
|
/* Start a subprocess and add it to the subprocess hashmap. */
|
2017-05-05 17:28:01 +02:00
|
|
|
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
|
|
|
|
subprocess_start_fn startfn);
|
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/* Kill a subprocess and remove it from the subprocess hashmap. */
|
2017-05-05 17:28:01 +02:00
|
|
|
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);
|
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/* Find a subprocess in the subprocess hashmap. */
|
2017-05-05 17:28:01 +02:00
|
|
|
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd);
|
|
|
|
|
|
|
|
/* subprocess helper functions */
|
|
|
|
|
2017-07-26 20:17:28 +02:00
|
|
|
/* Get the underlying `struct child_process` from a subprocess. */
|
2017-05-05 17:28:01 +02:00
|
|
|
static inline struct child_process *subprocess_get_child_process(
|
|
|
|
struct subprocess_entry *entry)
|
|
|
|
{
|
|
|
|
return &entry->process;
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:17:29 +02:00
|
|
|
/*
|
|
|
|
* Perform the version and capability negotiation as described in the "Long
|
|
|
|
* Running Filter Process" section of the gitattributes documentation using the
|
|
|
|
* given requested versions and capabilities. The "versions" and "capabilities"
|
|
|
|
* parameters are arrays terminated by a 0 or blank struct.
|
|
|
|
*
|
|
|
|
* This function is typically called when a subprocess is started (as part of
|
|
|
|
* the "startfn" passed to subprocess_start).
|
|
|
|
*/
|
|
|
|
int subprocess_handshake(struct subprocess_entry *entry,
|
|
|
|
const char *welcome_prefix,
|
|
|
|
int *versions,
|
|
|
|
int *chosen_version,
|
|
|
|
struct subprocess_capability *capabilities,
|
|
|
|
unsigned int *supported_capabilities);
|
|
|
|
|
2017-05-05 17:28:01 +02:00
|
|
|
/*
|
|
|
|
* Helper function that will read packets looking for "status=<foo>"
|
|
|
|
* key/value pairs and return the value from the last "status" packet
|
|
|
|
*/
|
|
|
|
|
2017-05-05 17:28:02 +02:00
|
|
|
int subprocess_read_status(int fd, struct strbuf *status);
|
2017-05-05 17:28:01 +02:00
|
|
|
|
|
|
|
#endif
|