Blame


1 9f7d7167 2018-04-29 stsp /*
2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 80ddbec8 2018-04-29 stsp
19 31120ada 2018-04-30 stsp #include <errno.h>
20 9f7d7167 2018-04-29 stsp #include <curses.h>
21 9f7d7167 2018-04-29 stsp #include <panel.h>
22 9f7d7167 2018-04-29 stsp #include <locale.h>
23 9f7d7167 2018-04-29 stsp #include <stdlib.h>
24 9f7d7167 2018-04-29 stsp #include <getopt.h>
25 9f7d7167 2018-04-29 stsp #include <string.h>
26 9f7d7167 2018-04-29 stsp #include <err.h>
27 80ddbec8 2018-04-29 stsp #include <unistd.h>
28 9f7d7167 2018-04-29 stsp
29 9f7d7167 2018-04-29 stsp #include "got_error.h"
30 80ddbec8 2018-04-29 stsp #include "got_object.h"
31 80ddbec8 2018-04-29 stsp #include "got_reference.h"
32 80ddbec8 2018-04-29 stsp #include "got_repository.h"
33 80ddbec8 2018-04-29 stsp #include "got_diff.h"
34 9f7d7167 2018-04-29 stsp
35 881b2d3e 2018-04-30 stsp #ifndef MIN
36 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 881b2d3e 2018-04-30 stsp #endif
38 881b2d3e 2018-04-30 stsp
39 9f7d7167 2018-04-29 stsp #ifndef nitems
40 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 9f7d7167 2018-04-29 stsp #endif
42 9f7d7167 2018-04-29 stsp
43 9f7d7167 2018-04-29 stsp enum tog_view_id {
44 9f7d7167 2018-04-29 stsp TOG_VIEW_LOG,
45 9f7d7167 2018-04-29 stsp TOG_VIEW_DIFF,
46 9f7d7167 2018-04-29 stsp TOG_VIEW_BLAME,
47 9f7d7167 2018-04-29 stsp };
48 9f7d7167 2018-04-29 stsp
49 9f7d7167 2018-04-29 stsp struct tog_cmd {
50 c2301be8 2018-04-30 stsp const char *name;
51 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
52 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
53 9f7d7167 2018-04-29 stsp enum tog_view_id view;
54 c2301be8 2018-04-30 stsp const char *descr;
55 9f7d7167 2018-04-29 stsp };
56 9f7d7167 2018-04-29 stsp
57 9f7d7167 2018-04-29 stsp __dead void usage(void);
58 9f7d7167 2018-04-29 stsp __dead void usage_log(void);
59 9f7d7167 2018-04-29 stsp __dead void usage_diff(void);
60 9f7d7167 2018-04-29 stsp __dead void usage_blame(void);
61 9f7d7167 2018-04-29 stsp
62 9f7d7167 2018-04-29 stsp const struct got_error* cmd_log(int, char *[]);
63 9f7d7167 2018-04-29 stsp const struct got_error* cmd_diff(int, char *[]);
64 9f7d7167 2018-04-29 stsp const struct got_error* cmd_blame(int, char *[]);
65 9f7d7167 2018-04-29 stsp
66 9f7d7167 2018-04-29 stsp struct tog_cmd tog_commands[] = {
67 9f7d7167 2018-04-29 stsp { "log", cmd_log, usage_log, TOG_VIEW_LOG,
68 9f7d7167 2018-04-29 stsp "show repository history" },
69 9f7d7167 2018-04-29 stsp { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
70 9f7d7167 2018-04-29 stsp "compare files and directories" },
71 9f7d7167 2018-04-29 stsp { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
72 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
73 9f7d7167 2018-04-29 stsp };
74 9f7d7167 2018-04-29 stsp
75 9f7d7167 2018-04-29 stsp /* globals */
76 9f7d7167 2018-04-29 stsp WINDOW *tog_main_win;
77 9f7d7167 2018-04-29 stsp PANEL *tog_main_panel;
78 80ddbec8 2018-04-29 stsp static struct tog_log_view {
79 80ddbec8 2018-04-29 stsp WINDOW *window;
80 80ddbec8 2018-04-29 stsp PANEL *panel;
81 80ddbec8 2018-04-29 stsp } tog_log_view;
82 9f7d7167 2018-04-29 stsp
83 9f7d7167 2018-04-29 stsp __dead void
84 9f7d7167 2018-04-29 stsp usage_log(void)
85 9f7d7167 2018-04-29 stsp {
86 80ddbec8 2018-04-29 stsp endwin();
87 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 9f7d7167 2018-04-29 stsp getprogname());
89 9f7d7167 2018-04-29 stsp exit(1);
90 80ddbec8 2018-04-29 stsp }
91 80ddbec8 2018-04-29 stsp
92 80ddbec8 2018-04-29 stsp static const struct got_error *
93 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
94 80ddbec8 2018-04-29 stsp {
95 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
96 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
97 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
98 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
99 80ddbec8 2018-04-29 stsp char *line = NULL;
100 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
101 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
102 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
103 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
104 881b2d3e 2018-04-30 stsp int i, col;
105 80ddbec8 2018-04-29 stsp
106 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
107 80ddbec8 2018-04-29 stsp if (err)
108 80ddbec8 2018-04-29 stsp return err;
109 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
110 881b2d3e 2018-04-30 stsp
111 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
112 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
113 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
114 6d9fbc00 2018-04-29 stsp goto done;
115 6d9fbc00 2018-04-29 stsp }
116 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
117 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
118 80ddbec8 2018-04-29 stsp logmsg++;
119 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
120 6d9fbc00 2018-04-29 stsp if (newline)
121 80ddbec8 2018-04-29 stsp *newline = '\0';
122 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
123 80ddbec8 2018-04-29 stsp
124 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
125 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
126 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
127 80ddbec8 2018-04-29 stsp goto done;
128 80ddbec8 2018-04-29 stsp }
129 6d9fbc00 2018-04-29 stsp author = author0;
130 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
131 6d9fbc00 2018-04-29 stsp if (smallerthan)
132 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
133 6d9fbc00 2018-04-29 stsp else {
134 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
135 6d9fbc00 2018-04-29 stsp if (at)
136 6d9fbc00 2018-04-29 stsp *at = '\0';
137 6d9fbc00 2018-04-29 stsp }
138 881b2d3e 2018-04-30 stsp author_len = strlen(author);
139 80ddbec8 2018-04-29 stsp
140 881b2d3e 2018-04-30 stsp avail = COLS - 1;
141 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
142 881b2d3e 2018-04-30 stsp if (line == NULL) {
143 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
144 6d9fbc00 2018-04-29 stsp goto done;
145 6d9fbc00 2018-04-29 stsp }
146 6d9fbc00 2018-04-29 stsp
147 881b2d3e 2018-04-30 stsp col = 0;
148 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 881b2d3e 2018-04-30 stsp if (col >= avail)
150 881b2d3e 2018-04-30 stsp goto draw;
151 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
152 80ddbec8 2018-04-29 stsp }
153 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
154 881b2d3e 2018-04-30 stsp if (col >= avail)
155 881b2d3e 2018-04-30 stsp goto draw;
156 881b2d3e 2018-04-30 stsp line[col++] = ' ';
157 881b2d3e 2018-04-30 stsp i++;
158 881b2d3e 2018-04-30 stsp }
159 881b2d3e 2018-04-30 stsp if (col >= avail)
160 881b2d3e 2018-04-30 stsp goto draw;
161 881b2d3e 2018-04-30 stsp line[col++] = ' ';
162 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 881b2d3e 2018-04-30 stsp if (col >= avail)
164 881b2d3e 2018-04-30 stsp goto draw;
165 881b2d3e 2018-04-30 stsp line[col++] = author[i];
166 881b2d3e 2018-04-30 stsp }
167 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
168 881b2d3e 2018-04-30 stsp if (col >= avail)
169 881b2d3e 2018-04-30 stsp goto draw;
170 881b2d3e 2018-04-30 stsp line[col++] = ' ';
171 881b2d3e 2018-04-30 stsp i++;
172 881b2d3e 2018-04-30 stsp }
173 881b2d3e 2018-04-30 stsp if (col >= avail)
174 881b2d3e 2018-04-30 stsp goto draw;
175 881b2d3e 2018-04-30 stsp line[col++] = ' ';
176 881b2d3e 2018-04-30 stsp
177 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
178 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
179 881b2d3e 2018-04-30 stsp while (col < avail)
180 881b2d3e 2018-04-30 stsp line[col++] = ' ';
181 881b2d3e 2018-04-30 stsp draw:
182 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
183 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
184 80ddbec8 2018-04-29 stsp done:
185 80ddbec8 2018-04-29 stsp free(logmsg0);
186 6d9fbc00 2018-04-29 stsp free(author0);
187 80ddbec8 2018-04-29 stsp free(line);
188 6d9fbc00 2018-04-29 stsp free(id_str);
189 80ddbec8 2018-04-29 stsp return err;
190 80ddbec8 2018-04-29 stsp }
191 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
192 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
193 80ddbec8 2018-04-29 stsp struct got_object_id *id;
194 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
195 80ddbec8 2018-04-29 stsp };
196 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
197 80ddbec8 2018-04-29 stsp
198 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
199 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
200 899d86c2 2018-05-10 stsp struct got_object_id *id)
201 80ddbec8 2018-04-29 stsp {
202 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
203 80ddbec8 2018-04-29 stsp
204 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
205 80ddbec8 2018-04-29 stsp if (entry == NULL)
206 899d86c2 2018-05-10 stsp return NULL;
207 99db9666 2018-05-07 stsp
208 899d86c2 2018-05-10 stsp entry->id = id;
209 99db9666 2018-05-07 stsp entry->commit = commit;
210 899d86c2 2018-05-10 stsp return entry;
211 99db9666 2018-05-07 stsp }
212 80ddbec8 2018-04-29 stsp
213 99db9666 2018-05-07 stsp static void
214 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
215 99db9666 2018-05-07 stsp {
216 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
217 99db9666 2018-05-07 stsp
218 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
219 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
220 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
221 99db9666 2018-05-07 stsp free(entry->id);
222 99db9666 2018-05-07 stsp free(entry);
223 99db9666 2018-05-07 stsp }
224 99db9666 2018-05-07 stsp
225 99db9666 2018-05-07 stsp static void
226 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
227 99db9666 2018-05-07 stsp {
228 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
229 99db9666 2018-05-07 stsp pop_commit(commits);
230 c4972b91 2018-05-07 stsp }
231 c4972b91 2018-05-07 stsp
232 c4972b91 2018-05-07 stsp
233 c4972b91 2018-05-07 stsp static const struct got_error *
234 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
235 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
236 c4972b91 2018-05-07 stsp {
237 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
238 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
239 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
240 899d86c2 2018-05-10 stsp struct got_object_id *id;
241 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
242 c4972b91 2018-05-07 stsp
243 899d86c2 2018-05-10 stsp *pentry = NULL;
244 c4972b91 2018-05-07 stsp
245 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
246 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
247 c4972b91 2018-05-07 stsp if (pid == NULL)
248 c4972b91 2018-05-07 stsp return NULL;
249 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
250 c4972b91 2018-05-07 stsp if (err)
251 c4972b91 2018-05-07 stsp return err;
252 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
254 c4972b91 2018-05-07 stsp got_object_close(obj);
255 c4972b91 2018-05-07 stsp return err;
256 c4972b91 2018-05-07 stsp }
257 c4972b91 2018-05-07 stsp
258 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
259 c4972b91 2018-05-07 stsp got_object_close(obj);
260 c4972b91 2018-05-07 stsp if (err)
261 c4972b91 2018-05-07 stsp return err;
262 c4972b91 2018-05-07 stsp
263 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
264 899d86c2 2018-05-10 stsp if (id == NULL) {
265 c4972b91 2018-05-07 stsp err = got_error_from_errno();
266 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
267 899d86c2 2018-05-10 stsp return err;;
268 c4972b91 2018-05-07 stsp }
269 899d86c2 2018-05-10 stsp
270 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
271 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
272 c4972b91 2018-05-07 stsp err = got_error_from_errno();
273 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
274 c4972b91 2018-05-07 stsp }
275 c4972b91 2018-05-07 stsp
276 899d86c2 2018-05-10 stsp return err;;
277 899d86c2 2018-05-10 stsp }
278 899d86c2 2018-05-10 stsp
279 899d86c2 2018-05-10 stsp static const struct got_error *
280 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
281 899d86c2 2018-05-10 stsp {
282 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
283 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
284 899d86c2 2018-05-10 stsp
285 899d86c2 2018-05-10 stsp *head_id = NULL;
286 899d86c2 2018-05-10 stsp
287 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
288 899d86c2 2018-05-10 stsp if (err)
289 899d86c2 2018-05-10 stsp return err;
290 899d86c2 2018-05-10 stsp
291 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
292 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
293 899d86c2 2018-05-10 stsp if (err) {
294 899d86c2 2018-05-10 stsp *head_id = NULL;
295 899d86c2 2018-05-10 stsp return err;
296 899d86c2 2018-05-10 stsp }
297 899d86c2 2018-05-10 stsp
298 c4972b91 2018-05-07 stsp return NULL;
299 99db9666 2018-05-07 stsp }
300 99db9666 2018-05-07 stsp
301 99db9666 2018-05-07 stsp static const struct got_error *
302 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
303 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
304 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
305 899d86c2 2018-05-10 stsp {
306 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
307 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
308 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
309 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
310 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
311 899d86c2 2018-05-10 stsp
312 899d86c2 2018-05-10 stsp *ncommits = 0;
313 899d86c2 2018-05-10 stsp
314 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
315 899d86c2 2018-05-10 stsp if (err)
316 899d86c2 2018-05-10 stsp goto done;
317 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
319 899d86c2 2018-05-10 stsp goto done;
320 899d86c2 2018-05-10 stsp }
321 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
322 899d86c2 2018-05-10 stsp if (err)
323 899d86c2 2018-05-10 stsp goto done;
324 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
326 899d86c2 2018-05-10 stsp goto done;
327 899d86c2 2018-05-10 stsp }
328 899d86c2 2018-05-10 stsp
329 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
330 899d86c2 2018-05-10 stsp if (err)
331 899d86c2 2018-05-10 stsp goto done;
332 899d86c2 2018-05-10 stsp
333 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
334 899d86c2 2018-05-10 stsp if (id == NULL) {
335 899d86c2 2018-05-10 stsp err = got_error_from_errno();
336 899d86c2 2018-05-10 stsp goto done;
337 899d86c2 2018-05-10 stsp }
338 899d86c2 2018-05-10 stsp
339 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
340 899d86c2 2018-05-10 stsp if (entry == NULL)
341 899d86c2 2018-05-10 stsp return got_error_from_errno();
342 899d86c2 2018-05-10 stsp
343 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
344 899d86c2 2018-05-10 stsp if (old_head_entry)
345 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
346 899d86c2 2018-05-10 stsp else
347 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
348 899d86c2 2018-05-10 stsp
349 899d86c2 2018-05-10 stsp *ncommits = 1;
350 899d86c2 2018-05-10 stsp
351 899d86c2 2018-05-10 stsp /*
352 899d86c2 2018-05-10 stsp * Fetch parent commits.
353 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
354 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
355 899d86c2 2018-05-10 stsp */
356 899d86c2 2018-05-10 stsp while (1) {
357 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
358 899d86c2 2018-05-10 stsp
359 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
360 899d86c2 2018-05-10 stsp if (err)
361 899d86c2 2018-05-10 stsp goto done;
362 899d86c2 2018-05-10 stsp if (pentry == NULL)
363 899d86c2 2018-05-10 stsp break;
364 899d86c2 2018-05-10 stsp
365 899d86c2 2018-05-10 stsp /*
366 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
367 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
368 899d86c2 2018-05-10 stsp */
369 899d86c2 2018-05-10 stsp if (old_head_entry &&
370 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
371 899d86c2 2018-05-10 stsp break;
372 899d86c2 2018-05-10 stsp
373 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
374 899d86c2 2018-05-10 stsp (*ncommits)++;
375 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
376 899d86c2 2018-05-10 stsp break;
377 899d86c2 2018-05-10 stsp
378 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
379 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
380 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
381 899d86c2 2018-05-10 stsp break;
382 899d86c2 2018-05-10 stsp
383 899d86c2 2018-05-10 stsp entry = pentry;
384 899d86c2 2018-05-10 stsp }
385 899d86c2 2018-05-10 stsp
386 899d86c2 2018-05-10 stsp done:
387 899d86c2 2018-05-10 stsp if (first_obj)
388 899d86c2 2018-05-10 stsp got_object_close(first_obj);
389 899d86c2 2018-05-10 stsp if (last_obj)
390 899d86c2 2018-05-10 stsp got_object_close(last_obj);
391 899d86c2 2018-05-10 stsp return err;
392 899d86c2 2018-05-10 stsp }
393 899d86c2 2018-05-10 stsp
394 899d86c2 2018-05-10 stsp static const struct got_error *
395 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
396 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
397 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
398 99db9666 2018-05-07 stsp {
399 99db9666 2018-05-07 stsp const struct got_error *err;
400 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
401 99db9666 2018-05-07 stsp int ncommits = 0;
402 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
403 99db9666 2018-05-07 stsp
404 899d86c2 2018-05-10 stsp *start_entry = NULL;
405 899d86c2 2018-05-10 stsp
406 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
407 99db9666 2018-05-07 stsp if (err)
408 99db9666 2018-05-07 stsp return err;
409 99db9666 2018-05-07 stsp
410 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
411 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
412 899d86c2 2018-05-10 stsp repo);
413 99db9666 2018-05-07 stsp if (err)
414 99db9666 2018-05-07 stsp return err;
415 99db9666 2018-05-07 stsp
416 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
417 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
418 899d86c2 2018-05-10 stsp else
419 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
420 899d86c2 2018-05-10 stsp
421 899d86c2 2018-05-10 stsp if (ncommits >= limit)
422 899d86c2 2018-05-10 stsp return NULL;
423 899d86c2 2018-05-10 stsp
424 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
425 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
426 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
427 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
428 899d86c2 2018-05-10 stsp
429 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
430 80ddbec8 2018-04-29 stsp if (err)
431 80ddbec8 2018-04-29 stsp break;
432 899d86c2 2018-05-10 stsp if (pentry)
433 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
434 899d86c2 2018-05-10 stsp entry = pentry;
435 0553a4e3 2018-04-30 stsp ncommits++;
436 0553a4e3 2018-04-30 stsp }
437 80ddbec8 2018-04-29 stsp
438 899d86c2 2018-05-10 stsp if (err)
439 899d86c2 2018-05-10 stsp *start_entry = NULL;
440 0553a4e3 2018-04-30 stsp return err;
441 0553a4e3 2018-04-30 stsp }
442 0553a4e3 2018-04-30 stsp
443 0553a4e3 2018-04-30 stsp static const struct got_error *
444 4a7f7875 2018-05-10 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
445 4a7f7875 2018-05-10 stsp int selected, int limit)
446 0553a4e3 2018-04-30 stsp {
447 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
448 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
449 0553a4e3 2018-04-30 stsp int ncommits = 0;
450 0553a4e3 2018-04-30 stsp
451 0553a4e3 2018-04-30 stsp wclear(tog_log_view.window);
452 0553a4e3 2018-04-30 stsp
453 899d86c2 2018-05-10 stsp entry = first;
454 899d86c2 2018-05-10 stsp *last = first;
455 899d86c2 2018-05-10 stsp while (entry) {
456 899d86c2 2018-05-10 stsp if (ncommits == limit)
457 899d86c2 2018-05-10 stsp break;
458 0553a4e3 2018-04-30 stsp if (ncommits == selected)
459 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
460 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
461 0553a4e3 2018-04-30 stsp if (ncommits == selected)
462 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
463 0553a4e3 2018-04-30 stsp if (err)
464 0553a4e3 2018-04-30 stsp break;
465 0553a4e3 2018-04-30 stsp ncommits++;
466 899d86c2 2018-05-10 stsp *last = entry;
467 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
468 80ddbec8 2018-04-29 stsp }
469 80ddbec8 2018-04-29 stsp
470 80ddbec8 2018-04-29 stsp update_panels();
471 80ddbec8 2018-04-29 stsp doupdate();
472 0553a4e3 2018-04-30 stsp
473 80ddbec8 2018-04-29 stsp return err;
474 9f7d7167 2018-04-29 stsp }
475 07b55e75 2018-05-10 stsp
476 07b55e75 2018-05-10 stsp static void
477 07b55e75 2018-05-10 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
478 07b55e75 2018-05-10 stsp struct commit_queue *commits)
479 07b55e75 2018-05-10 stsp {
480 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
481 07b55e75 2018-05-10 stsp int nscrolled = 0;
482 07b55e75 2018-05-10 stsp
483 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
484 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
485 07b55e75 2018-05-10 stsp return;
486 9f7d7167 2018-04-29 stsp
487 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
488 07b55e75 2018-05-10 stsp while (entry && nscrolled < n) {
489 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
490 07b55e75 2018-05-10 stsp if (entry) {
491 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
492 07b55e75 2018-05-10 stsp nscrolled++;
493 07b55e75 2018-05-10 stsp }
494 07b55e75 2018-05-10 stsp }
495 aa075928 2018-05-10 stsp }
496 aa075928 2018-05-10 stsp
497 aa075928 2018-05-10 stsp static const struct got_error *
498 aa075928 2018-05-10 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
499 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
500 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
501 aa075928 2018-05-10 stsp {
502 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
503 aa075928 2018-05-10 stsp struct commit_queue_entry *entry;
504 aa075928 2018-05-10 stsp int nscrolled = 0;
505 aa075928 2018-05-10 stsp
506 aa075928 2018-05-10 stsp if (last_displayed_entry->commit->nparents == 0)
507 aa075928 2018-05-10 stsp return NULL;
508 aa075928 2018-05-10 stsp
509 aa075928 2018-05-10 stsp entry = *first_displayed_entry;
510 aa075928 2018-05-10 stsp do {
511 aa075928 2018-05-10 stsp struct commit_queue_entry *pentry;
512 aa075928 2018-05-10 stsp
513 aa075928 2018-05-10 stsp pentry = TAILQ_NEXT(entry, entry);
514 aa075928 2018-05-10 stsp if (pentry == NULL) {
515 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
516 aa075928 2018-05-10 stsp if (err)
517 aa075928 2018-05-10 stsp break;
518 aa075928 2018-05-10 stsp if (pentry == NULL) {
519 aa075928 2018-05-10 stsp *first_displayed_entry = entry;
520 aa075928 2018-05-10 stsp return NULL;
521 aa075928 2018-05-10 stsp }
522 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
523 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
524 aa075928 2018-05-10 stsp }
525 aa075928 2018-05-10 stsp
526 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
527 aa075928 2018-05-10 stsp entry = pentry;
528 aa075928 2018-05-10 stsp
529 aa075928 2018-05-10 stsp if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
530 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, last_displayed_entry,
531 aa075928 2018-05-10 stsp repo);
532 aa075928 2018-05-10 stsp if (err)
533 aa075928 2018-05-10 stsp break;
534 aa075928 2018-05-10 stsp if (pentry) {
535 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
536 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
537 aa075928 2018-05-10 stsp }
538 aa075928 2018-05-10 stsp }
539 aa075928 2018-05-10 stsp } while (++nscrolled < n);
540 aa075928 2018-05-10 stsp
541 aa075928 2018-05-10 stsp return NULL;
542 07b55e75 2018-05-10 stsp }
543 4a7f7875 2018-05-10 stsp
544 4a7f7875 2018-05-10 stsp static int
545 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
546 4a7f7875 2018-05-10 stsp {
547 4a7f7875 2018-05-10 stsp int nparents = 0;
548 07b55e75 2018-05-10 stsp
549 4a7f7875 2018-05-10 stsp while (entry) {
550 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
551 4a7f7875 2018-05-10 stsp nparents++;
552 4a7f7875 2018-05-10 stsp }
553 4a7f7875 2018-05-10 stsp
554 4a7f7875 2018-05-10 stsp return nparents;
555 4a7f7875 2018-05-10 stsp }
556 4a7f7875 2018-05-10 stsp
557 80ddbec8 2018-04-29 stsp static const struct got_error *
558 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
559 80ddbec8 2018-04-29 stsp {
560 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
561 c4972b91 2018-05-07 stsp struct got_object_id *id;
562 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
563 0553a4e3 2018-04-30 stsp struct commit_queue commits;
564 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
565 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
566 80ddbec8 2018-04-29 stsp
567 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
568 c4972b91 2018-05-07 stsp if (id == NULL)
569 c4972b91 2018-05-07 stsp return got_error_from_errno();
570 c4972b91 2018-05-07 stsp
571 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
572 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
573 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
574 80ddbec8 2018-04-29 stsp return got_error_from_errno();
575 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
576 80ddbec8 2018-04-29 stsp }
577 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
578 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
579 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
580 80ddbec8 2018-04-29 stsp return got_error_from_errno();
581 80ddbec8 2018-04-29 stsp }
582 80ddbec8 2018-04-29 stsp
583 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
584 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
585 80ddbec8 2018-04-29 stsp if (err)
586 80ddbec8 2018-04-29 stsp goto done;
587 899d86c2 2018-05-10 stsp while (!done) {
588 899d86c2 2018-05-10 stsp err = draw_commits(&last_displayed_entry, first_displayed_entry,
589 899d86c2 2018-05-10 stsp selected, LINES);
590 80ddbec8 2018-04-29 stsp if (err)
591 d0f709cb 2018-04-30 stsp goto done;
592 80ddbec8 2018-04-29 stsp
593 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
594 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
595 80ddbec8 2018-04-29 stsp switch (ch) {
596 31120ada 2018-04-30 stsp case ERR:
597 31120ada 2018-04-30 stsp if (errno) {
598 31120ada 2018-04-30 stsp err = got_error_from_errno();
599 31120ada 2018-04-30 stsp goto done;
600 31120ada 2018-04-30 stsp }
601 31120ada 2018-04-30 stsp break;
602 80ddbec8 2018-04-29 stsp case 'q':
603 80ddbec8 2018-04-29 stsp done = 1;
604 80ddbec8 2018-04-29 stsp break;
605 80ddbec8 2018-04-29 stsp case 'k':
606 80ddbec8 2018-04-29 stsp case KEY_UP:
607 80ddbec8 2018-04-29 stsp if (selected > 0)
608 80ddbec8 2018-04-29 stsp selected--;
609 8ec9698d 2018-05-10 stsp if (selected > 0)
610 d91e25cb 2018-05-10 stsp break;
611 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
612 80ddbec8 2018-04-29 stsp break;
613 48531068 2018-05-10 stsp case KEY_PPAGE:
614 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
615 dfc1d240 2018-05-10 stsp first_displayed_entry) {
616 dfc1d240 2018-05-10 stsp selected = 0;
617 dfc1d240 2018-05-10 stsp break;
618 dfc1d240 2018-05-10 stsp }
619 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
620 48531068 2018-05-10 stsp &commits);
621 48531068 2018-05-10 stsp break;
622 80ddbec8 2018-04-29 stsp case 'j':
623 80ddbec8 2018-04-29 stsp case KEY_DOWN:
624 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
625 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
626 06abe2cd 2018-05-10 stsp selected < nparents - 1)
627 80ddbec8 2018-04-29 stsp selected++;
628 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
629 06abe2cd 2018-05-10 stsp selected < nparents - 1)
630 c4972b91 2018-05-07 stsp break;
631 aa075928 2018-05-10 stsp err = scroll_down(&first_displayed_entry, 1,
632 aa075928 2018-05-10 stsp last_displayed_entry, &commits, repo);
633 4a7f7875 2018-05-10 stsp if (err)
634 4a7f7875 2018-05-10 stsp goto done;
635 4a7f7875 2018-05-10 stsp break;
636 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
637 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
638 dfc1d240 2018-05-10 stsp if (nparents < LINES - 1 &&
639 dfc1d240 2018-05-10 stsp selected < nparents - 1) {
640 dfc1d240 2018-05-10 stsp selected = nparents - 1;
641 dfc1d240 2018-05-10 stsp break;
642 dfc1d240 2018-05-10 stsp }
643 4a7f7875 2018-05-10 stsp err = scroll_down(&first_displayed_entry,
644 4a7f7875 2018-05-10 stsp MIN(nparents, LINES), last_displayed_entry,
645 4a7f7875 2018-05-10 stsp &commits, repo);
646 899d86c2 2018-05-10 stsp if (err)
647 aa075928 2018-05-10 stsp goto done;
648 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
649 dfc1d240 2018-05-10 stsp if (selected > nparents)
650 dfc1d240 2018-05-10 stsp selected = nparents - 1;
651 80ddbec8 2018-04-29 stsp break;
652 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
653 31120ada 2018-04-30 stsp if (selected > LINES)
654 31120ada 2018-04-30 stsp selected = LINES - 1;
655 31120ada 2018-04-30 stsp break;
656 80ddbec8 2018-04-29 stsp default:
657 80ddbec8 2018-04-29 stsp break;
658 80ddbec8 2018-04-29 stsp }
659 80ddbec8 2018-04-29 stsp nodelay(stdscr, TRUE);
660 899d86c2 2018-05-10 stsp }
661 80ddbec8 2018-04-29 stsp done:
662 0553a4e3 2018-04-30 stsp free_commits(&commits);
663 80ddbec8 2018-04-29 stsp return err;
664 80ddbec8 2018-04-29 stsp }
665 80ddbec8 2018-04-29 stsp
666 9f7d7167 2018-04-29 stsp const struct got_error *
667 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
668 9f7d7167 2018-04-29 stsp {
669 80ddbec8 2018-04-29 stsp const struct got_error *error;
670 80ddbec8 2018-04-29 stsp struct got_repository *repo;
671 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
672 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
673 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
674 80ddbec8 2018-04-29 stsp int ch;
675 80ddbec8 2018-04-29 stsp
676 80ddbec8 2018-04-29 stsp #ifndef PROFILE
677 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
678 80ddbec8 2018-04-29 stsp err(1, "pledge");
679 80ddbec8 2018-04-29 stsp #endif
680 80ddbec8 2018-04-29 stsp
681 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
682 80ddbec8 2018-04-29 stsp switch (ch) {
683 80ddbec8 2018-04-29 stsp case 'c':
684 80ddbec8 2018-04-29 stsp start_commit = optarg;
685 80ddbec8 2018-04-29 stsp break;
686 80ddbec8 2018-04-29 stsp default:
687 80ddbec8 2018-04-29 stsp usage();
688 80ddbec8 2018-04-29 stsp /* NOTREACHED */
689 80ddbec8 2018-04-29 stsp }
690 80ddbec8 2018-04-29 stsp }
691 80ddbec8 2018-04-29 stsp
692 80ddbec8 2018-04-29 stsp argc -= optind;
693 80ddbec8 2018-04-29 stsp argv += optind;
694 80ddbec8 2018-04-29 stsp
695 80ddbec8 2018-04-29 stsp if (argc == 0) {
696 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
697 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
698 80ddbec8 2018-04-29 stsp return got_error_from_errno();
699 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
700 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
701 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
702 80ddbec8 2018-04-29 stsp return got_error_from_errno();
703 80ddbec8 2018-04-29 stsp } else
704 80ddbec8 2018-04-29 stsp usage_log();
705 80ddbec8 2018-04-29 stsp
706 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
707 80ddbec8 2018-04-29 stsp free(repo_path);
708 80ddbec8 2018-04-29 stsp if (error != NULL)
709 80ddbec8 2018-04-29 stsp return error;
710 80ddbec8 2018-04-29 stsp
711 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
712 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
713 80ddbec8 2018-04-29 stsp if (error != NULL)
714 80ddbec8 2018-04-29 stsp return error;
715 80ddbec8 2018-04-29 stsp } else {
716 80ddbec8 2018-04-29 stsp struct got_object *obj;
717 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
718 80ddbec8 2018-04-29 stsp if (error == NULL) {
719 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
720 899d86c2 2018-05-10 stsp if (start_id == NULL)
721 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
722 80ddbec8 2018-04-29 stsp }
723 80ddbec8 2018-04-29 stsp }
724 80ddbec8 2018-04-29 stsp if (error != NULL)
725 80ddbec8 2018-04-29 stsp return error;
726 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
727 899d86c2 2018-05-10 stsp free(start_id);
728 80ddbec8 2018-04-29 stsp got_repo_close(repo);
729 80ddbec8 2018-04-29 stsp return error;
730 9f7d7167 2018-04-29 stsp }
731 9f7d7167 2018-04-29 stsp
732 9f7d7167 2018-04-29 stsp __dead void
733 9f7d7167 2018-04-29 stsp usage_diff(void)
734 9f7d7167 2018-04-29 stsp {
735 80ddbec8 2018-04-29 stsp endwin();
736 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
737 9f7d7167 2018-04-29 stsp getprogname());
738 9f7d7167 2018-04-29 stsp exit(1);
739 9f7d7167 2018-04-29 stsp }
740 9f7d7167 2018-04-29 stsp
741 9f7d7167 2018-04-29 stsp const struct got_error *
742 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
743 9f7d7167 2018-04-29 stsp {
744 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
745 9f7d7167 2018-04-29 stsp }
746 9f7d7167 2018-04-29 stsp
747 9f7d7167 2018-04-29 stsp __dead void
748 9f7d7167 2018-04-29 stsp usage_blame(void)
749 9f7d7167 2018-04-29 stsp {
750 80ddbec8 2018-04-29 stsp endwin();
751 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
752 9f7d7167 2018-04-29 stsp getprogname());
753 9f7d7167 2018-04-29 stsp exit(1);
754 9f7d7167 2018-04-29 stsp }
755 9f7d7167 2018-04-29 stsp
756 9f7d7167 2018-04-29 stsp const struct got_error *
757 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
758 9f7d7167 2018-04-29 stsp {
759 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
760 9f7d7167 2018-04-29 stsp }
761 9f7d7167 2018-04-29 stsp
762 9f7d7167 2018-04-29 stsp static const struct got_error *
763 9f7d7167 2018-04-29 stsp init_curses(void)
764 9f7d7167 2018-04-29 stsp {
765 9f7d7167 2018-04-29 stsp initscr();
766 9f7d7167 2018-04-29 stsp cbreak();
767 9f7d7167 2018-04-29 stsp noecho();
768 9f7d7167 2018-04-29 stsp nonl();
769 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
770 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
771 1f475ad8 2018-05-10 stsp curs_set(0);
772 9f7d7167 2018-04-29 stsp
773 9f7d7167 2018-04-29 stsp tog_main_win = newwin(0, 0, 0, 0);
774 9f7d7167 2018-04-29 stsp if (tog_main_win == NULL)
775 9f7d7167 2018-04-29 stsp return got_error_from_errno();
776 9f7d7167 2018-04-29 stsp tog_main_panel = new_panel(tog_main_win);
777 9f7d7167 2018-04-29 stsp if (tog_main_panel == NULL)
778 9f7d7167 2018-04-29 stsp return got_error_from_errno();
779 9f7d7167 2018-04-29 stsp
780 9f7d7167 2018-04-29 stsp return NULL;
781 9f7d7167 2018-04-29 stsp }
782 9f7d7167 2018-04-29 stsp
783 9f7d7167 2018-04-29 stsp __dead void
784 9f7d7167 2018-04-29 stsp usage(void)
785 9f7d7167 2018-04-29 stsp {
786 9f7d7167 2018-04-29 stsp int i;
787 9f7d7167 2018-04-29 stsp
788 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
789 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
790 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
791 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
792 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
793 9f7d7167 2018-04-29 stsp }
794 9f7d7167 2018-04-29 stsp exit(1);
795 9f7d7167 2018-04-29 stsp }
796 9f7d7167 2018-04-29 stsp
797 c2301be8 2018-04-30 stsp static char **
798 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
799 c2301be8 2018-04-30 stsp {
800 c2301be8 2018-04-30 stsp char **argv;
801 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
802 c2301be8 2018-04-30 stsp
803 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
804 c2301be8 2018-04-30 stsp if (argv == NULL)
805 c2301be8 2018-04-30 stsp err(1, "calloc");
806 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
807 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
808 c2301be8 2018-04-30 stsp err(1, "calloc");
809 c2301be8 2018-04-30 stsp if (arg1) {
810 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
811 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
812 c2301be8 2018-04-30 stsp err(1, "calloc");
813 c2301be8 2018-04-30 stsp }
814 c2301be8 2018-04-30 stsp
815 c2301be8 2018-04-30 stsp return argv;
816 c2301be8 2018-04-30 stsp }
817 c2301be8 2018-04-30 stsp
818 9f7d7167 2018-04-29 stsp int
819 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
820 9f7d7167 2018-04-29 stsp {
821 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
822 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
823 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
824 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
825 9f7d7167 2018-04-29 stsp
826 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
827 9f7d7167 2018-04-29 stsp
828 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
829 9f7d7167 2018-04-29 stsp switch (ch) {
830 9f7d7167 2018-04-29 stsp case 'h':
831 9f7d7167 2018-04-29 stsp hflag = 1;
832 9f7d7167 2018-04-29 stsp break;
833 9f7d7167 2018-04-29 stsp default:
834 9f7d7167 2018-04-29 stsp usage();
835 9f7d7167 2018-04-29 stsp /* NOTREACHED */
836 9f7d7167 2018-04-29 stsp }
837 9f7d7167 2018-04-29 stsp }
838 9f7d7167 2018-04-29 stsp
839 9f7d7167 2018-04-29 stsp argc -= optind;
840 9f7d7167 2018-04-29 stsp argv += optind;
841 9f7d7167 2018-04-29 stsp optind = 0;
842 c2301be8 2018-04-30 stsp optreset = 1;
843 9f7d7167 2018-04-29 stsp
844 c2301be8 2018-04-30 stsp if (argc == 0) {
845 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
846 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
847 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
848 c2301be8 2018-04-30 stsp argc = 1;
849 c2301be8 2018-04-30 stsp } else {
850 9f7d7167 2018-04-29 stsp int i;
851 9f7d7167 2018-04-29 stsp
852 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
853 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
854 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
855 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
856 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
857 9f7d7167 2018-04-29 stsp if (hflag)
858 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
859 9f7d7167 2018-04-29 stsp break;
860 9f7d7167 2018-04-29 stsp }
861 9f7d7167 2018-04-29 stsp }
862 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
863 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
864 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
865 c2301be8 2018-04-30 stsp if (repo_path) {
866 c2301be8 2018-04-30 stsp struct got_repository *repo;
867 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
868 c2301be8 2018-04-30 stsp if (error == NULL)
869 c2301be8 2018-04-30 stsp got_repo_close(repo);
870 c2301be8 2018-04-30 stsp } else
871 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
872 c2301be8 2018-04-30 stsp if (error) {
873 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
874 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
875 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
876 ad7de8d9 2018-04-30 stsp free(repo_path);
877 c2301be8 2018-04-30 stsp return 1;
878 c2301be8 2018-04-30 stsp }
879 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
880 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
881 c2301be8 2018-04-30 stsp argc = 2;
882 c2301be8 2018-04-30 stsp free(repo_path);
883 9f7d7167 2018-04-29 stsp }
884 9f7d7167 2018-04-29 stsp }
885 9f7d7167 2018-04-29 stsp
886 9f7d7167 2018-04-29 stsp error = init_curses();
887 9f7d7167 2018-04-29 stsp if (error) {
888 80ddbec8 2018-04-29 stsp fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
889 9f7d7167 2018-04-29 stsp return 1;
890 9f7d7167 2018-04-29 stsp }
891 9f7d7167 2018-04-29 stsp
892 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
893 9f7d7167 2018-04-29 stsp if (error)
894 9f7d7167 2018-04-29 stsp goto done;
895 9f7d7167 2018-04-29 stsp done:
896 9f7d7167 2018-04-29 stsp endwin();
897 c2301be8 2018-04-30 stsp free(cmd_argv);
898 9f7d7167 2018-04-29 stsp if (error)
899 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
900 9f7d7167 2018-04-29 stsp return 0;
901 9f7d7167 2018-04-29 stsp }