11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
14 #include "common-cmds.h"
21 # define PATH_MAX 4096
24 static const char git_usage
[] =
25 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
27 /* most gui terms set COLUMNS (although some don't export it) */
28 static int term_columns(void)
30 char *col_string
= getenv("COLUMNS");
33 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
39 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
51 fprintf(stderr
, "git: out of memory\n");
55 static inline void mput_char(char c
, unsigned int num
)
61 static struct cmdname
{
65 static int cmdname_alloc
, cmdname_cnt
;
67 static void add_cmdname(const char *name
, int len
)
70 if (cmdname_alloc
<= cmdname_cnt
) {
71 cmdname_alloc
= cmdname_alloc
+ 200;
72 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
76 ent
= malloc(sizeof(*ent
) + len
);
80 memcpy(ent
->name
, name
, len
);
82 cmdname
[cmdname_cnt
++] = ent
;
85 static int cmdname_compare(const void *a_
, const void *b_
)
87 struct cmdname
*a
= *(struct cmdname
**)a_
;
88 struct cmdname
*b
= *(struct cmdname
**)b_
;
89 return strcmp(a
->name
, b
->name
);
92 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
95 int space
= longest
+ 1; /* min 1 SP between words */
96 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
100 cols
= max_cols
/ space
;
101 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
103 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
105 for (i
= 0; i
< rows
; i
++) {
108 for (j
= 0; j
< cols
; j
++) {
109 int n
= j
* rows
+ i
;
111 if (n
>= cmdname_cnt
)
113 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
115 printf("%-*s", size
, cmdname
[n
]->name
);
121 static void list_commands(const char *exec_path
, const char *pattern
)
123 unsigned int longest
= 0;
126 DIR *dir
= opendir(exec_path
);
130 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
134 dirlen
= strlen(exec_path
);
135 if (PATH_MAX
- 20 < dirlen
) {
136 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
141 memcpy(path
, exec_path
, dirlen
);
142 path
[dirlen
++] = '/';
144 while ((de
= readdir(dir
)) != NULL
) {
148 if (strncmp(de
->d_name
, "git-", 4))
150 strcpy(path
+dirlen
, de
->d_name
);
151 if (stat(path
, &st
) || /* stat, not lstat */
152 !S_ISREG(st
.st_mode
) ||
153 !(st
.st_mode
& S_IXUSR
))
156 entlen
= strlen(de
->d_name
);
157 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
160 if (longest
< entlen
)
163 add_cmdname(de
->d_name
+ 4, entlen
-4);
167 printf("git commands available in '%s'\n", exec_path
);
168 printf("----------------------------");
169 mput_char('-', strlen(exec_path
));
171 pretty_print_string_list(cmdname
, longest
- 4);
175 static void list_common_cmds_help()
179 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
180 if (longest
< strlen(common_cmds
[i
].name
))
181 longest
= strlen(common_cmds
[i
].name
);
184 puts("The most commonly used git commands are:");
185 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
186 printf(" %s", common_cmds
[i
].name
);
187 mput_char(' ', longest
- strlen(common_cmds
[i
].name
) + 4);
188 puts(common_cmds
[i
].help
);
190 puts("(use 'git help -a' to get a list of all installed git commands)");
194 static void cmd_usage(int show_all
, const char *exec_path
, const char *fmt
, ...)
195 __attribute__((__format__(__printf__
, 3, 4), __noreturn__
));
197 static void cmd_usage(int show_all
, const char *exec_path
, const char *fmt
, ...)
214 list_commands(exec_path
, "git-*");
216 list_common_cmds_help();
222 static void prepend_to_path(const char *dir
, int len
)
224 char *path
, *old_path
= getenv("PATH");
228 old_path
= "/usr/local/bin:/usr/bin:/bin";
230 path_len
= len
+ strlen(old_path
) + 1;
232 path
= malloc(path_len
+ 1);
234 memcpy(path
, dir
, len
);
236 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
238 setenv("PATH", path
, 1);
241 static void show_man_page(const char *git_cmd
)
245 if (!strncmp(git_cmd
, "git", 3))
248 int page_len
= strlen(git_cmd
) + 4;
249 char *p
= malloc(page_len
+ 1);
251 strcpy(p
+ 4, git_cmd
);
256 execlp("man", "man", page
, NULL
);
259 static int cmd_version(int argc
, const char **argv
, char **envp
)
261 printf("git version %s\n", GIT_VERSION
);
265 static int cmd_help(int argc
, const char **argv
, char **envp
)
267 const char *help_cmd
= argv
[1];
269 cmd_usage(0, git_exec_path(), NULL
);
270 else if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a"))
271 cmd_usage(1, git_exec_path(), NULL
);
273 show_man_page(help_cmd
);
277 #define LOGSIZE (65536)
279 static int cmd_log(int argc
, const char **argv
, char **envp
)
282 struct commit
*commit
;
283 char *buf
= xmalloc(LOGSIZE
);
284 static enum cmit_fmt commit_format
= CMIT_FMT_DEFAULT
;
285 int abbrev
= DEFAULT_ABBREV
;
286 int show_parents
= 0;
287 const char *commit_prefix
= "commit ";
289 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
291 const char *arg
= argv
[1];
292 if (!strncmp(arg
, "--pretty", 8)) {
293 commit_format
= get_commit_format(arg
+ 8);
294 if (commit_format
== CMIT_FMT_ONELINE
)
297 else if (!strcmp(arg
, "--parents")) {
300 else if (!strcmp(arg
, "--no-abbrev")) {
303 else if (!strncmp(arg
, "--abbrev=", 9)) {
304 abbrev
= strtoul(arg
+ 9, NULL
, 10);
305 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
306 abbrev
= MINIMUM_ABBREV
;
307 else if (40 < abbrev
)
311 die("unrecognized argument: %s", arg
);
315 prepare_revision_walk(&rev
);
317 while ((commit
= get_revision(&rev
)) != NULL
) {
318 printf("%s%s", commit_prefix
,
319 sha1_to_hex(commit
->object
.sha1
));
321 struct commit_list
*parents
= commit
->parents
;
323 struct object
*o
= &(parents
->item
->object
);
324 parents
= parents
->next
;
325 if (o
->flags
& TMP_MARK
)
327 printf(" %s", sha1_to_hex(o
->sha1
));
328 o
->flags
|= TMP_MARK
;
330 /* TMP_MARK is a general purpose flag that can
331 * be used locally, but the user should clean
332 * things up after it is done with them.
334 for (parents
= commit
->parents
;
336 parents
= parents
->next
)
337 parents
->item
->object
.flags
&= ~TMP_MARK
;
339 if (commit_format
== CMIT_FMT_ONELINE
)
343 pretty_print_commit(commit_format
, commit
, ~0, buf
,
351 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
353 const char *cmd
= argv
[0];
354 static struct cmd_struct
{
356 int (*fn
)(int, const char **, char **);
358 { "version", cmd_version
},
359 { "help", cmd_help
},
364 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
365 struct cmd_struct
*p
= commands
+i
;
366 if (strcmp(p
->cmd
, cmd
))
368 exit(p
->fn(argc
, argv
, envp
));
372 int main(int argc
, const char **argv
, char **envp
)
374 const char *cmd
= argv
[0];
375 char *slash
= strrchr(cmd
, '/');
376 char git_command
[PATH_MAX
+ 1];
377 const char *exec_path
= NULL
;
380 * Take the basename of argv[0] as the command
381 * name, and the dirname as the default exec_path
382 * if it's an absolute path and we don't have
393 * "git-xxxx" is the same as "git xxxx", but we obviously:
395 * - cannot take flags in between the "git" and the "xxxx".
396 * - cannot execute it externally (since it would just do
397 * the same thing over again)
399 * So we just directly call the internal command handler, and
400 * die if that one cannot handle it.
402 if (!strncmp(cmd
, "git-", 4)) {
405 handle_internal_command(argc
, argv
, envp
);
406 die("cannot handle %s internally", cmd
);
409 /* Default command: "help" */
412 /* Look for flags.. */
417 if (strncmp(cmd
, "--", 2))
423 * For legacy reasons, the "version" and "help"
424 * commands can be written with "--" prepended
425 * to make them look like flags.
427 if (!strcmp(cmd
, "help"))
429 if (!strcmp(cmd
, "version"))
433 * Check remaining flags (which by now must be
434 * "--exec-path", but maybe we will accept
435 * other arguments some day)
437 if (!strncmp(cmd
, "exec-path", 9)) {
440 git_set_exec_path(cmd
+ 1);
443 puts(git_exec_path());
446 cmd_usage(0, NULL
, NULL
);
451 * We search for git commands in the following order:
453 * - the path of the "git" command if we could find it
455 * - the regular PATH.
458 prepend_to_path(exec_path
, strlen(exec_path
));
459 exec_path
= git_exec_path();
460 prepend_to_path(exec_path
, strlen(exec_path
));
462 /* See if it's an internal command */
463 handle_internal_command(argc
, argv
, envp
);
465 /* .. then try the external ones */
469 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
471 fprintf(stderr
, "Failed to run command '%s': %s\n",
472 git_command
, strerror(errno
));