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);
80 d00136be 2019-03-26 stsp __dead static void usage_add(void);
81 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
84 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
87 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
88 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
90 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
91 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
92 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
95 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
96 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
97 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
98 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
99 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
100 1b6b95a8 2018-03-12 stsp "show repository history" },
101 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
102 b00d56cd 2018-04-01 stsp "compare files and directories" },
103 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
104 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
105 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
106 a67e2392 2019-03-26 stsp "list files and directories in repository" },
107 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
108 1b6b95a8 2018-03-12 stsp "show modification status of files" },
109 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
110 d0eebce4 2019-03-11 stsp "manage references in repository" },
111 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
112 d00136be 2019-03-26 stsp "add a new file to version control" },
113 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
114 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
118 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
120 5c860e29 2018-03-12 stsp struct cmd *cmd;
121 5c860e29 2018-03-12 stsp unsigned int i;
123 1b6b95a8 2018-03-12 stsp int hflag = 0;
125 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
127 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
128 5c860e29 2018-03-12 stsp switch (ch) {
134 5c860e29 2018-03-12 stsp /* NOTREACHED */
138 5c860e29 2018-03-12 stsp argc -= optind;
139 5c860e29 2018-03-12 stsp argv += optind;
140 1e70621d 2018-03-27 stsp optind = 0;
142 5c860e29 2018-03-12 stsp if (argc <= 0)
145 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
146 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
148 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
149 d7d4f210 2018-03-12 stsp const struct got_error *error;
151 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
153 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
157 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
159 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
160 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
161 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
168 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
172 4ed7e80c 2018-05-20 stsp __dead static void
173 5c860e29 2018-03-12 stsp usage(void)
177 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
178 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
179 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
180 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
181 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
186 0266afb7 2019-01-04 stsp static const struct got_error *
187 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
188 d0eebce4 2019-03-11 stsp const char *worktree_path)
190 0266afb7 2019-01-04 stsp const struct got_error *error;
192 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
193 0266afb7 2019-01-04 stsp return got_error_from_errno();
195 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
196 0266afb7 2019-01-04 stsp return got_error_from_errno();
198 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
199 0266afb7 2019-01-04 stsp return got_error_from_errno();
201 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
202 0266afb7 2019-01-04 stsp if (error != NULL)
203 0266afb7 2019-01-04 stsp return error;
205 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
206 0266afb7 2019-01-04 stsp return got_error_from_errno();
208 0266afb7 2019-01-04 stsp return NULL;
211 4ed7e80c 2018-05-20 stsp __dead static void
212 c09a553d 2018-03-12 stsp usage_checkout(void)
214 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
215 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
219 92a684f4 2018-03-12 stsp static void
220 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
222 92a684f4 2018-03-12 stsp char *worktree_path = arg;
224 92a684f4 2018-03-12 stsp while (path[0] == '/')
227 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
230 99437157 2018-11-11 stsp static const struct got_error *
231 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
233 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
234 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
235 99437157 2018-11-11 stsp return NULL;
238 8069f636 2019-01-12 stsp static const struct got_error *
239 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
240 8069f636 2019-01-12 stsp struct got_repository *repo)
242 8069f636 2019-01-12 stsp const struct got_error *err;
243 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
244 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
245 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
247 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
248 8069f636 2019-01-12 stsp if (head_ref == NULL)
249 8069f636 2019-01-12 stsp return got_error_from_errno();
251 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
252 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
256 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
260 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
263 8069f636 2019-01-12 stsp while (1) {
264 8069f636 2019-01-12 stsp struct got_object_id *id;
266 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
269 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
271 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
272 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
275 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
277 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
283 8069f636 2019-01-12 stsp if (id == NULL)
285 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
289 8069f636 2019-01-12 stsp if (head_ref)
290 8069f636 2019-01-12 stsp got_ref_close(head_ref);
292 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
293 8069f636 2019-01-12 stsp return err;
297 4ed7e80c 2018-05-20 stsp static const struct got_error *
298 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
300 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
301 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
302 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
303 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
304 c09a553d 2018-03-12 stsp char *repo_path = NULL;
305 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
306 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
307 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
308 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
310 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
311 0bb8a95e 2018-03-12 stsp switch (ch) {
313 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
314 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
315 8069f636 2019-01-12 stsp return got_error_from_errno();
318 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
321 2deda0b9 2019-03-07 stsp usage_checkout();
322 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
326 0bb8a95e 2018-03-12 stsp argc -= optind;
327 0bb8a95e 2018-03-12 stsp argv += optind;
329 6715a751 2018-03-16 stsp #ifndef PROFILE
330 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
331 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
332 c09a553d 2018-03-12 stsp err(1, "pledge");
334 0bb8a95e 2018-03-12 stsp if (argc == 1) {
335 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
336 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
337 76089277 2018-04-01 stsp if (repo_path == NULL)
338 76089277 2018-04-01 stsp return got_error_from_errno();
339 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
340 76089277 2018-04-01 stsp if (cwd == NULL) {
341 76089277 2018-04-01 stsp error = got_error_from_errno();
344 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
345 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
347 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
348 76089277 2018-04-01 stsp if (base == NULL) {
349 76089277 2018-04-01 stsp error = got_error_from_errno();
352 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
353 c09a553d 2018-03-12 stsp if (dotgit)
354 c09a553d 2018-03-12 stsp *dotgit = '\0';
355 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
356 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
361 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
362 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
363 76089277 2018-04-01 stsp if (repo_path == NULL) {
364 76089277 2018-04-01 stsp error = got_error_from_errno();
367 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
368 76089277 2018-04-01 stsp if (worktree_path == NULL) {
369 76089277 2018-04-01 stsp error = got_error_from_errno();
373 c09a553d 2018-03-12 stsp usage_checkout();
375 0cd1c46a 2019-03-11 stsp error = apply_unveil(repo_path, 0, worktree_path);
379 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
380 c09a553d 2018-03-12 stsp if (error != NULL)
383 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
384 c09a553d 2018-03-12 stsp if (error != NULL)
387 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
388 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
391 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
392 c09a553d 2018-03-12 stsp if (error != NULL)
395 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
396 e5dc7198 2018-12-29 stsp path_prefix);
397 e5dc7198 2018-12-29 stsp if (error != NULL)
399 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
400 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
404 8069f636 2019-01-12 stsp if (commit_id_str) {
405 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
406 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
407 8069f636 2019-01-12 stsp commit_id_str);
408 8069f636 2019-01-12 stsp if (error != NULL)
410 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
411 8069f636 2019-01-12 stsp if (error != NULL) {
412 8069f636 2019-01-12 stsp free(commit_id);
415 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
416 8069f636 2019-01-12 stsp commit_id);
417 8069f636 2019-01-12 stsp free(commit_id);
422 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
423 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
424 c09a553d 2018-03-12 stsp if (error != NULL)
427 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
430 8069f636 2019-01-12 stsp free(commit_id_str);
431 76089277 2018-04-01 stsp free(repo_path);
432 507dc3bb 2018-12-29 stsp free(worktree_path);
433 507dc3bb 2018-12-29 stsp return error;
436 507dc3bb 2018-12-29 stsp __dead static void
437 507dc3bb 2018-12-29 stsp usage_update(void)
439 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
440 507dc3bb 2018-12-29 stsp getprogname());
444 507dc3bb 2018-12-29 stsp static void
445 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
447 784955db 2019-01-12 stsp int *did_something = arg;
449 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
452 1545c615 2019-02-10 stsp *did_something = 1;
453 507dc3bb 2018-12-29 stsp while (path[0] == '/')
455 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
458 be7061eb 2018-12-30 stsp static const struct got_error *
459 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
461 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
462 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
463 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
464 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
465 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
466 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
467 784955db 2019-01-12 stsp int ch, did_something = 0;
469 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
470 507dc3bb 2018-12-29 stsp switch (ch) {
472 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
473 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
474 9c4b8182 2019-01-02 stsp return got_error_from_errno();
477 2deda0b9 2019-03-07 stsp usage_update();
478 507dc3bb 2018-12-29 stsp /* NOTREACHED */
482 507dc3bb 2018-12-29 stsp argc -= optind;
483 507dc3bb 2018-12-29 stsp argv += optind;
485 507dc3bb 2018-12-29 stsp #ifndef PROFILE
486 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
487 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
488 507dc3bb 2018-12-29 stsp err(1, "pledge");
490 507dc3bb 2018-12-29 stsp if (argc == 0) {
491 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
492 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
493 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
496 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
497 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
498 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
499 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
503 507dc3bb 2018-12-29 stsp usage_update();
505 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
506 507dc3bb 2018-12-29 stsp if (error != NULL)
509 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
510 507dc3bb 2018-12-29 stsp if (error != NULL)
513 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
514 8c02d095 2019-02-05 stsp got_worktree_get_root_path(worktree));
518 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
519 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
520 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
521 507dc3bb 2018-12-29 stsp if (error != NULL)
523 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
524 9c4b8182 2019-01-02 stsp if (error != NULL)
526 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
527 507dc3bb 2018-12-29 stsp if (error != NULL)
530 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
531 507dc3bb 2018-12-29 stsp commit_id_str);
532 507dc3bb 2018-12-29 stsp if (error != NULL)
536 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
537 be7061eb 2018-12-30 stsp if (error != NULL)
540 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
541 507dc3bb 2018-12-29 stsp commit_id) != 0) {
542 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
543 507dc3bb 2018-12-29 stsp commit_id);
548 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
549 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
550 507dc3bb 2018-12-29 stsp if (error != NULL)
553 784955db 2019-01-12 stsp if (did_something)
554 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
556 784955db 2019-01-12 stsp printf("Already up-to-date\n");
558 c09a553d 2018-03-12 stsp free(worktree_path);
559 507dc3bb 2018-12-29 stsp free(commit_id);
560 9c4b8182 2019-01-02 stsp free(commit_id_str);
561 c09a553d 2018-03-12 stsp return error;
564 f42b1b34 2018-03-12 stsp static const struct got_error *
565 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
566 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
568 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
569 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
570 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
571 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
573 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
574 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
576 79109fed 2018-03-27 stsp return err;
578 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
579 79f35eb3 2018-06-11 stsp if (qid != NULL) {
580 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
582 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
584 79109fed 2018-03-27 stsp return err;
586 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
587 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
588 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
590 0f2b3dca 2018-12-22 stsp return err;
592 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
594 79109fed 2018-03-27 stsp return err;
597 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
601 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
602 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
605 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
606 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
607 0f2b3dca 2018-12-22 stsp free(id_str1);
608 0f2b3dca 2018-12-22 stsp free(id_str2);
609 79109fed 2018-03-27 stsp return err;
612 4bb494d5 2018-06-16 stsp static char *
613 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
615 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
616 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
622 79109fed 2018-03-27 stsp static const struct got_error *
623 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
624 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
625 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
627 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
628 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
629 6c281f94 2018-06-11 stsp char datebuf[26];
630 45d799e2 2018-12-23 stsp time_t committer_time;
631 45d799e2 2018-12-23 stsp const char *author, *committer;
632 199a4027 2019-02-02 stsp char *refs_str = NULL;
633 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
635 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
637 199a4027 2019-02-02 stsp const char *name;
638 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
640 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
641 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
643 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
645 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
647 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
649 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
651 199a4027 2019-02-02 stsp s = refs_str;
652 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
653 199a4027 2019-02-02 stsp name) == -1) {
654 199a4027 2019-02-02 stsp err = got_error_from_errno();
660 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
662 8bf5b3c9 2018-03-17 stsp return err;
664 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
665 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
666 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
667 832c249c 2018-06-10 stsp free(id_str);
668 d3d493d7 2019-02-21 stsp id_str = NULL;
669 d3d493d7 2019-02-21 stsp free(refs_str);
670 d3d493d7 2019-02-21 stsp refs_str = NULL;
671 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
672 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
673 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
674 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
675 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
676 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
677 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
678 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
679 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
680 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
681 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
683 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
684 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
685 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
687 a0603db2 2018-06-10 stsp return err;
688 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
689 a0603db2 2018-06-10 stsp free(id_str);
693 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
694 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
695 832c249c 2018-06-10 stsp return got_error_from_errno();
697 621015ac 2018-07-23 stsp logmsg = logmsg0;
699 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
701 832c249c 2018-06-10 stsp printf(" %s\n", line);
702 832c249c 2018-06-10 stsp } while (line);
703 621015ac 2018-07-23 stsp free(logmsg0);
705 971751ac 2018-03-27 stsp if (show_patch) {
706 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
707 971751ac 2018-03-27 stsp if (err == 0)
708 971751ac 2018-03-27 stsp printf("\n");
711 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
712 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
713 07862c20 2018-09-15 stsp return err;
716 f42b1b34 2018-03-12 stsp static const struct got_error *
717 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
718 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
719 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
721 f42b1b34 2018-03-12 stsp const struct got_error *err;
722 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
724 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
725 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
727 f42b1b34 2018-03-12 stsp return err;
728 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
731 31cedeaf 2018-09-15 stsp while (1) {
732 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
733 372ccdbb 2018-06-10 stsp struct got_object_id *id;
735 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
738 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
740 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
741 9ba79e04 2018-06-11 stsp err = NULL;
744 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
746 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
752 b43fbaa0 2018-06-11 stsp if (id == NULL)
755 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
758 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
760 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
761 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
765 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
766 f42b1b34 2018-03-12 stsp return err;
769 4ed7e80c 2018-05-20 stsp __dead static void
770 6f3d1eb0 2018-03-12 stsp usage_log(void)
772 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
773 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
777 4ed7e80c 2018-05-20 stsp static const struct got_error *
778 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
780 f42b1b34 2018-03-12 stsp const struct got_error *error;
781 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
782 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
783 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
784 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
785 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
786 d142fc45 2018-04-01 stsp char *start_commit = NULL;
787 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
788 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
789 64a96a6d 2018-04-01 stsp const char *errstr;
790 199a4027 2019-02-02 stsp struct got_reflist_head refs;
792 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
794 6715a751 2018-03-16 stsp #ifndef PROFILE
795 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
798 f42b1b34 2018-03-12 stsp err(1, "pledge");
801 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
802 79109fed 2018-03-27 stsp switch (ch) {
804 79109fed 2018-03-27 stsp show_patch = 1;
807 d142fc45 2018-04-01 stsp start_commit = optarg;
810 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
812 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
813 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
816 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
817 64a96a6d 2018-04-01 stsp if (errstr != NULL)
818 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
821 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
824 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
825 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
826 04ca23f4 2018-07-16 stsp err(1, "-r option");
829 2deda0b9 2019-03-07 stsp usage_log();
830 79109fed 2018-03-27 stsp /* NOTREACHED */
834 79109fed 2018-03-27 stsp argc -= optind;
835 79109fed 2018-03-27 stsp argv += optind;
837 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
838 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
839 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
843 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
844 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
846 cffc0aa4 2019-02-05 stsp error = NULL;
848 e7301579 2019-03-18 stsp if (argc == 0) {
849 cbd1af7a 2019-03-18 stsp path = strdup("");
850 e7301579 2019-03-18 stsp if (path == NULL) {
851 e7301579 2019-03-18 stsp error = got_error_from_errno();
854 e7301579 2019-03-18 stsp } else if (argc == 1) {
855 e7301579 2019-03-18 stsp if (worktree) {
856 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
861 e7301579 2019-03-18 stsp path = strdup(argv[0]);
862 e7301579 2019-03-18 stsp if (path == NULL) {
863 e7301579 2019-03-18 stsp error = got_error_from_errno();
868 cbd1af7a 2019-03-18 stsp usage_log();
870 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
871 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
872 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
873 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
877 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1,
878 efcae1c6 2019-03-07 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
882 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
883 d7d4f210 2018-03-12 stsp if (error != NULL)
886 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
887 3235492e 2018-04-01 stsp struct got_reference *head_ref;
888 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
889 3235492e 2018-04-01 stsp if (error != NULL)
890 3235492e 2018-04-01 stsp return error;
891 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
892 3235492e 2018-04-01 stsp got_ref_close(head_ref);
893 3235492e 2018-04-01 stsp if (error != NULL)
894 3235492e 2018-04-01 stsp return error;
895 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
897 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
898 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
899 3235492e 2018-04-01 stsp if (error == NULL) {
900 1caad61b 2019-02-01 stsp int obj_type;
901 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
902 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
903 d1f2edc9 2018-06-13 stsp if (error != NULL)
905 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
906 1caad61b 2019-02-01 stsp if (error != NULL)
908 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
909 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
910 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
911 1caad61b 2019-02-01 stsp if (error != NULL)
913 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
914 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
915 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
916 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
920 1caad61b 2019-02-01 stsp id = got_object_id_dup(
921 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
922 1caad61b 2019-02-01 stsp if (id == NULL)
923 1caad61b 2019-02-01 stsp error = got_error_from_errno();
924 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
927 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
928 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
931 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
932 d1f2edc9 2018-06-13 stsp if (error != NULL)
935 15a94983 2018-12-23 stsp if (commit == NULL) {
936 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
937 d1f2edc9 2018-06-13 stsp start_commit);
938 d1f2edc9 2018-06-13 stsp if (error != NULL)
939 d1f2edc9 2018-06-13 stsp return error;
942 d7d4f210 2018-03-12 stsp if (error != NULL)
945 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
946 04ca23f4 2018-07-16 stsp if (error != NULL)
948 04ca23f4 2018-07-16 stsp if (in_repo_path) {
949 04ca23f4 2018-07-16 stsp free(path);
950 04ca23f4 2018-07-16 stsp path = in_repo_path;
953 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
957 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
958 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
960 04ca23f4 2018-07-16 stsp free(path);
961 04ca23f4 2018-07-16 stsp free(repo_path);
964 cffc0aa4 2019-02-05 stsp if (worktree)
965 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
966 ad242220 2018-09-08 stsp if (repo) {
967 ad242220 2018-09-08 stsp const struct got_error *repo_error;
968 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
969 ad242220 2018-09-08 stsp if (error == NULL)
970 ad242220 2018-09-08 stsp error = repo_error;
972 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
973 8bf5b3c9 2018-03-17 stsp return error;
976 4ed7e80c 2018-05-20 stsp __dead static void
977 3f8b7d6a 2018-04-01 stsp usage_diff(void)
979 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
980 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
984 b72f483a 2019-02-05 stsp struct print_diff_arg {
985 b72f483a 2019-02-05 stsp struct got_repository *repo;
986 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
987 b72f483a 2019-02-05 stsp int diff_context;
988 3fc0c068 2019-02-10 stsp const char *id_str;
989 3fc0c068 2019-02-10 stsp int header_shown;
992 4ed7e80c 2018-05-20 stsp static const struct got_error *
993 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
994 b72f483a 2019-02-05 stsp struct got_object_id *id)
996 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
997 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
998 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
999 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1000 b72f483a 2019-02-05 stsp char *abspath = NULL;
1001 b72f483a 2019-02-05 stsp struct stat sb;
1003 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1004 2ec1f75b 2019-03-26 stsp status != GOT_STATUS_DELETE)
1005 b72f483a 2019-02-05 stsp return NULL;
1007 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1008 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1009 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1010 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1013 2ec1f75b 2019-03-26 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_DELETE) {
1014 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1016 d00136be 2019-03-26 stsp goto done;
1020 2ec1f75b 2019-03-26 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1021 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1022 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1023 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1024 2ec1f75b 2019-03-26 stsp goto done;
1027 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1028 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1029 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1030 2ec1f75b 2019-03-26 stsp goto done;
1032 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1033 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1034 2ec1f75b 2019-03-26 stsp goto done;
1037 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1039 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1042 b72f483a 2019-02-05 stsp if (blob1)
1043 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1044 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1045 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1046 b72f483a 2019-02-05 stsp free(abspath);
1047 927df6b7 2019-02-10 stsp return err;
1050 927df6b7 2019-02-10 stsp static const struct got_error *
1051 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1053 b00d56cd 2018-04-01 stsp const struct got_error *error;
1054 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1055 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1056 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1057 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1058 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1059 15a94983 2018-12-23 stsp int type1, type2;
1060 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1061 c0cc5c62 2018-10-18 stsp const char *errstr;
1062 927df6b7 2019-02-10 stsp char *path = NULL;
1064 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1065 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1066 25eccc22 2019-01-04 stsp NULL) == -1)
1067 b00d56cd 2018-04-01 stsp err(1, "pledge");
1070 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1071 b00d56cd 2018-04-01 stsp switch (ch) {
1073 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1074 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1075 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1078 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1079 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1080 b72f483a 2019-02-05 stsp err(1, "-r option");
1083 2deda0b9 2019-03-07 stsp usage_diff();
1084 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1088 b00d56cd 2018-04-01 stsp argc -= optind;
1089 b00d56cd 2018-04-01 stsp argv += optind;
1091 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1092 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1093 b72f483a 2019-02-05 stsp error = got_error_from_errno();
1094 b72f483a 2019-02-05 stsp goto done;
1096 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1097 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1098 927df6b7 2019-02-10 stsp goto done;
1099 927df6b7 2019-02-10 stsp if (argc <= 1) {
1100 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1101 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1102 927df6b7 2019-02-10 stsp goto done;
1104 b72f483a 2019-02-05 stsp if (repo_path)
1106 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1107 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1108 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1109 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1110 927df6b7 2019-02-10 stsp goto done;
1112 927df6b7 2019-02-10 stsp if (argc == 1) {
1113 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1115 927df6b7 2019-02-10 stsp if (error)
1116 927df6b7 2019-02-10 stsp goto done;
1118 927df6b7 2019-02-10 stsp path = strdup("");
1119 927df6b7 2019-02-10 stsp if (path == NULL) {
1120 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1121 927df6b7 2019-02-10 stsp goto done;
1124 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1125 15a94983 2018-12-23 stsp id_str1 = argv[0];
1126 15a94983 2018-12-23 stsp id_str2 = argv[1];
1128 b00d56cd 2018-04-01 stsp usage_diff();
1130 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1131 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1132 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1133 b72f483a 2019-02-05 stsp return got_error_from_errno();
1136 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1,
1137 b72f483a 2019-02-05 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1138 25eccc22 2019-01-04 stsp if (error)
1139 25eccc22 2019-01-04 stsp goto done;
1141 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1142 76089277 2018-04-01 stsp free(repo_path);
1143 b00d56cd 2018-04-01 stsp if (error != NULL)
1144 b00d56cd 2018-04-01 stsp goto done;
1146 b72f483a 2019-02-05 stsp if (worktree) {
1147 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1148 b72f483a 2019-02-05 stsp char *id_str;
1149 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1150 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1151 b72f483a 2019-02-05 stsp if (error)
1152 b72f483a 2019-02-05 stsp goto done;
1153 b72f483a 2019-02-05 stsp arg.repo = repo;
1154 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1155 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1156 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1157 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1159 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1160 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1161 b72f483a 2019-02-05 stsp free(id_str);
1162 b72f483a 2019-02-05 stsp goto done;
1165 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1166 6402fb3c 2018-09-15 stsp if (error)
1167 b00d56cd 2018-04-01 stsp goto done;
1169 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1170 6402fb3c 2018-09-15 stsp if (error)
1171 b00d56cd 2018-04-01 stsp goto done;
1173 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1174 15a94983 2018-12-23 stsp if (error)
1175 15a94983 2018-12-23 stsp goto done;
1177 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1178 15a94983 2018-12-23 stsp if (error)
1179 15a94983 2018-12-23 stsp goto done;
1181 15a94983 2018-12-23 stsp if (type1 != type2) {
1182 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1183 b00d56cd 2018-04-01 stsp goto done;
1186 15a94983 2018-12-23 stsp switch (type1) {
1187 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1188 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1189 54156555 2018-12-24 stsp diff_context, repo, stdout);
1191 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1192 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1193 54156555 2018-12-24 stsp diff_context, repo, stdout);
1195 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1196 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1198 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1199 c0cc5c62 2018-10-18 stsp repo, stdout);
1202 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1206 15a94983 2018-12-23 stsp free(id1);
1207 15a94983 2018-12-23 stsp free(id2);
1208 927df6b7 2019-02-10 stsp free(path);
1209 b72f483a 2019-02-05 stsp if (worktree)
1210 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1211 ad242220 2018-09-08 stsp if (repo) {
1212 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1213 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1214 ad242220 2018-09-08 stsp if (error == NULL)
1215 ad242220 2018-09-08 stsp error = repo_error;
1217 b00d56cd 2018-04-01 stsp return error;
1220 404c43c4 2018-06-21 stsp __dead static void
1221 404c43c4 2018-06-21 stsp usage_blame(void)
1223 9270e621 2019-02-05 stsp fprintf(stderr,
1224 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1225 404c43c4 2018-06-21 stsp getprogname());
1229 404c43c4 2018-06-21 stsp static const struct got_error *
1230 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1232 404c43c4 2018-06-21 stsp const struct got_error *error;
1233 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1234 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1235 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1236 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1237 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1240 404c43c4 2018-06-21 stsp #ifndef PROFILE
1241 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1242 36e2fb66 2019-01-04 stsp NULL) == -1)
1243 404c43c4 2018-06-21 stsp err(1, "pledge");
1246 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1247 404c43c4 2018-06-21 stsp switch (ch) {
1249 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1252 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1253 66bea077 2018-08-02 stsp if (repo_path == NULL)
1254 66bea077 2018-08-02 stsp err(1, "-r option");
1257 2deda0b9 2019-03-07 stsp usage_blame();
1258 404c43c4 2018-06-21 stsp /* NOTREACHED */
1262 404c43c4 2018-06-21 stsp argc -= optind;
1263 404c43c4 2018-06-21 stsp argv += optind;
1265 a39318fd 2018-08-02 stsp if (argc == 1)
1266 404c43c4 2018-06-21 stsp path = argv[0];
1268 404c43c4 2018-06-21 stsp usage_blame();
1270 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1271 66bea077 2018-08-02 stsp if (cwd == NULL) {
1272 66bea077 2018-08-02 stsp error = got_error_from_errno();
1273 66bea077 2018-08-02 stsp goto done;
1275 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1276 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1277 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1278 66bea077 2018-08-02 stsp goto done;
1280 0c06baac 2019-02-05 stsp error = NULL;
1281 0c06baac 2019-02-05 stsp if (worktree) {
1282 0c06baac 2019-02-05 stsp repo_path =
1283 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1284 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1285 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1286 0c06baac 2019-02-05 stsp if (error)
1287 0c06baac 2019-02-05 stsp goto done;
1289 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1290 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1291 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1292 0c06baac 2019-02-05 stsp goto done;
1297 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1, NULL);
1298 36e2fb66 2019-01-04 stsp if (error)
1299 36e2fb66 2019-01-04 stsp goto done;
1301 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1302 404c43c4 2018-06-21 stsp if (error != NULL)
1303 404c43c4 2018-06-21 stsp goto done;
1305 0c06baac 2019-02-05 stsp if (worktree) {
1306 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1307 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1308 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1309 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1310 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1311 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1312 6efaaa2d 2019-02-05 stsp path) == -1) {
1313 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1314 0c06baac 2019-02-05 stsp goto done;
1316 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1319 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1321 0c06baac 2019-02-05 stsp if (error)
1322 66bea077 2018-08-02 stsp goto done;
1324 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1325 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1326 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1327 404c43c4 2018-06-21 stsp if (error != NULL)
1328 66bea077 2018-08-02 stsp goto done;
1329 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1330 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1331 404c43c4 2018-06-21 stsp if (error != NULL)
1332 66bea077 2018-08-02 stsp goto done;
1334 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1335 15a94983 2018-12-23 stsp commit_id_str);
1336 404c43c4 2018-06-21 stsp if (error != NULL)
1337 66bea077 2018-08-02 stsp goto done;
1340 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1342 66bea077 2018-08-02 stsp free(in_repo_path);
1343 66bea077 2018-08-02 stsp free(repo_path);
1344 66bea077 2018-08-02 stsp free(cwd);
1345 404c43c4 2018-06-21 stsp free(commit_id);
1346 0c06baac 2019-02-05 stsp if (worktree)
1347 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1348 ad242220 2018-09-08 stsp if (repo) {
1349 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1350 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1351 ad242220 2018-09-08 stsp if (error == NULL)
1352 ad242220 2018-09-08 stsp error = repo_error;
1354 404c43c4 2018-06-21 stsp return error;
1357 5de5890b 2018-10-18 stsp __dead static void
1358 5de5890b 2018-10-18 stsp usage_tree(void)
1360 c1669e2e 2019-01-09 stsp fprintf(stderr,
1361 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1362 5de5890b 2018-10-18 stsp getprogname());
1366 c1669e2e 2019-01-09 stsp static void
1367 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1368 c1669e2e 2019-01-09 stsp const char *root_path)
1370 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1372 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1373 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1376 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1377 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1378 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1381 5de5890b 2018-10-18 stsp static const struct got_error *
1382 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1383 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1384 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1386 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1387 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1388 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1389 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1390 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1392 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1394 5de5890b 2018-10-18 stsp goto done;
1396 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1398 5de5890b 2018-10-18 stsp goto done;
1399 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1400 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1401 5de5890b 2018-10-18 stsp while (te) {
1402 5de5890b 2018-10-18 stsp char *id = NULL;
1404 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1407 5de5890b 2018-10-18 stsp if (show_ids) {
1408 5de5890b 2018-10-18 stsp char *id_str;
1409 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1411 5de5890b 2018-10-18 stsp goto done;
1412 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1413 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1414 5de5890b 2018-10-18 stsp free(id_str);
1415 5de5890b 2018-10-18 stsp goto done;
1417 5de5890b 2018-10-18 stsp free(id_str);
1419 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1422 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1423 c1669e2e 2019-01-09 stsp char *child_path;
1424 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1425 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1426 c1669e2e 2019-01-09 stsp te->name) == -1) {
1427 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1428 c1669e2e 2019-01-09 stsp goto done;
1430 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1431 c1669e2e 2019-01-09 stsp root_path, repo);
1432 c1669e2e 2019-01-09 stsp free(child_path);
1434 c1669e2e 2019-01-09 stsp goto done;
1437 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1441 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1442 5de5890b 2018-10-18 stsp free(tree_id);
1443 5de5890b 2018-10-18 stsp return err;
1446 5de5890b 2018-10-18 stsp static const struct got_error *
1447 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1449 5de5890b 2018-10-18 stsp const struct got_error *error;
1450 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1451 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1452 7a2c19d6 2019-02-05 stsp const char *path;
1453 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1454 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1455 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1456 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1459 5de5890b 2018-10-18 stsp #ifndef PROFILE
1460 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1461 0f8d269b 2019-01-04 stsp NULL) == -1)
1462 5de5890b 2018-10-18 stsp err(1, "pledge");
1465 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1466 5de5890b 2018-10-18 stsp switch (ch) {
1468 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1471 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1472 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1473 5de5890b 2018-10-18 stsp err(1, "-r option");
1476 5de5890b 2018-10-18 stsp show_ids = 1;
1479 c1669e2e 2019-01-09 stsp recurse = 1;
1482 2deda0b9 2019-03-07 stsp usage_tree();
1483 5de5890b 2018-10-18 stsp /* NOTREACHED */
1487 5de5890b 2018-10-18 stsp argc -= optind;
1488 5de5890b 2018-10-18 stsp argv += optind;
1490 5de5890b 2018-10-18 stsp if (argc == 1)
1491 5de5890b 2018-10-18 stsp path = argv[0];
1492 5de5890b 2018-10-18 stsp else if (argc > 1)
1493 5de5890b 2018-10-18 stsp usage_tree();
1495 7a2c19d6 2019-02-05 stsp path = NULL;
1497 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1498 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1499 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1500 9bf7a39b 2019-02-05 stsp goto done;
1502 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1503 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1504 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1505 7a2c19d6 2019-02-05 stsp goto done;
1507 7a2c19d6 2019-02-05 stsp error = NULL;
1508 7a2c19d6 2019-02-05 stsp if (worktree) {
1509 7a2c19d6 2019-02-05 stsp repo_path =
1510 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1511 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1512 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1513 7a2c19d6 2019-02-05 stsp if (error)
1514 7a2c19d6 2019-02-05 stsp goto done;
1516 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1517 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1518 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1519 7a2c19d6 2019-02-05 stsp goto done;
1524 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, 1, NULL);
1525 0f8d269b 2019-01-04 stsp if (error)
1526 0f8d269b 2019-01-04 stsp goto done;
1528 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1529 5de5890b 2018-10-18 stsp if (error != NULL)
1530 5de5890b 2018-10-18 stsp goto done;
1532 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1533 9bf7a39b 2019-02-05 stsp if (worktree) {
1534 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1535 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1536 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1537 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1538 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1539 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1540 9bf7a39b 2019-02-05 stsp goto done;
1542 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1544 9bf7a39b 2019-02-05 stsp if (error)
1545 9bf7a39b 2019-02-05 stsp goto done;
1547 9bf7a39b 2019-02-05 stsp path = "/";
1549 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1550 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1551 9bf7a39b 2019-02-05 stsp if (error != NULL)
1552 9bf7a39b 2019-02-05 stsp goto done;
1555 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1556 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1557 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1558 5de5890b 2018-10-18 stsp if (error != NULL)
1559 5de5890b 2018-10-18 stsp goto done;
1560 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1561 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1562 5de5890b 2018-10-18 stsp if (error != NULL)
1563 5de5890b 2018-10-18 stsp goto done;
1565 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1566 15a94983 2018-12-23 stsp commit_id_str);
1567 5de5890b 2018-10-18 stsp if (error != NULL)
1568 5de5890b 2018-10-18 stsp goto done;
1571 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1572 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1574 5de5890b 2018-10-18 stsp free(in_repo_path);
1575 5de5890b 2018-10-18 stsp free(repo_path);
1576 5de5890b 2018-10-18 stsp free(cwd);
1577 5de5890b 2018-10-18 stsp free(commit_id);
1578 7a2c19d6 2019-02-05 stsp if (worktree)
1579 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1580 5de5890b 2018-10-18 stsp if (repo) {
1581 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1582 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1583 5de5890b 2018-10-18 stsp if (error == NULL)
1584 5de5890b 2018-10-18 stsp error = repo_error;
1586 5de5890b 2018-10-18 stsp return error;
1589 6bad629b 2019-02-04 stsp __dead static void
1590 6bad629b 2019-02-04 stsp usage_status(void)
1592 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1596 b72f483a 2019-02-05 stsp static const struct got_error *
1597 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1598 b72f483a 2019-02-05 stsp struct got_object_id *id)
1600 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1601 b72f483a 2019-02-05 stsp return NULL;
1604 6bad629b 2019-02-04 stsp static const struct got_error *
1605 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1607 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1608 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1609 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1610 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1613 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1614 6bad629b 2019-02-04 stsp switch (ch) {
1616 2deda0b9 2019-03-07 stsp usage_status();
1617 6bad629b 2019-02-04 stsp /* NOTREACHED */
1621 6bad629b 2019-02-04 stsp argc -= optind;
1622 6bad629b 2019-02-04 stsp argv += optind;
1624 6bad629b 2019-02-04 stsp #ifndef PROFILE
1625 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1626 6bad629b 2019-02-04 stsp NULL) == -1)
1627 6bad629b 2019-02-04 stsp err(1, "pledge");
1629 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1630 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1631 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1632 927df6b7 2019-02-10 stsp goto done;
1635 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1636 927df6b7 2019-02-10 stsp if (error != NULL)
1637 927df6b7 2019-02-10 stsp goto done;
1639 6bad629b 2019-02-04 stsp if (argc == 0) {
1640 927df6b7 2019-02-10 stsp path = strdup("");
1641 927df6b7 2019-02-10 stsp if (path == NULL) {
1642 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1643 6bad629b 2019-02-04 stsp goto done;
1645 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1646 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1647 927df6b7 2019-02-10 stsp if (error)
1648 6bad629b 2019-02-04 stsp goto done;
1650 6bad629b 2019-02-04 stsp usage_status();
1652 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1653 6bad629b 2019-02-04 stsp if (error != NULL)
1654 6bad629b 2019-02-04 stsp goto done;
1656 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1657 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(worktree));
1658 6bad629b 2019-02-04 stsp if (error)
1659 6bad629b 2019-02-04 stsp goto done;
1661 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1662 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1664 927df6b7 2019-02-10 stsp free(cwd);
1665 927df6b7 2019-02-10 stsp free(path);
1666 d0eebce4 2019-03-11 stsp return error;
1669 d0eebce4 2019-03-11 stsp __dead static void
1670 d0eebce4 2019-03-11 stsp usage_ref(void)
1672 d0eebce4 2019-03-11 stsp fprintf(stderr,
1673 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1674 d0eebce4 2019-03-11 stsp getprogname());
1678 d0eebce4 2019-03-11 stsp static const struct got_error *
1679 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1681 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1682 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1683 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1685 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1686 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1688 d0eebce4 2019-03-11 stsp return err;
1690 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1691 d0eebce4 2019-03-11 stsp char *refstr;
1692 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1693 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1694 d0eebce4 2019-03-11 stsp return got_error_from_errno();
1695 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1696 d0eebce4 2019-03-11 stsp free(refstr);
1699 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1700 d0eebce4 2019-03-11 stsp return NULL;
1703 d0eebce4 2019-03-11 stsp static const struct got_error *
1704 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1706 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1707 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1709 d0eebce4 2019-03-11 stsp err = got_ref_open(&ref, repo, refname);
1711 d0eebce4 2019-03-11 stsp return err;
1713 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1714 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1715 d0eebce4 2019-03-11 stsp return err;
1718 d0eebce4 2019-03-11 stsp static const struct got_error *
1719 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1721 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1722 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1723 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1725 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1727 d0eebce4 2019-03-11 stsp return err;
1729 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1731 d0eebce4 2019-03-11 stsp goto done;
1733 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1736 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 cmd_ref(int argc, char *argv[])
1744 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1745 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1746 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1747 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1748 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1749 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1751 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1752 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1753 d0eebce4 2019-03-11 stsp switch (ch) {
1755 d0eebce4 2019-03-11 stsp delref = optarg;
1758 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1759 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1760 d0eebce4 2019-03-11 stsp err(1, "-r option");
1763 d0eebce4 2019-03-11 stsp do_list = 1;
1766 d0eebce4 2019-03-11 stsp usage_ref();
1767 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1771 d0eebce4 2019-03-11 stsp if (do_list && delref)
1772 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1774 d0eebce4 2019-03-11 stsp argc -= optind;
1775 d0eebce4 2019-03-11 stsp argv += optind;
1777 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1778 d0eebce4 2019-03-11 stsp if (argc > 0)
1779 d0eebce4 2019-03-11 stsp usage_ref();
1780 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1781 d0eebce4 2019-03-11 stsp usage_ref();
1783 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1784 e0b57350 2019-03-12 stsp if (do_list) {
1785 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1786 e0b57350 2019-03-12 stsp NULL) == -1)
1787 e0b57350 2019-03-12 stsp err(1, "pledge");
1789 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1790 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1791 e0b57350 2019-03-12 stsp err(1, "pledge");
1794 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1795 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1796 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1797 d0eebce4 2019-03-11 stsp goto done;
1800 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1801 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1802 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1803 d0eebce4 2019-03-11 stsp goto done;
1805 d0eebce4 2019-03-11 stsp error = NULL;
1806 d0eebce4 2019-03-11 stsp if (worktree) {
1807 d0eebce4 2019-03-11 stsp repo_path =
1808 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1809 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1810 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1811 d0eebce4 2019-03-11 stsp if (error)
1812 d0eebce4 2019-03-11 stsp goto done;
1814 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1815 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1816 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1817 d0eebce4 2019-03-11 stsp goto done;
1822 d0eebce4 2019-03-11 stsp error = apply_unveil(repo_path, do_list,
1823 d0eebce4 2019-03-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1824 d0eebce4 2019-03-11 stsp if (error)
1825 d0eebce4 2019-03-11 stsp goto done;
1827 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1828 d0eebce4 2019-03-11 stsp if (error != NULL)
1829 d0eebce4 2019-03-11 stsp goto done;
1831 d0eebce4 2019-03-11 stsp if (do_list)
1832 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1833 d0eebce4 2019-03-11 stsp else if (delref)
1834 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1836 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1839 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1840 d0eebce4 2019-03-11 stsp if (worktree)
1841 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1842 d0eebce4 2019-03-11 stsp free(cwd);
1843 d0eebce4 2019-03-11 stsp free(repo_path);
1844 d00136be 2019-03-26 stsp return error;
1847 d00136be 2019-03-26 stsp __dead static void
1848 d00136be 2019-03-26 stsp usage_add(void)
1850 d00136be 2019-03-26 stsp fprintf(stderr, "usage: %s add file-path\n", getprogname());
1854 d00136be 2019-03-26 stsp static const struct got_error *
1855 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1857 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1858 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1859 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1860 d00136be 2019-03-26 stsp char *cwd = NULL, *path = NULL, *relpath = NULL;
1863 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1864 d00136be 2019-03-26 stsp switch (ch) {
1866 d00136be 2019-03-26 stsp usage_add();
1867 d00136be 2019-03-26 stsp /* NOTREACHED */
1871 d00136be 2019-03-26 stsp argc -= optind;
1872 d00136be 2019-03-26 stsp argv += optind;
1874 d00136be 2019-03-26 stsp if (argc != 1)
1875 d00136be 2019-03-26 stsp usage_add();
1877 d00136be 2019-03-26 stsp path = realpath(argv[0], NULL);
1878 d00136be 2019-03-26 stsp if (path == NULL) {
1879 d00136be 2019-03-26 stsp error = got_error_from_errno();
1880 d00136be 2019-03-26 stsp goto done;
1883 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1884 d00136be 2019-03-26 stsp if (cwd == NULL) {
1885 d00136be 2019-03-26 stsp error = got_error_from_errno();
1886 d00136be 2019-03-26 stsp goto done;
1888 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1889 d00136be 2019-03-26 stsp if (error)
1890 d00136be 2019-03-26 stsp goto done;
1892 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1893 031a5338 2019-03-26 stsp if (error != NULL)
1894 031a5338 2019-03-26 stsp goto done;
1896 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1897 031a5338 2019-03-26 stsp got_worktree_get_root_path(worktree));
1898 d00136be 2019-03-26 stsp if (error)
1899 d00136be 2019-03-26 stsp goto done;
1901 031a5338 2019-03-26 stsp error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1903 d00136be 2019-03-26 stsp if (error)
1904 d00136be 2019-03-26 stsp goto done;
1907 031a5338 2019-03-26 stsp got_repo_close(repo);
1908 d00136be 2019-03-26 stsp if (worktree)
1909 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1910 d00136be 2019-03-26 stsp free(path);
1911 d00136be 2019-03-26 stsp free(relpath);
1912 2ec1f75b 2019-03-26 stsp free(cwd);
1913 2ec1f75b 2019-03-26 stsp return error;
1916 2ec1f75b 2019-03-26 stsp __dead static void
1917 2ec1f75b 2019-03-26 stsp usage_rm(void)
1919 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1923 2ec1f75b 2019-03-26 stsp static const struct got_error *
1924 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
1926 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
1927 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
1928 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
1929 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
1930 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
1932 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
1933 2ec1f75b 2019-03-26 stsp switch (ch) {
1935 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
1938 2ec1f75b 2019-03-26 stsp usage_add();
1939 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
1943 2ec1f75b 2019-03-26 stsp argc -= optind;
1944 2ec1f75b 2019-03-26 stsp argv += optind;
1946 2ec1f75b 2019-03-26 stsp if (argc != 1)
1947 2ec1f75b 2019-03-26 stsp usage_rm();
1949 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
1950 2ec1f75b 2019-03-26 stsp if (path == NULL) {
1951 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1952 2ec1f75b 2019-03-26 stsp goto done;
1955 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
1956 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
1957 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1958 2ec1f75b 2019-03-26 stsp goto done;
1960 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1961 2ec1f75b 2019-03-26 stsp if (error)
1962 2ec1f75b 2019-03-26 stsp goto done;
1964 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1965 2ec1f75b 2019-03-26 stsp if (error != NULL)
1966 2ec1f75b 2019-03-26 stsp goto done;
1968 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1969 c2253644 2019-03-26 stsp got_worktree_get_root_path(worktree));
1970 2ec1f75b 2019-03-26 stsp if (error)
1971 2ec1f75b 2019-03-26 stsp goto done;
1973 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1974 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
1975 2ec1f75b 2019-03-26 stsp if (error)
1976 2ec1f75b 2019-03-26 stsp goto done;
1979 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
1980 2ec1f75b 2019-03-26 stsp if (worktree)
1981 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
1982 2ec1f75b 2019-03-26 stsp free(path);
1983 d00136be 2019-03-26 stsp free(cwd);
1984 6bad629b 2019-02-04 stsp return error;