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;
164 b700b5d6 2018-07-10 stsp *widthp = 0;
166 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
168 963b370f 2018-05-20 stsp return err;
171 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
172 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
173 963b370f 2018-05-20 stsp switch (width) {
179 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
180 b700b5d6 2018-07-10 stsp cols += width;
185 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
186 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
190 963b370f 2018-05-20 stsp err = got_error_from_errno();
194 963b370f 2018-05-20 stsp wline[i] = L'\0';
195 b700b5d6 2018-07-10 stsp if (widthp)
196 b700b5d6 2018-07-10 stsp *widthp = cols;
199 963b370f 2018-05-20 stsp free(wline);
201 963b370f 2018-05-20 stsp *wlinep = wline;
202 963b370f 2018-05-20 stsp return err;
205 963b370f 2018-05-20 stsp static const struct got_error *
206 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
208 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
209 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
210 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
211 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
212 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
213 bb737323 2018-05-20 stsp int author_width, logmsg_width;
214 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
215 80ddbec8 2018-04-29 stsp char *line = NULL;
216 bb737323 2018-05-20 stsp int col, limit;
217 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
218 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
219 9c2eaf34 2018-05-20 stsp const int avail = COLS;
221 b39d25c7 2018-07-10 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
222 b39d25c7 2018-07-10 stsp >= sizeof(datebuf))
223 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
225 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
226 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
228 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 b39d25c7 2018-07-10 stsp waddnstr(tog_log_view.window, datebuf, limit);
230 b39d25c7 2018-07-10 stsp col = limit + 1;
231 b39d25c7 2018-07-10 stsp if (col > avail)
234 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
235 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
236 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
239 6d9fbc00 2018-04-29 stsp author = author0;
240 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
241 6d9fbc00 2018-04-29 stsp if (smallerthan)
242 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
244 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
246 6d9fbc00 2018-04-29 stsp *at = '\0';
248 b39d25c7 2018-07-10 stsp limit = avail - col;
249 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
252 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
253 bb737323 2018-05-20 stsp col += author_width;
254 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
255 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
257 bb737323 2018-05-20 stsp author_width++;
259 9c2eaf34 2018-05-20 stsp if (col > avail)
262 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
263 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
264 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
267 bb737323 2018-05-20 stsp logmsg = logmsg0;
268 bb737323 2018-05-20 stsp while (*logmsg == '\n')
270 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
271 bb737323 2018-05-20 stsp if (newline)
272 bb737323 2018-05-20 stsp *newline = '\0';
273 bb737323 2018-05-20 stsp limit = avail - col;
274 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
277 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
278 bb737323 2018-05-20 stsp col += logmsg_width;
279 bb737323 2018-05-20 stsp while (col <= avail) {
280 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
284 80ddbec8 2018-04-29 stsp free(logmsg0);
285 bb737323 2018-05-20 stsp free(wlogmsg);
286 6d9fbc00 2018-04-29 stsp free(author0);
287 bb737323 2018-05-20 stsp free(wauthor);
288 80ddbec8 2018-04-29 stsp free(line);
289 80ddbec8 2018-04-29 stsp return err;
292 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
293 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
294 80ddbec8 2018-04-29 stsp struct got_object_id *id;
295 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
297 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
299 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
300 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
301 899d86c2 2018-05-10 stsp struct got_object_id *id)
303 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
305 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
306 80ddbec8 2018-04-29 stsp if (entry == NULL)
307 899d86c2 2018-05-10 stsp return NULL;
309 899d86c2 2018-05-10 stsp entry->id = id;
310 99db9666 2018-05-07 stsp entry->commit = commit;
311 899d86c2 2018-05-10 stsp return entry;
314 99db9666 2018-05-07 stsp static void
315 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
317 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
319 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
320 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
321 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
322 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
323 99db9666 2018-05-07 stsp free(entry);
326 99db9666 2018-05-07 stsp static void
327 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
329 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
330 99db9666 2018-05-07 stsp pop_commit(commits);
333 c4972b91 2018-05-07 stsp static const struct got_error *
334 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
335 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
337 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
338 899d86c2 2018-05-10 stsp struct got_object_id *id;
339 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
341 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
343 c4972b91 2018-05-07 stsp return err;
345 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
346 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
347 9ba79e04 2018-06-11 stsp int nfetched;
349 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
350 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
351 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
352 9ba79e04 2018-06-11 stsp return err;
354 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
356 9ba79e04 2018-06-11 stsp return err;
359 9ba79e04 2018-06-11 stsp while (1) {
360 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
362 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
364 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
365 9ba79e04 2018-06-11 stsp err = NULL;
369 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
373 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
374 9ba79e04 2018-06-11 stsp if (entry == NULL) {
375 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
379 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
382 9ba79e04 2018-06-11 stsp return err;
385 99db9666 2018-05-07 stsp static const struct got_error *
386 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
387 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
388 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
390 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
391 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
393 9ba79e04 2018-06-11 stsp *pentry = NULL;
395 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
396 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
397 9ba79e04 2018-06-11 stsp int nfetched;
398 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
399 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
401 9ba79e04 2018-06-11 stsp return err;
404 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
405 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
407 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
408 9ba79e04 2018-06-11 stsp err = NULL;
409 9ba79e04 2018-06-11 stsp return err;
412 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
413 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
414 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
415 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
417 9ba79e04 2018-06-11 stsp return NULL;
420 899d86c2 2018-05-10 stsp static const struct got_error *
421 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
423 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
424 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
426 9ba79e04 2018-06-11 stsp *head_id = NULL;
428 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
430 99db9666 2018-05-07 stsp return err;
432 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
433 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
435 9ba79e04 2018-06-11 stsp *head_id = NULL;
436 99db9666 2018-05-07 stsp return err;
439 9ba79e04 2018-06-11 stsp return NULL;
442 0553a4e3 2018-04-30 stsp static const struct got_error *
443 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
444 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
446 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
447 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
448 867c6645 2018-07-10 stsp int ncommits;
449 867c6645 2018-07-10 stsp char *id_str, *header;
450 867c6645 2018-07-10 stsp size_t header_len;
452 867c6645 2018-07-10 stsp entry = first;
453 867c6645 2018-07-10 stsp *selected = NULL;
454 867c6645 2018-07-10 stsp ncommits = 0;
455 867c6645 2018-07-10 stsp while (entry) {
456 867c6645 2018-07-10 stsp if (++ncommits - 1 == selected_idx) {
457 867c6645 2018-07-10 stsp *selected = entry;
460 867c6645 2018-07-10 stsp entry = TAILQ_NEXT(entry, entry);
462 867c6645 2018-07-10 stsp if (*selected == NULL)
463 867c6645 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
465 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
467 867c6645 2018-07-10 stsp return err;
469 867c6645 2018-07-10 stsp if (asprintf(&header, "commit: %s", id_str) == -1) {
470 867c6645 2018-07-10 stsp err = got_error_from_errno();
471 867c6645 2018-07-10 stsp free(id_str);
472 867c6645 2018-07-10 stsp return err;
475 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
477 867c6645 2018-07-10 stsp header_len = strlen(header);
478 867c6645 2018-07-10 stsp if (header_len > COLS) {
479 867c6645 2018-07-10 stsp id_str[COLS + 1] = '\0';
480 867c6645 2018-07-10 stsp header_len = COLS;
482 867c6645 2018-07-10 stsp wprintw(tog_log_view.window, header);
483 867c6645 2018-07-10 stsp while (header_len < COLS) {
484 867c6645 2018-07-10 stsp waddch(tog_log_view.window, ' ');
485 867c6645 2018-07-10 stsp header_len++;
487 867c6645 2018-07-10 stsp free(id_str);
488 867c6645 2018-07-10 stsp free(header);
490 899d86c2 2018-05-10 stsp entry = first;
491 899d86c2 2018-05-10 stsp *last = first;
492 867c6645 2018-07-10 stsp ncommits = 0;
493 899d86c2 2018-05-10 stsp while (entry) {
494 867c6645 2018-07-10 stsp if (ncommits == limit - 1)
496 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
497 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
498 cd0acaa7 2018-05-20 stsp *selected = entry;
500 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
501 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
502 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
505 0553a4e3 2018-04-30 stsp ncommits++;
506 899d86c2 2018-05-10 stsp *last = entry;
507 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
510 80ddbec8 2018-04-29 stsp update_panels();
511 80ddbec8 2018-04-29 stsp doupdate();
513 80ddbec8 2018-04-29 stsp return err;
516 07b55e75 2018-05-10 stsp static void
517 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
518 07b55e75 2018-05-10 stsp struct commit_queue *commits)
520 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
521 07b55e75 2018-05-10 stsp int nscrolled = 0;
523 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
524 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
527 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
528 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
529 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
530 07b55e75 2018-05-10 stsp if (entry) {
531 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
532 07b55e75 2018-05-10 stsp nscrolled++;
537 aa075928 2018-05-10 stsp static const struct got_error *
538 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
539 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
540 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
541 9ba79e04 2018-06-11 stsp struct got_repository *repo)
543 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
544 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
545 aa075928 2018-05-10 stsp int nscrolled = 0;
548 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
549 aa075928 2018-05-10 stsp if (pentry == NULL) {
550 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
551 9ba79e04 2018-06-11 stsp commits, graph, repo);
552 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
555 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
557 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
558 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
560 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
561 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
563 dd0a52c1 2018-05-20 stsp return err;
567 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
569 4a7f7875 2018-05-10 stsp int nparents = 0;
571 4a7f7875 2018-05-10 stsp while (entry) {
572 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
573 4a7f7875 2018-05-10 stsp nparents++;
576 4a7f7875 2018-05-10 stsp return nparents;
579 cd0acaa7 2018-05-20 stsp static const struct got_error *
580 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
582 cd0acaa7 2018-05-20 stsp const struct got_error *err;
583 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
584 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
586 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
588 cd0acaa7 2018-05-20 stsp return err;
590 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
591 9ba79e04 2018-06-11 stsp if (parent_id) {
592 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
597 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
600 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
602 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
603 cd0acaa7 2018-05-20 stsp return err;
606 80ddbec8 2018-04-29 stsp static const struct got_error *
607 9343a5fb 2018-06-23 stsp browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
609 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
610 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
612 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
614 9343a5fb 2018-06-23 stsp return err;
616 9343a5fb 2018-06-23 stsp err = show_tree_view(tree, entry->id, repo);
617 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
618 9343a5fb 2018-06-23 stsp return err;
621 9343a5fb 2018-06-23 stsp static const struct got_error *
622 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
624 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
625 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
626 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
627 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
628 0553a4e3 2018-04-30 stsp struct commit_queue commits;
629 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
630 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
631 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
632 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
634 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
635 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
636 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
637 80ddbec8 2018-04-29 stsp return got_error_from_errno();
638 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
640 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
641 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
642 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
643 80ddbec8 2018-04-29 stsp return got_error_from_errno();
645 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
647 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
649 9ba79e04 2018-06-11 stsp return err;
651 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
653 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
657 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
658 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
662 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
667 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
668 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
669 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
670 9ba79e04 2018-06-11 stsp * updating the display.
672 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
673 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
676 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
677 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
678 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
679 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
683 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
684 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
688 867c6645 2018-07-10 stsp selected_entry = first_displayed_entry;
689 899d86c2 2018-05-10 stsp while (!done) {
690 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
691 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
695 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
696 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
697 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
698 80ddbec8 2018-04-29 stsp switch (ch) {
700 31120ada 2018-04-30 stsp if (errno) {
701 31120ada 2018-04-30 stsp err = got_error_from_errno();
709 80ddbec8 2018-04-29 stsp case KEY_UP:
710 80ddbec8 2018-04-29 stsp if (selected > 0)
711 80ddbec8 2018-04-29 stsp selected--;
712 8ec9698d 2018-05-10 stsp if (selected > 0)
714 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
716 48531068 2018-05-10 stsp case KEY_PPAGE:
717 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
718 dfc1d240 2018-05-10 stsp first_displayed_entry) {
719 dfc1d240 2018-05-10 stsp selected = 0;
722 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
726 80ddbec8 2018-04-29 stsp case KEY_DOWN:
727 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
728 867c6645 2018-07-10 stsp if (selected < LINES - 2 &&
729 15c91275 2018-05-20 stsp selected < nparents - 1) {
730 15c91275 2018-05-20 stsp selected++;
733 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
734 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
739 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
740 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
741 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
745 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
747 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
748 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
749 867c6645 2018-07-10 stsp if (selected < LINES - 2 ||
750 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
751 867c6645 2018-07-10 stsp selected = MIN(LINES - 2, nparents - 1);
753 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
754 867c6645 2018-07-10 stsp if (selected > LINES - 1)
755 867c6645 2018-07-10 stsp selected = LINES - 2;
757 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
759 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
762 9343a5fb 2018-06-23 stsp show_panel(tog_log_view.panel);
765 9343a5fb 2018-06-23 stsp err = browse_commit(selected_entry, repo);
768 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
775 9ba79e04 2018-06-11 stsp free(head_id);
777 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
778 0553a4e3 2018-04-30 stsp free_commits(&commits);
779 80ddbec8 2018-04-29 stsp return err;
782 4ed7e80c 2018-05-20 stsp static const struct got_error *
783 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
785 80ddbec8 2018-04-29 stsp const struct got_error *error;
786 80ddbec8 2018-04-29 stsp struct got_repository *repo;
787 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
788 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
789 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
792 80ddbec8 2018-04-29 stsp #ifndef PROFILE
793 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
794 80ddbec8 2018-04-29 stsp err(1, "pledge");
797 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
798 80ddbec8 2018-04-29 stsp switch (ch) {
800 80ddbec8 2018-04-29 stsp start_commit = optarg;
804 80ddbec8 2018-04-29 stsp /* NOTREACHED */
808 80ddbec8 2018-04-29 stsp argc -= optind;
809 80ddbec8 2018-04-29 stsp argv += optind;
811 80ddbec8 2018-04-29 stsp if (argc == 0) {
812 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
813 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
814 80ddbec8 2018-04-29 stsp return got_error_from_errno();
815 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
816 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
817 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
818 80ddbec8 2018-04-29 stsp return got_error_from_errno();
820 80ddbec8 2018-04-29 stsp usage_log();
822 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
823 80ddbec8 2018-04-29 stsp free(repo_path);
824 80ddbec8 2018-04-29 stsp if (error != NULL)
825 80ddbec8 2018-04-29 stsp return error;
827 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
828 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
829 80ddbec8 2018-04-29 stsp if (error != NULL)
830 80ddbec8 2018-04-29 stsp return error;
832 80ddbec8 2018-04-29 stsp struct got_object *obj;
833 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
834 80ddbec8 2018-04-29 stsp if (error == NULL) {
835 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
836 899d86c2 2018-05-10 stsp if (start_id == NULL)
837 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
840 80ddbec8 2018-04-29 stsp if (error != NULL)
841 80ddbec8 2018-04-29 stsp return error;
842 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
843 899d86c2 2018-05-10 stsp free(start_id);
844 80ddbec8 2018-04-29 stsp got_repo_close(repo);
845 80ddbec8 2018-04-29 stsp return error;
848 4ed7e80c 2018-05-20 stsp __dead static void
849 9f7d7167 2018-04-29 stsp usage_diff(void)
852 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
853 9f7d7167 2018-04-29 stsp getprogname());
857 b304db33 2018-05-20 stsp static char *
858 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
860 b304db33 2018-05-20 stsp char *line;
861 b304db33 2018-05-20 stsp size_t linelen;
862 b304db33 2018-05-20 stsp size_t lineno;
863 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
865 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
867 b304db33 2018-05-20 stsp *len = linelen;
868 b304db33 2018-05-20 stsp return line;
871 4ed7e80c 2018-05-20 stsp static const struct got_error *
872 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
873 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
875 61e69b96 2018-05-20 stsp const struct got_error *err;
876 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
877 b304db33 2018-05-20 stsp char *line;
878 b304db33 2018-05-20 stsp size_t len;
879 61e69b96 2018-05-20 stsp wchar_t *wline;
883 a70480e0 2018-06-23 stsp werase(window);
886 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
887 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
888 26ed57b2 2018-05-19 stsp if (line == NULL) {
892 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
893 26ed57b2 2018-05-19 stsp free(line);
897 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
899 61e69b96 2018-05-20 stsp free(line);
900 61e69b96 2018-05-20 stsp return err;
902 a70480e0 2018-06-23 stsp waddwstr(window, wline);
903 e0b650dd 2018-05-20 stsp if (width < COLS)
904 a70480e0 2018-06-23 stsp waddch(window, '\n');
905 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
906 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
907 26ed57b2 2018-05-19 stsp free(line);
909 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
911 26ed57b2 2018-05-19 stsp update_panels();
912 26ed57b2 2018-05-19 stsp doupdate();
914 26ed57b2 2018-05-19 stsp return NULL;
917 4ed7e80c 2018-05-20 stsp static const struct got_error *
918 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
919 26ed57b2 2018-05-19 stsp struct got_repository *repo)
921 26ed57b2 2018-05-19 stsp const struct got_error *err;
923 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
924 b304db33 2018-05-20 stsp int eof, i;
926 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
927 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
928 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
930 511a516b 2018-05-19 stsp f = got_opentemp();
931 26ed57b2 2018-05-19 stsp if (f == NULL)
932 26ed57b2 2018-05-19 stsp return got_error_from_errno();
934 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
935 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
936 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
938 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
939 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
941 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
942 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
945 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
950 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
951 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
952 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
953 26ed57b2 2018-05-19 stsp return got_error_from_errno();
954 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
956 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
957 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
958 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
959 26ed57b2 2018-05-19 stsp return got_error_from_errno();
961 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
963 26ed57b2 2018-05-19 stsp while (!done) {
964 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
965 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
968 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
969 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
970 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
971 26ed57b2 2018-05-19 stsp switch (ch) {
976 26ed57b2 2018-05-19 stsp case KEY_UP:
977 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
978 b304db33 2018-05-20 stsp first_displayed_line--;
980 b304db33 2018-05-20 stsp case KEY_PPAGE:
981 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
983 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
984 d56caa55 2018-05-20 stsp first_displayed_line > 1)
985 26ed57b2 2018-05-19 stsp first_displayed_line--;
988 26ed57b2 2018-05-19 stsp case KEY_DOWN:
990 26ed57b2 2018-05-19 stsp first_displayed_line++;
992 b304db33 2018-05-20 stsp case KEY_NPAGE:
995 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
996 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
997 b304db33 2018-05-20 stsp first_displayed_line++;
998 b304db33 2018-05-20 stsp if (line == NULL)
1006 26ed57b2 2018-05-19 stsp fclose(f);
1007 26ed57b2 2018-05-19 stsp return err;
1010 4ed7e80c 2018-05-20 stsp static const struct got_error *
1011 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1013 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1014 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1015 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1016 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1017 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1020 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1021 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1022 26ed57b2 2018-05-19 stsp err(1, "pledge");
1025 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1026 26ed57b2 2018-05-19 stsp switch (ch) {
1029 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1033 26ed57b2 2018-05-19 stsp argc -= optind;
1034 26ed57b2 2018-05-19 stsp argv += optind;
1036 26ed57b2 2018-05-19 stsp if (argc == 0) {
1037 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1038 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1039 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1040 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1041 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1042 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1043 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1044 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1045 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1046 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1047 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1048 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1049 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1051 26ed57b2 2018-05-19 stsp usage_diff();
1053 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1054 26ed57b2 2018-05-19 stsp free(repo_path);
1055 26ed57b2 2018-05-19 stsp if (error)
1056 26ed57b2 2018-05-19 stsp goto done;
1058 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1059 26ed57b2 2018-05-19 stsp if (error)
1060 26ed57b2 2018-05-19 stsp goto done;
1062 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1063 26ed57b2 2018-05-19 stsp if (error)
1064 26ed57b2 2018-05-19 stsp goto done;
1066 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1068 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1070 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1072 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1073 26ed57b2 2018-05-19 stsp return error;
1076 4ed7e80c 2018-05-20 stsp __dead static void
1077 9f7d7167 2018-04-29 stsp usage_blame(void)
1080 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1081 9f7d7167 2018-04-29 stsp getprogname());
1085 84451b3e 2018-07-10 stsp struct tog_blame_line {
1086 84451b3e 2018-07-10 stsp int annotated;
1087 84451b3e 2018-07-10 stsp struct got_object_id *id;
1090 4ed7e80c 2018-05-20 stsp static const struct got_error *
1091 ab089a2a 2018-07-12 stsp draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1092 18430de3 2018-07-10 stsp struct tog_blame_line *lines, int nlines, int blame_complete,
1093 b700b5d6 2018-07-10 stsp int selected_line, int *first_displayed_line, int *last_displayed_line,
1094 18430de3 2018-07-10 stsp int *eof, int max_lines)
1096 84451b3e 2018-07-10 stsp const struct got_error *err;
1097 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1098 84451b3e 2018-07-10 stsp char *line;
1099 84451b3e 2018-07-10 stsp size_t len;
1100 84451b3e 2018-07-10 stsp wchar_t *wline;
1101 b700b5d6 2018-07-10 stsp int width, wlimit;
1102 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1103 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1104 ab089a2a 2018-07-12 stsp char *id_str;
1106 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1108 ab089a2a 2018-07-12 stsp return err;
1110 84451b3e 2018-07-10 stsp rewind(f);
1111 84451b3e 2018-07-10 stsp werase(window);
1113 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1114 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1115 ab089a2a 2018-07-12 stsp free(id_str);
1116 ab089a2a 2018-07-12 stsp return err;
1119 ab089a2a 2018-07-12 stsp err = format_line(&wline, &width, line, COLS);
1120 ab089a2a 2018-07-12 stsp free(line);
1121 ab089a2a 2018-07-12 stsp waddwstr(window, wline);
1122 ab089a2a 2018-07-12 stsp if (width < COLS)
1123 ab089a2a 2018-07-12 stsp waddch(window, '\n');
1125 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1126 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1127 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1128 ab089a2a 2018-07-12 stsp free(id_str);
1129 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1131 ab089a2a 2018-07-12 stsp free(id_str);
1132 3f60a8ef 2018-07-10 stsp err = format_line(&wline, &width, line, COLS);
1133 3f60a8ef 2018-07-10 stsp free(line);
1135 3f60a8ef 2018-07-10 stsp return err;
1136 3f60a8ef 2018-07-10 stsp waddwstr(window, wline);
1137 3f60a8ef 2018-07-10 stsp if (width < COLS)
1138 3f60a8ef 2018-07-10 stsp waddch(window, '\n');
1141 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1142 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1143 84451b3e 2018-07-10 stsp if (line == NULL) {
1147 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1148 84451b3e 2018-07-10 stsp free(line);
1152 b700b5d6 2018-07-10 stsp wlimit = COLS < 9 ? 0 : COLS - 9;
1153 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1154 84451b3e 2018-07-10 stsp if (err) {
1155 84451b3e 2018-07-10 stsp free(line);
1156 84451b3e 2018-07-10 stsp return err;
1159 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1160 b700b5d6 2018-07-10 stsp wstandout(window);
1162 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1163 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1164 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1165 ee41ec32 2018-07-10 stsp waddstr(window, " ");
1166 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1167 84451b3e 2018-07-10 stsp char *id_str;
1168 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1169 84451b3e 2018-07-10 stsp if (err) {
1170 84451b3e 2018-07-10 stsp free(line);
1171 84451b3e 2018-07-10 stsp return err;
1173 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1174 84451b3e 2018-07-10 stsp free(id_str);
1175 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1177 ee41ec32 2018-07-10 stsp waddstr(window, "........ ");
1178 ee41ec32 2018-07-10 stsp prev_id = NULL;
1181 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1182 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1183 b700b5d6 2018-07-10 stsp waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1186 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1187 b700b5d6 2018-07-10 stsp wstandend(window);
1188 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1189 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1190 84451b3e 2018-07-10 stsp free(line);
1192 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1194 84451b3e 2018-07-10 stsp update_panels();
1195 84451b3e 2018-07-10 stsp doupdate();
1197 84451b3e 2018-07-10 stsp return NULL;
1200 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1201 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1202 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1203 84451b3e 2018-07-10 stsp int nlines;
1205 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1207 3f60a8ef 2018-07-10 stsp const char *path;
1208 84451b3e 2018-07-10 stsp int *first_displayed_line;
1209 84451b3e 2018-07-10 stsp int *last_displayed_line;
1210 b700b5d6 2018-07-10 stsp int *selected_line;
1211 18430de3 2018-07-10 stsp int *quit;
1214 84451b3e 2018-07-10 stsp static const struct got_error *
1215 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1217 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1218 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1219 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1222 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1223 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1224 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1226 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1227 84451b3e 2018-07-10 stsp return got_error_from_errno();
1229 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1230 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1231 d68a0a7d 2018-07-10 stsp goto done;
1234 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1235 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1237 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1238 d68a0a7d 2018-07-10 stsp if (line->annotated)
1239 d68a0a7d 2018-07-10 stsp goto done;
1241 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1242 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1243 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1244 84451b3e 2018-07-10 stsp goto done;
1246 84451b3e 2018-07-10 stsp line->annotated = 1;
1248 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1249 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1250 ab089a2a 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1252 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1253 84451b3e 2018-07-10 stsp return got_error_from_errno();
1254 84451b3e 2018-07-10 stsp return err;
1257 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1258 84451b3e 2018-07-10 stsp const char *path;
1259 84451b3e 2018-07-10 stsp struct got_repository *repo;
1260 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1261 18430de3 2018-07-10 stsp int *complete;
1264 84451b3e 2018-07-10 stsp static void *
1265 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1267 18430de3 2018-07-10 stsp const struct got_error *err;
1268 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1269 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1272 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1273 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1274 18430de3 2018-07-10 stsp *ta->complete = 1;
1276 18430de3 2018-07-10 stsp return (void *)err;
1278 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1279 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1281 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1282 245d91c1 2018-07-12 stsp a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1283 245d91c1 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1285 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1286 18430de3 2018-07-10 stsp err = got_error_from_errno();
1288 18430de3 2018-07-10 stsp return (void *)err;
1291 245d91c1 2018-07-12 stsp static struct got_object_id *
1292 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1293 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1295 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1297 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1298 245d91c1 2018-07-12 stsp if (!line->annotated)
1299 245d91c1 2018-07-12 stsp return NULL;
1301 245d91c1 2018-07-12 stsp return line->id;
1304 84451b3e 2018-07-10 stsp static const struct got_error *
1305 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1306 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1307 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1309 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1310 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1311 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1312 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1314 b880a918 2018-07-10 stsp *pobj = NULL;
1315 b880a918 2018-07-10 stsp *obj = NULL;
1317 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1318 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1319 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1320 b880a918 2018-07-10 stsp return NULL;
1322 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1324 b880a918 2018-07-10 stsp goto done;
1326 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1328 b880a918 2018-07-10 stsp goto done;
1330 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1331 b880a918 2018-07-10 stsp if (pid) {
1332 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1334 b880a918 2018-07-10 stsp goto done;
1337 b880a918 2018-07-10 stsp if (commit)
1338 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1339 b880a918 2018-07-10 stsp return err;
1342 245d91c1 2018-07-12 stsp struct tog_blame {
1344 245d91c1 2018-07-12 stsp size_t filesize;
1345 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1346 245d91c1 2018-07-12 stsp size_t nlines;
1347 245d91c1 2018-07-12 stsp pthread_t thread;
1348 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1349 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1350 245d91c1 2018-07-12 stsp const char *path;
1351 245d91c1 2018-07-12 stsp struct got_object_id *commit_id;
1354 b880a918 2018-07-10 stsp static const struct got_error *
1355 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1357 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1360 245d91c1 2018-07-12 stsp if (blame->thread) {
1361 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1362 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1363 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1364 245d91c1 2018-07-12 stsp err = NULL;
1365 245d91c1 2018-07-12 stsp blame->thread = NULL;
1367 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1368 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1369 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1371 245d91c1 2018-07-12 stsp if (blame->f) {
1372 245d91c1 2018-07-12 stsp fclose(blame->f);
1373 245d91c1 2018-07-12 stsp blame->f = NULL;
1375 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1376 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1377 245d91c1 2018-07-12 stsp free(blame->lines);
1378 245d91c1 2018-07-12 stsp blame->lines = NULL;
1379 245d91c1 2018-07-12 stsp free(blame->commit_id);
1380 245d91c1 2018-07-12 stsp blame->commit_id = NULL;
1382 245d91c1 2018-07-12 stsp return err;
1385 245d91c1 2018-07-12 stsp static const struct got_error *
1386 245d91c1 2018-07-12 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1387 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1388 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1389 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1390 245d91c1 2018-07-12 stsp struct got_repository *repo)
1392 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1393 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1394 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1395 245d91c1 2018-07-12 stsp struct got_object *obj;
1397 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1399 84451b3e 2018-07-10 stsp goto done;
1400 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1401 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1402 84451b3e 2018-07-10 stsp goto done;
1405 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1407 a70480e0 2018-06-23 stsp goto done;
1408 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1409 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1410 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1411 84451b3e 2018-07-10 stsp goto done;
1413 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1414 245d91c1 2018-07-12 stsp blame->f, blob);
1416 84451b3e 2018-07-10 stsp goto done;
1418 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1419 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1420 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1421 84451b3e 2018-07-10 stsp goto done;
1424 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1426 bd24772e 2018-07-11 stsp goto done;
1428 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1429 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1430 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1431 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1432 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1433 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1434 245d91c1 2018-07-12 stsp goto done;
1436 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1437 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1438 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1439 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1440 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1441 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1443 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1444 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1445 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1446 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1447 245d91c1 2018-07-12 stsp *blame_complete = 0;
1449 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1450 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1451 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1452 245d91c1 2018-07-12 stsp goto done;
1457 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1459 245d91c1 2018-07-12 stsp got_object_close(obj);
1461 245d91c1 2018-07-12 stsp stop_blame(blame);
1462 245d91c1 2018-07-12 stsp return err;
1465 245d91c1 2018-07-12 stsp static const struct got_error *
1466 245d91c1 2018-07-12 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1467 245d91c1 2018-07-12 stsp struct got_repository *repo)
1469 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1470 245d91c1 2018-07-12 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1471 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1472 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1473 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1474 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1475 245d91c1 2018-07-12 stsp struct tog_blame blame;
1476 245d91c1 2018-07-12 stsp int blame_running = 0;
1477 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1478 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1480 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1482 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1483 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1484 245d91c1 2018-07-12 stsp goto done;
1487 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1489 245d91c1 2018-07-12 stsp goto done;
1490 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1492 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1493 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1494 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1495 a70480e0 2018-06-23 stsp return got_error_from_errno();
1496 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1498 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1499 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1500 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1501 a70480e0 2018-06-23 stsp return got_error_from_errno();
1503 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1505 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1506 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex, &blame_complete,
1507 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1508 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1510 245d91c1 2018-07-12 stsp return err;
1512 a70480e0 2018-06-23 stsp while (!done) {
1513 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1514 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1515 84451b3e 2018-07-10 stsp goto done;
1517 dbc6a6b6 2018-07-12 stsp err = draw_blame(tog_blame_view.window, blamed_commit->id,
1518 245d91c1 2018-07-12 stsp blame.f, path, blame.lines, blame.nlines, blame_complete,
1519 245d91c1 2018-07-12 stsp selected_line, &first_displayed_line, &last_displayed_line,
1520 245d91c1 2018-07-12 stsp &eof, LINES);
1521 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1522 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1523 84451b3e 2018-07-10 stsp goto done;
1527 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1528 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1529 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1530 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1531 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1532 84451b3e 2018-07-10 stsp goto done;
1534 a70480e0 2018-06-23 stsp switch (ch) {
1539 a70480e0 2018-06-23 stsp case KEY_UP:
1540 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1541 b700b5d6 2018-07-10 stsp selected_line--;
1542 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1543 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1544 a70480e0 2018-06-23 stsp first_displayed_line--;
1546 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1547 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1548 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1549 b700b5d6 2018-07-10 stsp selected_line = 1;
1552 084063cd 2018-07-12 stsp if (first_displayed_line > LINES - 2)
1553 084063cd 2018-07-12 stsp first_displayed_line -= (LINES - 2);
1555 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1558 a70480e0 2018-06-23 stsp case KEY_DOWN:
1559 a026b947 2018-07-12 stsp if (selected_line < LINES - 2 &&
1560 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1561 a026b947 2018-07-12 stsp blame.nlines)
1562 b700b5d6 2018-07-10 stsp selected_line++;
1563 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1564 b700b5d6 2018-07-10 stsp first_displayed_line++;
1567 7a2921f9 2018-07-12 stsp case 'p': {
1568 245d91c1 2018-07-12 stsp struct got_object_id *id;
1569 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1570 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1571 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1572 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1574 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1575 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1576 245d91c1 2018-07-12 stsp selected_line, repo);
1579 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1581 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1584 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1585 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1586 ad6a6c43 2018-07-10 stsp goto done;
1588 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1589 245d91c1 2018-07-12 stsp blame_running = 0;
1591 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1592 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1593 245d91c1 2018-07-12 stsp goto done;
1595 245d91c1 2018-07-12 stsp if (thread_err)
1597 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1598 1960a6f9 2018-07-12 stsp got_object_close(obj);
1599 1960a6f9 2018-07-12 stsp obj = NULL;
1600 1960a6f9 2018-07-12 stsp if (pobj) {
1601 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1602 14437fb1 2018-07-13 stsp pobj = NULL;
1604 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1605 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1608 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1611 dbc6a6b6 2018-07-12 stsp goto done;
1612 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1613 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1614 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex,
1615 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1616 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1617 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1620 dbc6a6b6 2018-07-12 stsp blame_running = 1;
1623 dbc6a6b6 2018-07-12 stsp case 'B': {
1624 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1625 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1626 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1629 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1630 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1631 dbc6a6b6 2018-07-12 stsp goto done;
1633 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1634 dbc6a6b6 2018-07-12 stsp blame_running = 0;
1636 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1637 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1638 dbc6a6b6 2018-07-12 stsp goto done;
1640 dbc6a6b6 2018-07-12 stsp if (thread_err)
1642 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1643 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1644 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1645 dbc6a6b6 2018-07-12 stsp err = run_blame(&blame, &mutex,
1646 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1647 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1648 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1651 245d91c1 2018-07-12 stsp blame_running = 1;
1654 245d91c1 2018-07-12 stsp case KEY_ENTER:
1655 245d91c1 2018-07-12 stsp case '\r':
1656 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1657 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1658 245d91c1 2018-07-12 stsp selected_line, repo);
1661 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1663 b880a918 2018-07-10 stsp err = show_diff_view(pobj, obj, repo);
1664 b880a918 2018-07-10 stsp if (pobj) {
1665 b880a918 2018-07-10 stsp got_object_close(pobj);
1666 b880a918 2018-07-10 stsp pobj = NULL;
1668 b880a918 2018-07-10 stsp got_object_close(obj);
1669 b880a918 2018-07-10 stsp obj = NULL;
1670 ad6a6c43 2018-07-10 stsp show_panel(tog_blame_view.panel);
1674 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1676 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1677 084063cd 2018-07-12 stsp selected_line < LINES - 2) {
1678 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1679 d2dfcfbf 2018-07-12 stsp LINES - 2);
1682 245d91c1 2018-07-12 stsp if (last_displayed_line + LINES - 2 <=
1683 245d91c1 2018-07-12 stsp blame.nlines)
1684 084063cd 2018-07-12 stsp first_displayed_line += LINES - 2;
1686 b700b5d6 2018-07-10 stsp first_displayed_line =
1687 245d91c1 2018-07-12 stsp blame.nlines - (LINES - 3);
1692 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1693 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1694 245d91c1 2018-07-12 stsp if (err || thread_err)
1699 b880a918 2018-07-10 stsp got_object_close(pobj);
1700 245d91c1 2018-07-12 stsp if (blame_running)
1701 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1702 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1703 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1704 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1705 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1707 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1710 a70480e0 2018-06-23 stsp static const struct got_error *
1711 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1713 a70480e0 2018-06-23 stsp const struct got_error *error;
1714 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1715 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1716 a70480e0 2018-06-23 stsp char *path = NULL;
1717 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1718 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1721 a70480e0 2018-06-23 stsp #ifndef PROFILE
1722 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1723 a70480e0 2018-06-23 stsp err(1, "pledge");
1726 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1727 a70480e0 2018-06-23 stsp switch (ch) {
1729 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1733 a70480e0 2018-06-23 stsp /* NOTREACHED */
1737 a70480e0 2018-06-23 stsp argc -= optind;
1738 a70480e0 2018-06-23 stsp argv += optind;
1740 a70480e0 2018-06-23 stsp if (argc == 0) {
1741 a70480e0 2018-06-23 stsp usage_blame();
1742 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1743 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1744 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1745 a70480e0 2018-06-23 stsp return got_error_from_errno();
1746 a70480e0 2018-06-23 stsp path = argv[0];
1747 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1748 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1749 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1750 a70480e0 2018-06-23 stsp return got_error_from_errno();
1751 a70480e0 2018-06-23 stsp path = argv[1];
1753 a70480e0 2018-06-23 stsp usage_blame();
1755 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1756 a70480e0 2018-06-23 stsp free(repo_path);
1757 a70480e0 2018-06-23 stsp if (error != NULL)
1758 66b4983c 2018-06-23 stsp return error;
1760 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1761 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1762 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1763 a70480e0 2018-06-23 stsp if (error != NULL)
1764 66b4983c 2018-06-23 stsp goto done;
1765 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1766 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1768 a70480e0 2018-06-23 stsp struct got_object *obj;
1769 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1770 a70480e0 2018-06-23 stsp if (error != NULL)
1771 66b4983c 2018-06-23 stsp goto done;
1772 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1773 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1774 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1775 a19e88aa 2018-06-23 stsp got_object_close(obj);
1777 a19e88aa 2018-06-23 stsp if (error != NULL)
1778 a19e88aa 2018-06-23 stsp goto done;
1780 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1782 a70480e0 2018-06-23 stsp free(commit_id);
1784 a70480e0 2018-06-23 stsp got_repo_close(repo);
1785 a70480e0 2018-06-23 stsp return error;
1788 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1789 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1790 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1791 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1792 1d13200f 2018-07-12 stsp WINDOW *window, const char *label, int show_ids, const char *parent_path,
1793 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1795 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1796 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1797 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1798 ffd1d5e5 2018-06-23 stsp int width, n;
1800 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1802 ffd1d5e5 2018-06-23 stsp werase(window);
1804 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1805 ffd1d5e5 2018-06-23 stsp return NULL;
1807 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1809 ffd1d5e5 2018-06-23 stsp return err;
1810 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1811 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1812 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1813 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1814 ffd1d5e5 2018-06-23 stsp return NULL;
1815 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1817 ce52c690 2018-06-23 stsp return err;
1818 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1819 ce52c690 2018-06-23 stsp if (width < COLS)
1820 ce52c690 2018-06-23 stsp waddch(window, '\n');
1821 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1822 ffd1d5e5 2018-06-23 stsp return NULL;
1823 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1824 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1825 a1eca9bb 2018-06-23 stsp return NULL;
1827 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1828 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1829 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1830 ffd1d5e5 2018-06-23 stsp wstandout(window);
1831 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1833 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1834 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1835 ffd1d5e5 2018-06-23 stsp wstandend(window);
1836 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1837 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1838 ffd1d5e5 2018-06-23 stsp return NULL;
1842 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1843 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1846 ffd1d5e5 2018-06-23 stsp while (te) {
1847 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
1849 1d13200f 2018-07-12 stsp if (show_ids) {
1850 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
1852 1d13200f 2018-07-12 stsp return got_error_from_errno();
1854 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1855 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1856 1d13200f 2018-07-12 stsp free(id_str);
1857 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1859 1d13200f 2018-07-12 stsp free(id_str);
1860 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1861 ffd1d5e5 2018-06-23 stsp if (err) {
1862 ffd1d5e5 2018-06-23 stsp free(line);
1865 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1866 ffd1d5e5 2018-06-23 stsp wstandout(window);
1867 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1869 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1870 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1871 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1872 ffd1d5e5 2018-06-23 stsp if (n == selected)
1873 ffd1d5e5 2018-06-23 stsp wstandend(window);
1874 ffd1d5e5 2018-06-23 stsp free(line);
1876 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1877 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1878 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1880 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1883 ffd1d5e5 2018-06-23 stsp return err;
1886 ffd1d5e5 2018-06-23 stsp static void
1887 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1888 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1890 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1893 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1896 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1897 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1898 ffd1d5e5 2018-06-23 stsp if (!isroot)
1899 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1903 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1904 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1905 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1906 ffd1d5e5 2018-06-23 stsp prev = te;
1907 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1909 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1910 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1912 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1913 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1916 ffd1d5e5 2018-06-23 stsp static void
1917 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1918 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1919 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1921 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1922 ffd1d5e5 2018-06-23 stsp int n = 0;
1924 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1927 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1928 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1930 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1931 ffd1d5e5 2018-06-23 stsp while (next) {
1932 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1933 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1935 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1939 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1940 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1941 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1942 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1943 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1944 ffd1d5e5 2018-06-23 stsp int selected;
1947 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1949 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1950 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1951 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1953 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1954 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1955 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1957 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1958 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1960 ce52c690 2018-06-23 stsp len += strlen(te->name);
1962 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1963 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1964 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1966 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1967 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1968 d9765a41 2018-06-23 stsp while (pt) {
1969 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1970 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1971 cb2ebc8a 2018-06-23 stsp goto done;
1973 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1974 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1975 cb2ebc8a 2018-06-23 stsp goto done;
1977 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1980 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1981 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1982 ce52c690 2018-06-23 stsp goto done;
1986 ce52c690 2018-06-23 stsp if (err) {
1987 ce52c690 2018-06-23 stsp free(*path);
1988 ce52c690 2018-06-23 stsp *path = NULL;
1990 ce52c690 2018-06-23 stsp return err;
1993 ce52c690 2018-06-23 stsp static const struct got_error *
1994 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1995 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1997 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1998 ce52c690 2018-06-23 stsp char *path;
2000 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2002 ce52c690 2018-06-23 stsp return err;
2004 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
2005 cb2ebc8a 2018-06-23 stsp free(path);
2006 cb2ebc8a 2018-06-23 stsp return err;
2009 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2010 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2011 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
2013 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2014 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2015 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2016 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2017 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2018 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2019 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2020 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2021 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2022 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2024 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2026 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2027 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2028 ffd1d5e5 2018-06-23 stsp goto done;
2030 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2031 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2032 ffd1d5e5 2018-06-23 stsp goto done;
2035 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
2036 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
2037 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
2038 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2039 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
2041 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
2042 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
2043 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
2044 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2046 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
2048 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2049 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2050 ffd1d5e5 2018-06-23 stsp while (!done) {
2051 ce52c690 2018-06-23 stsp char *parent_path;
2052 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2053 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2054 ffd1d5e5 2018-06-23 stsp if (tree != root)
2055 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2057 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2059 ce52c690 2018-06-23 stsp goto done;
2061 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
2062 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2063 1d13200f 2018-07-12 stsp tog_tree_view.window, tree_label, show_ids,
2064 1d13200f 2018-07-12 stsp parent_path, entries, selected, LINES, tree == root);
2065 ce52c690 2018-06-23 stsp free(parent_path);
2069 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2070 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
2071 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2072 ffd1d5e5 2018-06-23 stsp switch (ch) {
2077 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2080 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2081 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2082 ffd1d5e5 2018-06-23 stsp selected--;
2083 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2085 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2086 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2088 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2089 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2090 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2091 cf8f1261 2018-06-23 stsp if (tree != root)
2092 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2093 ffd1d5e5 2018-06-23 stsp selected = 0;
2096 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
2097 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2100 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2101 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2102 ffd1d5e5 2018-06-23 stsp selected++;
2105 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2106 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2108 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2109 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
2110 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2111 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2113 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2114 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2115 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2117 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2118 ffd1d5e5 2018-06-23 stsp case '\r':
2119 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2120 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2121 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2122 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2123 ffd1d5e5 2018-06-23 stsp if (tree == root)
2125 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2126 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2127 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2128 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2129 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2130 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2131 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2132 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2133 ffd1d5e5 2018-06-23 stsp free(parent);
2134 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2135 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2136 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2137 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2138 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2140 ffd1d5e5 2018-06-23 stsp goto done;
2141 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2142 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2143 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2144 ffd1d5e5 2018-06-23 stsp goto done;
2146 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2147 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2148 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2149 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2150 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2151 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2153 ffd1d5e5 2018-06-23 stsp tree = child;
2154 ffd1d5e5 2018-06-23 stsp selected = 0;
2155 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2156 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2157 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
2158 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
2160 ffd1d5e5 2018-06-23 stsp goto done;
2163 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2164 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
2165 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2172 ffd1d5e5 2018-06-23 stsp free(tree_label);
2173 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2174 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2175 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2176 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2177 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2178 ffd1d5e5 2018-06-23 stsp free(parent);
2181 ffd1d5e5 2018-06-23 stsp if (tree != root)
2182 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2183 ffd1d5e5 2018-06-23 stsp return err;
2186 ffd1d5e5 2018-06-23 stsp __dead static void
2187 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2190 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2191 ffd1d5e5 2018-06-23 stsp getprogname());
2195 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2196 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2198 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2199 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2200 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2201 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2202 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2203 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2204 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2207 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2208 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2209 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2212 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2213 ffd1d5e5 2018-06-23 stsp switch (ch) {
2215 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2219 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2223 ffd1d5e5 2018-06-23 stsp argc -= optind;
2224 ffd1d5e5 2018-06-23 stsp argv += optind;
2226 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2227 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2228 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2229 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2230 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2231 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2232 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2233 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2235 ffd1d5e5 2018-06-23 stsp usage_log();
2237 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2238 ffd1d5e5 2018-06-23 stsp free(repo_path);
2239 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2240 ffd1d5e5 2018-06-23 stsp return error;
2242 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2243 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2244 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2245 ffd1d5e5 2018-06-23 stsp goto done;
2247 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2248 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2249 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2250 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2251 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2252 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2255 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2256 ffd1d5e5 2018-06-23 stsp goto done;
2258 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2259 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2260 ffd1d5e5 2018-06-23 stsp goto done;
2262 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2263 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2264 ffd1d5e5 2018-06-23 stsp goto done;
2266 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
2268 ffd1d5e5 2018-06-23 stsp free(commit_id);
2269 ffd1d5e5 2018-06-23 stsp if (commit)
2270 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2272 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2274 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2275 ffd1d5e5 2018-06-23 stsp return error;
2277 5c5136c5 2018-05-20 stsp static void
2278 9f7d7167 2018-04-29 stsp init_curses(void)
2280 9f7d7167 2018-04-29 stsp initscr();
2284 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2285 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2286 1f475ad8 2018-05-10 stsp curs_set(0);
2289 4ed7e80c 2018-05-20 stsp __dead static void
2290 9f7d7167 2018-04-29 stsp usage(void)
2294 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2295 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2296 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2297 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2298 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2303 c2301be8 2018-04-30 stsp static char **
2304 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2306 c2301be8 2018-04-30 stsp char **argv;
2307 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2309 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2310 c2301be8 2018-04-30 stsp if (argv == NULL)
2311 c2301be8 2018-04-30 stsp err(1, "calloc");
2312 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2313 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2314 c2301be8 2018-04-30 stsp err(1, "calloc");
2315 c2301be8 2018-04-30 stsp if (arg1) {
2316 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2317 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2318 c2301be8 2018-04-30 stsp err(1, "calloc");
2321 c2301be8 2018-04-30 stsp return argv;
2325 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2327 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2328 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2329 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2330 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2332 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2334 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2335 9f7d7167 2018-04-29 stsp switch (ch) {
2337 9f7d7167 2018-04-29 stsp hflag = 1;
2341 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2345 9f7d7167 2018-04-29 stsp argc -= optind;
2346 9f7d7167 2018-04-29 stsp argv += optind;
2347 9f7d7167 2018-04-29 stsp optind = 0;
2348 c2301be8 2018-04-30 stsp optreset = 1;
2350 c2301be8 2018-04-30 stsp if (argc == 0) {
2351 f29d3e89 2018-06-23 stsp if (hflag)
2353 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2354 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2355 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2360 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2361 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2362 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2363 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2364 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2365 9f7d7167 2018-04-29 stsp if (hflag)
2366 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2370 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2371 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2372 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2373 c2301be8 2018-04-30 stsp if (repo_path) {
2374 c2301be8 2018-04-30 stsp struct got_repository *repo;
2375 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2376 c2301be8 2018-04-30 stsp if (error == NULL)
2377 c2301be8 2018-04-30 stsp got_repo_close(repo);
2379 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2380 c2301be8 2018-04-30 stsp if (error) {
2381 f29d3e89 2018-06-23 stsp if (hflag) {
2382 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2383 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2387 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2388 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2389 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2390 ad7de8d9 2018-04-30 stsp free(repo_path);
2393 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2394 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2396 c2301be8 2018-04-30 stsp free(repo_path);
2400 5c5136c5 2018-05-20 stsp init_curses();
2402 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2403 9f7d7167 2018-04-29 stsp if (error)
2404 9f7d7167 2018-04-29 stsp goto done;
2407 c2301be8 2018-04-30 stsp free(cmd_argv);
2408 9f7d7167 2018-04-29 stsp if (error)
2409 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);