2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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);
78 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
81 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
84 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
85 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
89 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
90 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
91 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
92 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
93 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
94 1b6b95a8 2018-03-12 stsp "show repository history" },
95 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
96 b00d56cd 2018-04-01 stsp "compare files and directories" },
97 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
98 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
99 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
100 5de5890b 2018-10-18 stsp " list files and directories in repository" },
101 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
102 1b6b95a8 2018-03-12 stsp "show modification status of files" },
106 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
108 5c860e29 2018-03-12 stsp struct cmd *cmd;
109 5c860e29 2018-03-12 stsp unsigned int i;
111 1b6b95a8 2018-03-12 stsp int hflag = 0;
113 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
115 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
116 5c860e29 2018-03-12 stsp switch (ch) {
122 5c860e29 2018-03-12 stsp /* NOTREACHED */
126 5c860e29 2018-03-12 stsp argc -= optind;
127 5c860e29 2018-03-12 stsp argv += optind;
128 1e70621d 2018-03-27 stsp optind = 0;
130 5c860e29 2018-03-12 stsp if (argc <= 0)
133 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
134 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
136 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
137 d7d4f210 2018-03-12 stsp const struct got_error *error;
139 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
141 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
145 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
147 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
148 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
149 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
156 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
160 4ed7e80c 2018-05-20 stsp __dead static void
161 5c860e29 2018-03-12 stsp usage(void)
165 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
166 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
167 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
168 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
169 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
174 0266afb7 2019-01-04 stsp static const struct got_error *
175 0266afb7 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
177 0266afb7 2019-01-04 stsp const struct got_error *error;
179 0266afb7 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
180 0266afb7 2019-01-04 stsp return got_error_from_errno();
182 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
183 0266afb7 2019-01-04 stsp return got_error_from_errno();
185 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
186 0266afb7 2019-01-04 stsp return got_error_from_errno();
188 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
189 0266afb7 2019-01-04 stsp if (error != NULL)
190 0266afb7 2019-01-04 stsp return error;
192 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
193 0266afb7 2019-01-04 stsp return got_error_from_errno();
195 0266afb7 2019-01-04 stsp return NULL;
198 4ed7e80c 2018-05-20 stsp __dead static void
199 c09a553d 2018-03-12 stsp usage_checkout(void)
201 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
206 92a684f4 2018-03-12 stsp static void
207 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
209 92a684f4 2018-03-12 stsp char *worktree_path = arg;
211 92a684f4 2018-03-12 stsp while (path[0] == '/')
214 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
217 99437157 2018-11-11 stsp static const struct got_error *
218 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
220 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
221 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
222 99437157 2018-11-11 stsp return NULL;
225 8069f636 2019-01-12 stsp static const struct got_error *
226 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
227 8069f636 2019-01-12 stsp struct got_repository *repo)
229 8069f636 2019-01-12 stsp const struct got_error *err;
230 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
231 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
232 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
234 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
235 8069f636 2019-01-12 stsp if (head_ref == NULL)
236 8069f636 2019-01-12 stsp return got_error_from_errno();
238 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
239 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
243 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
247 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
250 8069f636 2019-01-12 stsp while (1) {
251 8069f636 2019-01-12 stsp struct got_object_id *id;
253 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
256 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
258 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
259 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
262 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
264 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
270 8069f636 2019-01-12 stsp if (id == NULL)
272 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
276 8069f636 2019-01-12 stsp if (head_ref)
277 8069f636 2019-01-12 stsp got_ref_close(head_ref);
279 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
280 8069f636 2019-01-12 stsp return err;
284 4ed7e80c 2018-05-20 stsp static const struct got_error *
285 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
287 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
288 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
289 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
290 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
291 c09a553d 2018-03-12 stsp char *repo_path = NULL;
292 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
293 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
294 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
295 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
297 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
298 0bb8a95e 2018-03-12 stsp switch (ch) {
300 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
301 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
302 8069f636 2019-01-12 stsp return got_error_from_errno();
305 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
309 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
313 0bb8a95e 2018-03-12 stsp argc -= optind;
314 0bb8a95e 2018-03-12 stsp argv += optind;
316 6715a751 2018-03-16 stsp #ifndef PROFILE
317 63219cd2 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
318 63219cd2 2019-01-04 stsp NULL) == -1)
319 c09a553d 2018-03-12 stsp err(1, "pledge");
321 0bb8a95e 2018-03-12 stsp if (argc == 1) {
322 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
323 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
324 76089277 2018-04-01 stsp if (repo_path == NULL)
325 76089277 2018-04-01 stsp return got_error_from_errno();
326 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
327 76089277 2018-04-01 stsp if (cwd == NULL) {
328 76089277 2018-04-01 stsp error = got_error_from_errno();
331 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
332 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
334 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
335 76089277 2018-04-01 stsp if (base == NULL) {
336 76089277 2018-04-01 stsp error = got_error_from_errno();
339 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
340 c09a553d 2018-03-12 stsp if (dotgit)
341 c09a553d 2018-03-12 stsp *dotgit = '\0';
342 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
348 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
349 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
350 76089277 2018-04-01 stsp if (repo_path == NULL) {
351 76089277 2018-04-01 stsp error = got_error_from_errno();
354 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
355 76089277 2018-04-01 stsp if (worktree_path == NULL) {
356 76089277 2018-04-01 stsp error = got_error_from_errno();
360 c09a553d 2018-03-12 stsp usage_checkout();
362 0266afb7 2019-01-04 stsp error = apply_unveil(repo_path, worktree_path);
366 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
367 c09a553d 2018-03-12 stsp if (error != NULL)
370 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 c09a553d 2018-03-12 stsp if (error != NULL)
374 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
379 c09a553d 2018-03-12 stsp if (error != NULL)
382 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
383 e5dc7198 2018-12-29 stsp path_prefix);
384 e5dc7198 2018-12-29 stsp if (error != NULL)
386 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
387 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
391 8069f636 2019-01-12 stsp if (commit_id_str) {
392 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
393 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
394 8069f636 2019-01-12 stsp commit_id_str);
395 8069f636 2019-01-12 stsp if (error != NULL)
397 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
398 8069f636 2019-01-12 stsp if (error != NULL) {
399 8069f636 2019-01-12 stsp free(commit_id);
402 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
403 8069f636 2019-01-12 stsp commit_id);
404 8069f636 2019-01-12 stsp free(commit_id);
409 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
410 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
411 c09a553d 2018-03-12 stsp if (error != NULL)
414 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
417 8069f636 2019-01-12 stsp free(commit_id_str);
418 76089277 2018-04-01 stsp free(repo_path);
419 507dc3bb 2018-12-29 stsp free(worktree_path);
420 507dc3bb 2018-12-29 stsp return error;
423 507dc3bb 2018-12-29 stsp __dead static void
424 507dc3bb 2018-12-29 stsp usage_update(void)
426 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
427 507dc3bb 2018-12-29 stsp getprogname());
431 507dc3bb 2018-12-29 stsp static void
432 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
434 784955db 2019-01-12 stsp int *did_something = arg;
436 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
439 784955db 2019-01-12 stsp *did_something = 1;
440 507dc3bb 2018-12-29 stsp while (path[0] == '/')
442 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
445 be7061eb 2018-12-30 stsp static const struct got_error *
446 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
448 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
449 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
450 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
451 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
452 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
453 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
454 784955db 2019-01-12 stsp int ch, did_something = 0;
456 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
457 507dc3bb 2018-12-29 stsp switch (ch) {
459 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
460 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
461 9c4b8182 2019-01-02 stsp return got_error_from_errno();
465 507dc3bb 2018-12-29 stsp /* NOTREACHED */
469 507dc3bb 2018-12-29 stsp argc -= optind;
470 507dc3bb 2018-12-29 stsp argv += optind;
472 507dc3bb 2018-12-29 stsp #ifndef PROFILE
473 0266afb7 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
474 0266afb7 2019-01-04 stsp NULL) == -1)
475 507dc3bb 2018-12-29 stsp err(1, "pledge");
477 507dc3bb 2018-12-29 stsp if (argc == 0) {
478 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
479 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
480 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
483 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
484 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
485 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
486 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
490 507dc3bb 2018-12-29 stsp usage_update();
492 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
493 507dc3bb 2018-12-29 stsp if (error != NULL)
496 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
497 507dc3bb 2018-12-29 stsp if (error != NULL)
500 7839bc15 2019-01-06 stsp error = apply_unveil(got_repo_get_path(repo), worktree_path);
504 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
505 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
506 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
507 507dc3bb 2018-12-29 stsp if (error != NULL)
509 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
510 9c4b8182 2019-01-02 stsp if (error != NULL)
512 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
513 507dc3bb 2018-12-29 stsp if (error != NULL)
516 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
517 507dc3bb 2018-12-29 stsp commit_id_str);
518 507dc3bb 2018-12-29 stsp if (error != NULL)
522 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
523 be7061eb 2018-12-30 stsp if (error != NULL)
526 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
527 507dc3bb 2018-12-29 stsp commit_id) != 0) {
528 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
529 507dc3bb 2018-12-29 stsp commit_id);
534 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
535 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
536 507dc3bb 2018-12-29 stsp if (error != NULL)
539 784955db 2019-01-12 stsp if (did_something)
540 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
542 784955db 2019-01-12 stsp printf("Already up-to-date\n");
544 c09a553d 2018-03-12 stsp free(worktree_path);
545 507dc3bb 2018-12-29 stsp free(commit_id);
546 9c4b8182 2019-01-02 stsp free(commit_id_str);
547 c09a553d 2018-03-12 stsp return error;
550 f42b1b34 2018-03-12 stsp static const struct got_error *
551 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
552 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
554 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
555 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
556 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
557 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
559 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
560 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
562 79109fed 2018-03-27 stsp return err;
564 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
565 79f35eb3 2018-06-11 stsp if (qid != NULL) {
566 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
568 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
570 79109fed 2018-03-27 stsp return err;
572 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
573 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
574 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
576 0f2b3dca 2018-12-22 stsp return err;
578 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
580 79109fed 2018-03-27 stsp return err;
583 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
587 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
588 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
591 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
592 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
593 0f2b3dca 2018-12-22 stsp free(id_str1);
594 0f2b3dca 2018-12-22 stsp free(id_str2);
595 79109fed 2018-03-27 stsp return err;
598 4bb494d5 2018-06-16 stsp static char *
599 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
601 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
602 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
608 79109fed 2018-03-27 stsp static const struct got_error *
609 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
610 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
611 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
613 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
614 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
615 6c281f94 2018-06-11 stsp char datebuf[26];
616 45d799e2 2018-12-23 stsp time_t committer_time;
617 45d799e2 2018-12-23 stsp const char *author, *committer;
618 199a4027 2019-02-02 stsp char *refs_str = NULL;
619 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
621 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
623 199a4027 2019-02-02 stsp const char *name;
624 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
626 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
627 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
629 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
631 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
633 199a4027 2019-02-02 stsp s = refs_str;
634 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
635 199a4027 2019-02-02 stsp name) == -1) {
636 199a4027 2019-02-02 stsp err = got_error_from_errno();
642 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
644 8bf5b3c9 2018-03-17 stsp return err;
646 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
647 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
648 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
649 832c249c 2018-06-10 stsp free(id_str);
650 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
651 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
652 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
653 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
654 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
655 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
656 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
657 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
658 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
659 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
660 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
662 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
663 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
664 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
666 a0603db2 2018-06-10 stsp return err;
667 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
668 a0603db2 2018-06-10 stsp free(id_str);
672 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
673 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
674 832c249c 2018-06-10 stsp return got_error_from_errno();
676 621015ac 2018-07-23 stsp logmsg = logmsg0;
678 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
680 832c249c 2018-06-10 stsp printf(" %s\n", line);
681 832c249c 2018-06-10 stsp } while (line);
682 621015ac 2018-07-23 stsp free(logmsg0);
684 971751ac 2018-03-27 stsp if (show_patch) {
685 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
686 971751ac 2018-03-27 stsp if (err == 0)
687 971751ac 2018-03-27 stsp printf("\n");
690 d82de447 2018-09-15 stsp fflush(stdout);
691 07862c20 2018-09-15 stsp return err;
694 f42b1b34 2018-03-12 stsp static const struct got_error *
695 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
696 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
697 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
699 f42b1b34 2018-03-12 stsp const struct got_error *err;
700 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
702 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
703 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
705 f42b1b34 2018-03-12 stsp return err;
706 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
709 31cedeaf 2018-09-15 stsp while (1) {
710 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
711 372ccdbb 2018-06-10 stsp struct got_object_id *id;
713 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
716 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
718 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
719 9ba79e04 2018-06-11 stsp err = NULL;
722 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
724 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
730 b43fbaa0 2018-06-11 stsp if (id == NULL)
733 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
736 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
738 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
739 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
743 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
744 f42b1b34 2018-03-12 stsp return err;
747 4ed7e80c 2018-05-20 stsp __dead static void
748 6f3d1eb0 2018-03-12 stsp usage_log(void)
750 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
751 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
755 4ed7e80c 2018-05-20 stsp static const struct got_error *
756 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
758 f42b1b34 2018-03-12 stsp const struct got_error *error;
759 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
760 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
761 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
762 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
763 d142fc45 2018-04-01 stsp char *start_commit = NULL;
764 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
765 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
766 64a96a6d 2018-04-01 stsp const char *errstr;
767 199a4027 2019-02-02 stsp struct got_reflist_head refs;
769 6715a751 2018-03-16 stsp #ifndef PROFILE
770 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
773 f42b1b34 2018-03-12 stsp err(1, "pledge");
776 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
777 79109fed 2018-03-27 stsp switch (ch) {
779 79109fed 2018-03-27 stsp show_patch = 1;
782 d142fc45 2018-04-01 stsp start_commit = optarg;
785 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
787 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
788 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
791 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
792 64a96a6d 2018-04-01 stsp if (errstr != NULL)
793 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
796 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
799 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
800 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
801 04ca23f4 2018-07-16 stsp err(1, "-r option");
805 79109fed 2018-03-27 stsp /* NOTREACHED */
809 79109fed 2018-03-27 stsp argc -= optind;
810 79109fed 2018-03-27 stsp argv += optind;
812 04ca23f4 2018-07-16 stsp if (argc == 0)
813 04ca23f4 2018-07-16 stsp path = strdup("");
814 04ca23f4 2018-07-16 stsp else if (argc == 1)
815 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
817 6f3d1eb0 2018-03-12 stsp usage_log();
818 04ca23f4 2018-07-16 stsp if (path == NULL)
819 04ca23f4 2018-07-16 stsp return got_error_from_errno();
821 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
822 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
823 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
826 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
827 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
828 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
829 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
834 6098196c 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
838 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
839 d7d4f210 2018-03-12 stsp if (error != NULL)
842 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
843 3235492e 2018-04-01 stsp struct got_reference *head_ref;
844 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
845 3235492e 2018-04-01 stsp if (error != NULL)
846 3235492e 2018-04-01 stsp return error;
847 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
848 3235492e 2018-04-01 stsp got_ref_close(head_ref);
849 3235492e 2018-04-01 stsp if (error != NULL)
850 3235492e 2018-04-01 stsp return error;
851 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
853 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
854 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
855 3235492e 2018-04-01 stsp if (error == NULL) {
856 1caad61b 2019-02-01 stsp int obj_type;
857 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
858 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
859 d1f2edc9 2018-06-13 stsp if (error != NULL)
861 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
862 1caad61b 2019-02-01 stsp if (error != NULL)
864 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
865 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
866 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
867 1caad61b 2019-02-01 stsp if (error != NULL)
869 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
870 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
871 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
872 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
876 1caad61b 2019-02-01 stsp id = got_object_id_dup(
877 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
878 1caad61b 2019-02-01 stsp if (id == NULL)
879 1caad61b 2019-02-01 stsp error = got_error_from_errno();
880 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
883 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
884 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
887 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
888 d1f2edc9 2018-06-13 stsp if (error != NULL)
891 15a94983 2018-12-23 stsp if (commit == NULL) {
892 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
893 d1f2edc9 2018-06-13 stsp start_commit);
894 d1f2edc9 2018-06-13 stsp if (error != NULL)
895 d1f2edc9 2018-06-13 stsp return error;
898 d7d4f210 2018-03-12 stsp if (error != NULL)
901 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
902 04ca23f4 2018-07-16 stsp if (error != NULL)
904 04ca23f4 2018-07-16 stsp if (in_repo_path) {
905 04ca23f4 2018-07-16 stsp free(path);
906 04ca23f4 2018-07-16 stsp path = in_repo_path;
909 199a4027 2019-02-02 stsp SIMPLEQ_INIT(&refs);
910 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
914 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
915 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
917 04ca23f4 2018-07-16 stsp free(path);
918 04ca23f4 2018-07-16 stsp free(repo_path);
921 ad242220 2018-09-08 stsp if (repo) {
922 ad242220 2018-09-08 stsp const struct got_error *repo_error;
923 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
924 ad242220 2018-09-08 stsp if (error == NULL)
925 ad242220 2018-09-08 stsp error = repo_error;
927 8bf5b3c9 2018-03-17 stsp return error;
930 4ed7e80c 2018-05-20 stsp __dead static void
931 3f8b7d6a 2018-04-01 stsp usage_diff(void)
933 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
934 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
938 4ed7e80c 2018-05-20 stsp static const struct got_error *
939 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
941 b00d56cd 2018-04-01 stsp const struct got_error *error;
942 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
943 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
944 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
945 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
946 15a94983 2018-12-23 stsp int type1, type2;
947 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
948 c0cc5c62 2018-10-18 stsp const char *errstr;
950 b00d56cd 2018-04-01 stsp #ifndef PROFILE
951 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
952 25eccc22 2019-01-04 stsp NULL) == -1)
953 b00d56cd 2018-04-01 stsp err(1, "pledge");
956 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
957 b00d56cd 2018-04-01 stsp switch (ch) {
959 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
960 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
961 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
965 b00d56cd 2018-04-01 stsp /* NOTREACHED */
969 b00d56cd 2018-04-01 stsp argc -= optind;
970 b00d56cd 2018-04-01 stsp argv += optind;
972 b00d56cd 2018-04-01 stsp if (argc == 0) {
973 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
974 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
975 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
976 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
977 e1e3f570 2018-04-01 stsp return got_error_from_errno();
978 15a94983 2018-12-23 stsp id_str1 = argv[0];
979 15a94983 2018-12-23 stsp id_str2 = argv[1];
980 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
981 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
982 76089277 2018-04-01 stsp if (repo_path == NULL)
983 76089277 2018-04-01 stsp return got_error_from_errno();
984 15a94983 2018-12-23 stsp id_str1 = argv[1];
985 15a94983 2018-12-23 stsp id_str2 = argv[2];
987 b00d56cd 2018-04-01 stsp usage_diff();
989 25eccc22 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
993 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
994 76089277 2018-04-01 stsp free(repo_path);
995 b00d56cd 2018-04-01 stsp if (error != NULL)
998 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1000 b00d56cd 2018-04-01 stsp goto done;
1002 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1003 6402fb3c 2018-09-15 stsp if (error)
1004 b00d56cd 2018-04-01 stsp goto done;
1006 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1007 15a94983 2018-12-23 stsp if (error)
1008 15a94983 2018-12-23 stsp goto done;
1010 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1011 15a94983 2018-12-23 stsp if (error)
1012 15a94983 2018-12-23 stsp goto done;
1014 15a94983 2018-12-23 stsp if (type1 != type2) {
1015 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1016 b00d56cd 2018-04-01 stsp goto done;
1019 15a94983 2018-12-23 stsp switch (type1) {
1020 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1021 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1022 54156555 2018-12-24 stsp diff_context, repo, stdout);
1024 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1025 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1026 54156555 2018-12-24 stsp diff_context, repo, stdout);
1028 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1029 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1031 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1032 c0cc5c62 2018-10-18 stsp repo, stdout);
1035 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1039 15a94983 2018-12-23 stsp free(id1);
1040 15a94983 2018-12-23 stsp free(id2);
1041 ad242220 2018-09-08 stsp if (repo) {
1042 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1043 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1044 ad242220 2018-09-08 stsp if (error == NULL)
1045 ad242220 2018-09-08 stsp error = repo_error;
1047 b00d56cd 2018-04-01 stsp return error;
1050 404c43c4 2018-06-21 stsp __dead static void
1051 404c43c4 2018-06-21 stsp usage_blame(void)
1053 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1054 404c43c4 2018-06-21 stsp getprogname());
1058 404c43c4 2018-06-21 stsp static const struct got_error *
1059 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1061 404c43c4 2018-06-21 stsp const struct got_error *error;
1062 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1063 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1064 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1065 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1068 404c43c4 2018-06-21 stsp #ifndef PROFILE
1069 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1070 36e2fb66 2019-01-04 stsp NULL) == -1)
1071 404c43c4 2018-06-21 stsp err(1, "pledge");
1074 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1075 404c43c4 2018-06-21 stsp switch (ch) {
1077 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1080 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1081 66bea077 2018-08-02 stsp if (repo_path == NULL)
1082 66bea077 2018-08-02 stsp err(1, "-r option");
1086 404c43c4 2018-06-21 stsp /* NOTREACHED */
1090 404c43c4 2018-06-21 stsp argc -= optind;
1091 404c43c4 2018-06-21 stsp argv += optind;
1093 a39318fd 2018-08-02 stsp if (argc == 1)
1094 404c43c4 2018-06-21 stsp path = argv[0];
1096 404c43c4 2018-06-21 stsp usage_blame();
1098 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1099 66bea077 2018-08-02 stsp if (cwd == NULL) {
1100 66bea077 2018-08-02 stsp error = got_error_from_errno();
1101 66bea077 2018-08-02 stsp goto done;
1103 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1104 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
1105 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1106 66bea077 2018-08-02 stsp error = got_error_from_errno();
1107 66bea077 2018-08-02 stsp goto done;
1111 36e2fb66 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1112 36e2fb66 2019-01-04 stsp if (error)
1113 36e2fb66 2019-01-04 stsp goto done;
1115 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1116 404c43c4 2018-06-21 stsp if (error != NULL)
1117 404c43c4 2018-06-21 stsp goto done;
1119 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1120 66bea077 2018-08-02 stsp if (error != NULL)
1121 66bea077 2018-08-02 stsp goto done;
1123 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1124 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1125 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1126 404c43c4 2018-06-21 stsp if (error != NULL)
1127 66bea077 2018-08-02 stsp goto done;
1128 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1129 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1130 404c43c4 2018-06-21 stsp if (error != NULL)
1131 66bea077 2018-08-02 stsp goto done;
1133 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1134 15a94983 2018-12-23 stsp commit_id_str);
1135 404c43c4 2018-06-21 stsp if (error != NULL)
1136 66bea077 2018-08-02 stsp goto done;
1139 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1141 66bea077 2018-08-02 stsp free(in_repo_path);
1142 66bea077 2018-08-02 stsp free(repo_path);
1143 66bea077 2018-08-02 stsp free(cwd);
1144 404c43c4 2018-06-21 stsp free(commit_id);
1145 ad242220 2018-09-08 stsp if (repo) {
1146 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1147 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1148 ad242220 2018-09-08 stsp if (error == NULL)
1149 ad242220 2018-09-08 stsp error = repo_error;
1151 404c43c4 2018-06-21 stsp return error;
1154 5de5890b 2018-10-18 stsp __dead static void
1155 5de5890b 2018-10-18 stsp usage_tree(void)
1157 c1669e2e 2019-01-09 stsp fprintf(stderr,
1158 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1159 5de5890b 2018-10-18 stsp getprogname());
1163 c1669e2e 2019-01-09 stsp static void
1164 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1165 c1669e2e 2019-01-09 stsp const char *root_path)
1167 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1169 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1170 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1173 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1174 c1669e2e 2019-01-09 stsp is_root_path ? "" : "/",
1175 c1669e2e 2019-01-09 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1178 5de5890b 2018-10-18 stsp static const struct got_error *
1179 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1180 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1181 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1183 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1184 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1185 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1186 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1187 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1189 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1191 5de5890b 2018-10-18 stsp goto done;
1193 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1195 5de5890b 2018-10-18 stsp goto done;
1196 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1197 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1198 5de5890b 2018-10-18 stsp while (te) {
1199 5de5890b 2018-10-18 stsp char *id = NULL;
1201 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1204 5de5890b 2018-10-18 stsp if (show_ids) {
1205 5de5890b 2018-10-18 stsp char *id_str;
1206 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1208 5de5890b 2018-10-18 stsp goto done;
1209 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1210 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1211 5de5890b 2018-10-18 stsp free(id_str);
1212 5de5890b 2018-10-18 stsp goto done;
1214 5de5890b 2018-10-18 stsp free(id_str);
1216 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1219 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1220 c1669e2e 2019-01-09 stsp char *child_path;
1221 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1222 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1223 c1669e2e 2019-01-09 stsp te->name) == -1) {
1224 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1225 c1669e2e 2019-01-09 stsp goto done;
1227 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1228 c1669e2e 2019-01-09 stsp root_path, repo);
1229 c1669e2e 2019-01-09 stsp free(child_path);
1231 c1669e2e 2019-01-09 stsp goto done;
1234 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1238 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1239 5de5890b 2018-10-18 stsp free(tree_id);
1240 5de5890b 2018-10-18 stsp return err;
1243 5de5890b 2018-10-18 stsp static const struct got_error *
1244 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1246 5de5890b 2018-10-18 stsp const struct got_error *error;
1247 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1248 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1249 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1250 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1251 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1254 5de5890b 2018-10-18 stsp #ifndef PROFILE
1255 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1256 0f8d269b 2019-01-04 stsp NULL) == -1)
1257 5de5890b 2018-10-18 stsp err(1, "pledge");
1260 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1261 5de5890b 2018-10-18 stsp switch (ch) {
1263 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1266 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1267 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1268 5de5890b 2018-10-18 stsp err(1, "-r option");
1271 5de5890b 2018-10-18 stsp show_ids = 1;
1274 c1669e2e 2019-01-09 stsp recurse = 1;
1278 5de5890b 2018-10-18 stsp /* NOTREACHED */
1282 5de5890b 2018-10-18 stsp argc -= optind;
1283 5de5890b 2018-10-18 stsp argv += optind;
1285 5de5890b 2018-10-18 stsp if (argc == 1)
1286 5de5890b 2018-10-18 stsp path = argv[0];
1287 5de5890b 2018-10-18 stsp else if (argc > 1)
1288 5de5890b 2018-10-18 stsp usage_tree();
1290 5de5890b 2018-10-18 stsp path = "/";
1292 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
1293 5de5890b 2018-10-18 stsp if (cwd == NULL) {
1294 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1295 5de5890b 2018-10-18 stsp goto done;
1297 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1298 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
1299 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1300 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1301 5de5890b 2018-10-18 stsp goto done;
1305 0f8d269b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1306 0f8d269b 2019-01-04 stsp if (error)
1307 0f8d269b 2019-01-04 stsp goto done;
1309 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1310 5de5890b 2018-10-18 stsp if (error != NULL)
1311 5de5890b 2018-10-18 stsp goto done;
1313 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1314 5de5890b 2018-10-18 stsp if (error != NULL)
1315 5de5890b 2018-10-18 stsp goto done;
1317 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1318 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1319 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1320 5de5890b 2018-10-18 stsp if (error != NULL)
1321 5de5890b 2018-10-18 stsp goto done;
1322 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1323 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1324 5de5890b 2018-10-18 stsp if (error != NULL)
1325 5de5890b 2018-10-18 stsp goto done;
1327 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1328 15a94983 2018-12-23 stsp commit_id_str);
1329 5de5890b 2018-10-18 stsp if (error != NULL)
1330 5de5890b 2018-10-18 stsp goto done;
1333 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1334 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1336 5de5890b 2018-10-18 stsp free(in_repo_path);
1337 5de5890b 2018-10-18 stsp free(repo_path);
1338 5de5890b 2018-10-18 stsp free(cwd);
1339 5de5890b 2018-10-18 stsp free(commit_id);
1340 5de5890b 2018-10-18 stsp if (repo) {
1341 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1342 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1343 5de5890b 2018-10-18 stsp if (error == NULL)
1344 5de5890b 2018-10-18 stsp error = repo_error;
1346 5de5890b 2018-10-18 stsp return error;
1349 6bad629b 2019-02-04 stsp __dead static void
1350 6bad629b 2019-02-04 stsp usage_status(void)
1352 6bad629b 2019-02-04 stsp fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1356 6bad629b 2019-02-04 stsp static void
1357 6bad629b 2019-02-04 stsp print_status(void *arg, unsigned char status, const char *path)
1359 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1362 6bad629b 2019-02-04 stsp static const struct got_error *
1363 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1365 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1366 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1367 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1368 6bad629b 2019-02-04 stsp char *worktree_path = NULL;
1371 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1372 6bad629b 2019-02-04 stsp switch (ch) {
1375 6bad629b 2019-02-04 stsp /* NOTREACHED */
1379 6bad629b 2019-02-04 stsp argc -= optind;
1380 6bad629b 2019-02-04 stsp argv += optind;
1382 6bad629b 2019-02-04 stsp #ifndef PROFILE
1383 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1384 6bad629b 2019-02-04 stsp NULL) == -1)
1385 6bad629b 2019-02-04 stsp err(1, "pledge");
1387 6bad629b 2019-02-04 stsp if (argc == 0) {
1388 6bad629b 2019-02-04 stsp worktree_path = getcwd(NULL, 0);
1389 6bad629b 2019-02-04 stsp if (worktree_path == NULL) {
1390 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1391 6bad629b 2019-02-04 stsp goto done;
1393 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1394 6bad629b 2019-02-04 stsp worktree_path = realpath(argv[0], NULL);
1395 6bad629b 2019-02-04 stsp if (worktree_path == NULL) {
1396 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1397 6bad629b 2019-02-04 stsp goto done;
1400 6bad629b 2019-02-04 stsp usage_status();
1402 6bad629b 2019-02-04 stsp error = got_worktree_open(&worktree, worktree_path);
1403 6bad629b 2019-02-04 stsp if (error != NULL)
1404 6bad629b 2019-02-04 stsp goto done;
1406 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1407 6bad629b 2019-02-04 stsp if (error != NULL)
1408 6bad629b 2019-02-04 stsp goto done;
1410 6bad629b 2019-02-04 stsp error = apply_unveil(got_repo_get_path(repo), worktree_path);
1411 6bad629b 2019-02-04 stsp if (error)
1412 6bad629b 2019-02-04 stsp goto done;
1414 6bad629b 2019-02-04 stsp error = got_worktree_status(worktree, repo, print_status, NULL,
1415 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1417 6bad629b 2019-02-04 stsp free(worktree_path);
1418 6bad629b 2019-02-04 stsp return error;