fetch: introduce machine-parseable "porcelain" output format
The output of git-fetch(1) is obviously designed for consumption by
users, only: we neatly columnize data, we abbreviate reference names, we
print neat arrows and we don't provide information about actual object
IDs that have changed. This makes the output format basically unusable
in the context of scripted invocations of git-fetch(1) that want to
learn about the exact changes that the command performs.
Introduce a new machine-parseable "porcelain" output format that is
supposed to fix this shortcoming. This output format is intended to
provide information about every reference that is about to be updated,
the old object ID that the reference has been pointing to and the new
object ID it will be updated to. Furthermore, the output format provides
the same flags as the human-readable format to indicate basic conditions
for each reference update like whether it was a fast-forward update, a
branch deletion, a rejected update or others.
The output format is quite simple:
```
<flag> <old-object-id> <new-object-id> <local-reference>\n
```
We assume two conditions which are generally true:
- The old and new object IDs have fixed known widths and cannot
contain spaces.
- References cannot contain newlines.
With these assumptions, the output format becomes unambiguously
parseable. Furthermore, given that this output is designed to be
consumed by scripts, the machine-readable data is printed to stdout
instead of stderr like the human-readable output is. This is mostly done
so that other data printed to stderr, like error messages or progress
meters, don't interfere with the parseable data.
A notable ommission here is that the output format does not include the
remote from which a reference was fetched, which might be important
information especially in the context of multi-remote fetches. But as
such a format would require us to print the remote for every single
reference update due to parallelizable fetches it feels wasteful for the
most likely usecase, which is when fetching from a single remote.
In a similar spirit, a second restriction is that this cannot be used
with `--recurse-submodules`. This is because any reference updates would
be ambiguous without also printing the repository in which the update
happens.
Considering that both multi-remote and submodule fetches are user-facing
features, using them in conjunction with `--porcelain` that is intended
for scripting purposes is likely not going to be useful in the majority
of cases. With that in mind these restrictions feel acceptable. If
usecases for either of these come up in the future though it is easy
enough to add a new "porcelain-v2" format that adds this information.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
cdc034a0ac
commit
dd781e3856
@ -54,6 +54,7 @@ enum display_format {
|
||||
DISPLAY_FORMAT_UNKNOWN = 0,
|
||||
DISPLAY_FORMAT_FULL,
|
||||
DISPLAY_FORMAT_COMPACT,
|
||||
DISPLAY_FORMAT_PORCELAIN,
|
||||
};
|
||||
|
||||
struct display_state {
|
||||
@ -756,6 +757,9 @@ static void display_state_init(struct display_state *display_state, struct ref *
|
||||
display_state->refcol_width = refcol_width(ref_map,
|
||||
display_state->format == DISPLAY_FORMAT_COMPACT);
|
||||
break;
|
||||
case DISPLAY_FORMAT_PORCELAIN:
|
||||
/* We don't need to precompute anything here. */
|
||||
break;
|
||||
default:
|
||||
BUG("unexpected display format %d", display_state->format);
|
||||
}
|
||||
@ -826,8 +830,12 @@ static void print_compact(struct display_state *display_state,
|
||||
static void display_ref_update(struct display_state *display_state, char code,
|
||||
const char *summary, const char *error,
|
||||
const char *remote, const char *local,
|
||||
const struct object_id *old_oid,
|
||||
const struct object_id *new_oid,
|
||||
int summary_width)
|
||||
{
|
||||
FILE *f = stderr;
|
||||
|
||||
if (verbosity < 0)
|
||||
return;
|
||||
|
||||
@ -860,12 +868,17 @@ static void display_ref_update(struct display_state *display_state, char code,
|
||||
|
||||
break;
|
||||
}
|
||||
case DISPLAY_FORMAT_PORCELAIN:
|
||||
strbuf_addf(&display_state->buf, "%c %s %s %s", code,
|
||||
oid_to_hex(old_oid), oid_to_hex(new_oid), local);
|
||||
f = stdout;
|
||||
break;
|
||||
default:
|
||||
BUG("unexpected display format %d", display_state->format);
|
||||
};
|
||||
strbuf_addch(&display_state->buf, '\n');
|
||||
|
||||
fputs(display_state->buf.buf, stderr);
|
||||
fputs(display_state->buf.buf, f);
|
||||
}
|
||||
|
||||
static int update_local_ref(struct ref *ref,
|
||||
@ -883,7 +896,8 @@ static int update_local_ref(struct ref *ref,
|
||||
if (oideq(&ref->old_oid, &ref->new_oid)) {
|
||||
if (verbosity > 0)
|
||||
display_ref_update(display_state, '=', _("[up to date]"), NULL,
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -896,7 +910,8 @@ static int update_local_ref(struct ref *ref,
|
||||
*/
|
||||
display_ref_update(display_state, '!', _("[rejected]"),
|
||||
_("can't fetch into checked-out branch"),
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -907,12 +922,14 @@ static int update_local_ref(struct ref *ref,
|
||||
r = s_update_ref("updating tag", ref, transaction, 0);
|
||||
display_ref_update(display_state, r ? '!' : 't', _("[tag update]"),
|
||||
r ? _("unable to update local ref") : NULL,
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return r;
|
||||
} else {
|
||||
display_ref_update(display_state, '!', _("[rejected]"),
|
||||
_("would clobber existing tag"),
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -945,7 +962,8 @@ static int update_local_ref(struct ref *ref,
|
||||
r = s_update_ref(msg, ref, transaction, 0);
|
||||
display_ref_update(display_state, r ? '!' : '*', what,
|
||||
r ? _("unable to update local ref") : NULL,
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -968,7 +986,8 @@ static int update_local_ref(struct ref *ref,
|
||||
r = s_update_ref("fast-forward", ref, transaction, 1);
|
||||
display_ref_update(display_state, r ? '!' : ' ', quickref.buf,
|
||||
r ? _("unable to update local ref") : NULL,
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
strbuf_release(&quickref);
|
||||
return r;
|
||||
} else if (force || ref->force) {
|
||||
@ -980,12 +999,14 @@ static int update_local_ref(struct ref *ref,
|
||||
r = s_update_ref("forced-update", ref, transaction, 1);
|
||||
display_ref_update(display_state, r ? '!' : '+', quickref.buf,
|
||||
r ? _("unable to update local ref") : _("forced update"),
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
strbuf_release(&quickref);
|
||||
return r;
|
||||
} else {
|
||||
display_ref_update(display_state, '!', _("[rejected]"), _("non-fast-forward"),
|
||||
remote_ref->name, ref->name, summary_width);
|
||||
remote_ref->name, ref->name,
|
||||
&ref->old_oid, &ref->new_oid, summary_width);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -1226,7 +1247,9 @@ static int store_updated_refs(struct display_state *display_state,
|
||||
display_ref_update(display_state, '*',
|
||||
*kind ? kind : "branch", NULL,
|
||||
rm->name,
|
||||
"FETCH_HEAD", summary_width);
|
||||
"FETCH_HEAD",
|
||||
&rm->new_oid, &rm->old_oid,
|
||||
summary_width);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1366,6 +1389,7 @@ static int prune_refs(struct display_state *display_state,
|
||||
for (ref = stale_refs; ref; ref = ref->next) {
|
||||
display_ref_update(display_state, '-', _("[deleted]"), NULL,
|
||||
_("(none)"), ref->name,
|
||||
&ref->new_oid, &ref->old_oid,
|
||||
summary_width);
|
||||
warn_dangling_symref(stderr, dangling_msg, ref->name);
|
||||
}
|
||||
@ -1798,7 +1822,8 @@ static int add_remote_or_group(const char *name, struct string_list *list)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void add_options_to_argv(struct strvec *argv)
|
||||
static void add_options_to_argv(struct strvec *argv,
|
||||
enum display_format format)
|
||||
{
|
||||
if (dry_run)
|
||||
strvec_push(argv, "--dry-run");
|
||||
@ -1834,6 +1859,8 @@ static void add_options_to_argv(struct strvec *argv)
|
||||
strvec_push(argv, "--ipv6");
|
||||
if (!write_fetch_head)
|
||||
strvec_push(argv, "--no-write-fetch-head");
|
||||
if (format == DISPLAY_FORMAT_PORCELAIN)
|
||||
strvec_pushf(argv, "--porcelain");
|
||||
}
|
||||
|
||||
/* Fetch multiple remotes in parallel */
|
||||
@ -1842,6 +1869,7 @@ struct parallel_fetch_state {
|
||||
const char **argv;
|
||||
struct string_list *remotes;
|
||||
int next, result;
|
||||
enum display_format format;
|
||||
};
|
||||
|
||||
static int fetch_next_remote(struct child_process *cp,
|
||||
@ -1861,7 +1889,7 @@ static int fetch_next_remote(struct child_process *cp,
|
||||
strvec_push(&cp->args, remote);
|
||||
cp->git_cmd = 1;
|
||||
|
||||
if (verbosity >= 0)
|
||||
if (verbosity >= 0 && state->format != DISPLAY_FORMAT_PORCELAIN)
|
||||
printf(_("Fetching %s\n"), remote);
|
||||
|
||||
return 1;
|
||||
@ -1893,7 +1921,8 @@ static int fetch_finished(int result, struct strbuf *out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fetch_multiple(struct string_list *list, int max_children)
|
||||
static int fetch_multiple(struct string_list *list, int max_children,
|
||||
enum display_format format)
|
||||
{
|
||||
int i, result = 0;
|
||||
struct strvec argv = STRVEC_INIT;
|
||||
@ -1911,10 +1940,10 @@ static int fetch_multiple(struct string_list *list, int max_children)
|
||||
strvec_pushl(&argv, "-c", "fetch.bundleURI=",
|
||||
"fetch", "--append", "--no-auto-gc",
|
||||
"--no-write-commit-graph", NULL);
|
||||
add_options_to_argv(&argv);
|
||||
add_options_to_argv(&argv, format);
|
||||
|
||||
if (max_children != 1 && list->nr != 1) {
|
||||
struct parallel_fetch_state state = { argv.v, list, 0, 0 };
|
||||
struct parallel_fetch_state state = { argv.v, list, 0, 0, format };
|
||||
const struct run_process_parallel_opts opts = {
|
||||
.tr2_category = "fetch",
|
||||
.tr2_label = "parallel/fetch",
|
||||
@ -1938,7 +1967,7 @@ static int fetch_multiple(struct string_list *list, int max_children)
|
||||
|
||||
strvec_pushv(&cmd.args, argv.v);
|
||||
strvec_push(&cmd.args, name);
|
||||
if (verbosity >= 0)
|
||||
if (verbosity >= 0 && format != DISPLAY_FORMAT_PORCELAIN)
|
||||
printf(_("Fetching %s\n"), name);
|
||||
cmd.git_cmd = 1;
|
||||
if (run_command(&cmd)) {
|
||||
@ -2089,6 +2118,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
|
||||
int fetch_write_commit_graph = -1;
|
||||
int stdin_refspecs = 0;
|
||||
int negotiate_only = 0;
|
||||
int porcelain = 0;
|
||||
int i;
|
||||
|
||||
struct option builtin_fetch_options[] = {
|
||||
@ -2123,6 +2153,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
|
||||
PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
|
||||
OPT_BOOL(0, "dry-run", &dry_run,
|
||||
N_("dry run")),
|
||||
OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
|
||||
OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
|
||||
N_("write fetched references to the FETCH_HEAD file")),
|
||||
OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
|
||||
@ -2228,6 +2259,26 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
|
||||
fetch_config_from_gitmodules(sfjc, rs);
|
||||
}
|
||||
|
||||
|
||||
if (porcelain) {
|
||||
switch (recurse_submodules_cli) {
|
||||
case RECURSE_SUBMODULES_OFF:
|
||||
case RECURSE_SUBMODULES_DEFAULT:
|
||||
/*
|
||||
* Reference updates in submodules would be ambiguous
|
||||
* in porcelain mode, so we reject this combination.
|
||||
*/
|
||||
recurse_submodules = RECURSE_SUBMODULES_OFF;
|
||||
break;
|
||||
|
||||
default:
|
||||
die(_("options '%s' and '%s' cannot be used together"),
|
||||
"--porcelain", "--recurse-submodules");
|
||||
}
|
||||
|
||||
config.display_format = DISPLAY_FORMAT_PORCELAIN;
|
||||
}
|
||||
|
||||
if (negotiate_only && !negotiation_tip.nr)
|
||||
die(_("--negotiate-only needs one or more --negotiation-tip=*"));
|
||||
|
||||
@ -2347,10 +2398,9 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
|
||||
max_children = fetch_parallel_config;
|
||||
|
||||
/* TODO should this also die if we have a previous partial-clone? */
|
||||
result = fetch_multiple(&list, max_children);
|
||||
result = fetch_multiple(&list, max_children, config.display_format);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This is only needed after fetch_one(), which does not fetch
|
||||
* submodules by itself.
|
||||
@ -2369,7 +2419,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
|
||||
if (max_children < 0)
|
||||
max_children = fetch_parallel_config;
|
||||
|
||||
add_options_to_argv(&options);
|
||||
add_options_to_argv(&options, config.display_format);
|
||||
result = fetch_submodules(the_repository,
|
||||
&options,
|
||||
submodule_prefix,
|
||||
|
||||
Reference in New Issue
Block a user