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 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
207 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
208 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
209 bb737323 2018-05-20 stsp int author_width, logmsg_width;
210 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
211 80ddbec8 2018-04-29 stsp char *line = NULL;
212 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
213 bb737323 2018-05-20 stsp size_t id_len;
214 bb737323 2018-05-20 stsp int col, limit;
215 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
216 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
217 9c2eaf34 2018-05-20 stsp const int avail = COLS;
219 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
221 80ddbec8 2018-04-29 stsp return err;
222 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
223 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
224 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
225 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
227 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
228 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
230 bb737323 2018-05-20 stsp col = limit + 1;
231 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
232 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
235 9c2eaf34 2018-05-20 stsp if (col > avail)
238 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
239 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
240 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
243 6d9fbc00 2018-04-29 stsp author = author0;
244 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
245 6d9fbc00 2018-04-29 stsp if (smallerthan)
246 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
248 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
250 6d9fbc00 2018-04-29 stsp *at = '\0';
252 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
253 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
256 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
257 bb737323 2018-05-20 stsp col += author_width;
258 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
259 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
261 bb737323 2018-05-20 stsp author_width++;
263 9c2eaf34 2018-05-20 stsp if (col > avail)
266 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
267 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
268 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
271 bb737323 2018-05-20 stsp logmsg = logmsg0;
272 bb737323 2018-05-20 stsp while (*logmsg == '\n')
274 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
275 bb737323 2018-05-20 stsp if (newline)
276 bb737323 2018-05-20 stsp *newline = '\0';
277 bb737323 2018-05-20 stsp limit = avail - col;
278 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
281 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
282 bb737323 2018-05-20 stsp col += logmsg_width;
283 bb737323 2018-05-20 stsp while (col <= avail) {
284 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
288 80ddbec8 2018-04-29 stsp free(logmsg0);
289 bb737323 2018-05-20 stsp free(wlogmsg);
290 6d9fbc00 2018-04-29 stsp free(author0);
291 bb737323 2018-05-20 stsp free(wauthor);
292 80ddbec8 2018-04-29 stsp free(line);
293 6d9fbc00 2018-04-29 stsp free(id_str);
294 80ddbec8 2018-04-29 stsp return err;
297 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
298 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
299 80ddbec8 2018-04-29 stsp struct got_object_id *id;
300 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
302 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
304 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
305 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
306 899d86c2 2018-05-10 stsp struct got_object_id *id)
308 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
310 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
311 80ddbec8 2018-04-29 stsp if (entry == NULL)
312 899d86c2 2018-05-10 stsp return NULL;
314 899d86c2 2018-05-10 stsp entry->id = id;
315 99db9666 2018-05-07 stsp entry->commit = commit;
316 899d86c2 2018-05-10 stsp return entry;
319 99db9666 2018-05-07 stsp static void
320 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
322 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
324 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
325 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
326 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
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 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
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 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
342 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
343 899d86c2 2018-05-10 stsp struct got_object_id *id;
344 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
346 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
348 c4972b91 2018-05-07 stsp return err;
350 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
351 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
352 9ba79e04 2018-06-11 stsp int nfetched;
354 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
355 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
356 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
357 9ba79e04 2018-06-11 stsp return err;
359 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
361 9ba79e04 2018-06-11 stsp return err;
364 9ba79e04 2018-06-11 stsp while (1) {
365 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
367 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
369 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
370 9ba79e04 2018-06-11 stsp err = NULL;
374 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
378 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
379 9ba79e04 2018-06-11 stsp if (entry == NULL) {
380 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
384 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
387 9ba79e04 2018-06-11 stsp return err;
390 99db9666 2018-05-07 stsp static const struct got_error *
391 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
392 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
393 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
395 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
396 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
398 9ba79e04 2018-06-11 stsp *pentry = NULL;
400 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
401 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
402 9ba79e04 2018-06-11 stsp int nfetched;
403 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
404 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
406 9ba79e04 2018-06-11 stsp return err;
409 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
410 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
412 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
413 9ba79e04 2018-06-11 stsp err = NULL;
414 9ba79e04 2018-06-11 stsp return err;
417 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
418 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
419 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
420 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
422 9ba79e04 2018-06-11 stsp return NULL;
425 899d86c2 2018-05-10 stsp static const struct got_error *
426 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
428 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
429 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
431 9ba79e04 2018-06-11 stsp *head_id = NULL;
433 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
435 99db9666 2018-05-07 stsp return err;
437 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
438 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
440 9ba79e04 2018-06-11 stsp *head_id = NULL;
441 99db9666 2018-05-07 stsp return err;
444 9ba79e04 2018-06-11 stsp return NULL;
447 0553a4e3 2018-04-30 stsp static const struct got_error *
448 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
449 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
451 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
452 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
453 0553a4e3 2018-04-30 stsp int ncommits = 0;
455 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
457 899d86c2 2018-05-10 stsp entry = first;
458 899d86c2 2018-05-10 stsp *last = first;
459 899d86c2 2018-05-10 stsp while (entry) {
460 899d86c2 2018-05-10 stsp if (ncommits == limit)
462 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
463 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
464 cd0acaa7 2018-05-20 stsp *selected = entry;
466 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
467 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
468 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
471 0553a4e3 2018-04-30 stsp ncommits++;
472 899d86c2 2018-05-10 stsp *last = entry;
473 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
476 80ddbec8 2018-04-29 stsp update_panels();
477 80ddbec8 2018-04-29 stsp doupdate();
479 80ddbec8 2018-04-29 stsp return err;
482 07b55e75 2018-05-10 stsp static void
483 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
484 07b55e75 2018-05-10 stsp struct commit_queue *commits)
486 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
487 07b55e75 2018-05-10 stsp int nscrolled = 0;
489 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
490 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
493 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
494 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
495 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
496 07b55e75 2018-05-10 stsp if (entry) {
497 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
498 07b55e75 2018-05-10 stsp nscrolled++;
503 aa075928 2018-05-10 stsp static const struct got_error *
504 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
505 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
506 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
507 9ba79e04 2018-06-11 stsp struct got_repository *repo)
509 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
510 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
511 aa075928 2018-05-10 stsp int nscrolled = 0;
514 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
515 aa075928 2018-05-10 stsp if (pentry == NULL) {
516 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
517 9ba79e04 2018-06-11 stsp commits, graph, repo);
518 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
521 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
523 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
524 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
526 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
527 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
529 dd0a52c1 2018-05-20 stsp return err;
533 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
535 4a7f7875 2018-05-10 stsp int nparents = 0;
537 4a7f7875 2018-05-10 stsp while (entry) {
538 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
539 4a7f7875 2018-05-10 stsp nparents++;
542 4a7f7875 2018-05-10 stsp return nparents;
545 cd0acaa7 2018-05-20 stsp static const struct got_error *
546 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
548 cd0acaa7 2018-05-20 stsp const struct got_error *err;
549 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
550 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
552 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
554 cd0acaa7 2018-05-20 stsp return err;
556 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
557 9ba79e04 2018-06-11 stsp if (parent_id) {
558 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
563 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
566 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
568 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
569 cd0acaa7 2018-05-20 stsp return err;
572 80ddbec8 2018-04-29 stsp static const struct got_error *
573 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
575 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
576 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
578 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
580 9343a5fb 2018-06-23 stsp return err;
582 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
583 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
584 9343a5fb 2018-06-23 stsp return err;
587 9343a5fb 2018-06-23 stsp static const struct got_error *
588 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
590 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
591 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
592 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
593 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
594 0553a4e3 2018-04-30 stsp struct commit_queue commits;
595 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
596 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
597 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
598 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
600 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
601 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
602 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
603 80ddbec8 2018-04-29 stsp return got_error_from_errno();
604 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
606 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
607 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
608 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
609 80ddbec8 2018-04-29 stsp return got_error_from_errno();
611 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
613 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
615 9ba79e04 2018-06-11 stsp return err;
617 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
619 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
623 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
624 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
628 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
633 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
634 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
635 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
636 9ba79e04 2018-06-11 stsp * updating the display.
638 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
639 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
642 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
643 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
644 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
645 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
649 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
650 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
654 899d86c2 2018-05-10 stsp while (!done) {
655 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
656 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
660 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
661 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
662 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
663 80ddbec8 2018-04-29 stsp switch (ch) {
665 31120ada 2018-04-30 stsp if (errno) {
666 31120ada 2018-04-30 stsp err = got_error_from_errno();
674 80ddbec8 2018-04-29 stsp case KEY_UP:
675 80ddbec8 2018-04-29 stsp if (selected > 0)
676 80ddbec8 2018-04-29 stsp selected--;
677 8ec9698d 2018-05-10 stsp if (selected > 0)
679 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
681 48531068 2018-05-10 stsp case KEY_PPAGE:
682 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
683 dfc1d240 2018-05-10 stsp first_displayed_entry) {
684 dfc1d240 2018-05-10 stsp selected = 0;
687 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
691 80ddbec8 2018-04-29 stsp case KEY_DOWN:
692 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
693 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
694 15c91275 2018-05-20 stsp selected < nparents - 1) {
695 15c91275 2018-05-20 stsp selected++;
698 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
699 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
704 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
705 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
706 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
710 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
712 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
713 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
714 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
715 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
716 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
718 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
719 31120ada 2018-04-30 stsp if (selected > LINES)
720 31120ada 2018-04-30 stsp selected = LINES - 1;
722 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
724 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
727 9343a5fb 2018-06-23 stsp show_panel(tog_log_view.panel);
730 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
733 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
740 9ba79e04 2018-06-11 stsp free(head_id);
742 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
743 0553a4e3 2018-04-30 stsp free_commits(&commits);
744 80ddbec8 2018-04-29 stsp return err;
747 4ed7e80c 2018-05-20 stsp static const struct got_error *
748 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
750 80ddbec8 2018-04-29 stsp const struct got_error *error;
751 80ddbec8 2018-04-29 stsp struct got_repository *repo;
752 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
753 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
754 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
757 80ddbec8 2018-04-29 stsp #ifndef PROFILE
758 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
759 80ddbec8 2018-04-29 stsp err(1, "pledge");
762 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
763 80ddbec8 2018-04-29 stsp switch (ch) {
765 80ddbec8 2018-04-29 stsp start_commit = optarg;
769 80ddbec8 2018-04-29 stsp /* NOTREACHED */
773 80ddbec8 2018-04-29 stsp argc -= optind;
774 80ddbec8 2018-04-29 stsp argv += optind;
776 80ddbec8 2018-04-29 stsp if (argc == 0) {
777 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
778 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
779 80ddbec8 2018-04-29 stsp return got_error_from_errno();
780 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
781 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
782 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
783 80ddbec8 2018-04-29 stsp return got_error_from_errno();
785 80ddbec8 2018-04-29 stsp usage_log();
787 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
788 80ddbec8 2018-04-29 stsp free(repo_path);
789 80ddbec8 2018-04-29 stsp if (error != NULL)
790 80ddbec8 2018-04-29 stsp return error;
792 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
793 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
794 80ddbec8 2018-04-29 stsp if (error != NULL)
795 80ddbec8 2018-04-29 stsp return error;
797 80ddbec8 2018-04-29 stsp struct got_object *obj;
798 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
799 80ddbec8 2018-04-29 stsp if (error == NULL) {
800 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
801 899d86c2 2018-05-10 stsp if (start_id == NULL)
802 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
805 80ddbec8 2018-04-29 stsp if (error != NULL)
806 80ddbec8 2018-04-29 stsp return error;
807 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
808 899d86c2 2018-05-10 stsp free(start_id);
809 80ddbec8 2018-04-29 stsp got_repo_close(repo);
810 80ddbec8 2018-04-29 stsp return error;
813 4ed7e80c 2018-05-20 stsp __dead static void
814 9f7d7167 2018-04-29 stsp usage_diff(void)
817 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
818 9f7d7167 2018-04-29 stsp getprogname());
822 b304db33 2018-05-20 stsp static char *
823 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
825 b304db33 2018-05-20 stsp char *line;
826 b304db33 2018-05-20 stsp size_t linelen;
827 b304db33 2018-05-20 stsp size_t lineno;
828 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
830 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
832 b304db33 2018-05-20 stsp *len = linelen;
833 b304db33 2018-05-20 stsp return line;
836 4ed7e80c 2018-05-20 stsp static const struct got_error *
837 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
838 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
840 61e69b96 2018-05-20 stsp const struct got_error *err;
841 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
842 b304db33 2018-05-20 stsp char *line;
843 b304db33 2018-05-20 stsp size_t len;
844 61e69b96 2018-05-20 stsp wchar_t *wline;
848 a70480e0 2018-06-23 stsp werase(window);
851 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
852 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
853 26ed57b2 2018-05-19 stsp if (line == NULL) {
857 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
858 26ed57b2 2018-05-19 stsp free(line);
862 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
864 61e69b96 2018-05-20 stsp free(line);
865 61e69b96 2018-05-20 stsp return err;
867 a70480e0 2018-06-23 stsp waddwstr(window, wline);
868 e0b650dd 2018-05-20 stsp if (width < COLS)
869 a70480e0 2018-06-23 stsp waddch(window, '\n');
870 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
871 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
872 26ed57b2 2018-05-19 stsp free(line);
874 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
876 26ed57b2 2018-05-19 stsp update_panels();
877 26ed57b2 2018-05-19 stsp doupdate();
879 26ed57b2 2018-05-19 stsp return NULL;
882 4ed7e80c 2018-05-20 stsp static const struct got_error *
883 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
884 26ed57b2 2018-05-19 stsp struct got_repository *repo)
886 26ed57b2 2018-05-19 stsp const struct got_error *err;
888 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
889 b304db33 2018-05-20 stsp int eof, i;
891 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
892 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
893 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
895 511a516b 2018-05-19 stsp f = got_opentemp();
896 26ed57b2 2018-05-19 stsp if (f == NULL)
897 26ed57b2 2018-05-19 stsp return got_error_from_errno();
899 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
900 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
901 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
903 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
904 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
906 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
907 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
910 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
915 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
916 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
917 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
918 26ed57b2 2018-05-19 stsp return got_error_from_errno();
919 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
921 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
922 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
923 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
924 26ed57b2 2018-05-19 stsp return got_error_from_errno();
926 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
928 26ed57b2 2018-05-19 stsp while (!done) {
929 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
930 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
933 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
934 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
935 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
936 26ed57b2 2018-05-19 stsp switch (ch) {
941 26ed57b2 2018-05-19 stsp case KEY_UP:
942 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
943 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
944 b304db33 2018-05-20 stsp first_displayed_line--;
946 b304db33 2018-05-20 stsp case KEY_PPAGE:
948 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
949 d56caa55 2018-05-20 stsp first_displayed_line > 1)
950 26ed57b2 2018-05-19 stsp first_displayed_line--;
953 26ed57b2 2018-05-19 stsp case KEY_DOWN:
954 925e6f23 2018-05-20 stsp case KEY_ENTER:
957 26ed57b2 2018-05-19 stsp first_displayed_line++;
959 b304db33 2018-05-20 stsp case KEY_NPAGE:
962 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
963 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
964 b304db33 2018-05-20 stsp first_displayed_line++;
965 b304db33 2018-05-20 stsp if (line == NULL)
974 26ed57b2 2018-05-19 stsp return err;
977 4ed7e80c 2018-05-20 stsp static const struct got_error *
978 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
980 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
981 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
982 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
983 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
984 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
987 26ed57b2 2018-05-19 stsp #ifndef PROFILE
988 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
989 26ed57b2 2018-05-19 stsp err(1, "pledge");
992 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
993 26ed57b2 2018-05-19 stsp switch (ch) {
996 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1000 26ed57b2 2018-05-19 stsp argc -= optind;
1001 26ed57b2 2018-05-19 stsp argv += optind;
1003 26ed57b2 2018-05-19 stsp if (argc == 0) {
1004 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1005 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1006 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1007 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1008 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1009 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1010 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1011 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1012 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1013 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1014 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1015 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1016 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1018 26ed57b2 2018-05-19 stsp usage_diff();
1020 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1021 26ed57b2 2018-05-19 stsp free(repo_path);
1022 26ed57b2 2018-05-19 stsp if (error)
1023 26ed57b2 2018-05-19 stsp goto done;
1025 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1026 26ed57b2 2018-05-19 stsp if (error)
1027 26ed57b2 2018-05-19 stsp goto done;
1029 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1030 26ed57b2 2018-05-19 stsp if (error)
1031 26ed57b2 2018-05-19 stsp goto done;
1033 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1035 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1037 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1039 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1040 26ed57b2 2018-05-19 stsp return error;
1043 4ed7e80c 2018-05-20 stsp __dead static void
1044 9f7d7167 2018-04-29 stsp usage_blame(void)
1047 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1048 9f7d7167 2018-04-29 stsp getprogname());
1052 84451b3e 2018-07-10 stsp struct tog_blame_line {
1053 84451b3e 2018-07-10 stsp int annotated;
1054 84451b3e 2018-07-10 stsp struct got_object_id *id;
1057 4ed7e80c 2018-05-20 stsp static const struct got_error *
1058 84451b3e 2018-07-10 stsp draw_blame(WINDOW *window, FILE *f, struct tog_blame_line *lines, int nlines,
1059 84451b3e 2018-07-10 stsp int *first_displayed_line, int *last_displayed_line, int *eof,
1060 84451b3e 2018-07-10 stsp int max_lines)
1062 84451b3e 2018-07-10 stsp const struct got_error *err;
1063 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1064 84451b3e 2018-07-10 stsp char *line;
1065 84451b3e 2018-07-10 stsp size_t len;
1066 84451b3e 2018-07-10 stsp wchar_t *wline;
1067 84451b3e 2018-07-10 stsp int width;
1068 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1070 84451b3e 2018-07-10 stsp rewind(f);
1071 84451b3e 2018-07-10 stsp werase(window);
1074 84451b3e 2018-07-10 stsp while (nprinted < max_lines) {
1075 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1076 84451b3e 2018-07-10 stsp if (line == NULL) {
1080 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1081 84451b3e 2018-07-10 stsp free(line);
1085 84451b3e 2018-07-10 stsp err = format_line(&wline, &width, line, COLS - 9);
1086 84451b3e 2018-07-10 stsp if (err) {
1087 84451b3e 2018-07-10 stsp free(line);
1088 84451b3e 2018-07-10 stsp return err;
1091 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1092 84451b3e 2018-07-10 stsp if (blame_line->annotated) {
1093 84451b3e 2018-07-10 stsp char *id_str;
1094 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1095 84451b3e 2018-07-10 stsp if (err) {
1096 84451b3e 2018-07-10 stsp free(line);
1097 84451b3e 2018-07-10 stsp return err;
1099 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1100 84451b3e 2018-07-10 stsp free(id_str);
1102 84451b3e 2018-07-10 stsp waddstr(window, " ");
1104 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1105 84451b3e 2018-07-10 stsp if (width < COLS - 9)
1106 84451b3e 2018-07-10 stsp waddch(window, '\n');
1107 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1108 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1109 84451b3e 2018-07-10 stsp free(line);
1111 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1113 84451b3e 2018-07-10 stsp update_panels();
1114 84451b3e 2018-07-10 stsp doupdate();
1116 84451b3e 2018-07-10 stsp return NULL;
1119 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1120 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1121 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1122 84451b3e 2018-07-10 stsp int nlines;
1125 84451b3e 2018-07-10 stsp WINDOW *window;
1126 84451b3e 2018-07-10 stsp int *first_displayed_line;
1127 84451b3e 2018-07-10 stsp int *last_displayed_line;
1128 75b7a700 2018-07-10 stsp int *done;
1131 84451b3e 2018-07-10 stsp static const struct got_error *
1132 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1134 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1135 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1136 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1139 75b7a700 2018-07-10 stsp if (*a->done)
1140 75b7a700 2018-07-10 stsp return got_error(GOT_ERR_ITER_COMPLETED);
1142 84451b3e 2018-07-10 stsp if (nlines != a->nlines || lineno < 1 || lineno > a->nlines)
1143 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1145 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1146 84451b3e 2018-07-10 stsp return got_error_from_errno();
1148 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1149 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1150 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1151 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1152 84451b3e 2018-07-10 stsp goto done;
1154 84451b3e 2018-07-10 stsp line->annotated = 1;
1156 84451b3e 2018-07-10 stsp err = draw_blame(a->window, a->f, a->lines, a->nlines,
1157 84451b3e 2018-07-10 stsp a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1159 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1160 84451b3e 2018-07-10 stsp return got_error_from_errno();
1161 84451b3e 2018-07-10 stsp return err;
1164 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1165 84451b3e 2018-07-10 stsp const char *path;
1166 84451b3e 2018-07-10 stsp struct got_object_id *commit_id;
1167 84451b3e 2018-07-10 stsp struct got_repository *repo;
1168 84451b3e 2018-07-10 stsp void *blame_cb_args;
1171 84451b3e 2018-07-10 stsp static void *
1172 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1174 84451b3e 2018-07-10 stsp struct tog_blame_thread_args *a = arg;
1175 84451b3e 2018-07-10 stsp return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
1176 84451b3e 2018-07-10 stsp blame_cb, a->blame_cb_args);
1179 84451b3e 2018-07-10 stsp static const struct got_error *
1180 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1181 a70480e0 2018-06-23 stsp struct got_repository *repo)
1183 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1184 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1185 a70480e0 2018-06-23 stsp int eof, i;
1186 84451b3e 2018-07-10 stsp struct got_object *obj = NULL;
1187 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1188 84451b3e 2018-07-10 stsp FILE *f = NULL;
1189 67c4c153 2018-07-10 stsp size_t filesize, nlines = 0;
1190 84451b3e 2018-07-10 stsp struct tog_blame_line *lines = NULL;
1191 84451b3e 2018-07-10 stsp pthread_t thread = NULL;
1192 84451b3e 2018-07-10 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1193 84451b3e 2018-07-10 stsp struct tog_blame_cb_args blame_cb_args;
1194 84451b3e 2018-07-10 stsp struct tog_blame_thread_args blame_thread_args;
1196 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1198 84451b3e 2018-07-10 stsp goto done;
1199 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1200 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1201 84451b3e 2018-07-10 stsp got_object_close(obj);
1202 84451b3e 2018-07-10 stsp goto done;
1205 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1206 84451b3e 2018-07-10 stsp got_object_close(obj);
1208 a70480e0 2018-06-23 stsp goto done;
1209 84451b3e 2018-07-10 stsp f = got_opentemp();
1210 84451b3e 2018-07-10 stsp if (f == NULL) {
1211 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1212 84451b3e 2018-07-10 stsp goto done;
1214 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1216 84451b3e 2018-07-10 stsp goto done;
1218 84451b3e 2018-07-10 stsp lines = calloc(nlines, sizeof(*lines));
1219 84451b3e 2018-07-10 stsp if (lines == NULL) {
1220 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1221 84451b3e 2018-07-10 stsp goto done;
1224 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1225 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1226 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1227 a70480e0 2018-06-23 stsp return got_error_from_errno();
1228 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1230 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1231 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1232 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1233 a70480e0 2018-06-23 stsp return got_error_from_errno();
1235 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1237 84451b3e 2018-07-10 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1238 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1239 84451b3e 2018-07-10 stsp goto done;
1241 84451b3e 2018-07-10 stsp blame_cb_args.lines = lines;
1242 84451b3e 2018-07-10 stsp blame_cb_args.nlines = nlines;
1243 84451b3e 2018-07-10 stsp blame_cb_args.mutex = &mutex;
1244 84451b3e 2018-07-10 stsp blame_cb_args.f = f;
1245 84451b3e 2018-07-10 stsp blame_cb_args.window = tog_blame_view.window;
1246 84451b3e 2018-07-10 stsp blame_cb_args.first_displayed_line = &first_displayed_line;
1247 84451b3e 2018-07-10 stsp blame_cb_args.last_displayed_line = &last_displayed_line;
1248 75b7a700 2018-07-10 stsp blame_cb_args.done = &done;
1250 84451b3e 2018-07-10 stsp blame_thread_args.path = path;
1251 84451b3e 2018-07-10 stsp blame_thread_args.commit_id = commit_id;
1252 84451b3e 2018-07-10 stsp blame_thread_args.repo = repo;
1253 84451b3e 2018-07-10 stsp blame_thread_args.blame_cb_args = &blame_cb_args;
1255 84451b3e 2018-07-10 stsp if (pthread_create(&thread, NULL, blame_thread,
1256 84451b3e 2018-07-10 stsp &blame_thread_args) != 0) {
1257 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1258 84451b3e 2018-07-10 stsp goto done;
1261 a70480e0 2018-06-23 stsp while (!done) {
1262 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1263 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1264 84451b3e 2018-07-10 stsp goto done;
1266 84451b3e 2018-07-10 stsp err = draw_blame(tog_blame_view.window, f, lines, nlines,
1267 84451b3e 2018-07-10 stsp &first_displayed_line, &last_displayed_line, &eof, LINES);
1268 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1269 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1270 84451b3e 2018-07-10 stsp goto done;
1274 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1275 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1276 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1277 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1278 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1279 84451b3e 2018-07-10 stsp goto done;
1281 a70480e0 2018-06-23 stsp switch (ch) {
1286 a70480e0 2018-06-23 stsp case KEY_UP:
1287 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1288 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1289 a70480e0 2018-06-23 stsp first_displayed_line--;
1291 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1293 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1294 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1295 a70480e0 2018-06-23 stsp first_displayed_line--;
1298 a70480e0 2018-06-23 stsp case KEY_DOWN:
1299 a70480e0 2018-06-23 stsp case KEY_ENTER:
1300 a70480e0 2018-06-23 stsp case '\r':
1302 a70480e0 2018-06-23 stsp first_displayed_line++;
1304 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1307 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1308 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1309 a70480e0 2018-06-23 stsp first_displayed_line++;
1310 a70480e0 2018-06-23 stsp if (line == NULL)
1317 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1318 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1319 84451b3e 2018-07-10 stsp goto done;
1323 67c4c153 2018-07-10 stsp if (thread) {
1324 67c4c153 2018-07-10 stsp if (pthread_join(thread, (void **)&err) != 0)
1325 67c4c153 2018-07-10 stsp err = got_error_from_errno();
1326 75b7a700 2018-07-10 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1327 75b7a700 2018-07-10 stsp err = NULL;
1330 84451b3e 2018-07-10 stsp got_object_blob_close(blob);
1332 84451b3e 2018-07-10 stsp fclose(f);
1333 67c4c153 2018-07-10 stsp for (i = 0; i < nlines; i++)
1334 67c4c153 2018-07-10 stsp free(lines[i].id);
1335 84451b3e 2018-07-10 stsp free(lines);
1336 a70480e0 2018-06-23 stsp return err;
1339 a70480e0 2018-06-23 stsp static const struct got_error *
1340 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1342 a70480e0 2018-06-23 stsp const struct got_error *error;
1343 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1344 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1345 a70480e0 2018-06-23 stsp char *path = NULL;
1346 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1347 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1350 a70480e0 2018-06-23 stsp #ifndef PROFILE
1351 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1352 a70480e0 2018-06-23 stsp err(1, "pledge");
1355 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1356 a70480e0 2018-06-23 stsp switch (ch) {
1358 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1362 a70480e0 2018-06-23 stsp /* NOTREACHED */
1366 a70480e0 2018-06-23 stsp argc -= optind;
1367 a70480e0 2018-06-23 stsp argv += optind;
1369 a70480e0 2018-06-23 stsp if (argc == 0) {
1370 a70480e0 2018-06-23 stsp usage_blame();
1371 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1372 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1373 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1374 a70480e0 2018-06-23 stsp return got_error_from_errno();
1375 a70480e0 2018-06-23 stsp path = argv[0];
1376 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1377 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1378 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1379 a70480e0 2018-06-23 stsp return got_error_from_errno();
1380 a70480e0 2018-06-23 stsp path = argv[1];
1382 a70480e0 2018-06-23 stsp usage_blame();
1384 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1385 a70480e0 2018-06-23 stsp free(repo_path);
1386 a70480e0 2018-06-23 stsp if (error != NULL)
1387 66b4983c 2018-06-23 stsp return error;
1389 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1390 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1391 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1392 a70480e0 2018-06-23 stsp if (error != NULL)
1393 66b4983c 2018-06-23 stsp goto done;
1394 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1395 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1397 a70480e0 2018-06-23 stsp struct got_object *obj;
1398 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1399 a70480e0 2018-06-23 stsp if (error != NULL)
1400 66b4983c 2018-06-23 stsp goto done;
1401 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1402 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1403 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1404 a19e88aa 2018-06-23 stsp got_object_close(obj);
1406 a19e88aa 2018-06-23 stsp if (error != NULL)
1407 a19e88aa 2018-06-23 stsp goto done;
1409 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1411 a70480e0 2018-06-23 stsp free(commit_id);
1413 a70480e0 2018-06-23 stsp got_repo_close(repo);
1414 a70480e0 2018-06-23 stsp return error;
1417 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1418 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1419 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1420 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1421 ce52c690 2018-06-23 stsp WINDOW *window, const char *label, const char *parent_path,
1422 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1424 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1425 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1426 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1427 ffd1d5e5 2018-06-23 stsp int width, n;
1429 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1431 ffd1d5e5 2018-06-23 stsp werase(window);
1433 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1434 ffd1d5e5 2018-06-23 stsp return NULL;
1436 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1438 ffd1d5e5 2018-06-23 stsp return err;
1439 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1440 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1441 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1442 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1443 ffd1d5e5 2018-06-23 stsp return NULL;
1444 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1446 ce52c690 2018-06-23 stsp return err;
1447 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1448 ce52c690 2018-06-23 stsp if (width < COLS)
1449 ce52c690 2018-06-23 stsp waddch(window, '\n');
1450 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1451 ffd1d5e5 2018-06-23 stsp return NULL;
1452 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1453 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1454 a1eca9bb 2018-06-23 stsp return NULL;
1456 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1457 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1458 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1459 ffd1d5e5 2018-06-23 stsp wstandout(window);
1460 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1462 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1463 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1464 ffd1d5e5 2018-06-23 stsp wstandend(window);
1465 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1466 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1467 ffd1d5e5 2018-06-23 stsp return NULL;
1471 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1472 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1475 ffd1d5e5 2018-06-23 stsp while (te) {
1476 ce52c690 2018-06-23 stsp char *line = NULL;
1477 ffd1d5e5 2018-06-23 stsp if (asprintf(&line, " %s%s",
1478 ffd1d5e5 2018-06-23 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1479 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1480 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1481 ffd1d5e5 2018-06-23 stsp if (err) {
1482 ffd1d5e5 2018-06-23 stsp free(line);
1485 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1486 ffd1d5e5 2018-06-23 stsp wstandout(window);
1487 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1489 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1490 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1491 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1492 ffd1d5e5 2018-06-23 stsp if (n == selected)
1493 ffd1d5e5 2018-06-23 stsp wstandend(window);
1494 ffd1d5e5 2018-06-23 stsp free(line);
1496 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1497 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1498 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1500 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1503 ffd1d5e5 2018-06-23 stsp return err;
1506 ffd1d5e5 2018-06-23 stsp static void
1507 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1508 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1510 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1513 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1516 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1517 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1518 ffd1d5e5 2018-06-23 stsp if (!isroot)
1519 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1523 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1524 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1525 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1526 ffd1d5e5 2018-06-23 stsp prev = te;
1527 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1529 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1530 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1532 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1533 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1536 ffd1d5e5 2018-06-23 stsp static void
1537 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1538 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1539 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1541 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1542 ffd1d5e5 2018-06-23 stsp int n = 0;
1544 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1547 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1548 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1550 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1551 ffd1d5e5 2018-06-23 stsp while (next) {
1552 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1553 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1555 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1559 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1560 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1561 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1562 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1563 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1564 ffd1d5e5 2018-06-23 stsp int selected;
1567 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1569 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1570 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1571 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1573 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1574 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1575 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1577 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1578 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1580 ce52c690 2018-06-23 stsp len += strlen(te->name);
1582 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1583 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1584 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1586 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1587 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1588 d9765a41 2018-06-23 stsp while (pt) {
1589 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1590 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1591 cb2ebc8a 2018-06-23 stsp goto done;
1593 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1594 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1595 cb2ebc8a 2018-06-23 stsp goto done;
1597 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1600 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1601 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1602 ce52c690 2018-06-23 stsp goto done;
1606 ce52c690 2018-06-23 stsp if (err) {
1607 ce52c690 2018-06-23 stsp free(*path);
1608 ce52c690 2018-06-23 stsp *path = NULL;
1610 ce52c690 2018-06-23 stsp return err;
1613 ce52c690 2018-06-23 stsp static const struct got_error *
1614 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1615 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1617 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1618 ce52c690 2018-06-23 stsp char *path;
1620 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
1622 ce52c690 2018-06-23 stsp return err;
1624 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
1625 cb2ebc8a 2018-06-23 stsp free(path);
1626 cb2ebc8a 2018-06-23 stsp return err;
1629 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1630 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1631 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
1633 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1634 ffd1d5e5 2018-06-23 stsp int ch, done = 0, selected = 0;
1635 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
1636 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
1637 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
1638 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
1639 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
1640 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
1641 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
1642 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
1644 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
1646 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
1647 ffd1d5e5 2018-06-23 stsp if (err != NULL)
1648 ffd1d5e5 2018-06-23 stsp goto done;
1650 ffd1d5e5 2018-06-23 stsp if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1651 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1652 ffd1d5e5 2018-06-23 stsp goto done;
1655 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
1656 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
1657 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
1658 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1659 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
1661 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
1662 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
1663 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
1664 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1666 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
1668 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
1669 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1670 ffd1d5e5 2018-06-23 stsp while (!done) {
1671 ce52c690 2018-06-23 stsp char *parent_path;
1672 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
1673 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
1674 ffd1d5e5 2018-06-23 stsp if (tree != root)
1675 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
1677 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
1679 ce52c690 2018-06-23 stsp goto done;
1681 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
1682 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
1683 ce52c690 2018-06-23 stsp tog_tree_view.window, tree_label, parent_path, entries,
1684 ce52c690 2018-06-23 stsp selected, LINES, tree == root);
1685 ce52c690 2018-06-23 stsp free(parent_path);
1689 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
1690 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
1691 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
1692 ffd1d5e5 2018-06-23 stsp switch (ch) {
1697 ffd1d5e5 2018-06-23 stsp case KEY_UP:
1698 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1699 ffd1d5e5 2018-06-23 stsp selected--;
1700 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1702 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
1703 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1705 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
1706 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
1707 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
1708 cf8f1261 2018-06-23 stsp if (tree != root)
1709 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
1710 ffd1d5e5 2018-06-23 stsp selected = 0;
1713 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
1714 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1717 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
1718 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
1719 ffd1d5e5 2018-06-23 stsp selected++;
1722 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
1723 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1725 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
1726 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
1727 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1728 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1730 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
1731 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
1732 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1734 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
1735 ffd1d5e5 2018-06-23 stsp case '\r':
1736 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
1737 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1738 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
1739 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
1740 ffd1d5e5 2018-06-23 stsp if (tree == root)
1742 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1743 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1744 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1745 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
1746 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
1747 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
1748 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
1749 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
1750 ffd1d5e5 2018-06-23 stsp free(parent);
1751 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
1752 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1753 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
1754 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
1755 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
1757 ffd1d5e5 2018-06-23 stsp goto done;
1758 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
1759 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
1760 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1761 ffd1d5e5 2018-06-23 stsp goto done;
1763 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
1764 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
1765 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
1766 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
1767 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
1768 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
1770 ffd1d5e5 2018-06-23 stsp tree = child;
1771 ffd1d5e5 2018-06-23 stsp selected = 0;
1772 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
1773 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
1774 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
1775 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
1777 ffd1d5e5 2018-06-23 stsp goto done;
1780 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
1781 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
1782 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1789 ffd1d5e5 2018-06-23 stsp free(tree_label);
1790 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
1791 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
1792 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1793 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1794 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1795 ffd1d5e5 2018-06-23 stsp free(parent);
1798 ffd1d5e5 2018-06-23 stsp if (tree != root)
1799 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1800 ffd1d5e5 2018-06-23 stsp return err;
1803 ffd1d5e5 2018-06-23 stsp __dead static void
1804 ffd1d5e5 2018-06-23 stsp usage_tree(void)
1807 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1808 ffd1d5e5 2018-06-23 stsp getprogname());
1812 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1813 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
1815 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
1816 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
1817 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
1818 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1819 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
1820 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
1821 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
1824 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
1825 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1826 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
1829 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1830 ffd1d5e5 2018-06-23 stsp switch (ch) {
1832 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
1836 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
1840 ffd1d5e5 2018-06-23 stsp argc -= optind;
1841 ffd1d5e5 2018-06-23 stsp argv += optind;
1843 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
1844 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1845 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1846 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1847 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
1848 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1849 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1850 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1852 ffd1d5e5 2018-06-23 stsp usage_log();
1854 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1855 ffd1d5e5 2018-06-23 stsp free(repo_path);
1856 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1857 ffd1d5e5 2018-06-23 stsp return error;
1859 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
1860 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
1861 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1862 ffd1d5e5 2018-06-23 stsp goto done;
1864 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
1865 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1866 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
1867 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
1868 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
1869 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
1872 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1873 ffd1d5e5 2018-06-23 stsp goto done;
1875 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
1876 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1877 ffd1d5e5 2018-06-23 stsp goto done;
1879 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1880 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1881 ffd1d5e5 2018-06-23 stsp goto done;
1883 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
1885 ffd1d5e5 2018-06-23 stsp free(commit_id);
1886 ffd1d5e5 2018-06-23 stsp if (commit)
1887 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
1889 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1891 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
1892 ffd1d5e5 2018-06-23 stsp return error;
1894 5c5136c5 2018-05-20 stsp static void
1895 9f7d7167 2018-04-29 stsp init_curses(void)
1897 9f7d7167 2018-04-29 stsp initscr();
1901 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1902 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1903 1f475ad8 2018-05-10 stsp curs_set(0);
1906 4ed7e80c 2018-05-20 stsp __dead static void
1907 9f7d7167 2018-04-29 stsp usage(void)
1911 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1912 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1913 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1914 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1915 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1920 c2301be8 2018-04-30 stsp static char **
1921 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1923 c2301be8 2018-04-30 stsp char **argv;
1924 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1926 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1927 c2301be8 2018-04-30 stsp if (argv == NULL)
1928 c2301be8 2018-04-30 stsp err(1, "calloc");
1929 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1930 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1931 c2301be8 2018-04-30 stsp err(1, "calloc");
1932 c2301be8 2018-04-30 stsp if (arg1) {
1933 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1934 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1935 c2301be8 2018-04-30 stsp err(1, "calloc");
1938 c2301be8 2018-04-30 stsp return argv;
1942 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1944 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1945 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1946 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1947 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1949 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1951 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1952 9f7d7167 2018-04-29 stsp switch (ch) {
1954 9f7d7167 2018-04-29 stsp hflag = 1;
1958 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1962 9f7d7167 2018-04-29 stsp argc -= optind;
1963 9f7d7167 2018-04-29 stsp argv += optind;
1964 9f7d7167 2018-04-29 stsp optind = 0;
1965 c2301be8 2018-04-30 stsp optreset = 1;
1967 c2301be8 2018-04-30 stsp if (argc == 0) {
1968 f29d3e89 2018-06-23 stsp if (hflag)
1970 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1971 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1972 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1977 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1978 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1979 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1980 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1981 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1982 9f7d7167 2018-04-29 stsp if (hflag)
1983 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1987 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1988 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1989 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1990 c2301be8 2018-04-30 stsp if (repo_path) {
1991 c2301be8 2018-04-30 stsp struct got_repository *repo;
1992 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1993 c2301be8 2018-04-30 stsp if (error == NULL)
1994 c2301be8 2018-04-30 stsp got_repo_close(repo);
1996 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1997 c2301be8 2018-04-30 stsp if (error) {
1998 f29d3e89 2018-06-23 stsp if (hflag) {
1999 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2000 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2004 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2005 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2006 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2007 ad7de8d9 2018-04-30 stsp free(repo_path);
2010 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2011 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2013 c2301be8 2018-04-30 stsp free(repo_path);
2017 5c5136c5 2018-05-20 stsp init_curses();
2019 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2020 9f7d7167 2018-04-29 stsp if (error)
2021 9f7d7167 2018-04-29 stsp goto done;
2024 c2301be8 2018-04-30 stsp free(cmd_argv);
2025 9f7d7167 2018-04-29 stsp if (error)
2026 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);