2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 const struct got_error *(*cmd_main)(int, char *[]);
52 void (*cmd_usage)(void);
53 enum tog_view_id view;
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" },
77 PANEL *tog_main_panel;
78 static struct tog_log_view {
87 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
92 static const struct got_error *
93 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
95 const struct got_error *err = NULL;
96 char *logmsg0 = NULL, *logmsg = NULL;
97 char *author0 = NULL, *author = NULL;
98 char *newline, *smallerthan;
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;
106 err = got_object_id_str(&id_str, id);
109 id_len = strlen(id_str);
111 logmsg0 = strdup(commit->logmsg);
112 if (logmsg0 == NULL) {
113 err = got_error_from_errno();
117 while (*logmsg == '\n')
119 newline = strchr(logmsg, '\n');
122 logmsg_len = strlen(logmsg);
124 author0 = strdup(commit->author);
125 if (author0 == NULL) {
126 err = got_error_from_errno();
130 smallerthan = strchr(author, '<');
134 char *at = strchr(author, '@');
138 author_len = strlen(author);
141 line = calloc(avail + 1, sizeof(*line));
143 err = got_error_from_errno();
148 for (i = 0; i < MIN(id_display_len, id_len); i++) {
151 line[col++] = id_str[i];
153 while (i < id_display_len) {
162 for (i = 0; i < MIN(author_display_len, author_len); i++) {
165 line[col++] = author[i];
167 while (i < author_display_len) {
177 while (col < avail && *logmsg)
178 line[col++] = *logmsg++;
182 waddstr(tog_log_view.window, line);
183 waddch(tog_log_view.window, '\n');
191 struct commit_queue_entry {
192 TAILQ_ENTRY(commit_queue_entry) entry;
193 struct got_object_id *id;
194 struct got_commit_object *commit;
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;
207 err = got_object_commit_open(&root_commit, repo, root_obj);
211 entry = calloc(1, sizeof(*entry));
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();
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);
236 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
237 err = got_error(GOT_ERR_OBJ_TYPE);
241 err = got_object_commit_open(&pcommit, repo, obj);
242 got_object_close(obj);
246 pentry = calloc(1, sizeof(*pentry));
247 if (pentry == NULL) {
248 err = got_error_from_errno();
249 got_object_commit_close(pcommit);
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);
258 pentry->commit = pcommit;
259 TAILQ_INSERT_TAIL(commits, pentry, entry);
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);
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;
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);
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);
330 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
331 err = got_error(GOT_ERR_OBJ_TYPE);
335 TAILQ_INIT(&commits);
337 if (refetch_commits) {
338 free_commits(&commits);
339 err = fetch_commits(&commits, obj, id, repo, LINES);
345 err = draw_commits(&commits, selected);
349 nodelay(stdscr, FALSE);
350 ch = wgetch(tog_log_view.window);
354 err = got_error_from_errno();
368 if (selected < LINES - 1)
373 if (selected > LINES)
374 selected = LINES - 1;
379 nodelay(stdscr, TRUE);
382 free_commits(&commits);
384 got_object_close(obj);
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;
399 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
403 while ((ch = getopt(argc, argv, "c:")) != -1) {
406 start_commit = optarg;
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();
428 error = got_repo_open(&repo, repo_path);
433 if (start_commit == NULL) {
434 struct got_reference *head_ref;
435 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
438 error = got_ref_resolve(&id, repo, head_ref);
439 got_ref_close(head_ref);
443 struct got_object *obj;
444 error = got_object_open_by_id_str(&obj, repo, start_commit);
446 id = got_object_get_id(obj);
448 error = got_error_from_errno();
453 error = show_log_view(id, repo);
455 got_repo_close(repo);
463 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
468 const struct got_error *
469 cmd_diff(int argc, char *argv[])
471 return got_error(GOT_ERR_NOT_IMPL);
478 fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
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 *
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();
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);
524 make_argv(const char *arg0, const char *arg1)
527 int argc = (arg1 == NULL ? 1 : 2);
529 argv = calloc(argc, sizeof(char *));
532 argv[0] = strdup(arg0);
536 argv[1] = strdup(arg1);
545 main(int argc, char *argv[])
547 const struct got_error *error = NULL;
548 struct tog_cmd *cmd = NULL;
550 char **cmd_argv = NULL;
552 setlocale(LC_ALL, "");
554 while ((ch = getopt(argc, argv, "h")) != -1) {
571 /* Build an argument vector which runs a default command. */
572 cmd = &tog_commands[0];
573 cmd_argv = make_argv(cmd->name, NULL);
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];
584 tog_commands[i].cmd_usage();
589 /* Did the user specify a repository? */
590 char *repo_path = realpath(argv[0], NULL);
592 struct got_repository *repo;
593 error = got_repo_open(&repo, repo_path);
595 got_repo_close(repo);
597 error = got_error_from_errno();
599 fprintf(stderr, "%s: '%s' is neither a known "
600 "command nor a path to a repository\n",
601 getprogname(), argv[0]);
605 cmd = &tog_commands[0];
606 cmd_argv = make_argv(cmd->name, repo_path);
612 error = init_curses();
614 fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
618 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
625 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);