2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 63219cd2 2019-01-04 stsp #include "got_privsep.h"
44 5c860e29 2018-03-12 stsp #ifndef nitems
45 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
52 99437157 2018-11-11 stsp catch_sigint(int signo)
54 99437157 2018-11-11 stsp sigint_received = 1;
58 99437157 2018-11-11 stsp catch_sigpipe(int signo)
60 99437157 2018-11-11 stsp sigpipe_received = 1;
64 5c860e29 2018-03-12 stsp struct cmd {
65 5c860e29 2018-03-12 stsp const char *cmd_name;
66 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
67 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
68 46a0db7d 2018-03-12 stsp const char *cmd_descr;
71 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
73 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
76 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
77 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
80 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
84 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp #ifdef notyet
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
90 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
91 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
92 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
93 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
94 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
95 1b6b95a8 2018-03-12 stsp "show repository history" },
96 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
97 b00d56cd 2018-04-01 stsp "compare files and directories" },
98 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
99 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
100 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
101 5de5890b 2018-10-18 stsp " list files and directories in repository" },
102 f42b1b34 2018-03-12 stsp #ifdef notyet
103 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
104 1b6b95a8 2018-03-12 stsp "show modification status of files" },
109 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
111 5c860e29 2018-03-12 stsp struct cmd *cmd;
112 5c860e29 2018-03-12 stsp unsigned int i;
114 1b6b95a8 2018-03-12 stsp int hflag = 0;
116 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
118 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
119 5c860e29 2018-03-12 stsp switch (ch) {
125 5c860e29 2018-03-12 stsp /* NOTREACHED */
129 5c860e29 2018-03-12 stsp argc -= optind;
130 5c860e29 2018-03-12 stsp argv += optind;
131 1e70621d 2018-03-27 stsp optind = 0;
133 5c860e29 2018-03-12 stsp if (argc <= 0)
136 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
137 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
139 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
140 d7d4f210 2018-03-12 stsp const struct got_error *error;
142 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
144 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
148 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
150 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
151 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
152 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
159 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
163 4ed7e80c 2018-05-20 stsp __dead static void
164 5c860e29 2018-03-12 stsp usage(void)
168 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
169 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
170 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
171 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
172 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
177 0266afb7 2019-01-04 stsp static const struct got_error *
178 0266afb7 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
180 0266afb7 2019-01-04 stsp const struct got_error *error;
182 0266afb7 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
183 0266afb7 2019-01-04 stsp return got_error_from_errno();
185 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
186 0266afb7 2019-01-04 stsp return got_error_from_errno();
188 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
189 0266afb7 2019-01-04 stsp return got_error_from_errno();
191 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
192 0266afb7 2019-01-04 stsp if (error != NULL)
193 0266afb7 2019-01-04 stsp return error;
195 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
196 0266afb7 2019-01-04 stsp return got_error_from_errno();
198 0266afb7 2019-01-04 stsp return NULL;
201 4ed7e80c 2018-05-20 stsp __dead static void
202 c09a553d 2018-03-12 stsp usage_checkout(void)
204 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
205 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
209 92a684f4 2018-03-12 stsp static void
210 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
212 92a684f4 2018-03-12 stsp char *worktree_path = arg;
214 92a684f4 2018-03-12 stsp while (path[0] == '/')
217 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
220 99437157 2018-11-11 stsp static const struct got_error *
221 99437157 2018-11-11 stsp checkout_cancel(void *arg)
223 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
224 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
225 99437157 2018-11-11 stsp return NULL;
228 4ed7e80c 2018-05-20 stsp static const struct got_error *
229 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
231 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
232 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
233 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
234 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
235 c09a553d 2018-03-12 stsp char *repo_path = NULL;
236 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
237 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
238 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
240 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
241 0bb8a95e 2018-03-12 stsp switch (ch) {
243 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
247 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
251 0bb8a95e 2018-03-12 stsp argc -= optind;
252 0bb8a95e 2018-03-12 stsp argv += optind;
254 6715a751 2018-03-16 stsp #ifndef PROFILE
255 63219cd2 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
256 63219cd2 2019-01-04 stsp NULL) == -1)
257 c09a553d 2018-03-12 stsp err(1, "pledge");
259 0bb8a95e 2018-03-12 stsp if (argc == 1) {
260 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
261 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
262 76089277 2018-04-01 stsp if (repo_path == NULL)
263 76089277 2018-04-01 stsp return got_error_from_errno();
264 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
265 76089277 2018-04-01 stsp if (cwd == NULL) {
266 76089277 2018-04-01 stsp error = got_error_from_errno();
269 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
270 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
272 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
273 76089277 2018-04-01 stsp if (base == NULL) {
274 76089277 2018-04-01 stsp error = got_error_from_errno();
277 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
278 c09a553d 2018-03-12 stsp if (dotgit)
279 c09a553d 2018-03-12 stsp *dotgit = '\0';
280 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
281 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
286 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
287 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
288 76089277 2018-04-01 stsp if (repo_path == NULL) {
289 76089277 2018-04-01 stsp error = got_error_from_errno();
292 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
293 76089277 2018-04-01 stsp if (worktree_path == NULL) {
294 76089277 2018-04-01 stsp error = got_error_from_errno();
298 c09a553d 2018-03-12 stsp usage_checkout();
300 0266afb7 2019-01-04 stsp error = apply_unveil(repo_path, worktree_path);
304 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
305 c09a553d 2018-03-12 stsp if (error != NULL)
307 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
308 c09a553d 2018-03-12 stsp if (error != NULL)
311 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
312 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
315 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
316 c09a553d 2018-03-12 stsp if (error != NULL)
319 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
320 e5dc7198 2018-12-29 stsp path_prefix);
321 e5dc7198 2018-12-29 stsp if (error != NULL)
323 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
324 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
328 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
329 99437157 2018-11-11 stsp checkout_progress, worktree_path, checkout_cancel, NULL);
330 c09a553d 2018-03-12 stsp if (error != NULL)
333 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
336 76089277 2018-04-01 stsp free(repo_path);
337 507dc3bb 2018-12-29 stsp free(worktree_path);
338 507dc3bb 2018-12-29 stsp return error;
341 507dc3bb 2018-12-29 stsp __dead static void
342 507dc3bb 2018-12-29 stsp usage_update(void)
344 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
345 507dc3bb 2018-12-29 stsp getprogname());
349 507dc3bb 2018-12-29 stsp static void
350 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
352 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
355 507dc3bb 2018-12-29 stsp while (path[0] == '/')
357 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
360 507dc3bb 2018-12-29 stsp static const struct got_error *
361 be7061eb 2018-12-30 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
362 be7061eb 2018-12-30 stsp struct got_repository *repo)
364 be7061eb 2018-12-30 stsp const struct got_error *err;
365 be7061eb 2018-12-30 stsp struct got_reference *head_ref = NULL;
366 be7061eb 2018-12-30 stsp struct got_object_id *head_commit_id = NULL;
367 be7061eb 2018-12-30 stsp struct got_commit_graph *graph = NULL;
369 be7061eb 2018-12-30 stsp head_ref = got_worktree_get_head_ref(worktree);
370 be7061eb 2018-12-30 stsp if (head_ref == NULL)
371 be7061eb 2018-12-30 stsp return got_error_from_errno();
373 2944241d 2018-12-30 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
374 be7061eb 2018-12-30 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
378 be7061eb 2018-12-30 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
382 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
385 be7061eb 2018-12-30 stsp while (1) {
386 be7061eb 2018-12-30 stsp struct got_object_id *id;
388 be7061eb 2018-12-30 stsp if (sigint_received || sigpipe_received)
391 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_next(&id, graph);
393 be7061eb 2018-12-30 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
394 be7061eb 2018-12-30 stsp err = got_error(GOT_ERR_ANCESTRY);
397 be7061eb 2018-12-30 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
399 be7061eb 2018-12-30 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
405 be7061eb 2018-12-30 stsp if (id == NULL)
407 be7061eb 2018-12-30 stsp if (got_object_id_cmp(id, commit_id) == 0)
411 be7061eb 2018-12-30 stsp if (head_ref)
412 be7061eb 2018-12-30 stsp got_ref_close(head_ref);
414 be7061eb 2018-12-30 stsp got_commit_graph_close(graph);
415 be7061eb 2018-12-30 stsp return err;
418 be7061eb 2018-12-30 stsp static const struct got_error *
419 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
421 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
422 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
423 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
424 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
425 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
426 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
429 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
430 507dc3bb 2018-12-29 stsp switch (ch) {
432 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
433 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
434 9c4b8182 2019-01-02 stsp return got_error_from_errno();
438 507dc3bb 2018-12-29 stsp /* NOTREACHED */
442 507dc3bb 2018-12-29 stsp argc -= optind;
443 507dc3bb 2018-12-29 stsp argv += optind;
445 507dc3bb 2018-12-29 stsp #ifndef PROFILE
446 0266afb7 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
447 0266afb7 2019-01-04 stsp NULL) == -1)
448 507dc3bb 2018-12-29 stsp err(1, "pledge");
450 507dc3bb 2018-12-29 stsp if (argc == 0) {
451 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
452 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
453 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
456 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
457 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
458 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
459 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
463 507dc3bb 2018-12-29 stsp usage_update();
465 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
466 507dc3bb 2018-12-29 stsp if (error != NULL)
469 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
470 507dc3bb 2018-12-29 stsp if (error != NULL)
473 7839bc15 2019-01-06 stsp error = apply_unveil(got_repo_get_path(repo), worktree_path);
477 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
478 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
479 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
480 507dc3bb 2018-12-29 stsp if (error != NULL)
482 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
483 9c4b8182 2019-01-02 stsp if (error != NULL)
485 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
486 507dc3bb 2018-12-29 stsp if (error != NULL)
489 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
490 507dc3bb 2018-12-29 stsp commit_id_str);
491 507dc3bb 2018-12-29 stsp if (error != NULL)
495 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
496 be7061eb 2018-12-30 stsp if (error != NULL)
499 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
500 507dc3bb 2018-12-29 stsp commit_id) != 0) {
501 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
502 507dc3bb 2018-12-29 stsp commit_id);
507 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
508 507dc3bb 2018-12-29 stsp update_progress, NULL, checkout_cancel, NULL);
509 507dc3bb 2018-12-29 stsp if (error != NULL)
512 9c4b8182 2019-01-02 stsp printf("Updated to commit %s\n", commit_id_str);
514 c09a553d 2018-03-12 stsp free(worktree_path);
515 507dc3bb 2018-12-29 stsp free(commit_id);
516 9c4b8182 2019-01-02 stsp free(commit_id_str);
517 c09a553d 2018-03-12 stsp return error;
520 f42b1b34 2018-03-12 stsp static const struct got_error *
521 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
522 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
524 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
525 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
526 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
527 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
529 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
530 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
532 79109fed 2018-03-27 stsp return err;
534 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
535 79f35eb3 2018-06-11 stsp if (qid != NULL) {
536 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
538 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
540 79109fed 2018-03-27 stsp return err;
542 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
543 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
544 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
546 0f2b3dca 2018-12-22 stsp return err;
548 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
550 79109fed 2018-03-27 stsp return err;
553 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
557 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
558 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
561 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
562 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
563 0f2b3dca 2018-12-22 stsp free(id_str1);
564 0f2b3dca 2018-12-22 stsp free(id_str2);
565 79109fed 2018-03-27 stsp return err;
568 4bb494d5 2018-06-16 stsp static char *
569 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
571 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
572 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
578 79109fed 2018-03-27 stsp static const struct got_error *
579 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
580 c0cc5c62 2018-10-18 stsp struct got_repository *repo, int show_patch, int diff_context)
582 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
583 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
584 6c281f94 2018-06-11 stsp char datebuf[26];
585 45d799e2 2018-12-23 stsp time_t committer_time;
586 45d799e2 2018-12-23 stsp const char *author, *committer;
588 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
590 8bf5b3c9 2018-03-17 stsp return err;
592 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
593 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
594 832c249c 2018-06-10 stsp free(id_str);
595 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
596 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
597 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
598 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
599 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
600 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
601 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
602 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
603 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
604 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
605 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
607 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
608 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
609 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
611 a0603db2 2018-06-10 stsp return err;
612 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
613 a0603db2 2018-06-10 stsp free(id_str);
617 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
618 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
619 832c249c 2018-06-10 stsp return got_error_from_errno();
621 621015ac 2018-07-23 stsp logmsg = logmsg0;
623 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
625 832c249c 2018-06-10 stsp printf(" %s\n", line);
626 832c249c 2018-06-10 stsp } while (line);
627 621015ac 2018-07-23 stsp free(logmsg0);
629 971751ac 2018-03-27 stsp if (show_patch) {
630 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
631 971751ac 2018-03-27 stsp if (err == 0)
632 971751ac 2018-03-27 stsp printf("\n");
635 d82de447 2018-09-15 stsp fflush(stdout);
636 07862c20 2018-09-15 stsp return err;
639 f42b1b34 2018-03-12 stsp static const struct got_error *
640 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
641 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
642 15a94983 2018-12-23 stsp int first_parent_traversal)
644 f42b1b34 2018-03-12 stsp const struct got_error *err;
645 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
647 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
648 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
650 f42b1b34 2018-03-12 stsp return err;
651 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
654 31cedeaf 2018-09-15 stsp while (1) {
655 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
656 372ccdbb 2018-06-10 stsp struct got_object_id *id;
658 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
661 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
663 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
664 9ba79e04 2018-06-11 stsp err = NULL;
667 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
669 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
675 b43fbaa0 2018-06-11 stsp if (id == NULL)
678 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
681 c0cc5c62 2018-10-18 stsp err = print_commit(commit, id, repo, show_patch, diff_context);
682 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
683 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
687 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
688 f42b1b34 2018-03-12 stsp return err;
691 4ed7e80c 2018-05-20 stsp __dead static void
692 6f3d1eb0 2018-03-12 stsp usage_log(void)
694 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
695 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
699 4ed7e80c 2018-05-20 stsp static const struct got_error *
700 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
702 f42b1b34 2018-03-12 stsp const struct got_error *error;
703 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
704 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
705 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
706 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
707 d142fc45 2018-04-01 stsp char *start_commit = NULL;
708 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
709 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
710 64a96a6d 2018-04-01 stsp const char *errstr;
712 6715a751 2018-03-16 stsp #ifndef PROFILE
713 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
716 f42b1b34 2018-03-12 stsp err(1, "pledge");
719 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
720 79109fed 2018-03-27 stsp switch (ch) {
722 79109fed 2018-03-27 stsp show_patch = 1;
725 d142fc45 2018-04-01 stsp start_commit = optarg;
728 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
730 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
731 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
734 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
735 64a96a6d 2018-04-01 stsp if (errstr != NULL)
736 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
739 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
742 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
743 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
744 04ca23f4 2018-07-16 stsp err(1, "-r option");
748 79109fed 2018-03-27 stsp /* NOTREACHED */
752 79109fed 2018-03-27 stsp argc -= optind;
753 79109fed 2018-03-27 stsp argv += optind;
755 04ca23f4 2018-07-16 stsp if (argc == 0)
756 04ca23f4 2018-07-16 stsp path = strdup("");
757 04ca23f4 2018-07-16 stsp else if (argc == 1)
758 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
760 6f3d1eb0 2018-03-12 stsp usage_log();
761 04ca23f4 2018-07-16 stsp if (path == NULL)
762 04ca23f4 2018-07-16 stsp return got_error_from_errno();
764 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
765 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
766 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
769 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
770 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
771 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
772 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
777 6098196c 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
781 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
782 d7d4f210 2018-03-12 stsp if (error != NULL)
785 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
786 3235492e 2018-04-01 stsp struct got_reference *head_ref;
787 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
788 3235492e 2018-04-01 stsp if (error != NULL)
789 3235492e 2018-04-01 stsp return error;
790 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
791 3235492e 2018-04-01 stsp got_ref_close(head_ref);
792 3235492e 2018-04-01 stsp if (error != NULL)
793 3235492e 2018-04-01 stsp return error;
794 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
796 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
797 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
798 3235492e 2018-04-01 stsp if (error == NULL) {
799 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
800 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
801 d1f2edc9 2018-06-13 stsp if (error != NULL)
802 d1f2edc9 2018-06-13 stsp return error;
803 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
804 d1f2edc9 2018-06-13 stsp if (error != NULL)
805 d1f2edc9 2018-06-13 stsp return error;
807 15a94983 2018-12-23 stsp if (commit == NULL) {
808 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
809 d1f2edc9 2018-06-13 stsp start_commit);
810 d1f2edc9 2018-06-13 stsp if (error != NULL)
811 d1f2edc9 2018-06-13 stsp return error;
814 d7d4f210 2018-03-12 stsp if (error != NULL)
817 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
818 04ca23f4 2018-07-16 stsp if (error != NULL)
820 04ca23f4 2018-07-16 stsp if (in_repo_path) {
821 04ca23f4 2018-07-16 stsp free(path);
822 04ca23f4 2018-07-16 stsp path = in_repo_path;
825 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
826 c0cc5c62 2018-10-18 stsp diff_context, limit, first_parent_traversal);
828 04ca23f4 2018-07-16 stsp free(path);
829 04ca23f4 2018-07-16 stsp free(repo_path);
832 ad242220 2018-09-08 stsp if (repo) {
833 ad242220 2018-09-08 stsp const struct got_error *repo_error;
834 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
835 ad242220 2018-09-08 stsp if (error == NULL)
836 ad242220 2018-09-08 stsp error = repo_error;
838 8bf5b3c9 2018-03-17 stsp return error;
841 4ed7e80c 2018-05-20 stsp __dead static void
842 3f8b7d6a 2018-04-01 stsp usage_diff(void)
844 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
845 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
849 4ed7e80c 2018-05-20 stsp static const struct got_error *
850 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
852 b00d56cd 2018-04-01 stsp const struct got_error *error;
853 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
854 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
855 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
856 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
857 15a94983 2018-12-23 stsp int type1, type2;
858 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
859 c0cc5c62 2018-10-18 stsp const char *errstr;
861 b00d56cd 2018-04-01 stsp #ifndef PROFILE
862 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
863 25eccc22 2019-01-04 stsp NULL) == -1)
864 b00d56cd 2018-04-01 stsp err(1, "pledge");
867 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
868 b00d56cd 2018-04-01 stsp switch (ch) {
870 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
871 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
872 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
876 b00d56cd 2018-04-01 stsp /* NOTREACHED */
880 b00d56cd 2018-04-01 stsp argc -= optind;
881 b00d56cd 2018-04-01 stsp argv += optind;
883 b00d56cd 2018-04-01 stsp if (argc == 0) {
884 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
885 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
886 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
887 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
888 e1e3f570 2018-04-01 stsp return got_error_from_errno();
889 15a94983 2018-12-23 stsp id_str1 = argv[0];
890 15a94983 2018-12-23 stsp id_str2 = argv[1];
891 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
892 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
893 76089277 2018-04-01 stsp if (repo_path == NULL)
894 76089277 2018-04-01 stsp return got_error_from_errno();
895 15a94983 2018-12-23 stsp id_str1 = argv[1];
896 15a94983 2018-12-23 stsp id_str2 = argv[2];
898 b00d56cd 2018-04-01 stsp usage_diff();
900 25eccc22 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
904 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
905 76089277 2018-04-01 stsp free(repo_path);
906 b00d56cd 2018-04-01 stsp if (error != NULL)
909 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
913 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
917 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
921 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
925 15a94983 2018-12-23 stsp if (type1 != type2) {
926 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
930 15a94983 2018-12-23 stsp switch (type1) {
931 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
932 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
933 54156555 2018-12-24 stsp diff_context, repo, stdout);
935 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
936 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
937 54156555 2018-12-24 stsp diff_context, repo, stdout);
939 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
940 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
942 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
943 c0cc5c62 2018-10-18 stsp repo, stdout);
946 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
952 ad242220 2018-09-08 stsp if (repo) {
953 ad242220 2018-09-08 stsp const struct got_error *repo_error;
954 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
955 ad242220 2018-09-08 stsp if (error == NULL)
956 ad242220 2018-09-08 stsp error = repo_error;
958 b00d56cd 2018-04-01 stsp return error;
961 404c43c4 2018-06-21 stsp __dead static void
962 404c43c4 2018-06-21 stsp usage_blame(void)
964 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
965 404c43c4 2018-06-21 stsp getprogname());
969 404c43c4 2018-06-21 stsp static const struct got_error *
970 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
972 404c43c4 2018-06-21 stsp const struct got_error *error;
973 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
974 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
975 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
976 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
979 404c43c4 2018-06-21 stsp #ifndef PROFILE
980 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
981 36e2fb66 2019-01-04 stsp NULL) == -1)
982 404c43c4 2018-06-21 stsp err(1, "pledge");
985 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
986 404c43c4 2018-06-21 stsp switch (ch) {
988 404c43c4 2018-06-21 stsp commit_id_str = optarg;
991 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
992 66bea077 2018-08-02 stsp if (repo_path == NULL)
993 66bea077 2018-08-02 stsp err(1, "-r option");
997 404c43c4 2018-06-21 stsp /* NOTREACHED */
1001 404c43c4 2018-06-21 stsp argc -= optind;
1002 404c43c4 2018-06-21 stsp argv += optind;
1004 a39318fd 2018-08-02 stsp if (argc == 1)
1005 404c43c4 2018-06-21 stsp path = argv[0];
1007 404c43c4 2018-06-21 stsp usage_blame();
1009 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1010 66bea077 2018-08-02 stsp if (cwd == NULL) {
1011 66bea077 2018-08-02 stsp error = got_error_from_errno();
1012 66bea077 2018-08-02 stsp goto done;
1014 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1015 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
1016 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1017 66bea077 2018-08-02 stsp error = got_error_from_errno();
1018 66bea077 2018-08-02 stsp goto done;
1022 36e2fb66 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1023 36e2fb66 2019-01-04 stsp if (error)
1024 36e2fb66 2019-01-04 stsp goto done;
1026 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1027 404c43c4 2018-06-21 stsp if (error != NULL)
1028 404c43c4 2018-06-21 stsp goto done;
1030 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1031 66bea077 2018-08-02 stsp if (error != NULL)
1032 66bea077 2018-08-02 stsp goto done;
1034 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1035 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1036 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1037 404c43c4 2018-06-21 stsp if (error != NULL)
1038 66bea077 2018-08-02 stsp goto done;
1039 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1040 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1041 404c43c4 2018-06-21 stsp if (error != NULL)
1042 66bea077 2018-08-02 stsp goto done;
1044 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1045 15a94983 2018-12-23 stsp commit_id_str);
1046 404c43c4 2018-06-21 stsp if (error != NULL)
1047 66bea077 2018-08-02 stsp goto done;
1050 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1052 66bea077 2018-08-02 stsp free(in_repo_path);
1053 66bea077 2018-08-02 stsp free(repo_path);
1054 66bea077 2018-08-02 stsp free(cwd);
1055 404c43c4 2018-06-21 stsp free(commit_id);
1056 ad242220 2018-09-08 stsp if (repo) {
1057 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1058 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1059 ad242220 2018-09-08 stsp if (error == NULL)
1060 ad242220 2018-09-08 stsp error = repo_error;
1062 404c43c4 2018-06-21 stsp return error;
1065 5de5890b 2018-10-18 stsp __dead static void
1066 5de5890b 2018-10-18 stsp usage_tree(void)
1068 c1669e2e 2019-01-09 stsp fprintf(stderr,
1069 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1070 5de5890b 2018-10-18 stsp getprogname());
1074 c1669e2e 2019-01-09 stsp static void
1075 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1076 c1669e2e 2019-01-09 stsp const char *root_path)
1078 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1080 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1081 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1084 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1085 c1669e2e 2019-01-09 stsp is_root_path ? "" : "/",
1086 c1669e2e 2019-01-09 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1089 5de5890b 2018-10-18 stsp static const struct got_error *
1090 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1091 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1092 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1094 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1095 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1096 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1097 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1098 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1100 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1102 5de5890b 2018-10-18 stsp goto done;
1104 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1106 5de5890b 2018-10-18 stsp goto done;
1107 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1108 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1109 5de5890b 2018-10-18 stsp while (te) {
1110 5de5890b 2018-10-18 stsp char *id = NULL;
1112 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1115 5de5890b 2018-10-18 stsp if (show_ids) {
1116 5de5890b 2018-10-18 stsp char *id_str;
1117 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1119 5de5890b 2018-10-18 stsp goto done;
1120 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1121 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1122 5de5890b 2018-10-18 stsp free(id_str);
1123 5de5890b 2018-10-18 stsp goto done;
1125 5de5890b 2018-10-18 stsp free(id_str);
1127 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1130 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1131 c1669e2e 2019-01-09 stsp char *child_path;
1132 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1133 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1134 c1669e2e 2019-01-09 stsp te->name) == -1) {
1135 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1136 c1669e2e 2019-01-09 stsp goto done;
1138 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1139 c1669e2e 2019-01-09 stsp root_path, repo);
1140 c1669e2e 2019-01-09 stsp free(child_path);
1142 c1669e2e 2019-01-09 stsp goto done;
1145 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1149 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1150 5de5890b 2018-10-18 stsp free(tree_id);
1151 5de5890b 2018-10-18 stsp return err;
1154 5de5890b 2018-10-18 stsp static const struct got_error *
1155 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1157 5de5890b 2018-10-18 stsp const struct got_error *error;
1158 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1159 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1160 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1161 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1162 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1165 5de5890b 2018-10-18 stsp #ifndef PROFILE
1166 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1167 0f8d269b 2019-01-04 stsp NULL) == -1)
1168 5de5890b 2018-10-18 stsp err(1, "pledge");
1171 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1172 5de5890b 2018-10-18 stsp switch (ch) {
1174 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1177 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1178 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1179 5de5890b 2018-10-18 stsp err(1, "-r option");
1182 5de5890b 2018-10-18 stsp show_ids = 1;
1185 c1669e2e 2019-01-09 stsp recurse = 1;
1189 5de5890b 2018-10-18 stsp /* NOTREACHED */
1193 5de5890b 2018-10-18 stsp argc -= optind;
1194 5de5890b 2018-10-18 stsp argv += optind;
1196 5de5890b 2018-10-18 stsp if (argc == 1)
1197 5de5890b 2018-10-18 stsp path = argv[0];
1198 5de5890b 2018-10-18 stsp else if (argc > 1)
1199 5de5890b 2018-10-18 stsp usage_tree();
1201 5de5890b 2018-10-18 stsp path = "/";
1203 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
1204 5de5890b 2018-10-18 stsp if (cwd == NULL) {
1205 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1206 5de5890b 2018-10-18 stsp goto done;
1208 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1209 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
1210 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1211 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1212 5de5890b 2018-10-18 stsp goto done;
1216 0f8d269b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1217 0f8d269b 2019-01-04 stsp if (error)
1218 0f8d269b 2019-01-04 stsp goto done;
1220 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1221 5de5890b 2018-10-18 stsp if (error != NULL)
1222 5de5890b 2018-10-18 stsp goto done;
1224 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1225 5de5890b 2018-10-18 stsp if (error != NULL)
1226 5de5890b 2018-10-18 stsp goto done;
1228 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1229 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1230 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1231 5de5890b 2018-10-18 stsp if (error != NULL)
1232 5de5890b 2018-10-18 stsp goto done;
1233 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1234 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1235 5de5890b 2018-10-18 stsp if (error != NULL)
1236 5de5890b 2018-10-18 stsp goto done;
1238 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1239 15a94983 2018-12-23 stsp commit_id_str);
1240 5de5890b 2018-10-18 stsp if (error != NULL)
1241 5de5890b 2018-10-18 stsp goto done;
1244 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1245 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1247 5de5890b 2018-10-18 stsp free(in_repo_path);
1248 5de5890b 2018-10-18 stsp free(repo_path);
1249 5de5890b 2018-10-18 stsp free(cwd);
1250 5de5890b 2018-10-18 stsp free(commit_id);
1251 5de5890b 2018-10-18 stsp if (repo) {
1252 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1253 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1254 5de5890b 2018-10-18 stsp if (error == NULL)
1255 5de5890b 2018-10-18 stsp error = repo_error;
1257 5de5890b 2018-10-18 stsp return error;
1260 f42b1b34 2018-03-12 stsp #ifdef notyet
1261 4ed7e80c 2018-05-20 stsp static const struct got_error *
1262 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
1264 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
1265 5c860e29 2018-03-12 stsp git_status_list *status;
1266 5c860e29 2018-03-12 stsp git_status_options statusopts;
1269 5c860e29 2018-03-12 stsp git_libgit2_init();
1271 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
1272 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
1274 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
1275 5c860e29 2018-03-12 stsp errx(1, "bar repository");
1277 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1278 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
1280 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1281 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1282 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1283 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1285 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
1286 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
1288 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
1289 5c860e29 2018-03-12 stsp const git_status_entry *se;
1291 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
1292 5c860e29 2018-03-12 stsp switch (se->status) {
1293 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
1294 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
1296 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
1297 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
1299 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
1300 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
1302 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
1303 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1304 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
1305 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
1307 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
1308 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
1310 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
1311 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
1313 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
1314 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
1316 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
1317 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
1319 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
1320 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1321 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
1322 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
1324 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
1325 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
1327 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
1333 5c860e29 2018-03-12 stsp git_status_list_free(status);
1334 5c860e29 2018-03-12 stsp git_repository_free(repo);
1335 5c860e29 2018-03-12 stsp git_libgit2_shutdown();