Built-in git-whatchanged.
[git/debian.git] / git.c
blob9a89b0afbdb16f0c044741e2772066e73186c5c3
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
13 #include "exec_cmd.h"
14 #include "common-cmds.h"
16 #include "cache.h"
17 #include "commit.h"
18 #include "diff.h"
19 #include "revision.h"
20 #include "log-tree.h"
22 #ifndef PATH_MAX
23 # define PATH_MAX 4096
24 #endif
26 static const char git_usage[] =
27 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
29 /* most gui terms set COLUMNS (although some don't export it) */
30 static int term_columns(void)
32 char *col_string = getenv("COLUMNS");
33 int n_cols = 0;
35 if (col_string && (n_cols = atoi(col_string)) > 0)
36 return n_cols;
38 #ifdef TIOCGWINSZ
40 struct winsize ws;
41 if (!ioctl(1, TIOCGWINSZ, &ws)) {
42 if (ws.ws_col)
43 return ws.ws_col;
46 #endif
48 return 80;
51 static void oom(void)
53 fprintf(stderr, "git: out of memory\n");
54 exit(1);
57 static inline void mput_char(char c, unsigned int num)
59 while(num--)
60 putchar(c);
63 static struct cmdname {
64 size_t len;
65 char name[1];
66 } **cmdname;
67 static int cmdname_alloc, cmdname_cnt;
69 static void add_cmdname(const char *name, int len)
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();
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;
87 static int cmdname_compare(const void *a_, const void *b_)
89 struct cmdname *a = *(struct cmdname **)a_;
90 struct cmdname *b = *(struct cmdname **)b_;
91 return strcmp(a->name, b->name);
94 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
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;
101 if (space < max_cols)
102 cols = max_cols / space;
103 rows = (cmdname_cnt + cols - 1) / cols;
105 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
107 for (i = 0; i < rows; i++) {
108 printf(" ");
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);
119 putchar('\n');
123 static void list_commands(const char *exec_path, const char *pattern)
125 unsigned int longest = 0;
126 char path[PATH_MAX];
127 int dirlen;
128 DIR *dir = opendir(exec_path);
129 struct dirent *de;
131 if (!dir) {
132 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
133 exit(1);
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);
143 memcpy(path, exec_path, dirlen);
144 path[dirlen++] = '/';
146 while ((de = readdir(dir)) != NULL) {
147 struct stat st;
148 int entlen;
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;
158 entlen = strlen(de->d_name);
159 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
160 entlen -= 4;
162 if (longest < entlen)
163 longest = entlen;
165 add_cmdname(de->d_name + 4, entlen-4);
167 closedir(dir);
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');
177 static void list_common_cmds_help(void)
179 int i, longest = 0;
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);
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);
192 puts("(use 'git help -a' to get a list of all installed git commands)");
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, ...)
201 if (fmt) {
202 va_list ap;
204 va_start(ap, fmt);
205 printf("git: ");
206 vprintf(fmt, ap);
207 va_end(ap);
208 putchar('\n');
210 else
211 puts(git_usage);
213 if (exec_path) {
214 putchar('\n');
215 if (show_all)
216 list_commands(exec_path, "git-*");
217 else
218 list_common_cmds_help();
221 exit(1);
224 static void prepend_to_path(const char *dir, int len)
226 char *path, *old_path = getenv("PATH");
227 int path_len = len;
229 if (!old_path)
230 old_path = "/usr/local/bin:/usr/bin:/bin";
232 path_len = len + strlen(old_path) + 1;
234 path = malloc(path_len + 1);
236 memcpy(path, dir, len);
237 path[len] = ':';
238 memcpy(path + len + 1, old_path, path_len - len);
240 setenv("PATH", path, 1);
243 static void show_man_page(const char *git_cmd)
245 const char *page;
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;
258 execlp("man", "man", page, NULL);
261 static int cmd_version(int argc, const char **argv, char **envp)
263 printf("git version %s\n", GIT_VERSION);
264 return 0;
267 static int cmd_help(int argc, const char **argv, char **envp)
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;
279 #define LOGSIZE (65536)
281 static int cmd_log_wc(int argc, const char **argv, char **envp,
282 struct rev_info *rev)
284 struct commit *commit;
285 char *buf = xmalloc(LOGSIZE);
286 const char *commit_prefix = "commit ";
287 int shown = 0;
289 if (argc > 1)
290 die("unrecognized argument: %s", argv[1]);
291 if (rev->commit_format == CMIT_FMT_ONELINE)
292 commit_prefix = "";
294 prepare_revision_walk(rev);
295 setup_pager();
296 while ((commit = get_revision(rev)) != NULL) {
297 if (shown && rev->diff &&
298 rev->commit_format != CMIT_FMT_ONELINE)
299 putchar('\n');
300 fputs(commit_prefix, stdout);
301 if (rev->abbrev_commit && rev->abbrev)
302 fputs(find_unique_abbrev(commit->object.sha1,
303 rev->abbrev),
304 stdout);
305 else
306 fputs(sha1_to_hex(commit->object.sha1), stdout);
307 if (rev->parents) {
308 struct commit_list *parents = commit->parents;
309 while (parents) {
310 struct object *o = &(parents->item->object);
311 parents = parents->next;
312 if (o->flags & TMP_MARK)
313 continue;
314 printf(" %s", sha1_to_hex(o->sha1));
315 o->flags |= TMP_MARK;
317 /* TMP_MARK is a general purpose flag that can
318 * be used locally, but the user should clean
319 * things up after it is done with them.
321 for (parents = commit->parents;
322 parents;
323 parents = parents->next)
324 parents->item->object.flags &= ~TMP_MARK;
326 if (rev->commit_format == CMIT_FMT_ONELINE)
327 putchar(' ');
328 else
329 putchar('\n');
330 pretty_print_commit(rev->commit_format, commit, ~0, buf,
331 LOGSIZE, rev->abbrev);
332 printf("%s\n", buf);
333 if (rev->diff)
334 log_tree_commit(rev, commit);
335 shown = 1;
336 free(commit->buffer);
337 commit->buffer = NULL;
339 free(buf);
340 return 0;
343 static int cmd_wc(int argc, const char **argv, char **envp)
345 struct rev_info rev;
347 init_revisions(&rev);
348 rev.abbrev = DEFAULT_ABBREV;
349 rev.no_commit_id = 1;
350 rev.commit_format = CMIT_FMT_DEFAULT;
351 rev.diff = 1;
352 rev.diffopt.recursive = 1;
353 argc = setup_revisions(argc, argv, &rev, "HEAD");
354 return cmd_log_wc(argc, argv, envp, &rev);
357 static int cmd_log(int argc, const char **argv, char **envp)
359 struct rev_info rev;
361 init_revisions(&rev);
362 rev.abbrev = DEFAULT_ABBREV;
363 rev.no_commit_id = 1;
364 rev.commit_format = CMIT_FMT_DEFAULT;
365 argc = setup_revisions(argc, argv, &rev, "HEAD");
366 return cmd_log_wc(argc, argv, envp, &rev);
369 static void handle_internal_command(int argc, const char **argv, char **envp)
371 const char *cmd = argv[0];
372 static struct cmd_struct {
373 const char *cmd;
374 int (*fn)(int, const char **, char **);
375 } commands[] = {
376 { "version", cmd_version },
377 { "help", cmd_help },
378 { "log", cmd_log },
379 { "whatchanged", cmd_wc },
381 int i;
383 for (i = 0; i < ARRAY_SIZE(commands); i++) {
384 struct cmd_struct *p = commands+i;
385 if (strcmp(p->cmd, cmd))
386 continue;
387 exit(p->fn(argc, argv, envp));
391 int main(int argc, const char **argv, char **envp)
393 const char *cmd = argv[0];
394 char *slash = strrchr(cmd, '/');
395 char git_command[PATH_MAX + 1];
396 const char *exec_path = NULL;
399 * Take the basename of argv[0] as the command
400 * name, and the dirname as the default exec_path
401 * if it's an absolute path and we don't have
402 * anything better.
404 if (slash) {
405 *slash++ = 0;
406 if (*cmd == '/')
407 exec_path = cmd;
408 cmd = slash;
412 * "git-xxxx" is the same as "git xxxx", but we obviously:
414 * - cannot take flags in between the "git" and the "xxxx".
415 * - cannot execute it externally (since it would just do
416 * the same thing over again)
418 * So we just directly call the internal command handler, and
419 * die if that one cannot handle it.
421 if (!strncmp(cmd, "git-", 4)) {
422 cmd += 4;
423 argv[0] = cmd;
424 handle_internal_command(argc, argv, envp);
425 die("cannot handle %s internally", cmd);
428 /* Default command: "help" */
429 cmd = "help";
431 /* Look for flags.. */
432 while (argc > 1) {
433 cmd = *++argv;
434 argc--;
436 if (strncmp(cmd, "--", 2))
437 break;
439 cmd += 2;
442 * For legacy reasons, the "version" and "help"
443 * commands can be written with "--" prepended
444 * to make them look like flags.
446 if (!strcmp(cmd, "help"))
447 break;
448 if (!strcmp(cmd, "version"))
449 break;
452 * Check remaining flags (which by now must be
453 * "--exec-path", but maybe we will accept
454 * other arguments some day)
456 if (!strncmp(cmd, "exec-path", 9)) {
457 cmd += 9;
458 if (*cmd == '=') {
459 git_set_exec_path(cmd + 1);
460 continue;
462 puts(git_exec_path());
463 exit(0);
465 cmd_usage(0, NULL, NULL);
467 argv[0] = cmd;
470 * We search for git commands in the following order:
471 * - git_exec_path()
472 * - the path of the "git" command if we could find it
473 * in $0
474 * - the regular PATH.
476 if (exec_path)
477 prepend_to_path(exec_path, strlen(exec_path));
478 exec_path = git_exec_path();
479 prepend_to_path(exec_path, strlen(exec_path));
481 /* See if it's an internal command */
482 handle_internal_command(argc, argv, envp);
484 /* .. then try the external ones */
485 execv_git_cmd(argv);
487 if (errno == ENOENT)
488 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
490 fprintf(stderr, "Failed to run command '%s': %s\n",
491 git_command, strerror(errno));
493 return 1;