@matheusbsbs / matheus-bagliano / commits / 5b8e6f85f9
Fork 0

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 <gitster@pobox.com>

Dmitry Potapov committed Jun 28, 2008 at 00:46 UTC 5b8e6f85f971dba8eeac048e7821978bbca14121
6 files changed +79 -68
Makefile
+1
@@ -378,6 +378,7 @@ LIB_H += unpack-trees.h
378 LIB_H += utf8.h
379 LIB_H += wt-status.h
380
381 +LIB_OBJS += abspath.o
382 LIB_OBJS += alias.o
383 LIB_OBJS += alloc.o
384 LIB_OBJS += archive.o
abspath.c new
+68
@@ -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
-1
@@ -13,7 +13,6 @@ char git_default_email[MAX_GITNAME];
13 char git_default_name[MAX_GITNAME];
14 int user_ident_explicitly_given;
15 int trust_executable_bit = 1;
16 -int quote_path_fully = 1;
16 int has_symlinks = 1;
17 int ignore_case;
18 int assume_unchanged;
path.c
-67
@@ -327,9 +327,6 @@ const char *make_nonrelative_path(const char *path)
327 return buf;
328 }
329
330 -/* We allow "recursive" symbolic links. Only within reason, though. */
331 -#define MAXDEPTH 5
332 -
330 const char *make_relative_path(const char *abs, const char *base)
331 {
332 static char buf[PATH_MAX + 1];
@@ -346,67 +343,3 @@ const char *make_relative_path(const char *abs, const char *base)
343 strcpy(buf, abs + baselen);
344 return buf;
345 }
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
+2
@@ -1,6 +1,8 @@
1 #include "cache.h"
2 #include "quote.h"
3
4 +int quote_path_fully = 1;
5 +
6 /* Help to copy the thing properly quoted for the shell safety.
7 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a
shell.c
+8
@@ -3,6 +3,14 @@
3 #include "exec_cmd.h"
4 #include "strbuf.h"
5
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 +
14 static int do_generic_cmd(const char *me, char *arg)
15 {
16 const char *my_argv[4];