git-blame, take 2
[git/jnareb-git.git] / git.c
blobb0da6b19457cf06f8e880bd0b18f270da94069b1
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
13 #include "exec_cmd.h"
15 #include "cache.h"
16 #include "commit.h"
17 #include "revision.h"
19 #ifndef PATH_MAX
20 # define PATH_MAX 4096
21 #endif
23 static const char git_usage[] =
24 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
26 /* most gui terms set COLUMNS (although some don't export it) */
27 static int term_columns(void)
29 char *col_string = getenv("COLUMNS");
30 int n_cols = 0;
32 if (col_string && (n_cols = atoi(col_string)) > 0)
33 return n_cols;
35 #ifdef TIOCGWINSZ
37 struct winsize ws;
38 if (!ioctl(1, TIOCGWINSZ, &ws)) {
39 if (ws.ws_col)
40 return ws.ws_col;
43 #endif
45 return 80;
48 static void oom(void)
50 fprintf(stderr, "git: out of memory\n");
51 exit(1);
54 static inline void mput_char(char c, unsigned int num)
56 while(num--)
57 putchar(c);
60 static struct cmdname {
61 size_t len;
62 char name[1];
63 } **cmdname;
64 static int cmdname_alloc, cmdname_cnt;
66 static void add_cmdname(const char *name, int len)
68 struct cmdname *ent;
69 if (cmdname_alloc <= cmdname_cnt) {
70 cmdname_alloc = cmdname_alloc + 200;
71 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
72 if (!cmdname)
73 oom();
75 ent = malloc(sizeof(*ent) + len);
76 if (!ent)
77 oom();
78 ent->len = len;
79 memcpy(ent->name, name, len);
80 ent->name[len] = 0;
81 cmdname[cmdname_cnt++] = ent;
84 static int cmdname_compare(const void *a_, const void *b_)
86 struct cmdname *a = *(struct cmdname **)a_;
87 struct cmdname *b = *(struct cmdname **)b_;
88 return strcmp(a->name, b->name);
91 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
93 int cols = 1, rows;
94 int space = longest + 1; /* min 1 SP between words */
95 int max_cols = term_columns() - 1; /* don't print *on* the edge */
96 int i, j;
98 if (space < max_cols)
99 cols = max_cols / space;
100 rows = (cmdname_cnt + cols - 1) / cols;
102 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
104 for (i = 0; i < rows; i++) {
105 printf(" ");
107 for (j = 0; j < cols; j++) {
108 int n = j * rows + i;
109 int size = space;
110 if (n >= cmdname_cnt)
111 break;
112 if (j == cols-1 || n + rows >= cmdname_cnt)
113 size = 1;
114 printf("%-*s", size, cmdname[n]->name);
116 putchar('\n');
120 static void list_commands(const char *exec_path, const char *pattern)
122 unsigned int longest = 0;
123 char path[PATH_MAX];
124 int dirlen;
125 DIR *dir = opendir(exec_path);
126 struct dirent *de;
128 if (!dir) {
129 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
130 exit(1);
133 dirlen = strlen(exec_path);
134 if (PATH_MAX - 20 < dirlen) {
135 fprintf(stderr, "git: insanely long exec-path '%s'\n",
136 exec_path);
137 exit(1);
140 memcpy(path, exec_path, dirlen);
141 path[dirlen++] = '/';
143 while ((de = readdir(dir)) != NULL) {
144 struct stat st;
145 int entlen;
147 if (strncmp(de->d_name, "git-", 4))
148 continue;
149 strcpy(path+dirlen, de->d_name);
150 if (stat(path, &st) || /* stat, not lstat */
151 !S_ISREG(st.st_mode) ||
152 !(st.st_mode & S_IXUSR))
153 continue;
155 entlen = strlen(de->d_name);
156 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
157 entlen -= 4;
159 if (longest < entlen)
160 longest = entlen;
162 add_cmdname(de->d_name + 4, entlen-4);
164 closedir(dir);
166 printf("git commands available in '%s'\n", exec_path);
167 printf("----------------------------");
168 mput_char('-', strlen(exec_path));
169 putchar('\n');
170 pretty_print_string_list(cmdname, longest - 4);
171 putchar('\n');
174 #ifdef __GNUC__
175 static void cmd_usage(const char *exec_path, const char *fmt, ...)
176 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
177 #endif
178 static void cmd_usage(const char *exec_path, const char *fmt, ...)
180 if (fmt) {
181 va_list ap;
183 va_start(ap, fmt);
184 printf("git: ");
185 vprintf(fmt, ap);
186 va_end(ap);
187 putchar('\n');
189 else
190 puts(git_usage);
192 putchar('\n');
194 if(exec_path)
195 list_commands(exec_path, "git-*");
197 exit(1);
200 static void prepend_to_path(const char *dir, int len)
202 char *path, *old_path = getenv("PATH");
203 int path_len = len;
205 if (!old_path)
206 old_path = "/usr/local/bin:/usr/bin:/bin";
208 path_len = len + strlen(old_path) + 1;
210 path = malloc(path_len + 1);
212 memcpy(path, dir, len);
213 path[len] = ':';
214 memcpy(path + len + 1, old_path, path_len - len);
216 setenv("PATH", path, 1);
219 static void show_man_page(char *git_cmd)
221 char *page;
223 if (!strncmp(git_cmd, "git", 3))
224 page = git_cmd;
225 else {
226 int page_len = strlen(git_cmd) + 4;
228 page = malloc(page_len + 1);
229 strcpy(page, "git-");
230 strcpy(page + 4, git_cmd);
231 page[page_len] = 0;
234 execlp("man", "man", page, NULL);
237 static int cmd_version(int argc, char **argv, char **envp)
239 printf("git version %s\n", GIT_VERSION);
240 return 0;
243 static int cmd_help(int argc, char **argv, char **envp)
245 char *help_cmd = argv[1];
246 if (!help_cmd)
247 cmd_usage(git_exec_path(), NULL);
248 show_man_page(help_cmd);
249 return 0;
252 #define LOGSIZE (65536)
254 static int cmd_log(int argc, char **argv, char **envp)
256 struct rev_info rev;
257 struct commit *commit;
258 char *buf = xmalloc(LOGSIZE);
260 argc = setup_revisions(argc, argv, &rev, "HEAD");
261 prepare_revision_walk(&rev);
262 setup_pager();
263 while ((commit = get_revision(&rev)) != NULL) {
264 pretty_print_commit(CMIT_FMT_DEFAULT, commit, ~0, buf, LOGSIZE, 18);
265 printf("%s\n", buf);
267 free(buf);
268 return 0;
271 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
273 static void handle_internal_command(int argc, char **argv, char **envp)
275 const char *cmd = argv[0];
276 static struct cmd_struct {
277 const char *cmd;
278 int (*fn)(int, char **, char **);
279 } commands[] = {
280 { "version", cmd_version },
281 { "help", cmd_help },
282 { "log", cmd_log },
284 int i;
286 for (i = 0; i < ARRAY_SIZE(commands); i++) {
287 struct cmd_struct *p = commands+i;
288 if (strcmp(p->cmd, cmd))
289 continue;
290 exit(p->fn(argc, argv, envp));
294 int main(int argc, char **argv, char **envp)
296 char *cmd = argv[0];
297 char *slash = strrchr(cmd, '/');
298 char git_command[PATH_MAX + 1];
299 const char *exec_path = NULL;
302 * Take the basename of argv[0] as the command
303 * name, and the dirname as the default exec_path
304 * if it's an absolute path and we don't have
305 * anything better.
307 if (slash) {
308 *slash++ = 0;
309 if (*cmd == '/')
310 exec_path = cmd;
311 cmd = slash;
315 * "git-xxxx" is the same as "git xxxx", but we obviously:
317 * - cannot take flags in between the "git" and the "xxxx".
318 * - cannot execute it externally (since it would just do
319 * the same thing over again)
321 * So we just directly call the internal command handler, and
322 * die if that one cannot handle it.
324 if (!strncmp(cmd, "git-", 4)) {
325 cmd += 4;
326 argv[0] = cmd;
327 handle_internal_command(argc, argv, envp);
328 die("cannot handle %s internally", cmd);
331 /* Default command: "help" */
332 cmd = "help";
334 /* Look for flags.. */
335 while (argc > 1) {
336 cmd = *++argv;
337 argc--;
339 if (strncmp(cmd, "--", 2))
340 break;
342 cmd += 2;
345 * For legacy reasons, the "version" and "help"
346 * commands can be written with "--" prepended
347 * to make them look like flags.
349 if (!strcmp(cmd, "help"))
350 break;
351 if (!strcmp(cmd, "version"))
352 break;
355 * Check remaining flags (which by now must be
356 * "--exec-path", but maybe we will accept
357 * other arguments some day)
359 if (!strncmp(cmd, "exec-path", 9)) {
360 cmd += 9;
361 if (*cmd == '=') {
362 git_set_exec_path(cmd + 1);
363 continue;
365 puts(git_exec_path());
366 exit(0);
368 cmd_usage(NULL, NULL);
370 argv[0] = cmd;
373 * We search for git commands in the following order:
374 * - git_exec_path()
375 * - the path of the "git" command if we could find it
376 * in $0
377 * - the regular PATH.
379 if (exec_path)
380 prepend_to_path(exec_path, strlen(exec_path));
381 exec_path = git_exec_path();
382 prepend_to_path(exec_path, strlen(exec_path));
384 /* See if it's an internal command */
385 handle_internal_command(argc, argv, envp);
387 /* .. then try the external ones */
388 execv_git_cmd(argv);
390 if (errno == ENOENT)
391 cmd_usage(exec_path, "'%s' is not a git-command", cmd);
393 fprintf(stderr, "Failed to run command '%s': %s\n",
394 git_command, strerror(errno));
396 return 1;