@matheusbsbs / matheus-bagliano / commits / 70827b15bf
Fork 0

Split up builtin commands into separate files from git.c

Right now it split it into "builtin-log.c" for log-related commands ("log", "show" and "whatchanged"), and "builtin-help.c" for the informational commands (usage printing and "help" and "version"). This just makes things easier to read, I find. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Apr 21, 2006 at 10:27 UTC 70827b15bfb11f7aea52c6995956be9d149233e1
5 files changed +342 -305
Makefile
+6 -3
@@ -213,6 +213,9 @@ LIB_OBJS = \
213 fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
214 $(DIFF_OBJS)
215
216 +BUILTIN_OBJS = \
217 + builtin-log.o builtin-help.o
218 +
219 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
220 LIBS = $(GITLIBS) -lz
221
@@ -462,10 +465,10 @@ all:
465 strip: $(PROGRAMS) git$X
466 $(STRIP) $(STRIP_OPTS) $(PROGRAMS) git$X
467
465 -git$X: git.c common-cmds.h $(GITLIBS)
468 +git$X: git.c common-cmds.h $(BUILTIN_OBJS) $(GITLIBS)
469 $(CC) -DGIT_VERSION='"$(GIT_VERSION)"' \
470 $(ALL_CFLAGS) -o $@ $(filter %.c,$^) \
468 - $(ALL_LDFLAGS) $(LIBS)
471 + $(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)
472
473 $(BUILT_INS): git$X
474 rm -f $@ && ln git$X $@
@@ -565,7 +568,7 @@ init-db.o: init-db.c
568 $(CC) -c $(ALL_CFLAGS) \
569 -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $*.c
570
568 -$(LIB_OBJS): $(LIB_H)
571 +$(LIB_OBJS) $(BUILTIN_OBJS): $(LIB_H)
572 $(patsubst git-%$X,%.o,$(PROGRAMS)): $(GITLIBS)
573 $(DIFF_OBJS): diffcore.h
574
builtin-help.c new
+241
@@ -0,0 +1,241 @@
1 +/*
2 + * builtin-help.c
3 + *
4 + * Builtin help-related commands (help, usage, version)
5 + */
6 +#include "cache.h"
7 +#include "builtin.h"
8 +#include "exec_cmd.h"
9 +#include "common-cmds.h"
10 +
11 +static const char git_usage[] =
12 + "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
13 +
14 +/* most gui terms set COLUMNS (although some don't export it) */
15 +static int term_columns(void)
16 +{
17 + char *col_string = getenv("COLUMNS");
18 + int n_cols = 0;
19 +
20 + if (col_string && (n_cols = atoi(col_string)) > 0)
21 + return n_cols;
22 +
23 +#ifdef TIOCGWINSZ
24 + {
25 + struct winsize ws;
26 + if (!ioctl(1, TIOCGWINSZ, &ws)) {
27 + if (ws.ws_col)
28 + return ws.ws_col;
29 + }
30 + }
31 +#endif
32 +
33 + return 80;
34 +}
35 +
36 +static void oom(void)
37 +{
38 + fprintf(stderr, "git: out of memory\n");
39 + exit(1);
40 +}
41 +
42 +static inline void mput_char(char c, unsigned int num)
43 +{
44 + while(num--)
45 + putchar(c);
46 +}
47 +
48 +static struct cmdname {
49 + size_t len;
50 + char name[1];
51 +} **cmdname;
52 +static int cmdname_alloc, cmdname_cnt;
53 +
54 +static void add_cmdname(const char *name, int len)
55 +{
56 + struct cmdname *ent;
57 + if (cmdname_alloc <= cmdname_cnt) {
58 + cmdname_alloc = cmdname_alloc + 200;
59 + cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
60 + if (!cmdname)
61 + oom();
62 + }
63 + ent = malloc(sizeof(*ent) + len);
64 + if (!ent)
65 + oom();
66 + ent->len = len;
67 + memcpy(ent->name, name, len);
68 + ent->name[len] = 0;
69 + cmdname[cmdname_cnt++] = ent;
70 +}
71 +
72 +static int cmdname_compare(const void *a_, const void *b_)
73 +{
74 + struct cmdname *a = *(struct cmdname **)a_;
75 + struct cmdname *b = *(struct cmdname **)b_;
76 + return strcmp(a->name, b->name);
77 +}
78 +
79 +static void pretty_print_string_list(struct cmdname **cmdname, int longest)
80 +{
81 + int cols = 1, rows;
82 + int space = longest + 1; /* min 1 SP between words */
83 + int max_cols = term_columns() - 1; /* don't print *on* the edge */
84 + int i, j;
85 +
86 + if (space < max_cols)
87 + cols = max_cols / space;
88 + rows = (cmdname_cnt + cols - 1) / cols;
89 +
90 + qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
91 +
92 + for (i = 0; i < rows; i++) {
93 + printf(" ");
94 +
95 + for (j = 0; j < cols; j++) {
96 + int n = j * rows + i;
97 + int size = space;
98 + if (n >= cmdname_cnt)
99 + break;
100 + if (j == cols-1 || n + rows >= cmdname_cnt)
101 + size = 1;
102 + printf("%-*s", size, cmdname[n]->name);
103 + }
104 + putchar('\n');
105 + }
106 +}
107 +
108 +static void list_commands(const char *exec_path, const char *pattern)
109 +{
110 + unsigned int longest = 0;
111 + char path[PATH_MAX];
112 + int dirlen;
113 + DIR *dir = opendir(exec_path);
114 + struct dirent *de;
115 +
116 + if (!dir) {
117 + fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
118 + exit(1);
119 + }
120 +
121 + dirlen = strlen(exec_path);
122 + if (PATH_MAX - 20 < dirlen) {
123 + fprintf(stderr, "git: insanely long exec-path '%s'\n",
124 + exec_path);
125 + exit(1);
126 + }
127 +
128 + memcpy(path, exec_path, dirlen);
129 + path[dirlen++] = '/';
130 +
131 + while ((de = readdir(dir)) != NULL) {
132 + struct stat st;
133 + int entlen;
134 +
135 + if (strncmp(de->d_name, "git-", 4))
136 + continue;
137 + strcpy(path+dirlen, de->d_name);
138 + if (stat(path, &st) || /* stat, not lstat */
139 + !S_ISREG(st.st_mode) ||
140 + !(st.st_mode & S_IXUSR))
141 + continue;
142 +
143 + entlen = strlen(de->d_name);
144 + if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
145 + entlen -= 4;
146 +
147 + if (longest < entlen)
148 + longest = entlen;
149 +
150 + add_cmdname(de->d_name + 4, entlen-4);
151 + }
152 + closedir(dir);
153 +
154 + printf("git commands available in '%s'\n", exec_path);
155 + printf("----------------------------");
156 + mput_char('-', strlen(exec_path));
157 + putchar('\n');
158 + pretty_print_string_list(cmdname, longest - 4);
159 + putchar('\n');
160 +}
161 +
162 +static void list_common_cmds_help(void)
163 +{
164 + int i, longest = 0;
165 +
166 + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
167 + if (longest < strlen(common_cmds[i].name))
168 + longest = strlen(common_cmds[i].name);
169 + }
170 +
171 + puts("The most commonly used git commands are:");
172 + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
173 + printf(" %s", common_cmds[i].name);
174 + mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
175 + puts(common_cmds[i].help);
176 + }
177 + puts("(use 'git help -a' to get a list of all installed git commands)");
178 +}
179 +
180 +void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
181 +{
182 + if (fmt) {
183 + va_list ap;
184 +
185 + va_start(ap, fmt);
186 + printf("git: ");
187 + vprintf(fmt, ap);
188 + va_end(ap);
189 + putchar('\n');
190 + }
191 + else
192 + puts(git_usage);
193 +
194 + if (exec_path) {
195 + putchar('\n');
196 + if (show_all)
197 + list_commands(exec_path, "git-*");
198 + else
199 + list_common_cmds_help();
200 + }
201 +
202 + exit(1);
203 +}
204 +
205 +static void show_man_page(const char *git_cmd)
206 +{
207 + const char *page;
208 +
209 + if (!strncmp(git_cmd, "git", 3))
210 + page = git_cmd;
211 + else {
212 + int page_len = strlen(git_cmd) + 4;
213 + char *p = malloc(page_len + 1);
214 + strcpy(p, "git-");
215 + strcpy(p + 4, git_cmd);
216 + p[page_len] = 0;
217 + page = p;
218 + }
219 +
220 + execlp("man", "man", page, NULL);
221 +}
222 +
223 +int cmd_version(int argc, const char **argv, char **envp)
224 +{
225 + printf("git version %s\n", git_version_string);
226 + return 0;
227 +}
228 +
229 +int cmd_help(int argc, const char **argv, char **envp)
230 +{
231 + const char *help_cmd = argv[1];
232 + if (!help_cmd)
233 + cmd_usage(0, git_exec_path(), NULL);
234 + else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
235 + cmd_usage(1, git_exec_path(), NULL);
236 + else
237 + show_man_page(help_cmd);
238 + return 0;
239 +}
240 +
241 +
builtin-log.c new
+69
@@ -0,0 +1,69 @@
1 +/*
2 + * Builtin "git log" and related commands (show, whatchanged)
3 + *
4 + * (C) Copyright 2006 Linus Torvalds
5 + * 2006 Junio Hamano
6 + */
7 +#include "cache.h"
8 +#include "commit.h"
9 +#include "diff.h"
10 +#include "revision.h"
11 +#include "log-tree.h"
12 +
13 +static int cmd_log_wc(int argc, const char **argv, char **envp,
14 + struct rev_info *rev)
15 +{
16 + struct commit *commit;
17 +
18 + rev->abbrev = DEFAULT_ABBREV;
19 + rev->commit_format = CMIT_FMT_DEFAULT;
20 + rev->verbose_header = 1;
21 + argc = setup_revisions(argc, argv, rev, "HEAD");
22 +
23 + if (argc > 1)
24 + die("unrecognized argument: %s", argv[1]);
25 +
26 + prepare_revision_walk(rev);
27 + setup_pager();
28 + while ((commit = get_revision(rev)) != NULL) {
29 + log_tree_commit(rev, commit);
30 + free(commit->buffer);
31 + commit->buffer = NULL;
32 + }
33 + return 0;
34 +}
35 +
36 +int cmd_whatchanged(int argc, const char **argv, char **envp)
37 +{
38 + struct rev_info rev;
39 +
40 + init_revisions(&rev);
41 + rev.diff = 1;
42 + rev.diffopt.recursive = 1;
43 + return cmd_log_wc(argc, argv, envp, &rev);
44 +}
45 +
46 +int cmd_show(int argc, const char **argv, char **envp)
47 +{
48 + struct rev_info rev;
49 +
50 + init_revisions(&rev);
51 + rev.diff = 1;
52 + rev.diffopt.recursive = 1;
53 + rev.combine_merges = 1;
54 + rev.dense_combined_merges = 1;
55 + rev.always_show_header = 1;
56 + rev.ignore_merges = 0;
57 + rev.no_walk = 1;
58 + return cmd_log_wc(argc, argv, envp, &rev);
59 +}
60 +
61 +int cmd_log(int argc, const char **argv, char **envp)
62 +{
63 + struct rev_info rev;
64 +
65 + init_revisions(&rev);
66 + rev.always_show_header = 1;
67 + rev.diffopt.recursive = 1;
68 + return cmd_log_wc(argc, argv, envp, &rev);
69 +}
builtin.h new
+23
@@ -0,0 +1,23 @@
1 +#ifndef BUILTIN_H
2 +#define BUILTIN_H
3 +
4 +#ifndef PATH_MAX
5 +# define PATH_MAX 4096
6 +#endif
7 +
8 +extern const char git_version_string[];
9 +
10 +void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
11 +#ifdef __GNUC__
12 + __attribute__((__format__(__printf__, 3, 4), __noreturn__))
13 +#endif
14 + ;
15 +
16 +extern int cmd_help(int argc, const char **argv, char **envp);
17 +extern int cmd_version(int argc, const char **argv, char **envp);
18 +
19 +extern int cmd_whatchanged(int argc, const char **argv, char **envp);
20 +extern int cmd_show(int argc, const char **argv, char **envp);
21 +extern int cmd_log(int argc, const char **argv, char **envp);
22 +
23 +#endif
git.c
+3 -302
@@ -11,215 +11,8 @@
11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
13 #include "exec_cmd.h"
14 -#include "common-cmds.h"
14
16 -#include "cache.h"
17 -#include "commit.h"
18 -#include "diff.h"
19 -#include "revision.h"
20 -#include "log-tree.h"
21 -
22 -#ifndef PATH_MAX
23 -# define PATH_MAX 4096
24 -#endif
25 -
26 -static const char git_usage[] =
27 - "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
28 -
29 -/* most gui terms set COLUMNS (although some don't export it) */
30 -static int term_columns(void)
31 -{
32 - char *col_string = getenv("COLUMNS");
33 - int n_cols = 0;
34 -
35 - if (col_string && (n_cols = atoi(col_string)) > 0)
36 - return n_cols;
37 -
38 -#ifdef TIOCGWINSZ
39 - {
40 - struct winsize ws;
41 - if (!ioctl(1, TIOCGWINSZ, &ws)) {
42 - if (ws.ws_col)
43 - return ws.ws_col;
44 - }
45 - }
46 -#endif
47 -
48 - return 80;
49 -}
50 -
51 -static void oom(void)
52 -{
53 - fprintf(stderr, "git: out of memory\n");
54 - exit(1);
55 -}
56 -
57 -static inline void mput_char(char c, unsigned int num)
58 -{
59 - while(num--)
60 - putchar(c);
61 -}
62 -
63 -static struct cmdname {
64 - size_t len;
65 - char name[1];
66 -} **cmdname;
67 -static int cmdname_alloc, cmdname_cnt;
68 -
69 -static void add_cmdname(const char *name, int len)
70 -{
71 - struct cmdname *ent;
72 - if (cmdname_alloc <= cmdname_cnt) {
73 - cmdname_alloc = cmdname_alloc + 200;
74 - cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
75 - if (!cmdname)
76 - oom();
77 - }
78 - ent = malloc(sizeof(*ent) + len);
79 - if (!ent)
80 - oom();
81 - ent->len = len;
82 - memcpy(ent->name, name, len);
83 - ent->name[len] = 0;
84 - cmdname[cmdname_cnt++] = ent;
85 -}
86 -
87 -static int cmdname_compare(const void *a_, const void *b_)
88 -{
89 - struct cmdname *a = *(struct cmdname **)a_;
90 - struct cmdname *b = *(struct cmdname **)b_;
91 - return strcmp(a->name, b->name);
92 -}
93 -
94 -static void pretty_print_string_list(struct cmdname **cmdname, int longest)
95 -{
96 - int cols = 1, rows;
97 - int space = longest + 1; /* min 1 SP between words */
98 - int max_cols = term_columns() - 1; /* don't print *on* the edge */
99 - int i, j;
100 -
101 - if (space < max_cols)
102 - cols = max_cols / space;
103 - rows = (cmdname_cnt + cols - 1) / cols;
104 -
105 - qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
106 -
107 - for (i = 0; i < rows; i++) {
108 - printf(" ");
109 -
110 - for (j = 0; j < cols; j++) {
111 - int n = j * rows + i;
112 - int size = space;
113 - if (n >= cmdname_cnt)
114 - break;
115 - if (j == cols-1 || n + rows >= cmdname_cnt)
116 - size = 1;
117 - printf("%-*s", size, cmdname[n]->name);
118 - }
119 - putchar('\n');
120 - }
121 -}
122 -
123 -static void list_commands(const char *exec_path, const char *pattern)
124 -{
125 - unsigned int longest = 0;
126 - char path[PATH_MAX];
127 - int dirlen;
128 - DIR *dir = opendir(exec_path);
129 - struct dirent *de;
130 -
131 - if (!dir) {
132 - fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
133 - exit(1);
134 - }
135 -
136 - dirlen = strlen(exec_path);
137 - if (PATH_MAX - 20 < dirlen) {
138 - fprintf(stderr, "git: insanely long exec-path '%s'\n",
139 - exec_path);
140 - exit(1);
141 - }
142 -
143 - memcpy(path, exec_path, dirlen);
144 - path[dirlen++] = '/';
145 -
146 - while ((de = readdir(dir)) != NULL) {
147 - struct stat st;
148 - int entlen;
149 -
150 - if (strncmp(de->d_name, "git-", 4))
151 - continue;
152 - strcpy(path+dirlen, de->d_name);
153 - if (stat(path, &st) || /* stat, not lstat */
154 - !S_ISREG(st.st_mode) ||
155 - !(st.st_mode & S_IXUSR))
156 - continue;
157 -
158 - entlen = strlen(de->d_name);
159 - if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
160 - entlen -= 4;
161 -
162 - if (longest < entlen)
163 - longest = entlen;
164 -
165 - add_cmdname(de->d_name + 4, entlen-4);
166 - }
167 - closedir(dir);
168 -
169 - printf("git commands available in '%s'\n", exec_path);
170 - printf("----------------------------");
171 - mput_char('-', strlen(exec_path));
172 - putchar('\n');
173 - pretty_print_string_list(cmdname, longest - 4);
174 - putchar('\n');
175 -}
176 -
177 -static void list_common_cmds_help(void)
178 -{
179 - int i, longest = 0;
180 -
181 - for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
182 - if (longest < strlen(common_cmds[i].name))
183 - longest = strlen(common_cmds[i].name);
184 - }
185 -
186 - puts("The most commonly used git commands are:");
187 - for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
188 - printf(" %s", common_cmds[i].name);
189 - mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
190 - puts(common_cmds[i].help);
191 - }
192 - puts("(use 'git help -a' to get a list of all installed git commands)");
193 -}
194 -
195 -#ifdef __GNUC__
196 -static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
197 - __attribute__((__format__(__printf__, 3, 4), __noreturn__));
198 -#endif
199 -static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
200 -{
201 - if (fmt) {
202 - va_list ap;
203 -
204 - va_start(ap, fmt);
205 - printf("git: ");
206 - vprintf(fmt, ap);
207 - va_end(ap);
208 - putchar('\n');
209 - }
210 - else
211 - puts(git_usage);
212 -
213 - if (exec_path) {
214 - putchar('\n');
215 - if (show_all)
216 - list_commands(exec_path, "git-*");
217 - else
218 - list_common_cmds_help();
219 - }
220 -
221 - exit(1);
222 -}
15 +#include "builtin.h"
16
17 static void prepend_to_path(const char *dir, int len)
18 {
@@ -240,99 +33,7 @@ static void prepend_to_path(const char *dir, int len)
33 setenv("PATH", path, 1);
34 }
35
243 -static void show_man_page(const char *git_cmd)
244 -{
245 - const char *page;
246 -
247 - if (!strncmp(git_cmd, "git", 3))
248 - page = git_cmd;
249 - else {
250 - int page_len = strlen(git_cmd) + 4;
251 - char *p = malloc(page_len + 1);
252 - strcpy(p, "git-");
253 - strcpy(p + 4, git_cmd);
254 - p[page_len] = 0;
255 - page = p;
256 - }
257 -
258 - execlp("man", "man", page, NULL);
259 -}
260 -
261 -static int cmd_version(int argc, const char **argv, char **envp)
262 -{
263 - printf("git version %s\n", GIT_VERSION);
264 - return 0;
265 -}
266 -
267 -static int cmd_help(int argc, const char **argv, char **envp)
268 -{
269 - const char *help_cmd = argv[1];
270 - if (!help_cmd)
271 - cmd_usage(0, git_exec_path(), NULL);
272 - else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
273 - cmd_usage(1, git_exec_path(), NULL);
274 - else
275 - show_man_page(help_cmd);
276 - return 0;
277 -}
278 -
279 -static int cmd_log_wc(int argc, const char **argv, char **envp,
280 - struct rev_info *rev)
281 -{
282 - struct commit *commit;
283 -
284 - rev->abbrev = DEFAULT_ABBREV;
285 - rev->commit_format = CMIT_FMT_DEFAULT;
286 - rev->verbose_header = 1;
287 - argc = setup_revisions(argc, argv, rev, "HEAD");
288 -
289 - if (argc > 1)
290 - die("unrecognized argument: %s", argv[1]);
291 -
292 - prepare_revision_walk(rev);
293 - setup_pager();
294 - while ((commit = get_revision(rev)) != NULL) {
295 - log_tree_commit(rev, commit);
296 - free(commit->buffer);
297 - commit->buffer = NULL;
298 - }
299 - return 0;
300 -}
301 -
302 -static int cmd_wc(int argc, const char **argv, char **envp)
303 -{
304 - struct rev_info rev;
305 -
306 - init_revisions(&rev);
307 - rev.diff = 1;
308 - rev.diffopt.recursive = 1;
309 - return cmd_log_wc(argc, argv, envp, &rev);
310 -}
311 -
312 -static int cmd_show(int argc, const char **argv, char **envp)
313 -{
314 - struct rev_info rev;
315 -
316 - init_revisions(&rev);
317 - rev.diff = 1;
318 - rev.diffopt.recursive = 1;
319 - rev.combine_merges = 1;
320 - rev.dense_combined_merges = 1;
321 - rev.always_show_header = 1;
322 - rev.ignore_merges = 0;
323 - rev.no_walk = 1;
324 - return cmd_log_wc(argc, argv, envp, &rev);
325 -}
326 -
327 -static int cmd_log(int argc, const char **argv, char **envp)
328 -{
329 - struct rev_info rev;
330 -
331 - init_revisions(&rev);
332 - rev.always_show_header = 1;
333 - rev.diffopt.recursive = 1;
334 - return cmd_log_wc(argc, argv, envp, &rev);
335 -}
36 +const char git_version_string[] = GIT_VERSION;
37
38 static void handle_internal_command(int argc, const char **argv, char **envp)
39 {
@@ -344,7 +45,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
45 { "version", cmd_version },
46 { "help", cmd_help },
47 { "log", cmd_log },
347 - { "whatchanged", cmd_wc },
48 + { "whatchanged", cmd_whatchanged },
49 { "show", cmd_show },
50 };
51 int i;