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>
37 9f7d7167 2018-04-29 stsp #include "got_error.h"
38 80ddbec8 2018-04-29 stsp #include "got_object.h"
39 80ddbec8 2018-04-29 stsp #include "got_reference.h"
40 80ddbec8 2018-04-29 stsp #include "got_repository.h"
41 80ddbec8 2018-04-29 stsp #include "got_diff.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
44 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
45 a70480e0 2018-06-23 stsp #include "got_blame.h"
48 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 9f7d7167 2018-04-29 stsp #ifndef nitems
52 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 9f7d7167 2018-04-29 stsp struct tog_cmd {
56 c2301be8 2018-04-30 stsp const char *name;
57 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
58 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
59 c2301be8 2018-04-30 stsp const char *descr;
62 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
63 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
66 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
68 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
71 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
73 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
74 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
75 9f7d7167 2018-04-29 stsp "show repository history" },
76 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
77 9f7d7167 2018-04-29 stsp "compare files and directories" },
78 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
79 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
80 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
81 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
84 fed328cc 2018-05-20 stsp static struct tog_view {
85 26ed57b2 2018-05-19 stsp WINDOW *window;
86 26ed57b2 2018-05-19 stsp PANEL *panel;
87 ffd1d5e5 2018-06-23 stsp } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
89 cd0acaa7 2018-05-20 stsp static const struct got_error *
90 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
91 cd0acaa7 2018-05-20 stsp struct got_repository *);
92 cd0acaa7 2018-05-20 stsp static const struct got_error *
93 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
94 a70480e0 2018-06-23 stsp static const struct got_error *
95 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error *
97 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *, struct got_object_id *,
98 ffd1d5e5 2018-06-23 stsp struct got_repository *);
100 4ed7e80c 2018-05-20 stsp __dead static void
101 9f7d7167 2018-04-29 stsp usage_log(void)
104 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
105 9f7d7167 2018-04-29 stsp getprogname());
109 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
110 80ddbec8 2018-04-29 stsp static const struct got_error *
111 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
113 00dfcb92 2018-06-11 stsp char *vis = NULL;
114 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
116 963b370f 2018-05-20 stsp *ws = NULL;
117 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
118 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
119 00dfcb92 2018-06-11 stsp int vislen;
120 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
121 00dfcb92 2018-06-11 stsp return got_error_from_errno();
123 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
124 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
126 00dfcb92 2018-06-11 stsp return err;
127 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
128 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
129 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
134 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
135 a7f50699 2018-06-11 stsp if (*ws == NULL) {
136 a7f50699 2018-06-11 stsp err = got_error_from_errno();
140 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
141 963b370f 2018-05-20 stsp err = got_error_from_errno();
146 963b370f 2018-05-20 stsp *ws = NULL;
149 963b370f 2018-05-20 stsp return err;
152 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
153 963b370f 2018-05-20 stsp static const struct got_error *
154 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
156 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
157 963b370f 2018-05-20 stsp int cols = 0;
158 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
159 963b370f 2018-05-20 stsp size_t wlen;
162 963b370f 2018-05-20 stsp *wlinep = NULL;
164 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
166 963b370f 2018-05-20 stsp return err;
169 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
170 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
171 963b370f 2018-05-20 stsp switch (width) {
176 963b370f 2018-05-20 stsp cols += width;
179 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
180 963b370f 2018-05-20 stsp cols += TABSIZE;
183 963b370f 2018-05-20 stsp err = got_error_from_errno();
186 963b370f 2018-05-20 stsp if (cols <= COLS) {
188 963b370f 2018-05-20 stsp if (widthp)
189 963b370f 2018-05-20 stsp *widthp = cols;
192 963b370f 2018-05-20 stsp wline[i] = L'\0';
195 963b370f 2018-05-20 stsp free(wline);
197 963b370f 2018-05-20 stsp *wlinep = wline;
198 963b370f 2018-05-20 stsp return err;
201 963b370f 2018-05-20 stsp static const struct got_error *
202 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
204 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
205 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
206 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
207 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
208 bb737323 2018-05-20 stsp int author_width, logmsg_width;
209 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
210 80ddbec8 2018-04-29 stsp char *line = NULL;
211 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
212 bb737323 2018-05-20 stsp size_t id_len;
213 bb737323 2018-05-20 stsp int col, limit;
214 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
215 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
216 9c2eaf34 2018-05-20 stsp const int avail = COLS;
218 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
220 80ddbec8 2018-04-29 stsp return err;
221 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
222 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
223 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
224 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
226 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
227 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
229 bb737323 2018-05-20 stsp col = limit + 1;
230 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
231 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
234 9c2eaf34 2018-05-20 stsp if (col > avail)
237 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
238 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
239 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
242 6d9fbc00 2018-04-29 stsp author = author0;
243 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
244 6d9fbc00 2018-04-29 stsp if (smallerthan)
245 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
247 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
249 6d9fbc00 2018-04-29 stsp *at = '\0';
251 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
252 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
255 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
256 bb737323 2018-05-20 stsp col += author_width;
257 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
258 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
260 bb737323 2018-05-20 stsp author_width++;
262 9c2eaf34 2018-05-20 stsp if (col > avail)
265 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
266 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
267 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
270 bb737323 2018-05-20 stsp logmsg = logmsg0;
271 bb737323 2018-05-20 stsp while (*logmsg == '\n')
273 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
274 bb737323 2018-05-20 stsp if (newline)
275 bb737323 2018-05-20 stsp *newline = '\0';
276 bb737323 2018-05-20 stsp limit = avail - col;
277 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
280 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
281 bb737323 2018-05-20 stsp col += logmsg_width;
282 bb737323 2018-05-20 stsp while (col <= avail) {
283 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
287 80ddbec8 2018-04-29 stsp free(logmsg0);
288 bb737323 2018-05-20 stsp free(wlogmsg);
289 6d9fbc00 2018-04-29 stsp free(author0);
290 bb737323 2018-05-20 stsp free(wauthor);
291 80ddbec8 2018-04-29 stsp free(line);
292 6d9fbc00 2018-04-29 stsp free(id_str);
293 80ddbec8 2018-04-29 stsp return err;
296 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
297 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
298 80ddbec8 2018-04-29 stsp struct got_object_id *id;
299 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
301 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
303 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
304 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
305 899d86c2 2018-05-10 stsp struct got_object_id *id)
307 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
309 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
310 80ddbec8 2018-04-29 stsp if (entry == NULL)
311 899d86c2 2018-05-10 stsp return NULL;
313 899d86c2 2018-05-10 stsp entry->id = id;
314 99db9666 2018-05-07 stsp entry->commit = commit;
315 899d86c2 2018-05-10 stsp return entry;
318 99db9666 2018-05-07 stsp static void
319 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
321 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
323 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
324 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
325 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
326 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
327 99db9666 2018-05-07 stsp free(entry);
330 99db9666 2018-05-07 stsp static void
331 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
333 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
334 99db9666 2018-05-07 stsp pop_commit(commits);
337 c4972b91 2018-05-07 stsp static const struct got_error *
338 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
339 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
341 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
342 899d86c2 2018-05-10 stsp struct got_object_id *id;
343 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
345 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
347 c4972b91 2018-05-07 stsp return err;
349 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
350 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
351 9ba79e04 2018-06-11 stsp int nfetched;
353 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
354 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
355 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
356 9ba79e04 2018-06-11 stsp return err;
358 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
360 9ba79e04 2018-06-11 stsp return err;
363 9ba79e04 2018-06-11 stsp while (1) {
364 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
366 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
368 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
369 9ba79e04 2018-06-11 stsp err = NULL;
373 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
377 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
378 9ba79e04 2018-06-11 stsp if (entry == NULL) {
379 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
383 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
386 9ba79e04 2018-06-11 stsp return err;
389 99db9666 2018-05-07 stsp static const struct got_error *
390 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
391 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
392 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
394 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
395 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
397 9ba79e04 2018-06-11 stsp *pentry = NULL;
399 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
400 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
401 9ba79e04 2018-06-11 stsp int nfetched;
402 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
403 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
405 9ba79e04 2018-06-11 stsp return err;
408 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
409 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
411 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
412 9ba79e04 2018-06-11 stsp err = NULL;
413 9ba79e04 2018-06-11 stsp return err;
416 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
417 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
418 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
419 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
421 9ba79e04 2018-06-11 stsp return NULL;
424 899d86c2 2018-05-10 stsp static const struct got_error *
425 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
427 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
428 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
430 9ba79e04 2018-06-11 stsp *head_id = NULL;
432 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
434 99db9666 2018-05-07 stsp return err;
436 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
437 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
439 9ba79e04 2018-06-11 stsp *head_id = NULL;
440 99db9666 2018-05-07 stsp return err;
443 9ba79e04 2018-06-11 stsp return NULL;
446 0553a4e3 2018-04-30 stsp static const struct got_error *
447 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
448 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
450 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
451 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
452 0553a4e3 2018-04-30 stsp int ncommits = 0;
454 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
456 899d86c2 2018-05-10 stsp entry = first;
457 899d86c2 2018-05-10 stsp *last = first;
458 899d86c2 2018-05-10 stsp while (entry) {
459 899d86c2 2018-05-10 stsp if (ncommits == limit)
461 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
462 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
463 cd0acaa7 2018-05-20 stsp *selected = entry;
465 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
466 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
467 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
470 0553a4e3 2018-04-30 stsp ncommits++;
471 899d86c2 2018-05-10 stsp *last = entry;
472 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
475 80ddbec8 2018-04-29 stsp update_panels();
476 80ddbec8 2018-04-29 stsp doupdate();
478 80ddbec8 2018-04-29 stsp return err;
481 07b55e75 2018-05-10 stsp static void
482 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 07b55e75 2018-05-10 stsp struct commit_queue *commits)
485 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
486 07b55e75 2018-05-10 stsp int nscrolled = 0;
488 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
489 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
492 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
493 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
494 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
495 07b55e75 2018-05-10 stsp if (entry) {
496 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
497 07b55e75 2018-05-10 stsp nscrolled++;
502 aa075928 2018-05-10 stsp static const struct got_error *
503 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
505 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
506 9ba79e04 2018-06-11 stsp struct got_repository *repo)
508 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
509 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
510 aa075928 2018-05-10 stsp int nscrolled = 0;
513 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
514 aa075928 2018-05-10 stsp if (pentry == NULL) {
515 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
516 9ba79e04 2018-06-11 stsp commits, graph, repo);
517 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
520 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
522 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
523 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
525 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
526 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
528 dd0a52c1 2018-05-20 stsp return err;
532 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
534 4a7f7875 2018-05-10 stsp int nparents = 0;
536 4a7f7875 2018-05-10 stsp while (entry) {
537 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
538 4a7f7875 2018-05-10 stsp nparents++;
541 4a7f7875 2018-05-10 stsp return nparents;
544 cd0acaa7 2018-05-20 stsp static const struct got_error *
545 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 cd0acaa7 2018-05-20 stsp const struct got_error *err;
548 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
549 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
551 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
553 cd0acaa7 2018-05-20 stsp return err;
555 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
556 9ba79e04 2018-06-11 stsp if (parent_id) {
557 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
562 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
565 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
567 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
568 cd0acaa7 2018-05-20 stsp return err;
571 80ddbec8 2018-04-29 stsp static const struct got_error *
572 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
574 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
575 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
576 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
577 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
578 0553a4e3 2018-04-30 stsp struct commit_queue commits;
579 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
580 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
581 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
582 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
584 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
585 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
586 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
587 80ddbec8 2018-04-29 stsp return got_error_from_errno();
588 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
590 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
591 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
592 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
593 80ddbec8 2018-04-29 stsp return got_error_from_errno();
595 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
597 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
599 9ba79e04 2018-06-11 stsp return err;
601 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
603 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
607 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
608 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
612 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
617 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
618 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
619 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
620 9ba79e04 2018-06-11 stsp * updating the display.
622 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
623 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
626 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
627 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
628 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
629 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
633 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
634 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
638 899d86c2 2018-05-10 stsp while (!done) {
639 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
640 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
644 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
645 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
646 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
647 80ddbec8 2018-04-29 stsp switch (ch) {
649 31120ada 2018-04-30 stsp if (errno) {
650 31120ada 2018-04-30 stsp err = got_error_from_errno();
658 80ddbec8 2018-04-29 stsp case KEY_UP:
659 80ddbec8 2018-04-29 stsp if (selected > 0)
660 80ddbec8 2018-04-29 stsp selected--;
661 8ec9698d 2018-05-10 stsp if (selected > 0)
663 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
665 48531068 2018-05-10 stsp case KEY_PPAGE:
666 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
667 dfc1d240 2018-05-10 stsp first_displayed_entry) {
668 dfc1d240 2018-05-10 stsp selected = 0;
671 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
675 80ddbec8 2018-04-29 stsp case KEY_DOWN:
676 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
677 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
678 15c91275 2018-05-20 stsp selected < nparents - 1) {
679 15c91275 2018-05-20 stsp selected++;
682 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
683 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
688 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
689 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
690 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
694 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
696 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
697 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
698 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
699 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
700 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
702 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
703 31120ada 2018-04-30 stsp if (selected > LINES)
704 31120ada 2018-04-30 stsp selected = LINES - 1;
706 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
708 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
711 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
718 9ba79e04 2018-06-11 stsp free(head_id);
720 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
721 0553a4e3 2018-04-30 stsp free_commits(&commits);
722 80ddbec8 2018-04-29 stsp return err;
725 4ed7e80c 2018-05-20 stsp static const struct got_error *
726 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
728 80ddbec8 2018-04-29 stsp const struct got_error *error;
729 80ddbec8 2018-04-29 stsp struct got_repository *repo;
730 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
731 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
732 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
735 80ddbec8 2018-04-29 stsp #ifndef PROFILE
736 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
737 80ddbec8 2018-04-29 stsp err(1, "pledge");
740 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
741 80ddbec8 2018-04-29 stsp switch (ch) {
743 80ddbec8 2018-04-29 stsp start_commit = optarg;
747 80ddbec8 2018-04-29 stsp /* NOTREACHED */
751 80ddbec8 2018-04-29 stsp argc -= optind;
752 80ddbec8 2018-04-29 stsp argv += optind;
754 80ddbec8 2018-04-29 stsp if (argc == 0) {
755 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
756 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
757 80ddbec8 2018-04-29 stsp return got_error_from_errno();
758 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
759 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
760 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
761 80ddbec8 2018-04-29 stsp return got_error_from_errno();
763 80ddbec8 2018-04-29 stsp usage_log();
765 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
766 80ddbec8 2018-04-29 stsp free(repo_path);
767 80ddbec8 2018-04-29 stsp if (error != NULL)
768 80ddbec8 2018-04-29 stsp return error;
770 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
771 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
772 80ddbec8 2018-04-29 stsp if (error != NULL)
773 80ddbec8 2018-04-29 stsp return error;
775 80ddbec8 2018-04-29 stsp struct got_object *obj;
776 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
777 80ddbec8 2018-04-29 stsp if (error == NULL) {
778 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
779 899d86c2 2018-05-10 stsp if (start_id == NULL)
780 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
783 80ddbec8 2018-04-29 stsp if (error != NULL)
784 80ddbec8 2018-04-29 stsp return error;
785 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
786 899d86c2 2018-05-10 stsp free(start_id);
787 80ddbec8 2018-04-29 stsp got_repo_close(repo);
788 80ddbec8 2018-04-29 stsp return error;
791 4ed7e80c 2018-05-20 stsp __dead static void
792 9f7d7167 2018-04-29 stsp usage_diff(void)
795 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
796 9f7d7167 2018-04-29 stsp getprogname());
800 b304db33 2018-05-20 stsp static char *
801 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
803 b304db33 2018-05-20 stsp char *line;
804 b304db33 2018-05-20 stsp size_t linelen;
805 b304db33 2018-05-20 stsp size_t lineno;
806 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
808 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
810 b304db33 2018-05-20 stsp *len = linelen;
811 b304db33 2018-05-20 stsp return line;
814 4ed7e80c 2018-05-20 stsp static const struct got_error *
815 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
816 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
818 61e69b96 2018-05-20 stsp const struct got_error *err;
819 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
820 b304db33 2018-05-20 stsp char *line;
821 b304db33 2018-05-20 stsp size_t len;
822 61e69b96 2018-05-20 stsp wchar_t *wline;
826 a70480e0 2018-06-23 stsp werase(window);
829 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
830 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
831 26ed57b2 2018-05-19 stsp if (line == NULL) {
835 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
836 26ed57b2 2018-05-19 stsp free(line);
840 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
842 61e69b96 2018-05-20 stsp free(line);
843 61e69b96 2018-05-20 stsp return err;
845 a70480e0 2018-06-23 stsp waddwstr(window, wline);
846 e0b650dd 2018-05-20 stsp if (width < COLS)
847 a70480e0 2018-06-23 stsp waddch(window, '\n');
848 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
849 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
850 26ed57b2 2018-05-19 stsp free(line);
852 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
854 26ed57b2 2018-05-19 stsp update_panels();
855 26ed57b2 2018-05-19 stsp doupdate();
857 26ed57b2 2018-05-19 stsp return NULL;
860 4ed7e80c 2018-05-20 stsp static const struct got_error *
861 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
862 26ed57b2 2018-05-19 stsp struct got_repository *repo)
864 26ed57b2 2018-05-19 stsp const struct got_error *err;
866 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
867 b304db33 2018-05-20 stsp int eof, i;
869 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
870 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
871 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
873 511a516b 2018-05-19 stsp f = got_opentemp();
874 26ed57b2 2018-05-19 stsp if (f == NULL)
875 26ed57b2 2018-05-19 stsp return got_error_from_errno();
877 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
878 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
879 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
881 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
882 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
884 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
885 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
888 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
893 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
894 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
895 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
896 26ed57b2 2018-05-19 stsp return got_error_from_errno();
897 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
899 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
900 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
901 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
902 26ed57b2 2018-05-19 stsp return got_error_from_errno();
904 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
906 26ed57b2 2018-05-19 stsp while (!done) {
907 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
908 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
911 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
912 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
913 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
914 26ed57b2 2018-05-19 stsp switch (ch) {
919 26ed57b2 2018-05-19 stsp case KEY_UP:
920 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
921 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
922 b304db33 2018-05-20 stsp first_displayed_line--;
924 b304db33 2018-05-20 stsp case KEY_PPAGE:
926 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
927 d56caa55 2018-05-20 stsp first_displayed_line > 1)
928 26ed57b2 2018-05-19 stsp first_displayed_line--;
931 26ed57b2 2018-05-19 stsp case KEY_DOWN:
932 925e6f23 2018-05-20 stsp case KEY_ENTER:
935 26ed57b2 2018-05-19 stsp first_displayed_line++;
937 b304db33 2018-05-20 stsp case KEY_NPAGE:
940 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
941 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
942 b304db33 2018-05-20 stsp first_displayed_line++;
943 b304db33 2018-05-20 stsp if (line == NULL)
952 26ed57b2 2018-05-19 stsp return err;
955 4ed7e80c 2018-05-20 stsp static const struct got_error *
956 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
958 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
959 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
960 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
961 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
962 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
965 26ed57b2 2018-05-19 stsp #ifndef PROFILE
966 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
967 26ed57b2 2018-05-19 stsp err(1, "pledge");
970 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
971 26ed57b2 2018-05-19 stsp switch (ch) {
974 26ed57b2 2018-05-19 stsp /* NOTREACHED */
978 26ed57b2 2018-05-19 stsp argc -= optind;
979 26ed57b2 2018-05-19 stsp argv += optind;
981 26ed57b2 2018-05-19 stsp if (argc == 0) {
982 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
983 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
984 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
985 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
986 26ed57b2 2018-05-19 stsp return got_error_from_errno();
987 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
988 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
989 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
990 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
991 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
992 26ed57b2 2018-05-19 stsp return got_error_from_errno();
993 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
994 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
996 26ed57b2 2018-05-19 stsp usage_diff();
998 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
999 26ed57b2 2018-05-19 stsp free(repo_path);
1000 26ed57b2 2018-05-19 stsp if (error)
1001 26ed57b2 2018-05-19 stsp goto done;
1003 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1004 26ed57b2 2018-05-19 stsp if (error)
1005 26ed57b2 2018-05-19 stsp goto done;
1007 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1008 26ed57b2 2018-05-19 stsp if (error)
1009 26ed57b2 2018-05-19 stsp goto done;
1011 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1013 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1015 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1017 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1018 26ed57b2 2018-05-19 stsp return error;
1021 4ed7e80c 2018-05-20 stsp __dead static void
1022 9f7d7167 2018-04-29 stsp usage_blame(void)
1025 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1026 9f7d7167 2018-04-29 stsp getprogname());
1030 4ed7e80c 2018-05-20 stsp static const struct got_error *
1031 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1032 a70480e0 2018-06-23 stsp struct got_repository *repo)
1034 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1036 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1037 a70480e0 2018-06-23 stsp int eof, i;
1039 a70480e0 2018-06-23 stsp f = got_opentemp();
1040 a70480e0 2018-06-23 stsp if (f == NULL)
1041 a70480e0 2018-06-23 stsp return got_error_from_errno();
1043 a70480e0 2018-06-23 stsp err = got_blame(path, commit_id, repo, f);
1045 a70480e0 2018-06-23 stsp goto done;
1047 a70480e0 2018-06-23 stsp fflush(f);
1049 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1050 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1051 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1052 a70480e0 2018-06-23 stsp return got_error_from_errno();
1053 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1055 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1056 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1057 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1058 a70480e0 2018-06-23 stsp return got_error_from_errno();
1060 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1062 a70480e0 2018-06-23 stsp while (!done) {
1063 a70480e0 2018-06-23 stsp err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1064 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
1067 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1068 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1069 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1070 a70480e0 2018-06-23 stsp switch (ch) {
1075 a70480e0 2018-06-23 stsp case KEY_UP:
1076 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1077 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1078 a70480e0 2018-06-23 stsp first_displayed_line--;
1080 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1082 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1083 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1084 a70480e0 2018-06-23 stsp first_displayed_line--;
1087 a70480e0 2018-06-23 stsp case KEY_DOWN:
1088 a70480e0 2018-06-23 stsp case KEY_ENTER:
1089 a70480e0 2018-06-23 stsp case '\r':
1091 a70480e0 2018-06-23 stsp first_displayed_line++;
1093 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1096 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1097 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1098 a70480e0 2018-06-23 stsp first_displayed_line++;
1099 a70480e0 2018-06-23 stsp if (line == NULL)
1108 a70480e0 2018-06-23 stsp fclose(f);
1109 a70480e0 2018-06-23 stsp return err;
1112 a70480e0 2018-06-23 stsp static const struct got_error *
1113 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1115 a70480e0 2018-06-23 stsp const struct got_error *error;
1116 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1117 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1118 a70480e0 2018-06-23 stsp char *path = NULL;
1119 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1120 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1123 a70480e0 2018-06-23 stsp #ifndef PROFILE
1124 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1125 a70480e0 2018-06-23 stsp err(1, "pledge");
1128 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1129 a70480e0 2018-06-23 stsp switch (ch) {
1131 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1135 a70480e0 2018-06-23 stsp /* NOTREACHED */
1139 a70480e0 2018-06-23 stsp argc -= optind;
1140 a70480e0 2018-06-23 stsp argv += optind;
1142 a70480e0 2018-06-23 stsp if (argc == 0) {
1143 a70480e0 2018-06-23 stsp usage_blame();
1144 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1145 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1146 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1147 a70480e0 2018-06-23 stsp return got_error_from_errno();
1148 a70480e0 2018-06-23 stsp path = argv[0];
1149 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1150 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1151 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1152 a70480e0 2018-06-23 stsp return got_error_from_errno();
1153 a70480e0 2018-06-23 stsp path = argv[1];
1155 a70480e0 2018-06-23 stsp usage_blame();
1157 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1158 a70480e0 2018-06-23 stsp free(repo_path);
1159 a70480e0 2018-06-23 stsp if (error != NULL)
1160 66b4983c 2018-06-23 stsp return error;
1162 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1163 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1164 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1165 a70480e0 2018-06-23 stsp if (error != NULL)
1166 66b4983c 2018-06-23 stsp goto done;
1167 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1168 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1170 a70480e0 2018-06-23 stsp struct got_object *obj;
1171 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1172 a70480e0 2018-06-23 stsp if (error != NULL)
1173 66b4983c 2018-06-23 stsp goto done;
1174 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1175 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1176 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1177 a19e88aa 2018-06-23 stsp got_object_close(obj);
1179 a19e88aa 2018-06-23 stsp if (error != NULL)
1180 a19e88aa 2018-06-23 stsp goto done;
1182 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1184 a70480e0 2018-06-23 stsp free(commit_id);
1186 a70480e0 2018-06-23 stsp got_repo_close(repo);
1187 a70480e0 2018-06-23 stsp return error;
1190 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1191 ffd1d5e5 2018-06-23 stsp draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1192 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
1193 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
1194 ffd1d5e5 2018-06-23 stsp WINDOW *window, const char *label, const struct got_tree_entries *entries,
1195 ffd1d5e5 2018-06-23 stsp int selected, int limit, int isroot)
1197 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1198 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
1199 ffd1d5e5 2018-06-23 stsp char *line;
1200 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
1201 ffd1d5e5 2018-06-23 stsp int width, n;
1203 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
1205 ffd1d5e5 2018-06-23 stsp werase(window);
1207 ffd1d5e5 2018-06-23 stsp if (limit == 0)
1208 ffd1d5e5 2018-06-23 stsp return NULL;
1210 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, label, COLS);
1212 ffd1d5e5 2018-06-23 stsp return err;
1213 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1214 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1215 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1216 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1217 ffd1d5e5 2018-06-23 stsp return NULL;
1218 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1219 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1220 ffd1d5e5 2018-06-23 stsp return NULL;
1222 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1223 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
1224 ffd1d5e5 2018-06-23 stsp assert(!isroot);
1225 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
1226 ffd1d5e5 2018-06-23 stsp wstandout(window);
1227 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
1229 ffd1d5e5 2018-06-23 stsp waddstr(window, " ..\n"); /* parent directory */
1230 ffd1d5e5 2018-06-23 stsp if (selected == 0)
1231 ffd1d5e5 2018-06-23 stsp wstandend(window);
1232 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1233 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1234 ffd1d5e5 2018-06-23 stsp return NULL;
1238 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
1239 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1242 ffd1d5e5 2018-06-23 stsp while (te) {
1243 ffd1d5e5 2018-06-23 stsp if (asprintf(&line, " %s%s",
1244 ffd1d5e5 2018-06-23 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1245 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1246 ffd1d5e5 2018-06-23 stsp err = format_line(&wline, &width, line, COLS);
1247 ffd1d5e5 2018-06-23 stsp if (err) {
1248 ffd1d5e5 2018-06-23 stsp free(line);
1251 ffd1d5e5 2018-06-23 stsp if (n == selected) {
1252 ffd1d5e5 2018-06-23 stsp wstandout(window);
1253 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
1255 ffd1d5e5 2018-06-23 stsp waddwstr(window, wline);
1256 ffd1d5e5 2018-06-23 stsp if (width < COLS)
1257 ffd1d5e5 2018-06-23 stsp waddch(window, '\n');
1258 ffd1d5e5 2018-06-23 stsp if (n == selected)
1259 ffd1d5e5 2018-06-23 stsp wstandend(window);
1260 ffd1d5e5 2018-06-23 stsp free(line);
1262 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
1263 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
1264 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
1266 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1269 ffd1d5e5 2018-06-23 stsp return err;
1272 ffd1d5e5 2018-06-23 stsp static void
1273 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1274 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
1276 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
1279 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
1282 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1283 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
1284 ffd1d5e5 2018-06-23 stsp if (!isroot)
1285 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1289 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
1290 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
1291 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
1292 ffd1d5e5 2018-06-23 stsp prev = te;
1293 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
1295 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
1296 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
1298 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1299 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
1302 ffd1d5e5 2018-06-23 stsp static void
1303 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1304 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
1305 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
1307 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
1308 ffd1d5e5 2018-06-23 stsp int n = 0;
1310 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1313 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
1314 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1316 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
1317 ffd1d5e5 2018-06-23 stsp while (next) {
1318 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
1319 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
1321 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
1325 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
1326 ffd1d5e5 2018-06-23 stsp SLIST_ENTRY(tog_parent_tree) entry;
1327 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
1328 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
1329 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
1330 ffd1d5e5 2018-06-23 stsp int selected;
1333 ffd1d5e5 2018-06-23 stsp SLIST_HEAD(tog_parent_trees, tog_parent_tree);
1335 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1336 ffd1d5e5 2018-06-23 stsp blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1337 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
1339 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
1340 ffd1d5e5 2018-06-23 stsp char *path;
1341 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
1343 ffd1d5e5 2018-06-23 stsp SLIST_FOREACH(pt, parents, entry)
1344 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
1345 ffd1d5e5 2018-06-23 stsp len += strlen(te->name);
1346 ffd1d5e5 2018-06-23 stsp path = calloc(1, len);
1347 ffd1d5e5 2018-06-23 stsp if (path == NULL)
1348 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1350 ffd1d5e5 2018-06-23 stsp path[0] = '/';
1351 ffd1d5e5 2018-06-23 stsp SLIST_FOREACH(pt, parents, entry) {
1352 ffd1d5e5 2018-06-23 stsp if (strlcat(path, pt->selected_entry->name, len) >= len)
1353 ffd1d5e5 2018-06-23 stsp return got_error(GOT_ERR_NO_SPACE);
1354 ffd1d5e5 2018-06-23 stsp if (strlcat(path, "/", len) >= len)
1355 ffd1d5e5 2018-06-23 stsp return got_error(GOT_ERR_NO_SPACE);
1357 ffd1d5e5 2018-06-23 stsp if (strlcat(path, te->name, len) >= len)
1358 ffd1d5e5 2018-06-23 stsp return got_error(GOT_ERR_NO_SPACE);
1360 ffd1d5e5 2018-06-23 stsp return show_blame_view(path, commit_id, repo);
1363 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1364 ffd1d5e5 2018-06-23 stsp show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1365 ffd1d5e5 2018-06-23 stsp struct got_repository *repo)
1367 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
1368 ffd1d5e5 2018-06-23 stsp int ch, done = 0, selected = 0;
1369 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
1370 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
1371 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
1372 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
1373 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
1374 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
1375 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
1376 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
1378 ffd1d5e5 2018-06-23 stsp SLIST_INIT(&parents);
1380 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
1381 ffd1d5e5 2018-06-23 stsp if (err != NULL)
1382 ffd1d5e5 2018-06-23 stsp goto done;
1384 ffd1d5e5 2018-06-23 stsp if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1385 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1386 ffd1d5e5 2018-06-23 stsp goto done;
1389 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL) {
1390 ffd1d5e5 2018-06-23 stsp tog_tree_view.window = newwin(0, 0, 0, 0);
1391 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.window == NULL)
1392 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1393 ffd1d5e5 2018-06-23 stsp keypad(tog_tree_view.window, TRUE);
1395 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL) {
1396 ffd1d5e5 2018-06-23 stsp tog_tree_view.panel = new_panel(tog_tree_view.window);
1397 ffd1d5e5 2018-06-23 stsp if (tog_tree_view.panel == NULL)
1398 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1400 ffd1d5e5 2018-06-23 stsp show_panel(tog_tree_view.panel);
1402 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
1403 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1404 ffd1d5e5 2018-06-23 stsp while (!done) {
1405 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
1406 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
1407 ffd1d5e5 2018-06-23 stsp if (tree != root)
1408 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
1410 ffd1d5e5 2018-06-23 stsp err = draw_tree_entries(&first_displayed_entry,
1411 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
1412 ffd1d5e5 2018-06-23 stsp tog_tree_view.window, tree_label, entries, selected,
1413 ffd1d5e5 2018-06-23 stsp LINES, tree == root);
1417 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
1418 ffd1d5e5 2018-06-23 stsp ch = wgetch(tog_tree_view.window);
1419 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
1420 ffd1d5e5 2018-06-23 stsp switch (ch) {
1425 ffd1d5e5 2018-06-23 stsp case KEY_UP:
1426 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1427 ffd1d5e5 2018-06-23 stsp selected--;
1428 ffd1d5e5 2018-06-23 stsp if (selected > 0)
1430 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
1431 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1433 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
1434 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
1435 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
1436 ffd1d5e5 2018-06-23 stsp selected = 0;
1439 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, LINES,
1440 ffd1d5e5 2018-06-23 stsp entries, tree == root);
1443 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
1444 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
1445 ffd1d5e5 2018-06-23 stsp selected++;
1448 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
1449 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1451 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
1452 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, LINES,
1453 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
1454 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1456 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
1457 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
1458 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1460 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
1461 ffd1d5e5 2018-06-23 stsp case '\r':
1462 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
1463 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1464 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
1465 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
1466 ffd1d5e5 2018-06-23 stsp if (tree == root)
1468 ffd1d5e5 2018-06-23 stsp parent = SLIST_FIRST(&parents);
1469 ffd1d5e5 2018-06-23 stsp SLIST_REMOVE_HEAD(&parents, entry);
1470 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1471 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
1472 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
1473 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
1474 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
1475 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
1476 ffd1d5e5 2018-06-23 stsp free(parent);
1477 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
1478 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1479 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
1480 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
1481 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
1483 ffd1d5e5 2018-06-23 stsp goto done;
1484 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
1485 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
1486 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
1487 ffd1d5e5 2018-06-23 stsp goto done;
1489 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
1490 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
1491 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
1492 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
1493 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
1494 ffd1d5e5 2018-06-23 stsp SLIST_INSERT_HEAD(&parents, parent,
1496 ffd1d5e5 2018-06-23 stsp tree = child;
1497 ffd1d5e5 2018-06-23 stsp selected = 0;
1498 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
1499 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
1500 ffd1d5e5 2018-06-23 stsp err = blame_tree_entry(selected_entry,
1501 ffd1d5e5 2018-06-23 stsp &parents, commit_id, repo);
1503 ffd1d5e5 2018-06-23 stsp goto done;
1506 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
1507 ffd1d5e5 2018-06-23 stsp if (selected > LINES)
1508 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
1515 ffd1d5e5 2018-06-23 stsp free(tree_label);
1516 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
1517 ffd1d5e5 2018-06-23 stsp while (!SLIST_EMPTY(&parents)) {
1518 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
1519 ffd1d5e5 2018-06-23 stsp parent = SLIST_FIRST(&parents);
1520 ffd1d5e5 2018-06-23 stsp SLIST_REMOVE_HEAD(&parents, entry);
1521 ffd1d5e5 2018-06-23 stsp free(parent);
1524 ffd1d5e5 2018-06-23 stsp if (tree != root)
1525 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1526 ffd1d5e5 2018-06-23 stsp return err;
1529 ffd1d5e5 2018-06-23 stsp __dead static void
1530 ffd1d5e5 2018-06-23 stsp usage_tree(void)
1533 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1534 ffd1d5e5 2018-06-23 stsp getprogname());
1538 ffd1d5e5 2018-06-23 stsp static const struct got_error *
1539 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
1541 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
1542 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
1543 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
1544 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1545 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
1546 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
1547 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
1550 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
1551 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1552 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
1555 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1556 ffd1d5e5 2018-06-23 stsp switch (ch) {
1558 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
1562 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
1566 ffd1d5e5 2018-06-23 stsp argc -= optind;
1567 ffd1d5e5 2018-06-23 stsp argv += optind;
1569 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
1570 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1571 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1572 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1573 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
1574 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1575 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
1576 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
1578 ffd1d5e5 2018-06-23 stsp usage_log();
1580 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1581 ffd1d5e5 2018-06-23 stsp free(repo_path);
1582 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1583 ffd1d5e5 2018-06-23 stsp return error;
1585 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
1586 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
1587 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1588 ffd1d5e5 2018-06-23 stsp goto done;
1590 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
1591 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1592 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
1593 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
1594 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
1595 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
1598 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1599 ffd1d5e5 2018-06-23 stsp goto done;
1601 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
1602 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1603 ffd1d5e5 2018-06-23 stsp goto done;
1605 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1606 ffd1d5e5 2018-06-23 stsp if (error != NULL)
1607 ffd1d5e5 2018-06-23 stsp goto done;
1609 ffd1d5e5 2018-06-23 stsp error = show_tree_view(tree, commit_id, repo);
1611 ffd1d5e5 2018-06-23 stsp free(commit_id);
1612 ffd1d5e5 2018-06-23 stsp if (commit)
1613 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
1615 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
1617 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
1618 ffd1d5e5 2018-06-23 stsp return error;
1620 5c5136c5 2018-05-20 stsp static void
1621 9f7d7167 2018-04-29 stsp init_curses(void)
1623 9f7d7167 2018-04-29 stsp initscr();
1627 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1628 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1629 1f475ad8 2018-05-10 stsp curs_set(0);
1632 4ed7e80c 2018-05-20 stsp __dead static void
1633 9f7d7167 2018-04-29 stsp usage(void)
1637 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1638 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1639 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1640 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1641 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1646 c2301be8 2018-04-30 stsp static char **
1647 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1649 c2301be8 2018-04-30 stsp char **argv;
1650 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1652 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1653 c2301be8 2018-04-30 stsp if (argv == NULL)
1654 c2301be8 2018-04-30 stsp err(1, "calloc");
1655 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1656 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1657 c2301be8 2018-04-30 stsp err(1, "calloc");
1658 c2301be8 2018-04-30 stsp if (arg1) {
1659 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1660 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1661 c2301be8 2018-04-30 stsp err(1, "calloc");
1664 c2301be8 2018-04-30 stsp return argv;
1668 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1670 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1671 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1672 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1673 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1675 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1677 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1678 9f7d7167 2018-04-29 stsp switch (ch) {
1680 9f7d7167 2018-04-29 stsp hflag = 1;
1684 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1688 9f7d7167 2018-04-29 stsp argc -= optind;
1689 9f7d7167 2018-04-29 stsp argv += optind;
1690 9f7d7167 2018-04-29 stsp optind = 0;
1691 c2301be8 2018-04-30 stsp optreset = 1;
1693 c2301be8 2018-04-30 stsp if (argc == 0) {
1694 f29d3e89 2018-06-23 stsp if (hflag)
1696 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1697 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1698 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1703 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1704 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1705 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1706 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1707 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1708 9f7d7167 2018-04-29 stsp if (hflag)
1709 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1713 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1714 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1715 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1716 c2301be8 2018-04-30 stsp if (repo_path) {
1717 c2301be8 2018-04-30 stsp struct got_repository *repo;
1718 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1719 c2301be8 2018-04-30 stsp if (error == NULL)
1720 c2301be8 2018-04-30 stsp got_repo_close(repo);
1722 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1723 c2301be8 2018-04-30 stsp if (error) {
1724 f29d3e89 2018-06-23 stsp if (hflag) {
1725 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
1726 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
1730 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1731 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1732 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1733 ad7de8d9 2018-04-30 stsp free(repo_path);
1736 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1737 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1739 c2301be8 2018-04-30 stsp free(repo_path);
1743 5c5136c5 2018-05-20 stsp init_curses();
1745 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1746 9f7d7167 2018-04-29 stsp if (error)
1747 9f7d7167 2018-04-29 stsp goto done;
1750 c2301be8 2018-04-30 stsp free(cmd_argv);
1751 9f7d7167 2018-04-29 stsp if (error)
1752 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);