2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 9f7d7167 2018-04-29 stsp #include <curses.h>
18 9f7d7167 2018-04-29 stsp #include <panel.h>
19 9f7d7167 2018-04-29 stsp #include <locale.h>
20 9f7d7167 2018-04-29 stsp #include <stdlib.h>
21 9f7d7167 2018-04-29 stsp #include <getopt.h>
22 9f7d7167 2018-04-29 stsp #include <string.h>
23 9f7d7167 2018-04-29 stsp #include <err.h>
25 9f7d7167 2018-04-29 stsp #include "got_error.h"
27 9f7d7167 2018-04-29 stsp #ifndef nitems
28 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
31 9f7d7167 2018-04-29 stsp enum tog_view_id {
32 9f7d7167 2018-04-29 stsp TOG_VIEW_LOG,
33 9f7d7167 2018-04-29 stsp TOG_VIEW_DIFF,
34 9f7d7167 2018-04-29 stsp TOG_VIEW_BLAME,
37 9f7d7167 2018-04-29 stsp struct tog_cmd {
38 9f7d7167 2018-04-29 stsp const char *cmd_name;
39 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
40 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
41 9f7d7167 2018-04-29 stsp enum tog_view_id view;
42 9f7d7167 2018-04-29 stsp const char *cmd_descr;
45 9f7d7167 2018-04-29 stsp __dead void usage(void);
46 9f7d7167 2018-04-29 stsp __dead void usage_log(void);
47 9f7d7167 2018-04-29 stsp __dead void usage_diff(void);
48 9f7d7167 2018-04-29 stsp __dead void usage_blame(void);
50 9f7d7167 2018-04-29 stsp const struct got_error* cmd_log(int, char *[]);
51 9f7d7167 2018-04-29 stsp const struct got_error* cmd_diff(int, char *[]);
52 9f7d7167 2018-04-29 stsp const struct got_error* cmd_blame(int, char *[]);
54 9f7d7167 2018-04-29 stsp struct tog_cmd tog_commands[] = {
55 9f7d7167 2018-04-29 stsp { "log", cmd_log, usage_log, TOG_VIEW_LOG,
56 9f7d7167 2018-04-29 stsp "show repository history" },
57 9f7d7167 2018-04-29 stsp { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
58 9f7d7167 2018-04-29 stsp "compare files and directories" },
59 9f7d7167 2018-04-29 stsp { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
60 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
63 9f7d7167 2018-04-29 stsp /* globals */
64 9f7d7167 2018-04-29 stsp enum tog_view_id tog_view_id;
65 9f7d7167 2018-04-29 stsp WINDOW *tog_main_win;
66 9f7d7167 2018-04-29 stsp PANEL *tog_main_panel;
69 9f7d7167 2018-04-29 stsp usage_log(void)
71 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s log [repository-path]\n",
72 9f7d7167 2018-04-29 stsp getprogname());
76 9f7d7167 2018-04-29 stsp const struct got_error *
77 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
79 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
83 9f7d7167 2018-04-29 stsp usage_diff(void)
85 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
86 9f7d7167 2018-04-29 stsp getprogname());
90 9f7d7167 2018-04-29 stsp const struct got_error *
91 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
93 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
97 9f7d7167 2018-04-29 stsp usage_blame(void)
99 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
100 9f7d7167 2018-04-29 stsp getprogname());
104 9f7d7167 2018-04-29 stsp const struct got_error *
105 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
107 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
110 9f7d7167 2018-04-29 stsp static const struct got_error *
111 9f7d7167 2018-04-29 stsp init_curses(void)
117 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
118 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
120 9f7d7167 2018-04-29 stsp tog_main_win = newwin(0, 0, 0, 0);
121 9f7d7167 2018-04-29 stsp if (tog_main_win == NULL)
122 9f7d7167 2018-04-29 stsp return got_error_from_errno();
123 9f7d7167 2018-04-29 stsp tog_main_panel = new_panel(tog_main_win);
124 9f7d7167 2018-04-29 stsp if (tog_main_panel == NULL)
125 9f7d7167 2018-04-29 stsp return got_error_from_errno();
127 9f7d7167 2018-04-29 stsp return NULL;
130 9f7d7167 2018-04-29 stsp __dead void
131 9f7d7167 2018-04-29 stsp usage(void)
135 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
136 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
137 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
138 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
139 9f7d7167 2018-04-29 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
145 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
147 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
148 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
149 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
151 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
153 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
154 9f7d7167 2018-04-29 stsp switch (ch) {
160 9f7d7167 2018-04-29 stsp /* NOTREACHED */
164 9f7d7167 2018-04-29 stsp argc -= optind;
165 9f7d7167 2018-04-29 stsp argv += optind;
166 9f7d7167 2018-04-29 stsp optind = 0;
168 9f7d7167 2018-04-29 stsp if (argc == 0)
169 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
173 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
174 9f7d7167 2018-04-29 stsp if (strncmp(tog_commands[i].cmd_name, argv[0],
175 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
176 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
178 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
182 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
183 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: unknown command '%s'\n",
184 9f7d7167 2018-04-29 stsp getprogname(), argv[0]);
189 9f7d7167 2018-04-29 stsp error = init_curses();
190 9f7d7167 2018-04-29 stsp if (error) {
191 9f7d7167 2018-04-29 stsp fprintf(stderr, "Cannot initialize ncurses: %s\n", error->msg);
195 9f7d7167 2018-04-29 stsp error = cmd->cmd_main(argc, argv);
201 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);