1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-04 12:26:12 +02:00
git/builtin/remote-fd.c
Jeff King 7915691377 builtins: annotate always-empty prefix parameters
It's usually a bad idea for a builtin's cmd_foo() to ignore the "prefix"
argument it gets, as it needs to prepend that string when accessing any
paths given by the user.

But if a builtin does not ask for the git wrapper to run repository
setup (via the RUN_SETUP or RUN_SETUP_GENTLY flags), then we know the
prefix will always be NULL (it is adjusting for the chdir() done during
repo setup, but there cannot be one if we did not set up the repo). In
those cases it's OK to ignore "prefix", but it's worth annotating for a
few reasons:

  1. It serves as documentation to somebody reading the code about what
     we expect.

  2. If the flags in git.c ever change, the run-time assertion may help
     detect the problem (though only if the command is run from a
     subdirectory of the repository).

  3. It notes to the compiler that we are OK ignoring "prefix". In
     particular, this silences -Wunused-parameter. It _could_ also help
     the compiler generate better code (because it will know the prefix
     is NULL), but in practice this is quite unlikely to matter.

Note that I've only added this annotation to commands which triggered
-Wunused-parameter. It would be correct to add it to any builtin which
doesn't ask for RUN_SETUP, but most of the rest of them do the sensible
thing with "prefix" by passing it to parse_options(). So they're much
more likely to just work if they ever switched to RUN_SETUP, and aren't
worth annotating.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-03-28 14:11:24 -07:00

85 lines
1.9 KiB
C

#include "builtin.h"
#include "transport.h"
static const char usage_msg[] =
"git remote-fd <remote> <url>";
/*
* URL syntax:
* 'fd::<inoutfd>[/<anything>]' Read/write socket pair
* <inoutfd>.
* 'fd::<infd>,<outfd>[/<anything>]' Read pipe <infd> and write
* pipe <outfd>.
* [foo] indicates 'foo' is optional. <anything> is any string.
*
* The data output to <outfd>/<inoutfd> should be passed unmolested to
* git-receive-pack/git-upload-pack/git-upload-archive and output of
* git-receive-pack/git-upload-pack/git-upload-archive should be passed
* unmolested to <infd>/<inoutfd>.
*
*/
#define MAXCOMMAND 4096
static void command_loop(int input_fd, int output_fd)
{
char buffer[MAXCOMMAND];
while (1) {
size_t i;
if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
if (ferror(stdin))
die("Input error");
return;
}
/* Strip end of line characters. */
i = strlen(buffer);
while (i > 0 && isspace(buffer[i - 1]))
buffer[--i] = 0;
if (!strcmp(buffer, "capabilities")) {
printf("*connect\n\n");
fflush(stdout);
} else if (starts_with(buffer, "connect ")) {
printf("\n");
fflush(stdout);
if (bidirectional_transfer_loop(input_fd,
output_fd))
die("Copying data between file descriptors failed");
return;
} else {
die("Bad command: %s", buffer);
}
}
}
int cmd_remote_fd(int argc, const char **argv, const char *prefix)
{
int input_fd = -1;
int output_fd = -1;
char *end;
BUG_ON_NON_EMPTY_PREFIX(prefix);
if (argc != 3)
usage(usage_msg);
input_fd = (int)strtoul(argv[2], &end, 10);
if ((end == argv[2]) || (*end != ',' && *end != '/' && *end))
die("Bad URL syntax");
if (*end == '/' || !*end) {
output_fd = input_fd;
} else {
char *end2;
output_fd = (int)strtoul(end + 1, &end2, 10);
if ((end2 == end + 1) || (*end2 != '/' && *end2))
die("Bad URL syntax");
}
command_loop(input_fd, output_fd);
return 0;
}