@matheusbsbs / matheus-bagliano / commits / 8e49d50388
Fork 0

C implementation of the 'git' program, take two.

This patch provides a C implementation of the 'git' program and introduces support for putting the git-* commands in a directory of their own. It also saves some time on executing those commands in a tight loop and it prints the currently available git commands in a nicely formatted list. The location of the GIT_EXEC_PATH (name discussion's closed, thank gods) can be obtained by running git --exec-path which will hopefully give porcelainistas ample time to adapt their heavy-duty loops to call the core programs directly and thus save the extra fork() / execve() overhead, although that's not really necessary any more. The --exec-path value is prepended to $PATH, so the git-* programs should Just Work without ever requiring any changes to how they call other programs in the suite. Some timing values for 10000 invocations of git-var >&/dev/null: git.sh: 24.194s git.c: 9.044s git-var: 7.377s The git-<tab><tab> behaviour can, along with the someday-to-be-deprecated git-<command> form of invocation, be indefinitely retained by adding the following line to one's .bash_profile or equivalent: PATH=$PATH:$(git --exec-path) Experimental libraries can be used by either setting the environment variable GIT_EXEC_PATH, or by using git --exec-path=/some/experimental/exec-path Relative paths are properly grok'ed as exec-path values. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Andreas Ericsson committed Nov 16, 2005 at 00:31 UTC 8e49d50388211a0f3e7286f6ee600bf7736f4814
3 files changed +237 -88
Makefile
+8 -12
@@ -88,7 +88,7 @@ SCRIPT_SH = \
88 git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
89 git-repack.sh git-request-pull.sh git-reset.sh \
90 git-resolve.sh git-revert.sh git-sh-setup.sh git-status.sh \
91 - git-tag.sh git-verify-tag.sh git-whatchanged.sh git.sh \
91 + git-tag.sh git-verify-tag.sh git-whatchanged.sh \
92 git-applymbox.sh git-applypatch.sh git-am.sh \
93 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
94 git-merge-resolve.sh git-merge-ours.sh git-grep.sh \
@@ -334,19 +334,15 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
334 export prefix TAR INSTALL DESTDIR SHELL_PATH template_dir
335 ### Build rules
336
337 -all: $(PROGRAMS) $(SCRIPTS)
337 +all: $(PROGRAMS) $(SCRIPTS) git
338
339 all:
340 $(MAKE) -C templates
341
342 -git: git.sh Makefile
343 - rm -f $@+ $@
344 - sed -e '1s|#!.*/sh|#!$(call shq,$(SHELL_PATH))|' \
345 - -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
346 - -e 's/@@X@@/$(X)/g' \
347 - $(GIT_LIST_TWEAK) <$@.sh >$@+
348 - chmod +x $@+
349 - mv $@+ $@
342 +# Only use $(CFLAGS). We don't need anything else.
343 +git: git.c Makefile
344 + $(CC) -DGIT_EXEC_PATH='"$(bindir)"' -DGIT_VERSION='"$(GIT_VERSION)"' \
345 + $(CFLAGS) $@.c -o $@
346
347 $(filter-out git,$(patsubst %.sh,%,$(SCRIPT_SH))) : % : %.sh
348 rm -f $@
@@ -431,9 +427,9 @@ check:
427
428 ### Installation rules
429
434 -install: $(PROGRAMS) $(SCRIPTS)
430 +install: $(PROGRAMS) $(SCRIPTS) git
431 $(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(bindir))
436 - $(INSTALL) $(PROGRAMS) $(SCRIPTS) $(call shellquote,$(DESTDIR)$(bindir))
432 + $(INSTALL) git $(PROGRAMS) $(SCRIPTS) $(call shellquote,$(DESTDIR)$(bindir))
433 $(MAKE) -C templates install
434 $(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
435 $(INSTALL) $(PYMODULES) $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
git.c new
+229
@@ -0,0 +1,229 @@
1 +#include <stdio.h>
2 +#include <unistd.h>
3 +#include <stdlib.h>
4 +#include <string.h>
5 +#include <errno.h>
6 +#include <limits.h>
7 +#include <stdarg.h>
8 +#include <glob.h>
9 +
10 +#ifndef PATH_MAX
11 +# define PATH_MAX 4096
12 +#endif
13 +
14 +static const char git_usage[] =
15 + "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
16 +
17 +struct string_list {
18 + size_t len;
19 + char *str;
20 + struct string_list *next;
21 +};
22 +
23 +/* most gui terms set COLUMNS (although some don't export it) */
24 +static int term_columns(void)
25 +{
26 + char *col_string = getenv("COLUMNS");
27 + int n_cols = 0;
28 +
29 + if (col_string && (n_cols = atoi(col_string)) > 0)
30 + return n_cols;
31 +
32 + return 80;
33 +}
34 +
35 +static inline void mput_char(char c, unsigned int num)
36 +{
37 + while(num--)
38 + putchar(c);
39 +}
40 +
41 +static void pretty_print_string_list(struct string_list *list, int longest)
42 +{
43 + int cols = 1;
44 + int space = longest + 1; /* min 1 SP between words */
45 + int max_cols = term_columns() - 1; /* don't print *on* the edge */
46 +
47 + if (space < max_cols)
48 + cols = max_cols / space;
49 +
50 + while (list) {
51 + int c;
52 + printf(" ");
53 +
54 + for (c = cols; c && list; list = list->next) {
55 + printf("%s", list->str);
56 +
57 + if (--c)
58 + mput_char(' ', space - list->len);
59 + }
60 + putchar('\n');
61 + }
62 +}
63 +
64 +static void list_commands(const char *exec_path, const char *pattern)
65 +{
66 + struct string_list *list = NULL, *tail = NULL;
67 + unsigned int longest = 0, i;
68 + glob_t gl;
69 +
70 + if (chdir(exec_path) < 0) {
71 + printf("git: '%s': %s\n", exec_path, strerror(errno));
72 + exit(1);
73 + }
74 +
75 + i = glob(pattern, 0, NULL, &gl);
76 + switch(i) {
77 + case GLOB_NOSPACE:
78 + puts("Out of memory when running glob()");
79 + exit(2);
80 + case GLOB_ABORTED:
81 + printf("'%s': Read error: %s\n", exec_path, strerror(errno));
82 + exit(2);
83 + case GLOB_NOMATCH:
84 + printf("No git commands available in '%s'.\n", exec_path);
85 + printf("Do you need to specify --exec-path or set GIT_EXEC_PATH?\n");
86 + exit(1);
87 + }
88 +
89 + for (i = 0; i < gl.gl_pathc; i++) {
90 + int len = strlen(gl.gl_pathv[i] + 4);
91 +
92 + if (access(gl.gl_pathv[i], X_OK))
93 + continue;
94 +
95 + if (longest < len)
96 + longest = len;
97 +
98 + if (!tail)
99 + tail = list = malloc(sizeof(struct string_list));
100 + else {
101 + tail->next = malloc(sizeof(struct string_list));
102 + tail = tail->next;
103 + }
104 + tail->len = len;
105 + tail->str = gl.gl_pathv[i] + 4;
106 + tail->next = NULL;
107 + }
108 +
109 + printf("git commands available in '%s'\n", exec_path);
110 + printf("----------------------------");
111 + mput_char('-', strlen(exec_path));
112 + putchar('\n');
113 + pretty_print_string_list(list, longest);
114 + putchar('\n');
115 +}
116 +
117 +#ifdef __GNUC__
118 +static void usage(const char *exec_path, const char *fmt, ...)
119 + __attribute__((__format__(__printf__, 2, 3), __noreturn__));
120 +#endif
121 +static void usage(const char *exec_path, const char *fmt, ...)
122 +{
123 + if (fmt) {
124 + va_list ap;
125 +
126 + va_start(ap, fmt);
127 + printf("git: ");
128 + vprintf(fmt, ap);
129 + va_end(ap);
130 + putchar('\n');
131 + }
132 + else
133 + puts(git_usage);
134 +
135 + putchar('\n');
136 +
137 + if(exec_path)
138 + list_commands(exec_path, "git-*");
139 +
140 + exit(1);
141 +}
142 +
143 +static void prepend_to_path(const char *dir, int len)
144 +{
145 + char *path, *old_path = getenv("PATH");
146 + int path_len = len;
147 +
148 + if (!old_path)
149 + old_path = "/bin:/usr/bin:.";
150 +
151 + path_len = len + strlen(old_path) + 1;
152 +
153 + path = malloc(path_len + 1);
154 + path[path_len + 1] = '\0';
155 +
156 + memcpy(path, dir, len);
157 + path[len] = ':';
158 + memcpy(path + len + 1, old_path, path_len - len);
159 +
160 + setenv("PATH", path, 1);
161 +}
162 +
163 +int main(int argc, char **argv, char **envp)
164 +{
165 + char git_command[PATH_MAX + 1];
166 + char wd[PATH_MAX + 1];
167 + int i, len, show_help = 0;
168 + char *exec_path = getenv("GIT_EXEC_PATH");
169 +
170 + getcwd(wd, PATH_MAX);
171 +
172 + if (!exec_path)
173 + exec_path = GIT_EXEC_PATH;
174 +
175 + for (i = 1; i < argc; i++) {
176 + char *arg = argv[i];
177 +
178 + if (strncmp(arg, "--", 2))
179 + break;
180 +
181 + arg += 2;
182 +
183 + if (!strncmp(arg, "exec-path", 9)) {
184 + arg += 9;
185 + if (*arg == '=')
186 + exec_path = arg + 1;
187 + else {
188 + puts(exec_path);
189 + exit(0);
190 + }
191 + }
192 + else if (!strcmp(arg, "version")) {
193 + printf("git version %s\n", GIT_VERSION);
194 + exit(0);
195 + }
196 + else if (!strcmp(arg, "help"))
197 + show_help = 1;
198 + else if (!show_help)
199 + usage(NULL, NULL);
200 + }
201 +
202 + if (i >= argc || show_help)
203 + usage(exec_path, NULL);
204 +
205 + /* allow relative paths, but run with exact */
206 + if (chdir(exec_path)) {
207 + printf("git: '%s': %s\n", exec_path, strerror(errno));
208 + exit (1);
209 + }
210 +
211 + getcwd(git_command, sizeof(git_command));
212 + chdir(wd);
213 +
214 + len = strlen(git_command);
215 + prepend_to_path(git_command, len);
216 +
217 + strncat(&git_command[len], "/git-", sizeof(git_command) - len);
218 + len += 5;
219 + strncat(&git_command[len], argv[i], sizeof(git_command) - len);
220 +
221 + if (access(git_command, X_OK))
222 + usage(exec_path, "'%s' is not a git-command", argv[i]);
223 +
224 + /* execve() can only ever return if it fails */
225 + execve(git_command, &argv[i], envp);
226 + printf("Failed to run command '%s': %s\n", git_command, strerror(errno));
227 +
228 + return 1;
229 +}
git.sh deleted
-76
@@ -1,76 +0,0 @@
1 -#!/bin/sh
2 -
3 -cmd=
4 -path=$(dirname "$0")
5 -case "$#" in
6 -0) ;;
7 -*) cmd="$1"
8 - shift
9 - case "$cmd" in
10 - -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
11 - echo "git version @@GIT_VERSION@@"
12 - exit 0 ;;
13 - esac
14 -
15 - test -x "$path/git-$cmd" && exec "$path/git-$cmd" "$@"
16 -
17 - case '@@X@@' in
18 - '')
19 - ;;
20 - *)
21 - test -x "$path/git-$cmd@@X@@" &&
22 - exec "$path/git-$cmd@@X@@" "$@"
23 - ;;
24 - esac
25 - ;;
26 -esac
27 -
28 -echo "Usage: git COMMAND [OPTIONS] [TARGET]"
29 -if [ -n "$cmd" ]; then
30 - echo "git command '$cmd' not found."
31 -fi
32 -echo "git commands are:"
33 -
34 -fmt <<\EOF | sed -e 's/^/ /'
35 -add
36 -apply
37 -archimport
38 -bisect
39 -branch
40 -checkout
41 -cherry
42 -clone
43 -commit
44 -count-objects
45 -cvsimport
46 -diff
47 -fetch
48 -format-patch
49 -fsck-objects
50 -get-tar-commit-id
51 -init-db
52 -log
53 -ls-remote
54 -octopus
55 -pack-objects
56 -parse-remote
57 -patch-id
58 -prune
59 -pull
60 -push
61 -rebase
62 -relink
63 -rename
64 -repack
65 -request-pull
66 -reset
67 -resolve
68 -revert
69 -send-email
70 -shortlog
71 -show-branch
72 -status
73 -tag
74 -verify-tag
75 -whatchanged
76 -EOF