help.c 5.56 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 unsigned int list_commands_in_dir(struct cmdnames *cmds,
137 138
					 const char *path,
					 const char *prefix)
139 140
{
	unsigned int longest = 0;
141
	int prefix_len;
142
	DIR *dir = opendir(path);
143
	struct dirent *de;
144 145
	struct strbuf buf = STRBUF_INIT;
	int len;
146

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

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

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

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

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

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

		if (longest < entlen)
			longest = entlen;

174
		add_cmdname(cmds, de->d_name + prefix_len, entlen);
175 176
	}
	closedir(dir);
177
	strbuf_release(&buf);
178

179 180 181
	return longest;
}

182 183 184
unsigned int load_command_list(const char *prefix,
		struct cmdnames *main_cmds,
		struct cmdnames *other_cmds)
185 186 187 188 189 190 191 192
{
	unsigned int longest = 0;
	unsigned int len;
	const char *env_path = getenv("PATH");
	char *paths, *path, *colon;
	const char *exec_path = git_exec_path();

	if (exec_path)
193
		longest = list_commands_in_dir(main_cmds, exec_path, prefix);
194 195 196 197 198 199 200 201

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

	path = paths = xstrdup(env_path);
	while (1) {
202
		if ((colon = strchr(path, PATH_SEP)))
203 204
			*colon = 0;

205
		len = list_commands_in_dir(other_cmds, path, prefix);
206 207 208 209 210 211 212 213 214
		if (len > longest)
			longest = len;

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

215 216 217
	qsort(main_cmds->names, main_cmds->cnt,
	      sizeof(*main_cmds->names), cmdname_compare);
	uniq(main_cmds);
218

219 220 221 222
	qsort(other_cmds->names, other_cmds->cnt,
	      sizeof(*other_cmds->names), cmdname_compare);
	uniq(other_cmds);
	exclude_cmds(other_cmds, main_cmds);
223

Jeff King's avatar
Jeff King committed
224 225 226
	return longest;
}

227 228
void list_commands(const char *title, unsigned int longest,
		struct cmdnames *main_cmds, struct cmdnames *other_cmds)
Jeff King's avatar
Jeff King committed
229 230 231
{
	const char *exec_path = git_exec_path();

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

241 242 243 244 245 246
	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);
247 248
		putchar('\n');
	}
249 250
}

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

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

266
int cmd_version(int argc, const char **argv, const char *prefix)
267 268 269 270
{
	printf("git version %s\n", git_version_string);
	return 0;
}