Skip to content

Commit

Permalink
mingw: when path_lookup() failed, try BusyBox
Browse files Browse the repository at this point in the history
BusyBox comes with a ton of applets ("applet" being the identical
concept to Git's "builtins"). And similar to Git's builtins, the applets
can be called via `busybox <command>`, or the BusyBox executable can be
copied/hard-linked to the command name.

The similarities do not end here. Just as with Git's builtins, it is
problematic that BusyBox' hard-linked applets cannot easily be put into
a .zip file: .zip archives have no concept of hard-links and therefore
would store identical copies (and also extract identical copies,
"inflating" the archive unnecessarily).

To counteract that issue, MinGit already ships without hard-linked
copies of the builtins, and the plan is to do the same with BusyBox'
applets: simply ship busybox.exe as single executable, without
hard-linked applets.

To accommodate that, Git is being taught by this commit a very special
trick, exploiting the fact that it is possible to call an executable
with a command-line whose argv[0] is different from the executable's
name: when `sh` is to be spawned, and no `sh` is found in the PATH, but
busybox.exe is, use that executable (with unchanged argv).

Likewise, if any executable to be spawned is not on the PATH, but
busybox.exe is found, parse the output of `busybox.exe --help` to find
out what applets are included, and if the command matches an included
applet name, use busybox.exe to execute it.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Sep 18, 2024
1 parent 6b1d24d commit a850248
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../repository.h"
#include "win32/fscache.h"
#include "../attr.h"
#include "../string-list.h"

#define HCAST(type, handle) ((type)(intptr_t)handle)

Expand Down Expand Up @@ -1665,6 +1666,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
return NULL;
}

static char *path_lookup(const char *cmd, int exe_only);

static char *is_busybox_applet(const char *cmd)
{
static struct string_list applets = STRING_LIST_INIT_DUP;
static char *busybox_path;
static int busybox_path_initialized;

/* Avoid infinite loop */
if (!strncasecmp(cmd, "busybox", 7) &&
(!cmd[7] || !strcasecmp(cmd + 7, ".exe")))
return NULL;

if (!busybox_path_initialized) {
busybox_path = path_lookup("busybox.exe", 1);
busybox_path_initialized = 1;
}

/* Assume that sh is compiled in... */
if (!busybox_path || !strcasecmp(cmd, "sh"))
return xstrdup_or_null(busybox_path);

if (!applets.nr) {
struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
char *p;

strvec_pushl(&cp.args, busybox_path, "--help", NULL);

if (capture_command(&cp, &buf, 2048)) {
string_list_append(&applets, "");
return NULL;
}

/* parse output */
p = strstr(buf.buf, "Currently defined functions:\n");
if (!p) {
warning("Could not parse output of busybox --help");
string_list_append(&applets, "");
return NULL;
}
p = strchrnul(p, '\n');
for (;;) {
size_t len;

p += strspn(p, "\n\t ,");
len = strcspn(p, "\n\t ,");
if (!len)
break;
p[len] = '\0';
string_list_insert(&applets, p);
p = p + len + 1;
}
}

return string_list_has_string(&applets, cmd) ?
xstrdup(busybox_path) : NULL;
}

/*
* Determines the absolute path of cmd using the split path in path.
* If cmd contains a slash or backslash, no lookup is performed.
Expand Down Expand Up @@ -1693,6 +1753,9 @@ static char *path_lookup(const char *cmd, int exe_only)
path = sep + 1;
}

if (!prog && !isexe)
prog = is_busybox_applet(cmd);

return prog;
}

Expand Down
2 changes: 1 addition & 1 deletion t/t0014-alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_expect_success 'looping aliases - internal execution' '

test_expect_success 'run-command formats empty args properly' '
test_must_fail env GIT_TRACE=1 git frotz a "" b " " c 2>actual.raw &&
sed -ne "/run_command:/s/.*trace: run_command: //p" actual.raw >actual &&
sed -ne "/run_command: git-frotz/s/.*trace: run_command: //p" actual.raw >actual &&
echo "git-frotz a '\'''\'' b '\'' '\'' c" >expect &&
test_cmp expect actual
'
Expand Down

0 comments on commit a850248

Please sign in to comment.