11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
20 # define PATH_MAX 4096
23 static const char git_usage[] =
24 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
26 /* most gui terms set COLUMNS (although some don't export it) */
27 static int term_columns(void)
29 char *col_string = getenv("COLUMNS");
32 if (col_string && (n_cols = atoi(col_string)) > 0)
38 if (!ioctl(1, TIOCGWINSZ, &ws)) {
50 fprintf(stderr, "git: out of memory\n");
54 static inline void mput_char(char c, unsigned int num)
60 static struct cmdname {
64 static int cmdname_alloc, cmdname_cnt;
66 static void add_cmdname(const char *name, int len)
69 if (cmdname_alloc <= cmdname_cnt) {
70 cmdname_alloc = cmdname_alloc + 200;
71 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
75 ent = malloc(sizeof(*ent) + len);
79 memcpy(ent->name, name, len);
81 cmdname[cmdname_cnt++] = ent;
84 static int cmdname_compare(const void *a_, const void *b_)
86 struct cmdname *a = *(struct cmdname **)a_;
87 struct cmdname *b = *(struct cmdname **)b_;
88 return strcmp(a->name, b->name);
91 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
94 int space = longest + 1; /* min 1 SP between words */
95 int max_cols = term_columns() - 1; /* don't print *on* the edge */
99 cols = max_cols / space;
100 rows = (cmdname_cnt + cols - 1) / cols;
102 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
104 for (i = 0; i < rows; i++) {
107 for (j = 0; j < cols; j++) {
108 int n = j * rows + i;
110 if (n >= cmdname_cnt)
112 if (j == cols-1 || n + rows >= cmdname_cnt)
114 printf("%-*s", size, cmdname[n]->name);
120 static void list_commands(const char *exec_path, const char *pattern)
122 unsigned int longest = 0;
125 DIR *dir = opendir(exec_path);
129 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
133 dirlen = strlen(exec_path);
134 if (PATH_MAX - 20 < dirlen) {
135 fprintf(stderr, "git: insanely long exec-path '%s'\n",
140 memcpy(path, exec_path, dirlen);
141 path[dirlen++] = '/';
143 while ((de = readdir(dir)) != NULL) {
147 if (strncmp(de->d_name, "git-", 4))
149 strcpy(path+dirlen, de->d_name);
150 if (stat(path, &st) || /* stat, not lstat */
151 !S_ISREG(st.st_mode) ||
152 !(st.st_mode & S_IXUSR))
155 entlen = strlen(de->d_name);
156 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
159 if (longest < entlen)
162 add_cmdname(de->d_name + 4, entlen-4);
166 printf("git commands available in '%s'\n", exec_path);
167 printf("----------------------------");
168 mput_char('-', strlen(exec_path));
170 pretty_print_string_list(cmdname, longest - 4);
175 static void cmd_usage(const char *exec_path, const char *fmt, ...)
176 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
178 static void cmd_usage(const char *exec_path, const char *fmt, ...)
195 list_commands(exec_path, "git-*");
200 static void prepend_to_path(const char *dir, int len)
202 char *path, *old_path = getenv("PATH");
206 old_path = "/usr/local/bin:/usr/bin:/bin";
208 path_len = len + strlen(old_path) + 1;
210 path = malloc(path_len + 1);
212 memcpy(path, dir, len);
214 memcpy(path + len + 1, old_path, path_len - len);
216 setenv("PATH", path, 1);
219 static void show_man_page(char *git_cmd)
223 if (!strncmp(git_cmd, "git", 3))
226 int page_len = strlen(git_cmd) + 4;
228 page = malloc(page_len + 1);
229 strcpy(page, "git-");
230 strcpy(page + 4, git_cmd);
234 execlp("man", "man", page, NULL);
237 static int cmd_version(int argc, char **argv, char **envp)
239 printf("git version %s\n", GIT_VERSION);
243 static int cmd_help(int argc, char **argv, char **envp)
245 char *help_cmd = argv[1];
247 cmd_usage(git_exec_path(), NULL);
248 show_man_page(help_cmd);
252 #define LOGSIZE (65536)
254 static int cmd_log(int argc, char **argv, char **envp)
257 struct commit *commit;
258 char *buf = xmalloc(LOGSIZE);
259 static enum cmit_fmt commit_format = CMIT_FMT_DEFAULT;
260 int abbrev = DEFAULT_ABBREV;
261 int show_parents = 0;
262 const char *commit_prefix = "commit ";
264 argc = setup_revisions(argc, argv, &rev, "HEAD");
267 /* accept -<digit>, like traditilnal "head" */
268 if ((*arg == '-') && isdigit(arg[1])) {
269 rev.max_count = atoi(arg + 1);
271 else if (!strcmp(arg, "-n")) {
273 die("-n requires an argument");
274 rev.max_count = atoi(argv[2]);
277 else if (!strncmp(arg,"-n",2)) {
278 rev.max_count = atoi(arg + 2);
280 else if (!strncmp(arg, "--pretty", 8)) {
281 commit_format = get_commit_format(arg + 8);
282 if (commit_format == CMIT_FMT_ONELINE)
285 else if (!strcmp(arg, "--parents")) {
288 else if (!strcmp(arg, "--no-abbrev")) {
291 else if (!strncmp(arg, "--abbrev=", 9)) {
292 abbrev = strtoul(arg + 9, NULL, 10);
293 if (abbrev && abbrev < MINIMUM_ABBREV)
294 abbrev = MINIMUM_ABBREV;
295 else if (40 < abbrev)
299 die("unrecognized argument: %s", arg);
303 prepare_revision_walk(&rev);
305 while ((commit = get_revision(&rev)) != NULL) {
306 printf("%s%s", commit_prefix,
307 sha1_to_hex(commit->object.sha1));
309 struct commit_list *parents = commit->parents;
311 struct object *o = &(parents->item->object);
312 parents = parents->next;
313 if (o->flags & TMP_MARK)
315 printf(" %s", sha1_to_hex(o->sha1));
316 o->flags |= TMP_MARK;
318 /* TMP_MARK is a general purpose flag that can
319 * be used locally, but the user should clean
320 * things up after it is done with them.
322 for (parents = commit->parents;
324 parents = parents->next)
325 parents->item->object.flags &= ~TMP_MARK;
327 if (commit_format == CMIT_FMT_ONELINE)
331 pretty_print_commit(commit_format, commit, ~0, buf,
339 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
341 static void handle_internal_command(int argc, char **argv, char **envp)
343 const char *cmd = argv[0];
344 static struct cmd_struct {
346 int (*fn)(int, char **, char **);
348 { "version", cmd_version },
349 { "help", cmd_help },
354 for (i = 0; i < ARRAY_SIZE(commands); i++) {
355 struct cmd_struct *p = commands+i;
356 if (strcmp(p->cmd, cmd))
358 exit(p->fn(argc, argv, envp));
362 int main(int argc, char **argv, char **envp)
365 char *slash = strrchr(cmd, '/');
366 char git_command[PATH_MAX + 1];
367 const char *exec_path = NULL;
370 * Take the basename of argv[0] as the command
371 * name, and the dirname as the default exec_path
372 * if it's an absolute path and we don't have
383 * "git-xxxx" is the same as "git xxxx", but we obviously:
385 * - cannot take flags in between the "git" and the "xxxx".
386 * - cannot execute it externally (since it would just do
387 * the same thing over again)
389 * So we just directly call the internal command handler, and
390 * die if that one cannot handle it.
392 if (!strncmp(cmd, "git-", 4)) {
395 handle_internal_command(argc, argv, envp);
396 die("cannot handle %s internally", cmd);
399 /* Default command: "help" */
402 /* Look for flags.. */
407 if (strncmp(cmd, "--", 2))
413 * For legacy reasons, the "version" and "help"
414 * commands can be written with "--" prepended
415 * to make them look like flags.
417 if (!strcmp(cmd, "help"))
419 if (!strcmp(cmd, "version"))
423 * Check remaining flags (which by now must be
424 * "--exec-path", but maybe we will accept
425 * other arguments some day)
427 if (!strncmp(cmd, "exec-path", 9)) {
430 git_set_exec_path(cmd + 1);
433 puts(git_exec_path());
436 cmd_usage(NULL, NULL);
441 * We search for git commands in the following order:
443 * - the path of the "git" command if we could find it
445 * - the regular PATH.
448 prepend_to_path(exec_path, strlen(exec_path));
449 exec_path = git_exec_path();
450 prepend_to_path(exec_path, strlen(exec_path));
452 /* See if it's an internal command */
453 handle_internal_command(argc, argv, envp);
455 /* .. then try the external ones */
459 cmd_usage(exec_path, "'%s' is not a git-command", cmd);
461 fprintf(stderr, "Failed to run command '%s': %s\n",
462 git_command, strerror(errno));