Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <curses.h>
21 #include <panel.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <string.h>
26 #include <err.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
35 #ifndef MIN
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 #endif
39 #ifndef nitems
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 #endif
43 enum tog_view_id {
44 TOG_VIEW_LOG,
45 TOG_VIEW_DIFF,
46 TOG_VIEW_BLAME,
47 };
49 struct tog_cmd {
50 const char *name;
51 const struct got_error *(*cmd_main)(int, char *[]);
52 void (*cmd_usage)(void);
53 enum tog_view_id view;
54 const char *descr;
55 };
57 __dead void usage(void);
58 __dead void usage_log(void);
59 __dead void usage_diff(void);
60 __dead void usage_blame(void);
62 const struct got_error* cmd_log(int, char *[]);
63 const struct got_error* cmd_diff(int, char *[]);
64 const struct got_error* cmd_blame(int, char *[]);
66 struct tog_cmd tog_commands[] = {
67 { "log", cmd_log, usage_log, TOG_VIEW_LOG,
68 "show repository history" },
69 { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
70 "compare files and directories" },
71 { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
72 "show line-by-line file history" },
73 };
75 /* globals */
76 WINDOW *tog_main_win;
77 PANEL *tog_main_panel;
78 static struct tog_log_view {
79 WINDOW *window;
80 PANEL *panel;
81 } tog_log_view;
83 __dead void
84 usage_log(void)
85 {
86 endwin();
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 getprogname());
89 exit(1);
90 }
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
94 {
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
99 char *line = NULL;
100 char *id_str = NULL;
101 const size_t id_display_len = 8;
102 const size_t author_display_len = 16;
103 size_t id_len, author_len, logmsg_len, avail;
104 int i, col;
106 err = got_object_id_str(&id_str, id);
107 if (err)
108 return err;
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
114 goto done;
116 logmsg = logmsg0;
117 while (*logmsg == '\n')
118 logmsg++;
119 newline = strchr(logmsg, '\n');
120 if (newline)
121 *newline = '\0';
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
127 goto done;
129 author = author0;
130 smallerthan = strchr(author, '<');
131 if (smallerthan)
132 *smallerthan = '\0';
133 else {
134 char *at = strchr(author, '@');
135 if (at)
136 *at = '\0';
138 author_len = strlen(author);
140 avail = COLS - 1;
141 line = calloc(avail + 1, sizeof(*line));
142 if (line == NULL) {
143 err = got_error_from_errno();
144 goto done;
147 col = 0;
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 if (col >= avail)
150 goto draw;
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
154 if (col >= avail)
155 goto draw;
156 line[col++] = ' ';
157 i++;
159 if (col >= avail)
160 goto draw;
161 line[col++] = ' ';
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 if (col >= avail)
164 goto draw;
165 line[col++] = author[i];
167 while (i < author_display_len) {
168 if (col >= avail)
169 goto draw;
170 line[col++] = ' ';
171 i++;
173 if (col >= avail)
174 goto draw;
175 line[col++] = ' ';
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
179 while (col < avail)
180 line[col++] = ' ';
181 draw:
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
184 done:
185 free(logmsg0);
186 free(author0);
187 free(line);
188 free(id_str);
189 return err;
191 struct commit_queue_entry {
192 TAILQ_ENTRY(commit_queue_entry) entry;
193 struct got_object_id *id;
194 struct got_commit_object *commit;
195 };
196 TAILQ_HEAD(commit_queue, commit_queue_entry);
198 static const struct got_error *
199 fetch_commits(struct commit_queue *commits, struct got_object *root_obj,
200 struct got_object_id *root_id, struct got_repository *repo, int limit)
202 const struct got_error *err;
203 struct got_commit_object *root_commit;
204 struct commit_queue_entry *entry;
205 int ncommits = 0;
207 err = got_object_commit_open(&root_commit, repo, root_obj);
208 if (err)
209 return err;
211 entry = calloc(1, sizeof(*entry));
212 if (entry == NULL)
213 return got_error_from_errno();
214 entry->id = got_object_id_dup(root_id);
215 if (entry->id == NULL) {
216 err = got_error_from_errno();
217 free(entry);
218 return err;
220 entry->commit = root_commit;
221 TAILQ_INSERT_HEAD(commits, entry, entry);
223 while (entry->commit->nparents > 0 && ncommits < limit) {
224 struct got_parent_id *pid;
225 struct got_object *obj;
226 struct got_commit_object *pcommit;
227 struct commit_queue_entry *pentry;
229 entry = TAILQ_LAST(commits, commit_queue);
231 /* Follow the first parent (TODO: handle merge commits). */
232 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
233 err = got_object_open(&obj, repo, pid->id);
234 if (err)
235 break;
236 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
237 err = got_error(GOT_ERR_OBJ_TYPE);
238 break;
241 err = got_object_commit_open(&pcommit, repo, obj);
242 got_object_close(obj);
243 if (err)
244 break;
246 pentry = calloc(1, sizeof(*pentry));
247 if (pentry == NULL) {
248 err = got_error_from_errno();
249 got_object_commit_close(pcommit);
250 break;
252 pentry->id = got_object_id_dup(pid->id);
253 if (pentry->id == NULL) {
254 err = got_error_from_errno();
255 got_object_commit_close(pcommit);
256 break;
258 pentry->commit = pcommit;
259 TAILQ_INSERT_TAIL(commits, pentry, entry);
260 ncommits++;
263 return err;
266 static void
267 free_commits(struct commit_queue *commits)
269 struct commit_queue_entry *entry;
271 while (!TAILQ_EMPTY(commits)) {
272 entry = TAILQ_FIRST(commits);
273 TAILQ_REMOVE(commits, entry, entry);
274 got_object_commit_close(entry->commit);
275 free(entry->id);
276 free(entry);
280 static const struct got_error *
281 draw_commits(struct commit_queue *commits, int selected)
283 const struct got_error *err = NULL;
284 struct commit_queue_entry *entry;
285 int ncommits = 0;
287 wclear(tog_log_view.window);
289 TAILQ_FOREACH(entry, commits, entry) {
290 if (ncommits == selected)
291 wstandout(tog_log_view.window);
292 err = draw_commit(entry->commit, entry->id);
293 if (ncommits == selected)
294 wstandend(tog_log_view.window);
295 if (err)
296 break;
297 ncommits++;
300 update_panels();
301 doupdate();
303 return err;
306 static const struct got_error *
307 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_object *obj = NULL;
311 int ch, done = 0, selected = 0, refetch_commits = 1;
312 struct got_object_id *id = start_id;
313 struct commit_queue commits;
315 if (tog_log_view.window == NULL) {
316 tog_log_view.window = newwin(0, 0, 0, 0);
317 if (tog_log_view.window == NULL)
318 return got_error_from_errno();
319 keypad(tog_log_view.window, TRUE);
321 if (tog_log_view.panel == NULL) {
322 tog_log_view.panel = new_panel(tog_log_view.window);
323 if (tog_log_view.panel == NULL)
324 return got_error_from_errno();
327 err = got_object_open(&obj, repo, id);
328 if (err)
329 return err;
330 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
331 err = got_error(GOT_ERR_OBJ_TYPE);
332 goto done;
335 TAILQ_INIT(&commits);
336 do {
337 if (refetch_commits) {
338 free_commits(&commits);
339 err = fetch_commits(&commits, obj, id, repo, LINES);
340 refetch_commits = 0;
341 if (err)
342 goto done;
345 err = draw_commits(&commits, selected);
346 if (err)
347 goto done;
349 nodelay(stdscr, FALSE);
350 ch = wgetch(tog_log_view.window);
351 switch (ch) {
352 case ERR:
353 if (errno) {
354 err = got_error_from_errno();
355 goto done;
357 break;
358 case 'q':
359 done = 1;
360 break;
361 case 'k':
362 case KEY_UP:
363 if (selected > 0)
364 selected--;
365 break;
366 case 'j':
367 case KEY_DOWN:
368 if (selected < LINES - 1)
369 selected++;
370 break;
371 case KEY_RESIZE:
372 refetch_commits = 1;
373 if (selected > LINES)
374 selected = LINES - 1;
375 break;
376 default:
377 break;
379 nodelay(stdscr, TRUE);
380 } while (!done);
381 done:
382 free_commits(&commits);
383 if (obj)
384 got_object_close(obj);
385 return err;
388 const struct got_error *
389 cmd_log(int argc, char *argv[])
391 const struct got_error *error;
392 struct got_repository *repo;
393 struct got_object_id *id = NULL;
394 char *repo_path = NULL;
395 char *start_commit = NULL;
396 int ch;
398 #ifndef PROFILE
399 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
400 err(1, "pledge");
401 #endif
403 while ((ch = getopt(argc, argv, "c:")) != -1) {
404 switch (ch) {
405 case 'c':
406 start_commit = optarg;
407 break;
408 default:
409 usage();
410 /* NOTREACHED */
414 argc -= optind;
415 argv += optind;
417 if (argc == 0) {
418 repo_path = getcwd(NULL, 0);
419 if (repo_path == NULL)
420 return got_error_from_errno();
421 } else if (argc == 1) {
422 repo_path = realpath(argv[0], NULL);
423 if (repo_path == NULL)
424 return got_error_from_errno();
425 } else
426 usage_log();
428 error = got_repo_open(&repo, repo_path);
429 free(repo_path);
430 if (error != NULL)
431 return error;
433 if (start_commit == NULL) {
434 struct got_reference *head_ref;
435 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
436 if (error != NULL)
437 return error;
438 error = got_ref_resolve(&id, repo, head_ref);
439 got_ref_close(head_ref);
440 if (error != NULL)
441 return error;
442 } else {
443 struct got_object *obj;
444 error = got_object_open_by_id_str(&obj, repo, start_commit);
445 if (error == NULL) {
446 id = got_object_get_id(obj);
447 if (id == NULL)
448 error = got_error_from_errno();
451 if (error != NULL)
452 return error;
453 error = show_log_view(id, repo);
454 free(id);
455 got_repo_close(repo);
456 return error;
459 __dead void
460 usage_diff(void)
462 endwin();
463 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
464 getprogname());
465 exit(1);
468 const struct got_error *
469 cmd_diff(int argc, char *argv[])
471 return got_error(GOT_ERR_NOT_IMPL);
474 __dead void
475 usage_blame(void)
477 endwin();
478 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
479 getprogname());
480 exit(1);
483 const struct got_error *
484 cmd_blame(int argc, char *argv[])
486 return got_error(GOT_ERR_NOT_IMPL);
489 static const struct got_error *
490 init_curses(void)
492 initscr();
493 cbreak();
494 noecho();
495 nonl();
496 intrflush(stdscr, FALSE);
497 keypad(stdscr, TRUE);
499 tog_main_win = newwin(0, 0, 0, 0);
500 if (tog_main_win == NULL)
501 return got_error_from_errno();
502 tog_main_panel = new_panel(tog_main_win);
503 if (tog_main_panel == NULL)
504 return got_error_from_errno();
506 return NULL;
509 __dead void
510 usage(void)
512 int i;
514 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
515 "Available commands:\n", getprogname());
516 for (i = 0; i < nitems(tog_commands); i++) {
517 struct tog_cmd *cmd = &tog_commands[i];
518 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
520 exit(1);
523 static char **
524 make_argv(const char *arg0, const char *arg1)
526 char **argv;
527 int argc = (arg1 == NULL ? 1 : 2);
529 argv = calloc(argc, sizeof(char *));
530 if (argv == NULL)
531 err(1, "calloc");
532 argv[0] = strdup(arg0);
533 if (argv[0] == NULL)
534 err(1, "calloc");
535 if (arg1) {
536 argv[1] = strdup(arg1);
537 if (argv[1] == NULL)
538 err(1, "calloc");
541 return argv;
544 int
545 main(int argc, char *argv[])
547 const struct got_error *error = NULL;
548 struct tog_cmd *cmd = NULL;
549 int ch, hflag = 0;
550 char **cmd_argv = NULL;
552 setlocale(LC_ALL, "");
554 while ((ch = getopt(argc, argv, "h")) != -1) {
555 switch (ch) {
556 case 'h':
557 hflag = 1;
558 break;
559 default:
560 usage();
561 /* NOTREACHED */
565 argc -= optind;
566 argv += optind;
567 optind = 0;
568 optreset = 1;
570 if (argc == 0) {
571 /* Build an argument vector which runs a default command. */
572 cmd = &tog_commands[0];
573 cmd_argv = make_argv(cmd->name, NULL);
574 argc = 1;
575 } else {
576 int i;
578 /* Did the user specific a command? */
579 for (i = 0; i < nitems(tog_commands); i++) {
580 if (strncmp(tog_commands[i].name, argv[0],
581 strlen(argv[0])) == 0) {
582 cmd = &tog_commands[i];
583 if (hflag)
584 tog_commands[i].cmd_usage();
585 break;
588 if (cmd == NULL) {
589 /* Did the user specify a repository? */
590 char *repo_path = realpath(argv[0], NULL);
591 if (repo_path) {
592 struct got_repository *repo;
593 error = got_repo_open(&repo, repo_path);
594 if (error == NULL)
595 got_repo_close(repo);
596 } else
597 error = got_error_from_errno();
598 if (error) {
599 fprintf(stderr, "%s: '%s' is neither a known "
600 "command nor a path to a repository\n",
601 getprogname(), argv[0]);
602 free(repo_path);
603 return 1;
605 cmd = &tog_commands[0];
606 cmd_argv = make_argv(cmd->name, repo_path);
607 argc = 2;
608 free(repo_path);
612 error = init_curses();
613 if (error) {
614 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
615 return 1;
618 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
619 if (error)
620 goto done;
621 done:
622 endwin();
623 free(cmd_argv);
624 if (error)
625 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
626 return 0;