help.c 5.58 KB
Newer Older
1 2 3
#include "cache.h"
#include "builtin.h"
#include "exec_cmd.h"
4
#include "help.h"
5

6
/* most GUI terminals set COLUMNS (although some don't export it) */
7 8 9
static int term_columns(void)
{
	char *col_string = getenv("COLUMNS");
10
	int n_cols;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

	if (col_string && (n_cols = atoi(col_string)) > 0)
		return n_cols;

#ifdef TIOCGWINSZ
	{
		struct winsize ws;
		if (!ioctl(1, TIOCGWINSZ, &ws)) {
			if (ws.ws_col)
				return ws.ws_col;
		}
	}
#endif

	return 80;
}

28
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
29
{
30
	struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
31

32 33 34
	ent->len = len;
	memcpy(ent->name, name, len);
	ent->name[len] = 0;
35 36 37

	ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
	cmds->names[cmds->cnt++] = ent;
38 39 40 41 42 43 44 45 46
}

static int cmdname_compare(const void *a_, const void *b_)
{
	struct cmdname *a = *(struct cmdname **)a_;
	struct cmdname *b = *(struct cmdname **)b_;
	return strcmp(a->name, b->name);
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60
static void uniq(struct cmdnames *cmds)
{
	int i, j;

	if (!cmds->cnt)
		return;

	for (i = j = 1; i < cmds->cnt; i++)
		if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
			cmds->names[j++] = cmds->names[i];

	cmds->cnt = j;
}

61
void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
62
{
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	int ci, cj, ei;
	int cmp;

	ci = cj = ei = 0;
	while (ci < cmds->cnt && ei < excludes->cnt) {
		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
		if (cmp < 0)
			cmds->names[cj++] = cmds->names[ci++];
		else if (cmp == 0)
			ci++, ei++;
		else if (cmp > 0)
			ei++;
	}

	while (ci < cmds->cnt)
		cmds->names[cj++] = cmds->names[ci++];

	cmds->cnt = cj;
}

static void pretty_print_string_list(struct cmdnames *cmds, int longest)
84 85 86 87 88 89 90 91
{
	int cols = 1, rows;
	int space = longest + 1; /* min 1 SP between words */
	int max_cols = term_columns() - 1; /* don't print *on* the edge */
	int i, j;

	if (space < max_cols)
		cols = max_cols / space;
92
	rows = (cmds->cnt + cols - 1) / cols;
93 94 95 96 97 98 99

	for (i = 0; i < rows; i++) {
		printf("  ");

		for (j = 0; j < cols; j++) {
			int n = j * rows + i;
			int size = space;
100
			if (n >= cmds->cnt)
101
				break;
102
			if (j == cols-1 || n + rows >= cmds->cnt)
103
				size = 1;
104
			printf("%-*s", size, cmds->names[n]->name);
105 106 107 108 109
		}
		putchar('\n');
	}
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static int is_executable(const char *name)
{
	struct stat st;

	if (stat(name, &st) || /* stat, not lstat */
	    !S_ISREG(st.st_mode))
		return 0;

#ifdef __MINGW32__
	/* cannot trust the executable bit, peek into the file instead */
	char buf[3] = { 0 };
	int n;
	int fd = open(name, O_RDONLY);
	st.st_mode &= ~S_IXUSR;
	if (fd >= 0) {
		n = read(fd, buf, 2);
		if (n == 2)
			/* DOS executables start with "MZ" */
			if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
				st.st_mode |= S_IXUSR;
		close(fd);
	}
#endif
	return st.st_mode & S_IXUSR;
}

136
static void list_commands_in_dir(struct cmdnames *cmds,
137 138
					 const char *path,
					 const char *prefix)
139
{
140
	int prefix_len;
141
	DIR *dir = opendir(path);
142
	struct dirent *de;
143 144
	struct strbuf buf = STRBUF_INIT;
	int len;
145

146
	if (!dir)
147
		return;
148 149 150
	if (!prefix)
		prefix = "git-";
	prefix_len = strlen(prefix);
151

152 153 154
	strbuf_addf(&buf, "%s/", path);
	len = buf.len;

155 156 157
	while ((de = readdir(dir)) != NULL) {
		int entlen;

158
		if (prefixcmp(de->d_name, prefix))
159
			continue;
160

161 162 163
		strbuf_setlen(&buf, len);
		strbuf_addstr(&buf, de->d_name);
		if (!is_executable(buf.buf))
164 165
			continue;

166
		entlen = strlen(de->d_name) - prefix_len;
167
		if (has_extension(de->d_name, ".exe"))
168 169
			entlen -= 4;

170
		add_cmdname(cmds, de->d_name + prefix_len, entlen);
171 172
	}
	closedir(dir);
173
	strbuf_release(&buf);
174 175
}

176
void load_command_list(const char *prefix,
177 178
		struct cmdnames *main_cmds,
		struct cmdnames *other_cmds)
179 180 181 182 183 184
{
	const char *env_path = getenv("PATH");
	char *paths, *path, *colon;
	const char *exec_path = git_exec_path();

	if (exec_path)
185
		list_commands_in_dir(main_cmds, exec_path, prefix);
186 187 188 189 190 191 192 193

	if (!env_path) {
		fprintf(stderr, "PATH not set\n");
		exit(1);
	}

	path = paths = xstrdup(env_path);
	while (1) {
194
		if ((colon = strchr(path, PATH_SEP)))
195 196
			*colon = 0;

197
		list_commands_in_dir(other_cmds, path, prefix);
198 199 200 201 202 203 204

		if (!colon)
			break;
		path = colon + 1;
	}
	free(paths);

205 206 207
	qsort(main_cmds->names, main_cmds->cnt,
	      sizeof(*main_cmds->names), cmdname_compare);
	uniq(main_cmds);
208

209 210 211 212
	qsort(other_cmds->names, other_cmds->cnt,
	      sizeof(*other_cmds->names), cmdname_compare);
	uniq(other_cmds);
	exclude_cmds(other_cmds, main_cmds);
Jeff King's avatar
Jeff King committed
213 214
}

215 216
void list_commands(const char *title, struct cmdnames *main_cmds,
		   struct cmdnames *other_cmds)
Jeff King's avatar
Jeff King committed
217 218
{
	const char *exec_path = git_exec_path();
219 220 221 222 223 224 225 226
	int i, longest = 0;

	for (i = 0; i < main_cmds->cnt; i++)
		if (longest < main_cmds->names[i]->len)
			longest = main_cmds->names[i]->len;
	for (i = 0; i < other_cmds->cnt; i++)
		if (longest < other_cmds->names[i]->len)
			longest = other_cmds->names[i]->len;
Jeff King's avatar
Jeff King committed
227

228 229 230 231
	if (main_cmds->cnt) {
		printf("available %s in '%s'\n", title, exec_path);
		printf("----------------");
		mput_char('-', strlen(title) + strlen(exec_path));
232
		putchar('\n');
233
		pretty_print_string_list(main_cmds, longest);
234 235 236
		putchar('\n');
	}

237 238 239 240 241 242
	if (other_cmds->cnt) {
		printf("%s available from elsewhere on your $PATH\n", title);
		printf("---------------------------------------");
		mput_char('-', strlen(title));
		putchar('\n');
		pretty_print_string_list(other_cmds, longest);
243 244
		putchar('\n');
	}
245 246
}

247
int is_in_cmdlist(struct cmdnames *c, const char *s)
Jeff King's avatar
Jeff King committed
248 249 250 251 252 253 254 255
{
	int i;
	for (i = 0; i < c->cnt; i++)
		if (!strcmp(s, c->names[i]->name))
			return 1;
	return 0;
}

256 257
void help_unknown_cmd(const char *cmd)
{
258
	fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
259 260 261
	exit(1);
}

262
int cmd_version(int argc, const char **argv, const char *prefix)
263 264 265 266
{
	printf("git version %s\n", git_version_string);
	return 0;
}