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;
1130 84451b3e 2018-07-10 stsp static const struct got_error *
1131 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1133 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1134 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1135 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1138 84451b3e 2018-07-10 stsp if (nlines != a->nlines || lineno < 1 || lineno > a->nlines)
1139 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1141 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1142 84451b3e 2018-07-10 stsp return got_error_from_errno();
1144 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1145 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1146 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1147 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1148 84451b3e 2018-07-10 stsp goto done;
1150 84451b3e 2018-07-10 stsp line->annotated = 1;
1152 84451b3e 2018-07-10 stsp err = draw_blame(a->window, a->f, a->lines, a->nlines,
1153 84451b3e 2018-07-10 stsp a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1155 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1156 84451b3e 2018-07-10 stsp return got_error_from_errno();
1157 84451b3e 2018-07-10 stsp return err;
1160 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1161 84451b3e 2018-07-10 stsp const char *path;
1162 84451b3e 2018-07-10 stsp struct got_object_id *commit_id;
1163 84451b3e 2018-07-10 stsp struct got_repository *repo;
1164 84451b3e 2018-07-10 stsp void *blame_cb_args;
1167 84451b3e 2018-07-10 stsp static void *
1168 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1170 84451b3e 2018-07-10 stsp struct tog_blame_thread_args *a = arg;
1171 84451b3e 2018-07-10 stsp return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
1172 84451b3e 2018-07-10 stsp blame_cb, a->blame_cb_args);
1175 84451b3e 2018-07-10 stsp static const struct got_error *
1176 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1177 a70480e0 2018-06-23 stsp struct got_repository *repo)
1179 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1180 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1181 a70480e0 2018-06-23 stsp int eof, i;
1182 84451b3e 2018-07-10 stsp struct got_object *obj = NULL;
1183 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1184 84451b3e 2018-07-10 stsp FILE *f = NULL;
1185 84451b3e 2018-07-10 stsp size_t filesize, nlines;
1186 84451b3e 2018-07-10 stsp struct tog_blame_line *lines = NULL;
1187 84451b3e 2018-07-10 stsp pthread_t thread = NULL;
1188 84451b3e 2018-07-10 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1189 84451b3e 2018-07-10 stsp struct tog_blame_cb_args blame_cb_args;
1190 84451b3e 2018-07-10 stsp struct tog_blame_thread_args blame_thread_args;
1192 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1194 84451b3e 2018-07-10 stsp goto done;
1195 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1196 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1197 84451b3e 2018-07-10 stsp got_object_close(obj);
1198 84451b3e 2018-07-10 stsp goto done;
1201 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1202 84451b3e 2018-07-10 stsp got_object_close(obj);
1204 a70480e0 2018-06-23 stsp goto done;
1205 84451b3e 2018-07-10 stsp f = got_opentemp();
1206 84451b3e 2018-07-10 stsp if (f == NULL) {
1207 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1208 84451b3e 2018-07-10 stsp goto done;
1210 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1212 84451b3e 2018-07-10 stsp goto done;
1214 84451b3e 2018-07-10 stsp lines = calloc(nlines, sizeof(*lines));
1215 84451b3e 2018-07-10 stsp if (lines == NULL) {
1216 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1217 84451b3e 2018-07-10 stsp goto done;
1220 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1221 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1222 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1223 a70480e0 2018-06-23 stsp return got_error_from_errno();
1224 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1226 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1227 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1228 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1229 a70480e0 2018-06-23 stsp return got_error_from_errno();
1231 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1233 84451b3e 2018-07-10 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1234 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1235 84451b3e 2018-07-10 stsp goto done;
1237 84451b3e 2018-07-10 stsp blame_cb_args.lines = lines;
1238 84451b3e 2018-07-10 stsp blame_cb_args.nlines = nlines;
1239 84451b3e 2018-07-10 stsp blame_cb_args.mutex = &mutex;
1240 84451b3e 2018-07-10 stsp blame_cb_args.f = f;
1241 84451b3e 2018-07-10 stsp blame_cb_args.window = tog_blame_view.window;
1242 84451b3e 2018-07-10 stsp blame_cb_args.first_displayed_line = &first_displayed_line;
1243 84451b3e 2018-07-10 stsp blame_cb_args.last_displayed_line = &last_displayed_line;
1245 84451b3e 2018-07-10 stsp blame_thread_args.path = path;
1246 84451b3e 2018-07-10 stsp blame_thread_args.commit_id = commit_id;
1247 84451b3e 2018-07-10 stsp blame_thread_args.repo = repo;
1248 84451b3e 2018-07-10 stsp blame_thread_args.blame_cb_args = &blame_cb_args;
1250 84451b3e 2018-07-10 stsp if (pthread_create(&thread, NULL, blame_thread,
1251 84451b3e 2018-07-10 stsp &blame_thread_args) != 0) {
1252 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1253 84451b3e 2018-07-10 stsp goto done;
1256 a70480e0 2018-06-23 stsp while (!done) {
1257 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1258 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1259 84451b3e 2018-07-10 stsp goto done;
1261 84451b3e 2018-07-10 stsp err = draw_blame(tog_blame_view.window, f, lines, nlines,
1262 84451b3e 2018-07-10 stsp &first_displayed_line, &last_displayed_line, &eof, LINES);
1263 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1264 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1265 84451b3e 2018-07-10 stsp goto done;
1269 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1270 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1271 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1272 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1273 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1274 84451b3e 2018-07-10 stsp goto done;
1276 a70480e0 2018-06-23 stsp switch (ch) {
1281 a70480e0 2018-06-23 stsp case KEY_UP:
1282 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1283 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1284 a70480e0 2018-06-23 stsp first_displayed_line--;
1286 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1288 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1289 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1290 a70480e0 2018-06-23 stsp first_displayed_line--;
1293 a70480e0 2018-06-23 stsp case KEY_DOWN:
1294 a70480e0 2018-06-23 stsp case KEY_ENTER:
1295 a70480e0 2018-06-23 stsp case '\r':
1297 a70480e0 2018-06-23 stsp first_displayed_line++;
1299 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1302 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1303 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1304 a70480e0 2018-06-23 stsp first_displayed_line++;
1305 a70480e0 2018-06-23 stsp if (line == NULL)
1312 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1313 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1314 84451b3e 2018-07-10 stsp goto done;
1319 84451b3e 2018-07-10 stsp got_object_blob_close(blob);
1321 84451b3e 2018-07-10 stsp fclose(f);
1322 84451b3e 2018-07-10 stsp free(lines);
1323 84451b3e 2018-07-10 stsp if (thread) {
1324 84451b3e 2018-07-10 stsp if (pthread_join(thread, (void **)&err) != 0)
1325 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1327 a70480e0 2018-06-23 stsp return err;
1330 a70480e0 2018-06-23 stsp static const struct got_error *
1331 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1333 a70480e0 2018-06-23 stsp const struct got_error *error;
1334 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1335 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1336 a70480e0 2018-06-23 stsp char *path = NULL;
1337 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1338 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1341 a70480e0 2018-06-23 stsp #ifndef PROFILE
1342 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1343 a70480e0 2018-06-23 stsp err(1, "pledge");
1346 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1347 a70480e0 2018-06-23 stsp switch (ch) {
1349 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1353 a70480e0 2018-06-23 stsp /* NOTREACHED */
1357 a70480e0 2018-06-23 stsp argc -= optind;
1358 a70480e0 2018-06-23 stsp argv += optind;
1360 a70480e0 2018-06-23 stsp if (argc == 0) {
1361 a70480e0 2018-06-23 stsp usage_blame();
1362 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1363 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1364 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1365 a70480e0 2018-06-23 stsp return got_error_from_errno();
1366 a70480e0 2018-06-23 stsp path = argv[0];
1367 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1368 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1369 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1370 a70480e0 2018-06-23 stsp return got_error_from_errno();
1371 a70480e0 2018-06-23 stsp path = argv[1];
1373 a70480e0 2018-06-23 stsp usage_blame();
1375 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1376 a70480e0 2018-06-23 stsp free(repo_path);
1377 a70480e0 2018-06-23 stsp if (error != NULL)
1378 66b4983c 2018-06-23 stsp return error;
1380 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1381 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1382 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1383 a70480e0 2018-06-23 stsp if (error != NULL)
1384 66b4983c 2018-06-23 stsp goto done;
1385 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1386 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1388 a70480e0 2018-06-23 stsp struct got_object *obj;
1389 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1390 a70480e0 2018-06-23 stsp if (error != NULL)
1391 66b4983c 2018-06-23 stsp goto done;
1392 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1393 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1394 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1395 a19e88aa 2018-06-23 stsp got_object_close(obj);
1397 a19e88aa 2018-06-23 stsp if (error != NULL)
1398 a19e88aa 2018-06-23 stsp goto done;
1400 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1402 a70480e0 2018-06-23 stsp free(commit_id);
1404 a70480e0 2018-06-23 stsp got_repo_close(repo);
1405 a70480e0 2018-06-23 stsp return error;
1408 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1409 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1410 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1411 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1412 ce52c690 2018-06-23 stsp WINDOW *window, const char *label, const char *parent_path,
1413 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1415 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1416 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1417 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1418 ffd1d5e5 2018-06-23 stsp int width, n;
1420 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1422 ffd1d5e5 2018-06-23 stsp werase(window);
1424 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1425 ffd1d5e5 2018-06-23 stsp return NULL;
1427 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1429 ffd1d5e5 2018-06-23 stsp return err;
1430 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1431 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1432 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1433 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1434 ffd1d5e5 2018-06-23 stsp return NULL;
1435 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1437 ce52c690 2018-06-23 stsp return err;
1438 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1439 ce52c690 2018-06-23 stsp if (width < COLS)
1440 ce52c690 2018-06-23 stsp waddch(window, '\n');
1441 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1442 ffd1d5e5 2018-06-23 stsp return NULL;
1443 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1444 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1445 a1eca9bb 2018-06-23 stsp return NULL;
1447 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1448 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1449 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1450 ffd1d5e5 2018-06-23 stsp wstandout(window);
1451 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1453 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1454 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1455 ffd1d5e5 2018-06-23 stsp wstandend(window);
1456 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1457 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1458 ffd1d5e5 2018-06-23 stsp return NULL;
1462 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1463 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1466 ffd1d5e5 2018-06-23 stsp while (te) {
1467 ce52c690 2018-06-23 stsp char *line = NULL;
1468 ffd1d5e5 2018-06-23 stsp if (asprintf(&line, " %s%s",
1469 ffd1d5e5 2018-06-23 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1470 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1471 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1472 ffd1d5e5 2018-06-23 stsp if (err) {
1473 ffd1d5e5 2018-06-23 stsp free(line);
1476 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1477 ffd1d5e5 2018-06-23 stsp wstandout(window);
1478 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1480 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1481 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1482 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1483 ffd1d5e5 2018-06-23 stsp if (n == selected)
1484 ffd1d5e5 2018-06-23 stsp wstandend(window);
1485 ffd1d5e5 2018-06-23 stsp free(line);
1487 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1488 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1489 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1491 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1494 ffd1d5e5 2018-06-23 stsp return err;
1497 ffd1d5e5 2018-06-23 stsp static void
1498 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1499 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1501 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1504 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1507 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1508 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1509 ffd1d5e5 2018-06-23 stsp if (!isroot)
1510 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1514 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1515 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1516 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1517 ffd1d5e5 2018-06-23 stsp prev = te;
1518 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1520 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1521 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1523 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1524 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1527 ffd1d5e5 2018-06-23 stsp static void
1528 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1529 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1530 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1532 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1533 ffd1d5e5 2018-06-23 stsp int n = 0;
1535 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1538 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1539 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1541 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1542 ffd1d5e5 2018-06-23 stsp while (next) {
1543 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1544 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1546 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1550 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1551 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1552 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1553 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1554 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1555 ffd1d5e5 2018-06-23 stsp int selected;
1558 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1560 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1561 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1562 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1564 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1565 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1566 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1568 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1569 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1571 ce52c690 2018-06-23 stsp len += strlen(te->name);
1573 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1574 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1575 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1577 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1578 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1579 d9765a41 2018-06-23 stsp while (pt) {
1580 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1581 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1582 cb2ebc8a 2018-06-23 stsp goto done;
1584 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1585 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1586 cb2ebc8a 2018-06-23 stsp goto done;
1588 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1591 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1592 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1593 ce52c690 2018-06-23 stsp goto done;
1597 ce52c690 2018-06-23 stsp if (err) {
1598 ce52c690 2018-06-23 stsp free(*path);
1599 ce52c690 2018-06-23 stsp *path = NULL;
1601 ce52c690 2018-06-23 stsp return err;
1604 ce52c690 2018-06-23 stsp static const struct got_error *
1605 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1606 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1608 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1609 ce52c690 2018-06-23 stsp char *path;
1611 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
1613 ce52c690 2018-06-23 stsp return err;
1615 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
1616 cb2ebc8a 2018-06-23 stsp free(path);
1617 cb2ebc8a 2018-06-23 stsp return err;
1620 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1621 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1622 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
1624 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1625 ffd1d5e5 2018-06-23 stsp int ch, done = 0, selected = 0;
1626 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
1627 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
1628 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
1629 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
1630 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
1631 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
1632 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
1633 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
1635 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
1637 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
1638 ffd1d5e5 2018-06-23 stsp if (err != NULL)
1639 ffd1d5e5 2018-06-23 stsp goto done;
1641 ffd1d5e5 2018-06-23 stsp if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1642 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1643 ffd1d5e5 2018-06-23 stsp goto done;
1646 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
1647 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
1648 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
1649 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1650 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
1652 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
1653 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
1654 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
1655 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1657 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
1659 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
1660 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1661 ffd1d5e5 2018-06-23 stsp while (!done) {
1662 ce52c690 2018-06-23 stsp char *parent_path;
1663 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
1664 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
1665 ffd1d5e5 2018-06-23 stsp if (tree != root)
1666 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
1668 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
1670 ce52c690 2018-06-23 stsp goto done;
1672 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
1673 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
1674 ce52c690 2018-06-23 stsp tog_tree_view.window, tree_label, parent_path, entries,
1675 ce52c690 2018-06-23 stsp selected, LINES, tree == root);
1676 ce52c690 2018-06-23 stsp free(parent_path);
1680 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
1681 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
1682 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
1683 ffd1d5e5 2018-06-23 stsp switch (ch) {
1688 ffd1d5e5 2018-06-23 stsp case KEY_UP:
1689 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1690 ffd1d5e5 2018-06-23 stsp selected--;
1691 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1693 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
1694 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1696 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
1697 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
1698 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
1699 cf8f1261 2018-06-23 stsp if (tree != root)
1700 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
1701 ffd1d5e5 2018-06-23 stsp selected = 0;
1704 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
1705 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1708 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
1709 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
1710 ffd1d5e5 2018-06-23 stsp selected++;
1713 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
1714 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1716 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
1717 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
1718 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1719 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1721 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
1722 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
1723 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1725 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
1726 ffd1d5e5 2018-06-23 stsp case '\r':
1727 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
1728 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1729 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
1730 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
1731 ffd1d5e5 2018-06-23 stsp if (tree == root)
1733 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1734 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1735 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1736 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
1737 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
1738 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
1739 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
1740 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
1741 ffd1d5e5 2018-06-23 stsp free(parent);
1742 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
1743 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1744 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
1745 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
1746 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
1748 ffd1d5e5 2018-06-23 stsp goto done;
1749 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
1750 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
1751 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1752 ffd1d5e5 2018-06-23 stsp goto done;
1754 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
1755 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
1756 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
1757 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
1758 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
1759 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
1761 ffd1d5e5 2018-06-23 stsp tree = child;
1762 ffd1d5e5 2018-06-23 stsp selected = 0;
1763 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
1764 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
1765 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
1766 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
1768 ffd1d5e5 2018-06-23 stsp goto done;
1771 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
1772 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
1773 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1780 ffd1d5e5 2018-06-23 stsp free(tree_label);
1781 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
1782 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
1783 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1784 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
1785 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
1786 ffd1d5e5 2018-06-23 stsp free(parent);
1789 ffd1d5e5 2018-06-23 stsp if (tree != root)
1790 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1791 ffd1d5e5 2018-06-23 stsp return err;
1794 ffd1d5e5 2018-06-23 stsp __dead static void
1795 ffd1d5e5 2018-06-23 stsp usage_tree(void)
1798 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1799 ffd1d5e5 2018-06-23 stsp getprogname());
1803 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1804 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
1806 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
1807 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
1808 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
1809 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1810 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
1811 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
1812 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
1815 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
1816 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1817 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
1820 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1821 ffd1d5e5 2018-06-23 stsp switch (ch) {
1823 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
1827 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
1831 ffd1d5e5 2018-06-23 stsp argc -= optind;
1832 ffd1d5e5 2018-06-23 stsp argv += optind;
1834 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
1835 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1836 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1837 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1838 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
1839 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1840 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1841 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1843 ffd1d5e5 2018-06-23 stsp usage_log();
1845 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1846 ffd1d5e5 2018-06-23 stsp free(repo_path);
1847 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1848 ffd1d5e5 2018-06-23 stsp return error;
1850 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
1851 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
1852 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1853 ffd1d5e5 2018-06-23 stsp goto done;
1855 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
1856 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1857 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
1858 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
1859 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
1860 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
1863 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1864 ffd1d5e5 2018-06-23 stsp goto done;
1866 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
1867 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1868 ffd1d5e5 2018-06-23 stsp goto done;
1870 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1871 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1872 ffd1d5e5 2018-06-23 stsp goto done;
1874 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
1876 ffd1d5e5 2018-06-23 stsp free(commit_id);
1877 ffd1d5e5 2018-06-23 stsp if (commit)
1878 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
1880 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1882 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
1883 ffd1d5e5 2018-06-23 stsp return error;
1885 5c5136c5 2018-05-20 stsp static void
1886 9f7d7167 2018-04-29 stsp init_curses(void)
1888 9f7d7167 2018-04-29 stsp initscr();
1892 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1893 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1894 1f475ad8 2018-05-10 stsp curs_set(0);
1897 4ed7e80c 2018-05-20 stsp __dead static void
1898 9f7d7167 2018-04-29 stsp usage(void)
1902 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1903 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1904 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1905 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1906 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1911 c2301be8 2018-04-30 stsp static char **
1912 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1914 c2301be8 2018-04-30 stsp char **argv;
1915 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1917 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1918 c2301be8 2018-04-30 stsp if (argv == NULL)
1919 c2301be8 2018-04-30 stsp err(1, "calloc");
1920 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1921 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1922 c2301be8 2018-04-30 stsp err(1, "calloc");
1923 c2301be8 2018-04-30 stsp if (arg1) {
1924 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1925 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1926 c2301be8 2018-04-30 stsp err(1, "calloc");
1929 c2301be8 2018-04-30 stsp return argv;
1933 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1935 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1936 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1937 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1938 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1940 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1942 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1943 9f7d7167 2018-04-29 stsp switch (ch) {
1945 9f7d7167 2018-04-29 stsp hflag = 1;
1949 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1953 9f7d7167 2018-04-29 stsp argc -= optind;
1954 9f7d7167 2018-04-29 stsp argv += optind;
1955 9f7d7167 2018-04-29 stsp optind = 0;
1956 c2301be8 2018-04-30 stsp optreset = 1;
1958 c2301be8 2018-04-30 stsp if (argc == 0) {
1959 f29d3e89 2018-06-23 stsp if (hflag)
1961 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1962 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1963 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1968 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1969 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1970 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1971 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1972 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1973 9f7d7167 2018-04-29 stsp if (hflag)
1974 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1978 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1979 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1980 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1981 c2301be8 2018-04-30 stsp if (repo_path) {
1982 c2301be8 2018-04-30 stsp struct got_repository *repo;
1983 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1984 c2301be8 2018-04-30 stsp if (error == NULL)
1985 c2301be8 2018-04-30 stsp got_repo_close(repo);
1987 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1988 c2301be8 2018-04-30 stsp if (error) {
1989 f29d3e89 2018-06-23 stsp if (hflag) {
1990 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
1991 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
1995 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1996 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1997 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1998 ad7de8d9 2018-04-30 stsp free(repo_path);
2001 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2002 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2004 c2301be8 2018-04-30 stsp free(repo_path);
2008 5c5136c5 2018-05-20 stsp init_curses();
2010 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2011 9f7d7167 2018-04-29 stsp if (error)
2012 9f7d7167 2018-04-29 stsp goto done;
2015 c2301be8 2018-04-30 stsp free(cmd_argv);
2016 9f7d7167 2018-04-29 stsp if (error)
2017 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);