6 /* most GUI terminals set COLUMNS (although some don't export it) */
7 static int term_columns(void)
9 char *col_string
= getenv("COLUMNS");
12 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
18 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
28 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
30 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
+ 1);
33 memcpy(ent
->name
, name
, len
);
36 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
37 cmds
->names
[cmds
->cnt
++] = ent
;
40 static int cmdname_compare(const void *a_
, const void *b_
)
42 struct cmdname
*a
= *(struct cmdname
**)a_
;
43 struct cmdname
*b
= *(struct cmdname
**)b_
;
44 return strcmp(a
->name
, b
->name
);
47 static void uniq(struct cmdnames
*cmds
)
54 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
55 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
56 cmds
->names
[j
++] = cmds
->names
[i
];
61 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
67 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
68 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
70 cmds
->names
[cj
++] = cmds
->names
[ci
++];
77 while (ci
< cmds
->cnt
)
78 cmds
->names
[cj
++] = cmds
->names
[ci
++];
83 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
86 int space
= longest
+ 1; /* min 1 SP between words */
87 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
91 cols
= max_cols
/ space
;
92 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
94 for (i
= 0; i
< rows
; i
++) {
97 for (j
= 0; j
< cols
; j
++) {
102 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
104 printf("%-*s", size
, cmds
->names
[n
]->name
);
110 static int is_executable(const char *name
)
114 if (stat(name
, &st
) || /* stat, not lstat */
115 !S_ISREG(st
.st_mode
))
119 /* cannot trust the executable bit, peek into the file instead */
122 int fd
= open(name
, O_RDONLY
);
123 st
.st_mode
&= ~S_IXUSR
;
125 n
= read(fd
, buf
, 2);
127 /* DOS executables start with "MZ" */
128 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
129 st
.st_mode
|= S_IXUSR
;
133 return st
.st_mode
& S_IXUSR
;
136 static void list_commands_in_dir(struct cmdnames
*cmds
,
141 DIR *dir
= opendir(path
);
143 struct strbuf buf
= STRBUF_INIT
;
150 prefix_len
= strlen(prefix
);
152 strbuf_addf(&buf
, "%s/", path
);
155 while ((de
= readdir(dir
)) != NULL
) {
158 if (prefixcmp(de
->d_name
, prefix
))
161 strbuf_setlen(&buf
, len
);
162 strbuf_addstr(&buf
, de
->d_name
);
163 if (!is_executable(buf
.buf
))
166 entlen
= strlen(de
->d_name
) - prefix_len
;
167 if (has_extension(de
->d_name
, ".exe"))
170 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
173 strbuf_release(&buf
);
176 void load_command_list(const char *prefix
,
177 struct cmdnames
*main_cmds
,
178 struct cmdnames
*other_cmds
)
180 const char *env_path
= getenv("PATH");
181 const char *exec_path
= git_exec_path();
184 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
185 qsort(main_cmds
->names
, main_cmds
->cnt
,
186 sizeof(*main_cmds
->names
), cmdname_compare
);
191 char *paths
, *path
, *colon
;
192 path
= paths
= xstrdup(env_path
);
194 if ((colon
= strchr(path
, PATH_SEP
)))
197 list_commands_in_dir(other_cmds
, path
, prefix
);
205 qsort(other_cmds
->names
, other_cmds
->cnt
,
206 sizeof(*other_cmds
->names
), cmdname_compare
);
209 exclude_cmds(other_cmds
, main_cmds
);
212 void list_commands(const char *title
, struct cmdnames
*main_cmds
,
213 struct cmdnames
*other_cmds
)
217 for (i
= 0; i
< main_cmds
->cnt
; i
++)
218 if (longest
< main_cmds
->names
[i
]->len
)
219 longest
= main_cmds
->names
[i
]->len
;
220 for (i
= 0; i
< other_cmds
->cnt
; i
++)
221 if (longest
< other_cmds
->names
[i
]->len
)
222 longest
= other_cmds
->names
[i
]->len
;
224 if (main_cmds
->cnt
) {
225 const char *exec_path
= git_exec_path();
226 printf("available %s in '%s'\n", title
, exec_path
);
227 printf("----------------");
228 mput_char('-', strlen(title
) + strlen(exec_path
));
230 pretty_print_string_list(main_cmds
, longest
);
234 if (other_cmds
->cnt
) {
235 printf("%s available from elsewhere on your $PATH\n", title
);
236 printf("---------------------------------------");
237 mput_char('-', strlen(title
));
239 pretty_print_string_list(other_cmds
, longest
);
244 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
247 for (i
= 0; i
< c
->cnt
; i
++)
248 if (!strcmp(s
, c
->names
[i
]->name
))
253 void help_unknown_cmd(const char *cmd
)
255 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
259 int cmd_version(int argc
, const char **argv
, const char *prefix
)
261 printf("git version %s\n", git_version_string
);