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"
43 a0937847 2019-05-11 stsp #include "got_path.h"
45 5c860e29 2018-03-12 stsp #ifndef nitems
46 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
50 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
53 99437157 2018-11-11 stsp catch_sigint(int signo)
55 99437157 2018-11-11 stsp sigint_received = 1;
59 99437157 2018-11-11 stsp catch_sigpipe(int signo)
61 99437157 2018-11-11 stsp sigpipe_received = 1;
65 5c860e29 2018-03-12 stsp struct cmd {
66 5c860e29 2018-03-12 stsp const char *cmd_name;
67 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
68 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
69 46a0db7d 2018-03-12 stsp const char *cmd_descr;
72 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
74 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
77 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
78 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
79 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
80 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
81 d00136be 2019-03-26 stsp __dead static void usage_add(void);
82 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
83 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
84 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
87 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
90 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
91 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
93 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
94 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
95 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
96 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
97 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
100 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
101 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
102 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
103 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
104 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
105 1b6b95a8 2018-03-12 stsp "show repository history" },
106 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
107 b00d56cd 2018-04-01 stsp "compare files and directories" },
108 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
109 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
110 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
111 a67e2392 2019-03-26 stsp "list files and directories in repository" },
112 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
113 1b6b95a8 2018-03-12 stsp "show modification status of files" },
114 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
115 d0eebce4 2019-03-11 stsp "manage references in repository" },
116 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
117 d00136be 2019-03-26 stsp "add a new file to version control" },
118 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
119 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
120 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
121 a129376b 2019-03-28 stsp "revert uncommitted changes" },
122 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
123 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
127 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
129 5c860e29 2018-03-12 stsp struct cmd *cmd;
130 5c860e29 2018-03-12 stsp unsigned int i;
132 1b6b95a8 2018-03-12 stsp int hflag = 0;
134 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
136 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
137 5c860e29 2018-03-12 stsp switch (ch) {
143 5c860e29 2018-03-12 stsp /* NOTREACHED */
147 5c860e29 2018-03-12 stsp argc -= optind;
148 5c860e29 2018-03-12 stsp argv += optind;
149 1e70621d 2018-03-27 stsp optind = 0;
151 5c860e29 2018-03-12 stsp if (argc <= 0)
154 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
155 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
157 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
158 d7d4f210 2018-03-12 stsp const struct got_error *error;
160 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
162 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
168 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
169 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
170 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
177 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
181 4ed7e80c 2018-05-20 stsp __dead static void
182 5c860e29 2018-03-12 stsp usage(void)
186 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
187 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
188 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
189 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
190 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
195 0266afb7 2019-01-04 stsp static const struct got_error *
196 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
197 a0937847 2019-05-11 stsp const char *worktree_path, int create_worktree)
199 0266afb7 2019-01-04 stsp const struct got_error *error;
201 a0937847 2019-05-11 stsp if (create_worktree) {
202 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
203 a0937847 2019-05-11 stsp error = got_path_mkdir(worktree_path);
204 a0937847 2019-05-11 stsp if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
205 a0937847 2019-05-11 stsp return error;
208 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
209 0266afb7 2019-01-04 stsp return got_error_from_errno();
211 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 0266afb7 2019-01-04 stsp return got_error_from_errno();
214 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
215 0266afb7 2019-01-04 stsp return got_error_from_errno();
217 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
218 0266afb7 2019-01-04 stsp if (error != NULL)
219 0266afb7 2019-01-04 stsp return error;
221 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
222 0266afb7 2019-01-04 stsp return got_error_from_errno();
224 0266afb7 2019-01-04 stsp return NULL;
227 4ed7e80c 2018-05-20 stsp __dead static void
228 c09a553d 2018-03-12 stsp usage_checkout(void)
230 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
231 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
235 92a684f4 2018-03-12 stsp static void
236 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
238 92a684f4 2018-03-12 stsp char *worktree_path = arg;
240 92a684f4 2018-03-12 stsp while (path[0] == '/')
243 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
246 99437157 2018-11-11 stsp static const struct got_error *
247 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
249 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
250 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
251 99437157 2018-11-11 stsp return NULL;
254 8069f636 2019-01-12 stsp static const struct got_error *
255 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
256 8069f636 2019-01-12 stsp struct got_repository *repo)
258 8069f636 2019-01-12 stsp const struct got_error *err;
259 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
260 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
261 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
263 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo,
264 36a38700 2019-05-10 stsp got_worktree_get_head_ref_name(worktree));
266 36a38700 2019-05-10 stsp return err;
268 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
269 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
273 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
277 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
280 8069f636 2019-01-12 stsp while (1) {
281 8069f636 2019-01-12 stsp struct got_object_id *id;
283 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
286 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
288 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
289 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
292 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
294 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
300 8069f636 2019-01-12 stsp if (id == NULL)
302 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
306 8069f636 2019-01-12 stsp if (head_ref)
307 8069f636 2019-01-12 stsp got_ref_close(head_ref);
309 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
310 8069f636 2019-01-12 stsp return err;
314 4ed7e80c 2018-05-20 stsp static const struct got_error *
315 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
317 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
318 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
319 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
320 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
321 c09a553d 2018-03-12 stsp char *repo_path = NULL;
322 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
323 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
324 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
325 72151b04 2019-05-11 stsp int ch, same_path_prefix;
327 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
328 0bb8a95e 2018-03-12 stsp switch (ch) {
330 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
331 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
332 8069f636 2019-01-12 stsp return got_error_from_errno();
335 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
338 2deda0b9 2019-03-07 stsp usage_checkout();
339 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
343 0bb8a95e 2018-03-12 stsp argc -= optind;
344 0bb8a95e 2018-03-12 stsp argv += optind;
346 6715a751 2018-03-16 stsp #ifndef PROFILE
347 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
348 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
349 c09a553d 2018-03-12 stsp err(1, "pledge");
351 0bb8a95e 2018-03-12 stsp if (argc == 1) {
352 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
353 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
354 76089277 2018-04-01 stsp if (repo_path == NULL)
355 76089277 2018-04-01 stsp return got_error_from_errno();
356 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
357 76089277 2018-04-01 stsp if (cwd == NULL) {
358 76089277 2018-04-01 stsp error = got_error_from_errno();
361 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
362 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
364 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
365 76089277 2018-04-01 stsp if (base == NULL) {
366 76089277 2018-04-01 stsp error = got_error_from_errno();
369 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
370 c09a553d 2018-03-12 stsp if (dotgit)
371 c09a553d 2018-03-12 stsp *dotgit = '\0';
372 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
373 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
378 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
379 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
380 76089277 2018-04-01 stsp if (repo_path == NULL) {
381 76089277 2018-04-01 stsp error = got_error_from_errno();
384 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
385 76089277 2018-04-01 stsp if (worktree_path == NULL) {
386 76089277 2018-04-01 stsp error = got_error_from_errno();
390 c09a553d 2018-03-12 stsp usage_checkout();
392 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
394 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
395 c09a553d 2018-03-12 stsp if (error != NULL)
398 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
402 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
403 c09a553d 2018-03-12 stsp if (error != NULL)
406 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
407 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
410 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
411 c09a553d 2018-03-12 stsp if (error != NULL)
414 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
415 e5dc7198 2018-12-29 stsp path_prefix);
416 e5dc7198 2018-12-29 stsp if (error != NULL)
418 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
419 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
423 8069f636 2019-01-12 stsp if (commit_id_str) {
424 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
425 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
426 8069f636 2019-01-12 stsp commit_id_str);
427 8069f636 2019-01-12 stsp if (error != NULL)
429 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
430 8069f636 2019-01-12 stsp if (error != NULL) {
431 8069f636 2019-01-12 stsp free(commit_id);
434 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
435 8069f636 2019-01-12 stsp commit_id);
436 8069f636 2019-01-12 stsp free(commit_id);
441 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
442 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
443 c09a553d 2018-03-12 stsp if (error != NULL)
446 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
449 8069f636 2019-01-12 stsp free(commit_id_str);
450 76089277 2018-04-01 stsp free(repo_path);
451 507dc3bb 2018-12-29 stsp free(worktree_path);
452 507dc3bb 2018-12-29 stsp return error;
455 507dc3bb 2018-12-29 stsp __dead static void
456 507dc3bb 2018-12-29 stsp usage_update(void)
458 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
459 507dc3bb 2018-12-29 stsp getprogname());
463 507dc3bb 2018-12-29 stsp static void
464 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
466 784955db 2019-01-12 stsp int *did_something = arg;
468 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
471 1545c615 2019-02-10 stsp *did_something = 1;
472 507dc3bb 2018-12-29 stsp while (path[0] == '/')
474 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
477 be7061eb 2018-12-30 stsp static const struct got_error *
478 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
480 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
481 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
482 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
483 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
484 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
485 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
486 784955db 2019-01-12 stsp int ch, did_something = 0;
488 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
489 507dc3bb 2018-12-29 stsp switch (ch) {
491 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
492 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
493 9c4b8182 2019-01-02 stsp return got_error_from_errno();
496 2deda0b9 2019-03-07 stsp usage_update();
497 507dc3bb 2018-12-29 stsp /* NOTREACHED */
501 507dc3bb 2018-12-29 stsp argc -= optind;
502 507dc3bb 2018-12-29 stsp argv += optind;
504 507dc3bb 2018-12-29 stsp #ifndef PROFILE
505 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
506 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
507 507dc3bb 2018-12-29 stsp err(1, "pledge");
509 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
510 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
511 c4cdcb68 2019-04-03 stsp error = got_error_from_errno();
514 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
518 507dc3bb 2018-12-29 stsp if (argc == 0) {
519 c4cdcb68 2019-04-03 stsp path = strdup("");
520 c4cdcb68 2019-04-03 stsp if (path == NULL) {
521 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
524 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
525 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
529 507dc3bb 2018-12-29 stsp usage_update();
531 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
532 507dc3bb 2018-12-29 stsp if (error != NULL)
535 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
536 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
540 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
541 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
542 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
543 507dc3bb 2018-12-29 stsp if (error != NULL)
545 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
546 9c4b8182 2019-01-02 stsp if (error != NULL)
548 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
549 507dc3bb 2018-12-29 stsp if (error != NULL)
552 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
553 507dc3bb 2018-12-29 stsp commit_id_str);
554 507dc3bb 2018-12-29 stsp if (error != NULL)
558 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
559 be7061eb 2018-12-30 stsp if (error != NULL)
562 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
563 507dc3bb 2018-12-29 stsp commit_id) != 0) {
564 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
565 507dc3bb 2018-12-29 stsp commit_id);
570 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
571 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
572 507dc3bb 2018-12-29 stsp if (error != NULL)
575 784955db 2019-01-12 stsp if (did_something)
576 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
578 784955db 2019-01-12 stsp printf("Already up-to-date\n");
580 c09a553d 2018-03-12 stsp free(worktree_path);
581 c4cdcb68 2019-04-03 stsp free(path);
582 507dc3bb 2018-12-29 stsp free(commit_id);
583 9c4b8182 2019-01-02 stsp free(commit_id_str);
584 c09a553d 2018-03-12 stsp return error;
587 f42b1b34 2018-03-12 stsp static const struct got_error *
588 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
589 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
591 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
592 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
593 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
594 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
596 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
597 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
599 79109fed 2018-03-27 stsp return err;
601 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
602 79f35eb3 2018-06-11 stsp if (qid != NULL) {
603 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
605 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
607 79109fed 2018-03-27 stsp return err;
609 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
610 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
611 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
613 0f2b3dca 2018-12-22 stsp return err;
615 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
617 79109fed 2018-03-27 stsp return err;
620 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
624 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
625 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
628 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
629 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
630 0f2b3dca 2018-12-22 stsp free(id_str1);
631 0f2b3dca 2018-12-22 stsp free(id_str2);
632 79109fed 2018-03-27 stsp return err;
635 4bb494d5 2018-06-16 stsp static char *
636 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
638 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
639 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
645 79109fed 2018-03-27 stsp static const struct got_error *
646 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
647 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
648 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
650 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
651 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
652 6c281f94 2018-06-11 stsp char datebuf[26];
653 45d799e2 2018-12-23 stsp time_t committer_time;
654 45d799e2 2018-12-23 stsp const char *author, *committer;
655 199a4027 2019-02-02 stsp char *refs_str = NULL;
656 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
658 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
660 199a4027 2019-02-02 stsp const char *name;
661 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
663 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
664 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
666 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
668 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
670 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
672 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
674 199a4027 2019-02-02 stsp s = refs_str;
675 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
676 199a4027 2019-02-02 stsp name) == -1) {
677 199a4027 2019-02-02 stsp err = got_error_from_errno();
683 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
685 8bf5b3c9 2018-03-17 stsp return err;
687 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
688 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
689 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
690 832c249c 2018-06-10 stsp free(id_str);
691 d3d493d7 2019-02-21 stsp id_str = NULL;
692 d3d493d7 2019-02-21 stsp free(refs_str);
693 d3d493d7 2019-02-21 stsp refs_str = NULL;
694 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
695 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
696 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
697 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
698 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
699 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
700 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
701 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
702 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
703 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
704 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
706 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
707 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
708 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
710 a0603db2 2018-06-10 stsp return err;
711 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
712 a0603db2 2018-06-10 stsp free(id_str);
716 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
717 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
718 832c249c 2018-06-10 stsp return got_error_from_errno();
720 621015ac 2018-07-23 stsp logmsg = logmsg0;
722 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
724 832c249c 2018-06-10 stsp printf(" %s\n", line);
725 832c249c 2018-06-10 stsp } while (line);
726 621015ac 2018-07-23 stsp free(logmsg0);
728 971751ac 2018-03-27 stsp if (show_patch) {
729 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
730 971751ac 2018-03-27 stsp if (err == 0)
731 971751ac 2018-03-27 stsp printf("\n");
734 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
735 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
736 07862c20 2018-09-15 stsp return err;
739 f42b1b34 2018-03-12 stsp static const struct got_error *
740 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
741 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
742 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
744 f42b1b34 2018-03-12 stsp const struct got_error *err;
745 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
747 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
748 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
750 f42b1b34 2018-03-12 stsp return err;
751 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
754 31cedeaf 2018-09-15 stsp while (1) {
755 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
756 372ccdbb 2018-06-10 stsp struct got_object_id *id;
758 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
761 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
763 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
764 9ba79e04 2018-06-11 stsp err = NULL;
767 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
769 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
775 b43fbaa0 2018-06-11 stsp if (id == NULL)
778 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
781 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
783 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
784 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
788 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
789 f42b1b34 2018-03-12 stsp return err;
792 4ed7e80c 2018-05-20 stsp __dead static void
793 6f3d1eb0 2018-03-12 stsp usage_log(void)
795 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
796 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
800 4ed7e80c 2018-05-20 stsp static const struct got_error *
801 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
803 f42b1b34 2018-03-12 stsp const struct got_error *error;
804 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
805 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
806 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
807 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
808 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
809 d142fc45 2018-04-01 stsp char *start_commit = NULL;
810 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
811 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
812 64a96a6d 2018-04-01 stsp const char *errstr;
813 199a4027 2019-02-02 stsp struct got_reflist_head refs;
815 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
817 6715a751 2018-03-16 stsp #ifndef PROFILE
818 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
821 f42b1b34 2018-03-12 stsp err(1, "pledge");
824 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
825 79109fed 2018-03-27 stsp switch (ch) {
827 79109fed 2018-03-27 stsp show_patch = 1;
830 d142fc45 2018-04-01 stsp start_commit = optarg;
833 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
835 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
836 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
839 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
840 64a96a6d 2018-04-01 stsp if (errstr != NULL)
841 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
844 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
847 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
848 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
849 04ca23f4 2018-07-16 stsp err(1, "-r option");
852 2deda0b9 2019-03-07 stsp usage_log();
853 79109fed 2018-03-27 stsp /* NOTREACHED */
857 79109fed 2018-03-27 stsp argc -= optind;
858 79109fed 2018-03-27 stsp argv += optind;
860 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
861 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
862 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
866 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
867 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
869 cffc0aa4 2019-02-05 stsp error = NULL;
871 e7301579 2019-03-18 stsp if (argc == 0) {
872 cbd1af7a 2019-03-18 stsp path = strdup("");
873 e7301579 2019-03-18 stsp if (path == NULL) {
874 e7301579 2019-03-18 stsp error = got_error_from_errno();
877 e7301579 2019-03-18 stsp } else if (argc == 1) {
878 e7301579 2019-03-18 stsp if (worktree) {
879 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
884 e7301579 2019-03-18 stsp path = strdup(argv[0]);
885 e7301579 2019-03-18 stsp if (path == NULL) {
886 e7301579 2019-03-18 stsp error = got_error_from_errno();
891 cbd1af7a 2019-03-18 stsp usage_log();
893 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
894 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
895 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
896 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
900 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
901 d7d4f210 2018-03-12 stsp if (error != NULL)
904 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
905 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
909 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
910 3235492e 2018-04-01 stsp struct got_reference *head_ref;
911 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
912 3235492e 2018-04-01 stsp if (error != NULL)
913 3235492e 2018-04-01 stsp return error;
914 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
915 3235492e 2018-04-01 stsp got_ref_close(head_ref);
916 3235492e 2018-04-01 stsp if (error != NULL)
917 3235492e 2018-04-01 stsp return error;
918 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
920 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
921 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
922 3235492e 2018-04-01 stsp if (error == NULL) {
923 1caad61b 2019-02-01 stsp int obj_type;
924 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
925 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
926 d1f2edc9 2018-06-13 stsp if (error != NULL)
928 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
929 1caad61b 2019-02-01 stsp if (error != NULL)
931 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
932 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
933 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
934 1caad61b 2019-02-01 stsp if (error != NULL)
936 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
937 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
938 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
939 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
943 1caad61b 2019-02-01 stsp id = got_object_id_dup(
944 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
945 1caad61b 2019-02-01 stsp if (id == NULL)
946 1caad61b 2019-02-01 stsp error = got_error_from_errno();
947 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
950 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
951 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
954 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
955 d1f2edc9 2018-06-13 stsp if (error != NULL)
958 15a94983 2018-12-23 stsp if (commit == NULL) {
959 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
960 d1f2edc9 2018-06-13 stsp start_commit);
961 d1f2edc9 2018-06-13 stsp if (error != NULL)
962 d1f2edc9 2018-06-13 stsp return error;
965 d7d4f210 2018-03-12 stsp if (error != NULL)
968 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
969 04ca23f4 2018-07-16 stsp if (error != NULL)
971 04ca23f4 2018-07-16 stsp if (in_repo_path) {
972 04ca23f4 2018-07-16 stsp free(path);
973 04ca23f4 2018-07-16 stsp path = in_repo_path;
976 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
980 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
981 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
983 04ca23f4 2018-07-16 stsp free(path);
984 04ca23f4 2018-07-16 stsp free(repo_path);
987 cffc0aa4 2019-02-05 stsp if (worktree)
988 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
989 ad242220 2018-09-08 stsp if (repo) {
990 ad242220 2018-09-08 stsp const struct got_error *repo_error;
991 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
992 ad242220 2018-09-08 stsp if (error == NULL)
993 ad242220 2018-09-08 stsp error = repo_error;
995 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
996 8bf5b3c9 2018-03-17 stsp return error;
999 4ed7e80c 2018-05-20 stsp __dead static void
1000 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1002 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1003 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1007 b72f483a 2019-02-05 stsp struct print_diff_arg {
1008 b72f483a 2019-02-05 stsp struct got_repository *repo;
1009 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1010 b72f483a 2019-02-05 stsp int diff_context;
1011 3fc0c068 2019-02-10 stsp const char *id_str;
1012 3fc0c068 2019-02-10 stsp int header_shown;
1015 4ed7e80c 2018-05-20 stsp static const struct got_error *
1016 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1017 b72f483a 2019-02-05 stsp struct got_object_id *id)
1019 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1020 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1021 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1022 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1023 b72f483a 2019-02-05 stsp char *abspath = NULL;
1024 b72f483a 2019-02-05 stsp struct stat sb;
1026 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1027 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1028 b72f483a 2019-02-05 stsp return NULL;
1030 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1031 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1032 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1033 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1036 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1037 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1039 d00136be 2019-03-26 stsp goto done;
1043 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1044 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1045 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1046 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1047 2ec1f75b 2019-03-26 stsp goto done;
1050 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1051 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1052 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1053 2ec1f75b 2019-03-26 stsp goto done;
1055 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1056 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1057 2ec1f75b 2019-03-26 stsp goto done;
1060 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1062 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1065 b72f483a 2019-02-05 stsp if (blob1)
1066 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1067 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1068 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1069 b72f483a 2019-02-05 stsp free(abspath);
1070 927df6b7 2019-02-10 stsp return err;
1073 927df6b7 2019-02-10 stsp static const struct got_error *
1074 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1076 b00d56cd 2018-04-01 stsp const struct got_error *error;
1077 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1078 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1079 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1080 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1081 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1082 15a94983 2018-12-23 stsp int type1, type2;
1083 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1084 c0cc5c62 2018-10-18 stsp const char *errstr;
1085 927df6b7 2019-02-10 stsp char *path = NULL;
1087 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1088 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1089 25eccc22 2019-01-04 stsp NULL) == -1)
1090 b00d56cd 2018-04-01 stsp err(1, "pledge");
1093 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1094 b00d56cd 2018-04-01 stsp switch (ch) {
1096 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1097 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1098 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1101 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1102 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1103 b72f483a 2019-02-05 stsp err(1, "-r option");
1106 2deda0b9 2019-03-07 stsp usage_diff();
1107 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1111 b00d56cd 2018-04-01 stsp argc -= optind;
1112 b00d56cd 2018-04-01 stsp argv += optind;
1114 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1115 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1116 b72f483a 2019-02-05 stsp error = got_error_from_errno();
1117 b72f483a 2019-02-05 stsp goto done;
1119 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1120 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1121 927df6b7 2019-02-10 stsp goto done;
1122 927df6b7 2019-02-10 stsp if (argc <= 1) {
1123 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1124 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1125 927df6b7 2019-02-10 stsp goto done;
1127 b72f483a 2019-02-05 stsp if (repo_path)
1129 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1130 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1131 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1132 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1133 927df6b7 2019-02-10 stsp goto done;
1135 927df6b7 2019-02-10 stsp if (argc == 1) {
1136 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1138 927df6b7 2019-02-10 stsp if (error)
1139 927df6b7 2019-02-10 stsp goto done;
1141 927df6b7 2019-02-10 stsp path = strdup("");
1142 927df6b7 2019-02-10 stsp if (path == NULL) {
1143 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1144 927df6b7 2019-02-10 stsp goto done;
1147 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1148 15a94983 2018-12-23 stsp id_str1 = argv[0];
1149 15a94983 2018-12-23 stsp id_str2 = argv[1];
1151 b00d56cd 2018-04-01 stsp usage_diff();
1153 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1154 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1155 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1156 b72f483a 2019-02-05 stsp return got_error_from_errno();
1159 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1160 76089277 2018-04-01 stsp free(repo_path);
1161 b00d56cd 2018-04-01 stsp if (error != NULL)
1162 b00d56cd 2018-04-01 stsp goto done;
1164 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1165 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1166 c02c541e 2019-03-29 stsp if (error)
1167 c02c541e 2019-03-29 stsp goto done;
1169 b72f483a 2019-02-05 stsp if (worktree) {
1170 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1171 b72f483a 2019-02-05 stsp char *id_str;
1172 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1173 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1174 b72f483a 2019-02-05 stsp if (error)
1175 b72f483a 2019-02-05 stsp goto done;
1176 b72f483a 2019-02-05 stsp arg.repo = repo;
1177 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1178 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1179 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1180 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1182 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1183 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1184 b72f483a 2019-02-05 stsp free(id_str);
1185 b72f483a 2019-02-05 stsp goto done;
1188 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1189 6402fb3c 2018-09-15 stsp if (error)
1190 b00d56cd 2018-04-01 stsp goto done;
1192 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1193 6402fb3c 2018-09-15 stsp if (error)
1194 b00d56cd 2018-04-01 stsp goto done;
1196 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1197 15a94983 2018-12-23 stsp if (error)
1198 15a94983 2018-12-23 stsp goto done;
1200 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1201 15a94983 2018-12-23 stsp if (error)
1202 15a94983 2018-12-23 stsp goto done;
1204 15a94983 2018-12-23 stsp if (type1 != type2) {
1205 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1206 b00d56cd 2018-04-01 stsp goto done;
1209 15a94983 2018-12-23 stsp switch (type1) {
1210 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1211 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1212 54156555 2018-12-24 stsp diff_context, repo, stdout);
1214 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1215 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1216 54156555 2018-12-24 stsp diff_context, repo, stdout);
1218 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1219 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1221 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1222 c0cc5c62 2018-10-18 stsp repo, stdout);
1225 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1229 15a94983 2018-12-23 stsp free(id1);
1230 15a94983 2018-12-23 stsp free(id2);
1231 927df6b7 2019-02-10 stsp free(path);
1232 b72f483a 2019-02-05 stsp if (worktree)
1233 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1234 ad242220 2018-09-08 stsp if (repo) {
1235 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1236 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1237 ad242220 2018-09-08 stsp if (error == NULL)
1238 ad242220 2018-09-08 stsp error = repo_error;
1240 b00d56cd 2018-04-01 stsp return error;
1243 404c43c4 2018-06-21 stsp __dead static void
1244 404c43c4 2018-06-21 stsp usage_blame(void)
1246 9270e621 2019-02-05 stsp fprintf(stderr,
1247 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1248 404c43c4 2018-06-21 stsp getprogname());
1252 404c43c4 2018-06-21 stsp static const struct got_error *
1253 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1255 404c43c4 2018-06-21 stsp const struct got_error *error;
1256 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1257 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1258 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1259 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1260 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1263 404c43c4 2018-06-21 stsp #ifndef PROFILE
1264 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1265 36e2fb66 2019-01-04 stsp NULL) == -1)
1266 404c43c4 2018-06-21 stsp err(1, "pledge");
1269 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1270 404c43c4 2018-06-21 stsp switch (ch) {
1272 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1275 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1276 66bea077 2018-08-02 stsp if (repo_path == NULL)
1277 66bea077 2018-08-02 stsp err(1, "-r option");
1280 2deda0b9 2019-03-07 stsp usage_blame();
1281 404c43c4 2018-06-21 stsp /* NOTREACHED */
1285 404c43c4 2018-06-21 stsp argc -= optind;
1286 404c43c4 2018-06-21 stsp argv += optind;
1288 a39318fd 2018-08-02 stsp if (argc == 1)
1289 404c43c4 2018-06-21 stsp path = argv[0];
1291 404c43c4 2018-06-21 stsp usage_blame();
1293 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1294 66bea077 2018-08-02 stsp if (cwd == NULL) {
1295 66bea077 2018-08-02 stsp error = got_error_from_errno();
1296 66bea077 2018-08-02 stsp goto done;
1298 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1299 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1300 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1301 66bea077 2018-08-02 stsp goto done;
1303 0c06baac 2019-02-05 stsp error = NULL;
1304 0c06baac 2019-02-05 stsp if (worktree) {
1305 0c06baac 2019-02-05 stsp repo_path =
1306 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1307 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1308 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1309 0c06baac 2019-02-05 stsp if (error)
1310 0c06baac 2019-02-05 stsp goto done;
1312 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1313 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1314 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1315 0c06baac 2019-02-05 stsp goto done;
1320 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1321 c02c541e 2019-03-29 stsp if (error != NULL)
1322 36e2fb66 2019-01-04 stsp goto done;
1324 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1325 c02c541e 2019-03-29 stsp if (error)
1326 404c43c4 2018-06-21 stsp goto done;
1328 0c06baac 2019-02-05 stsp if (worktree) {
1329 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1330 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1331 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1332 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1333 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1334 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1335 6efaaa2d 2019-02-05 stsp path) == -1) {
1336 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1337 0c06baac 2019-02-05 stsp goto done;
1339 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1342 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1344 0c06baac 2019-02-05 stsp if (error)
1345 66bea077 2018-08-02 stsp goto done;
1347 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1348 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1349 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1350 404c43c4 2018-06-21 stsp if (error != NULL)
1351 66bea077 2018-08-02 stsp goto done;
1352 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1353 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1354 404c43c4 2018-06-21 stsp if (error != NULL)
1355 66bea077 2018-08-02 stsp goto done;
1357 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1358 15a94983 2018-12-23 stsp commit_id_str);
1359 404c43c4 2018-06-21 stsp if (error != NULL)
1360 66bea077 2018-08-02 stsp goto done;
1363 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1365 66bea077 2018-08-02 stsp free(in_repo_path);
1366 66bea077 2018-08-02 stsp free(repo_path);
1367 66bea077 2018-08-02 stsp free(cwd);
1368 404c43c4 2018-06-21 stsp free(commit_id);
1369 0c06baac 2019-02-05 stsp if (worktree)
1370 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1371 ad242220 2018-09-08 stsp if (repo) {
1372 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1373 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1374 ad242220 2018-09-08 stsp if (error == NULL)
1375 ad242220 2018-09-08 stsp error = repo_error;
1377 404c43c4 2018-06-21 stsp return error;
1380 5de5890b 2018-10-18 stsp __dead static void
1381 5de5890b 2018-10-18 stsp usage_tree(void)
1383 c1669e2e 2019-01-09 stsp fprintf(stderr,
1384 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1385 5de5890b 2018-10-18 stsp getprogname());
1389 c1669e2e 2019-01-09 stsp static void
1390 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1391 c1669e2e 2019-01-09 stsp const char *root_path)
1393 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1395 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1396 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1399 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1400 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1401 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1404 5de5890b 2018-10-18 stsp static const struct got_error *
1405 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1406 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1407 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1409 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1410 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1411 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1412 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1413 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1415 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1417 5de5890b 2018-10-18 stsp goto done;
1419 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1421 5de5890b 2018-10-18 stsp goto done;
1422 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1423 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1424 5de5890b 2018-10-18 stsp while (te) {
1425 5de5890b 2018-10-18 stsp char *id = NULL;
1427 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1430 5de5890b 2018-10-18 stsp if (show_ids) {
1431 5de5890b 2018-10-18 stsp char *id_str;
1432 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1434 5de5890b 2018-10-18 stsp goto done;
1435 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1436 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1437 5de5890b 2018-10-18 stsp free(id_str);
1438 5de5890b 2018-10-18 stsp goto done;
1440 5de5890b 2018-10-18 stsp free(id_str);
1442 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1445 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1446 c1669e2e 2019-01-09 stsp char *child_path;
1447 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1448 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1449 c1669e2e 2019-01-09 stsp te->name) == -1) {
1450 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1451 c1669e2e 2019-01-09 stsp goto done;
1453 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1454 c1669e2e 2019-01-09 stsp root_path, repo);
1455 c1669e2e 2019-01-09 stsp free(child_path);
1457 c1669e2e 2019-01-09 stsp goto done;
1460 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1464 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1465 5de5890b 2018-10-18 stsp free(tree_id);
1466 5de5890b 2018-10-18 stsp return err;
1469 5de5890b 2018-10-18 stsp static const struct got_error *
1470 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1472 5de5890b 2018-10-18 stsp const struct got_error *error;
1473 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1474 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1475 7a2c19d6 2019-02-05 stsp const char *path;
1476 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1477 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1478 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1479 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1482 5de5890b 2018-10-18 stsp #ifndef PROFILE
1483 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1484 0f8d269b 2019-01-04 stsp NULL) == -1)
1485 5de5890b 2018-10-18 stsp err(1, "pledge");
1488 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1489 5de5890b 2018-10-18 stsp switch (ch) {
1491 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1494 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1495 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1496 5de5890b 2018-10-18 stsp err(1, "-r option");
1499 5de5890b 2018-10-18 stsp show_ids = 1;
1502 c1669e2e 2019-01-09 stsp recurse = 1;
1505 2deda0b9 2019-03-07 stsp usage_tree();
1506 5de5890b 2018-10-18 stsp /* NOTREACHED */
1510 5de5890b 2018-10-18 stsp argc -= optind;
1511 5de5890b 2018-10-18 stsp argv += optind;
1513 5de5890b 2018-10-18 stsp if (argc == 1)
1514 5de5890b 2018-10-18 stsp path = argv[0];
1515 5de5890b 2018-10-18 stsp else if (argc > 1)
1516 5de5890b 2018-10-18 stsp usage_tree();
1518 7a2c19d6 2019-02-05 stsp path = NULL;
1520 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1521 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1522 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1523 9bf7a39b 2019-02-05 stsp goto done;
1525 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1526 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1527 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1528 7a2c19d6 2019-02-05 stsp goto done;
1530 7a2c19d6 2019-02-05 stsp error = NULL;
1531 7a2c19d6 2019-02-05 stsp if (worktree) {
1532 7a2c19d6 2019-02-05 stsp repo_path =
1533 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1534 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1535 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1536 7a2c19d6 2019-02-05 stsp if (error)
1537 7a2c19d6 2019-02-05 stsp goto done;
1539 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1540 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1541 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1542 7a2c19d6 2019-02-05 stsp goto done;
1547 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1548 5de5890b 2018-10-18 stsp if (error != NULL)
1549 c02c541e 2019-03-29 stsp goto done;
1551 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1552 c02c541e 2019-03-29 stsp if (error)
1553 5de5890b 2018-10-18 stsp goto done;
1555 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1556 9bf7a39b 2019-02-05 stsp if (worktree) {
1557 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1558 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1559 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1560 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1561 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1562 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1563 9bf7a39b 2019-02-05 stsp goto done;
1565 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1567 9bf7a39b 2019-02-05 stsp if (error)
1568 9bf7a39b 2019-02-05 stsp goto done;
1570 9bf7a39b 2019-02-05 stsp path = "/";
1572 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1573 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1574 9bf7a39b 2019-02-05 stsp if (error != NULL)
1575 9bf7a39b 2019-02-05 stsp goto done;
1578 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1579 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1580 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1581 5de5890b 2018-10-18 stsp if (error != NULL)
1582 5de5890b 2018-10-18 stsp goto done;
1583 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1584 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1585 5de5890b 2018-10-18 stsp if (error != NULL)
1586 5de5890b 2018-10-18 stsp goto done;
1588 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1589 15a94983 2018-12-23 stsp commit_id_str);
1590 5de5890b 2018-10-18 stsp if (error != NULL)
1591 5de5890b 2018-10-18 stsp goto done;
1594 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1595 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1597 5de5890b 2018-10-18 stsp free(in_repo_path);
1598 5de5890b 2018-10-18 stsp free(repo_path);
1599 5de5890b 2018-10-18 stsp free(cwd);
1600 5de5890b 2018-10-18 stsp free(commit_id);
1601 7a2c19d6 2019-02-05 stsp if (worktree)
1602 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1603 5de5890b 2018-10-18 stsp if (repo) {
1604 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1605 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1606 5de5890b 2018-10-18 stsp if (error == NULL)
1607 5de5890b 2018-10-18 stsp error = repo_error;
1609 5de5890b 2018-10-18 stsp return error;
1612 6bad629b 2019-02-04 stsp __dead static void
1613 6bad629b 2019-02-04 stsp usage_status(void)
1615 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1619 b72f483a 2019-02-05 stsp static const struct got_error *
1620 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1621 b72f483a 2019-02-05 stsp struct got_object_id *id)
1623 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1624 b72f483a 2019-02-05 stsp return NULL;
1627 6bad629b 2019-02-04 stsp static const struct got_error *
1628 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1630 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1631 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1632 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1633 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1636 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1637 6bad629b 2019-02-04 stsp switch (ch) {
1639 2deda0b9 2019-03-07 stsp usage_status();
1640 6bad629b 2019-02-04 stsp /* NOTREACHED */
1644 6bad629b 2019-02-04 stsp argc -= optind;
1645 6bad629b 2019-02-04 stsp argv += optind;
1647 6bad629b 2019-02-04 stsp #ifndef PROFILE
1648 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1649 6bad629b 2019-02-04 stsp NULL) == -1)
1650 6bad629b 2019-02-04 stsp err(1, "pledge");
1652 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1653 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1654 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1655 927df6b7 2019-02-10 stsp goto done;
1658 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1659 927df6b7 2019-02-10 stsp if (error != NULL)
1660 927df6b7 2019-02-10 stsp goto done;
1662 6bad629b 2019-02-04 stsp if (argc == 0) {
1663 927df6b7 2019-02-10 stsp path = strdup("");
1664 927df6b7 2019-02-10 stsp if (path == NULL) {
1665 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1666 6bad629b 2019-02-04 stsp goto done;
1668 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1669 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1670 927df6b7 2019-02-10 stsp if (error)
1671 6bad629b 2019-02-04 stsp goto done;
1673 6bad629b 2019-02-04 stsp usage_status();
1675 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1676 6bad629b 2019-02-04 stsp if (error != NULL)
1677 6bad629b 2019-02-04 stsp goto done;
1679 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1680 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1681 6bad629b 2019-02-04 stsp if (error)
1682 6bad629b 2019-02-04 stsp goto done;
1684 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1685 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1687 927df6b7 2019-02-10 stsp free(cwd);
1688 927df6b7 2019-02-10 stsp free(path);
1689 d0eebce4 2019-03-11 stsp return error;
1692 d0eebce4 2019-03-11 stsp __dead static void
1693 d0eebce4 2019-03-11 stsp usage_ref(void)
1695 d0eebce4 2019-03-11 stsp fprintf(stderr,
1696 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1697 d0eebce4 2019-03-11 stsp getprogname());
1701 d0eebce4 2019-03-11 stsp static const struct got_error *
1702 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1704 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1705 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1706 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1708 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1709 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1711 d0eebce4 2019-03-11 stsp return err;
1713 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1714 d0eebce4 2019-03-11 stsp char *refstr;
1715 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1716 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1717 d0eebce4 2019-03-11 stsp return got_error_from_errno();
1718 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1719 d0eebce4 2019-03-11 stsp free(refstr);
1722 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1723 d0eebce4 2019-03-11 stsp return NULL;
1726 d0eebce4 2019-03-11 stsp static const struct got_error *
1727 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1729 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1730 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1732 d0eebce4 2019-03-11 stsp err = got_ref_open(&ref, repo, refname);
1734 d0eebce4 2019-03-11 stsp return err;
1736 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1737 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1738 d0eebce4 2019-03-11 stsp return err;
1741 d0eebce4 2019-03-11 stsp static const struct got_error *
1742 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1744 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1745 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1746 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1748 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1750 d0eebce4 2019-03-11 stsp return err;
1752 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1754 d0eebce4 2019-03-11 stsp goto done;
1756 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1759 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1761 d0eebce4 2019-03-11 stsp return err;
1764 d0eebce4 2019-03-11 stsp static const struct got_error *
1765 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1767 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1768 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1769 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1770 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1771 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1772 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1774 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1775 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1776 d0eebce4 2019-03-11 stsp switch (ch) {
1778 d0eebce4 2019-03-11 stsp delref = optarg;
1781 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1782 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1783 d0eebce4 2019-03-11 stsp err(1, "-r option");
1786 d0eebce4 2019-03-11 stsp do_list = 1;
1789 d0eebce4 2019-03-11 stsp usage_ref();
1790 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1794 d0eebce4 2019-03-11 stsp if (do_list && delref)
1795 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1797 d0eebce4 2019-03-11 stsp argc -= optind;
1798 d0eebce4 2019-03-11 stsp argv += optind;
1800 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1801 d0eebce4 2019-03-11 stsp if (argc > 0)
1802 d0eebce4 2019-03-11 stsp usage_ref();
1803 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1804 d0eebce4 2019-03-11 stsp usage_ref();
1806 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1807 e0b57350 2019-03-12 stsp if (do_list) {
1808 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1809 e0b57350 2019-03-12 stsp NULL) == -1)
1810 e0b57350 2019-03-12 stsp err(1, "pledge");
1812 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1813 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1814 e0b57350 2019-03-12 stsp err(1, "pledge");
1817 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1818 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1819 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1820 d0eebce4 2019-03-11 stsp goto done;
1823 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1824 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1825 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1826 d0eebce4 2019-03-11 stsp goto done;
1828 d0eebce4 2019-03-11 stsp error = NULL;
1829 d0eebce4 2019-03-11 stsp if (worktree) {
1830 d0eebce4 2019-03-11 stsp repo_path =
1831 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1832 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1833 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1834 d0eebce4 2019-03-11 stsp if (error)
1835 d0eebce4 2019-03-11 stsp goto done;
1837 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1838 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1839 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1840 d0eebce4 2019-03-11 stsp goto done;
1845 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1846 d0eebce4 2019-03-11 stsp if (error != NULL)
1847 d0eebce4 2019-03-11 stsp goto done;
1849 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1850 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1851 c02c541e 2019-03-29 stsp if (error)
1852 c02c541e 2019-03-29 stsp goto done;
1854 d0eebce4 2019-03-11 stsp if (do_list)
1855 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1856 d0eebce4 2019-03-11 stsp else if (delref)
1857 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1859 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1862 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1863 d0eebce4 2019-03-11 stsp if (worktree)
1864 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1865 d0eebce4 2019-03-11 stsp free(cwd);
1866 d0eebce4 2019-03-11 stsp free(repo_path);
1867 d00136be 2019-03-26 stsp return error;
1870 d00136be 2019-03-26 stsp __dead static void
1871 d00136be 2019-03-26 stsp usage_add(void)
1873 d00136be 2019-03-26 stsp fprintf(stderr, "usage: %s add file-path\n", getprogname());
1877 d00136be 2019-03-26 stsp static const struct got_error *
1878 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1880 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1881 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1882 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1883 d00136be 2019-03-26 stsp char *cwd = NULL, *path = NULL, *relpath = NULL;
1886 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1887 d00136be 2019-03-26 stsp switch (ch) {
1889 d00136be 2019-03-26 stsp usage_add();
1890 d00136be 2019-03-26 stsp /* NOTREACHED */
1894 d00136be 2019-03-26 stsp argc -= optind;
1895 d00136be 2019-03-26 stsp argv += optind;
1897 d00136be 2019-03-26 stsp if (argc != 1)
1898 d00136be 2019-03-26 stsp usage_add();
1900 d00136be 2019-03-26 stsp path = realpath(argv[0], NULL);
1901 d00136be 2019-03-26 stsp if (path == NULL) {
1902 d00136be 2019-03-26 stsp error = got_error_from_errno();
1903 d00136be 2019-03-26 stsp goto done;
1906 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1907 d00136be 2019-03-26 stsp if (cwd == NULL) {
1908 d00136be 2019-03-26 stsp error = got_error_from_errno();
1909 d00136be 2019-03-26 stsp goto done;
1911 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1912 d00136be 2019-03-26 stsp if (error)
1913 d00136be 2019-03-26 stsp goto done;
1915 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1916 031a5338 2019-03-26 stsp if (error != NULL)
1917 031a5338 2019-03-26 stsp goto done;
1919 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1920 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1921 d00136be 2019-03-26 stsp if (error)
1922 d00136be 2019-03-26 stsp goto done;
1924 031a5338 2019-03-26 stsp error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1926 d00136be 2019-03-26 stsp if (error)
1927 d00136be 2019-03-26 stsp goto done;
1930 031a5338 2019-03-26 stsp got_repo_close(repo);
1931 d00136be 2019-03-26 stsp if (worktree)
1932 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1933 d00136be 2019-03-26 stsp free(path);
1934 d00136be 2019-03-26 stsp free(relpath);
1935 2ec1f75b 2019-03-26 stsp free(cwd);
1936 2ec1f75b 2019-03-26 stsp return error;
1939 2ec1f75b 2019-03-26 stsp __dead static void
1940 2ec1f75b 2019-03-26 stsp usage_rm(void)
1942 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1946 2ec1f75b 2019-03-26 stsp static const struct got_error *
1947 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
1949 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
1950 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
1951 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
1952 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
1953 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
1955 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
1956 2ec1f75b 2019-03-26 stsp switch (ch) {
1958 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
1961 2ec1f75b 2019-03-26 stsp usage_add();
1962 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
1966 2ec1f75b 2019-03-26 stsp argc -= optind;
1967 2ec1f75b 2019-03-26 stsp argv += optind;
1969 2ec1f75b 2019-03-26 stsp if (argc != 1)
1970 2ec1f75b 2019-03-26 stsp usage_rm();
1972 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
1973 2ec1f75b 2019-03-26 stsp if (path == NULL) {
1974 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1975 2ec1f75b 2019-03-26 stsp goto done;
1978 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
1979 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
1980 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1981 2ec1f75b 2019-03-26 stsp goto done;
1983 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1984 2ec1f75b 2019-03-26 stsp if (error)
1985 2ec1f75b 2019-03-26 stsp goto done;
1987 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1988 2ec1f75b 2019-03-26 stsp if (error != NULL)
1989 2ec1f75b 2019-03-26 stsp goto done;
1991 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1992 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1993 2ec1f75b 2019-03-26 stsp if (error)
1994 2ec1f75b 2019-03-26 stsp goto done;
1996 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1997 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
1998 a129376b 2019-03-28 stsp if (error)
1999 a129376b 2019-03-28 stsp goto done;
2002 a129376b 2019-03-28 stsp got_repo_close(repo);
2003 a129376b 2019-03-28 stsp if (worktree)
2004 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2005 a129376b 2019-03-28 stsp free(path);
2006 a129376b 2019-03-28 stsp free(cwd);
2007 a129376b 2019-03-28 stsp return error;
2010 a129376b 2019-03-28 stsp __dead static void
2011 a129376b 2019-03-28 stsp usage_revert(void)
2013 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2017 a129376b 2019-03-28 stsp static void
2018 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2020 a129376b 2019-03-28 stsp while (path[0] == '/')
2022 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2025 a129376b 2019-03-28 stsp static const struct got_error *
2026 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2028 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2029 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2030 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2031 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2034 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2035 a129376b 2019-03-28 stsp switch (ch) {
2037 a129376b 2019-03-28 stsp usage_revert();
2038 a129376b 2019-03-28 stsp /* NOTREACHED */
2042 a129376b 2019-03-28 stsp argc -= optind;
2043 a129376b 2019-03-28 stsp argv += optind;
2045 a129376b 2019-03-28 stsp if (argc != 1)
2046 a129376b 2019-03-28 stsp usage_revert();
2048 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2049 a129376b 2019-03-28 stsp if (path == NULL) {
2050 a129376b 2019-03-28 stsp error = got_error_from_errno();
2051 a129376b 2019-03-28 stsp goto done;
2054 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2055 a129376b 2019-03-28 stsp if (cwd == NULL) {
2056 a129376b 2019-03-28 stsp error = got_error_from_errno();
2057 a129376b 2019-03-28 stsp goto done;
2059 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2060 2ec1f75b 2019-03-26 stsp if (error)
2061 2ec1f75b 2019-03-26 stsp goto done;
2063 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2064 a129376b 2019-03-28 stsp if (error != NULL)
2065 a129376b 2019-03-28 stsp goto done;
2067 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2068 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2069 a129376b 2019-03-28 stsp if (error)
2070 a129376b 2019-03-28 stsp goto done;
2072 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2073 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2074 a129376b 2019-03-28 stsp if (error)
2075 a129376b 2019-03-28 stsp goto done;
2078 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2079 2ec1f75b 2019-03-26 stsp if (worktree)
2080 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2081 2ec1f75b 2019-03-26 stsp free(path);
2082 d00136be 2019-03-26 stsp free(cwd);
2083 6bad629b 2019-02-04 stsp return error;
2086 c4296144 2019-05-09 stsp __dead static void
2087 c4296144 2019-05-09 stsp usage_commit(void)
2089 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2093 c4296144 2019-05-09 stsp static const struct got_error *
2094 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2096 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2097 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2098 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2099 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2100 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2101 c4296144 2019-05-09 stsp const char *logmsg = "<no log message was specified>";
2102 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2105 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2106 c4296144 2019-05-09 stsp switch (ch) {
2108 c4296144 2019-05-09 stsp logmsg = optarg;
2111 c4296144 2019-05-09 stsp usage_commit();
2112 c4296144 2019-05-09 stsp /* NOTREACHED */
2116 c4296144 2019-05-09 stsp argc -= optind;
2117 c4296144 2019-05-09 stsp argv += optind;
2119 c4296144 2019-05-09 stsp if (argc == 1) {
2120 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2121 c4296144 2019-05-09 stsp if (path == NULL) {
2122 c4296144 2019-05-09 stsp error = got_error_from_errno();
2123 c4296144 2019-05-09 stsp goto done;
2125 c4296144 2019-05-09 stsp } else if (argc != 0)
2126 c4296144 2019-05-09 stsp usage_commit();
2128 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2129 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2130 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2131 35bd8fed 2019-05-09 stsp goto done;
2134 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2135 c4296144 2019-05-09 stsp if (cwd == NULL) {
2136 c4296144 2019-05-09 stsp error = got_error_from_errno();
2137 c4296144 2019-05-09 stsp goto done;
2139 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2140 c4296144 2019-05-09 stsp if (error)
2141 c4296144 2019-05-09 stsp goto done;
2143 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2144 c4296144 2019-05-09 stsp if (error != NULL)
2145 c4296144 2019-05-09 stsp goto done;
2147 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2148 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2149 c4296144 2019-05-09 stsp if (error)
2150 c4296144 2019-05-09 stsp goto done;
2152 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2153 35bd8fed 2019-05-09 stsp logmsg, print_status, NULL, repo);
2154 c4296144 2019-05-09 stsp if (error)
2155 c4296144 2019-05-09 stsp goto done;
2157 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2158 c4296144 2019-05-09 stsp if (error)
2159 c4296144 2019-05-09 stsp goto done;
2160 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2163 c4296144 2019-05-09 stsp got_repo_close(repo);
2164 c4296144 2019-05-09 stsp if (worktree)
2165 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2166 c4296144 2019-05-09 stsp free(path);
2167 c4296144 2019-05-09 stsp free(cwd);
2168 c4296144 2019-05-09 stsp free(id_str);
2169 c4296144 2019-05-09 stsp return error;