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);
79 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
82 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
85 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
86 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
88 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
90 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
91 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
92 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
93 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
94 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
95 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
96 1b6b95a8 2018-03-12 stsp "show repository history" },
97 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
98 b00d56cd 2018-04-01 stsp "compare files and directories" },
99 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
100 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
101 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
102 5de5890b 2018-10-18 stsp " list files and directories in repository" },
103 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
104 1b6b95a8 2018-03-12 stsp "show modification status of files" },
105 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
106 d0eebce4 2019-03-11 stsp "manage references in repository" },
110 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
112 5c860e29 2018-03-12 stsp struct cmd *cmd;
113 5c860e29 2018-03-12 stsp unsigned int i;
115 1b6b95a8 2018-03-12 stsp int hflag = 0;
117 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
119 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
120 5c860e29 2018-03-12 stsp switch (ch) {
126 5c860e29 2018-03-12 stsp /* NOTREACHED */
130 5c860e29 2018-03-12 stsp argc -= optind;
131 5c860e29 2018-03-12 stsp argv += optind;
132 1e70621d 2018-03-27 stsp optind = 0;
134 5c860e29 2018-03-12 stsp if (argc <= 0)
137 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
138 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
140 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
141 d7d4f210 2018-03-12 stsp const struct got_error *error;
143 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
145 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
149 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
151 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
152 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
153 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
160 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
164 4ed7e80c 2018-05-20 stsp __dead static void
165 5c860e29 2018-03-12 stsp usage(void)
169 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
170 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
171 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
172 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
173 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
178 0266afb7 2019-01-04 stsp static const struct got_error *
179 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
180 d0eebce4 2019-03-11 stsp const char *worktree_path)
182 0266afb7 2019-01-04 stsp const struct got_error *error;
184 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
185 0266afb7 2019-01-04 stsp return got_error_from_errno();
187 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
188 0266afb7 2019-01-04 stsp return got_error_from_errno();
190 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
191 0266afb7 2019-01-04 stsp return got_error_from_errno();
193 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
194 0266afb7 2019-01-04 stsp if (error != NULL)
195 0266afb7 2019-01-04 stsp return error;
197 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
198 0266afb7 2019-01-04 stsp return got_error_from_errno();
200 0266afb7 2019-01-04 stsp return NULL;
203 4ed7e80c 2018-05-20 stsp __dead static void
204 c09a553d 2018-03-12 stsp usage_checkout(void)
206 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
207 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
211 92a684f4 2018-03-12 stsp static void
212 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
214 92a684f4 2018-03-12 stsp char *worktree_path = arg;
216 92a684f4 2018-03-12 stsp while (path[0] == '/')
219 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
222 99437157 2018-11-11 stsp static const struct got_error *
223 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
225 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
226 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
227 99437157 2018-11-11 stsp return NULL;
230 8069f636 2019-01-12 stsp static const struct got_error *
231 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
232 8069f636 2019-01-12 stsp struct got_repository *repo)
234 8069f636 2019-01-12 stsp const struct got_error *err;
235 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
236 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
237 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
239 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
240 8069f636 2019-01-12 stsp if (head_ref == NULL)
241 8069f636 2019-01-12 stsp return got_error_from_errno();
243 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
244 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
248 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
252 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
255 8069f636 2019-01-12 stsp while (1) {
256 8069f636 2019-01-12 stsp struct got_object_id *id;
258 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
261 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
263 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
264 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
267 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
269 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
275 8069f636 2019-01-12 stsp if (id == NULL)
277 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
281 8069f636 2019-01-12 stsp if (head_ref)
282 8069f636 2019-01-12 stsp got_ref_close(head_ref);
284 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
285 8069f636 2019-01-12 stsp return err;
289 4ed7e80c 2018-05-20 stsp static const struct got_error *
290 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
292 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
293 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
294 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
295 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
296 c09a553d 2018-03-12 stsp char *repo_path = NULL;
297 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
298 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
299 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
300 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
302 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
303 0bb8a95e 2018-03-12 stsp switch (ch) {
305 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
306 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
307 8069f636 2019-01-12 stsp return got_error_from_errno();
310 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
313 2deda0b9 2019-03-07 stsp usage_checkout();
314 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
318 0bb8a95e 2018-03-12 stsp argc -= optind;
319 0bb8a95e 2018-03-12 stsp argv += optind;
321 6715a751 2018-03-16 stsp #ifndef PROFILE
322 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
323 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
324 c09a553d 2018-03-12 stsp err(1, "pledge");
326 0bb8a95e 2018-03-12 stsp if (argc == 1) {
327 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
328 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
329 76089277 2018-04-01 stsp if (repo_path == NULL)
330 76089277 2018-04-01 stsp return got_error_from_errno();
331 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
332 76089277 2018-04-01 stsp if (cwd == NULL) {
333 76089277 2018-04-01 stsp error = got_error_from_errno();
336 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
337 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
339 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
340 76089277 2018-04-01 stsp if (base == NULL) {
341 76089277 2018-04-01 stsp error = got_error_from_errno();
344 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
345 c09a553d 2018-03-12 stsp if (dotgit)
346 c09a553d 2018-03-12 stsp *dotgit = '\0';
347 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
348 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
353 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
354 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
355 76089277 2018-04-01 stsp if (repo_path == NULL) {
356 76089277 2018-04-01 stsp error = got_error_from_errno();
359 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
360 76089277 2018-04-01 stsp if (worktree_path == NULL) {
361 76089277 2018-04-01 stsp error = got_error_from_errno();
365 c09a553d 2018-03-12 stsp usage_checkout();
367 0cd1c46a 2019-03-11 stsp error = apply_unveil(repo_path, 0, worktree_path);
371 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
372 c09a553d 2018-03-12 stsp if (error != NULL)
375 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
376 c09a553d 2018-03-12 stsp if (error != NULL)
379 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
380 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
383 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
384 c09a553d 2018-03-12 stsp if (error != NULL)
387 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
388 e5dc7198 2018-12-29 stsp path_prefix);
389 e5dc7198 2018-12-29 stsp if (error != NULL)
391 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
392 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
396 8069f636 2019-01-12 stsp if (commit_id_str) {
397 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
398 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
399 8069f636 2019-01-12 stsp commit_id_str);
400 8069f636 2019-01-12 stsp if (error != NULL)
402 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
403 8069f636 2019-01-12 stsp if (error != NULL) {
404 8069f636 2019-01-12 stsp free(commit_id);
407 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
408 8069f636 2019-01-12 stsp commit_id);
409 8069f636 2019-01-12 stsp free(commit_id);
414 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
415 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
416 c09a553d 2018-03-12 stsp if (error != NULL)
419 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
422 8069f636 2019-01-12 stsp free(commit_id_str);
423 76089277 2018-04-01 stsp free(repo_path);
424 507dc3bb 2018-12-29 stsp free(worktree_path);
425 507dc3bb 2018-12-29 stsp return error;
428 507dc3bb 2018-12-29 stsp __dead static void
429 507dc3bb 2018-12-29 stsp usage_update(void)
431 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
432 507dc3bb 2018-12-29 stsp getprogname());
436 507dc3bb 2018-12-29 stsp static void
437 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
439 784955db 2019-01-12 stsp int *did_something = arg;
441 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
444 1545c615 2019-02-10 stsp *did_something = 1;
445 507dc3bb 2018-12-29 stsp while (path[0] == '/')
447 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
450 be7061eb 2018-12-30 stsp static const struct got_error *
451 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
453 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
454 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
455 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
456 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
457 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
458 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
459 784955db 2019-01-12 stsp int ch, did_something = 0;
461 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
462 507dc3bb 2018-12-29 stsp switch (ch) {
464 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
465 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
466 9c4b8182 2019-01-02 stsp return got_error_from_errno();
469 2deda0b9 2019-03-07 stsp usage_update();
470 507dc3bb 2018-12-29 stsp /* NOTREACHED */
474 507dc3bb 2018-12-29 stsp argc -= optind;
475 507dc3bb 2018-12-29 stsp argv += optind;
477 507dc3bb 2018-12-29 stsp #ifndef PROFILE
478 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
479 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
480 507dc3bb 2018-12-29 stsp err(1, "pledge");
482 507dc3bb 2018-12-29 stsp if (argc == 0) {
483 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
484 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
485 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
488 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
489 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
490 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
491 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
495 507dc3bb 2018-12-29 stsp usage_update();
497 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
498 507dc3bb 2018-12-29 stsp if (error != NULL)
501 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
502 507dc3bb 2018-12-29 stsp if (error != NULL)
505 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
506 8c02d095 2019-02-05 stsp got_worktree_get_root_path(worktree));
510 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
511 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
512 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
513 507dc3bb 2018-12-29 stsp if (error != NULL)
515 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
516 9c4b8182 2019-01-02 stsp if (error != NULL)
518 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
519 507dc3bb 2018-12-29 stsp if (error != NULL)
522 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
523 507dc3bb 2018-12-29 stsp commit_id_str);
524 507dc3bb 2018-12-29 stsp if (error != NULL)
528 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
529 be7061eb 2018-12-30 stsp if (error != NULL)
532 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
533 507dc3bb 2018-12-29 stsp commit_id) != 0) {
534 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
535 507dc3bb 2018-12-29 stsp commit_id);
540 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
541 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
542 507dc3bb 2018-12-29 stsp if (error != NULL)
545 784955db 2019-01-12 stsp if (did_something)
546 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
548 784955db 2019-01-12 stsp printf("Already up-to-date\n");
550 c09a553d 2018-03-12 stsp free(worktree_path);
551 507dc3bb 2018-12-29 stsp free(commit_id);
552 9c4b8182 2019-01-02 stsp free(commit_id_str);
553 c09a553d 2018-03-12 stsp return error;
556 f42b1b34 2018-03-12 stsp static const struct got_error *
557 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
558 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
560 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
561 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
562 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
563 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
565 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
566 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
568 79109fed 2018-03-27 stsp return err;
570 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
571 79f35eb3 2018-06-11 stsp if (qid != NULL) {
572 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
574 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
576 79109fed 2018-03-27 stsp return err;
578 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
579 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
580 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
582 0f2b3dca 2018-12-22 stsp return err;
584 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
586 79109fed 2018-03-27 stsp return err;
589 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
593 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
594 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
597 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
598 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
599 0f2b3dca 2018-12-22 stsp free(id_str1);
600 0f2b3dca 2018-12-22 stsp free(id_str2);
601 79109fed 2018-03-27 stsp return err;
604 4bb494d5 2018-06-16 stsp static char *
605 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
607 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
608 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
614 79109fed 2018-03-27 stsp static const struct got_error *
615 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
616 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
617 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
619 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
620 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
621 6c281f94 2018-06-11 stsp char datebuf[26];
622 45d799e2 2018-12-23 stsp time_t committer_time;
623 45d799e2 2018-12-23 stsp const char *author, *committer;
624 199a4027 2019-02-02 stsp char *refs_str = NULL;
625 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
627 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
629 199a4027 2019-02-02 stsp const char *name;
630 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
632 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
633 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
635 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
637 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
639 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
641 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
643 199a4027 2019-02-02 stsp s = refs_str;
644 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
645 199a4027 2019-02-02 stsp name) == -1) {
646 199a4027 2019-02-02 stsp err = got_error_from_errno();
652 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
654 8bf5b3c9 2018-03-17 stsp return err;
656 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
657 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
658 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
659 832c249c 2018-06-10 stsp free(id_str);
660 d3d493d7 2019-02-21 stsp id_str = NULL;
661 d3d493d7 2019-02-21 stsp free(refs_str);
662 d3d493d7 2019-02-21 stsp refs_str = NULL;
663 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
664 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
665 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
666 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
667 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
668 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
669 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
670 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
671 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
672 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
673 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
675 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
676 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
677 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
679 a0603db2 2018-06-10 stsp return err;
680 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
681 a0603db2 2018-06-10 stsp free(id_str);
685 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
686 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
687 832c249c 2018-06-10 stsp return got_error_from_errno();
689 621015ac 2018-07-23 stsp logmsg = logmsg0;
691 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
693 832c249c 2018-06-10 stsp printf(" %s\n", line);
694 832c249c 2018-06-10 stsp } while (line);
695 621015ac 2018-07-23 stsp free(logmsg0);
697 971751ac 2018-03-27 stsp if (show_patch) {
698 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
699 971751ac 2018-03-27 stsp if (err == 0)
700 971751ac 2018-03-27 stsp printf("\n");
703 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
704 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
705 07862c20 2018-09-15 stsp return err;
708 f42b1b34 2018-03-12 stsp static const struct got_error *
709 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
710 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
711 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
713 f42b1b34 2018-03-12 stsp const struct got_error *err;
714 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
716 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
717 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
719 f42b1b34 2018-03-12 stsp return err;
720 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
723 31cedeaf 2018-09-15 stsp while (1) {
724 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
725 372ccdbb 2018-06-10 stsp struct got_object_id *id;
727 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
730 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
732 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
733 9ba79e04 2018-06-11 stsp err = NULL;
736 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
738 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
744 b43fbaa0 2018-06-11 stsp if (id == NULL)
747 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
750 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
752 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
753 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
757 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
758 f42b1b34 2018-03-12 stsp return err;
761 4ed7e80c 2018-05-20 stsp __dead static void
762 6f3d1eb0 2018-03-12 stsp usage_log(void)
764 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
765 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
769 4ed7e80c 2018-05-20 stsp static const struct got_error *
770 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
772 f42b1b34 2018-03-12 stsp const struct got_error *error;
773 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
774 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
775 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
776 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
777 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
778 d142fc45 2018-04-01 stsp char *start_commit = NULL;
779 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
780 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
781 64a96a6d 2018-04-01 stsp const char *errstr;
782 199a4027 2019-02-02 stsp struct got_reflist_head refs;
784 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
786 6715a751 2018-03-16 stsp #ifndef PROFILE
787 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
790 f42b1b34 2018-03-12 stsp err(1, "pledge");
793 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
794 79109fed 2018-03-27 stsp switch (ch) {
796 79109fed 2018-03-27 stsp show_patch = 1;
799 d142fc45 2018-04-01 stsp start_commit = optarg;
802 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
804 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
805 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
808 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
809 64a96a6d 2018-04-01 stsp if (errstr != NULL)
810 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
813 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
816 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
817 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
818 04ca23f4 2018-07-16 stsp err(1, "-r option");
821 2deda0b9 2019-03-07 stsp usage_log();
822 79109fed 2018-03-27 stsp /* NOTREACHED */
826 79109fed 2018-03-27 stsp argc -= optind;
827 79109fed 2018-03-27 stsp argv += optind;
829 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
830 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
831 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
835 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
836 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
838 cffc0aa4 2019-02-05 stsp error = NULL;
840 e7301579 2019-03-18 stsp if (argc == 0) {
841 cbd1af7a 2019-03-18 stsp path = strdup("");
842 e7301579 2019-03-18 stsp if (path == NULL) {
843 e7301579 2019-03-18 stsp error = got_error_from_errno();
846 e7301579 2019-03-18 stsp } else if (argc == 1) {
847 e7301579 2019-03-18 stsp if (worktree) {
848 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
853 e7301579 2019-03-18 stsp path = strdup(argv[0]);
854 e7301579 2019-03-18 stsp if (path == NULL) {
855 e7301579 2019-03-18 stsp error = got_error_from_errno();
860 cbd1af7a 2019-03-18 stsp usage_log();
862 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
863 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
864 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
865 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
869 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1,
870 efcae1c6 2019-03-07 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
874 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
875 d7d4f210 2018-03-12 stsp if (error != NULL)
878 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
879 3235492e 2018-04-01 stsp struct got_reference *head_ref;
880 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
881 3235492e 2018-04-01 stsp if (error != NULL)
882 3235492e 2018-04-01 stsp return error;
883 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
884 3235492e 2018-04-01 stsp got_ref_close(head_ref);
885 3235492e 2018-04-01 stsp if (error != NULL)
886 3235492e 2018-04-01 stsp return error;
887 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
889 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
890 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
891 3235492e 2018-04-01 stsp if (error == NULL) {
892 1caad61b 2019-02-01 stsp int obj_type;
893 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
894 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
895 d1f2edc9 2018-06-13 stsp if (error != NULL)
897 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
898 1caad61b 2019-02-01 stsp if (error != NULL)
900 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
901 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
902 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
903 1caad61b 2019-02-01 stsp if (error != NULL)
905 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
906 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
907 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
908 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
912 1caad61b 2019-02-01 stsp id = got_object_id_dup(
913 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
914 1caad61b 2019-02-01 stsp if (id == NULL)
915 1caad61b 2019-02-01 stsp error = got_error_from_errno();
916 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
919 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
920 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
923 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
924 d1f2edc9 2018-06-13 stsp if (error != NULL)
927 15a94983 2018-12-23 stsp if (commit == NULL) {
928 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
929 d1f2edc9 2018-06-13 stsp start_commit);
930 d1f2edc9 2018-06-13 stsp if (error != NULL)
931 d1f2edc9 2018-06-13 stsp return error;
934 d7d4f210 2018-03-12 stsp if (error != NULL)
937 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
938 04ca23f4 2018-07-16 stsp if (error != NULL)
940 04ca23f4 2018-07-16 stsp if (in_repo_path) {
941 04ca23f4 2018-07-16 stsp free(path);
942 04ca23f4 2018-07-16 stsp path = in_repo_path;
945 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
949 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
950 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
952 04ca23f4 2018-07-16 stsp free(path);
953 04ca23f4 2018-07-16 stsp free(repo_path);
956 cffc0aa4 2019-02-05 stsp if (worktree)
957 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
958 ad242220 2018-09-08 stsp if (repo) {
959 ad242220 2018-09-08 stsp const struct got_error *repo_error;
960 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
961 ad242220 2018-09-08 stsp if (error == NULL)
962 ad242220 2018-09-08 stsp error = repo_error;
964 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
965 8bf5b3c9 2018-03-17 stsp return error;
968 4ed7e80c 2018-05-20 stsp __dead static void
969 3f8b7d6a 2018-04-01 stsp usage_diff(void)
971 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
972 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
976 b72f483a 2019-02-05 stsp struct print_diff_arg {
977 b72f483a 2019-02-05 stsp struct got_repository *repo;
978 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
979 b72f483a 2019-02-05 stsp int diff_context;
980 3fc0c068 2019-02-10 stsp const char *id_str;
981 3fc0c068 2019-02-10 stsp int header_shown;
984 4ed7e80c 2018-05-20 stsp static const struct got_error *
985 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
986 b72f483a 2019-02-05 stsp struct got_object_id *id)
988 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
989 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
990 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
991 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
992 b72f483a 2019-02-05 stsp char *abspath = NULL;
993 b72f483a 2019-02-05 stsp struct stat sb;
995 276262e8 2019-02-08 stsp if (status != GOT_STATUS_MODIFY)
996 b72f483a 2019-02-05 stsp return NULL;
998 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
999 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1000 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1001 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1004 b72f483a 2019-02-05 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1006 b72f483a 2019-02-05 stsp goto done;
1008 b72f483a 2019-02-05 stsp if (asprintf(&abspath, "%s/%s",
1009 b72f483a 2019-02-05 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1010 b72f483a 2019-02-05 stsp err = got_error_from_errno();
1011 b72f483a 2019-02-05 stsp goto done;
1014 b72f483a 2019-02-05 stsp f2 = fopen(abspath, "r");
1015 b72f483a 2019-02-05 stsp if (f2 == NULL) {
1016 b72f483a 2019-02-05 stsp err = got_error_from_errno();
1017 b72f483a 2019-02-05 stsp goto done;
1019 b72f483a 2019-02-05 stsp if (lstat(abspath, &sb) == -1) {
1020 b72f483a 2019-02-05 stsp err = got_error_from_errno();
1021 b72f483a 2019-02-05 stsp goto done;
1024 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1027 b72f483a 2019-02-05 stsp if (blob1)
1028 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1029 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1030 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1031 b72f483a 2019-02-05 stsp free(abspath);
1032 927df6b7 2019-02-10 stsp return err;
1035 927df6b7 2019-02-10 stsp static const struct got_error *
1036 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1038 b00d56cd 2018-04-01 stsp const struct got_error *error;
1039 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1040 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1041 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1042 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1043 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1044 15a94983 2018-12-23 stsp int type1, type2;
1045 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1046 c0cc5c62 2018-10-18 stsp const char *errstr;
1047 927df6b7 2019-02-10 stsp char *path = NULL;
1049 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1050 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1051 25eccc22 2019-01-04 stsp NULL) == -1)
1052 b00d56cd 2018-04-01 stsp err(1, "pledge");
1055 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1056 b00d56cd 2018-04-01 stsp switch (ch) {
1058 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1059 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1060 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1063 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1064 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1065 b72f483a 2019-02-05 stsp err(1, "-r option");
1068 2deda0b9 2019-03-07 stsp usage_diff();
1069 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1073 b00d56cd 2018-04-01 stsp argc -= optind;
1074 b00d56cd 2018-04-01 stsp argv += optind;
1076 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1077 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1078 b72f483a 2019-02-05 stsp error = got_error_from_errno();
1079 b72f483a 2019-02-05 stsp goto done;
1081 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1082 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1083 927df6b7 2019-02-10 stsp goto done;
1084 927df6b7 2019-02-10 stsp if (argc <= 1) {
1085 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1086 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1087 927df6b7 2019-02-10 stsp goto done;
1089 b72f483a 2019-02-05 stsp if (repo_path)
1091 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1092 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1093 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1094 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1095 927df6b7 2019-02-10 stsp goto done;
1097 927df6b7 2019-02-10 stsp if (argc == 1) {
1098 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1100 927df6b7 2019-02-10 stsp if (error)
1101 927df6b7 2019-02-10 stsp goto done;
1103 927df6b7 2019-02-10 stsp path = strdup("");
1104 927df6b7 2019-02-10 stsp if (path == NULL) {
1105 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1106 927df6b7 2019-02-10 stsp goto done;
1109 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1110 15a94983 2018-12-23 stsp id_str1 = argv[0];
1111 15a94983 2018-12-23 stsp id_str2 = argv[1];
1113 b00d56cd 2018-04-01 stsp usage_diff();
1115 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1116 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1117 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1118 b72f483a 2019-02-05 stsp return got_error_from_errno();
1121 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1,
1122 b72f483a 2019-02-05 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1123 25eccc22 2019-01-04 stsp if (error)
1124 25eccc22 2019-01-04 stsp goto done;
1126 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1127 76089277 2018-04-01 stsp free(repo_path);
1128 b00d56cd 2018-04-01 stsp if (error != NULL)
1129 b00d56cd 2018-04-01 stsp goto done;
1131 b72f483a 2019-02-05 stsp if (worktree) {
1132 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1133 b72f483a 2019-02-05 stsp char *id_str;
1134 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1135 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1136 b72f483a 2019-02-05 stsp if (error)
1137 b72f483a 2019-02-05 stsp goto done;
1138 b72f483a 2019-02-05 stsp arg.repo = repo;
1139 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1140 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1141 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1142 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1144 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1145 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1146 b72f483a 2019-02-05 stsp free(id_str);
1147 b72f483a 2019-02-05 stsp goto done;
1150 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1151 6402fb3c 2018-09-15 stsp if (error)
1152 b00d56cd 2018-04-01 stsp goto done;
1154 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1155 6402fb3c 2018-09-15 stsp if (error)
1156 b00d56cd 2018-04-01 stsp goto done;
1158 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1159 15a94983 2018-12-23 stsp if (error)
1160 15a94983 2018-12-23 stsp goto done;
1162 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1163 15a94983 2018-12-23 stsp if (error)
1164 15a94983 2018-12-23 stsp goto done;
1166 15a94983 2018-12-23 stsp if (type1 != type2) {
1167 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1168 b00d56cd 2018-04-01 stsp goto done;
1171 15a94983 2018-12-23 stsp switch (type1) {
1172 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1173 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1174 54156555 2018-12-24 stsp diff_context, repo, stdout);
1176 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1177 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1178 54156555 2018-12-24 stsp diff_context, repo, stdout);
1180 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1181 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1183 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1184 c0cc5c62 2018-10-18 stsp repo, stdout);
1187 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1191 15a94983 2018-12-23 stsp free(id1);
1192 15a94983 2018-12-23 stsp free(id2);
1193 927df6b7 2019-02-10 stsp free(path);
1194 b72f483a 2019-02-05 stsp if (worktree)
1195 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1196 ad242220 2018-09-08 stsp if (repo) {
1197 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1198 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1199 ad242220 2018-09-08 stsp if (error == NULL)
1200 ad242220 2018-09-08 stsp error = repo_error;
1202 b00d56cd 2018-04-01 stsp return error;
1205 404c43c4 2018-06-21 stsp __dead static void
1206 404c43c4 2018-06-21 stsp usage_blame(void)
1208 9270e621 2019-02-05 stsp fprintf(stderr,
1209 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1210 404c43c4 2018-06-21 stsp getprogname());
1214 404c43c4 2018-06-21 stsp static const struct got_error *
1215 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1217 404c43c4 2018-06-21 stsp const struct got_error *error;
1218 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1219 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1220 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1221 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1222 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1225 404c43c4 2018-06-21 stsp #ifndef PROFILE
1226 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1227 36e2fb66 2019-01-04 stsp NULL) == -1)
1228 404c43c4 2018-06-21 stsp err(1, "pledge");
1231 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1232 404c43c4 2018-06-21 stsp switch (ch) {
1234 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1237 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1238 66bea077 2018-08-02 stsp if (repo_path == NULL)
1239 66bea077 2018-08-02 stsp err(1, "-r option");
1242 2deda0b9 2019-03-07 stsp usage_blame();
1243 404c43c4 2018-06-21 stsp /* NOTREACHED */
1247 404c43c4 2018-06-21 stsp argc -= optind;
1248 404c43c4 2018-06-21 stsp argv += optind;
1250 a39318fd 2018-08-02 stsp if (argc == 1)
1251 404c43c4 2018-06-21 stsp path = argv[0];
1253 404c43c4 2018-06-21 stsp usage_blame();
1255 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1256 66bea077 2018-08-02 stsp if (cwd == NULL) {
1257 66bea077 2018-08-02 stsp error = got_error_from_errno();
1258 66bea077 2018-08-02 stsp goto done;
1260 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1261 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1262 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1263 66bea077 2018-08-02 stsp goto done;
1265 0c06baac 2019-02-05 stsp error = NULL;
1266 0c06baac 2019-02-05 stsp if (worktree) {
1267 0c06baac 2019-02-05 stsp repo_path =
1268 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1269 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1270 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1271 0c06baac 2019-02-05 stsp if (error)
1272 0c06baac 2019-02-05 stsp goto done;
1274 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1275 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1276 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1277 0c06baac 2019-02-05 stsp goto done;
1282 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1, NULL);
1283 36e2fb66 2019-01-04 stsp if (error)
1284 36e2fb66 2019-01-04 stsp goto done;
1286 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1287 404c43c4 2018-06-21 stsp if (error != NULL)
1288 404c43c4 2018-06-21 stsp goto done;
1290 0c06baac 2019-02-05 stsp if (worktree) {
1291 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1292 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1293 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1294 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1295 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1296 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1297 6efaaa2d 2019-02-05 stsp path) == -1) {
1298 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1299 0c06baac 2019-02-05 stsp goto done;
1301 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1304 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1306 0c06baac 2019-02-05 stsp if (error)
1307 66bea077 2018-08-02 stsp goto done;
1309 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1310 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1311 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1312 404c43c4 2018-06-21 stsp if (error != NULL)
1313 66bea077 2018-08-02 stsp goto done;
1314 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1315 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1316 404c43c4 2018-06-21 stsp if (error != NULL)
1317 66bea077 2018-08-02 stsp goto done;
1319 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1320 15a94983 2018-12-23 stsp commit_id_str);
1321 404c43c4 2018-06-21 stsp if (error != NULL)
1322 66bea077 2018-08-02 stsp goto done;
1325 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1327 66bea077 2018-08-02 stsp free(in_repo_path);
1328 66bea077 2018-08-02 stsp free(repo_path);
1329 66bea077 2018-08-02 stsp free(cwd);
1330 404c43c4 2018-06-21 stsp free(commit_id);
1331 0c06baac 2019-02-05 stsp if (worktree)
1332 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1333 ad242220 2018-09-08 stsp if (repo) {
1334 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1335 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1336 ad242220 2018-09-08 stsp if (error == NULL)
1337 ad242220 2018-09-08 stsp error = repo_error;
1339 404c43c4 2018-06-21 stsp return error;
1342 5de5890b 2018-10-18 stsp __dead static void
1343 5de5890b 2018-10-18 stsp usage_tree(void)
1345 c1669e2e 2019-01-09 stsp fprintf(stderr,
1346 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1347 5de5890b 2018-10-18 stsp getprogname());
1351 c1669e2e 2019-01-09 stsp static void
1352 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1353 c1669e2e 2019-01-09 stsp const char *root_path)
1355 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1357 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1358 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1361 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1362 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1363 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1366 5de5890b 2018-10-18 stsp static const struct got_error *
1367 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1368 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1369 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1371 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1372 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1373 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1374 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1375 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1377 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1379 5de5890b 2018-10-18 stsp goto done;
1381 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1383 5de5890b 2018-10-18 stsp goto done;
1384 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1385 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1386 5de5890b 2018-10-18 stsp while (te) {
1387 5de5890b 2018-10-18 stsp char *id = NULL;
1389 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1392 5de5890b 2018-10-18 stsp if (show_ids) {
1393 5de5890b 2018-10-18 stsp char *id_str;
1394 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1396 5de5890b 2018-10-18 stsp goto done;
1397 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1398 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1399 5de5890b 2018-10-18 stsp free(id_str);
1400 5de5890b 2018-10-18 stsp goto done;
1402 5de5890b 2018-10-18 stsp free(id_str);
1404 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1407 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1408 c1669e2e 2019-01-09 stsp char *child_path;
1409 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1410 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1411 c1669e2e 2019-01-09 stsp te->name) == -1) {
1412 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1413 c1669e2e 2019-01-09 stsp goto done;
1415 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1416 c1669e2e 2019-01-09 stsp root_path, repo);
1417 c1669e2e 2019-01-09 stsp free(child_path);
1419 c1669e2e 2019-01-09 stsp goto done;
1422 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1426 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1427 5de5890b 2018-10-18 stsp free(tree_id);
1428 5de5890b 2018-10-18 stsp return err;
1431 5de5890b 2018-10-18 stsp static const struct got_error *
1432 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1434 5de5890b 2018-10-18 stsp const struct got_error *error;
1435 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1436 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1437 7a2c19d6 2019-02-05 stsp const char *path;
1438 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1439 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1440 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1441 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1444 5de5890b 2018-10-18 stsp #ifndef PROFILE
1445 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1446 0f8d269b 2019-01-04 stsp NULL) == -1)
1447 5de5890b 2018-10-18 stsp err(1, "pledge");
1450 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1451 5de5890b 2018-10-18 stsp switch (ch) {
1453 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1456 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1457 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1458 5de5890b 2018-10-18 stsp err(1, "-r option");
1461 5de5890b 2018-10-18 stsp show_ids = 1;
1464 c1669e2e 2019-01-09 stsp recurse = 1;
1467 2deda0b9 2019-03-07 stsp usage_tree();
1468 5de5890b 2018-10-18 stsp /* NOTREACHED */
1472 5de5890b 2018-10-18 stsp argc -= optind;
1473 5de5890b 2018-10-18 stsp argv += optind;
1475 5de5890b 2018-10-18 stsp if (argc == 1)
1476 5de5890b 2018-10-18 stsp path = argv[0];
1477 5de5890b 2018-10-18 stsp else if (argc > 1)
1478 5de5890b 2018-10-18 stsp usage_tree();
1480 7a2c19d6 2019-02-05 stsp path = NULL;
1482 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1483 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1484 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1485 9bf7a39b 2019-02-05 stsp goto done;
1487 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1488 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1489 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1490 7a2c19d6 2019-02-05 stsp goto done;
1492 7a2c19d6 2019-02-05 stsp error = NULL;
1493 7a2c19d6 2019-02-05 stsp if (worktree) {
1494 7a2c19d6 2019-02-05 stsp repo_path =
1495 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1496 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1497 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1498 7a2c19d6 2019-02-05 stsp if (error)
1499 7a2c19d6 2019-02-05 stsp goto done;
1501 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1502 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1503 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1504 7a2c19d6 2019-02-05 stsp goto done;
1509 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1, NULL);
1510 0f8d269b 2019-01-04 stsp if (error)
1511 0f8d269b 2019-01-04 stsp goto done;
1513 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1514 5de5890b 2018-10-18 stsp if (error != NULL)
1515 5de5890b 2018-10-18 stsp goto done;
1517 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1518 9bf7a39b 2019-02-05 stsp if (worktree) {
1519 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1520 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1521 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1522 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1523 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1524 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1525 9bf7a39b 2019-02-05 stsp goto done;
1527 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1529 9bf7a39b 2019-02-05 stsp if (error)
1530 9bf7a39b 2019-02-05 stsp goto done;
1532 9bf7a39b 2019-02-05 stsp path = "/";
1534 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1535 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1536 9bf7a39b 2019-02-05 stsp if (error != NULL)
1537 9bf7a39b 2019-02-05 stsp goto done;
1540 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1541 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1542 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1543 5de5890b 2018-10-18 stsp if (error != NULL)
1544 5de5890b 2018-10-18 stsp goto done;
1545 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1546 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1547 5de5890b 2018-10-18 stsp if (error != NULL)
1548 5de5890b 2018-10-18 stsp goto done;
1550 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1551 15a94983 2018-12-23 stsp commit_id_str);
1552 5de5890b 2018-10-18 stsp if (error != NULL)
1553 5de5890b 2018-10-18 stsp goto done;
1556 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1557 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1559 5de5890b 2018-10-18 stsp free(in_repo_path);
1560 5de5890b 2018-10-18 stsp free(repo_path);
1561 5de5890b 2018-10-18 stsp free(cwd);
1562 5de5890b 2018-10-18 stsp free(commit_id);
1563 7a2c19d6 2019-02-05 stsp if (worktree)
1564 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1565 5de5890b 2018-10-18 stsp if (repo) {
1566 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1567 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1568 5de5890b 2018-10-18 stsp if (error == NULL)
1569 5de5890b 2018-10-18 stsp error = repo_error;
1571 5de5890b 2018-10-18 stsp return error;
1574 6bad629b 2019-02-04 stsp __dead static void
1575 6bad629b 2019-02-04 stsp usage_status(void)
1577 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1581 b72f483a 2019-02-05 stsp static const struct got_error *
1582 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1583 b72f483a 2019-02-05 stsp struct got_object_id *id)
1585 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1586 b72f483a 2019-02-05 stsp return NULL;
1589 6bad629b 2019-02-04 stsp static const struct got_error *
1590 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1592 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1593 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1594 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1595 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1598 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1599 6bad629b 2019-02-04 stsp switch (ch) {
1601 2deda0b9 2019-03-07 stsp usage_status();
1602 6bad629b 2019-02-04 stsp /* NOTREACHED */
1606 6bad629b 2019-02-04 stsp argc -= optind;
1607 6bad629b 2019-02-04 stsp argv += optind;
1609 6bad629b 2019-02-04 stsp #ifndef PROFILE
1610 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1611 6bad629b 2019-02-04 stsp NULL) == -1)
1612 6bad629b 2019-02-04 stsp err(1, "pledge");
1614 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1615 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1616 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1617 927df6b7 2019-02-10 stsp goto done;
1620 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1621 927df6b7 2019-02-10 stsp if (error != NULL)
1622 927df6b7 2019-02-10 stsp goto done;
1624 6bad629b 2019-02-04 stsp if (argc == 0) {
1625 927df6b7 2019-02-10 stsp path = strdup("");
1626 927df6b7 2019-02-10 stsp if (path == NULL) {
1627 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1628 6bad629b 2019-02-04 stsp goto done;
1630 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1631 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1632 927df6b7 2019-02-10 stsp if (error)
1633 6bad629b 2019-02-04 stsp goto done;
1635 6bad629b 2019-02-04 stsp usage_status();
1637 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1638 6bad629b 2019-02-04 stsp if (error != NULL)
1639 6bad629b 2019-02-04 stsp goto done;
1641 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1642 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(worktree));
1643 6bad629b 2019-02-04 stsp if (error)
1644 6bad629b 2019-02-04 stsp goto done;
1646 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1647 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1649 927df6b7 2019-02-10 stsp free(cwd);
1650 927df6b7 2019-02-10 stsp free(path);
1651 d0eebce4 2019-03-11 stsp return error;
1654 d0eebce4 2019-03-11 stsp __dead static void
1655 d0eebce4 2019-03-11 stsp usage_ref(void)
1657 d0eebce4 2019-03-11 stsp fprintf(stderr,
1658 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1659 d0eebce4 2019-03-11 stsp getprogname());
1663 d0eebce4 2019-03-11 stsp static const struct got_error *
1664 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1666 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1667 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1668 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1670 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1671 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1673 d0eebce4 2019-03-11 stsp return err;
1675 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1676 d0eebce4 2019-03-11 stsp char *refstr;
1677 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1678 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1679 d0eebce4 2019-03-11 stsp return got_error_from_errno();
1680 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1681 d0eebce4 2019-03-11 stsp free(refstr);
1684 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1685 d0eebce4 2019-03-11 stsp return NULL;
1688 d0eebce4 2019-03-11 stsp static const struct got_error *
1689 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1691 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1692 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1694 d0eebce4 2019-03-11 stsp err = got_ref_open(&ref, repo, refname);
1696 d0eebce4 2019-03-11 stsp return err;
1698 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1699 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1700 d0eebce4 2019-03-11 stsp return err;
1703 d0eebce4 2019-03-11 stsp static const struct got_error *
1704 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1706 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1707 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1708 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1710 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1712 d0eebce4 2019-03-11 stsp return err;
1714 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1716 d0eebce4 2019-03-11 stsp goto done;
1718 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1721 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1723 d0eebce4 2019-03-11 stsp return err;
1726 d0eebce4 2019-03-11 stsp static const struct got_error *
1727 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1729 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1730 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1731 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1732 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1733 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1734 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1736 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1737 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1738 d0eebce4 2019-03-11 stsp switch (ch) {
1740 d0eebce4 2019-03-11 stsp delref = optarg;
1743 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1744 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1745 d0eebce4 2019-03-11 stsp err(1, "-r option");
1748 d0eebce4 2019-03-11 stsp do_list = 1;
1751 d0eebce4 2019-03-11 stsp usage_ref();
1752 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1756 d0eebce4 2019-03-11 stsp if (do_list && delref)
1757 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1759 d0eebce4 2019-03-11 stsp argc -= optind;
1760 d0eebce4 2019-03-11 stsp argv += optind;
1762 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1763 d0eebce4 2019-03-11 stsp if (argc > 0)
1764 d0eebce4 2019-03-11 stsp usage_ref();
1765 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1766 d0eebce4 2019-03-11 stsp usage_ref();
1768 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1769 e0b57350 2019-03-12 stsp if (do_list) {
1770 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1771 e0b57350 2019-03-12 stsp NULL) == -1)
1772 e0b57350 2019-03-12 stsp err(1, "pledge");
1774 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1775 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1776 e0b57350 2019-03-12 stsp err(1, "pledge");
1779 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1780 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1781 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1782 d0eebce4 2019-03-11 stsp goto done;
1785 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1786 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1787 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1788 d0eebce4 2019-03-11 stsp goto done;
1790 d0eebce4 2019-03-11 stsp error = NULL;
1791 d0eebce4 2019-03-11 stsp if (worktree) {
1792 d0eebce4 2019-03-11 stsp repo_path =
1793 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1794 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1795 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1796 d0eebce4 2019-03-11 stsp if (error)
1797 d0eebce4 2019-03-11 stsp goto done;
1799 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1800 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1801 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1802 d0eebce4 2019-03-11 stsp goto done;
1807 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, do_list,
1808 d0eebce4 2019-03-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1809 d0eebce4 2019-03-11 stsp if (error)
1810 d0eebce4 2019-03-11 stsp goto done;
1812 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1813 d0eebce4 2019-03-11 stsp if (error != NULL)
1814 d0eebce4 2019-03-11 stsp goto done;
1816 d0eebce4 2019-03-11 stsp if (do_list)
1817 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1818 d0eebce4 2019-03-11 stsp else if (delref)
1819 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1821 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1824 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1825 d0eebce4 2019-03-11 stsp if (worktree)
1826 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1827 d0eebce4 2019-03-11 stsp free(cwd);
1828 d0eebce4 2019-03-11 stsp free(repo_path);
1829 6bad629b 2019-02-04 stsp return error;