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 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
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 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-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;
165 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
167 963b370f 2018-05-20 stsp return err;
170 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
171 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
172 963b370f 2018-05-20 stsp switch (width) {
177 963b370f 2018-05-20 stsp cols += width;
180 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
181 e7ea91f8 2018-07-08 stsp cols += TABSIZE - (cols % TABSIZE);
184 963b370f 2018-05-20 stsp err = got_error_from_errno();
187 963b370f 2018-05-20 stsp if (cols <= COLS) {
189 963b370f 2018-05-20 stsp if (widthp)
190 963b370f 2018-05-20 stsp *widthp = cols;
193 963b370f 2018-05-20 stsp wline[i] = L'\0';
196 963b370f 2018-05-20 stsp free(wline);
198 963b370f 2018-05-20 stsp *wlinep = wline;
199 963b370f 2018-05-20 stsp return err;
202 963b370f 2018-05-20 stsp static const struct got_error *
203 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
205 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
206 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
207 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
208 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
209 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
210 bb737323 2018-05-20 stsp int author_width, logmsg_width;
211 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
212 80ddbec8 2018-04-29 stsp char *line = NULL;
213 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
214 bb737323 2018-05-20 stsp size_t id_len;
215 bb737323 2018-05-20 stsp int col, limit;
216 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
217 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
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 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
236 80ddbec8 2018-04-29 stsp return err;
237 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
238 b39d25c7 2018-07-10 stsp if (avail < date_display_cols + id_display_cols) {
239 b39d25c7 2018-07-10 stsp limit = MIN(id_len, avail - date_display_cols);
240 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
242 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
243 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
245 b39d25c7 2018-07-10 stsp col += limit;
246 b39d25c7 2018-07-10 stsp while (col <= avail && col < date_display_cols + id_display_cols + 2) {
247 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
250 9c2eaf34 2018-05-20 stsp if (col > avail)
253 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
254 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
255 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
258 6d9fbc00 2018-04-29 stsp author = author0;
259 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
260 6d9fbc00 2018-04-29 stsp if (smallerthan)
261 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
263 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
265 6d9fbc00 2018-04-29 stsp *at = '\0';
267 b39d25c7 2018-07-10 stsp limit = avail - col;
268 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
271 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
272 bb737323 2018-05-20 stsp col += author_width;
273 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
274 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
276 bb737323 2018-05-20 stsp author_width++;
278 9c2eaf34 2018-05-20 stsp if (col > avail)
281 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
282 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
283 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
286 bb737323 2018-05-20 stsp logmsg = logmsg0;
287 bb737323 2018-05-20 stsp while (*logmsg == '\n')
289 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
290 bb737323 2018-05-20 stsp if (newline)
291 bb737323 2018-05-20 stsp *newline = '\0';
292 bb737323 2018-05-20 stsp limit = avail - col;
293 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
296 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
297 bb737323 2018-05-20 stsp col += logmsg_width;
298 bb737323 2018-05-20 stsp while (col <= avail) {
299 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
303 80ddbec8 2018-04-29 stsp free(logmsg0);
304 bb737323 2018-05-20 stsp free(wlogmsg);
305 6d9fbc00 2018-04-29 stsp free(author0);
306 bb737323 2018-05-20 stsp free(wauthor);
307 80ddbec8 2018-04-29 stsp free(line);
308 6d9fbc00 2018-04-29 stsp free(id_str);
309 80ddbec8 2018-04-29 stsp return err;
312 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
313 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
314 80ddbec8 2018-04-29 stsp struct got_object_id *id;
315 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
317 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
319 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
320 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
321 899d86c2 2018-05-10 stsp struct got_object_id *id)
323 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
325 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
326 80ddbec8 2018-04-29 stsp if (entry == NULL)
327 899d86c2 2018-05-10 stsp return NULL;
329 899d86c2 2018-05-10 stsp entry->id = id;
330 99db9666 2018-05-07 stsp entry->commit = commit;
331 899d86c2 2018-05-10 stsp return entry;
334 99db9666 2018-05-07 stsp static void
335 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
337 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
339 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
340 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
341 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
342 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
343 99db9666 2018-05-07 stsp free(entry);
346 99db9666 2018-05-07 stsp static void
347 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
349 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
350 99db9666 2018-05-07 stsp pop_commit(commits);
353 c4972b91 2018-05-07 stsp static const struct got_error *
354 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
355 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
357 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
358 899d86c2 2018-05-10 stsp struct got_object_id *id;
359 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
361 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
363 c4972b91 2018-05-07 stsp return err;
365 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
366 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
367 9ba79e04 2018-06-11 stsp int nfetched;
369 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
370 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
371 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
372 9ba79e04 2018-06-11 stsp return err;
374 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
376 9ba79e04 2018-06-11 stsp return err;
379 9ba79e04 2018-06-11 stsp while (1) {
380 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
382 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
384 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
385 9ba79e04 2018-06-11 stsp err = NULL;
389 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
393 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
394 9ba79e04 2018-06-11 stsp if (entry == NULL) {
395 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
399 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
402 9ba79e04 2018-06-11 stsp return err;
405 99db9666 2018-05-07 stsp static const struct got_error *
406 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
407 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
408 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
410 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
411 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
413 9ba79e04 2018-06-11 stsp *pentry = NULL;
415 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
416 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
417 9ba79e04 2018-06-11 stsp int nfetched;
418 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
419 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
421 9ba79e04 2018-06-11 stsp return err;
424 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
425 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
427 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
428 9ba79e04 2018-06-11 stsp err = NULL;
429 9ba79e04 2018-06-11 stsp return err;
432 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
433 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
434 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
435 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
437 9ba79e04 2018-06-11 stsp return NULL;
440 899d86c2 2018-05-10 stsp static const struct got_error *
441 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
443 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
444 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
446 9ba79e04 2018-06-11 stsp *head_id = NULL;
448 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
450 99db9666 2018-05-07 stsp return err;
452 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
453 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
455 9ba79e04 2018-06-11 stsp *head_id = NULL;
456 99db9666 2018-05-07 stsp return err;
459 9ba79e04 2018-06-11 stsp return NULL;
462 0553a4e3 2018-04-30 stsp static const struct got_error *
463 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
464 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
466 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
467 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
468 0553a4e3 2018-04-30 stsp int ncommits = 0;
470 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
472 899d86c2 2018-05-10 stsp entry = first;
473 899d86c2 2018-05-10 stsp *last = first;
474 899d86c2 2018-05-10 stsp while (entry) {
475 899d86c2 2018-05-10 stsp if (ncommits == limit)
477 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
478 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
479 cd0acaa7 2018-05-20 stsp *selected = entry;
481 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
482 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
483 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
486 0553a4e3 2018-04-30 stsp ncommits++;
487 899d86c2 2018-05-10 stsp *last = entry;
488 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
491 80ddbec8 2018-04-29 stsp update_panels();
492 80ddbec8 2018-04-29 stsp doupdate();
494 80ddbec8 2018-04-29 stsp return err;
497 07b55e75 2018-05-10 stsp static void
498 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
499 07b55e75 2018-05-10 stsp struct commit_queue *commits)
501 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
502 07b55e75 2018-05-10 stsp int nscrolled = 0;
504 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
505 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
508 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
509 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
510 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
511 07b55e75 2018-05-10 stsp if (entry) {
512 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
513 07b55e75 2018-05-10 stsp nscrolled++;
518 aa075928 2018-05-10 stsp static const struct got_error *
519 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
520 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
521 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
522 9ba79e04 2018-06-11 stsp struct got_repository *repo)
524 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
525 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
526 aa075928 2018-05-10 stsp int nscrolled = 0;
529 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
530 aa075928 2018-05-10 stsp if (pentry == NULL) {
531 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
532 9ba79e04 2018-06-11 stsp commits, graph, repo);
533 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
536 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
538 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
539 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
541 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
542 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
544 dd0a52c1 2018-05-20 stsp return err;
548 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
550 4a7f7875 2018-05-10 stsp int nparents = 0;
552 4a7f7875 2018-05-10 stsp while (entry) {
553 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
554 4a7f7875 2018-05-10 stsp nparents++;
557 4a7f7875 2018-05-10 stsp return nparents;
560 cd0acaa7 2018-05-20 stsp static const struct got_error *
561 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
563 cd0acaa7 2018-05-20 stsp const struct got_error *err;
564 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
565 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
567 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
569 cd0acaa7 2018-05-20 stsp return err;
571 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
572 9ba79e04 2018-06-11 stsp if (parent_id) {
573 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
578 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
581 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
583 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
584 cd0acaa7 2018-05-20 stsp return err;
587 80ddbec8 2018-04-29 stsp static const struct got_error *
588 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
590 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
591 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
593 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
595 9343a5fb 2018-06-23 stsp return err;
597 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
598 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
599 9343a5fb 2018-06-23 stsp return err;
602 9343a5fb 2018-06-23 stsp static const struct got_error *
603 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
605 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
606 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
607 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
608 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
609 0553a4e3 2018-04-30 stsp struct commit_queue commits;
610 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
611 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
612 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
613 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
615 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
616 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
617 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
618 80ddbec8 2018-04-29 stsp return got_error_from_errno();
619 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
621 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
622 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
623 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
624 80ddbec8 2018-04-29 stsp return got_error_from_errno();
626 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
628 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
630 9ba79e04 2018-06-11 stsp return err;
632 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
634 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
638 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
639 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
643 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
648 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
649 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
650 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
651 9ba79e04 2018-06-11 stsp * updating the display.
653 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
654 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
657 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
658 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
659 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
660 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
664 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
665 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
669 899d86c2 2018-05-10 stsp while (!done) {
670 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
671 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
675 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
676 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
677 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
678 80ddbec8 2018-04-29 stsp switch (ch) {
680 31120ada 2018-04-30 stsp if (errno) {
681 31120ada 2018-04-30 stsp err = got_error_from_errno();
689 80ddbec8 2018-04-29 stsp case KEY_UP:
690 80ddbec8 2018-04-29 stsp if (selected > 0)
691 80ddbec8 2018-04-29 stsp selected--;
692 8ec9698d 2018-05-10 stsp if (selected > 0)
694 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
696 48531068 2018-05-10 stsp case KEY_PPAGE:
697 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
698 dfc1d240 2018-05-10 stsp first_displayed_entry) {
699 dfc1d240 2018-05-10 stsp selected = 0;
702 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
706 80ddbec8 2018-04-29 stsp case KEY_DOWN:
707 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
708 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
709 15c91275 2018-05-20 stsp selected < nparents - 1) {
710 15c91275 2018-05-20 stsp selected++;
713 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
714 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
719 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
720 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
721 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
725 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
727 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
728 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
729 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
730 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
731 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
733 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
734 31120ada 2018-04-30 stsp if (selected > LINES)
735 31120ada 2018-04-30 stsp selected = LINES - 1;
737 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
739 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
742 9343a5fb 2018-06-23 stsp show_panel(tog_log_view.panel);
745 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
748 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
755 9ba79e04 2018-06-11 stsp free(head_id);
757 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
758 0553a4e3 2018-04-30 stsp free_commits(&commits);
759 80ddbec8 2018-04-29 stsp return err;
762 4ed7e80c 2018-05-20 stsp static const struct got_error *
763 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
765 80ddbec8 2018-04-29 stsp const struct got_error *error;
766 80ddbec8 2018-04-29 stsp struct got_repository *repo;
767 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
768 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
769 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
772 80ddbec8 2018-04-29 stsp #ifndef PROFILE
773 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
774 80ddbec8 2018-04-29 stsp err(1, "pledge");
777 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
778 80ddbec8 2018-04-29 stsp switch (ch) {
780 80ddbec8 2018-04-29 stsp start_commit = optarg;
784 80ddbec8 2018-04-29 stsp /* NOTREACHED */
788 80ddbec8 2018-04-29 stsp argc -= optind;
789 80ddbec8 2018-04-29 stsp argv += optind;
791 80ddbec8 2018-04-29 stsp if (argc == 0) {
792 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
793 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
794 80ddbec8 2018-04-29 stsp return got_error_from_errno();
795 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
796 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
797 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
798 80ddbec8 2018-04-29 stsp return got_error_from_errno();
800 80ddbec8 2018-04-29 stsp usage_log();
802 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
803 80ddbec8 2018-04-29 stsp free(repo_path);
804 80ddbec8 2018-04-29 stsp if (error != NULL)
805 80ddbec8 2018-04-29 stsp return error;
807 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
808 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
809 80ddbec8 2018-04-29 stsp if (error != NULL)
810 80ddbec8 2018-04-29 stsp return error;
812 80ddbec8 2018-04-29 stsp struct got_object *obj;
813 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
814 80ddbec8 2018-04-29 stsp if (error == NULL) {
815 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
816 899d86c2 2018-05-10 stsp if (start_id == NULL)
817 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
820 80ddbec8 2018-04-29 stsp if (error != NULL)
821 80ddbec8 2018-04-29 stsp return error;
822 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
823 899d86c2 2018-05-10 stsp free(start_id);
824 80ddbec8 2018-04-29 stsp got_repo_close(repo);
825 80ddbec8 2018-04-29 stsp return error;
828 4ed7e80c 2018-05-20 stsp __dead static void
829 9f7d7167 2018-04-29 stsp usage_diff(void)
832 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
833 9f7d7167 2018-04-29 stsp getprogname());
837 b304db33 2018-05-20 stsp static char *
838 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
840 b304db33 2018-05-20 stsp char *line;
841 b304db33 2018-05-20 stsp size_t linelen;
842 b304db33 2018-05-20 stsp size_t lineno;
843 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
845 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
847 b304db33 2018-05-20 stsp *len = linelen;
848 b304db33 2018-05-20 stsp return line;
851 4ed7e80c 2018-05-20 stsp static const struct got_error *
852 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
853 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
855 61e69b96 2018-05-20 stsp const struct got_error *err;
856 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
857 b304db33 2018-05-20 stsp char *line;
858 b304db33 2018-05-20 stsp size_t len;
859 61e69b96 2018-05-20 stsp wchar_t *wline;
863 a70480e0 2018-06-23 stsp werase(window);
866 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
867 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
868 26ed57b2 2018-05-19 stsp if (line == NULL) {
872 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
873 26ed57b2 2018-05-19 stsp free(line);
877 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
879 61e69b96 2018-05-20 stsp free(line);
880 61e69b96 2018-05-20 stsp return err;
882 a70480e0 2018-06-23 stsp waddwstr(window, wline);
883 e0b650dd 2018-05-20 stsp if (width < COLS)
884 a70480e0 2018-06-23 stsp waddch(window, '\n');
885 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
886 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
887 26ed57b2 2018-05-19 stsp free(line);
889 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
891 26ed57b2 2018-05-19 stsp update_panels();
892 26ed57b2 2018-05-19 stsp doupdate();
894 26ed57b2 2018-05-19 stsp return NULL;
897 4ed7e80c 2018-05-20 stsp static const struct got_error *
898 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
899 26ed57b2 2018-05-19 stsp struct got_repository *repo)
901 26ed57b2 2018-05-19 stsp const struct got_error *err;
903 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
904 b304db33 2018-05-20 stsp int eof, i;
906 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
907 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
908 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
910 511a516b 2018-05-19 stsp f = got_opentemp();
911 26ed57b2 2018-05-19 stsp if (f == NULL)
912 26ed57b2 2018-05-19 stsp return got_error_from_errno();
914 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
915 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
916 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
918 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
919 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
921 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
922 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
925 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
930 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
931 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
932 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
933 26ed57b2 2018-05-19 stsp return got_error_from_errno();
934 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
936 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
937 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
938 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
939 26ed57b2 2018-05-19 stsp return got_error_from_errno();
941 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
943 26ed57b2 2018-05-19 stsp while (!done) {
944 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
945 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
948 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
949 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
950 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
951 26ed57b2 2018-05-19 stsp switch (ch) {
956 26ed57b2 2018-05-19 stsp case KEY_UP:
957 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
958 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
959 b304db33 2018-05-20 stsp first_displayed_line--;
961 b304db33 2018-05-20 stsp case KEY_PPAGE:
963 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
964 d56caa55 2018-05-20 stsp first_displayed_line > 1)
965 26ed57b2 2018-05-19 stsp first_displayed_line--;
968 26ed57b2 2018-05-19 stsp case KEY_DOWN:
969 925e6f23 2018-05-20 stsp case KEY_ENTER:
972 26ed57b2 2018-05-19 stsp first_displayed_line++;
974 b304db33 2018-05-20 stsp case KEY_NPAGE:
977 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
978 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
979 b304db33 2018-05-20 stsp first_displayed_line++;
980 b304db33 2018-05-20 stsp if (line == NULL)
989 26ed57b2 2018-05-19 stsp return err;
992 4ed7e80c 2018-05-20 stsp static const struct got_error *
993 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
995 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
996 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
997 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
998 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
999 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1002 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1003 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1004 26ed57b2 2018-05-19 stsp err(1, "pledge");
1007 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1008 26ed57b2 2018-05-19 stsp switch (ch) {
1011 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1015 26ed57b2 2018-05-19 stsp argc -= optind;
1016 26ed57b2 2018-05-19 stsp argv += optind;
1018 26ed57b2 2018-05-19 stsp if (argc == 0) {
1019 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1020 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1021 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1022 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1023 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1024 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1025 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1026 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1027 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1028 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1029 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1030 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1031 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1033 26ed57b2 2018-05-19 stsp usage_diff();
1035 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1036 26ed57b2 2018-05-19 stsp free(repo_path);
1037 26ed57b2 2018-05-19 stsp if (error)
1038 26ed57b2 2018-05-19 stsp goto done;
1040 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1041 26ed57b2 2018-05-19 stsp if (error)
1042 26ed57b2 2018-05-19 stsp goto done;
1044 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1045 26ed57b2 2018-05-19 stsp if (error)
1046 26ed57b2 2018-05-19 stsp goto done;
1048 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1050 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1052 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1054 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1055 26ed57b2 2018-05-19 stsp return error;
1058 4ed7e80c 2018-05-20 stsp __dead static void
1059 9f7d7167 2018-04-29 stsp usage_blame(void)
1062 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1063 9f7d7167 2018-04-29 stsp getprogname());
1067 84451b3e 2018-07-10 stsp struct tog_blame_line {
1068 84451b3e 2018-07-10 stsp int annotated;
1069 84451b3e 2018-07-10 stsp struct got_object_id *id;
1072 4ed7e80c 2018-05-20 stsp static const struct got_error *
1073 3f60a8ef 2018-07-10 stsp draw_blame(WINDOW *window, FILE *f, const char *path,
1074 18430de3 2018-07-10 stsp struct tog_blame_line *lines, int nlines, int blame_complete,
1075 18430de3 2018-07-10 stsp int *first_displayed_line, int *last_displayed_line,
1076 18430de3 2018-07-10 stsp int *eof, int max_lines)
1078 84451b3e 2018-07-10 stsp const struct got_error *err;
1079 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1080 84451b3e 2018-07-10 stsp char *line;
1081 84451b3e 2018-07-10 stsp size_t len;
1082 84451b3e 2018-07-10 stsp wchar_t *wline;
1083 84451b3e 2018-07-10 stsp int width;
1084 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1086 84451b3e 2018-07-10 stsp rewind(f);
1087 84451b3e 2018-07-10 stsp werase(window);
1089 18430de3 2018-07-10 stsp if (asprintf(&line, "[%d-%d/%d] annotation of %s%s",
1090 18430de3 2018-07-10 stsp *first_displayed_line, *last_displayed_line, nlines,
1091 18430de3 2018-07-10 stsp path, blame_complete ? "" : " in progress...") == -1)
1092 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1093 3f60a8ef 2018-07-10 stsp err = format_line(&wline, &width, line, COLS);
1094 3f60a8ef 2018-07-10 stsp free(line);
1096 3f60a8ef 2018-07-10 stsp return err;
1097 3f60a8ef 2018-07-10 stsp waddwstr(window, wline);
1098 3f60a8ef 2018-07-10 stsp if (width < COLS)
1099 3f60a8ef 2018-07-10 stsp waddch(window, '\n');
1102 3f60a8ef 2018-07-10 stsp while (nprinted < max_lines - 1) {
1103 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1104 84451b3e 2018-07-10 stsp if (line == NULL) {
1108 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1109 84451b3e 2018-07-10 stsp free(line);
1113 84451b3e 2018-07-10 stsp err = format_line(&wline, &width, line, COLS - 9);
1114 84451b3e 2018-07-10 stsp if (err) {
1115 84451b3e 2018-07-10 stsp free(line);
1116 84451b3e 2018-07-10 stsp return err;
1119 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1120 84451b3e 2018-07-10 stsp if (blame_line->annotated) {
1121 84451b3e 2018-07-10 stsp char *id_str;
1122 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1123 84451b3e 2018-07-10 stsp if (err) {
1124 84451b3e 2018-07-10 stsp free(line);
1125 84451b3e 2018-07-10 stsp return err;
1127 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1128 84451b3e 2018-07-10 stsp free(id_str);
1130 84451b3e 2018-07-10 stsp waddstr(window, " ");
1132 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1133 84451b3e 2018-07-10 stsp if (width < COLS - 9)
1134 84451b3e 2018-07-10 stsp waddch(window, '\n');
1135 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1136 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1137 84451b3e 2018-07-10 stsp free(line);
1139 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1141 84451b3e 2018-07-10 stsp update_panels();
1142 84451b3e 2018-07-10 stsp doupdate();
1144 84451b3e 2018-07-10 stsp return NULL;
1147 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1148 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1149 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1150 84451b3e 2018-07-10 stsp int nlines;
1153 3f60a8ef 2018-07-10 stsp const char *path;
1154 84451b3e 2018-07-10 stsp WINDOW *window;
1155 84451b3e 2018-07-10 stsp int *first_displayed_line;
1156 84451b3e 2018-07-10 stsp int *last_displayed_line;
1157 18430de3 2018-07-10 stsp int *quit;
1160 84451b3e 2018-07-10 stsp static const struct got_error *
1161 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1163 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1164 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1165 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1168 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1169 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1170 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1172 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1173 84451b3e 2018-07-10 stsp return got_error_from_errno();
1175 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1176 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1177 d68a0a7d 2018-07-10 stsp goto done;
1180 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1181 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1183 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1184 d68a0a7d 2018-07-10 stsp if (line->annotated)
1185 d68a0a7d 2018-07-10 stsp goto done;
1187 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1188 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1189 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1190 84451b3e 2018-07-10 stsp goto done;
1192 84451b3e 2018-07-10 stsp line->annotated = 1;
1194 18430de3 2018-07-10 stsp err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 0,
1195 84451b3e 2018-07-10 stsp a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1197 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1198 84451b3e 2018-07-10 stsp return got_error_from_errno();
1199 84451b3e 2018-07-10 stsp return err;
1202 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1203 84451b3e 2018-07-10 stsp const char *path;
1204 84451b3e 2018-07-10 stsp struct got_object_id *commit_id;
1205 84451b3e 2018-07-10 stsp struct got_repository *repo;
1206 84451b3e 2018-07-10 stsp void *blame_cb_args;
1207 18430de3 2018-07-10 stsp int *complete;
1210 84451b3e 2018-07-10 stsp static void *
1211 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1213 18430de3 2018-07-10 stsp const struct got_error *err;
1214 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1215 18430de3 2018-07-10 stsp struct tog_blame_cb_args *a = ta->blame_cb_args;
1218 18430de3 2018-07-10 stsp err = got_blame_incremental(ta->path, ta->commit_id, ta->repo,
1219 18430de3 2018-07-10 stsp blame_cb, ta->blame_cb_args);
1220 18430de3 2018-07-10 stsp *ta->complete = 1;
1222 18430de3 2018-07-10 stsp return (void *)err;
1224 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1225 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1227 18430de3 2018-07-10 stsp err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 1,
1228 18430de3 2018-07-10 stsp a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1230 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1231 18430de3 2018-07-10 stsp err = got_error_from_errno();
1233 18430de3 2018-07-10 stsp return (void *)err;
1236 84451b3e 2018-07-10 stsp static const struct got_error *
1237 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1238 a70480e0 2018-06-23 stsp struct got_repository *repo)
1240 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1241 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1242 18430de3 2018-07-10 stsp int eof, i, blame_complete = 0;
1243 84451b3e 2018-07-10 stsp struct got_object *obj = NULL;
1244 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1245 84451b3e 2018-07-10 stsp FILE *f = NULL;
1246 67c4c153 2018-07-10 stsp size_t filesize, nlines = 0;
1247 84451b3e 2018-07-10 stsp struct tog_blame_line *lines = NULL;
1248 84451b3e 2018-07-10 stsp pthread_t thread = NULL;
1249 84451b3e 2018-07-10 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1250 84451b3e 2018-07-10 stsp struct tog_blame_cb_args blame_cb_args;
1251 84451b3e 2018-07-10 stsp struct tog_blame_thread_args blame_thread_args;
1253 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1255 84451b3e 2018-07-10 stsp goto done;
1256 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1257 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1258 84451b3e 2018-07-10 stsp got_object_close(obj);
1259 84451b3e 2018-07-10 stsp goto done;
1262 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1263 84451b3e 2018-07-10 stsp got_object_close(obj);
1265 a70480e0 2018-06-23 stsp goto done;
1266 84451b3e 2018-07-10 stsp f = got_opentemp();
1267 84451b3e 2018-07-10 stsp if (f == NULL) {
1268 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1269 84451b3e 2018-07-10 stsp goto done;
1271 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1273 84451b3e 2018-07-10 stsp goto done;
1275 84451b3e 2018-07-10 stsp lines = calloc(nlines, sizeof(*lines));
1276 84451b3e 2018-07-10 stsp if (lines == NULL) {
1277 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1278 84451b3e 2018-07-10 stsp goto done;
1281 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1282 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1283 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1284 a70480e0 2018-06-23 stsp return got_error_from_errno();
1285 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1287 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1288 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1289 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1290 a70480e0 2018-06-23 stsp return got_error_from_errno();
1292 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1294 84451b3e 2018-07-10 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1295 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1296 84451b3e 2018-07-10 stsp goto done;
1298 84451b3e 2018-07-10 stsp blame_cb_args.lines = lines;
1299 84451b3e 2018-07-10 stsp blame_cb_args.nlines = nlines;
1300 84451b3e 2018-07-10 stsp blame_cb_args.mutex = &mutex;
1301 84451b3e 2018-07-10 stsp blame_cb_args.f = f;
1302 3f60a8ef 2018-07-10 stsp blame_cb_args.path = path;
1303 84451b3e 2018-07-10 stsp blame_cb_args.window = tog_blame_view.window;
1304 84451b3e 2018-07-10 stsp blame_cb_args.first_displayed_line = &first_displayed_line;
1305 84451b3e 2018-07-10 stsp blame_cb_args.last_displayed_line = &last_displayed_line;
1306 18430de3 2018-07-10 stsp blame_cb_args.quit = &done;
1308 84451b3e 2018-07-10 stsp blame_thread_args.path = path;
1309 84451b3e 2018-07-10 stsp blame_thread_args.commit_id = commit_id;
1310 84451b3e 2018-07-10 stsp blame_thread_args.repo = repo;
1311 84451b3e 2018-07-10 stsp blame_thread_args.blame_cb_args = &blame_cb_args;
1312 18430de3 2018-07-10 stsp blame_thread_args.complete = &blame_complete;
1314 84451b3e 2018-07-10 stsp if (pthread_create(&thread, NULL, blame_thread,
1315 84451b3e 2018-07-10 stsp &blame_thread_args) != 0) {
1316 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1317 84451b3e 2018-07-10 stsp goto done;
1320 a70480e0 2018-06-23 stsp while (!done) {
1321 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1322 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1323 84451b3e 2018-07-10 stsp goto done;
1325 3f60a8ef 2018-07-10 stsp err = draw_blame(tog_blame_view.window, f, path, lines, nlines,
1326 18430de3 2018-07-10 stsp blame_complete, &first_displayed_line, &last_displayed_line,
1327 18430de3 2018-07-10 stsp &eof, LINES);
1328 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1329 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1330 84451b3e 2018-07-10 stsp goto done;
1334 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1335 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1336 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1337 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1338 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1339 84451b3e 2018-07-10 stsp goto done;
1341 a70480e0 2018-06-23 stsp switch (ch) {
1346 a70480e0 2018-06-23 stsp case KEY_UP:
1347 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1348 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1349 a70480e0 2018-06-23 stsp first_displayed_line--;
1351 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1353 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1354 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1355 a70480e0 2018-06-23 stsp first_displayed_line--;
1358 a70480e0 2018-06-23 stsp case KEY_DOWN:
1359 a70480e0 2018-06-23 stsp case KEY_ENTER:
1360 a70480e0 2018-06-23 stsp case '\r':
1362 a70480e0 2018-06-23 stsp first_displayed_line++;
1364 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1367 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1368 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1369 a70480e0 2018-06-23 stsp first_displayed_line++;
1370 a70480e0 2018-06-23 stsp if (line == NULL)
1377 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1378 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1379 84451b3e 2018-07-10 stsp goto done;
1383 67c4c153 2018-07-10 stsp if (thread) {
1384 67c4c153 2018-07-10 stsp if (pthread_join(thread, (void **)&err) != 0)
1385 67c4c153 2018-07-10 stsp err = got_error_from_errno();
1386 75b7a700 2018-07-10 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1387 75b7a700 2018-07-10 stsp err = NULL;
1390 84451b3e 2018-07-10 stsp got_object_blob_close(blob);
1392 84451b3e 2018-07-10 stsp fclose(f);
1393 67c4c153 2018-07-10 stsp for (i = 0; i < nlines; i++)
1394 67c4c153 2018-07-10 stsp free(lines[i].id);
1395 84451b3e 2018-07-10 stsp free(lines);
1396 a70480e0 2018-06-23 stsp return err;
1399 a70480e0 2018-06-23 stsp static const struct got_error *
1400 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1402 a70480e0 2018-06-23 stsp const struct got_error *error;
1403 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1404 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1405 a70480e0 2018-06-23 stsp char *path = NULL;
1406 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1407 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1410 a70480e0 2018-06-23 stsp #ifndef PROFILE
1411 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1412 a70480e0 2018-06-23 stsp err(1, "pledge");
1415 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1416 a70480e0 2018-06-23 stsp switch (ch) {
1418 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1422 a70480e0 2018-06-23 stsp /* NOTREACHED */
1426 a70480e0 2018-06-23 stsp argc -= optind;
1427 a70480e0 2018-06-23 stsp argv += optind;
1429 a70480e0 2018-06-23 stsp if (argc == 0) {
1430 a70480e0 2018-06-23 stsp usage_blame();
1431 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1432 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1433 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1434 a70480e0 2018-06-23 stsp return got_error_from_errno();
1435 a70480e0 2018-06-23 stsp path = argv[0];
1436 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1437 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1438 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1439 a70480e0 2018-06-23 stsp return got_error_from_errno();
1440 a70480e0 2018-06-23 stsp path = argv[1];
1442 a70480e0 2018-06-23 stsp usage_blame();
1444 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1445 a70480e0 2018-06-23 stsp free(repo_path);
1446 a70480e0 2018-06-23 stsp if (error != NULL)
1447 66b4983c 2018-06-23 stsp return error;
1449 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1450 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1451 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1452 a70480e0 2018-06-23 stsp if (error != NULL)
1453 66b4983c 2018-06-23 stsp goto done;
1454 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1455 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1457 a70480e0 2018-06-23 stsp struct got_object *obj;
1458 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1459 a70480e0 2018-06-23 stsp if (error != NULL)
1460 66b4983c 2018-06-23 stsp goto done;
1461 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1462 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1463 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1464 a19e88aa 2018-06-23 stsp got_object_close(obj);
1466 a19e88aa 2018-06-23 stsp if (error != NULL)
1467 a19e88aa 2018-06-23 stsp goto done;
1469 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1471 a70480e0 2018-06-23 stsp free(commit_id);
1473 a70480e0 2018-06-23 stsp got_repo_close(repo);
1474 a70480e0 2018-06-23 stsp return error;
1477 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1478 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1479 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1480 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1481 ce52c690 2018-06-23 stsp WINDOW *window, const char *label, const char *parent_path,
1482 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1484 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1485 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1486 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1487 ffd1d5e5 2018-06-23 stsp int width, n;
1489 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1491 ffd1d5e5 2018-06-23 stsp werase(window);
1493 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1494 ffd1d5e5 2018-06-23 stsp return NULL;
1496 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1498 ffd1d5e5 2018-06-23 stsp return err;
1499 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1500 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1501 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1502 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1503 ffd1d5e5 2018-06-23 stsp return NULL;
1504 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1506 ce52c690 2018-06-23 stsp return err;
1507 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1508 ce52c690 2018-06-23 stsp if (width < COLS)
1509 ce52c690 2018-06-23 stsp waddch(window, '\n');
1510 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1511 ffd1d5e5 2018-06-23 stsp return NULL;
1512 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1513 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1514 a1eca9bb 2018-06-23 stsp return NULL;
1516 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1517 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1518 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1519 ffd1d5e5 2018-06-23 stsp wstandout(window);
1520 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1522 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1523 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1524 ffd1d5e5 2018-06-23 stsp wstandend(window);
1525 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1526 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1527 ffd1d5e5 2018-06-23 stsp return NULL;
1531 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1532 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1535 ffd1d5e5 2018-06-23 stsp while (te) {
1536 ce52c690 2018-06-23 stsp char *line = NULL;
1537 ffd1d5e5 2018-06-23 stsp if (asprintf(&line, " %s%s",
1538 ffd1d5e5 2018-06-23 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1539 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1540 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1541 ffd1d5e5 2018-06-23 stsp if (err) {
1542 ffd1d5e5 2018-06-23 stsp free(line);
1545 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1546 ffd1d5e5 2018-06-23 stsp wstandout(window);
1547 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1549 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1550 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1551 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1552 ffd1d5e5 2018-06-23 stsp if (n == selected)
1553 ffd1d5e5 2018-06-23 stsp wstandend(window);
1554 ffd1d5e5 2018-06-23 stsp free(line);
1556 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1557 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1558 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1560 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1563 ffd1d5e5 2018-06-23 stsp return err;
1566 ffd1d5e5 2018-06-23 stsp static void
1567 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1568 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1570 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1573 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1576 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1577 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1578 ffd1d5e5 2018-06-23 stsp if (!isroot)
1579 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1583 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1584 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1585 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1586 ffd1d5e5 2018-06-23 stsp prev = te;
1587 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1589 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1590 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1592 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1593 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1596 ffd1d5e5 2018-06-23 stsp static void
1597 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1598 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1599 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1601 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1602 ffd1d5e5 2018-06-23 stsp int n = 0;
1604 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1607 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1608 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1610 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1611 ffd1d5e5 2018-06-23 stsp while (next) {
1612 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1613 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1615 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1619 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1620 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1621 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1622 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1623 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1624 ffd1d5e5 2018-06-23 stsp int selected;
1627 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1629 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1630 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1631 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1633 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1634 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1635 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1637 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1638 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1640 ce52c690 2018-06-23 stsp len += strlen(te->name);
1642 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1643 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1644 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1646 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1647 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1648 d9765a41 2018-06-23 stsp while (pt) {
1649 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1650 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1651 cb2ebc8a 2018-06-23 stsp goto done;
1653 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1654 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1655 cb2ebc8a 2018-06-23 stsp goto done;
1657 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1660 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1661 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1662 ce52c690 2018-06-23 stsp goto done;
1666 ce52c690 2018-06-23 stsp if (err) {
1667 ce52c690 2018-06-23 stsp free(*path);
1668 ce52c690 2018-06-23 stsp *path = NULL;
1670 ce52c690 2018-06-23 stsp return err;
1673 ce52c690 2018-06-23 stsp static const struct got_error *
1674 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1675 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1677 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1678 ce52c690 2018-06-23 stsp char *path;
1680 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
1682 ce52c690 2018-06-23 stsp return err;
1684 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
1685 cb2ebc8a 2018-06-23 stsp free(path);
1686 cb2ebc8a 2018-06-23 stsp return err;
1689 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1690 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1691 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
1693 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1694 ffd1d5e5 2018-06-23 stsp int ch, done = 0, selected = 0;
1695 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
1696 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
1697 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
1698 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
1699 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
1700 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
1701 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
1702 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
1704 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
1706 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
1707 ffd1d5e5 2018-06-23 stsp if (err != NULL)
1708 ffd1d5e5 2018-06-23 stsp goto done;
1710 ffd1d5e5 2018-06-23 stsp if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1711 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1712 ffd1d5e5 2018-06-23 stsp goto done;
1715 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
1716 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
1717 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
1718 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1719 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
1721 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
1722 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
1723 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
1724 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1726 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
1728 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
1729 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1730 ffd1d5e5 2018-06-23 stsp while (!done) {
1731 ce52c690 2018-06-23 stsp char *parent_path;
1732 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
1733 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
1734 ffd1d5e5 2018-06-23 stsp if (tree != root)
1735 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
1737 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
1739 ce52c690 2018-06-23 stsp goto done;
1741 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
1742 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
1743 ce52c690 2018-06-23 stsp tog_tree_view.window, tree_label, parent_path, entries,
1744 ce52c690 2018-06-23 stsp selected, LINES, tree == root);
1745 ce52c690 2018-06-23 stsp free(parent_path);
1749 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
1750 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
1751 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
1752 ffd1d5e5 2018-06-23 stsp switch (ch) {
1757 ffd1d5e5 2018-06-23 stsp case KEY_UP:
1758 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1759 ffd1d5e5 2018-06-23 stsp selected--;
1760 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1762 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
1763 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1765 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
1766 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
1767 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
1768 cf8f1261 2018-06-23 stsp if (tree != root)
1769 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
1770 ffd1d5e5 2018-06-23 stsp selected = 0;
1773 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
1774 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1777 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
1778 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
1779 ffd1d5e5 2018-06-23 stsp selected++;
1782 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
1783 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1785 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
1786 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
1787 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1788 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1790 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
1791 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
1792 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1794 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
1795 ffd1d5e5 2018-06-23 stsp case '\r':
1796 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
1797 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1798 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
1799 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
1800 ffd1d5e5 2018-06-23 stsp if (tree == root)
1802 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1803 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1804 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1805 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
1806 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
1807 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
1808 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
1809 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
1810 ffd1d5e5 2018-06-23 stsp free(parent);
1811 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
1812 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1813 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
1814 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
1815 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
1817 ffd1d5e5 2018-06-23 stsp goto done;
1818 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
1819 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
1820 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1821 ffd1d5e5 2018-06-23 stsp goto done;
1823 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
1824 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
1825 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
1826 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
1827 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
1828 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
1830 ffd1d5e5 2018-06-23 stsp tree = child;
1831 ffd1d5e5 2018-06-23 stsp selected = 0;
1832 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
1833 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
1834 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
1835 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
1837 ffd1d5e5 2018-06-23 stsp goto done;
1840 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
1841 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
1842 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1849 ffd1d5e5 2018-06-23 stsp free(tree_label);
1850 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
1851 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
1852 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1853 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1854 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1855 ffd1d5e5 2018-06-23 stsp free(parent);
1858 ffd1d5e5 2018-06-23 stsp if (tree != root)
1859 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1860 ffd1d5e5 2018-06-23 stsp return err;
1863 ffd1d5e5 2018-06-23 stsp __dead static void
1864 ffd1d5e5 2018-06-23 stsp usage_tree(void)
1867 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1868 ffd1d5e5 2018-06-23 stsp getprogname());
1872 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1873 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
1875 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
1876 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
1877 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
1878 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1879 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
1880 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
1881 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
1884 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
1885 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1886 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
1889 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1890 ffd1d5e5 2018-06-23 stsp switch (ch) {
1892 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
1896 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
1900 ffd1d5e5 2018-06-23 stsp argc -= optind;
1901 ffd1d5e5 2018-06-23 stsp argv += optind;
1903 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
1904 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1905 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1906 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1907 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
1908 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1909 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1910 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1912 ffd1d5e5 2018-06-23 stsp usage_log();
1914 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1915 ffd1d5e5 2018-06-23 stsp free(repo_path);
1916 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1917 ffd1d5e5 2018-06-23 stsp return error;
1919 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
1920 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
1921 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1922 ffd1d5e5 2018-06-23 stsp goto done;
1924 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
1925 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1926 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
1927 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
1928 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
1929 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
1932 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1933 ffd1d5e5 2018-06-23 stsp goto done;
1935 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
1936 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1937 ffd1d5e5 2018-06-23 stsp goto done;
1939 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1940 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1941 ffd1d5e5 2018-06-23 stsp goto done;
1943 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
1945 ffd1d5e5 2018-06-23 stsp free(commit_id);
1946 ffd1d5e5 2018-06-23 stsp if (commit)
1947 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
1949 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1951 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
1952 ffd1d5e5 2018-06-23 stsp return error;
1954 5c5136c5 2018-05-20 stsp static void
1955 9f7d7167 2018-04-29 stsp init_curses(void)
1957 9f7d7167 2018-04-29 stsp initscr();
1961 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1962 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1963 1f475ad8 2018-05-10 stsp curs_set(0);
1966 4ed7e80c 2018-05-20 stsp __dead static void
1967 9f7d7167 2018-04-29 stsp usage(void)
1971 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1972 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1973 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1974 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1975 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1980 c2301be8 2018-04-30 stsp static char **
1981 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1983 c2301be8 2018-04-30 stsp char **argv;
1984 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1986 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1987 c2301be8 2018-04-30 stsp if (argv == NULL)
1988 c2301be8 2018-04-30 stsp err(1, "calloc");
1989 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1990 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1991 c2301be8 2018-04-30 stsp err(1, "calloc");
1992 c2301be8 2018-04-30 stsp if (arg1) {
1993 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1994 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1995 c2301be8 2018-04-30 stsp err(1, "calloc");
1998 c2301be8 2018-04-30 stsp return argv;
2002 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2004 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2005 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2006 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2007 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2009 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2011 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2012 9f7d7167 2018-04-29 stsp switch (ch) {
2014 9f7d7167 2018-04-29 stsp hflag = 1;
2018 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2022 9f7d7167 2018-04-29 stsp argc -= optind;
2023 9f7d7167 2018-04-29 stsp argv += optind;
2024 9f7d7167 2018-04-29 stsp optind = 0;
2025 c2301be8 2018-04-30 stsp optreset = 1;
2027 c2301be8 2018-04-30 stsp if (argc == 0) {
2028 f29d3e89 2018-06-23 stsp if (hflag)
2030 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2031 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2032 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2037 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2038 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2039 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2040 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2041 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2042 9f7d7167 2018-04-29 stsp if (hflag)
2043 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2047 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2048 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2049 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2050 c2301be8 2018-04-30 stsp if (repo_path) {
2051 c2301be8 2018-04-30 stsp struct got_repository *repo;
2052 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2053 c2301be8 2018-04-30 stsp if (error == NULL)
2054 c2301be8 2018-04-30 stsp got_repo_close(repo);
2056 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2057 c2301be8 2018-04-30 stsp if (error) {
2058 f29d3e89 2018-06-23 stsp if (hflag) {
2059 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2060 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2064 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2065 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2066 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2067 ad7de8d9 2018-04-30 stsp free(repo_path);
2070 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2071 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2073 c2301be8 2018-04-30 stsp free(repo_path);
2077 5c5136c5 2018-05-20 stsp init_curses();
2079 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2080 9f7d7167 2018-04-29 stsp if (error)
2081 9f7d7167 2018-04-29 stsp goto done;
2084 c2301be8 2018-04-30 stsp free(cmd_argv);
2085 9f7d7167 2018-04-29 stsp if (error)
2086 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);