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"
46 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 9f7d7167 2018-04-29 stsp #ifndef nitems
50 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 9f7d7167 2018-04-29 stsp struct tog_cmd {
54 c2301be8 2018-04-30 stsp const char *name;
55 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
56 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
57 c2301be8 2018-04-30 stsp const char *descr;
60 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
61 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
62 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
63 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
65 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
67 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
69 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
70 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
71 9f7d7167 2018-04-29 stsp "show repository history" },
72 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
73 9f7d7167 2018-04-29 stsp "compare files and directories" },
74 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
75 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
78 fed328cc 2018-05-20 stsp static struct tog_view {
79 26ed57b2 2018-05-19 stsp WINDOW *window;
80 26ed57b2 2018-05-19 stsp PANEL *panel;
81 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
83 cd0acaa7 2018-05-20 stsp static const struct got_error *
84 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
85 cd0acaa7 2018-05-20 stsp struct got_repository *);
86 cd0acaa7 2018-05-20 stsp static const struct got_error *
87 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
89 4ed7e80c 2018-05-20 stsp __dead static void
90 9f7d7167 2018-04-29 stsp usage_log(void)
93 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
94 9f7d7167 2018-04-29 stsp getprogname());
98 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
99 80ddbec8 2018-04-29 stsp static const struct got_error *
100 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
102 00dfcb92 2018-06-11 stsp char *vis = NULL;
103 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
105 963b370f 2018-05-20 stsp *ws = NULL;
106 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
107 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
108 00dfcb92 2018-06-11 stsp int vislen;
109 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
110 00dfcb92 2018-06-11 stsp return got_error_from_errno();
112 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
113 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
115 00dfcb92 2018-06-11 stsp return err;
116 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
117 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
118 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
123 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
124 a7f50699 2018-06-11 stsp if (*ws == NULL) {
125 a7f50699 2018-06-11 stsp err = got_error_from_errno();
129 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
130 963b370f 2018-05-20 stsp err = got_error_from_errno();
135 963b370f 2018-05-20 stsp *ws = NULL;
138 963b370f 2018-05-20 stsp return err;
141 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
142 963b370f 2018-05-20 stsp static const struct got_error *
143 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
145 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
146 963b370f 2018-05-20 stsp int cols = 0;
147 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
148 963b370f 2018-05-20 stsp size_t wlen;
151 963b370f 2018-05-20 stsp *wlinep = NULL;
153 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
155 963b370f 2018-05-20 stsp return err;
158 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
159 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
160 963b370f 2018-05-20 stsp switch (width) {
165 963b370f 2018-05-20 stsp cols += width;
168 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
169 963b370f 2018-05-20 stsp cols += TABSIZE;
172 963b370f 2018-05-20 stsp err = got_error_from_errno();
175 963b370f 2018-05-20 stsp if (cols <= COLS) {
177 963b370f 2018-05-20 stsp if (widthp)
178 963b370f 2018-05-20 stsp *widthp = cols;
181 963b370f 2018-05-20 stsp wline[i] = L'\0';
184 963b370f 2018-05-20 stsp free(wline);
186 963b370f 2018-05-20 stsp *wlinep = wline;
187 963b370f 2018-05-20 stsp return err;
190 963b370f 2018-05-20 stsp static const struct got_error *
191 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
193 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
194 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
195 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
196 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
197 bb737323 2018-05-20 stsp int author_width, logmsg_width;
198 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
199 80ddbec8 2018-04-29 stsp char *line = NULL;
200 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
201 bb737323 2018-05-20 stsp size_t id_len;
202 bb737323 2018-05-20 stsp int col, limit;
203 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
204 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
205 9c2eaf34 2018-05-20 stsp const int avail = COLS;
207 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
209 80ddbec8 2018-04-29 stsp return err;
210 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
211 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
212 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
213 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
215 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
216 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
218 bb737323 2018-05-20 stsp col = limit + 1;
219 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
220 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
223 9c2eaf34 2018-05-20 stsp if (col > avail)
226 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
227 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
228 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
231 6d9fbc00 2018-04-29 stsp author = author0;
232 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
233 6d9fbc00 2018-04-29 stsp if (smallerthan)
234 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
236 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
238 6d9fbc00 2018-04-29 stsp *at = '\0';
240 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
241 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
244 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
245 bb737323 2018-05-20 stsp col += author_width;
246 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
247 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
249 bb737323 2018-05-20 stsp author_width++;
251 9c2eaf34 2018-05-20 stsp if (col > avail)
254 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
255 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
256 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
259 bb737323 2018-05-20 stsp logmsg = logmsg0;
260 bb737323 2018-05-20 stsp while (*logmsg == '\n')
262 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
263 bb737323 2018-05-20 stsp if (newline)
264 bb737323 2018-05-20 stsp *newline = '\0';
265 bb737323 2018-05-20 stsp limit = avail - col;
266 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
269 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
270 bb737323 2018-05-20 stsp col += logmsg_width;
271 bb737323 2018-05-20 stsp while (col <= avail) {
272 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
276 80ddbec8 2018-04-29 stsp free(logmsg0);
277 bb737323 2018-05-20 stsp free(wlogmsg);
278 6d9fbc00 2018-04-29 stsp free(author0);
279 bb737323 2018-05-20 stsp free(wauthor);
280 80ddbec8 2018-04-29 stsp free(line);
281 6d9fbc00 2018-04-29 stsp free(id_str);
282 80ddbec8 2018-04-29 stsp return err;
285 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
286 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
287 80ddbec8 2018-04-29 stsp struct got_object_id *id;
288 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
290 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
292 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
293 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
294 899d86c2 2018-05-10 stsp struct got_object_id *id)
296 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
298 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
299 80ddbec8 2018-04-29 stsp if (entry == NULL)
300 899d86c2 2018-05-10 stsp return NULL;
302 899d86c2 2018-05-10 stsp entry->id = id;
303 99db9666 2018-05-07 stsp entry->commit = commit;
304 899d86c2 2018-05-10 stsp return entry;
307 99db9666 2018-05-07 stsp static void
308 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
310 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
312 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
313 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
314 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
315 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
316 99db9666 2018-05-07 stsp free(entry);
319 99db9666 2018-05-07 stsp static void
320 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
322 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
323 99db9666 2018-05-07 stsp pop_commit(commits);
326 c4972b91 2018-05-07 stsp static const struct got_error *
327 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
328 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
330 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
331 899d86c2 2018-05-10 stsp struct got_object_id *id;
332 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
334 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
336 c4972b91 2018-05-07 stsp return err;
338 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
339 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
340 9ba79e04 2018-06-11 stsp int nfetched;
342 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
343 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
344 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
345 9ba79e04 2018-06-11 stsp return err;
347 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
349 9ba79e04 2018-06-11 stsp return err;
352 9ba79e04 2018-06-11 stsp while (1) {
353 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
355 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
357 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
358 9ba79e04 2018-06-11 stsp err = NULL;
362 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
366 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
367 9ba79e04 2018-06-11 stsp if (entry == NULL) {
368 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
372 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
375 9ba79e04 2018-06-11 stsp return err;
378 99db9666 2018-05-07 stsp static const struct got_error *
379 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
380 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
381 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
383 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
384 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
386 9ba79e04 2018-06-11 stsp *pentry = NULL;
388 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
389 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
390 9ba79e04 2018-06-11 stsp int nfetched;
391 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
392 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
394 9ba79e04 2018-06-11 stsp return err;
397 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
398 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
400 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
401 9ba79e04 2018-06-11 stsp err = NULL;
402 9ba79e04 2018-06-11 stsp return err;
405 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
406 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
407 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
408 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
410 9ba79e04 2018-06-11 stsp return NULL;
413 899d86c2 2018-05-10 stsp static const struct got_error *
414 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
416 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
417 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
419 9ba79e04 2018-06-11 stsp *head_id = NULL;
421 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
423 99db9666 2018-05-07 stsp return err;
425 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
426 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
428 9ba79e04 2018-06-11 stsp *head_id = NULL;
429 99db9666 2018-05-07 stsp return err;
432 9ba79e04 2018-06-11 stsp return NULL;
435 0553a4e3 2018-04-30 stsp static const struct got_error *
436 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
437 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
439 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
440 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
441 0553a4e3 2018-04-30 stsp int ncommits = 0;
443 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
445 899d86c2 2018-05-10 stsp entry = first;
446 899d86c2 2018-05-10 stsp *last = first;
447 899d86c2 2018-05-10 stsp while (entry) {
448 899d86c2 2018-05-10 stsp if (ncommits == limit)
450 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
451 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
452 cd0acaa7 2018-05-20 stsp *selected = entry;
454 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
455 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
456 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
459 0553a4e3 2018-04-30 stsp ncommits++;
460 899d86c2 2018-05-10 stsp *last = entry;
461 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
464 80ddbec8 2018-04-29 stsp update_panels();
465 80ddbec8 2018-04-29 stsp doupdate();
467 80ddbec8 2018-04-29 stsp return err;
470 07b55e75 2018-05-10 stsp static void
471 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
472 07b55e75 2018-05-10 stsp struct commit_queue *commits)
474 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
475 07b55e75 2018-05-10 stsp int nscrolled = 0;
477 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
478 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
481 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
482 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
483 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
484 07b55e75 2018-05-10 stsp if (entry) {
485 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
486 07b55e75 2018-05-10 stsp nscrolled++;
491 aa075928 2018-05-10 stsp static const struct got_error *
492 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
493 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
494 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
495 9ba79e04 2018-06-11 stsp struct got_repository *repo)
497 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
498 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
499 aa075928 2018-05-10 stsp int nscrolled = 0;
502 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
503 aa075928 2018-05-10 stsp if (pentry == NULL) {
504 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
505 9ba79e04 2018-06-11 stsp commits, graph, repo);
506 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
509 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
511 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
512 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
514 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
515 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
517 dd0a52c1 2018-05-20 stsp return err;
521 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
523 4a7f7875 2018-05-10 stsp int nparents = 0;
525 4a7f7875 2018-05-10 stsp while (entry) {
526 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
527 4a7f7875 2018-05-10 stsp nparents++;
530 4a7f7875 2018-05-10 stsp return nparents;
533 cd0acaa7 2018-05-20 stsp static const struct got_error *
534 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
536 cd0acaa7 2018-05-20 stsp const struct got_error *err;
537 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
538 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
540 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
542 cd0acaa7 2018-05-20 stsp return err;
544 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
545 9ba79e04 2018-06-11 stsp if (parent_id) {
546 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
551 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
554 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
556 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
557 cd0acaa7 2018-05-20 stsp return err;
560 80ddbec8 2018-04-29 stsp static const struct got_error *
561 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
563 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
564 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
565 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
566 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
567 0553a4e3 2018-04-30 stsp struct commit_queue commits;
568 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
569 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
570 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
571 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
573 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
574 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
575 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
576 80ddbec8 2018-04-29 stsp return got_error_from_errno();
577 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
579 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
580 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
581 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
582 80ddbec8 2018-04-29 stsp return got_error_from_errno();
584 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
586 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
588 9ba79e04 2018-06-11 stsp return err;
590 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
592 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
596 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
597 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
601 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
606 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
607 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
608 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
609 9ba79e04 2018-06-11 stsp * updating the display.
611 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
612 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
615 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
616 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
617 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
618 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
622 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
623 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
627 899d86c2 2018-05-10 stsp while (!done) {
628 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
629 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
633 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
634 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
635 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
636 80ddbec8 2018-04-29 stsp switch (ch) {
638 31120ada 2018-04-30 stsp if (errno) {
639 31120ada 2018-04-30 stsp err = got_error_from_errno();
647 80ddbec8 2018-04-29 stsp case KEY_UP:
648 80ddbec8 2018-04-29 stsp if (selected > 0)
649 80ddbec8 2018-04-29 stsp selected--;
650 8ec9698d 2018-05-10 stsp if (selected > 0)
652 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
654 48531068 2018-05-10 stsp case KEY_PPAGE:
655 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
656 dfc1d240 2018-05-10 stsp first_displayed_entry) {
657 dfc1d240 2018-05-10 stsp selected = 0;
660 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
664 80ddbec8 2018-04-29 stsp case KEY_DOWN:
665 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
666 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
667 15c91275 2018-05-20 stsp selected < nparents - 1) {
668 15c91275 2018-05-20 stsp selected++;
671 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
672 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
677 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
678 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
679 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
683 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
685 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
686 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
687 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
688 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
689 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
691 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
692 31120ada 2018-04-30 stsp if (selected > LINES)
693 31120ada 2018-04-30 stsp selected = LINES - 1;
695 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
697 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
700 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
707 9ba79e04 2018-06-11 stsp free(head_id);
709 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
710 0553a4e3 2018-04-30 stsp free_commits(&commits);
711 80ddbec8 2018-04-29 stsp return err;
714 4ed7e80c 2018-05-20 stsp static const struct got_error *
715 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
717 80ddbec8 2018-04-29 stsp const struct got_error *error;
718 80ddbec8 2018-04-29 stsp struct got_repository *repo;
719 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
720 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
721 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
724 80ddbec8 2018-04-29 stsp #ifndef PROFILE
725 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
726 80ddbec8 2018-04-29 stsp err(1, "pledge");
729 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
730 80ddbec8 2018-04-29 stsp switch (ch) {
732 80ddbec8 2018-04-29 stsp start_commit = optarg;
736 80ddbec8 2018-04-29 stsp /* NOTREACHED */
740 80ddbec8 2018-04-29 stsp argc -= optind;
741 80ddbec8 2018-04-29 stsp argv += optind;
743 80ddbec8 2018-04-29 stsp if (argc == 0) {
744 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
745 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
746 80ddbec8 2018-04-29 stsp return got_error_from_errno();
747 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
748 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
749 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
750 80ddbec8 2018-04-29 stsp return got_error_from_errno();
752 80ddbec8 2018-04-29 stsp usage_log();
754 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
755 80ddbec8 2018-04-29 stsp free(repo_path);
756 80ddbec8 2018-04-29 stsp if (error != NULL)
757 80ddbec8 2018-04-29 stsp return error;
759 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
760 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
761 80ddbec8 2018-04-29 stsp if (error != NULL)
762 80ddbec8 2018-04-29 stsp return error;
764 80ddbec8 2018-04-29 stsp struct got_object *obj;
765 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
766 80ddbec8 2018-04-29 stsp if (error == NULL) {
767 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
768 899d86c2 2018-05-10 stsp if (start_id == NULL)
769 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
772 80ddbec8 2018-04-29 stsp if (error != NULL)
773 80ddbec8 2018-04-29 stsp return error;
774 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
775 899d86c2 2018-05-10 stsp free(start_id);
776 80ddbec8 2018-04-29 stsp got_repo_close(repo);
777 80ddbec8 2018-04-29 stsp return error;
780 4ed7e80c 2018-05-20 stsp __dead static void
781 9f7d7167 2018-04-29 stsp usage_diff(void)
784 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
785 9f7d7167 2018-04-29 stsp getprogname());
789 b304db33 2018-05-20 stsp static char *
790 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
792 b304db33 2018-05-20 stsp char *line;
793 b304db33 2018-05-20 stsp size_t linelen;
794 b304db33 2018-05-20 stsp size_t lineno;
795 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
797 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
799 b304db33 2018-05-20 stsp *len = linelen;
800 b304db33 2018-05-20 stsp return line;
803 4ed7e80c 2018-05-20 stsp static const struct got_error *
804 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
805 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
807 61e69b96 2018-05-20 stsp const struct got_error *err;
808 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
809 b304db33 2018-05-20 stsp char *line;
810 b304db33 2018-05-20 stsp size_t len;
811 61e69b96 2018-05-20 stsp wchar_t *wline;
815 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
818 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
819 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
820 26ed57b2 2018-05-19 stsp if (line == NULL) {
824 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
825 26ed57b2 2018-05-19 stsp free(line);
829 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
831 61e69b96 2018-05-20 stsp free(line);
832 61e69b96 2018-05-20 stsp return err;
834 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
835 e0b650dd 2018-05-20 stsp if (width < COLS)
836 e0b650dd 2018-05-20 stsp waddch(tog_diff_view.window, '\n');
837 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
838 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
839 26ed57b2 2018-05-19 stsp free(line);
841 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
843 26ed57b2 2018-05-19 stsp update_panels();
844 26ed57b2 2018-05-19 stsp doupdate();
846 26ed57b2 2018-05-19 stsp return NULL;
849 4ed7e80c 2018-05-20 stsp static const struct got_error *
850 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
851 26ed57b2 2018-05-19 stsp struct got_repository *repo)
853 26ed57b2 2018-05-19 stsp const struct got_error *err;
855 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
856 b304db33 2018-05-20 stsp int eof, i;
858 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
859 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
860 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
862 511a516b 2018-05-19 stsp f = got_opentemp();
863 26ed57b2 2018-05-19 stsp if (f == NULL)
864 26ed57b2 2018-05-19 stsp return got_error_from_errno();
866 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
867 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
868 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
870 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
871 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
873 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
874 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
877 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
882 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
883 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
884 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
885 26ed57b2 2018-05-19 stsp return got_error_from_errno();
886 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
888 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
889 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
890 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
891 26ed57b2 2018-05-19 stsp return got_error_from_errno();
893 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
895 26ed57b2 2018-05-19 stsp while (!done) {
896 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
897 26ed57b2 2018-05-19 stsp &eof, LINES);
900 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
901 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
902 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
903 26ed57b2 2018-05-19 stsp switch (ch) {
908 26ed57b2 2018-05-19 stsp case KEY_UP:
909 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
910 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
911 b304db33 2018-05-20 stsp first_displayed_line--;
913 b304db33 2018-05-20 stsp case KEY_PPAGE:
915 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
916 d56caa55 2018-05-20 stsp first_displayed_line > 1)
917 26ed57b2 2018-05-19 stsp first_displayed_line--;
920 26ed57b2 2018-05-19 stsp case KEY_DOWN:
921 925e6f23 2018-05-20 stsp case KEY_ENTER:
924 26ed57b2 2018-05-19 stsp first_displayed_line++;
926 b304db33 2018-05-20 stsp case KEY_NPAGE:
929 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
930 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
931 b304db33 2018-05-20 stsp first_displayed_line++;
932 b304db33 2018-05-20 stsp if (line == NULL)
941 26ed57b2 2018-05-19 stsp return err;
944 4ed7e80c 2018-05-20 stsp static const struct got_error *
945 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
947 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
948 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
949 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
950 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
951 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
954 26ed57b2 2018-05-19 stsp #ifndef PROFILE
955 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
956 26ed57b2 2018-05-19 stsp err(1, "pledge");
959 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
960 26ed57b2 2018-05-19 stsp switch (ch) {
963 26ed57b2 2018-05-19 stsp /* NOTREACHED */
967 26ed57b2 2018-05-19 stsp argc -= optind;
968 26ed57b2 2018-05-19 stsp argv += optind;
970 26ed57b2 2018-05-19 stsp if (argc == 0) {
971 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
972 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
973 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
974 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
975 26ed57b2 2018-05-19 stsp return got_error_from_errno();
976 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
977 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
978 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
979 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
980 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
981 26ed57b2 2018-05-19 stsp return got_error_from_errno();
982 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
983 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
985 26ed57b2 2018-05-19 stsp usage_diff();
987 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
988 26ed57b2 2018-05-19 stsp free(repo_path);
992 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
996 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1000 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1002 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1004 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1006 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1007 26ed57b2 2018-05-19 stsp return error;
1010 4ed7e80c 2018-05-20 stsp __dead static void
1011 9f7d7167 2018-04-29 stsp usage_blame(void)
1014 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1015 9f7d7167 2018-04-29 stsp getprogname());
1019 4ed7e80c 2018-05-20 stsp static const struct got_error *
1020 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1022 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1025 5c5136c5 2018-05-20 stsp static void
1026 9f7d7167 2018-04-29 stsp init_curses(void)
1028 9f7d7167 2018-04-29 stsp initscr();
1032 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1033 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1034 1f475ad8 2018-05-10 stsp curs_set(0);
1037 4ed7e80c 2018-05-20 stsp __dead static void
1038 9f7d7167 2018-04-29 stsp usage(void)
1042 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1043 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1044 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1045 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1046 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1051 c2301be8 2018-04-30 stsp static char **
1052 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1054 c2301be8 2018-04-30 stsp char **argv;
1055 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1057 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1058 c2301be8 2018-04-30 stsp if (argv == NULL)
1059 c2301be8 2018-04-30 stsp err(1, "calloc");
1060 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1061 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1062 c2301be8 2018-04-30 stsp err(1, "calloc");
1063 c2301be8 2018-04-30 stsp if (arg1) {
1064 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1065 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1066 c2301be8 2018-04-30 stsp err(1, "calloc");
1069 c2301be8 2018-04-30 stsp return argv;
1073 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1075 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1076 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1077 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1078 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1080 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1082 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1083 9f7d7167 2018-04-29 stsp switch (ch) {
1085 9f7d7167 2018-04-29 stsp hflag = 1;
1089 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1093 9f7d7167 2018-04-29 stsp argc -= optind;
1094 9f7d7167 2018-04-29 stsp argv += optind;
1095 9f7d7167 2018-04-29 stsp optind = 0;
1096 c2301be8 2018-04-30 stsp optreset = 1;
1098 c2301be8 2018-04-30 stsp if (argc == 0) {
1099 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1100 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1101 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1106 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1107 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1108 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1109 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1110 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1111 9f7d7167 2018-04-29 stsp if (hflag)
1112 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1116 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1117 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1118 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1119 c2301be8 2018-04-30 stsp if (repo_path) {
1120 c2301be8 2018-04-30 stsp struct got_repository *repo;
1121 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1122 c2301be8 2018-04-30 stsp if (error == NULL)
1123 c2301be8 2018-04-30 stsp got_repo_close(repo);
1125 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1126 c2301be8 2018-04-30 stsp if (error) {
1127 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1128 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1129 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1130 ad7de8d9 2018-04-30 stsp free(repo_path);
1133 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1134 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1136 c2301be8 2018-04-30 stsp free(repo_path);
1140 5c5136c5 2018-05-20 stsp init_curses();
1142 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1143 9f7d7167 2018-04-29 stsp if (error)
1144 9f7d7167 2018-04-29 stsp goto done;
1147 c2301be8 2018-04-30 stsp free(cmd_argv);
1148 9f7d7167 2018-04-29 stsp if (error)
1149 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);