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 9f7d7167 2018-04-29 stsp #include <curses.h>
21 9f7d7167 2018-04-29 stsp #include <panel.h>
22 9f7d7167 2018-04-29 stsp #include <locale.h>
23 9f7d7167 2018-04-29 stsp #include <stdlib.h>
24 26ed57b2 2018-05-19 stsp #include <stdio.h>
25 9f7d7167 2018-04-29 stsp #include <getopt.h>
26 9f7d7167 2018-04-29 stsp #include <string.h>
27 9f7d7167 2018-04-29 stsp #include <err.h>
28 80ddbec8 2018-04-29 stsp #include <unistd.h>
29 26ed57b2 2018-05-19 stsp #include <util.h>
30 26ed57b2 2018-05-19 stsp #include <limits.h>
32 9f7d7167 2018-04-29 stsp #include "got_error.h"
33 80ddbec8 2018-04-29 stsp #include "got_object.h"
34 80ddbec8 2018-04-29 stsp #include "got_reference.h"
35 80ddbec8 2018-04-29 stsp #include "got_repository.h"
36 80ddbec8 2018-04-29 stsp #include "got_diff.h"
37 511a516b 2018-05-19 stsp #include "got_opentemp.h"
40 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 9f7d7167 2018-04-29 stsp #ifndef nitems
44 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 9f7d7167 2018-04-29 stsp struct tog_cmd {
48 c2301be8 2018-04-30 stsp const char *name;
49 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
50 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
51 c2301be8 2018-04-30 stsp const char *descr;
54 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
55 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
56 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
57 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
59 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
60 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
61 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
63 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
64 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
65 9f7d7167 2018-04-29 stsp "show repository history" },
66 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
67 9f7d7167 2018-04-29 stsp "compare files and directories" },
68 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
69 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
72 fed328cc 2018-05-20 stsp static struct tog_view {
73 26ed57b2 2018-05-19 stsp WINDOW *window;
74 26ed57b2 2018-05-19 stsp PANEL *panel;
75 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
77 cd0acaa7 2018-05-20 stsp static const struct got_error *
78 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
79 cd0acaa7 2018-05-20 stsp struct got_repository *);
80 cd0acaa7 2018-05-20 stsp static const struct got_error *
81 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
83 4ed7e80c 2018-05-20 stsp __dead static void
84 9f7d7167 2018-04-29 stsp usage_log(void)
87 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
88 9f7d7167 2018-04-29 stsp getprogname());
92 80ddbec8 2018-04-29 stsp static const struct got_error *
93 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
95 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
96 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
97 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
98 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
99 80ddbec8 2018-04-29 stsp char *line = NULL;
100 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
101 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
102 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
103 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
104 881b2d3e 2018-04-30 stsp int i, col;
106 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
108 80ddbec8 2018-04-29 stsp return err;
109 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
111 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
112 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
113 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
116 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
117 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
119 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
120 6d9fbc00 2018-04-29 stsp if (newline)
121 80ddbec8 2018-04-29 stsp *newline = '\0';
122 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
124 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
125 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
126 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
129 6d9fbc00 2018-04-29 stsp author = author0;
130 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
131 6d9fbc00 2018-04-29 stsp if (smallerthan)
132 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
134 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
136 6d9fbc00 2018-04-29 stsp *at = '\0';
138 881b2d3e 2018-04-30 stsp author_len = strlen(author);
140 881b2d3e 2018-04-30 stsp avail = COLS - 1;
141 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
142 881b2d3e 2018-04-30 stsp if (line == NULL) {
143 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
148 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
149 881b2d3e 2018-04-30 stsp if (col >= avail)
151 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
153 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
154 881b2d3e 2018-04-30 stsp if (col >= avail)
156 881b2d3e 2018-04-30 stsp line[col++] = ' ';
159 881b2d3e 2018-04-30 stsp if (col >= avail)
161 881b2d3e 2018-04-30 stsp line[col++] = ' ';
162 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
163 881b2d3e 2018-04-30 stsp if (col >= avail)
165 881b2d3e 2018-04-30 stsp line[col++] = author[i];
167 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
168 881b2d3e 2018-04-30 stsp if (col >= avail)
170 881b2d3e 2018-04-30 stsp line[col++] = ' ';
173 881b2d3e 2018-04-30 stsp if (col >= avail)
175 881b2d3e 2018-04-30 stsp line[col++] = ' ';
177 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
178 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
179 881b2d3e 2018-04-30 stsp while (col < avail)
180 881b2d3e 2018-04-30 stsp line[col++] = ' ';
182 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
183 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
185 80ddbec8 2018-04-29 stsp free(logmsg0);
186 6d9fbc00 2018-04-29 stsp free(author0);
187 80ddbec8 2018-04-29 stsp free(line);
188 6d9fbc00 2018-04-29 stsp free(id_str);
189 80ddbec8 2018-04-29 stsp return err;
192 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
193 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
194 80ddbec8 2018-04-29 stsp struct got_object_id *id;
195 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
197 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
199 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
200 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
201 899d86c2 2018-05-10 stsp struct got_object_id *id)
203 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
205 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
206 80ddbec8 2018-04-29 stsp if (entry == NULL)
207 899d86c2 2018-05-10 stsp return NULL;
209 899d86c2 2018-05-10 stsp entry->id = id;
210 99db9666 2018-05-07 stsp entry->commit = commit;
211 899d86c2 2018-05-10 stsp return entry;
214 99db9666 2018-05-07 stsp static void
215 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
217 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
219 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
220 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
221 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
222 99db9666 2018-05-07 stsp free(entry->id);
223 99db9666 2018-05-07 stsp free(entry);
226 99db9666 2018-05-07 stsp static void
227 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
229 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
230 99db9666 2018-05-07 stsp pop_commit(commits);
233 c4972b91 2018-05-07 stsp static const struct got_error *
234 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
235 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
237 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
238 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
239 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
240 899d86c2 2018-05-10 stsp struct got_object_id *id;
241 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
243 899d86c2 2018-05-10 stsp *pentry = NULL;
245 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
246 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
247 c4972b91 2018-05-07 stsp if (pid == NULL)
248 c4972b91 2018-05-07 stsp return NULL;
249 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
251 c4972b91 2018-05-07 stsp return err;
252 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
253 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
254 c4972b91 2018-05-07 stsp got_object_close(obj);
255 c4972b91 2018-05-07 stsp return err;
258 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
259 c4972b91 2018-05-07 stsp got_object_close(obj);
261 c4972b91 2018-05-07 stsp return err;
263 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
264 899d86c2 2018-05-10 stsp if (id == NULL) {
265 c4972b91 2018-05-07 stsp err = got_error_from_errno();
266 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
267 899d86c2 2018-05-10 stsp return err;;
270 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
271 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
272 c4972b91 2018-05-07 stsp err = got_error_from_errno();
273 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
276 899d86c2 2018-05-10 stsp return err;;
279 899d86c2 2018-05-10 stsp static const struct got_error *
280 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
282 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
283 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
285 899d86c2 2018-05-10 stsp *head_id = NULL;
287 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
289 899d86c2 2018-05-10 stsp return err;
291 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
292 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
294 899d86c2 2018-05-10 stsp *head_id = NULL;
295 899d86c2 2018-05-10 stsp return err;
298 c4972b91 2018-05-07 stsp return NULL;
301 99db9666 2018-05-07 stsp static const struct got_error *
302 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
303 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
304 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
306 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
307 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
308 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
309 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
310 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
312 899d86c2 2018-05-10 stsp *ncommits = 0;
314 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
317 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
318 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
321 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
324 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
325 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
329 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
333 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
334 899d86c2 2018-05-10 stsp if (id == NULL) {
335 899d86c2 2018-05-10 stsp err = got_error_from_errno();
339 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
340 899d86c2 2018-05-10 stsp if (entry == NULL)
341 899d86c2 2018-05-10 stsp return got_error_from_errno();
343 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
344 899d86c2 2018-05-10 stsp if (old_head_entry)
345 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
347 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
349 899d86c2 2018-05-10 stsp *ncommits = 1;
352 899d86c2 2018-05-10 stsp * Fetch parent commits.
353 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
354 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
356 899d86c2 2018-05-10 stsp while (1) {
357 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
359 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
362 899d86c2 2018-05-10 stsp if (pentry == NULL)
366 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
367 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
369 899d86c2 2018-05-10 stsp if (old_head_entry &&
370 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
373 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
374 899d86c2 2018-05-10 stsp (*ncommits)++;
375 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
378 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
379 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
380 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
383 899d86c2 2018-05-10 stsp entry = pentry;
387 899d86c2 2018-05-10 stsp if (first_obj)
388 899d86c2 2018-05-10 stsp got_object_close(first_obj);
389 899d86c2 2018-05-10 stsp if (last_obj)
390 899d86c2 2018-05-10 stsp got_object_close(last_obj);
391 899d86c2 2018-05-10 stsp return err;
394 899d86c2 2018-05-10 stsp static const struct got_error *
395 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
396 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
397 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
399 99db9666 2018-05-07 stsp const struct got_error *err;
400 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
401 99db9666 2018-05-07 stsp int ncommits = 0;
402 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
404 899d86c2 2018-05-10 stsp *start_entry = NULL;
406 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
408 99db9666 2018-05-07 stsp return err;
410 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
411 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
414 99db9666 2018-05-07 stsp return err;
416 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
417 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
419 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
421 899d86c2 2018-05-10 stsp if (ncommits >= limit)
422 899d86c2 2018-05-10 stsp return NULL;
424 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
425 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
426 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
427 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
429 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
432 899d86c2 2018-05-10 stsp if (pentry)
433 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
434 899d86c2 2018-05-10 stsp entry = pentry;
435 0553a4e3 2018-04-30 stsp ncommits++;
439 899d86c2 2018-05-10 stsp *start_entry = NULL;
440 0553a4e3 2018-04-30 stsp return err;
443 0553a4e3 2018-04-30 stsp static const struct got_error *
444 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
445 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
447 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
448 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
449 0553a4e3 2018-04-30 stsp int ncommits = 0;
451 0553a4e3 2018-04-30 stsp wclear(tog_log_view.window);
453 899d86c2 2018-05-10 stsp entry = first;
454 899d86c2 2018-05-10 stsp *last = first;
455 899d86c2 2018-05-10 stsp while (entry) {
456 899d86c2 2018-05-10 stsp if (ncommits == limit)
458 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
459 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
460 cd0acaa7 2018-05-20 stsp *selected = entry;
462 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
463 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
464 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
467 0553a4e3 2018-04-30 stsp ncommits++;
468 899d86c2 2018-05-10 stsp *last = entry;
469 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
472 80ddbec8 2018-04-29 stsp update_panels();
473 80ddbec8 2018-04-29 stsp doupdate();
475 80ddbec8 2018-04-29 stsp return err;
478 07b55e75 2018-05-10 stsp static void
479 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
480 07b55e75 2018-05-10 stsp struct commit_queue *commits)
482 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
483 07b55e75 2018-05-10 stsp int nscrolled = 0;
485 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
486 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
489 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
490 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
491 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
492 07b55e75 2018-05-10 stsp if (entry) {
493 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
494 07b55e75 2018-05-10 stsp nscrolled++;
499 aa075928 2018-05-10 stsp static const struct got_error *
500 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
501 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
502 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
504 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
505 aa075928 2018-05-10 stsp struct commit_queue_entry *entry;
506 aa075928 2018-05-10 stsp int nscrolled = 0;
508 aa075928 2018-05-10 stsp if (last_displayed_entry->commit->nparents == 0)
509 aa075928 2018-05-10 stsp return NULL;
511 aa075928 2018-05-10 stsp entry = *first_displayed_entry;
513 aa075928 2018-05-10 stsp struct commit_queue_entry *pentry;
515 aa075928 2018-05-10 stsp pentry = TAILQ_NEXT(entry, entry);
516 aa075928 2018-05-10 stsp if (pentry == NULL) {
517 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
518 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
520 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
521 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
524 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
525 aa075928 2018-05-10 stsp entry = pentry;
527 aa075928 2018-05-10 stsp if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
528 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, last_displayed_entry,
532 aa075928 2018-05-10 stsp if (pentry) {
533 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
534 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
537 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
539 aa075928 2018-05-10 stsp return NULL;
543 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
545 4a7f7875 2018-05-10 stsp int nparents = 0;
547 4a7f7875 2018-05-10 stsp while (entry) {
548 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
549 4a7f7875 2018-05-10 stsp nparents++;
552 4a7f7875 2018-05-10 stsp return nparents;
555 cd0acaa7 2018-05-20 stsp static const struct got_error *
556 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
558 cd0acaa7 2018-05-20 stsp const struct got_error *err;
559 350efba7 2018-05-20 stsp struct commit_queue_entry *pentry;
560 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
562 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
564 cd0acaa7 2018-05-20 stsp return err;
566 350efba7 2018-05-20 stsp pentry = TAILQ_NEXT(entry, entry);
567 350efba7 2018-05-20 stsp if (pentry == NULL) {
568 350efba7 2018-05-20 stsp err = fetch_parent_commit(&pentry, entry, repo);
570 350efba7 2018-05-20 stsp return err;
572 350efba7 2018-05-20 stsp if (pentry) {
573 350efba7 2018-05-20 stsp err = got_object_open(&obj1, repo, pentry->id);
578 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
581 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
583 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
584 cd0acaa7 2018-05-20 stsp return err;
587 80ddbec8 2018-04-29 stsp static const struct got_error *
588 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
590 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
591 c4972b91 2018-05-07 stsp struct got_object_id *id;
592 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
593 0553a4e3 2018-04-30 stsp struct commit_queue commits;
594 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
595 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
596 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
598 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
599 c4972b91 2018-05-07 stsp if (id == NULL)
600 c4972b91 2018-05-07 stsp return got_error_from_errno();
602 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
603 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
604 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
605 80ddbec8 2018-04-29 stsp return got_error_from_errno();
606 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
608 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
609 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
610 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
611 80ddbec8 2018-04-29 stsp return got_error_from_errno();
613 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
615 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
616 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
619 899d86c2 2018-05-10 stsp while (!done) {
620 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
621 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
625 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
626 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
627 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
628 80ddbec8 2018-04-29 stsp switch (ch) {
630 31120ada 2018-04-30 stsp if (errno) {
631 31120ada 2018-04-30 stsp err = got_error_from_errno();
639 80ddbec8 2018-04-29 stsp case KEY_UP:
640 80ddbec8 2018-04-29 stsp if (selected > 0)
641 80ddbec8 2018-04-29 stsp selected--;
642 8ec9698d 2018-05-10 stsp if (selected > 0)
644 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
646 48531068 2018-05-10 stsp case KEY_PPAGE:
647 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
648 dfc1d240 2018-05-10 stsp first_displayed_entry) {
649 dfc1d240 2018-05-10 stsp selected = 0;
652 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
656 80ddbec8 2018-04-29 stsp case KEY_DOWN:
657 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
658 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
659 06abe2cd 2018-05-10 stsp selected < nparents - 1)
660 80ddbec8 2018-04-29 stsp selected++;
661 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
662 06abe2cd 2018-05-10 stsp selected < nparents - 1)
664 aa075928 2018-05-10 stsp err = scroll_down(&first_displayed_entry, 1,
665 aa075928 2018-05-10 stsp last_displayed_entry, &commits, repo);
669 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
670 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
671 dfc1d240 2018-05-10 stsp if (nparents < LINES - 1 &&
672 dfc1d240 2018-05-10 stsp selected < nparents - 1) {
673 dfc1d240 2018-05-10 stsp selected = nparents - 1;
676 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
677 e50ee4f1 2018-05-10 stsp last_displayed_entry, &commits, repo);
680 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
681 dfc1d240 2018-05-10 stsp if (selected > nparents)
682 dfc1d240 2018-05-10 stsp selected = nparents - 1;
684 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
685 31120ada 2018-04-30 stsp if (selected > LINES)
686 31120ada 2018-04-30 stsp selected = LINES - 1;
688 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
690 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
693 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
700 0553a4e3 2018-04-30 stsp free_commits(&commits);
701 80ddbec8 2018-04-29 stsp return err;
704 4ed7e80c 2018-05-20 stsp static const struct got_error *
705 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
707 80ddbec8 2018-04-29 stsp const struct got_error *error;
708 80ddbec8 2018-04-29 stsp struct got_repository *repo;
709 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
710 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
711 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
714 80ddbec8 2018-04-29 stsp #ifndef PROFILE
715 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
716 80ddbec8 2018-04-29 stsp err(1, "pledge");
719 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
720 80ddbec8 2018-04-29 stsp switch (ch) {
722 80ddbec8 2018-04-29 stsp start_commit = optarg;
726 80ddbec8 2018-04-29 stsp /* NOTREACHED */
730 80ddbec8 2018-04-29 stsp argc -= optind;
731 80ddbec8 2018-04-29 stsp argv += optind;
733 80ddbec8 2018-04-29 stsp if (argc == 0) {
734 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
735 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
736 80ddbec8 2018-04-29 stsp return got_error_from_errno();
737 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
738 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
739 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
740 80ddbec8 2018-04-29 stsp return got_error_from_errno();
742 80ddbec8 2018-04-29 stsp usage_log();
744 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
745 80ddbec8 2018-04-29 stsp free(repo_path);
746 80ddbec8 2018-04-29 stsp if (error != NULL)
747 80ddbec8 2018-04-29 stsp return error;
749 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
750 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
751 80ddbec8 2018-04-29 stsp if (error != NULL)
752 80ddbec8 2018-04-29 stsp return error;
754 80ddbec8 2018-04-29 stsp struct got_object *obj;
755 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
756 80ddbec8 2018-04-29 stsp if (error == NULL) {
757 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
758 899d86c2 2018-05-10 stsp if (start_id == NULL)
759 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
762 80ddbec8 2018-04-29 stsp if (error != NULL)
763 80ddbec8 2018-04-29 stsp return error;
764 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
765 899d86c2 2018-05-10 stsp free(start_id);
766 80ddbec8 2018-04-29 stsp got_repo_close(repo);
767 80ddbec8 2018-04-29 stsp return error;
770 4ed7e80c 2018-05-20 stsp __dead static void
771 9f7d7167 2018-04-29 stsp usage_diff(void)
774 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
775 9f7d7167 2018-04-29 stsp getprogname());
779 b304db33 2018-05-20 stsp static char *
780 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
782 b304db33 2018-05-20 stsp char *line;
783 b304db33 2018-05-20 stsp size_t linelen;
784 b304db33 2018-05-20 stsp size_t lineno;
785 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
787 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
789 b304db33 2018-05-20 stsp *len = linelen;
790 b304db33 2018-05-20 stsp return line;
793 4ed7e80c 2018-05-20 stsp static const struct got_error *
794 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
795 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
797 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
798 b304db33 2018-05-20 stsp char *line;
799 b304db33 2018-05-20 stsp size_t len;
802 26ed57b2 2018-05-19 stsp wclear(tog_diff_view.window);
805 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
806 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
807 26ed57b2 2018-05-19 stsp if (line == NULL) {
811 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
812 26ed57b2 2018-05-19 stsp free(line);
816 b304db33 2018-05-20 stsp if (len > COLS - 1)
817 26ed57b2 2018-05-19 stsp line[COLS - 1] = '\0';
818 26ed57b2 2018-05-19 stsp waddstr(tog_diff_view.window, line);
819 26ed57b2 2018-05-19 stsp waddch(tog_diff_view.window, '\n');
820 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
821 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
822 26ed57b2 2018-05-19 stsp free(line);
824 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
826 26ed57b2 2018-05-19 stsp update_panels();
827 26ed57b2 2018-05-19 stsp doupdate();
829 26ed57b2 2018-05-19 stsp return NULL;
832 4ed7e80c 2018-05-20 stsp static const struct got_error *
833 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
834 26ed57b2 2018-05-19 stsp struct got_repository *repo)
836 26ed57b2 2018-05-19 stsp const struct got_error *err;
838 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
839 b304db33 2018-05-20 stsp int eof, i;
841 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
842 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
843 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
845 511a516b 2018-05-19 stsp f = got_opentemp();
846 26ed57b2 2018-05-19 stsp if (f == NULL)
847 26ed57b2 2018-05-19 stsp return got_error_from_errno();
849 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
850 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
851 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
853 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
854 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
856 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
857 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
860 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
865 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
866 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
867 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
868 26ed57b2 2018-05-19 stsp return got_error_from_errno();
869 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
871 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
872 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
873 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
874 26ed57b2 2018-05-19 stsp return got_error_from_errno();
876 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
878 26ed57b2 2018-05-19 stsp while (!done) {
879 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
880 26ed57b2 2018-05-19 stsp &eof, LINES);
883 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
884 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
885 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
886 26ed57b2 2018-05-19 stsp switch (ch) {
891 26ed57b2 2018-05-19 stsp case KEY_UP:
892 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
893 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
894 b304db33 2018-05-20 stsp first_displayed_line--;
896 b304db33 2018-05-20 stsp case KEY_PPAGE:
898 b304db33 2018-05-20 stsp while (i++ < LINES - 1 && first_displayed_line > 1)
899 26ed57b2 2018-05-19 stsp first_displayed_line--;
902 26ed57b2 2018-05-19 stsp case KEY_DOWN:
903 925e6f23 2018-05-20 stsp case KEY_ENTER:
906 26ed57b2 2018-05-19 stsp first_displayed_line++;
908 b304db33 2018-05-20 stsp case KEY_NPAGE:
911 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
912 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
913 b304db33 2018-05-20 stsp first_displayed_line++;
914 b304db33 2018-05-20 stsp if (line == NULL)
923 26ed57b2 2018-05-19 stsp return err;
926 4ed7e80c 2018-05-20 stsp static const struct got_error *
927 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
929 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
930 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
931 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
932 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
933 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
936 26ed57b2 2018-05-19 stsp #ifndef PROFILE
937 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
938 26ed57b2 2018-05-19 stsp err(1, "pledge");
941 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
942 26ed57b2 2018-05-19 stsp switch (ch) {
945 26ed57b2 2018-05-19 stsp /* NOTREACHED */
949 26ed57b2 2018-05-19 stsp argc -= optind;
950 26ed57b2 2018-05-19 stsp argv += optind;
952 26ed57b2 2018-05-19 stsp if (argc == 0) {
953 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
954 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
955 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
956 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
957 26ed57b2 2018-05-19 stsp return got_error_from_errno();
958 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
959 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
960 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
961 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
962 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
963 26ed57b2 2018-05-19 stsp return got_error_from_errno();
964 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
965 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
967 26ed57b2 2018-05-19 stsp usage_diff();
969 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
970 26ed57b2 2018-05-19 stsp free(repo_path);
974 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
978 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
982 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
984 26ed57b2 2018-05-19 stsp got_repo_close(repo);
986 26ed57b2 2018-05-19 stsp got_object_close(obj1);
988 26ed57b2 2018-05-19 stsp got_object_close(obj2);
989 26ed57b2 2018-05-19 stsp return error;
992 4ed7e80c 2018-05-20 stsp __dead static void
993 9f7d7167 2018-04-29 stsp usage_blame(void)
996 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
997 9f7d7167 2018-04-29 stsp getprogname());
1001 4ed7e80c 2018-05-20 stsp static const struct got_error *
1002 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1004 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1007 5c5136c5 2018-05-20 stsp static void
1008 9f7d7167 2018-04-29 stsp init_curses(void)
1010 9f7d7167 2018-04-29 stsp initscr();
1014 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1015 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1016 1f475ad8 2018-05-10 stsp curs_set(0);
1019 4ed7e80c 2018-05-20 stsp __dead static void
1020 9f7d7167 2018-04-29 stsp usage(void)
1024 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1025 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1026 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1027 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1028 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1033 c2301be8 2018-04-30 stsp static char **
1034 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1036 c2301be8 2018-04-30 stsp char **argv;
1037 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1039 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1040 c2301be8 2018-04-30 stsp if (argv == NULL)
1041 c2301be8 2018-04-30 stsp err(1, "calloc");
1042 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1043 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1044 c2301be8 2018-04-30 stsp err(1, "calloc");
1045 c2301be8 2018-04-30 stsp if (arg1) {
1046 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1047 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1048 c2301be8 2018-04-30 stsp err(1, "calloc");
1051 c2301be8 2018-04-30 stsp return argv;
1055 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1057 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1058 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1059 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1060 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1062 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1064 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1065 9f7d7167 2018-04-29 stsp switch (ch) {
1067 9f7d7167 2018-04-29 stsp hflag = 1;
1071 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1075 9f7d7167 2018-04-29 stsp argc -= optind;
1076 9f7d7167 2018-04-29 stsp argv += optind;
1077 9f7d7167 2018-04-29 stsp optind = 0;
1078 c2301be8 2018-04-30 stsp optreset = 1;
1080 c2301be8 2018-04-30 stsp if (argc == 0) {
1081 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1082 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1083 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1088 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1089 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1090 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1091 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1092 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1093 9f7d7167 2018-04-29 stsp if (hflag)
1094 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1098 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1099 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1100 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1101 c2301be8 2018-04-30 stsp if (repo_path) {
1102 c2301be8 2018-04-30 stsp struct got_repository *repo;
1103 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1104 c2301be8 2018-04-30 stsp if (error == NULL)
1105 c2301be8 2018-04-30 stsp got_repo_close(repo);
1107 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1108 c2301be8 2018-04-30 stsp if (error) {
1109 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1110 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1111 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1112 ad7de8d9 2018-04-30 stsp free(repo_path);
1115 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1116 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1118 c2301be8 2018-04-30 stsp free(repo_path);
1122 5c5136c5 2018-05-20 stsp init_curses();
1124 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1125 9f7d7167 2018-04-29 stsp if (error)
1126 9f7d7167 2018-04-29 stsp goto done;
1129 c2301be8 2018-04-30 stsp free(cmd_argv);
1130 9f7d7167 2018-04-29 stsp if (error)
1131 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);