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>
35 9f7d7167 2018-04-29 stsp #include "got_error.h"
36 80ddbec8 2018-04-29 stsp #include "got_object.h"
37 80ddbec8 2018-04-29 stsp #include "got_reference.h"
38 80ddbec8 2018-04-29 stsp #include "got_repository.h"
39 80ddbec8 2018-04-29 stsp #include "got_diff.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
42 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
45 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 9f7d7167 2018-04-29 stsp #ifndef nitems
49 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 9f7d7167 2018-04-29 stsp struct tog_cmd {
53 c2301be8 2018-04-30 stsp const char *name;
54 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
55 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
56 c2301be8 2018-04-30 stsp const char *descr;
59 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
60 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
61 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
62 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
64 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
65 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
68 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
69 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
70 9f7d7167 2018-04-29 stsp "show repository history" },
71 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
72 9f7d7167 2018-04-29 stsp "compare files and directories" },
73 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
74 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
77 fed328cc 2018-05-20 stsp static struct tog_view {
78 26ed57b2 2018-05-19 stsp WINDOW *window;
79 26ed57b2 2018-05-19 stsp PANEL *panel;
80 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
82 cd0acaa7 2018-05-20 stsp static const struct got_error *
83 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
84 cd0acaa7 2018-05-20 stsp struct got_repository *);
85 cd0acaa7 2018-05-20 stsp static const struct got_error *
86 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
88 4ed7e80c 2018-05-20 stsp __dead static void
89 9f7d7167 2018-04-29 stsp usage_log(void)
92 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
93 9f7d7167 2018-04-29 stsp getprogname());
97 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
98 80ddbec8 2018-04-29 stsp static const struct got_error *
99 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
101 00dfcb92 2018-06-11 stsp char *vis = NULL;
102 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
104 963b370f 2018-05-20 stsp *ws = NULL;
105 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
106 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
107 00dfcb92 2018-06-11 stsp int vislen;
108 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
109 00dfcb92 2018-06-11 stsp return got_error_from_errno();
111 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
112 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
114 00dfcb92 2018-06-11 stsp return err;
115 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
116 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1)
117 00dfcb92 2018-06-11 stsp return got_error_from_errno(); /* give up */
120 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
121 963b370f 2018-05-20 stsp if (*ws == NULL)
122 963b370f 2018-05-20 stsp return got_error_from_errno();
124 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
125 963b370f 2018-05-20 stsp err = got_error_from_errno();
130 963b370f 2018-05-20 stsp *ws = NULL;
133 963b370f 2018-05-20 stsp return err;
136 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
137 963b370f 2018-05-20 stsp static const struct got_error *
138 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
140 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
141 963b370f 2018-05-20 stsp int cols = 0;
142 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
143 963b370f 2018-05-20 stsp size_t wlen;
146 963b370f 2018-05-20 stsp *wlinep = NULL;
148 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
150 963b370f 2018-05-20 stsp return err;
153 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
154 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
155 963b370f 2018-05-20 stsp switch (width) {
160 963b370f 2018-05-20 stsp cols += width;
163 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
164 963b370f 2018-05-20 stsp cols += TABSIZE;
167 963b370f 2018-05-20 stsp err = got_error_from_errno();
170 963b370f 2018-05-20 stsp if (cols <= COLS) {
172 963b370f 2018-05-20 stsp if (widthp)
173 963b370f 2018-05-20 stsp *widthp = cols;
176 963b370f 2018-05-20 stsp wline[i] = L'\0';
179 963b370f 2018-05-20 stsp free(wline);
181 963b370f 2018-05-20 stsp *wlinep = wline;
182 963b370f 2018-05-20 stsp return err;
185 963b370f 2018-05-20 stsp static const struct got_error *
186 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
188 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
189 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
190 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
191 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
192 bb737323 2018-05-20 stsp int author_width, logmsg_width;
193 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
194 80ddbec8 2018-04-29 stsp char *line = NULL;
195 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
196 bb737323 2018-05-20 stsp size_t id_len;
197 bb737323 2018-05-20 stsp int col, limit;
198 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
199 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
200 9c2eaf34 2018-05-20 stsp const int avail = COLS;
202 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
204 80ddbec8 2018-04-29 stsp return err;
205 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
206 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
207 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
208 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
210 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
211 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
213 bb737323 2018-05-20 stsp col = limit + 1;
214 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
215 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
218 9c2eaf34 2018-05-20 stsp if (col > avail)
221 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
222 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
223 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
226 6d9fbc00 2018-04-29 stsp author = author0;
227 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
228 6d9fbc00 2018-04-29 stsp if (smallerthan)
229 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
231 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
233 6d9fbc00 2018-04-29 stsp *at = '\0';
235 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
236 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
239 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
240 bb737323 2018-05-20 stsp col += author_width;
241 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
242 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
244 bb737323 2018-05-20 stsp author_width++;
246 9c2eaf34 2018-05-20 stsp if (col > avail)
249 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
250 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
251 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
254 bb737323 2018-05-20 stsp logmsg = logmsg0;
255 bb737323 2018-05-20 stsp while (*logmsg == '\n')
257 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
258 bb737323 2018-05-20 stsp if (newline)
259 bb737323 2018-05-20 stsp *newline = '\0';
260 bb737323 2018-05-20 stsp limit = avail - col;
261 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
264 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
265 bb737323 2018-05-20 stsp col += logmsg_width;
266 bb737323 2018-05-20 stsp while (col <= avail) {
267 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
271 80ddbec8 2018-04-29 stsp free(logmsg0);
272 bb737323 2018-05-20 stsp free(wlogmsg);
273 6d9fbc00 2018-04-29 stsp free(author0);
274 bb737323 2018-05-20 stsp free(wauthor);
275 80ddbec8 2018-04-29 stsp free(line);
276 6d9fbc00 2018-04-29 stsp free(id_str);
277 80ddbec8 2018-04-29 stsp return err;
280 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
281 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
282 80ddbec8 2018-04-29 stsp struct got_object_id *id;
283 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
285 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
287 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
288 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
289 899d86c2 2018-05-10 stsp struct got_object_id *id)
291 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
293 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
294 80ddbec8 2018-04-29 stsp if (entry == NULL)
295 899d86c2 2018-05-10 stsp return NULL;
297 899d86c2 2018-05-10 stsp entry->id = id;
298 99db9666 2018-05-07 stsp entry->commit = commit;
299 899d86c2 2018-05-10 stsp return entry;
302 99db9666 2018-05-07 stsp static void
303 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
305 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
307 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
308 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
309 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
310 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
311 99db9666 2018-05-07 stsp free(entry);
314 99db9666 2018-05-07 stsp static void
315 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
317 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
318 99db9666 2018-05-07 stsp pop_commit(commits);
321 c4972b91 2018-05-07 stsp static const struct got_error *
322 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
323 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
325 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
326 899d86c2 2018-05-10 stsp struct got_object_id *id;
327 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
329 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
331 c4972b91 2018-05-07 stsp return err;
333 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
334 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
335 9ba79e04 2018-06-11 stsp int nfetched;
337 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
338 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
339 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
340 9ba79e04 2018-06-11 stsp return err;
342 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
344 9ba79e04 2018-06-11 stsp return err;
347 9ba79e04 2018-06-11 stsp while (1) {
348 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
350 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
352 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
353 9ba79e04 2018-06-11 stsp err = NULL;
357 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
361 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
362 9ba79e04 2018-06-11 stsp if (entry == NULL) {
363 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
367 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
370 9ba79e04 2018-06-11 stsp return err;
373 99db9666 2018-05-07 stsp static const struct got_error *
374 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
375 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
376 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
378 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
379 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
381 9ba79e04 2018-06-11 stsp *pentry = NULL;
383 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
384 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
385 9ba79e04 2018-06-11 stsp int nfetched;
386 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
387 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
389 9ba79e04 2018-06-11 stsp return err;
392 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
393 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
395 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
396 9ba79e04 2018-06-11 stsp err = NULL;
397 9ba79e04 2018-06-11 stsp return err;
400 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
401 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
402 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
403 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
405 9ba79e04 2018-06-11 stsp return NULL;
408 899d86c2 2018-05-10 stsp static const struct got_error *
409 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
411 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
412 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
414 9ba79e04 2018-06-11 stsp *head_id = NULL;
416 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
418 99db9666 2018-05-07 stsp return err;
420 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
421 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
423 9ba79e04 2018-06-11 stsp *head_id = NULL;
424 99db9666 2018-05-07 stsp return err;
427 9ba79e04 2018-06-11 stsp return NULL;
430 0553a4e3 2018-04-30 stsp static const struct got_error *
431 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
432 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
434 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
435 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
436 0553a4e3 2018-04-30 stsp int ncommits = 0;
438 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
440 899d86c2 2018-05-10 stsp entry = first;
441 899d86c2 2018-05-10 stsp *last = first;
442 899d86c2 2018-05-10 stsp while (entry) {
443 899d86c2 2018-05-10 stsp if (ncommits == limit)
445 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
446 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
447 cd0acaa7 2018-05-20 stsp *selected = entry;
449 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
450 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
451 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
454 0553a4e3 2018-04-30 stsp ncommits++;
455 899d86c2 2018-05-10 stsp *last = entry;
456 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
459 80ddbec8 2018-04-29 stsp update_panels();
460 80ddbec8 2018-04-29 stsp doupdate();
462 80ddbec8 2018-04-29 stsp return err;
465 07b55e75 2018-05-10 stsp static void
466 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
467 07b55e75 2018-05-10 stsp struct commit_queue *commits)
469 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
470 07b55e75 2018-05-10 stsp int nscrolled = 0;
472 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
473 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
476 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
477 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
478 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
479 07b55e75 2018-05-10 stsp if (entry) {
480 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
481 07b55e75 2018-05-10 stsp nscrolled++;
486 aa075928 2018-05-10 stsp static const struct got_error *
487 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
488 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
489 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
490 9ba79e04 2018-06-11 stsp struct got_repository *repo)
492 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
493 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
494 aa075928 2018-05-10 stsp int nscrolled = 0;
497 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
498 aa075928 2018-05-10 stsp if (pentry == NULL) {
499 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
500 9ba79e04 2018-06-11 stsp commits, graph, repo);
501 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
504 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
506 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
507 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
509 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
510 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
512 dd0a52c1 2018-05-20 stsp return err;
516 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
518 4a7f7875 2018-05-10 stsp int nparents = 0;
520 4a7f7875 2018-05-10 stsp while (entry) {
521 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
522 4a7f7875 2018-05-10 stsp nparents++;
525 4a7f7875 2018-05-10 stsp return nparents;
528 cd0acaa7 2018-05-20 stsp static const struct got_error *
529 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
531 cd0acaa7 2018-05-20 stsp const struct got_error *err;
532 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
533 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
535 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
537 cd0acaa7 2018-05-20 stsp return err;
539 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
540 9ba79e04 2018-06-11 stsp if (parent_id) {
541 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
546 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
549 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
551 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
552 cd0acaa7 2018-05-20 stsp return err;
555 80ddbec8 2018-04-29 stsp static const struct got_error *
556 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
558 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
559 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
560 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
561 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
562 0553a4e3 2018-04-30 stsp struct commit_queue commits;
563 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
564 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
565 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
566 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
568 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
569 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
570 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
571 80ddbec8 2018-04-29 stsp return got_error_from_errno();
572 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
574 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
575 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
576 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
577 80ddbec8 2018-04-29 stsp return got_error_from_errno();
579 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
581 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
583 9ba79e04 2018-06-11 stsp return err;
585 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
587 9ba79e04 2018-06-11 stsp err = got_commit_graph_open(&graph, head_id, repo);
591 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
592 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
596 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
601 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
602 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
603 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
604 9ba79e04 2018-06-11 stsp * updating the display.
606 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
607 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
610 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
611 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
612 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
613 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
617 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
618 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
622 899d86c2 2018-05-10 stsp while (!done) {
623 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
624 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
628 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
629 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
630 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
631 80ddbec8 2018-04-29 stsp switch (ch) {
633 31120ada 2018-04-30 stsp if (errno) {
634 31120ada 2018-04-30 stsp err = got_error_from_errno();
642 80ddbec8 2018-04-29 stsp case KEY_UP:
643 80ddbec8 2018-04-29 stsp if (selected > 0)
644 80ddbec8 2018-04-29 stsp selected--;
645 8ec9698d 2018-05-10 stsp if (selected > 0)
647 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
649 48531068 2018-05-10 stsp case KEY_PPAGE:
650 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
651 dfc1d240 2018-05-10 stsp first_displayed_entry) {
652 dfc1d240 2018-05-10 stsp selected = 0;
655 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
659 80ddbec8 2018-04-29 stsp case KEY_DOWN:
660 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
661 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
662 15c91275 2018-05-20 stsp selected < nparents - 1) {
663 15c91275 2018-05-20 stsp selected++;
666 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
667 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
672 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
673 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
674 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
678 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
680 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
681 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
682 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
683 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
684 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
686 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
687 31120ada 2018-04-30 stsp if (selected > LINES)
688 31120ada 2018-04-30 stsp selected = LINES - 1;
690 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
692 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
695 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
702 9ba79e04 2018-06-11 stsp free(head_id);
704 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
705 0553a4e3 2018-04-30 stsp free_commits(&commits);
706 80ddbec8 2018-04-29 stsp return err;
709 4ed7e80c 2018-05-20 stsp static const struct got_error *
710 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
712 80ddbec8 2018-04-29 stsp const struct got_error *error;
713 80ddbec8 2018-04-29 stsp struct got_repository *repo;
714 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
715 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
716 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
719 80ddbec8 2018-04-29 stsp #ifndef PROFILE
720 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
721 80ddbec8 2018-04-29 stsp err(1, "pledge");
724 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
725 80ddbec8 2018-04-29 stsp switch (ch) {
727 80ddbec8 2018-04-29 stsp start_commit = optarg;
731 80ddbec8 2018-04-29 stsp /* NOTREACHED */
735 80ddbec8 2018-04-29 stsp argc -= optind;
736 80ddbec8 2018-04-29 stsp argv += optind;
738 80ddbec8 2018-04-29 stsp if (argc == 0) {
739 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
740 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
741 80ddbec8 2018-04-29 stsp return got_error_from_errno();
742 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
743 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
744 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
745 80ddbec8 2018-04-29 stsp return got_error_from_errno();
747 80ddbec8 2018-04-29 stsp usage_log();
749 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
750 80ddbec8 2018-04-29 stsp free(repo_path);
751 80ddbec8 2018-04-29 stsp if (error != NULL)
752 80ddbec8 2018-04-29 stsp return error;
754 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
755 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
756 80ddbec8 2018-04-29 stsp if (error != NULL)
757 80ddbec8 2018-04-29 stsp return error;
759 80ddbec8 2018-04-29 stsp struct got_object *obj;
760 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
761 80ddbec8 2018-04-29 stsp if (error == NULL) {
762 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
763 899d86c2 2018-05-10 stsp if (start_id == NULL)
764 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
767 80ddbec8 2018-04-29 stsp if (error != NULL)
768 80ddbec8 2018-04-29 stsp return error;
769 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
770 899d86c2 2018-05-10 stsp free(start_id);
771 80ddbec8 2018-04-29 stsp got_repo_close(repo);
772 80ddbec8 2018-04-29 stsp return error;
775 4ed7e80c 2018-05-20 stsp __dead static void
776 9f7d7167 2018-04-29 stsp usage_diff(void)
779 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
780 9f7d7167 2018-04-29 stsp getprogname());
784 b304db33 2018-05-20 stsp static char *
785 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
787 b304db33 2018-05-20 stsp char *line;
788 b304db33 2018-05-20 stsp size_t linelen;
789 b304db33 2018-05-20 stsp size_t lineno;
790 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
792 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
794 b304db33 2018-05-20 stsp *len = linelen;
795 b304db33 2018-05-20 stsp return line;
798 4ed7e80c 2018-05-20 stsp static const struct got_error *
799 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
800 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
802 61e69b96 2018-05-20 stsp const struct got_error *err;
803 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
804 b304db33 2018-05-20 stsp char *line;
805 b304db33 2018-05-20 stsp size_t len;
806 61e69b96 2018-05-20 stsp wchar_t *wline;
810 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
813 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
814 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
815 26ed57b2 2018-05-19 stsp if (line == NULL) {
819 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
820 26ed57b2 2018-05-19 stsp free(line);
824 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
826 61e69b96 2018-05-20 stsp free(line);
827 61e69b96 2018-05-20 stsp return err;
829 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
830 e0b650dd 2018-05-20 stsp if (width < COLS)
831 e0b650dd 2018-05-20 stsp waddch(tog_diff_view.window, '\n');
832 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
833 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
834 26ed57b2 2018-05-19 stsp free(line);
836 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
838 26ed57b2 2018-05-19 stsp update_panels();
839 26ed57b2 2018-05-19 stsp doupdate();
841 26ed57b2 2018-05-19 stsp return NULL;
844 4ed7e80c 2018-05-20 stsp static const struct got_error *
845 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
846 26ed57b2 2018-05-19 stsp struct got_repository *repo)
848 26ed57b2 2018-05-19 stsp const struct got_error *err;
850 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
851 b304db33 2018-05-20 stsp int eof, i;
853 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
854 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
855 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
857 511a516b 2018-05-19 stsp f = got_opentemp();
858 26ed57b2 2018-05-19 stsp if (f == NULL)
859 26ed57b2 2018-05-19 stsp return got_error_from_errno();
861 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
862 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
863 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
865 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
866 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
868 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
869 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
872 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
877 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
878 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
879 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
880 26ed57b2 2018-05-19 stsp return got_error_from_errno();
881 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
883 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
884 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
885 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
886 26ed57b2 2018-05-19 stsp return got_error_from_errno();
888 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
890 26ed57b2 2018-05-19 stsp while (!done) {
891 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
892 26ed57b2 2018-05-19 stsp &eof, LINES);
895 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
896 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
897 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
898 26ed57b2 2018-05-19 stsp switch (ch) {
903 26ed57b2 2018-05-19 stsp case KEY_UP:
904 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
905 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
906 b304db33 2018-05-20 stsp first_displayed_line--;
908 b304db33 2018-05-20 stsp case KEY_PPAGE:
910 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
911 d56caa55 2018-05-20 stsp first_displayed_line > 1)
912 26ed57b2 2018-05-19 stsp first_displayed_line--;
915 26ed57b2 2018-05-19 stsp case KEY_DOWN:
916 925e6f23 2018-05-20 stsp case KEY_ENTER:
919 26ed57b2 2018-05-19 stsp first_displayed_line++;
921 b304db33 2018-05-20 stsp case KEY_NPAGE:
924 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
925 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
926 b304db33 2018-05-20 stsp first_displayed_line++;
927 b304db33 2018-05-20 stsp if (line == NULL)
936 26ed57b2 2018-05-19 stsp return err;
939 4ed7e80c 2018-05-20 stsp static const struct got_error *
940 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
942 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
943 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
944 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
945 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
946 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
949 26ed57b2 2018-05-19 stsp #ifndef PROFILE
950 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
951 26ed57b2 2018-05-19 stsp err(1, "pledge");
954 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
955 26ed57b2 2018-05-19 stsp switch (ch) {
958 26ed57b2 2018-05-19 stsp /* NOTREACHED */
962 26ed57b2 2018-05-19 stsp argc -= optind;
963 26ed57b2 2018-05-19 stsp argv += optind;
965 26ed57b2 2018-05-19 stsp if (argc == 0) {
966 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
967 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
968 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
969 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
970 26ed57b2 2018-05-19 stsp return got_error_from_errno();
971 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
972 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
973 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
974 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
975 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
976 26ed57b2 2018-05-19 stsp return got_error_from_errno();
977 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
978 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
980 26ed57b2 2018-05-19 stsp usage_diff();
982 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
983 26ed57b2 2018-05-19 stsp free(repo_path);
987 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
991 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
995 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
997 26ed57b2 2018-05-19 stsp got_repo_close(repo);
999 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1001 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1002 26ed57b2 2018-05-19 stsp return error;
1005 4ed7e80c 2018-05-20 stsp __dead static void
1006 9f7d7167 2018-04-29 stsp usage_blame(void)
1009 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1010 9f7d7167 2018-04-29 stsp getprogname());
1014 4ed7e80c 2018-05-20 stsp static const struct got_error *
1015 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1017 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1020 5c5136c5 2018-05-20 stsp static void
1021 9f7d7167 2018-04-29 stsp init_curses(void)
1023 9f7d7167 2018-04-29 stsp initscr();
1027 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1028 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1029 1f475ad8 2018-05-10 stsp curs_set(0);
1032 4ed7e80c 2018-05-20 stsp __dead static void
1033 9f7d7167 2018-04-29 stsp usage(void)
1037 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1038 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1039 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1040 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1041 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1046 c2301be8 2018-04-30 stsp static char **
1047 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1049 c2301be8 2018-04-30 stsp char **argv;
1050 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1052 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1053 c2301be8 2018-04-30 stsp if (argv == NULL)
1054 c2301be8 2018-04-30 stsp err(1, "calloc");
1055 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1056 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1057 c2301be8 2018-04-30 stsp err(1, "calloc");
1058 c2301be8 2018-04-30 stsp if (arg1) {
1059 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1060 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1061 c2301be8 2018-04-30 stsp err(1, "calloc");
1064 c2301be8 2018-04-30 stsp return argv;
1068 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1070 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1071 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1072 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1073 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1075 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1077 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1078 9f7d7167 2018-04-29 stsp switch (ch) {
1080 9f7d7167 2018-04-29 stsp hflag = 1;
1084 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1088 9f7d7167 2018-04-29 stsp argc -= optind;
1089 9f7d7167 2018-04-29 stsp argv += optind;
1090 9f7d7167 2018-04-29 stsp optind = 0;
1091 c2301be8 2018-04-30 stsp optreset = 1;
1093 c2301be8 2018-04-30 stsp if (argc == 0) {
1094 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1095 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1096 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1101 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1102 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1103 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1104 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1105 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1106 9f7d7167 2018-04-29 stsp if (hflag)
1107 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1111 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1112 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1113 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1114 c2301be8 2018-04-30 stsp if (repo_path) {
1115 c2301be8 2018-04-30 stsp struct got_repository *repo;
1116 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1117 c2301be8 2018-04-30 stsp if (error == NULL)
1118 c2301be8 2018-04-30 stsp got_repo_close(repo);
1120 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1121 c2301be8 2018-04-30 stsp if (error) {
1122 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1123 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1124 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1125 ad7de8d9 2018-04-30 stsp free(repo_path);
1128 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1129 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1131 c2301be8 2018-04-30 stsp free(repo_path);
1135 5c5136c5 2018-05-20 stsp init_curses();
1137 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1138 9f7d7167 2018-04-29 stsp if (error)
1139 9f7d7167 2018-04-29 stsp goto done;
1142 c2301be8 2018-04-30 stsp free(cmd_argv);
1143 9f7d7167 2018-04-29 stsp if (error)
1144 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);