Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * builtin-help.c |
| 3 | * |
| 4 | * Builtin help-related commands (help, usage, version) |
| 5 | */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "builtin.h" |
| 8 | #include "exec_cmd.h" |
| 9 | #include "common-cmds.h" |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 10 | #include <sys/ioctl.h> |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 11 | |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 12 | /* most GUI terminals set COLUMNS (although some don't export it) */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 13 | static int term_columns(void) |
| 14 | { |
| 15 | char *col_string = getenv("COLUMNS"); |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 16 | int n_cols; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 17 | |
| 18 | if (col_string && (n_cols = atoi(col_string)) > 0) |
| 19 | return n_cols; |
| 20 | |
| 21 | #ifdef TIOCGWINSZ |
| 22 | { |
| 23 | struct winsize ws; |
| 24 | if (!ioctl(1, TIOCGWINSZ, &ws)) { |
| 25 | if (ws.ws_col) |
| 26 | return ws.ws_col; |
| 27 | } |
| 28 | } |
| 29 | #endif |
| 30 | |
| 31 | return 80; |
| 32 | } |
| 33 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 34 | static inline void mput_char(char c, unsigned int num) |
| 35 | { |
| 36 | while(num--) |
| 37 | putchar(c); |
| 38 | } |
| 39 | |
| 40 | static struct cmdname { |
| 41 | size_t len; |
| 42 | char name[1]; |
| 43 | } **cmdname; |
| 44 | static int cmdname_alloc, cmdname_cnt; |
| 45 | |
| 46 | static void add_cmdname(const char *name, int len) |
| 47 | { |
| 48 | struct cmdname *ent; |
| 49 | if (cmdname_alloc <= cmdname_cnt) { |
| 50 | cmdname_alloc = cmdname_alloc + 200; |
James Bowes | c6e0caa | 2007-03-27 18:30:08 -0400 | [diff] [blame] | 51 | cmdname = xrealloc(cmdname, cmdname_alloc * sizeof(*cmdname)); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 52 | } |
James Bowes | 3301521 | 2007-03-25 20:39:36 -0400 | [diff] [blame] | 53 | ent = xmalloc(sizeof(*ent) + len); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 54 | ent->len = len; |
| 55 | memcpy(ent->name, name, len); |
| 56 | ent->name[len] = 0; |
| 57 | cmdname[cmdname_cnt++] = ent; |
| 58 | } |
| 59 | |
| 60 | static int cmdname_compare(const void *a_, const void *b_) |
| 61 | { |
| 62 | struct cmdname *a = *(struct cmdname **)a_; |
| 63 | struct cmdname *b = *(struct cmdname **)b_; |
| 64 | return strcmp(a->name, b->name); |
| 65 | } |
| 66 | |
| 67 | static void pretty_print_string_list(struct cmdname **cmdname, int longest) |
| 68 | { |
| 69 | int cols = 1, rows; |
| 70 | int space = longest + 1; /* min 1 SP between words */ |
| 71 | int max_cols = term_columns() - 1; /* don't print *on* the edge */ |
| 72 | int i, j; |
| 73 | |
| 74 | if (space < max_cols) |
| 75 | cols = max_cols / space; |
| 76 | rows = (cmdname_cnt + cols - 1) / cols; |
| 77 | |
| 78 | qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare); |
| 79 | |
| 80 | for (i = 0; i < rows; i++) { |
| 81 | printf(" "); |
| 82 | |
| 83 | for (j = 0; j < cols; j++) { |
| 84 | int n = j * rows + i; |
| 85 | int size = space; |
| 86 | if (n >= cmdname_cnt) |
| 87 | break; |
| 88 | if (j == cols-1 || n + rows >= cmdname_cnt) |
| 89 | size = 1; |
| 90 | printf("%-*s", size, cmdname[n]->name); |
| 91 | } |
| 92 | putchar('\n'); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void list_commands(const char *exec_path, const char *pattern) |
| 97 | { |
| 98 | unsigned int longest = 0; |
| 99 | char path[PATH_MAX]; |
| 100 | int dirlen; |
| 101 | DIR *dir = opendir(exec_path); |
| 102 | struct dirent *de; |
| 103 | |
| 104 | if (!dir) { |
| 105 | fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno)); |
| 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | dirlen = strlen(exec_path); |
| 110 | if (PATH_MAX - 20 < dirlen) { |
| 111 | fprintf(stderr, "git: insanely long exec-path '%s'\n", |
| 112 | exec_path); |
| 113 | exit(1); |
| 114 | } |
| 115 | |
| 116 | memcpy(path, exec_path, dirlen); |
| 117 | path[dirlen++] = '/'; |
| 118 | |
| 119 | while ((de = readdir(dir)) != NULL) { |
| 120 | struct stat st; |
| 121 | int entlen; |
| 122 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 123 | if (prefixcmp(de->d_name, "git-")) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 124 | continue; |
| 125 | strcpy(path+dirlen, de->d_name); |
| 126 | if (stat(path, &st) || /* stat, not lstat */ |
| 127 | !S_ISREG(st.st_mode) || |
| 128 | !(st.st_mode & S_IXUSR)) |
| 129 | continue; |
| 130 | |
| 131 | entlen = strlen(de->d_name); |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 132 | if (has_extension(de->d_name, ".exe")) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 133 | entlen -= 4; |
| 134 | |
| 135 | if (longest < entlen) |
| 136 | longest = entlen; |
| 137 | |
| 138 | add_cmdname(de->d_name + 4, entlen-4); |
| 139 | } |
| 140 | closedir(dir); |
| 141 | |
| 142 | printf("git commands available in '%s'\n", exec_path); |
| 143 | printf("----------------------------"); |
| 144 | mput_char('-', strlen(exec_path)); |
| 145 | putchar('\n'); |
| 146 | pretty_print_string_list(cmdname, longest - 4); |
| 147 | putchar('\n'); |
| 148 | } |
| 149 | |
| 150 | static void list_common_cmds_help(void) |
| 151 | { |
| 152 | int i, longest = 0; |
| 153 | |
| 154 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
| 155 | if (longest < strlen(common_cmds[i].name)) |
| 156 | longest = strlen(common_cmds[i].name); |
| 157 | } |
| 158 | |
| 159 | puts("The most commonly used git commands are:"); |
| 160 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
Ren,bi(B Scharfe | 4f75b11 | 2007-02-11 14:29:58 +0100 | [diff] [blame] | 161 | printf(" %s ", common_cmds[i].name); |
| 162 | mput_char(' ', longest - strlen(common_cmds[i].name)); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 163 | puts(common_cmds[i].help); |
| 164 | } |
| 165 | puts("(use 'git help -a' to get a list of all installed git commands)"); |
| 166 | } |
| 167 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 168 | static void show_man_page(const char *git_cmd) |
| 169 | { |
| 170 | const char *page; |
| 171 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 172 | if (!prefixcmp(git_cmd, "git")) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 173 | page = git_cmd; |
| 174 | else { |
| 175 | int page_len = strlen(git_cmd) + 4; |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 176 | char *p = xmalloc(page_len + 1); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 177 | strcpy(p, "git-"); |
| 178 | strcpy(p + 4, git_cmd); |
| 179 | p[page_len] = 0; |
| 180 | page = p; |
| 181 | } |
| 182 | |
| 183 | execlp("man", "man", page, NULL); |
| 184 | } |
| 185 | |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 186 | void help_unknown_cmd(const char *cmd) |
| 187 | { |
| 188 | printf("git: '%s' is not a git-command\n\n", cmd); |
| 189 | list_common_cmds_help(); |
| 190 | exit(1); |
| 191 | } |
| 192 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 193 | int cmd_version(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 194 | { |
| 195 | printf("git version %s\n", git_version_string); |
| 196 | return 0; |
| 197 | } |
| 198 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 199 | int cmd_help(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 200 | { |
Johannes Schindelin | 6acbcb9 | 2006-07-25 20:24:22 +0200 | [diff] [blame] | 201 | const char *help_cmd = argc > 1 ? argv[1] : NULL; |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 202 | const char *exec_path = git_exec_path(); |
| 203 | |
| 204 | if (!help_cmd) { |
| 205 | printf("usage: %s\n\n", git_usage_string); |
| 206 | list_common_cmds_help(); |
| 207 | exit(1); |
| 208 | } |
| 209 | |
| 210 | else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) { |
| 211 | printf("usage: %s\n\n", git_usage_string); |
| 212 | if(exec_path) |
| 213 | list_commands(exec_path, "git-*"); |
| 214 | exit(1); |
| 215 | } |
| 216 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 217 | else |
| 218 | show_man_page(help_cmd); |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 219 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |