Skip to content

Commit 5b8e6f8

Browse files
dmpotgitster
authored andcommitted
shrink git-shell by avoiding redundant dependencies
A lot of modules that have nothing to do with git-shell functionality were linked in, bloating git-shell more than 8 times. This patch cuts off redundant dependencies by: 1. providing stubs for three functions that make no sense for git-shell; 2. moving quote_path_fully from environment.c to quote.c to make the later self sufficient; 3. moving make_absolute_path into a new separate file. The following numbers have been received with the default optimization settings on master using GCC 4.1.2: Before: text data bss dec hex filename 143915 1348 93168 238431 3a35f git-shell After: text data bss dec hex filename 17670 788 8232 26690 6842 git-shell Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2dce956 commit 5b8e6f8

File tree

6 files changed

+79
-68
lines changed

6 files changed

+79
-68
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ LIB_H += unpack-trees.h
378378
LIB_H += utf8.h
379379
LIB_H += wt-status.h
380380

381+
LIB_OBJS += abspath.o
381382
LIB_OBJS += alias.o
382383
LIB_OBJS += alloc.o
383384
LIB_OBJS += archive.o

abspath.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "cache.h"
2+
3+
/* We allow "recursive" symbolic links. Only within reason, though. */
4+
#define MAXDEPTH 5
5+
6+
const char *make_absolute_path(const char *path)
7+
{
8+
static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
9+
char cwd[1024] = "";
10+
int buf_index = 1, len;
11+
12+
int depth = MAXDEPTH;
13+
char *last_elem = NULL;
14+
struct stat st;
15+
16+
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
17+
die ("Too long path: %.*s", 60, path);
18+
19+
while (depth--) {
20+
if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
21+
char *last_slash = strrchr(buf, '/');
22+
if (last_slash) {
23+
*last_slash = '\0';
24+
last_elem = xstrdup(last_slash + 1);
25+
} else {
26+
last_elem = xstrdup(buf);
27+
*buf = '\0';
28+
}
29+
}
30+
31+
if (*buf) {
32+
if (!*cwd && !getcwd(cwd, sizeof(cwd)))
33+
die ("Could not get current working directory");
34+
35+
if (chdir(buf))
36+
die ("Could not switch to '%s'", buf);
37+
}
38+
if (!getcwd(buf, PATH_MAX))
39+
die ("Could not get current working directory");
40+
41+
if (last_elem) {
42+
int len = strlen(buf);
43+
if (len + strlen(last_elem) + 2 > PATH_MAX)
44+
die ("Too long path name: '%s/%s'",
45+
buf, last_elem);
46+
buf[len] = '/';
47+
strcpy(buf + len + 1, last_elem);
48+
free(last_elem);
49+
last_elem = NULL;
50+
}
51+
52+
if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
53+
len = readlink(buf, next_buf, PATH_MAX);
54+
if (len < 0)
55+
die ("Invalid symlink: %s", buf);
56+
next_buf[len] = '\0';
57+
buf = next_buf;
58+
buf_index = 1 - buf_index;
59+
next_buf = bufs[buf_index];
60+
} else
61+
break;
62+
}
63+
64+
if (*cwd && chdir(cwd))
65+
die ("Could not change back to '%s'", cwd);
66+
67+
return buf;
68+
}

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ char git_default_email[MAX_GITNAME];
1313
char git_default_name[MAX_GITNAME];
1414
int user_ident_explicitly_given;
1515
int trust_executable_bit = 1;
16-
int quote_path_fully = 1;
1716
int has_symlinks = 1;
1817
int ignore_case;
1918
int assume_unchanged;

path.c

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ const char *make_nonrelative_path(const char *path)
327327
return buf;
328328
}
329329

330-
/* We allow "recursive" symbolic links. Only within reason, though. */
331-
#define MAXDEPTH 5
332-
333330
const char *make_relative_path(const char *abs, const char *base)
334331
{
335332
static char buf[PATH_MAX + 1];
@@ -346,67 +343,3 @@ const char *make_relative_path(const char *abs, const char *base)
346343
strcpy(buf, abs + baselen);
347344
return buf;
348345
}
349-
350-
const char *make_absolute_path(const char *path)
351-
{
352-
static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
353-
char cwd[1024] = "";
354-
int buf_index = 1, len;
355-
356-
int depth = MAXDEPTH;
357-
char *last_elem = NULL;
358-
struct stat st;
359-
360-
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
361-
die ("Too long path: %.*s", 60, path);
362-
363-
while (depth--) {
364-
if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
365-
char *last_slash = strrchr(buf, '/');
366-
if (last_slash) {
367-
*last_slash = '\0';
368-
last_elem = xstrdup(last_slash + 1);
369-
} else {
370-
last_elem = xstrdup(buf);
371-
*buf = '\0';
372-
}
373-
}
374-
375-
if (*buf) {
376-
if (!*cwd && !getcwd(cwd, sizeof(cwd)))
377-
die ("Could not get current working directory");
378-
379-
if (chdir(buf))
380-
die ("Could not switch to '%s'", buf);
381-
}
382-
if (!getcwd(buf, PATH_MAX))
383-
die ("Could not get current working directory");
384-
385-
if (last_elem) {
386-
int len = strlen(buf);
387-
if (len + strlen(last_elem) + 2 > PATH_MAX)
388-
die ("Too long path name: '%s/%s'",
389-
buf, last_elem);
390-
buf[len] = '/';
391-
strcpy(buf + len + 1, last_elem);
392-
free(last_elem);
393-
last_elem = NULL;
394-
}
395-
396-
if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
397-
len = readlink(buf, next_buf, PATH_MAX);
398-
if (len < 0)
399-
die ("Invalid symlink: %s", buf);
400-
next_buf[len] = '\0';
401-
buf = next_buf;
402-
buf_index = 1 - buf_index;
403-
next_buf = bufs[buf_index];
404-
} else
405-
break;
406-
}
407-
408-
if (*cwd && chdir(cwd))
409-
die ("Could not change back to '%s'", cwd);
410-
411-
return buf;
412-
}

quote.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "cache.h"
22
#include "quote.h"
33

4+
int quote_path_fully = 1;
5+
46
/* Help to copy the thing properly quoted for the shell safety.
57
* any single quote is replaced with '\'', any exclamation point
68
* is replaced with '\!', and the whole thing is enclosed in a

shell.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
#include "exec_cmd.h"
44
#include "strbuf.h"
55

6+
/* Stubs for functions that make no sense for git-shell. These stubs
7+
* are provided here to avoid linking in external redundant modules.
8+
*/
9+
void release_pack_memory(size_t need, int fd){}
10+
void trace_argv_printf(const char **argv, const char *fmt, ...){}
11+
void trace_printf(const char *fmt, ...){}
12+
13+
614
static int do_generic_cmd(const char *me, char *arg)
715
{
816
const char *my_argv[4];

0 commit comments

Comments
 (0)