11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
14 #include "common-cmds.h"
23 # define PATH_MAX 4096
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");
35 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
41 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
53 fprintf(stderr
, "git: out of memory\n");
57 static inline void mput_char(char c
, unsigned int num
)
63 static struct cmdname
{
67 static int cmdname_alloc
, cmdname_cnt
;
69 static void add_cmdname(const char *name
, int len
)
72 if (cmdname_alloc
<= cmdname_cnt
) {
73 cmdname_alloc
= cmdname_alloc
+ 200;
74 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
78 ent
= malloc(sizeof(*ent
) + len
);
82 memcpy(ent
->name
, name
, len
);
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
)
97 int space
= longest
+ 1; /* min 1 SP between words */
98 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
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
++) {
110 for (j
= 0; j
< cols
; j
++) {
111 int n
= j
* rows
+ i
;
113 if (n
>= cmdname_cnt
)
115 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
117 printf("%-*s", size
, cmdname
[n
]->name
);
123 static void list_commands(const char *exec_path
, const char *pattern
)
125 unsigned int longest
= 0;
128 DIR *dir
= opendir(exec_path
);
132 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
136 dirlen
= strlen(exec_path
);
137 if (PATH_MAX
- 20 < dirlen
) {
138 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
143 memcpy(path
, exec_path
, dirlen
);
144 path
[dirlen
++] = '/';
146 while ((de
= readdir(dir
)) != NULL
) {
150 if (strncmp(de
->d_name
, "git-", 4))
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
))
158 entlen
= strlen(de
->d_name
);
159 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
162 if (longest
< entlen
)
165 add_cmdname(de
->d_name
+ 4, entlen
-4);
169 printf("git commands available in '%s'\n", exec_path
);
170 printf("----------------------------");
171 mput_char('-', strlen(exec_path
));
173 pretty_print_string_list(cmdname
, longest
- 4);
177 static void list_common_cmds_help(void)
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)");
196 static void cmd_usage(int show_all
, const char *exec_path
, const char *fmt
, ...)
197 __attribute__((__format__(__printf__
, 3, 4), __noreturn__
));
199 static void cmd_usage(int show_all
, const char *exec_path
, const char *fmt
, ...)
216 list_commands(exec_path
, "git-*");
218 list_common_cmds_help();
224 static void prepend_to_path(const char *dir
, int len
)
226 char *path
, *old_path
= getenv("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
);
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
)
247 if (!strncmp(git_cmd
, "git", 3))
250 int page_len
= strlen(git_cmd
) + 4;
251 char *p
= malloc(page_len
+ 1);
253 strcpy(p
+ 4, git_cmd
);
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
);
267 static int cmd_help(int argc
, const char **argv
, char **envp
)
269 const char *help_cmd
= argv
[1];
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
);
275 show_man_page(help_cmd
);
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 ";
289 rev
->abbrev
= DEFAULT_ABBREV
;
290 rev
->commit_format
= CMIT_FMT_DEFAULT
;
291 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
294 die("unrecognized argument: %s", argv
[1]);
295 if (rev
->commit_format
== CMIT_FMT_ONELINE
)
298 prepare_revision_walk(rev
);
300 while ((commit
= get_revision(rev
)) != NULL
) {
301 unsigned long ofs
= 0;
303 if (shown
&& rev
->diff
&&
304 rev
->commit_format
!= CMIT_FMT_ONELINE
)
307 ofs
= sprintf(buf
, "%s", commit_prefix
);
308 if (rev
->abbrev_commit
&& rev
->abbrev
)
309 ofs
+= sprintf(buf
+ ofs
, "%s",
310 find_unique_abbrev(commit
->object
.sha1
,
313 ofs
+= sprintf(buf
+ ofs
, "%s",
314 sha1_to_hex(commit
->object
.sha1
));
316 struct commit_list
*parents
= commit
->parents
;
318 struct object
*o
= &(parents
->item
->object
);
319 parents
= parents
->next
;
320 if (o
->flags
& TMP_MARK
)
322 ofs
+= sprintf(buf
+ ofs
, " %s",
323 sha1_to_hex(o
->sha1
));
324 o
->flags
|= TMP_MARK
;
326 /* TMP_MARK is a general purpose flag that can
327 * be used locally, but the user should clean
328 * things up after it is done with them.
330 for (parents
= commit
->parents
;
332 parents
= parents
->next
)
333 parents
->item
->object
.flags
&= ~TMP_MARK
;
336 (rev
->commit_format
== CMIT_FMT_ONELINE
) ? ' ' : '\n';
337 ofs
+= pretty_print_commit(rev
->commit_format
, commit
, ~0,
343 rev
->use_precomputed_header
= buf
;
344 strcpy(buf
+ ofs
, "\n---\n");
345 log_tree_commit(rev
, commit
);
350 free(commit
->buffer
);
351 commit
->buffer
= NULL
;
357 static int cmd_wc(int argc
, const char **argv
, char **envp
)
361 init_revisions(&rev
);
363 rev
.diffopt
.recursive
= 1;
364 return cmd_log_wc(argc
, argv
, envp
, &rev
);
367 static int cmd_show(int argc
, const char **argv
, char **envp
)
371 init_revisions(&rev
);
373 rev
.diffopt
.recursive
= 1;
374 rev
.combine_merges
= 1;
375 rev
.dense_combined_merges
= 1;
376 rev
.ignore_merges
= 0;
378 return cmd_log_wc(argc
, argv
, envp
, &rev
);
381 static int cmd_log(int argc
, const char **argv
, char **envp
)
385 init_revisions(&rev
);
386 rev
.always_show_header
= 1;
387 rev
.diffopt
.recursive
= 1;
388 rev
.combine_merges
= 1;
389 rev
.ignore_merges
= 0;
390 return cmd_log_wc(argc
, argv
, envp
, &rev
);
393 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
395 const char *cmd
= argv
[0];
396 static struct cmd_struct
{
398 int (*fn
)(int, const char **, char **);
400 { "version", cmd_version
},
401 { "help", cmd_help
},
403 { "whatchanged", cmd_wc
},
404 { "show", cmd_show
},
408 /* Turn "git cmd --help" into "git help cmd" */
409 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
411 argv
[0] = cmd
= "help";
414 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
415 struct cmd_struct
*p
= commands
+i
;
416 if (strcmp(p
->cmd
, cmd
))
418 exit(p
->fn(argc
, argv
, envp
));
422 int main(int argc
, const char **argv
, char **envp
)
424 const char *cmd
= argv
[0];
425 char *slash
= strrchr(cmd
, '/');
426 char git_command
[PATH_MAX
+ 1];
427 const char *exec_path
= NULL
;
430 * Take the basename of argv[0] as the command
431 * name, and the dirname as the default exec_path
432 * if it's an absolute path and we don't have
443 * "git-xxxx" is the same as "git xxxx", but we obviously:
445 * - cannot take flags in between the "git" and the "xxxx".
446 * - cannot execute it externally (since it would just do
447 * the same thing over again)
449 * So we just directly call the internal command handler, and
450 * die if that one cannot handle it.
452 if (!strncmp(cmd
, "git-", 4)) {
455 handle_internal_command(argc
, argv
, envp
);
456 die("cannot handle %s internally", cmd
);
459 /* Default command: "help" */
462 /* Look for flags.. */
467 if (strncmp(cmd
, "--", 2))
473 * For legacy reasons, the "version" and "help"
474 * commands can be written with "--" prepended
475 * to make them look like flags.
477 if (!strcmp(cmd
, "help"))
479 if (!strcmp(cmd
, "version"))
483 * Check remaining flags (which by now must be
484 * "--exec-path", but maybe we will accept
485 * other arguments some day)
487 if (!strncmp(cmd
, "exec-path", 9)) {
490 git_set_exec_path(cmd
+ 1);
493 puts(git_exec_path());
496 cmd_usage(0, NULL
, NULL
);
501 * We search for git commands in the following order:
503 * - the path of the "git" command if we could find it
505 * - the regular PATH.
508 prepend_to_path(exec_path
, strlen(exec_path
));
509 exec_path
= git_exec_path();
510 prepend_to_path(exec_path
, strlen(exec_path
));
512 /* See if it's an internal command */
513 handle_internal_command(argc
, argv
, envp
);
515 /* .. then try the external ones */
519 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
521 fprintf(stderr
, "Failed to run command '%s': %s\n",
522 git_command
, strerror(errno
));