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