2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
85 fed328cc 2018-05-20 stsp static struct tog_view {
86 26ed57b2 2018-05-19 stsp WINDOW *window;
87 26ed57b2 2018-05-19 stsp PANEL *panel;
88 ffd1d5e5 2018-06-23 stsp } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
90 cd0acaa7 2018-05-20 stsp static const struct got_error *
91 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
92 cd0acaa7 2018-05-20 stsp struct got_repository *);
93 cd0acaa7 2018-05-20 stsp static const struct got_error *
94 ecb28ae0 2018-07-16 stsp show_log_view(struct got_object_id *, struct got_repository *, const char *);
95 a70480e0 2018-06-23 stsp static const struct got_error *
96 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
97 ffd1d5e5 2018-06-23 stsp static const struct got_error *
98 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *, struct got_object_id *,
99 ffd1d5e5 2018-06-23 stsp struct got_repository *);
101 4ed7e80c 2018-05-20 stsp __dead static void
102 9f7d7167 2018-04-29 stsp usage_log(void)
105 ecb28ae0 2018-07-16 stsp fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
106 9f7d7167 2018-04-29 stsp getprogname());
110 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
111 80ddbec8 2018-04-29 stsp static const struct got_error *
112 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
114 00dfcb92 2018-06-11 stsp char *vis = NULL;
115 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
117 963b370f 2018-05-20 stsp *ws = NULL;
118 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
119 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
120 00dfcb92 2018-06-11 stsp int vislen;
121 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
122 00dfcb92 2018-06-11 stsp return got_error_from_errno();
124 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
125 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
127 00dfcb92 2018-06-11 stsp return err;
128 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
129 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
130 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
135 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
136 a7f50699 2018-06-11 stsp if (*ws == NULL) {
137 a7f50699 2018-06-11 stsp err = got_error_from_errno();
141 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
142 963b370f 2018-05-20 stsp err = got_error_from_errno();
147 963b370f 2018-05-20 stsp *ws = NULL;
150 963b370f 2018-05-20 stsp return err;
153 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
154 963b370f 2018-05-20 stsp static const struct got_error *
155 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
157 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
158 963b370f 2018-05-20 stsp int cols = 0;
159 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
160 963b370f 2018-05-20 stsp size_t wlen;
163 963b370f 2018-05-20 stsp *wlinep = NULL;
164 b700b5d6 2018-07-10 stsp *widthp = 0;
166 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
168 963b370f 2018-05-20 stsp return err;
171 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
172 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
173 963b370f 2018-05-20 stsp switch (width) {
179 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
180 b700b5d6 2018-07-10 stsp cols += width;
185 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
186 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
190 963b370f 2018-05-20 stsp err = got_error_from_errno();
194 963b370f 2018-05-20 stsp wline[i] = L'\0';
195 b700b5d6 2018-07-10 stsp if (widthp)
196 b700b5d6 2018-07-10 stsp *widthp = cols;
199 963b370f 2018-05-20 stsp free(wline);
201 963b370f 2018-05-20 stsp *wlinep = wline;
202 963b370f 2018-05-20 stsp return err;
205 963b370f 2018-05-20 stsp static const struct got_error *
206 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
208 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
209 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
210 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
211 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
212 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
213 bb737323 2018-05-20 stsp int author_width, logmsg_width;
214 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
215 80ddbec8 2018-04-29 stsp char *line = NULL;
216 bb737323 2018-05-20 stsp int col, limit;
217 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
218 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
219 9c2eaf34 2018-05-20 stsp const int avail = COLS;
221 b39d25c7 2018-07-10 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
222 b39d25c7 2018-07-10 stsp >= sizeof(datebuf))
223 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
225 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
226 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
228 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 b39d25c7 2018-07-10 stsp waddnstr(tog_log_view.window, datebuf, limit);
230 b39d25c7 2018-07-10 stsp col = limit + 1;
231 b39d25c7 2018-07-10 stsp if (col > avail)
234 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
235 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
236 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
239 6d9fbc00 2018-04-29 stsp author = author0;
240 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
241 6d9fbc00 2018-04-29 stsp if (smallerthan)
242 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
244 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
246 6d9fbc00 2018-04-29 stsp *at = '\0';
248 b39d25c7 2018-07-10 stsp limit = avail - col;
249 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
252 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
253 bb737323 2018-05-20 stsp col += author_width;
254 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
255 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
257 bb737323 2018-05-20 stsp author_width++;
259 9c2eaf34 2018-05-20 stsp if (col > avail)
262 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
263 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
264 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
267 bb737323 2018-05-20 stsp logmsg = logmsg0;
268 bb737323 2018-05-20 stsp while (*logmsg == '\n')
270 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
271 bb737323 2018-05-20 stsp if (newline)
272 bb737323 2018-05-20 stsp *newline = '\0';
273 bb737323 2018-05-20 stsp limit = avail - col;
274 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
277 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
278 bb737323 2018-05-20 stsp col += logmsg_width;
279 bb737323 2018-05-20 stsp while (col <= avail) {
280 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
284 80ddbec8 2018-04-29 stsp free(logmsg0);
285 bb737323 2018-05-20 stsp free(wlogmsg);
286 6d9fbc00 2018-04-29 stsp free(author0);
287 bb737323 2018-05-20 stsp free(wauthor);
288 80ddbec8 2018-04-29 stsp free(line);
289 80ddbec8 2018-04-29 stsp return err;
292 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
293 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
294 80ddbec8 2018-04-29 stsp struct got_object_id *id;
295 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
297 ecb28ae0 2018-07-16 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
298 ecb28ae0 2018-07-16 stsp struct commit_queue {
299 ecb28ae0 2018-07-16 stsp int ncommits;
300 ecb28ae0 2018-07-16 stsp struct commit_queue_head head;
303 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
304 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
305 899d86c2 2018-05-10 stsp struct got_object_id *id)
307 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
309 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
310 80ddbec8 2018-04-29 stsp if (entry == NULL)
311 899d86c2 2018-05-10 stsp return NULL;
313 899d86c2 2018-05-10 stsp entry->id = id;
314 99db9666 2018-05-07 stsp entry->commit = commit;
315 899d86c2 2018-05-10 stsp return entry;
318 99db9666 2018-05-07 stsp static void
319 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
321 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
323 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
324 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
325 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
326 ecb28ae0 2018-07-16 stsp commits->ncommits--;
327 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
328 99db9666 2018-05-07 stsp free(entry);
331 99db9666 2018-05-07 stsp static void
332 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
334 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
335 99db9666 2018-05-07 stsp pop_commit(commits);
338 c4972b91 2018-05-07 stsp static const struct got_error *
339 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
340 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
341 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
343 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
344 899d86c2 2018-05-10 stsp struct got_object_id *id;
345 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
346 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
348 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
350 c4972b91 2018-05-07 stsp return err;
352 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
353 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
354 9ba79e04 2018-06-11 stsp int nfetched;
356 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
357 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
358 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
359 9ba79e04 2018-06-11 stsp return err;
361 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
363 9ba79e04 2018-06-11 stsp return err;
366 9ba79e04 2018-06-11 stsp while (1) {
367 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
369 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
371 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
373 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
374 9ba79e04 2018-06-11 stsp err = NULL;
377 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
378 ecb28ae0 2018-07-16 stsp graph, 1, repo);
380 ecb28ae0 2018-07-16 stsp return err;
383 ecb28ae0 2018-07-16 stsp if (id == NULL)
386 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
390 ecb28ae0 2018-07-16 stsp if (path) {
391 ecb28ae0 2018-07-16 stsp struct got_object *obj;
392 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
393 ecb28ae0 2018-07-16 stsp int changed = 0;
395 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
397 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
398 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
399 ecb28ae0 2018-07-16 stsp /* History stops here. */
400 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
404 ecb28ae0 2018-07-16 stsp found_obj = 1;
406 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
407 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
408 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
409 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
410 ecb28ae0 2018-07-16 stsp pid->id, path);
412 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
413 ecb28ae0 2018-07-16 stsp got_object_close(obj);
416 ecb28ae0 2018-07-16 stsp err = NULL;
417 ecb28ae0 2018-07-16 stsp changed = 1;
419 ecb28ae0 2018-07-16 stsp changed = (got_object_id_cmp(
420 ecb28ae0 2018-07-16 stsp got_object_get_id(obj),
421 ecb28ae0 2018-07-16 stsp got_object_get_id(pobj)) != 0);
422 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
425 ecb28ae0 2018-07-16 stsp if (!changed) {
426 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
431 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
432 9ba79e04 2018-06-11 stsp if (entry == NULL) {
433 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
436 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
438 ecb28ae0 2018-07-16 stsp commits->ncommits++;
441 9ba79e04 2018-06-11 stsp return err;
444 99db9666 2018-05-07 stsp static const struct got_error *
445 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
446 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
447 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
448 ecb28ae0 2018-07-16 stsp const char *path)
450 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
452 9ba79e04 2018-06-11 stsp *pentry = NULL;
454 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
456 9ba79e04 2018-06-11 stsp return err;
458 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
459 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
460 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
461 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
463 9ba79e04 2018-06-11 stsp return NULL;
466 899d86c2 2018-05-10 stsp static const struct got_error *
467 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
469 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
470 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
472 9ba79e04 2018-06-11 stsp *head_id = NULL;
474 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
476 99db9666 2018-05-07 stsp return err;
478 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
479 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
481 9ba79e04 2018-06-11 stsp *head_id = NULL;
482 99db9666 2018-05-07 stsp return err;
485 9ba79e04 2018-06-11 stsp return NULL;
488 0553a4e3 2018-04-30 stsp static const struct got_error *
489 ecb28ae0 2018-07-16 stsp draw_commits(struct commit_queue_entry **last,
490 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
491 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
492 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
493 a7f40148 2018-07-18 stsp const char *path)
495 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
496 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
497 ecb28ae0 2018-07-16 stsp int ncommits, width;
498 867c6645 2018-07-10 stsp char *id_str, *header;
499 ecb28ae0 2018-07-16 stsp wchar_t *wline;
501 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
503 867c6645 2018-07-10 stsp return err;
505 ecb28ae0 2018-07-16 stsp if (path) {
506 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
507 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
508 ecb28ae0 2018-07-16 stsp free(id_str);
509 ecb28ae0 2018-07-16 stsp return err;
511 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
512 867c6645 2018-07-10 stsp err = got_error_from_errno();
513 867c6645 2018-07-10 stsp free(id_str);
514 867c6645 2018-07-10 stsp return err;
516 ecb28ae0 2018-07-16 stsp free(id_str);
517 ecb28ae0 2018-07-16 stsp err = format_line(&wline, &width, header, COLS);
519 ecb28ae0 2018-07-16 stsp free(header);
520 ecb28ae0 2018-07-16 stsp return err;
522 ecb28ae0 2018-07-16 stsp free(header);
524 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
526 ecb28ae0 2018-07-16 stsp waddwstr(tog_log_view.window, wline);
527 ecb28ae0 2018-07-16 stsp if (width < COLS)
528 ecb28ae0 2018-07-16 stsp waddch(tog_log_view.window, '\n');
529 ecb28ae0 2018-07-16 stsp free(wline);
530 ecb28ae0 2018-07-16 stsp if (limit <= 1)
531 ecb28ae0 2018-07-16 stsp return NULL;
533 899d86c2 2018-05-10 stsp entry = first;
534 899d86c2 2018-05-10 stsp *last = first;
535 867c6645 2018-07-10 stsp ncommits = 0;
536 899d86c2 2018-05-10 stsp while (entry) {
537 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
539 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
540 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
541 cd0acaa7 2018-05-20 stsp *selected = entry;
543 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
544 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
545 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
548 0553a4e3 2018-04-30 stsp ncommits++;
549 899d86c2 2018-05-10 stsp *last = entry;
550 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
551 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
552 a7f40148 2018-07-18 stsp 0, repo, path);
554 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
555 a7f40148 2018-07-18 stsp return err;
556 a7f40148 2018-07-18 stsp err = NULL;
559 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
562 80ddbec8 2018-04-29 stsp update_panels();
563 80ddbec8 2018-04-29 stsp doupdate();
565 80ddbec8 2018-04-29 stsp return err;
568 07b55e75 2018-05-10 stsp static void
569 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
570 07b55e75 2018-05-10 stsp struct commit_queue *commits)
572 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
573 07b55e75 2018-05-10 stsp int nscrolled = 0;
575 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
576 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
579 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
580 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
581 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
582 07b55e75 2018-05-10 stsp if (entry) {
583 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
584 07b55e75 2018-05-10 stsp nscrolled++;
589 aa075928 2018-05-10 stsp static const struct got_error *
590 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
591 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
592 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
593 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
595 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
596 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
597 aa075928 2018-05-10 stsp int nscrolled = 0;
600 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
601 aa075928 2018-05-10 stsp if (pentry == NULL) {
602 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
603 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
604 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
607 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
609 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
610 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
612 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
613 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
615 dd0a52c1 2018-05-20 stsp return err;
618 cd0acaa7 2018-05-20 stsp static const struct got_error *
619 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
621 cd0acaa7 2018-05-20 stsp const struct got_error *err;
622 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
623 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
625 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
627 cd0acaa7 2018-05-20 stsp return err;
629 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
630 9ba79e04 2018-06-11 stsp if (parent_id) {
631 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
636 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
639 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
641 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
642 cd0acaa7 2018-05-20 stsp return err;
645 80ddbec8 2018-04-29 stsp static const struct got_error *
646 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
648 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
649 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
651 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
653 9343a5fb 2018-06-23 stsp return err;
655 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
656 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
657 9343a5fb 2018-06-23 stsp return err;
660 9343a5fb 2018-06-23 stsp static const struct got_error *
661 ecb28ae0 2018-07-16 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo,
662 ecb28ae0 2018-07-16 stsp const char *path)
664 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
665 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
666 ecb28ae0 2018-07-16 stsp int ch, done = 0, selected = 0, nfetched;
667 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph = NULL;
668 0553a4e3 2018-04-30 stsp struct commit_queue commits;
669 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
670 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
671 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
672 ecb28ae0 2018-07-16 stsp struct commit_queue_entry *entry;
673 ecb28ae0 2018-07-16 stsp char *in_repo_path = NULL;
675 ecb28ae0 2018-07-16 stsp err = got_repo_map_path(&in_repo_path, repo, path);
676 ecb28ae0 2018-07-16 stsp if (err != NULL)
679 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
680 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
681 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
682 80ddbec8 2018-04-29 stsp return got_error_from_errno();
683 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
685 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
686 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
687 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
688 80ddbec8 2018-04-29 stsp return got_error_from_errno();
690 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
692 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
694 9ba79e04 2018-06-11 stsp return err;
696 ecb28ae0 2018-07-16 stsp TAILQ_INIT(&commits.head);
697 ecb28ae0 2018-07-16 stsp commits.ncommits = 0;
699 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
703 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
704 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
710 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
711 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
712 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
713 9ba79e04 2018-06-11 stsp * updating the display.
715 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, &commits, head_id, LINES, 1, repo,
716 ecb28ae0 2018-07-16 stsp in_repo_path);
718 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
720 55198a88 2018-07-16 stsp err = NULL;
724 ecb28ae0 2018-07-16 stsp * Find entry corresponding to the first commit to display.
725 ecb28ae0 2018-07-16 stsp * if both a path and start commit was specified, the first commit
726 ecb28ae0 2018-07-16 stsp * shown should be a commit <= start_commit which modified the path.
728 ecb28ae0 2018-07-16 stsp if (in_repo_path) {
729 ecb28ae0 2018-07-16 stsp struct got_object_id *id;
731 ecb28ae0 2018-07-16 stsp err = got_commit_graph_iter_start(graph, start_id);
733 ecb28ae0 2018-07-16 stsp return err;
735 ecb28ae0 2018-07-16 stsp err = got_commit_graph_iter_next(&id, graph);
738 ecb28ae0 2018-07-16 stsp if (id == NULL) {
739 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_NO_OBJ);
743 ecb28ae0 2018-07-16 stsp * The graph contains all commits. The commit queue
744 ecb28ae0 2018-07-16 stsp * contains a subset of commits filtered by path.
746 ecb28ae0 2018-07-16 stsp TAILQ_FOREACH(entry, &commits.head, entry) {
747 ecb28ae0 2018-07-16 stsp if (got_object_id_cmp(entry->id, id) == 0) {
748 ecb28ae0 2018-07-16 stsp first_displayed_entry = entry;
752 ecb28ae0 2018-07-16 stsp } while (first_displayed_entry == NULL);
754 ecb28ae0 2018-07-16 stsp TAILQ_FOREACH(entry, &commits.head, entry) {
755 ecb28ae0 2018-07-16 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
756 ecb28ae0 2018-07-16 stsp first_displayed_entry = entry;
761 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
762 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
766 867c6645 2018-07-10 stsp selected_entry = first_displayed_entry;
767 899d86c2 2018-05-10 stsp while (!done) {
768 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
769 a7f40148 2018-07-18 stsp first_displayed_entry, &commits, selected, LINES,
770 a7f40148 2018-07-18 stsp graph, repo, in_repo_path);
774 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
775 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
776 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
777 80ddbec8 2018-04-29 stsp switch (ch) {
784 80ddbec8 2018-04-29 stsp case KEY_UP:
785 80ddbec8 2018-04-29 stsp if (selected > 0)
786 80ddbec8 2018-04-29 stsp selected--;
787 8ec9698d 2018-05-10 stsp if (selected > 0)
789 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
791 48531068 2018-05-10 stsp case KEY_PPAGE:
792 ecb28ae0 2018-07-16 stsp if (TAILQ_FIRST(&commits.head) ==
793 dfc1d240 2018-05-10 stsp first_displayed_entry) {
794 dfc1d240 2018-05-10 stsp selected = 0;
797 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
801 80ddbec8 2018-04-29 stsp case KEY_DOWN:
802 ecb28ae0 2018-07-16 stsp if (selected < MIN(LINES - 2,
803 ecb28ae0 2018-07-16 stsp commits.ncommits - 1)) {
804 15c91275 2018-05-20 stsp selected++;
807 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
808 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
809 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
811 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
813 ecb28ae0 2018-07-16 stsp err = NULL;
816 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
817 ecb28ae0 2018-07-16 stsp struct commit_queue_entry *first = first_displayed_entry;
818 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
819 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
820 ecb28ae0 2018-07-16 stsp repo, in_repo_path);
822 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
824 ecb28ae0 2018-07-16 stsp /* can't scroll any further; move cursor down */
825 ecb28ae0 2018-07-16 stsp if (first == first_displayed_entry && selected <
826 ecb28ae0 2018-07-16 stsp MIN(LINES - 2, commits.ncommits - 1)) {
827 ecb28ae0 2018-07-16 stsp selected = MIN(LINES - 2,
828 ecb28ae0 2018-07-16 stsp commits.ncommits - 1);
830 ecb28ae0 2018-07-16 stsp err = NULL;
834 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
835 55198a88 2018-07-16 stsp if (selected > LINES - 2)
836 867c6645 2018-07-10 stsp selected = LINES - 2;
837 55198a88 2018-07-16 stsp if (selected > commits.ncommits - 1)
838 55198a88 2018-07-16 stsp selected = commits.ncommits - 1;
840 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
842 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
845 9343a5fb 2018-06-23 stsp show_panel(tog_log_view.panel);
848 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
851 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
858 9ba79e04 2018-06-11 stsp free(head_id);
860 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
861 0553a4e3 2018-04-30 stsp free_commits(&commits);
862 ecb28ae0 2018-07-16 stsp free(in_repo_path);
863 80ddbec8 2018-04-29 stsp return err;
866 4ed7e80c 2018-05-20 stsp static const struct got_error *
867 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
869 80ddbec8 2018-04-29 stsp const struct got_error *error;
870 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
871 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
872 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
873 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
876 80ddbec8 2018-04-29 stsp #ifndef PROFILE
877 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
878 80ddbec8 2018-04-29 stsp err(1, "pledge");
881 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
882 80ddbec8 2018-04-29 stsp switch (ch) {
884 80ddbec8 2018-04-29 stsp start_commit = optarg;
887 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
888 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
889 ecb28ae0 2018-07-16 stsp err(1, "-r option");
893 80ddbec8 2018-04-29 stsp /* NOTREACHED */
897 80ddbec8 2018-04-29 stsp argc -= optind;
898 80ddbec8 2018-04-29 stsp argv += optind;
900 ecb28ae0 2018-07-16 stsp if (argc == 0)
901 ecb28ae0 2018-07-16 stsp path = strdup("");
902 ecb28ae0 2018-07-16 stsp else if (argc == 1)
903 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
905 80ddbec8 2018-04-29 stsp usage_log();
906 ecb28ae0 2018-07-16 stsp if (path == NULL)
907 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
909 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
910 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
911 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
914 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
915 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
916 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
917 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
922 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
923 80ddbec8 2018-04-29 stsp if (error != NULL)
926 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
927 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
928 80ddbec8 2018-04-29 stsp if (error != NULL)
931 80ddbec8 2018-04-29 stsp struct got_object *obj;
932 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
933 80ddbec8 2018-04-29 stsp if (error == NULL) {
934 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
935 899d86c2 2018-05-10 stsp if (start_id == NULL)
936 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
940 80ddbec8 2018-04-29 stsp if (error != NULL)
943 ecb28ae0 2018-07-16 stsp error = show_log_view(start_id, repo, path);
945 ecb28ae0 2018-07-16 stsp free(repo_path);
947 ecb28ae0 2018-07-16 stsp free(path);
948 899d86c2 2018-05-10 stsp free(start_id);
950 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
951 80ddbec8 2018-04-29 stsp return error;
954 4ed7e80c 2018-05-20 stsp __dead static void
955 9f7d7167 2018-04-29 stsp usage_diff(void)
958 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
959 9f7d7167 2018-04-29 stsp getprogname());
963 b304db33 2018-05-20 stsp static char *
964 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
966 b304db33 2018-05-20 stsp char *line;
967 b304db33 2018-05-20 stsp size_t linelen;
968 b304db33 2018-05-20 stsp size_t lineno;
969 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
971 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
973 b304db33 2018-05-20 stsp *len = linelen;
974 b304db33 2018-05-20 stsp return line;
977 4ed7e80c 2018-05-20 stsp static const struct got_error *
978 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
979 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
981 61e69b96 2018-05-20 stsp const struct got_error *err;
982 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
983 b304db33 2018-05-20 stsp char *line;
984 b304db33 2018-05-20 stsp size_t len;
985 61e69b96 2018-05-20 stsp wchar_t *wline;
989 a70480e0 2018-06-23 stsp werase(window);
992 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
993 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
994 26ed57b2 2018-05-19 stsp if (line == NULL) {
998 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
999 26ed57b2 2018-05-19 stsp free(line);
1003 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
1004 61e69b96 2018-05-20 stsp if (err) {
1005 61e69b96 2018-05-20 stsp free(line);
1006 2550e4c3 2018-07-13 stsp free(wline);
1007 61e69b96 2018-05-20 stsp return err;
1009 a70480e0 2018-06-23 stsp waddwstr(window, wline);
1010 e0b650dd 2018-05-20 stsp if (width < COLS)
1011 a70480e0 2018-06-23 stsp waddch(window, '\n');
1012 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1013 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1014 26ed57b2 2018-05-19 stsp free(line);
1015 2550e4c3 2018-07-13 stsp free(wline);
1016 2550e4c3 2018-07-13 stsp wline = NULL;
1018 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1020 26ed57b2 2018-05-19 stsp update_panels();
1021 26ed57b2 2018-05-19 stsp doupdate();
1023 26ed57b2 2018-05-19 stsp return NULL;
1026 4ed7e80c 2018-05-20 stsp static const struct got_error *
1027 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
1028 26ed57b2 2018-05-19 stsp struct got_repository *repo)
1030 26ed57b2 2018-05-19 stsp const struct got_error *err;
1032 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1033 b304db33 2018-05-20 stsp int eof, i;
1035 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1036 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1037 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1039 511a516b 2018-05-19 stsp f = got_opentemp();
1040 26ed57b2 2018-05-19 stsp if (f == NULL)
1041 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1043 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1044 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1045 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1047 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1048 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1050 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1051 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1054 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1057 26ed57b2 2018-05-19 stsp fflush(f);
1059 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
1060 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
1061 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
1062 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1063 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
1065 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
1066 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
1067 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
1068 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1070 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
1072 26ed57b2 2018-05-19 stsp while (!done) {
1073 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
1074 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
1077 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1078 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
1079 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1080 26ed57b2 2018-05-19 stsp switch (ch) {
1085 26ed57b2 2018-05-19 stsp case KEY_UP:
1086 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
1087 b304db33 2018-05-20 stsp first_displayed_line--;
1089 b304db33 2018-05-20 stsp case KEY_PPAGE:
1090 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1092 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
1093 d56caa55 2018-05-20 stsp first_displayed_line > 1)
1094 26ed57b2 2018-05-19 stsp first_displayed_line--;
1097 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1099 26ed57b2 2018-05-19 stsp first_displayed_line++;
1101 b304db33 2018-05-20 stsp case KEY_NPAGE:
1104 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
1105 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
1106 b304db33 2018-05-20 stsp first_displayed_line++;
1107 b304db33 2018-05-20 stsp if (line == NULL)
1115 26ed57b2 2018-05-19 stsp fclose(f);
1116 26ed57b2 2018-05-19 stsp return err;
1119 4ed7e80c 2018-05-20 stsp static const struct got_error *
1120 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1122 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1123 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1124 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1125 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1126 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1129 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1130 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1131 26ed57b2 2018-05-19 stsp err(1, "pledge");
1134 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1135 26ed57b2 2018-05-19 stsp switch (ch) {
1138 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1142 26ed57b2 2018-05-19 stsp argc -= optind;
1143 26ed57b2 2018-05-19 stsp argv += optind;
1145 26ed57b2 2018-05-19 stsp if (argc == 0) {
1146 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1147 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1148 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1149 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1150 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1151 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1152 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1153 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1154 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1155 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1156 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1157 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1158 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1160 26ed57b2 2018-05-19 stsp usage_diff();
1162 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1163 26ed57b2 2018-05-19 stsp free(repo_path);
1164 26ed57b2 2018-05-19 stsp if (error)
1165 26ed57b2 2018-05-19 stsp goto done;
1167 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1168 26ed57b2 2018-05-19 stsp if (error)
1169 26ed57b2 2018-05-19 stsp goto done;
1171 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1172 26ed57b2 2018-05-19 stsp if (error)
1173 26ed57b2 2018-05-19 stsp goto done;
1175 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1177 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1179 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1181 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1182 26ed57b2 2018-05-19 stsp return error;
1185 4ed7e80c 2018-05-20 stsp __dead static void
1186 9f7d7167 2018-04-29 stsp usage_blame(void)
1189 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1190 9f7d7167 2018-04-29 stsp getprogname());
1194 84451b3e 2018-07-10 stsp struct tog_blame_line {
1195 84451b3e 2018-07-10 stsp int annotated;
1196 84451b3e 2018-07-10 stsp struct got_object_id *id;
1199 4ed7e80c 2018-05-20 stsp static const struct got_error *
1200 ab089a2a 2018-07-12 stsp draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1201 18430de3 2018-07-10 stsp struct tog_blame_line *lines, int nlines, int blame_complete,
1202 b700b5d6 2018-07-10 stsp int selected_line, int *first_displayed_line, int *last_displayed_line,
1203 18430de3 2018-07-10 stsp int *eof, int max_lines)
1205 84451b3e 2018-07-10 stsp const struct got_error *err;
1206 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1207 84451b3e 2018-07-10 stsp char *line;
1208 84451b3e 2018-07-10 stsp size_t len;
1209 84451b3e 2018-07-10 stsp wchar_t *wline;
1210 b700b5d6 2018-07-10 stsp int width, wlimit;
1211 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1212 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1213 ab089a2a 2018-07-12 stsp char *id_str;
1215 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1217 ab089a2a 2018-07-12 stsp return err;
1219 84451b3e 2018-07-10 stsp rewind(f);
1220 84451b3e 2018-07-10 stsp werase(window);
1222 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1223 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1224 ab089a2a 2018-07-12 stsp free(id_str);
1225 ab089a2a 2018-07-12 stsp return err;
1228 ab089a2a 2018-07-12 stsp err = format_line(&wline, &width, line, COLS);
1229 ab089a2a 2018-07-12 stsp free(line);
1230 2550e4c3 2018-07-13 stsp line = NULL;
1231 ab089a2a 2018-07-12 stsp waddwstr(window, wline);
1232 2550e4c3 2018-07-13 stsp free(wline);
1233 2550e4c3 2018-07-13 stsp wline = NULL;
1234 ab089a2a 2018-07-12 stsp if (width < COLS)
1235 ab089a2a 2018-07-12 stsp waddch(window, '\n');
1237 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1238 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1239 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1240 ab089a2a 2018-07-12 stsp free(id_str);
1241 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1243 ab089a2a 2018-07-12 stsp free(id_str);
1244 3f60a8ef 2018-07-10 stsp err = format_line(&wline, &width, line, COLS);
1245 3f60a8ef 2018-07-10 stsp free(line);
1246 2550e4c3 2018-07-13 stsp line = NULL;
1248 3f60a8ef 2018-07-10 stsp return err;
1249 3f60a8ef 2018-07-10 stsp waddwstr(window, wline);
1250 2550e4c3 2018-07-13 stsp free(wline);
1251 2550e4c3 2018-07-13 stsp wline = NULL;
1252 3f60a8ef 2018-07-10 stsp if (width < COLS)
1253 3f60a8ef 2018-07-10 stsp waddch(window, '\n');
1256 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1257 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1258 84451b3e 2018-07-10 stsp if (line == NULL) {
1262 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1263 84451b3e 2018-07-10 stsp free(line);
1267 b700b5d6 2018-07-10 stsp wlimit = COLS < 9 ? 0 : COLS - 9;
1268 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1269 84451b3e 2018-07-10 stsp if (err) {
1270 84451b3e 2018-07-10 stsp free(line);
1271 84451b3e 2018-07-10 stsp return err;
1274 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1275 b700b5d6 2018-07-10 stsp wstandout(window);
1277 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1278 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1279 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1280 ee41ec32 2018-07-10 stsp waddstr(window, " ");
1281 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1282 84451b3e 2018-07-10 stsp char *id_str;
1283 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1284 84451b3e 2018-07-10 stsp if (err) {
1285 84451b3e 2018-07-10 stsp free(line);
1286 2550e4c3 2018-07-13 stsp free(wline);
1287 84451b3e 2018-07-10 stsp return err;
1289 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1290 84451b3e 2018-07-10 stsp free(id_str);
1291 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1293 ee41ec32 2018-07-10 stsp waddstr(window, "........ ");
1294 ee41ec32 2018-07-10 stsp prev_id = NULL;
1297 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1298 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1299 b700b5d6 2018-07-10 stsp waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1302 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1303 b700b5d6 2018-07-10 stsp wstandend(window);
1304 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1305 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1306 84451b3e 2018-07-10 stsp free(line);
1307 2550e4c3 2018-07-13 stsp free(wline);
1308 2550e4c3 2018-07-13 stsp wline = NULL;
1310 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1312 84451b3e 2018-07-10 stsp update_panels();
1313 84451b3e 2018-07-10 stsp doupdate();
1315 84451b3e 2018-07-10 stsp return NULL;
1318 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1319 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1320 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1321 84451b3e 2018-07-10 stsp int nlines;
1323 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1325 3f60a8ef 2018-07-10 stsp const char *path;
1326 84451b3e 2018-07-10 stsp int *first_displayed_line;
1327 84451b3e 2018-07-10 stsp int *last_displayed_line;
1328 b700b5d6 2018-07-10 stsp int *selected_line;
1329 18430de3 2018-07-10 stsp int *quit;
1332 84451b3e 2018-07-10 stsp static const struct got_error *
1333 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1335 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1336 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1337 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1340 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1341 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1342 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1344 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1345 84451b3e 2018-07-10 stsp return got_error_from_errno();
1347 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1348 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1349 d68a0a7d 2018-07-10 stsp goto done;
1352 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1353 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1355 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1356 d68a0a7d 2018-07-10 stsp if (line->annotated)
1357 d68a0a7d 2018-07-10 stsp goto done;
1359 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1360 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1361 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1362 84451b3e 2018-07-10 stsp goto done;
1364 84451b3e 2018-07-10 stsp line->annotated = 1;
1366 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1367 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1368 ab089a2a 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1370 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1371 84451b3e 2018-07-10 stsp return got_error_from_errno();
1372 84451b3e 2018-07-10 stsp return err;
1375 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1376 84451b3e 2018-07-10 stsp const char *path;
1377 84451b3e 2018-07-10 stsp struct got_repository *repo;
1378 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1379 18430de3 2018-07-10 stsp int *complete;
1382 84451b3e 2018-07-10 stsp static void *
1383 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1385 18430de3 2018-07-10 stsp const struct got_error *err;
1386 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1387 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1390 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1391 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1392 9328d2ed 2018-07-13 stsp got_repo_close(ta->repo);
1393 9328d2ed 2018-07-13 stsp ta->repo = NULL;
1394 18430de3 2018-07-10 stsp *ta->complete = 1;
1396 18430de3 2018-07-10 stsp return (void *)err;
1398 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1399 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1401 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1402 245d91c1 2018-07-12 stsp a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1403 245d91c1 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1405 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1406 18430de3 2018-07-10 stsp err = got_error_from_errno();
1408 18430de3 2018-07-10 stsp return (void *)err;
1411 245d91c1 2018-07-12 stsp static struct got_object_id *
1412 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1413 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1415 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1417 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1418 245d91c1 2018-07-12 stsp if (!line->annotated)
1419 245d91c1 2018-07-12 stsp return NULL;
1421 245d91c1 2018-07-12 stsp return line->id;
1424 84451b3e 2018-07-10 stsp static const struct got_error *
1425 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1426 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1427 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1429 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1430 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1431 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1432 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1434 b880a918 2018-07-10 stsp *pobj = NULL;
1435 b880a918 2018-07-10 stsp *obj = NULL;
1437 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1438 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1439 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1440 b880a918 2018-07-10 stsp return NULL;
1442 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1444 b880a918 2018-07-10 stsp goto done;
1446 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1448 b880a918 2018-07-10 stsp goto done;
1450 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1451 b880a918 2018-07-10 stsp if (pid) {
1452 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1454 b880a918 2018-07-10 stsp goto done;
1457 b880a918 2018-07-10 stsp if (commit)
1458 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1459 b880a918 2018-07-10 stsp return err;
1462 245d91c1 2018-07-12 stsp struct tog_blame {
1464 245d91c1 2018-07-12 stsp size_t filesize;
1465 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1466 245d91c1 2018-07-12 stsp size_t nlines;
1467 245d91c1 2018-07-12 stsp pthread_t thread;
1468 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1469 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1470 245d91c1 2018-07-12 stsp const char *path;
1471 245d91c1 2018-07-12 stsp struct got_object_id *commit_id;
1474 b880a918 2018-07-10 stsp static const struct got_error *
1475 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1477 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1480 245d91c1 2018-07-12 stsp if (blame->thread) {
1481 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1482 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1483 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1484 245d91c1 2018-07-12 stsp err = NULL;
1485 245d91c1 2018-07-12 stsp blame->thread = NULL;
1487 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1488 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1489 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1491 245d91c1 2018-07-12 stsp if (blame->f) {
1492 245d91c1 2018-07-12 stsp fclose(blame->f);
1493 245d91c1 2018-07-12 stsp blame->f = NULL;
1495 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1496 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1497 245d91c1 2018-07-12 stsp free(blame->lines);
1498 245d91c1 2018-07-12 stsp blame->lines = NULL;
1499 245d91c1 2018-07-12 stsp free(blame->commit_id);
1500 245d91c1 2018-07-12 stsp blame->commit_id = NULL;
1502 245d91c1 2018-07-12 stsp return err;
1505 245d91c1 2018-07-12 stsp static const struct got_error *
1506 245d91c1 2018-07-12 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1507 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1508 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1509 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1510 245d91c1 2018-07-12 stsp struct got_repository *repo)
1512 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1513 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1514 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1515 245d91c1 2018-07-12 stsp struct got_object *obj;
1517 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1519 84451b3e 2018-07-10 stsp goto done;
1520 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1521 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1522 84451b3e 2018-07-10 stsp goto done;
1525 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1527 a70480e0 2018-06-23 stsp goto done;
1528 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1529 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1530 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1531 84451b3e 2018-07-10 stsp goto done;
1533 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1534 245d91c1 2018-07-12 stsp blame->f, blob);
1536 84451b3e 2018-07-10 stsp goto done;
1538 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1539 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1540 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1541 84451b3e 2018-07-10 stsp goto done;
1544 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1546 bd24772e 2018-07-11 stsp goto done;
1548 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1549 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1550 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1551 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1552 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1553 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1554 245d91c1 2018-07-12 stsp goto done;
1556 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1557 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1558 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1559 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1560 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1561 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1563 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1564 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1565 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1566 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1567 245d91c1 2018-07-12 stsp *blame_complete = 0;
1569 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1570 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1571 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1572 245d91c1 2018-07-12 stsp goto done;
1577 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1579 245d91c1 2018-07-12 stsp got_object_close(obj);
1581 245d91c1 2018-07-12 stsp stop_blame(blame);
1582 245d91c1 2018-07-12 stsp return err;
1585 245d91c1 2018-07-12 stsp static const struct got_error *
1586 245d91c1 2018-07-12 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1587 245d91c1 2018-07-12 stsp struct got_repository *repo)
1589 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1590 245d91c1 2018-07-12 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1591 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1592 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1593 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1594 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1595 245d91c1 2018-07-12 stsp struct tog_blame blame;
1596 245d91c1 2018-07-12 stsp int blame_running = 0;
1597 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1598 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1600 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1602 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1603 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1604 245d91c1 2018-07-12 stsp goto done;
1607 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1609 245d91c1 2018-07-12 stsp goto done;
1610 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1612 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1613 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1614 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1615 a70480e0 2018-06-23 stsp return got_error_from_errno();
1616 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1618 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1619 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1620 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1621 a70480e0 2018-06-23 stsp return got_error_from_errno();
1623 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1625 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1626 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex, &blame_complete,
1627 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1628 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1630 245d91c1 2018-07-12 stsp return err;
1632 a70480e0 2018-06-23 stsp while (!done) {
1633 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1634 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1635 84451b3e 2018-07-10 stsp goto done;
1637 dbc6a6b6 2018-07-12 stsp err = draw_blame(tog_blame_view.window, blamed_commit->id,
1638 245d91c1 2018-07-12 stsp blame.f, path, blame.lines, blame.nlines, blame_complete,
1639 245d91c1 2018-07-12 stsp selected_line, &first_displayed_line, &last_displayed_line,
1640 245d91c1 2018-07-12 stsp &eof, LINES);
1641 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1642 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1643 84451b3e 2018-07-10 stsp goto done;
1647 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1648 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1649 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1650 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1651 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1652 84451b3e 2018-07-10 stsp goto done;
1654 a70480e0 2018-06-23 stsp switch (ch) {
1659 a70480e0 2018-06-23 stsp case KEY_UP:
1660 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1661 b700b5d6 2018-07-10 stsp selected_line--;
1662 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1663 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1664 a70480e0 2018-06-23 stsp first_displayed_line--;
1666 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1667 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1668 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1669 b700b5d6 2018-07-10 stsp selected_line = 1;
1672 084063cd 2018-07-12 stsp if (first_displayed_line > LINES - 2)
1673 084063cd 2018-07-12 stsp first_displayed_line -= (LINES - 2);
1675 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1678 a70480e0 2018-06-23 stsp case KEY_DOWN:
1679 a026b947 2018-07-12 stsp if (selected_line < LINES - 2 &&
1680 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1681 a026b947 2018-07-12 stsp blame.nlines)
1682 b700b5d6 2018-07-10 stsp selected_line++;
1683 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1684 b700b5d6 2018-07-10 stsp first_displayed_line++;
1687 7a2921f9 2018-07-12 stsp case 'p': {
1688 245d91c1 2018-07-12 stsp struct got_object_id *id;
1689 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1690 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1691 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1692 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1694 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1695 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1696 245d91c1 2018-07-12 stsp selected_line, repo);
1699 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1701 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1704 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1705 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1706 ad6a6c43 2018-07-10 stsp goto done;
1708 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1709 245d91c1 2018-07-12 stsp blame_running = 0;
1711 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1712 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1713 245d91c1 2018-07-12 stsp goto done;
1715 245d91c1 2018-07-12 stsp if (thread_err)
1717 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1718 1960a6f9 2018-07-12 stsp got_object_close(obj);
1719 1960a6f9 2018-07-12 stsp obj = NULL;
1720 1960a6f9 2018-07-12 stsp if (pobj) {
1721 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1722 14437fb1 2018-07-13 stsp pobj = NULL;
1724 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1725 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1728 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1731 dbc6a6b6 2018-07-12 stsp goto done;
1732 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1733 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1734 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex,
1735 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1736 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1737 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1740 dbc6a6b6 2018-07-12 stsp blame_running = 1;
1743 dbc6a6b6 2018-07-12 stsp case 'B': {
1744 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1745 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1746 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1749 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1750 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1751 dbc6a6b6 2018-07-12 stsp goto done;
1753 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1754 dbc6a6b6 2018-07-12 stsp blame_running = 0;
1756 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1757 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1758 dbc6a6b6 2018-07-12 stsp goto done;
1760 dbc6a6b6 2018-07-12 stsp if (thread_err)
1762 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1763 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1764 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1765 dbc6a6b6 2018-07-12 stsp err = run_blame(&blame, &mutex,
1766 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1767 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1768 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1771 245d91c1 2018-07-12 stsp blame_running = 1;
1774 245d91c1 2018-07-12 stsp case KEY_ENTER:
1775 245d91c1 2018-07-12 stsp case '\r':
1776 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1777 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1778 245d91c1 2018-07-12 stsp selected_line, repo);
1781 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1783 b880a918 2018-07-10 stsp err = show_diff_view(pobj, obj, repo);
1784 b880a918 2018-07-10 stsp if (pobj) {
1785 b880a918 2018-07-10 stsp got_object_close(pobj);
1786 b880a918 2018-07-10 stsp pobj = NULL;
1788 b880a918 2018-07-10 stsp got_object_close(obj);
1789 b880a918 2018-07-10 stsp obj = NULL;
1790 ad6a6c43 2018-07-10 stsp show_panel(tog_blame_view.panel);
1794 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1796 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1797 084063cd 2018-07-12 stsp selected_line < LINES - 2) {
1798 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1799 d2dfcfbf 2018-07-12 stsp LINES - 2);
1802 245d91c1 2018-07-12 stsp if (last_displayed_line + LINES - 2 <=
1803 245d91c1 2018-07-12 stsp blame.nlines)
1804 084063cd 2018-07-12 stsp first_displayed_line += LINES - 2;
1806 b700b5d6 2018-07-10 stsp first_displayed_line =
1807 245d91c1 2018-07-12 stsp blame.nlines - (LINES - 3);
1812 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1813 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1814 245d91c1 2018-07-12 stsp if (err || thread_err)
1819 b880a918 2018-07-10 stsp got_object_close(pobj);
1820 245d91c1 2018-07-12 stsp if (blame_running)
1821 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1822 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1823 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1824 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1825 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1827 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1830 a70480e0 2018-06-23 stsp static const struct got_error *
1831 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1833 a70480e0 2018-06-23 stsp const struct got_error *error;
1834 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1835 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1836 a70480e0 2018-06-23 stsp char *path = NULL;
1837 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1838 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1841 a70480e0 2018-06-23 stsp #ifndef PROFILE
1842 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1843 a70480e0 2018-06-23 stsp err(1, "pledge");
1846 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1847 a70480e0 2018-06-23 stsp switch (ch) {
1849 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1853 a70480e0 2018-06-23 stsp /* NOTREACHED */
1857 a70480e0 2018-06-23 stsp argc -= optind;
1858 a70480e0 2018-06-23 stsp argv += optind;
1860 a70480e0 2018-06-23 stsp if (argc == 0) {
1861 a70480e0 2018-06-23 stsp usage_blame();
1862 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1863 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1864 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1865 a70480e0 2018-06-23 stsp return got_error_from_errno();
1866 a70480e0 2018-06-23 stsp path = argv[0];
1867 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1868 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1869 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1870 a70480e0 2018-06-23 stsp return got_error_from_errno();
1871 a70480e0 2018-06-23 stsp path = argv[1];
1873 a70480e0 2018-06-23 stsp usage_blame();
1875 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1876 a70480e0 2018-06-23 stsp free(repo_path);
1877 a70480e0 2018-06-23 stsp if (error != NULL)
1878 66b4983c 2018-06-23 stsp return error;
1880 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1881 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1882 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1883 a70480e0 2018-06-23 stsp if (error != NULL)
1884 66b4983c 2018-06-23 stsp goto done;
1885 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1886 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1888 a70480e0 2018-06-23 stsp struct got_object *obj;
1889 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1890 a70480e0 2018-06-23 stsp if (error != NULL)
1891 66b4983c 2018-06-23 stsp goto done;
1892 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1893 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1894 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1895 a19e88aa 2018-06-23 stsp got_object_close(obj);
1897 a19e88aa 2018-06-23 stsp if (error != NULL)
1898 a19e88aa 2018-06-23 stsp goto done;
1900 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1902 a70480e0 2018-06-23 stsp free(commit_id);
1904 a70480e0 2018-06-23 stsp got_repo_close(repo);
1905 a70480e0 2018-06-23 stsp return error;
1908 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1909 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1910 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1911 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1912 1d13200f 2018-07-12 stsp WINDOW *window, const char *label, int show_ids, const char *parent_path,
1913 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1915 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1916 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1917 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1918 ffd1d5e5 2018-06-23 stsp int width, n;
1920 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1922 ffd1d5e5 2018-06-23 stsp werase(window);
1924 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1925 ffd1d5e5 2018-06-23 stsp return NULL;
1927 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1929 ffd1d5e5 2018-06-23 stsp return err;
1930 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1931 2550e4c3 2018-07-13 stsp free(wline);
1932 2550e4c3 2018-07-13 stsp wline = NULL;
1933 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1934 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1935 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1936 ffd1d5e5 2018-06-23 stsp return NULL;
1937 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1939 ce52c690 2018-06-23 stsp return err;
1940 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1941 2550e4c3 2018-07-13 stsp free(wline);
1942 2550e4c3 2018-07-13 stsp wline = NULL;
1943 ce52c690 2018-06-23 stsp if (width < COLS)
1944 ce52c690 2018-06-23 stsp waddch(window, '\n');
1945 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1946 ffd1d5e5 2018-06-23 stsp return NULL;
1947 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1948 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1949 a1eca9bb 2018-06-23 stsp return NULL;
1951 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1952 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1953 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1954 ffd1d5e5 2018-06-23 stsp wstandout(window);
1955 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1957 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1958 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1959 ffd1d5e5 2018-06-23 stsp wstandend(window);
1960 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1961 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1962 ffd1d5e5 2018-06-23 stsp return NULL;
1966 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1967 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1970 ffd1d5e5 2018-06-23 stsp while (te) {
1971 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
1973 1d13200f 2018-07-12 stsp if (show_ids) {
1974 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
1976 1d13200f 2018-07-12 stsp return got_error_from_errno();
1978 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1979 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1980 1d13200f 2018-07-12 stsp free(id_str);
1981 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1983 1d13200f 2018-07-12 stsp free(id_str);
1984 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1985 ffd1d5e5 2018-06-23 stsp if (err) {
1986 ffd1d5e5 2018-06-23 stsp free(line);
1989 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1990 ffd1d5e5 2018-06-23 stsp wstandout(window);
1991 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1993 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1994 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1995 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1996 ffd1d5e5 2018-06-23 stsp if (n == selected)
1997 ffd1d5e5 2018-06-23 stsp wstandend(window);
1998 ffd1d5e5 2018-06-23 stsp free(line);
1999 2550e4c3 2018-07-13 stsp free(wline);
2000 2550e4c3 2018-07-13 stsp wline = NULL;
2002 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2003 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2004 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2006 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2009 ffd1d5e5 2018-06-23 stsp return err;
2012 ffd1d5e5 2018-06-23 stsp static void
2013 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2014 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2016 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2019 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2022 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2023 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2024 ffd1d5e5 2018-06-23 stsp if (!isroot)
2025 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2029 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2030 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2031 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2032 ffd1d5e5 2018-06-23 stsp prev = te;
2033 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2035 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2036 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2038 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2039 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2042 ffd1d5e5 2018-06-23 stsp static void
2043 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2044 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2045 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2047 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2048 ffd1d5e5 2018-06-23 stsp int n = 0;
2050 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2053 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2054 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2056 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2057 ffd1d5e5 2018-06-23 stsp while (next) {
2058 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2059 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2061 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2065 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2066 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2067 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2068 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2069 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2070 ffd1d5e5 2018-06-23 stsp int selected;
2073 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2075 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2076 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2077 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2079 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2080 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2081 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2083 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2084 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2086 ce52c690 2018-06-23 stsp len += strlen(te->name);
2088 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2089 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2090 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2092 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2093 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2094 d9765a41 2018-06-23 stsp while (pt) {
2095 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2096 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2097 cb2ebc8a 2018-06-23 stsp goto done;
2099 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2100 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2101 cb2ebc8a 2018-06-23 stsp goto done;
2103 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2106 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2107 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2108 ce52c690 2018-06-23 stsp goto done;
2112 ce52c690 2018-06-23 stsp if (err) {
2113 ce52c690 2018-06-23 stsp free(*path);
2114 ce52c690 2018-06-23 stsp *path = NULL;
2116 ce52c690 2018-06-23 stsp return err;
2119 ce52c690 2018-06-23 stsp static const struct got_error *
2120 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2121 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2123 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2124 ce52c690 2018-06-23 stsp char *path;
2126 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2128 ce52c690 2018-06-23 stsp return err;
2130 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
2131 69efd4c4 2018-07-18 stsp free(path);
2132 69efd4c4 2018-07-18 stsp return err;
2135 69efd4c4 2018-07-18 stsp static const struct got_error *
2136 69efd4c4 2018-07-18 stsp log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2137 69efd4c4 2018-07-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2139 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2140 69efd4c4 2018-07-18 stsp char *path;
2142 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2144 69efd4c4 2018-07-18 stsp return err;
2146 69efd4c4 2018-07-18 stsp err = show_log_view(commit_id, repo, path);
2147 cb2ebc8a 2018-06-23 stsp free(path);
2148 cb2ebc8a 2018-06-23 stsp return err;
2151 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2152 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2153 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
2155 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2156 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2157 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2158 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2159 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2160 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2161 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2162 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2163 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2164 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2166 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2168 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2169 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2170 ffd1d5e5 2018-06-23 stsp goto done;
2172 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2173 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2174 ffd1d5e5 2018-06-23 stsp goto done;
2177 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
2178 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
2179 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
2180 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2181 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
2183 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
2184 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
2185 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
2186 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2188 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
2190 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2191 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2192 ffd1d5e5 2018-06-23 stsp while (!done) {
2193 ce52c690 2018-06-23 stsp char *parent_path;
2194 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2195 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2196 ffd1d5e5 2018-06-23 stsp if (tree != root)
2197 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2199 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2201 ce52c690 2018-06-23 stsp goto done;
2203 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
2204 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2205 1d13200f 2018-07-12 stsp tog_tree_view.window, tree_label, show_ids,
2206 1d13200f 2018-07-12 stsp parent_path, entries, selected, LINES, tree == root);
2207 ce52c690 2018-06-23 stsp free(parent_path);
2211 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2212 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
2213 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2214 ffd1d5e5 2018-06-23 stsp switch (ch) {
2219 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2222 69efd4c4 2018-07-18 stsp if (selected_entry) {
2223 69efd4c4 2018-07-18 stsp err = log_tree_entry(selected_entry,
2224 69efd4c4 2018-07-18 stsp &parents, commit_id, repo);
2226 69efd4c4 2018-07-18 stsp goto done;
2230 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2231 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2232 ffd1d5e5 2018-06-23 stsp selected--;
2233 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2235 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2236 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2238 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2239 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2240 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2241 cf8f1261 2018-06-23 stsp if (tree != root)
2242 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2243 ffd1d5e5 2018-06-23 stsp selected = 0;
2246 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
2247 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2250 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2251 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2252 ffd1d5e5 2018-06-23 stsp selected++;
2255 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2256 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2258 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2259 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
2260 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2261 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2263 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2264 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2265 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2267 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2268 ffd1d5e5 2018-06-23 stsp case '\r':
2269 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2270 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2271 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2272 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2273 ffd1d5e5 2018-06-23 stsp if (tree == root)
2275 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2276 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2277 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2278 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2279 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2280 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2281 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2282 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2283 ffd1d5e5 2018-06-23 stsp free(parent);
2284 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2285 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2286 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2287 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2288 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2290 ffd1d5e5 2018-06-23 stsp goto done;
2291 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2292 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2293 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2294 ffd1d5e5 2018-06-23 stsp goto done;
2296 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2297 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2298 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2299 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2300 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2301 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2303 ffd1d5e5 2018-06-23 stsp tree = child;
2304 ffd1d5e5 2018-06-23 stsp selected = 0;
2305 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2306 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2307 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
2308 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
2310 ffd1d5e5 2018-06-23 stsp goto done;
2313 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2314 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
2315 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2322 ffd1d5e5 2018-06-23 stsp free(tree_label);
2323 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2324 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2325 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2326 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2327 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2328 ffd1d5e5 2018-06-23 stsp free(parent);
2331 ffd1d5e5 2018-06-23 stsp if (tree != root)
2332 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2333 ffd1d5e5 2018-06-23 stsp return err;
2336 ffd1d5e5 2018-06-23 stsp __dead static void
2337 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2340 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2341 ffd1d5e5 2018-06-23 stsp getprogname());
2345 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2346 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2348 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2349 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2350 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2351 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2352 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2353 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2354 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2357 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2358 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2359 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2362 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2363 ffd1d5e5 2018-06-23 stsp switch (ch) {
2365 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2369 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2373 ffd1d5e5 2018-06-23 stsp argc -= optind;
2374 ffd1d5e5 2018-06-23 stsp argv += optind;
2376 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2377 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2378 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2379 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2380 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2381 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2382 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2383 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2385 ffd1d5e5 2018-06-23 stsp usage_log();
2387 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2388 ffd1d5e5 2018-06-23 stsp free(repo_path);
2389 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2390 ffd1d5e5 2018-06-23 stsp return error;
2392 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2393 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2394 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2395 ffd1d5e5 2018-06-23 stsp goto done;
2397 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2398 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2399 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2400 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2401 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2402 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2405 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2406 ffd1d5e5 2018-06-23 stsp goto done;
2408 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2409 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2410 ffd1d5e5 2018-06-23 stsp goto done;
2412 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2413 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2414 ffd1d5e5 2018-06-23 stsp goto done;
2416 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
2418 ffd1d5e5 2018-06-23 stsp free(commit_id);
2419 ffd1d5e5 2018-06-23 stsp if (commit)
2420 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2422 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2424 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2425 ffd1d5e5 2018-06-23 stsp return error;
2427 5c5136c5 2018-05-20 stsp static void
2428 9f7d7167 2018-04-29 stsp init_curses(void)
2430 9f7d7167 2018-04-29 stsp initscr();
2434 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2435 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2436 1f475ad8 2018-05-10 stsp curs_set(0);
2439 4ed7e80c 2018-05-20 stsp __dead static void
2440 9f7d7167 2018-04-29 stsp usage(void)
2444 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2445 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2446 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2447 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2448 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2453 c2301be8 2018-04-30 stsp static char **
2454 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2456 c2301be8 2018-04-30 stsp char **argv;
2457 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2459 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2460 c2301be8 2018-04-30 stsp if (argv == NULL)
2461 c2301be8 2018-04-30 stsp err(1, "calloc");
2462 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2463 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2464 c2301be8 2018-04-30 stsp err(1, "calloc");
2465 c2301be8 2018-04-30 stsp if (arg1) {
2466 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2467 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2468 c2301be8 2018-04-30 stsp err(1, "calloc");
2471 c2301be8 2018-04-30 stsp return argv;
2475 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2477 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2478 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2479 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2480 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2482 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2484 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2485 9f7d7167 2018-04-29 stsp switch (ch) {
2487 9f7d7167 2018-04-29 stsp hflag = 1;
2491 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2495 9f7d7167 2018-04-29 stsp argc -= optind;
2496 9f7d7167 2018-04-29 stsp argv += optind;
2497 9f7d7167 2018-04-29 stsp optind = 0;
2498 c2301be8 2018-04-30 stsp optreset = 1;
2500 c2301be8 2018-04-30 stsp if (argc == 0) {
2501 f29d3e89 2018-06-23 stsp if (hflag)
2503 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2504 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2505 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2510 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2511 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2512 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2513 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2514 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2515 9f7d7167 2018-04-29 stsp if (hflag)
2516 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2520 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2521 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2522 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2523 c2301be8 2018-04-30 stsp if (repo_path) {
2524 c2301be8 2018-04-30 stsp struct got_repository *repo;
2525 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2526 c2301be8 2018-04-30 stsp if (error == NULL)
2527 c2301be8 2018-04-30 stsp got_repo_close(repo);
2529 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2530 c2301be8 2018-04-30 stsp if (error) {
2531 f29d3e89 2018-06-23 stsp if (hflag) {
2532 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2533 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2537 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2538 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2539 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2540 ad7de8d9 2018-04-30 stsp free(repo_path);
2543 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2544 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2546 c2301be8 2018-04-30 stsp free(repo_path);
2550 5c5136c5 2018-05-20 stsp init_curses();
2552 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2553 9f7d7167 2018-04-29 stsp if (error)
2554 9f7d7167 2018-04-29 stsp goto done;
2557 c2301be8 2018-04-30 stsp free(cmd_argv);
2558 9f7d7167 2018-04-29 stsp if (error)
2559 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);