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>
19 31120ada 2018-04-30 stsp #include <errno.h>
20 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
21 9f7d7167 2018-04-29 stsp #include <curses.h>
22 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <panel.h>
24 9f7d7167 2018-04-29 stsp #include <locale.h>
25 9f7d7167 2018-04-29 stsp #include <stdlib.h>
26 26ed57b2 2018-05-19 stsp #include <stdio.h>
27 9f7d7167 2018-04-29 stsp #include <getopt.h>
28 9f7d7167 2018-04-29 stsp #include <string.h>
29 9f7d7167 2018-04-29 stsp #include <err.h>
30 80ddbec8 2018-04-29 stsp #include <unistd.h>
31 26ed57b2 2018-05-19 stsp #include <util.h>
32 26ed57b2 2018-05-19 stsp #include <limits.h>
33 61e69b96 2018-05-20 stsp #include <wchar.h>
34 788c352e 2018-06-16 stsp #include <time.h>
36 9f7d7167 2018-04-29 stsp #include "got_error.h"
37 80ddbec8 2018-04-29 stsp #include "got_object.h"
38 80ddbec8 2018-04-29 stsp #include "got_reference.h"
39 80ddbec8 2018-04-29 stsp #include "got_repository.h"
40 80ddbec8 2018-04-29 stsp #include "got_diff.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
43 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
44 a70480e0 2018-06-23 stsp #include "got_blame.h"
47 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 9f7d7167 2018-04-29 stsp #ifndef nitems
51 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 9f7d7167 2018-04-29 stsp struct tog_cmd {
55 c2301be8 2018-04-30 stsp const char *name;
56 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
57 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
58 c2301be8 2018-04-30 stsp const char *descr;
61 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
62 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
63 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
67 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
68 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
71 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
72 9f7d7167 2018-04-29 stsp "show repository history" },
73 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
74 9f7d7167 2018-04-29 stsp "compare files and directories" },
75 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
76 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
79 fed328cc 2018-05-20 stsp static struct tog_view {
80 26ed57b2 2018-05-19 stsp WINDOW *window;
81 26ed57b2 2018-05-19 stsp PANEL *panel;
82 a70480e0 2018-06-23 stsp } tog_log_view, tog_diff_view, tog_blame_view;
84 cd0acaa7 2018-05-20 stsp static const struct got_error *
85 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
86 cd0acaa7 2018-05-20 stsp struct got_repository *);
87 cd0acaa7 2018-05-20 stsp static const struct got_error *
88 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
89 a70480e0 2018-06-23 stsp static const struct got_error *
90 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
92 4ed7e80c 2018-05-20 stsp __dead static void
93 9f7d7167 2018-04-29 stsp usage_log(void)
96 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
97 9f7d7167 2018-04-29 stsp getprogname());
101 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
102 80ddbec8 2018-04-29 stsp static const struct got_error *
103 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
105 00dfcb92 2018-06-11 stsp char *vis = NULL;
106 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
108 963b370f 2018-05-20 stsp *ws = NULL;
109 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
110 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
111 00dfcb92 2018-06-11 stsp int vislen;
112 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
113 00dfcb92 2018-06-11 stsp return got_error_from_errno();
115 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
116 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
118 00dfcb92 2018-06-11 stsp return err;
119 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
120 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
121 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
126 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
127 a7f50699 2018-06-11 stsp if (*ws == NULL) {
128 a7f50699 2018-06-11 stsp err = got_error_from_errno();
132 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
133 963b370f 2018-05-20 stsp err = got_error_from_errno();
138 963b370f 2018-05-20 stsp *ws = NULL;
141 963b370f 2018-05-20 stsp return err;
144 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
145 963b370f 2018-05-20 stsp static const struct got_error *
146 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
148 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
149 963b370f 2018-05-20 stsp int cols = 0;
150 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
151 963b370f 2018-05-20 stsp size_t wlen;
154 963b370f 2018-05-20 stsp *wlinep = NULL;
156 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
158 963b370f 2018-05-20 stsp return err;
161 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
162 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
163 963b370f 2018-05-20 stsp switch (width) {
168 963b370f 2018-05-20 stsp cols += width;
171 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
172 963b370f 2018-05-20 stsp cols += TABSIZE;
175 963b370f 2018-05-20 stsp err = got_error_from_errno();
178 963b370f 2018-05-20 stsp if (cols <= COLS) {
180 963b370f 2018-05-20 stsp if (widthp)
181 963b370f 2018-05-20 stsp *widthp = cols;
184 963b370f 2018-05-20 stsp wline[i] = L'\0';
187 963b370f 2018-05-20 stsp free(wline);
189 963b370f 2018-05-20 stsp *wlinep = wline;
190 963b370f 2018-05-20 stsp return err;
193 963b370f 2018-05-20 stsp static const struct got_error *
194 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
196 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
197 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
198 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
199 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
200 bb737323 2018-05-20 stsp int author_width, logmsg_width;
201 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
202 80ddbec8 2018-04-29 stsp char *line = NULL;
203 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
204 bb737323 2018-05-20 stsp size_t id_len;
205 bb737323 2018-05-20 stsp int col, limit;
206 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
207 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
208 9c2eaf34 2018-05-20 stsp const int avail = COLS;
210 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
212 80ddbec8 2018-04-29 stsp return err;
213 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
214 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
215 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
216 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
218 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
219 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
221 bb737323 2018-05-20 stsp col = limit + 1;
222 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
223 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
226 9c2eaf34 2018-05-20 stsp if (col > avail)
229 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
230 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
231 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
234 6d9fbc00 2018-04-29 stsp author = author0;
235 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
236 6d9fbc00 2018-04-29 stsp if (smallerthan)
237 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
239 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
241 6d9fbc00 2018-04-29 stsp *at = '\0';
243 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
244 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
247 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
248 bb737323 2018-05-20 stsp col += author_width;
249 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
250 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
252 bb737323 2018-05-20 stsp author_width++;
254 9c2eaf34 2018-05-20 stsp if (col > avail)
257 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
258 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
259 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
262 bb737323 2018-05-20 stsp logmsg = logmsg0;
263 bb737323 2018-05-20 stsp while (*logmsg == '\n')
265 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
266 bb737323 2018-05-20 stsp if (newline)
267 bb737323 2018-05-20 stsp *newline = '\0';
268 bb737323 2018-05-20 stsp limit = avail - col;
269 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
272 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
273 bb737323 2018-05-20 stsp col += logmsg_width;
274 bb737323 2018-05-20 stsp while (col <= avail) {
275 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
279 80ddbec8 2018-04-29 stsp free(logmsg0);
280 bb737323 2018-05-20 stsp free(wlogmsg);
281 6d9fbc00 2018-04-29 stsp free(author0);
282 bb737323 2018-05-20 stsp free(wauthor);
283 80ddbec8 2018-04-29 stsp free(line);
284 6d9fbc00 2018-04-29 stsp free(id_str);
285 80ddbec8 2018-04-29 stsp return err;
288 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
289 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
290 80ddbec8 2018-04-29 stsp struct got_object_id *id;
291 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
293 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
295 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
296 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
297 899d86c2 2018-05-10 stsp struct got_object_id *id)
299 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
301 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
302 80ddbec8 2018-04-29 stsp if (entry == NULL)
303 899d86c2 2018-05-10 stsp return NULL;
305 899d86c2 2018-05-10 stsp entry->id = id;
306 99db9666 2018-05-07 stsp entry->commit = commit;
307 899d86c2 2018-05-10 stsp return entry;
310 99db9666 2018-05-07 stsp static void
311 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
313 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
315 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
316 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
317 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
318 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
319 99db9666 2018-05-07 stsp free(entry);
322 99db9666 2018-05-07 stsp static void
323 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
325 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
326 99db9666 2018-05-07 stsp pop_commit(commits);
329 c4972b91 2018-05-07 stsp static const struct got_error *
330 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
331 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
333 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
334 899d86c2 2018-05-10 stsp struct got_object_id *id;
335 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
337 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
339 c4972b91 2018-05-07 stsp return err;
341 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
342 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
343 9ba79e04 2018-06-11 stsp int nfetched;
345 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
346 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
347 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
348 9ba79e04 2018-06-11 stsp return err;
350 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
352 9ba79e04 2018-06-11 stsp return err;
355 9ba79e04 2018-06-11 stsp while (1) {
356 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
358 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
360 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
361 9ba79e04 2018-06-11 stsp err = NULL;
365 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
369 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
370 9ba79e04 2018-06-11 stsp if (entry == NULL) {
371 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
375 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
378 9ba79e04 2018-06-11 stsp return err;
381 99db9666 2018-05-07 stsp static const struct got_error *
382 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
383 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
384 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
386 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
387 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
389 9ba79e04 2018-06-11 stsp *pentry = NULL;
391 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
392 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
393 9ba79e04 2018-06-11 stsp int nfetched;
394 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
395 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
397 9ba79e04 2018-06-11 stsp return err;
400 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
401 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
403 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
404 9ba79e04 2018-06-11 stsp err = NULL;
405 9ba79e04 2018-06-11 stsp return err;
408 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
409 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
410 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
411 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
413 9ba79e04 2018-06-11 stsp return NULL;
416 899d86c2 2018-05-10 stsp static const struct got_error *
417 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
419 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
420 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
422 9ba79e04 2018-06-11 stsp *head_id = NULL;
424 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
426 99db9666 2018-05-07 stsp return err;
428 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
429 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
431 9ba79e04 2018-06-11 stsp *head_id = NULL;
432 99db9666 2018-05-07 stsp return err;
435 9ba79e04 2018-06-11 stsp return NULL;
438 0553a4e3 2018-04-30 stsp static const struct got_error *
439 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
440 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
442 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
443 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
444 0553a4e3 2018-04-30 stsp int ncommits = 0;
446 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
448 899d86c2 2018-05-10 stsp entry = first;
449 899d86c2 2018-05-10 stsp *last = first;
450 899d86c2 2018-05-10 stsp while (entry) {
451 899d86c2 2018-05-10 stsp if (ncommits == limit)
453 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
454 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
455 cd0acaa7 2018-05-20 stsp *selected = entry;
457 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
458 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
459 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
462 0553a4e3 2018-04-30 stsp ncommits++;
463 899d86c2 2018-05-10 stsp *last = entry;
464 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
467 80ddbec8 2018-04-29 stsp update_panels();
468 80ddbec8 2018-04-29 stsp doupdate();
470 80ddbec8 2018-04-29 stsp return err;
473 07b55e75 2018-05-10 stsp static void
474 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
475 07b55e75 2018-05-10 stsp struct commit_queue *commits)
477 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
478 07b55e75 2018-05-10 stsp int nscrolled = 0;
480 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
481 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
484 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
485 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
486 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
487 07b55e75 2018-05-10 stsp if (entry) {
488 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
489 07b55e75 2018-05-10 stsp nscrolled++;
494 aa075928 2018-05-10 stsp static const struct got_error *
495 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
496 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
497 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
498 9ba79e04 2018-06-11 stsp struct got_repository *repo)
500 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
501 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
502 aa075928 2018-05-10 stsp int nscrolled = 0;
505 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
506 aa075928 2018-05-10 stsp if (pentry == NULL) {
507 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
508 9ba79e04 2018-06-11 stsp commits, graph, repo);
509 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
512 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
514 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
515 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
517 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
518 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
520 dd0a52c1 2018-05-20 stsp return err;
524 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
526 4a7f7875 2018-05-10 stsp int nparents = 0;
528 4a7f7875 2018-05-10 stsp while (entry) {
529 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
530 4a7f7875 2018-05-10 stsp nparents++;
533 4a7f7875 2018-05-10 stsp return nparents;
536 cd0acaa7 2018-05-20 stsp static const struct got_error *
537 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
539 cd0acaa7 2018-05-20 stsp const struct got_error *err;
540 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
541 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
543 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
545 cd0acaa7 2018-05-20 stsp return err;
547 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
548 9ba79e04 2018-06-11 stsp if (parent_id) {
549 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
554 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
557 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
559 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
560 cd0acaa7 2018-05-20 stsp return err;
563 80ddbec8 2018-04-29 stsp static const struct got_error *
564 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
566 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
567 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
568 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
569 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
570 0553a4e3 2018-04-30 stsp struct commit_queue commits;
571 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
572 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
573 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
574 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
576 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
577 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
578 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
579 80ddbec8 2018-04-29 stsp return got_error_from_errno();
580 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
582 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
583 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
584 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
585 80ddbec8 2018-04-29 stsp return got_error_from_errno();
587 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
589 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
591 9ba79e04 2018-06-11 stsp return err;
593 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
595 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
599 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
600 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
604 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
609 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
610 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
611 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
612 9ba79e04 2018-06-11 stsp * updating the display.
614 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
615 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
618 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
619 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
620 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
621 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
625 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
626 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
630 899d86c2 2018-05-10 stsp while (!done) {
631 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
632 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
636 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
637 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
638 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
639 80ddbec8 2018-04-29 stsp switch (ch) {
641 31120ada 2018-04-30 stsp if (errno) {
642 31120ada 2018-04-30 stsp err = got_error_from_errno();
650 80ddbec8 2018-04-29 stsp case KEY_UP:
651 80ddbec8 2018-04-29 stsp if (selected > 0)
652 80ddbec8 2018-04-29 stsp selected--;
653 8ec9698d 2018-05-10 stsp if (selected > 0)
655 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
657 48531068 2018-05-10 stsp case KEY_PPAGE:
658 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
659 dfc1d240 2018-05-10 stsp first_displayed_entry) {
660 dfc1d240 2018-05-10 stsp selected = 0;
663 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
667 80ddbec8 2018-04-29 stsp case KEY_DOWN:
668 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
669 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
670 15c91275 2018-05-20 stsp selected < nparents - 1) {
671 15c91275 2018-05-20 stsp selected++;
674 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
675 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
680 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
681 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
682 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
686 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
688 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
689 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
690 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
691 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
692 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
694 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
695 31120ada 2018-04-30 stsp if (selected > LINES)
696 31120ada 2018-04-30 stsp selected = LINES - 1;
698 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
700 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
703 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
710 9ba79e04 2018-06-11 stsp free(head_id);
712 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
713 0553a4e3 2018-04-30 stsp free_commits(&commits);
714 80ddbec8 2018-04-29 stsp return err;
717 4ed7e80c 2018-05-20 stsp static const struct got_error *
718 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
720 80ddbec8 2018-04-29 stsp const struct got_error *error;
721 80ddbec8 2018-04-29 stsp struct got_repository *repo;
722 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
723 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
724 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
727 80ddbec8 2018-04-29 stsp #ifndef PROFILE
728 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
729 80ddbec8 2018-04-29 stsp err(1, "pledge");
732 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
733 80ddbec8 2018-04-29 stsp switch (ch) {
735 80ddbec8 2018-04-29 stsp start_commit = optarg;
739 80ddbec8 2018-04-29 stsp /* NOTREACHED */
743 80ddbec8 2018-04-29 stsp argc -= optind;
744 80ddbec8 2018-04-29 stsp argv += optind;
746 80ddbec8 2018-04-29 stsp if (argc == 0) {
747 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
748 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
749 80ddbec8 2018-04-29 stsp return got_error_from_errno();
750 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
751 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
752 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
753 80ddbec8 2018-04-29 stsp return got_error_from_errno();
755 80ddbec8 2018-04-29 stsp usage_log();
757 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
758 80ddbec8 2018-04-29 stsp free(repo_path);
759 80ddbec8 2018-04-29 stsp if (error != NULL)
760 80ddbec8 2018-04-29 stsp return error;
762 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
763 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
764 80ddbec8 2018-04-29 stsp if (error != NULL)
765 80ddbec8 2018-04-29 stsp return error;
767 80ddbec8 2018-04-29 stsp struct got_object *obj;
768 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
769 80ddbec8 2018-04-29 stsp if (error == NULL) {
770 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
771 899d86c2 2018-05-10 stsp if (start_id == NULL)
772 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
775 80ddbec8 2018-04-29 stsp if (error != NULL)
776 80ddbec8 2018-04-29 stsp return error;
777 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
778 899d86c2 2018-05-10 stsp free(start_id);
779 80ddbec8 2018-04-29 stsp got_repo_close(repo);
780 80ddbec8 2018-04-29 stsp return error;
783 4ed7e80c 2018-05-20 stsp __dead static void
784 9f7d7167 2018-04-29 stsp usage_diff(void)
787 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
788 9f7d7167 2018-04-29 stsp getprogname());
792 b304db33 2018-05-20 stsp static char *
793 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
795 b304db33 2018-05-20 stsp char *line;
796 b304db33 2018-05-20 stsp size_t linelen;
797 b304db33 2018-05-20 stsp size_t lineno;
798 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
800 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
802 b304db33 2018-05-20 stsp *len = linelen;
803 b304db33 2018-05-20 stsp return line;
806 4ed7e80c 2018-05-20 stsp static const struct got_error *
807 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
808 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
810 61e69b96 2018-05-20 stsp const struct got_error *err;
811 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
812 b304db33 2018-05-20 stsp char *line;
813 b304db33 2018-05-20 stsp size_t len;
814 61e69b96 2018-05-20 stsp wchar_t *wline;
818 a70480e0 2018-06-23 stsp werase(window);
821 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
822 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
823 26ed57b2 2018-05-19 stsp if (line == NULL) {
827 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
828 26ed57b2 2018-05-19 stsp free(line);
832 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
834 61e69b96 2018-05-20 stsp free(line);
835 61e69b96 2018-05-20 stsp return err;
837 a70480e0 2018-06-23 stsp waddwstr(window, wline);
838 e0b650dd 2018-05-20 stsp if (width < COLS)
839 a70480e0 2018-06-23 stsp waddch(window, '\n');
840 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
841 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
842 26ed57b2 2018-05-19 stsp free(line);
844 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
846 26ed57b2 2018-05-19 stsp update_panels();
847 26ed57b2 2018-05-19 stsp doupdate();
849 26ed57b2 2018-05-19 stsp return NULL;
852 4ed7e80c 2018-05-20 stsp static const struct got_error *
853 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
854 26ed57b2 2018-05-19 stsp struct got_repository *repo)
856 26ed57b2 2018-05-19 stsp const struct got_error *err;
858 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
859 b304db33 2018-05-20 stsp int eof, i;
861 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
862 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
863 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
865 511a516b 2018-05-19 stsp f = got_opentemp();
866 26ed57b2 2018-05-19 stsp if (f == NULL)
867 26ed57b2 2018-05-19 stsp return got_error_from_errno();
869 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
870 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
871 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
873 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
874 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
876 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
877 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
880 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
885 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
886 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
887 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
888 26ed57b2 2018-05-19 stsp return got_error_from_errno();
889 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
891 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
892 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
893 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
894 26ed57b2 2018-05-19 stsp return got_error_from_errno();
896 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
898 26ed57b2 2018-05-19 stsp while (!done) {
899 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
900 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
903 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
904 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
905 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
906 26ed57b2 2018-05-19 stsp switch (ch) {
911 26ed57b2 2018-05-19 stsp case KEY_UP:
912 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
913 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
914 b304db33 2018-05-20 stsp first_displayed_line--;
916 b304db33 2018-05-20 stsp case KEY_PPAGE:
918 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
919 d56caa55 2018-05-20 stsp first_displayed_line > 1)
920 26ed57b2 2018-05-19 stsp first_displayed_line--;
923 26ed57b2 2018-05-19 stsp case KEY_DOWN:
924 925e6f23 2018-05-20 stsp case KEY_ENTER:
927 26ed57b2 2018-05-19 stsp first_displayed_line++;
929 b304db33 2018-05-20 stsp case KEY_NPAGE:
932 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
933 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
934 b304db33 2018-05-20 stsp first_displayed_line++;
935 b304db33 2018-05-20 stsp if (line == NULL)
944 26ed57b2 2018-05-19 stsp return err;
947 4ed7e80c 2018-05-20 stsp static const struct got_error *
948 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
950 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
951 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
952 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
953 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
954 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
957 26ed57b2 2018-05-19 stsp #ifndef PROFILE
958 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
959 26ed57b2 2018-05-19 stsp err(1, "pledge");
962 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
963 26ed57b2 2018-05-19 stsp switch (ch) {
966 26ed57b2 2018-05-19 stsp /* NOTREACHED */
970 26ed57b2 2018-05-19 stsp argc -= optind;
971 26ed57b2 2018-05-19 stsp argv += optind;
973 26ed57b2 2018-05-19 stsp if (argc == 0) {
974 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
975 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
976 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
977 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
978 26ed57b2 2018-05-19 stsp return got_error_from_errno();
979 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
980 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
981 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
982 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
983 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
984 26ed57b2 2018-05-19 stsp return got_error_from_errno();
985 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
986 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
988 26ed57b2 2018-05-19 stsp usage_diff();
990 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
991 26ed57b2 2018-05-19 stsp free(repo_path);
995 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
999 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1000 26ed57b2 2018-05-19 stsp if (error)
1001 26ed57b2 2018-05-19 stsp goto done;
1003 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1005 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1007 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1009 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1010 26ed57b2 2018-05-19 stsp return error;
1013 4ed7e80c 2018-05-20 stsp __dead static void
1014 9f7d7167 2018-04-29 stsp usage_blame(void)
1017 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1018 9f7d7167 2018-04-29 stsp getprogname());
1022 4ed7e80c 2018-05-20 stsp static const struct got_error *
1023 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1024 a70480e0 2018-06-23 stsp struct got_repository *repo)
1026 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1028 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1029 a70480e0 2018-06-23 stsp int eof, i;
1031 a70480e0 2018-06-23 stsp f = got_opentemp();
1032 a70480e0 2018-06-23 stsp if (f == NULL)
1033 a70480e0 2018-06-23 stsp return got_error_from_errno();
1035 a70480e0 2018-06-23 stsp err = got_blame(path, commit_id, repo, f);
1037 a70480e0 2018-06-23 stsp goto done;
1039 a70480e0 2018-06-23 stsp fflush(f);
1041 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1042 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1043 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1044 a70480e0 2018-06-23 stsp return got_error_from_errno();
1045 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1047 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1048 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1049 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1050 a70480e0 2018-06-23 stsp return got_error_from_errno();
1052 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1054 a70480e0 2018-06-23 stsp while (!done) {
1055 a70480e0 2018-06-23 stsp err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1056 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
1059 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1060 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1061 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1062 a70480e0 2018-06-23 stsp switch (ch) {
1067 a70480e0 2018-06-23 stsp case KEY_UP:
1068 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1069 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1070 a70480e0 2018-06-23 stsp first_displayed_line--;
1072 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1074 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1075 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1076 a70480e0 2018-06-23 stsp first_displayed_line--;
1079 a70480e0 2018-06-23 stsp case KEY_DOWN:
1080 a70480e0 2018-06-23 stsp case KEY_ENTER:
1081 a70480e0 2018-06-23 stsp case '\r':
1083 a70480e0 2018-06-23 stsp first_displayed_line++;
1085 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1088 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1089 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1090 a70480e0 2018-06-23 stsp first_displayed_line++;
1091 a70480e0 2018-06-23 stsp if (line == NULL)
1100 a70480e0 2018-06-23 stsp fclose(f);
1101 a70480e0 2018-06-23 stsp return err;
1104 a70480e0 2018-06-23 stsp static const struct got_error *
1105 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1107 a70480e0 2018-06-23 stsp const struct got_error *error;
1108 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1109 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1110 a70480e0 2018-06-23 stsp char *path = NULL;
1111 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1112 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1115 a70480e0 2018-06-23 stsp #ifndef PROFILE
1116 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1117 a70480e0 2018-06-23 stsp err(1, "pledge");
1120 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1121 a70480e0 2018-06-23 stsp switch (ch) {
1123 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1127 a70480e0 2018-06-23 stsp /* NOTREACHED */
1131 a70480e0 2018-06-23 stsp argc -= optind;
1132 a70480e0 2018-06-23 stsp argv += optind;
1134 a70480e0 2018-06-23 stsp if (argc == 0) {
1135 a70480e0 2018-06-23 stsp usage_blame();
1136 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1137 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1138 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1139 a70480e0 2018-06-23 stsp return got_error_from_errno();
1140 a70480e0 2018-06-23 stsp path = argv[0];
1141 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1142 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1143 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1144 a70480e0 2018-06-23 stsp return got_error_from_errno();
1145 a70480e0 2018-06-23 stsp path = argv[1];
1147 a70480e0 2018-06-23 stsp usage_blame();
1149 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1150 a70480e0 2018-06-23 stsp free(repo_path);
1151 a70480e0 2018-06-23 stsp if (error != NULL)
1152 66b4983c 2018-06-23 stsp return error;
1154 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1155 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1156 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1157 a70480e0 2018-06-23 stsp if (error != NULL)
1158 66b4983c 2018-06-23 stsp goto done;
1159 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1160 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1162 a70480e0 2018-06-23 stsp struct got_object *obj;
1163 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1164 a70480e0 2018-06-23 stsp if (error != NULL)
1165 66b4983c 2018-06-23 stsp goto done;
1166 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1167 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1168 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1169 a19e88aa 2018-06-23 stsp got_object_close(obj);
1171 a19e88aa 2018-06-23 stsp if (error != NULL)
1172 a19e88aa 2018-06-23 stsp goto done;
1174 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1176 a70480e0 2018-06-23 stsp free(commit_id);
1178 a70480e0 2018-06-23 stsp got_repo_close(repo);
1179 a70480e0 2018-06-23 stsp return error;
1182 5c5136c5 2018-05-20 stsp static void
1183 9f7d7167 2018-04-29 stsp init_curses(void)
1185 9f7d7167 2018-04-29 stsp initscr();
1189 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1190 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1191 1f475ad8 2018-05-10 stsp curs_set(0);
1194 4ed7e80c 2018-05-20 stsp __dead static void
1195 9f7d7167 2018-04-29 stsp usage(void)
1199 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1200 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1201 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1202 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1203 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1208 c2301be8 2018-04-30 stsp static char **
1209 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1211 c2301be8 2018-04-30 stsp char **argv;
1212 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1214 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1215 c2301be8 2018-04-30 stsp if (argv == NULL)
1216 c2301be8 2018-04-30 stsp err(1, "calloc");
1217 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1218 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1219 c2301be8 2018-04-30 stsp err(1, "calloc");
1220 c2301be8 2018-04-30 stsp if (arg1) {
1221 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1222 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1223 c2301be8 2018-04-30 stsp err(1, "calloc");
1226 c2301be8 2018-04-30 stsp return argv;
1230 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1232 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1233 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1234 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1235 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1237 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1239 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1240 9f7d7167 2018-04-29 stsp switch (ch) {
1242 9f7d7167 2018-04-29 stsp hflag = 1;
1246 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1250 9f7d7167 2018-04-29 stsp argc -= optind;
1251 9f7d7167 2018-04-29 stsp argv += optind;
1252 9f7d7167 2018-04-29 stsp optind = 0;
1253 c2301be8 2018-04-30 stsp optreset = 1;
1255 c2301be8 2018-04-30 stsp if (argc == 0) {
1256 f29d3e89 2018-06-23 stsp if (hflag)
1258 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1259 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1260 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1265 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1266 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1267 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1268 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1269 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1270 9f7d7167 2018-04-29 stsp if (hflag)
1271 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1275 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1276 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1277 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1278 c2301be8 2018-04-30 stsp if (repo_path) {
1279 c2301be8 2018-04-30 stsp struct got_repository *repo;
1280 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1281 c2301be8 2018-04-30 stsp if (error == NULL)
1282 c2301be8 2018-04-30 stsp got_repo_close(repo);
1284 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1285 c2301be8 2018-04-30 stsp if (error) {
1286 f29d3e89 2018-06-23 stsp if (hflag) {
1287 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
1288 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
1292 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1293 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1294 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1295 ad7de8d9 2018-04-30 stsp free(repo_path);
1298 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1299 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1301 c2301be8 2018-04-30 stsp free(repo_path);
1305 5c5136c5 2018-05-20 stsp init_curses();
1307 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1308 9f7d7167 2018-04-29 stsp if (error)
1309 9f7d7167 2018-04-29 stsp goto done;
1312 c2301be8 2018-04-30 stsp free(cmd_argv);
1313 9f7d7167 2018-04-29 stsp if (error)
1314 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);