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 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
978 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
979 b304db33 2018-05-20 stsp first_displayed_line--;
981 b304db33 2018-05-20 stsp case KEY_PPAGE:
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:
989 925e6f23 2018-05-20 stsp case KEY_ENTER:
992 26ed57b2 2018-05-19 stsp first_displayed_line++;
994 b304db33 2018-05-20 stsp case KEY_NPAGE:
997 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
998 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
999 b304db33 2018-05-20 stsp first_displayed_line++;
1000 b304db33 2018-05-20 stsp if (line == NULL)
1008 26ed57b2 2018-05-19 stsp fclose(f);
1009 26ed57b2 2018-05-19 stsp return err;
1012 4ed7e80c 2018-05-20 stsp static const struct got_error *
1013 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1015 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1016 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1017 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1018 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1019 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1022 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1023 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1024 26ed57b2 2018-05-19 stsp err(1, "pledge");
1027 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1028 26ed57b2 2018-05-19 stsp switch (ch) {
1031 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1035 26ed57b2 2018-05-19 stsp argc -= optind;
1036 26ed57b2 2018-05-19 stsp argv += optind;
1038 26ed57b2 2018-05-19 stsp if (argc == 0) {
1039 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1040 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1041 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1042 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1043 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1044 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1045 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1046 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1047 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1048 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1049 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1050 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1051 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1053 26ed57b2 2018-05-19 stsp usage_diff();
1055 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1056 26ed57b2 2018-05-19 stsp free(repo_path);
1057 26ed57b2 2018-05-19 stsp if (error)
1058 26ed57b2 2018-05-19 stsp goto done;
1060 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1061 26ed57b2 2018-05-19 stsp if (error)
1062 26ed57b2 2018-05-19 stsp goto done;
1064 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1065 26ed57b2 2018-05-19 stsp if (error)
1066 26ed57b2 2018-05-19 stsp goto done;
1068 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1070 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1072 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1074 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1075 26ed57b2 2018-05-19 stsp return error;
1078 4ed7e80c 2018-05-20 stsp __dead static void
1079 9f7d7167 2018-04-29 stsp usage_blame(void)
1082 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1083 9f7d7167 2018-04-29 stsp getprogname());
1087 84451b3e 2018-07-10 stsp struct tog_blame_line {
1088 84451b3e 2018-07-10 stsp int annotated;
1089 84451b3e 2018-07-10 stsp struct got_object_id *id;
1092 4ed7e80c 2018-05-20 stsp static const struct got_error *
1093 ab089a2a 2018-07-12 stsp draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1094 18430de3 2018-07-10 stsp struct tog_blame_line *lines, int nlines, int blame_complete,
1095 b700b5d6 2018-07-10 stsp int selected_line, int *first_displayed_line, int *last_displayed_line,
1096 18430de3 2018-07-10 stsp int *eof, int max_lines)
1098 84451b3e 2018-07-10 stsp const struct got_error *err;
1099 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1100 84451b3e 2018-07-10 stsp char *line;
1101 84451b3e 2018-07-10 stsp size_t len;
1102 84451b3e 2018-07-10 stsp wchar_t *wline;
1103 b700b5d6 2018-07-10 stsp int width, wlimit;
1104 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1105 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1106 ab089a2a 2018-07-12 stsp char *id_str;
1108 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1110 ab089a2a 2018-07-12 stsp return err;
1112 84451b3e 2018-07-10 stsp rewind(f);
1113 84451b3e 2018-07-10 stsp werase(window);
1115 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1116 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1117 ab089a2a 2018-07-12 stsp free(id_str);
1118 ab089a2a 2018-07-12 stsp return err;
1121 ab089a2a 2018-07-12 stsp err = format_line(&wline, &width, line, COLS);
1122 ab089a2a 2018-07-12 stsp free(line);
1123 ab089a2a 2018-07-12 stsp waddwstr(window, wline);
1124 ab089a2a 2018-07-12 stsp if (width < COLS)
1125 ab089a2a 2018-07-12 stsp waddch(window, '\n');
1127 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1128 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1129 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1130 ab089a2a 2018-07-12 stsp free(id_str);
1131 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1133 ab089a2a 2018-07-12 stsp free(id_str);
1134 3f60a8ef 2018-07-10 stsp err = format_line(&wline, &width, line, COLS);
1135 3f60a8ef 2018-07-10 stsp free(line);
1137 3f60a8ef 2018-07-10 stsp return err;
1138 3f60a8ef 2018-07-10 stsp waddwstr(window, wline);
1139 3f60a8ef 2018-07-10 stsp if (width < COLS)
1140 3f60a8ef 2018-07-10 stsp waddch(window, '\n');
1143 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1144 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1145 84451b3e 2018-07-10 stsp if (line == NULL) {
1149 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1150 84451b3e 2018-07-10 stsp free(line);
1154 b700b5d6 2018-07-10 stsp wlimit = COLS < 9 ? 0 : COLS - 9;
1155 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1156 84451b3e 2018-07-10 stsp if (err) {
1157 84451b3e 2018-07-10 stsp free(line);
1158 84451b3e 2018-07-10 stsp return err;
1161 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1162 b700b5d6 2018-07-10 stsp wstandout(window);
1164 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1165 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1166 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1167 ee41ec32 2018-07-10 stsp waddstr(window, " ");
1168 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1169 84451b3e 2018-07-10 stsp char *id_str;
1170 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1171 84451b3e 2018-07-10 stsp if (err) {
1172 84451b3e 2018-07-10 stsp free(line);
1173 84451b3e 2018-07-10 stsp return err;
1175 84451b3e 2018-07-10 stsp wprintw(window, "%.8s ", id_str);
1176 84451b3e 2018-07-10 stsp free(id_str);
1177 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1179 ee41ec32 2018-07-10 stsp waddstr(window, "........ ");
1180 ee41ec32 2018-07-10 stsp prev_id = NULL;
1183 84451b3e 2018-07-10 stsp waddwstr(window, wline);
1184 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1185 b700b5d6 2018-07-10 stsp waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1188 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1189 b700b5d6 2018-07-10 stsp wstandend(window);
1190 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1191 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1192 84451b3e 2018-07-10 stsp free(line);
1194 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1196 84451b3e 2018-07-10 stsp update_panels();
1197 84451b3e 2018-07-10 stsp doupdate();
1199 84451b3e 2018-07-10 stsp return NULL;
1202 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1203 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1204 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1205 84451b3e 2018-07-10 stsp int nlines;
1207 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1209 3f60a8ef 2018-07-10 stsp const char *path;
1210 84451b3e 2018-07-10 stsp int *first_displayed_line;
1211 84451b3e 2018-07-10 stsp int *last_displayed_line;
1212 b700b5d6 2018-07-10 stsp int *selected_line;
1213 18430de3 2018-07-10 stsp int *quit;
1216 84451b3e 2018-07-10 stsp static const struct got_error *
1217 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1219 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1220 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1221 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1224 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1225 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1226 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1228 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1229 84451b3e 2018-07-10 stsp return got_error_from_errno();
1231 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1232 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1233 d68a0a7d 2018-07-10 stsp goto done;
1236 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1237 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1239 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1240 d68a0a7d 2018-07-10 stsp if (line->annotated)
1241 d68a0a7d 2018-07-10 stsp goto done;
1243 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1244 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1245 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1246 84451b3e 2018-07-10 stsp goto done;
1248 84451b3e 2018-07-10 stsp line->annotated = 1;
1250 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1251 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1252 ab089a2a 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1254 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1255 84451b3e 2018-07-10 stsp return got_error_from_errno();
1256 84451b3e 2018-07-10 stsp return err;
1259 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1260 84451b3e 2018-07-10 stsp const char *path;
1261 84451b3e 2018-07-10 stsp struct got_repository *repo;
1262 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1263 18430de3 2018-07-10 stsp int *complete;
1266 84451b3e 2018-07-10 stsp static void *
1267 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1269 18430de3 2018-07-10 stsp const struct got_error *err;
1270 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1271 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1274 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1275 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1276 18430de3 2018-07-10 stsp *ta->complete = 1;
1278 18430de3 2018-07-10 stsp return (void *)err;
1280 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1281 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1283 245d91c1 2018-07-12 stsp err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1284 245d91c1 2018-07-12 stsp a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1285 245d91c1 2018-07-12 stsp a->last_displayed_line, &eof, LINES);
1287 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1288 18430de3 2018-07-10 stsp err = got_error_from_errno();
1290 18430de3 2018-07-10 stsp return (void *)err;
1293 245d91c1 2018-07-12 stsp static struct got_object_id *
1294 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1295 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1297 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1299 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1300 245d91c1 2018-07-12 stsp if (!line->annotated)
1301 245d91c1 2018-07-12 stsp return NULL;
1303 245d91c1 2018-07-12 stsp return line->id;
1306 84451b3e 2018-07-10 stsp static const struct got_error *
1307 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1308 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1309 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1311 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1312 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1313 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1314 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1316 b880a918 2018-07-10 stsp *pobj = NULL;
1317 b880a918 2018-07-10 stsp *obj = NULL;
1319 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1320 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1321 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1322 b880a918 2018-07-10 stsp return NULL;
1324 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1326 b880a918 2018-07-10 stsp goto done;
1328 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1330 b880a918 2018-07-10 stsp goto done;
1332 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1333 b880a918 2018-07-10 stsp if (pid) {
1334 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1336 b880a918 2018-07-10 stsp goto done;
1339 b880a918 2018-07-10 stsp if (commit)
1340 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1341 b880a918 2018-07-10 stsp return err;
1344 245d91c1 2018-07-12 stsp struct tog_blame {
1346 245d91c1 2018-07-12 stsp size_t filesize;
1347 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1348 245d91c1 2018-07-12 stsp size_t nlines;
1349 245d91c1 2018-07-12 stsp pthread_t thread;
1350 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1351 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1352 245d91c1 2018-07-12 stsp const char *path;
1353 245d91c1 2018-07-12 stsp struct got_object_id *commit_id;
1356 b880a918 2018-07-10 stsp static const struct got_error *
1357 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1359 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1362 245d91c1 2018-07-12 stsp if (blame->thread) {
1363 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1364 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1365 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1366 245d91c1 2018-07-12 stsp err = NULL;
1367 245d91c1 2018-07-12 stsp blame->thread = NULL;
1369 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1370 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1371 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1373 245d91c1 2018-07-12 stsp if (blame->f) {
1374 245d91c1 2018-07-12 stsp fclose(blame->f);
1375 245d91c1 2018-07-12 stsp blame->f = NULL;
1377 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1378 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1379 245d91c1 2018-07-12 stsp free(blame->lines);
1380 245d91c1 2018-07-12 stsp blame->lines = NULL;
1381 245d91c1 2018-07-12 stsp free(blame->commit_id);
1382 245d91c1 2018-07-12 stsp blame->commit_id = NULL;
1384 245d91c1 2018-07-12 stsp return err;
1387 245d91c1 2018-07-12 stsp static const struct got_error *
1388 245d91c1 2018-07-12 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1389 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1390 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1391 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1392 245d91c1 2018-07-12 stsp struct got_repository *repo)
1394 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1395 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1396 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1397 245d91c1 2018-07-12 stsp struct got_object *obj;
1399 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1401 84451b3e 2018-07-10 stsp goto done;
1402 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1403 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1404 84451b3e 2018-07-10 stsp goto done;
1407 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1409 a70480e0 2018-06-23 stsp goto done;
1410 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1411 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1412 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1413 84451b3e 2018-07-10 stsp goto done;
1415 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1416 245d91c1 2018-07-12 stsp blame->f, blob);
1418 84451b3e 2018-07-10 stsp goto done;
1420 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1421 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1422 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1423 84451b3e 2018-07-10 stsp goto done;
1426 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1428 bd24772e 2018-07-11 stsp goto done;
1430 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1431 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1432 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1433 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1434 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1435 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1436 245d91c1 2018-07-12 stsp goto done;
1438 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1439 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1440 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1441 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1442 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1443 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1445 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1446 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1447 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1448 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1449 245d91c1 2018-07-12 stsp *blame_complete = 0;
1451 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1452 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1453 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1454 245d91c1 2018-07-12 stsp goto done;
1459 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1461 245d91c1 2018-07-12 stsp got_object_close(obj);
1463 245d91c1 2018-07-12 stsp stop_blame(blame);
1464 245d91c1 2018-07-12 stsp return err;
1467 245d91c1 2018-07-12 stsp static const struct got_error *
1468 245d91c1 2018-07-12 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1469 245d91c1 2018-07-12 stsp struct got_repository *repo)
1471 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1472 245d91c1 2018-07-12 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1473 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1474 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1475 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1476 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1477 245d91c1 2018-07-12 stsp struct tog_blame blame;
1478 245d91c1 2018-07-12 stsp int blame_running = 0;
1479 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1480 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1482 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1484 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1485 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1486 245d91c1 2018-07-12 stsp goto done;
1489 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1491 245d91c1 2018-07-12 stsp goto done;
1492 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1494 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1495 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1496 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1497 a70480e0 2018-06-23 stsp return got_error_from_errno();
1498 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1500 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1501 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1502 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1503 a70480e0 2018-06-23 stsp return got_error_from_errno();
1505 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1507 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1508 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex, &blame_complete,
1509 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1510 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1512 245d91c1 2018-07-12 stsp return err;
1514 a70480e0 2018-06-23 stsp while (!done) {
1515 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1516 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1517 84451b3e 2018-07-10 stsp goto done;
1519 dbc6a6b6 2018-07-12 stsp err = draw_blame(tog_blame_view.window, blamed_commit->id,
1520 245d91c1 2018-07-12 stsp blame.f, path, blame.lines, blame.nlines, blame_complete,
1521 245d91c1 2018-07-12 stsp selected_line, &first_displayed_line, &last_displayed_line,
1522 245d91c1 2018-07-12 stsp &eof, LINES);
1523 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1524 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1525 84451b3e 2018-07-10 stsp goto done;
1529 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1530 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1531 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1532 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1533 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1534 84451b3e 2018-07-10 stsp goto done;
1536 a70480e0 2018-06-23 stsp switch (ch) {
1541 a70480e0 2018-06-23 stsp case KEY_UP:
1542 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1543 b700b5d6 2018-07-10 stsp selected_line--;
1544 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1545 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1546 a70480e0 2018-06-23 stsp first_displayed_line--;
1548 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1549 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1550 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1551 b700b5d6 2018-07-10 stsp selected_line = 1;
1554 084063cd 2018-07-12 stsp if (first_displayed_line > LINES - 2)
1555 084063cd 2018-07-12 stsp first_displayed_line -= (LINES - 2);
1557 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1560 a70480e0 2018-06-23 stsp case KEY_DOWN:
1561 a026b947 2018-07-12 stsp if (selected_line < LINES - 2 &&
1562 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1563 a026b947 2018-07-12 stsp blame.nlines)
1564 b700b5d6 2018-07-10 stsp selected_line++;
1565 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1566 b700b5d6 2018-07-10 stsp first_displayed_line++;
1568 245d91c1 2018-07-12 stsp case 'b': {
1569 245d91c1 2018-07-12 stsp struct got_object_id *id;
1570 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1571 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1572 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1573 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1575 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1576 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1577 245d91c1 2018-07-12 stsp selected_line, repo);
1580 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1583 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1584 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1585 ad6a6c43 2018-07-10 stsp goto done;
1587 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1588 245d91c1 2018-07-12 stsp blame_running = 0;
1590 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1591 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1592 245d91c1 2018-07-12 stsp goto done;
1594 245d91c1 2018-07-12 stsp if (thread_err)
1596 dbc6a6b6 2018-07-12 stsp id = got_object_get_id(obj);
1597 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1598 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1601 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1604 dbc6a6b6 2018-07-12 stsp goto done;
1605 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1606 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1607 245d91c1 2018-07-12 stsp err = run_blame(&blame, &mutex,
1608 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1609 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1610 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1613 dbc6a6b6 2018-07-12 stsp blame_running = 1;
1616 dbc6a6b6 2018-07-12 stsp case 'B': {
1617 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1618 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1619 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1622 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1623 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1624 dbc6a6b6 2018-07-12 stsp goto done;
1626 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1627 dbc6a6b6 2018-07-12 stsp blame_running = 0;
1629 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&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 if (thread_err)
1635 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1636 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1637 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1638 dbc6a6b6 2018-07-12 stsp err = run_blame(&blame, &mutex,
1639 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1640 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1641 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1644 245d91c1 2018-07-12 stsp blame_running = 1;
1647 245d91c1 2018-07-12 stsp case KEY_ENTER:
1648 245d91c1 2018-07-12 stsp case '\r':
1649 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1650 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1651 245d91c1 2018-07-12 stsp selected_line, repo);
1654 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1656 b880a918 2018-07-10 stsp err = show_diff_view(pobj, obj, repo);
1657 b880a918 2018-07-10 stsp if (pobj) {
1658 b880a918 2018-07-10 stsp got_object_close(pobj);
1659 b880a918 2018-07-10 stsp pobj = NULL;
1661 b880a918 2018-07-10 stsp got_object_close(obj);
1662 b880a918 2018-07-10 stsp obj = NULL;
1663 ad6a6c43 2018-07-10 stsp show_panel(tog_blame_view.panel);
1667 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1669 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1670 084063cd 2018-07-12 stsp selected_line < LINES - 2) {
1671 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1672 d2dfcfbf 2018-07-12 stsp LINES - 2);
1675 245d91c1 2018-07-12 stsp if (last_displayed_line + LINES - 2 <=
1676 245d91c1 2018-07-12 stsp blame.nlines)
1677 084063cd 2018-07-12 stsp first_displayed_line += LINES - 2;
1679 b700b5d6 2018-07-10 stsp first_displayed_line =
1680 245d91c1 2018-07-12 stsp blame.nlines - (LINES - 3);
1685 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
1686 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1687 245d91c1 2018-07-12 stsp if (err || thread_err)
1692 b880a918 2018-07-10 stsp got_object_close(pobj);
1693 245d91c1 2018-07-12 stsp if (blame_running)
1694 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1695 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1696 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1697 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1698 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1700 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
1703 a70480e0 2018-06-23 stsp static const struct got_error *
1704 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1706 a70480e0 2018-06-23 stsp const struct got_error *error;
1707 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1708 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1709 a70480e0 2018-06-23 stsp char *path = NULL;
1710 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1711 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1714 a70480e0 2018-06-23 stsp #ifndef PROFILE
1715 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1716 a70480e0 2018-06-23 stsp err(1, "pledge");
1719 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1720 a70480e0 2018-06-23 stsp switch (ch) {
1722 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1726 a70480e0 2018-06-23 stsp /* NOTREACHED */
1730 a70480e0 2018-06-23 stsp argc -= optind;
1731 a70480e0 2018-06-23 stsp argv += optind;
1733 a70480e0 2018-06-23 stsp if (argc == 0) {
1734 a70480e0 2018-06-23 stsp usage_blame();
1735 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1736 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1737 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1738 a70480e0 2018-06-23 stsp return got_error_from_errno();
1739 a70480e0 2018-06-23 stsp path = argv[0];
1740 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1741 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1742 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1743 a70480e0 2018-06-23 stsp return got_error_from_errno();
1744 a70480e0 2018-06-23 stsp path = argv[1];
1746 a70480e0 2018-06-23 stsp usage_blame();
1748 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1749 a70480e0 2018-06-23 stsp free(repo_path);
1750 a70480e0 2018-06-23 stsp if (error != NULL)
1751 66b4983c 2018-06-23 stsp return error;
1753 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1754 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1755 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1756 a70480e0 2018-06-23 stsp if (error != NULL)
1757 66b4983c 2018-06-23 stsp goto done;
1758 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1759 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1761 a70480e0 2018-06-23 stsp struct got_object *obj;
1762 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1763 a70480e0 2018-06-23 stsp if (error != NULL)
1764 66b4983c 2018-06-23 stsp goto done;
1765 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1766 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1767 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1768 a19e88aa 2018-06-23 stsp got_object_close(obj);
1770 a19e88aa 2018-06-23 stsp if (error != NULL)
1771 a19e88aa 2018-06-23 stsp goto done;
1773 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1775 a70480e0 2018-06-23 stsp free(commit_id);
1777 a70480e0 2018-06-23 stsp got_repo_close(repo);
1778 a70480e0 2018-06-23 stsp return error;
1781 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1782 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1783 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1784 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1785 1d13200f 2018-07-12 stsp WINDOW *window, const char *label, int show_ids, const char *parent_path,
1786 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
1788 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1789 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1790 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1791 ffd1d5e5 2018-06-23 stsp int width, n;
1793 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1795 ffd1d5e5 2018-06-23 stsp werase(window);
1797 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1798 ffd1d5e5 2018-06-23 stsp return NULL;
1800 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1802 ffd1d5e5 2018-06-23 stsp return err;
1803 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1804 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1805 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1806 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1807 ffd1d5e5 2018-06-23 stsp return NULL;
1808 ce52c690 2018-06-23 stsp err = format_line(&wline, &width, parent_path, COLS);
1810 ce52c690 2018-06-23 stsp return err;
1811 ce52c690 2018-06-23 stsp waddwstr(window, wline);
1812 ce52c690 2018-06-23 stsp if (width < COLS)
1813 ce52c690 2018-06-23 stsp waddch(window, '\n');
1814 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1815 ffd1d5e5 2018-06-23 stsp return NULL;
1816 a1eca9bb 2018-06-23 stsp waddch(window, '\n');
1817 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
1818 a1eca9bb 2018-06-23 stsp return NULL;
1820 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1821 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1822 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1823 ffd1d5e5 2018-06-23 stsp wstandout(window);
1824 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1826 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1827 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1828 ffd1d5e5 2018-06-23 stsp wstandend(window);
1829 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1830 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1831 ffd1d5e5 2018-06-23 stsp return NULL;
1835 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1836 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1839 ffd1d5e5 2018-06-23 stsp while (te) {
1840 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
1842 1d13200f 2018-07-12 stsp if (show_ids) {
1843 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
1845 1d13200f 2018-07-12 stsp return got_error_from_errno();
1847 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1848 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1849 1d13200f 2018-07-12 stsp free(id_str);
1850 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1852 1d13200f 2018-07-12 stsp free(id_str);
1853 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1854 ffd1d5e5 2018-06-23 stsp if (err) {
1855 ffd1d5e5 2018-06-23 stsp free(line);
1858 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1859 ffd1d5e5 2018-06-23 stsp wstandout(window);
1860 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1862 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1863 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1864 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1865 ffd1d5e5 2018-06-23 stsp if (n == selected)
1866 ffd1d5e5 2018-06-23 stsp wstandend(window);
1867 ffd1d5e5 2018-06-23 stsp free(line);
1869 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1870 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1871 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1873 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1876 ffd1d5e5 2018-06-23 stsp return err;
1879 ffd1d5e5 2018-06-23 stsp static void
1880 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1881 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1883 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1886 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1889 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1890 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1891 ffd1d5e5 2018-06-23 stsp if (!isroot)
1892 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1896 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1897 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1898 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1899 ffd1d5e5 2018-06-23 stsp prev = te;
1900 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1902 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1903 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1905 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1906 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1909 ffd1d5e5 2018-06-23 stsp static void
1910 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1911 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1912 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1914 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1915 ffd1d5e5 2018-06-23 stsp int n = 0;
1917 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1920 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1921 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1923 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1924 ffd1d5e5 2018-06-23 stsp while (next) {
1925 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1926 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1928 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1932 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1933 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
1934 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1935 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1936 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1937 ffd1d5e5 2018-06-23 stsp int selected;
1940 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1942 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1943 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
1944 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
1946 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
1947 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1948 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1950 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
1951 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1953 ce52c690 2018-06-23 stsp len += strlen(te->name);
1955 ce52c690 2018-06-23 stsp *path = calloc(1, len);
1956 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1957 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1959 ce52c690 2018-06-23 stsp (*path)[0] = '/';
1960 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
1961 d9765a41 2018-06-23 stsp while (pt) {
1962 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1963 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1964 cb2ebc8a 2018-06-23 stsp goto done;
1966 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
1967 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1968 cb2ebc8a 2018-06-23 stsp goto done;
1970 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1973 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
1974 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1975 ce52c690 2018-06-23 stsp goto done;
1979 ce52c690 2018-06-23 stsp if (err) {
1980 ce52c690 2018-06-23 stsp free(*path);
1981 ce52c690 2018-06-23 stsp *path = NULL;
1983 ce52c690 2018-06-23 stsp return err;
1986 ce52c690 2018-06-23 stsp static const struct got_error *
1987 ce52c690 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1988 ce52c690 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1990 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
1991 ce52c690 2018-06-23 stsp char *path;
1993 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
1995 ce52c690 2018-06-23 stsp return err;
1997 cb2ebc8a 2018-06-23 stsp err = show_blame_view(path, commit_id, repo);
1998 cb2ebc8a 2018-06-23 stsp free(path);
1999 cb2ebc8a 2018-06-23 stsp return err;
2002 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2003 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2004 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
2006 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2007 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2008 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2009 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2010 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2011 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2012 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2013 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2014 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2015 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2017 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2019 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2020 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2021 ffd1d5e5 2018-06-23 stsp goto done;
2023 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2024 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2025 ffd1d5e5 2018-06-23 stsp goto done;
2028 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
2029 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
2030 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
2031 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2032 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
2034 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
2035 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
2036 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
2037 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2039 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
2041 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2042 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2043 ffd1d5e5 2018-06-23 stsp while (!done) {
2044 ce52c690 2018-06-23 stsp char *parent_path;
2045 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2046 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2047 ffd1d5e5 2018-06-23 stsp if (tree != root)
2048 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2050 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2052 ce52c690 2018-06-23 stsp goto done;
2054 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
2055 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2056 1d13200f 2018-07-12 stsp tog_tree_view.window, tree_label, show_ids,
2057 1d13200f 2018-07-12 stsp parent_path, entries, selected, LINES, tree == root);
2058 ce52c690 2018-06-23 stsp free(parent_path);
2062 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2063 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
2064 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2065 ffd1d5e5 2018-06-23 stsp switch (ch) {
2070 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2073 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2074 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2075 ffd1d5e5 2018-06-23 stsp selected--;
2076 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2078 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2079 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2081 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2082 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2083 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2084 cf8f1261 2018-06-23 stsp if (tree != root)
2085 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2086 ffd1d5e5 2018-06-23 stsp selected = 0;
2089 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
2090 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2093 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2094 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2095 ffd1d5e5 2018-06-23 stsp selected++;
2098 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2099 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2101 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2102 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
2103 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2104 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2106 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2107 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2108 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2110 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2111 ffd1d5e5 2018-06-23 stsp case '\r':
2112 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2113 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2114 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2115 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2116 ffd1d5e5 2018-06-23 stsp if (tree == root)
2118 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2119 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2120 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2121 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2122 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2123 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2124 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2125 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2126 ffd1d5e5 2018-06-23 stsp free(parent);
2127 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2128 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2129 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2130 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2131 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2133 ffd1d5e5 2018-06-23 stsp goto done;
2134 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2135 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2136 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2137 ffd1d5e5 2018-06-23 stsp goto done;
2139 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2140 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2141 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2142 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2143 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2144 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2146 ffd1d5e5 2018-06-23 stsp tree = child;
2147 ffd1d5e5 2018-06-23 stsp selected = 0;
2148 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2149 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2150 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
2151 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
2153 ffd1d5e5 2018-06-23 stsp goto done;
2156 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2157 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
2158 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2165 ffd1d5e5 2018-06-23 stsp free(tree_label);
2166 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2167 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2168 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2169 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2170 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2171 ffd1d5e5 2018-06-23 stsp free(parent);
2174 ffd1d5e5 2018-06-23 stsp if (tree != root)
2175 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2176 ffd1d5e5 2018-06-23 stsp return err;
2179 ffd1d5e5 2018-06-23 stsp __dead static void
2180 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2183 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2184 ffd1d5e5 2018-06-23 stsp getprogname());
2188 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2189 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2191 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2192 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2193 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2194 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2195 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2196 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2197 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2200 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2201 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2202 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2205 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2206 ffd1d5e5 2018-06-23 stsp switch (ch) {
2208 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2212 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2216 ffd1d5e5 2018-06-23 stsp argc -= optind;
2217 ffd1d5e5 2018-06-23 stsp argv += optind;
2219 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2220 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2221 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2222 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2223 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2224 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2225 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2226 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2228 ffd1d5e5 2018-06-23 stsp usage_log();
2230 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2231 ffd1d5e5 2018-06-23 stsp free(repo_path);
2232 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2233 ffd1d5e5 2018-06-23 stsp return error;
2235 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2236 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2237 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2238 ffd1d5e5 2018-06-23 stsp goto done;
2240 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2241 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2242 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2243 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2244 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2245 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2248 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2249 ffd1d5e5 2018-06-23 stsp goto done;
2251 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2252 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2253 ffd1d5e5 2018-06-23 stsp goto done;
2255 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2256 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2257 ffd1d5e5 2018-06-23 stsp goto done;
2259 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
2261 ffd1d5e5 2018-06-23 stsp free(commit_id);
2262 ffd1d5e5 2018-06-23 stsp if (commit)
2263 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2265 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2267 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2268 ffd1d5e5 2018-06-23 stsp return error;
2270 5c5136c5 2018-05-20 stsp static void
2271 9f7d7167 2018-04-29 stsp init_curses(void)
2273 9f7d7167 2018-04-29 stsp initscr();
2277 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2278 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2279 1f475ad8 2018-05-10 stsp curs_set(0);
2282 4ed7e80c 2018-05-20 stsp __dead static void
2283 9f7d7167 2018-04-29 stsp usage(void)
2287 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2288 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2289 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2290 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2291 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2296 c2301be8 2018-04-30 stsp static char **
2297 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2299 c2301be8 2018-04-30 stsp char **argv;
2300 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2302 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2303 c2301be8 2018-04-30 stsp if (argv == NULL)
2304 c2301be8 2018-04-30 stsp err(1, "calloc");
2305 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2306 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2307 c2301be8 2018-04-30 stsp err(1, "calloc");
2308 c2301be8 2018-04-30 stsp if (arg1) {
2309 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2310 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2311 c2301be8 2018-04-30 stsp err(1, "calloc");
2314 c2301be8 2018-04-30 stsp return argv;
2318 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2320 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2321 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2322 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2323 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2325 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2327 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2328 9f7d7167 2018-04-29 stsp switch (ch) {
2330 9f7d7167 2018-04-29 stsp hflag = 1;
2334 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2338 9f7d7167 2018-04-29 stsp argc -= optind;
2339 9f7d7167 2018-04-29 stsp argv += optind;
2340 9f7d7167 2018-04-29 stsp optind = 0;
2341 c2301be8 2018-04-30 stsp optreset = 1;
2343 c2301be8 2018-04-30 stsp if (argc == 0) {
2344 f29d3e89 2018-06-23 stsp if (hflag)
2346 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2347 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2348 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2353 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2354 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2355 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2356 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2357 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2358 9f7d7167 2018-04-29 stsp if (hflag)
2359 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2363 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2364 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2365 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2366 c2301be8 2018-04-30 stsp if (repo_path) {
2367 c2301be8 2018-04-30 stsp struct got_repository *repo;
2368 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2369 c2301be8 2018-04-30 stsp if (error == NULL)
2370 c2301be8 2018-04-30 stsp got_repo_close(repo);
2372 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2373 c2301be8 2018-04-30 stsp if (error) {
2374 f29d3e89 2018-06-23 stsp if (hflag) {
2375 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2376 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2380 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2381 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2382 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2383 ad7de8d9 2018-04-30 stsp free(repo_path);
2386 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2387 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2389 c2301be8 2018-04-30 stsp free(repo_path);
2393 5c5136c5 2018-05-20 stsp init_curses();
2395 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2396 9f7d7167 2018-04-29 stsp if (error)
2397 9f7d7167 2018-04-29 stsp goto done;
2400 c2301be8 2018-04-30 stsp free(cmd_argv);
2401 9f7d7167 2018-04-29 stsp if (error)
2402 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);