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>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 5c860e29 2018-03-12 stsp #include <locale.h>
27 99437157 2018-11-11 stsp #include <signal.h>
28 5c860e29 2018-03-12 stsp #include <stdio.h>
29 5c860e29 2018-03-12 stsp #include <stdlib.h>
30 5c860e29 2018-03-12 stsp #include <string.h>
31 5c860e29 2018-03-12 stsp #include <unistd.h>
32 c09a553d 2018-03-12 stsp #include <libgen.h>
33 c0768b0f 2018-06-10 stsp #include <time.h>
35 f42b1b34 2018-03-12 stsp #include "got_error.h"
36 f42b1b34 2018-03-12 stsp #include "got_object.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 f42b1b34 2018-03-12 stsp #include "got_repository.h"
39 1dd54920 2019-05-11 stsp #include "got_path.h"
40 c09a553d 2018-03-12 stsp #include "got_worktree.h"
41 79109fed 2018-03-27 stsp #include "got_diff.h"
42 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
43 404c43c4 2018-06-21 stsp #include "got_blame.h"
44 63219cd2 2019-01-04 stsp #include "got_privsep.h"
46 5c860e29 2018-03-12 stsp #ifndef nitems
47 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
51 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
54 99437157 2018-11-11 stsp catch_sigint(int signo)
56 99437157 2018-11-11 stsp sigint_received = 1;
60 99437157 2018-11-11 stsp catch_sigpipe(int signo)
62 99437157 2018-11-11 stsp sigpipe_received = 1;
66 5c860e29 2018-03-12 stsp struct cmd {
67 5c860e29 2018-03-12 stsp const char *cmd_name;
68 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
69 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
70 46a0db7d 2018-03-12 stsp const char *cmd_descr;
73 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
75 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
78 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
79 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
80 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
81 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
82 d00136be 2019-03-26 stsp __dead static void usage_add(void);
83 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
84 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
85 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
88 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
90 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
91 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
92 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
94 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
95 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
96 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
97 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
98 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
100 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
101 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
102 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
103 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
104 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
105 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
106 1b6b95a8 2018-03-12 stsp "show repository history" },
107 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
108 b00d56cd 2018-04-01 stsp "compare files and directories" },
109 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
110 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
111 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
112 a67e2392 2019-03-26 stsp "list files and directories in repository" },
113 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
114 1b6b95a8 2018-03-12 stsp "show modification status of files" },
115 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
116 d0eebce4 2019-03-11 stsp "manage references in repository" },
117 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
118 d00136be 2019-03-26 stsp "add a new file to version control" },
119 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
120 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
121 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
122 a129376b 2019-03-28 stsp "revert uncommitted changes" },
123 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
124 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
128 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
130 5c860e29 2018-03-12 stsp struct cmd *cmd;
131 5c860e29 2018-03-12 stsp unsigned int i;
133 1b6b95a8 2018-03-12 stsp int hflag = 0;
135 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
137 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
138 5c860e29 2018-03-12 stsp switch (ch) {
144 5c860e29 2018-03-12 stsp /* NOTREACHED */
148 5c860e29 2018-03-12 stsp argc -= optind;
149 5c860e29 2018-03-12 stsp argv += optind;
150 1e70621d 2018-03-27 stsp optind = 0;
152 5c860e29 2018-03-12 stsp if (argc <= 0)
155 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
156 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
158 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
159 d7d4f210 2018-03-12 stsp const struct got_error *error;
161 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
163 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
167 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
169 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
170 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
171 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
178 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 4ed7e80c 2018-05-20 stsp __dead static void
183 5c860e29 2018-03-12 stsp usage(void)
187 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
188 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
189 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
190 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
191 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
196 0266afb7 2019-01-04 stsp static const struct got_error *
197 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
198 a0937847 2019-05-11 stsp const char *worktree_path, int create_worktree)
200 0266afb7 2019-01-04 stsp const struct got_error *error;
201 3c45a30a 2019-05-12 jcs static char err_msg[MAXPATHLEN + 36];
203 a0937847 2019-05-11 stsp if (create_worktree) {
204 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
205 a0937847 2019-05-11 stsp error = got_path_mkdir(worktree_path);
207 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
208 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
210 3c45a30a 2019-05-12 jcs error = NULL;
212 3c45a30a 2019-05-12 jcs snprintf(err_msg, sizeof(err_msg),
213 3c45a30a 2019-05-12 jcs "%s: directory exists but is not empty",
214 3c45a30a 2019-05-12 jcs worktree_path);
215 3c45a30a 2019-05-12 jcs error = got_error_msg(GOT_ERR_BAD_PATH,
220 a0937847 2019-05-11 stsp if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
221 a0937847 2019-05-11 stsp return error;
224 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
225 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", repo_path);
227 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
228 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", worktree_path);
230 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
231 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", "/tmp");
233 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
234 0266afb7 2019-01-04 stsp if (error != NULL)
235 0266afb7 2019-01-04 stsp return error;
237 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
238 230a42bd 2019-05-11 jcs return got_error_prefix_errno("unveil");
240 0266afb7 2019-01-04 stsp return NULL;
243 4ed7e80c 2018-05-20 stsp __dead static void
244 c09a553d 2018-03-12 stsp usage_checkout(void)
246 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
247 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
251 92a684f4 2018-03-12 stsp static void
252 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
254 92a684f4 2018-03-12 stsp char *worktree_path = arg;
256 92a684f4 2018-03-12 stsp while (path[0] == '/')
259 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
262 99437157 2018-11-11 stsp static const struct got_error *
263 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
265 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
266 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
267 99437157 2018-11-11 stsp return NULL;
270 8069f636 2019-01-12 stsp static const struct got_error *
271 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
272 8069f636 2019-01-12 stsp struct got_repository *repo)
274 8069f636 2019-01-12 stsp const struct got_error *err;
275 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
276 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
277 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
279 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo,
280 2f17228e 2019-05-12 stsp got_worktree_get_head_ref_name(worktree), 0);
282 36a38700 2019-05-10 stsp return err;
284 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
285 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
289 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
293 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
297 8069f636 2019-01-12 stsp struct got_object_id *id;
299 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
302 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
304 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
305 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
308 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
310 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
316 8069f636 2019-01-12 stsp if (id == NULL)
318 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
322 8069f636 2019-01-12 stsp if (head_ref)
323 8069f636 2019-01-12 stsp got_ref_close(head_ref);
325 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
326 8069f636 2019-01-12 stsp return err;
330 4ed7e80c 2018-05-20 stsp static const struct got_error *
331 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
333 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
334 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
335 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
336 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
337 c09a553d 2018-03-12 stsp char *repo_path = NULL;
338 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
339 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
340 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
341 72151b04 2019-05-11 stsp int ch, same_path_prefix;
343 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
344 0bb8a95e 2018-03-12 stsp switch (ch) {
346 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
347 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
348 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
351 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
354 2deda0b9 2019-03-07 stsp usage_checkout();
355 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
359 0bb8a95e 2018-03-12 stsp argc -= optind;
360 0bb8a95e 2018-03-12 stsp argv += optind;
362 6715a751 2018-03-16 stsp #ifndef PROFILE
363 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
364 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
365 c09a553d 2018-03-12 stsp err(1, "pledge");
367 0bb8a95e 2018-03-12 stsp if (argc == 1) {
368 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
369 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
370 76089277 2018-04-01 stsp if (repo_path == NULL)
371 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
372 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
373 76089277 2018-04-01 stsp if (cwd == NULL) {
374 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
377 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
378 230a42bd 2019-05-11 jcs base = basename(path_prefix);
379 230a42bd 2019-05-11 jcs if (base == NULL) {
380 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
381 230a42bd 2019-05-11 jcs path_prefix);
385 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
386 230a42bd 2019-05-11 jcs if (base == NULL) {
387 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
392 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
393 c09a553d 2018-03-12 stsp if (dotgit)
394 c09a553d 2018-03-12 stsp *dotgit = '\0';
395 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
396 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
401 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
402 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
403 76089277 2018-04-01 stsp if (repo_path == NULL) {
404 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
407 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
408 76089277 2018-04-01 stsp if (worktree_path == NULL) {
409 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[1]);
413 c09a553d 2018-03-12 stsp usage_checkout();
415 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
416 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
418 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
419 c09a553d 2018-03-12 stsp if (error != NULL)
422 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
426 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
427 c09a553d 2018-03-12 stsp if (error != NULL)
430 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
431 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
434 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
435 c09a553d 2018-03-12 stsp if (error != NULL)
438 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
439 e5dc7198 2018-12-29 stsp path_prefix);
440 e5dc7198 2018-12-29 stsp if (error != NULL)
442 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
443 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
447 8069f636 2019-01-12 stsp if (commit_id_str) {
448 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
449 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
450 8069f636 2019-01-12 stsp commit_id_str);
451 8069f636 2019-01-12 stsp if (error != NULL)
453 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
454 8069f636 2019-01-12 stsp if (error != NULL) {
455 8069f636 2019-01-12 stsp free(commit_id);
458 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
459 8069f636 2019-01-12 stsp commit_id);
460 8069f636 2019-01-12 stsp free(commit_id);
465 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
466 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
467 c09a553d 2018-03-12 stsp if (error != NULL)
470 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
473 8069f636 2019-01-12 stsp free(commit_id_str);
474 76089277 2018-04-01 stsp free(repo_path);
475 507dc3bb 2018-12-29 stsp free(worktree_path);
476 507dc3bb 2018-12-29 stsp return error;
479 507dc3bb 2018-12-29 stsp __dead static void
480 507dc3bb 2018-12-29 stsp usage_update(void)
482 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
483 507dc3bb 2018-12-29 stsp getprogname());
487 507dc3bb 2018-12-29 stsp static void
488 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
490 784955db 2019-01-12 stsp int *did_something = arg;
492 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
495 1545c615 2019-02-10 stsp *did_something = 1;
496 507dc3bb 2018-12-29 stsp while (path[0] == '/')
498 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
501 be7061eb 2018-12-30 stsp static const struct got_error *
502 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
504 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
505 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
506 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
507 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
508 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
509 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
510 784955db 2019-01-12 stsp int ch, did_something = 0;
512 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
513 507dc3bb 2018-12-29 stsp switch (ch) {
515 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
516 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
517 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
520 2deda0b9 2019-03-07 stsp usage_update();
521 507dc3bb 2018-12-29 stsp /* NOTREACHED */
525 507dc3bb 2018-12-29 stsp argc -= optind;
526 507dc3bb 2018-12-29 stsp argv += optind;
528 507dc3bb 2018-12-29 stsp #ifndef PROFILE
529 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
530 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
531 507dc3bb 2018-12-29 stsp err(1, "pledge");
533 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
534 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
535 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
538 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
542 507dc3bb 2018-12-29 stsp if (argc == 0) {
543 c4cdcb68 2019-04-03 stsp path = strdup("");
544 c4cdcb68 2019-04-03 stsp if (path == NULL) {
545 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
548 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
549 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
553 507dc3bb 2018-12-29 stsp usage_update();
555 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
556 507dc3bb 2018-12-29 stsp if (error != NULL)
559 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
560 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
564 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
565 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
566 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
567 507dc3bb 2018-12-29 stsp if (error != NULL)
569 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
570 9c4b8182 2019-01-02 stsp if (error != NULL)
572 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
573 507dc3bb 2018-12-29 stsp if (error != NULL)
576 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
577 507dc3bb 2018-12-29 stsp commit_id_str);
578 507dc3bb 2018-12-29 stsp if (error != NULL)
582 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
583 be7061eb 2018-12-30 stsp if (error != NULL)
586 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
587 507dc3bb 2018-12-29 stsp commit_id) != 0) {
588 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
589 507dc3bb 2018-12-29 stsp commit_id);
594 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
595 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
596 507dc3bb 2018-12-29 stsp if (error != NULL)
599 784955db 2019-01-12 stsp if (did_something)
600 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
602 784955db 2019-01-12 stsp printf("Already up-to-date\n");
604 c09a553d 2018-03-12 stsp free(worktree_path);
605 c4cdcb68 2019-04-03 stsp free(path);
606 507dc3bb 2018-12-29 stsp free(commit_id);
607 9c4b8182 2019-01-02 stsp free(commit_id_str);
608 c09a553d 2018-03-12 stsp return error;
611 f42b1b34 2018-03-12 stsp static const struct got_error *
612 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
613 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
615 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
616 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
617 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
618 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
620 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
621 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
623 79109fed 2018-03-27 stsp return err;
625 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
626 79f35eb3 2018-06-11 stsp if (qid != NULL) {
627 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
629 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
631 79109fed 2018-03-27 stsp return err;
633 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
634 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
635 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
637 0f2b3dca 2018-12-22 stsp return err;
639 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
641 79109fed 2018-03-27 stsp return err;
644 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
648 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
649 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
652 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
653 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
654 0f2b3dca 2018-12-22 stsp free(id_str1);
655 0f2b3dca 2018-12-22 stsp free(id_str2);
656 79109fed 2018-03-27 stsp return err;
659 4bb494d5 2018-06-16 stsp static char *
660 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
662 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
663 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
669 79109fed 2018-03-27 stsp static const struct got_error *
670 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
671 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
672 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
674 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
675 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
676 6c281f94 2018-06-11 stsp char datebuf[26];
677 45d799e2 2018-12-23 stsp time_t committer_time;
678 45d799e2 2018-12-23 stsp const char *author, *committer;
679 199a4027 2019-02-02 stsp char *refs_str = NULL;
680 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
682 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
684 199a4027 2019-02-02 stsp const char *name;
685 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
687 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
688 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
690 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
692 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
694 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
696 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
698 199a4027 2019-02-02 stsp s = refs_str;
699 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
700 199a4027 2019-02-02 stsp name) == -1) {
701 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
707 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
709 8bf5b3c9 2018-03-17 stsp return err;
711 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
712 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
713 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
714 832c249c 2018-06-10 stsp free(id_str);
715 d3d493d7 2019-02-21 stsp id_str = NULL;
716 d3d493d7 2019-02-21 stsp free(refs_str);
717 d3d493d7 2019-02-21 stsp refs_str = NULL;
718 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
719 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
720 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
721 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
722 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
723 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
724 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
725 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
726 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
727 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
728 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
730 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
731 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
732 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
734 a0603db2 2018-06-10 stsp return err;
735 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
736 a0603db2 2018-06-10 stsp free(id_str);
740 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
741 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
742 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
744 621015ac 2018-07-23 stsp logmsg = logmsg0;
746 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
748 832c249c 2018-06-10 stsp printf(" %s\n", line);
749 832c249c 2018-06-10 stsp } while (line);
750 621015ac 2018-07-23 stsp free(logmsg0);
752 971751ac 2018-03-27 stsp if (show_patch) {
753 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
754 971751ac 2018-03-27 stsp if (err == 0)
755 971751ac 2018-03-27 stsp printf("\n");
758 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
759 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fflush");
760 07862c20 2018-09-15 stsp return err;
763 f42b1b34 2018-03-12 stsp static const struct got_error *
764 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
765 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
766 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
768 f42b1b34 2018-03-12 stsp const struct got_error *err;
769 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
771 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
772 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
774 f42b1b34 2018-03-12 stsp return err;
775 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
779 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
780 372ccdbb 2018-06-10 stsp struct got_object_id *id;
782 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
785 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
787 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
788 9ba79e04 2018-06-11 stsp err = NULL;
791 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
793 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
799 b43fbaa0 2018-06-11 stsp if (id == NULL)
802 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
805 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
807 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
808 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
812 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
813 f42b1b34 2018-03-12 stsp return err;
816 4ed7e80c 2018-05-20 stsp __dead static void
817 6f3d1eb0 2018-03-12 stsp usage_log(void)
819 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
820 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
824 4ed7e80c 2018-05-20 stsp static const struct got_error *
825 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
827 f42b1b34 2018-03-12 stsp const struct got_error *error;
828 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
829 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
830 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
831 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
832 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
833 d142fc45 2018-04-01 stsp char *start_commit = NULL;
834 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
835 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
836 64a96a6d 2018-04-01 stsp const char *errstr;
837 199a4027 2019-02-02 stsp struct got_reflist_head refs;
839 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
841 6715a751 2018-03-16 stsp #ifndef PROFILE
842 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
845 f42b1b34 2018-03-12 stsp err(1, "pledge");
848 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
849 79109fed 2018-03-27 stsp switch (ch) {
851 79109fed 2018-03-27 stsp show_patch = 1;
854 d142fc45 2018-04-01 stsp start_commit = optarg;
857 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
859 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
860 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
863 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
864 64a96a6d 2018-04-01 stsp if (errstr != NULL)
865 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
868 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
871 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
872 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
873 04ca23f4 2018-07-16 stsp err(1, "-r option");
874 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
877 2deda0b9 2019-03-07 stsp usage_log();
878 79109fed 2018-03-27 stsp /* NOTREACHED */
882 79109fed 2018-03-27 stsp argc -= optind;
883 79109fed 2018-03-27 stsp argv += optind;
885 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
886 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
887 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
891 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
892 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
894 cffc0aa4 2019-02-05 stsp error = NULL;
896 e7301579 2019-03-18 stsp if (argc == 0) {
897 cbd1af7a 2019-03-18 stsp path = strdup("");
898 e7301579 2019-03-18 stsp if (path == NULL) {
899 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
902 e7301579 2019-03-18 stsp } else if (argc == 1) {
903 e7301579 2019-03-18 stsp if (worktree) {
904 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
909 e7301579 2019-03-18 stsp path = strdup(argv[0]);
910 e7301579 2019-03-18 stsp if (path == NULL) {
911 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
916 cbd1af7a 2019-03-18 stsp usage_log();
918 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
919 5486daa2 2019-05-11 stsp repo_path = worktree ?
920 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
922 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
923 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
927 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
928 d7d4f210 2018-03-12 stsp if (error != NULL)
931 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
932 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
936 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
937 3235492e 2018-04-01 stsp struct got_reference *head_ref;
938 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
939 3235492e 2018-04-01 stsp if (error != NULL)
940 3235492e 2018-04-01 stsp return error;
941 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
942 3235492e 2018-04-01 stsp got_ref_close(head_ref);
943 3235492e 2018-04-01 stsp if (error != NULL)
944 3235492e 2018-04-01 stsp return error;
945 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
947 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
948 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
949 3235492e 2018-04-01 stsp if (error == NULL) {
950 1caad61b 2019-02-01 stsp int obj_type;
951 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
952 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
953 d1f2edc9 2018-06-13 stsp if (error != NULL)
955 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
956 1caad61b 2019-02-01 stsp if (error != NULL)
958 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
959 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
960 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
961 1caad61b 2019-02-01 stsp if (error != NULL)
963 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
964 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
965 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
966 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
970 1caad61b 2019-02-01 stsp id = got_object_id_dup(
971 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
972 1caad61b 2019-02-01 stsp if (id == NULL)
973 230a42bd 2019-05-11 jcs error = got_error_prefix_errno(
974 230a42bd 2019-05-11 jcs "got_object_id_dup");
975 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
978 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
979 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
982 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
983 d1f2edc9 2018-06-13 stsp if (error != NULL)
986 15a94983 2018-12-23 stsp if (commit == NULL) {
987 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
988 d1f2edc9 2018-06-13 stsp start_commit);
989 d1f2edc9 2018-06-13 stsp if (error != NULL)
990 d1f2edc9 2018-06-13 stsp return error;
993 d7d4f210 2018-03-12 stsp if (error != NULL)
996 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
997 04ca23f4 2018-07-16 stsp if (error != NULL)
999 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1000 04ca23f4 2018-07-16 stsp free(path);
1001 04ca23f4 2018-07-16 stsp path = in_repo_path;
1004 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1005 199a4027 2019-02-02 stsp if (error)
1006 199a4027 2019-02-02 stsp goto done;
1008 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1009 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1011 04ca23f4 2018-07-16 stsp free(path);
1012 04ca23f4 2018-07-16 stsp free(repo_path);
1013 04ca23f4 2018-07-16 stsp free(cwd);
1015 cffc0aa4 2019-02-05 stsp if (worktree)
1016 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1017 ad242220 2018-09-08 stsp if (repo) {
1018 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1019 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1020 ad242220 2018-09-08 stsp if (error == NULL)
1021 ad242220 2018-09-08 stsp error = repo_error;
1023 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1024 8bf5b3c9 2018-03-17 stsp return error;
1027 4ed7e80c 2018-05-20 stsp __dead static void
1028 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1030 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1031 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1035 b72f483a 2019-02-05 stsp struct print_diff_arg {
1036 b72f483a 2019-02-05 stsp struct got_repository *repo;
1037 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1038 b72f483a 2019-02-05 stsp int diff_context;
1039 3fc0c068 2019-02-10 stsp const char *id_str;
1040 3fc0c068 2019-02-10 stsp int header_shown;
1043 4ed7e80c 2018-05-20 stsp static const struct got_error *
1044 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1045 b72f483a 2019-02-05 stsp struct got_object_id *id)
1047 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1048 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1049 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1050 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1051 b72f483a 2019-02-05 stsp char *abspath = NULL;
1052 b72f483a 2019-02-05 stsp struct stat sb;
1054 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1055 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1056 b72f483a 2019-02-05 stsp return NULL;
1058 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1059 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1060 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1061 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1064 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1065 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1067 d00136be 2019-03-26 stsp goto done;
1071 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1072 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1073 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1074 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1075 2ec1f75b 2019-03-26 stsp goto done;
1078 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1079 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1080 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", abspath);
1081 2ec1f75b 2019-03-26 stsp goto done;
1083 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1084 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", abspath);
1085 2ec1f75b 2019-03-26 stsp goto done;
1088 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1090 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1093 b72f483a 2019-02-05 stsp if (blob1)
1094 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1095 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1096 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
1097 b72f483a 2019-02-05 stsp free(abspath);
1098 927df6b7 2019-02-10 stsp return err;
1101 927df6b7 2019-02-10 stsp static const struct got_error *
1102 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1104 b00d56cd 2018-04-01 stsp const struct got_error *error;
1105 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1106 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1107 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1108 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1109 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1110 15a94983 2018-12-23 stsp int type1, type2;
1111 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1112 c0cc5c62 2018-10-18 stsp const char *errstr;
1113 927df6b7 2019-02-10 stsp char *path = NULL;
1115 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1116 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1117 25eccc22 2019-01-04 stsp NULL) == -1)
1118 b00d56cd 2018-04-01 stsp err(1, "pledge");
1121 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1122 b00d56cd 2018-04-01 stsp switch (ch) {
1124 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1125 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1126 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1129 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1130 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1131 b72f483a 2019-02-05 stsp err(1, "-r option");
1132 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1135 2deda0b9 2019-03-07 stsp usage_diff();
1136 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1140 b00d56cd 2018-04-01 stsp argc -= optind;
1141 b00d56cd 2018-04-01 stsp argv += optind;
1143 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1144 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1145 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1146 b72f483a 2019-02-05 stsp goto done;
1148 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1149 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1150 927df6b7 2019-02-10 stsp goto done;
1151 927df6b7 2019-02-10 stsp if (argc <= 1) {
1152 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1153 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1154 927df6b7 2019-02-10 stsp goto done;
1156 b72f483a 2019-02-05 stsp if (repo_path)
1158 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1159 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1160 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1161 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1162 927df6b7 2019-02-10 stsp goto done;
1164 927df6b7 2019-02-10 stsp if (argc == 1) {
1165 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1167 927df6b7 2019-02-10 stsp if (error)
1168 927df6b7 2019-02-10 stsp goto done;
1170 927df6b7 2019-02-10 stsp path = strdup("");
1171 927df6b7 2019-02-10 stsp if (path == NULL) {
1172 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1173 927df6b7 2019-02-10 stsp goto done;
1176 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1177 15a94983 2018-12-23 stsp id_str1 = argv[0];
1178 15a94983 2018-12-23 stsp id_str2 = argv[1];
1180 b00d56cd 2018-04-01 stsp usage_diff();
1182 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1183 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1184 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1185 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
1188 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1189 76089277 2018-04-01 stsp free(repo_path);
1190 b00d56cd 2018-04-01 stsp if (error != NULL)
1191 b00d56cd 2018-04-01 stsp goto done;
1193 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1194 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1195 c02c541e 2019-03-29 stsp if (error)
1196 c02c541e 2019-03-29 stsp goto done;
1198 b72f483a 2019-02-05 stsp if (worktree) {
1199 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1200 b72f483a 2019-02-05 stsp char *id_str;
1201 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1202 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1203 b72f483a 2019-02-05 stsp if (error)
1204 b72f483a 2019-02-05 stsp goto done;
1205 b72f483a 2019-02-05 stsp arg.repo = repo;
1206 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1207 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1208 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1209 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1211 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1212 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1213 b72f483a 2019-02-05 stsp free(id_str);
1214 b72f483a 2019-02-05 stsp goto done;
1217 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1218 6402fb3c 2018-09-15 stsp if (error)
1219 b00d56cd 2018-04-01 stsp goto done;
1221 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1222 6402fb3c 2018-09-15 stsp if (error)
1223 b00d56cd 2018-04-01 stsp goto done;
1225 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1226 15a94983 2018-12-23 stsp if (error)
1227 15a94983 2018-12-23 stsp goto done;
1229 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1230 15a94983 2018-12-23 stsp if (error)
1231 15a94983 2018-12-23 stsp goto done;
1233 15a94983 2018-12-23 stsp if (type1 != type2) {
1234 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1235 b00d56cd 2018-04-01 stsp goto done;
1238 15a94983 2018-12-23 stsp switch (type1) {
1239 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1240 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1241 54156555 2018-12-24 stsp diff_context, repo, stdout);
1243 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1244 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1245 54156555 2018-12-24 stsp diff_context, repo, stdout);
1247 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1248 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1250 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1251 c0cc5c62 2018-10-18 stsp repo, stdout);
1254 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1258 15a94983 2018-12-23 stsp free(id1);
1259 15a94983 2018-12-23 stsp free(id2);
1260 927df6b7 2019-02-10 stsp free(path);
1261 b72f483a 2019-02-05 stsp if (worktree)
1262 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1263 ad242220 2018-09-08 stsp if (repo) {
1264 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1265 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1266 ad242220 2018-09-08 stsp if (error == NULL)
1267 ad242220 2018-09-08 stsp error = repo_error;
1269 b00d56cd 2018-04-01 stsp return error;
1272 404c43c4 2018-06-21 stsp __dead static void
1273 404c43c4 2018-06-21 stsp usage_blame(void)
1275 9270e621 2019-02-05 stsp fprintf(stderr,
1276 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1277 404c43c4 2018-06-21 stsp getprogname());
1281 404c43c4 2018-06-21 stsp static const struct got_error *
1282 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1284 404c43c4 2018-06-21 stsp const struct got_error *error;
1285 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1286 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1287 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1288 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1289 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1292 404c43c4 2018-06-21 stsp #ifndef PROFILE
1293 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1294 36e2fb66 2019-01-04 stsp NULL) == -1)
1295 404c43c4 2018-06-21 stsp err(1, "pledge");
1298 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1299 404c43c4 2018-06-21 stsp switch (ch) {
1301 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1304 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1305 66bea077 2018-08-02 stsp if (repo_path == NULL)
1306 66bea077 2018-08-02 stsp err(1, "-r option");
1307 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1310 2deda0b9 2019-03-07 stsp usage_blame();
1311 404c43c4 2018-06-21 stsp /* NOTREACHED */
1315 404c43c4 2018-06-21 stsp argc -= optind;
1316 404c43c4 2018-06-21 stsp argv += optind;
1318 a39318fd 2018-08-02 stsp if (argc == 1)
1319 404c43c4 2018-06-21 stsp path = argv[0];
1321 404c43c4 2018-06-21 stsp usage_blame();
1323 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1324 66bea077 2018-08-02 stsp if (cwd == NULL) {
1325 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1326 66bea077 2018-08-02 stsp goto done;
1328 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1329 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1330 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1331 66bea077 2018-08-02 stsp goto done;
1333 0c06baac 2019-02-05 stsp error = NULL;
1334 0c06baac 2019-02-05 stsp if (worktree) {
1335 0c06baac 2019-02-05 stsp repo_path =
1336 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1337 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1338 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1339 0c06baac 2019-02-05 stsp if (error)
1340 0c06baac 2019-02-05 stsp goto done;
1342 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1343 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1344 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1345 0c06baac 2019-02-05 stsp goto done;
1350 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1351 c02c541e 2019-03-29 stsp if (error != NULL)
1352 36e2fb66 2019-01-04 stsp goto done;
1354 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1355 c02c541e 2019-03-29 stsp if (error)
1356 404c43c4 2018-06-21 stsp goto done;
1358 0c06baac 2019-02-05 stsp if (worktree) {
1359 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1360 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1361 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1362 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1363 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1364 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1365 6efaaa2d 2019-02-05 stsp path) == -1) {
1366 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1367 0c06baac 2019-02-05 stsp goto done;
1369 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1372 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1374 0c06baac 2019-02-05 stsp if (error)
1375 66bea077 2018-08-02 stsp goto done;
1377 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1378 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1379 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1380 404c43c4 2018-06-21 stsp if (error != NULL)
1381 66bea077 2018-08-02 stsp goto done;
1382 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1383 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1384 404c43c4 2018-06-21 stsp if (error != NULL)
1385 66bea077 2018-08-02 stsp goto done;
1387 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1388 15a94983 2018-12-23 stsp commit_id_str);
1389 404c43c4 2018-06-21 stsp if (error != NULL)
1390 66bea077 2018-08-02 stsp goto done;
1393 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1395 66bea077 2018-08-02 stsp free(in_repo_path);
1396 66bea077 2018-08-02 stsp free(repo_path);
1397 66bea077 2018-08-02 stsp free(cwd);
1398 404c43c4 2018-06-21 stsp free(commit_id);
1399 0c06baac 2019-02-05 stsp if (worktree)
1400 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1401 ad242220 2018-09-08 stsp if (repo) {
1402 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1403 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1404 ad242220 2018-09-08 stsp if (error == NULL)
1405 ad242220 2018-09-08 stsp error = repo_error;
1407 404c43c4 2018-06-21 stsp return error;
1410 5de5890b 2018-10-18 stsp __dead static void
1411 5de5890b 2018-10-18 stsp usage_tree(void)
1413 c1669e2e 2019-01-09 stsp fprintf(stderr,
1414 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1415 5de5890b 2018-10-18 stsp getprogname());
1419 c1669e2e 2019-01-09 stsp static void
1420 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1421 c1669e2e 2019-01-09 stsp const char *root_path)
1423 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1425 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1426 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1429 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1430 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1431 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1434 5de5890b 2018-10-18 stsp static const struct got_error *
1435 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1436 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1437 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1439 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1440 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1441 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1442 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1443 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1445 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1447 5de5890b 2018-10-18 stsp goto done;
1449 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1451 5de5890b 2018-10-18 stsp goto done;
1452 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1453 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1454 5de5890b 2018-10-18 stsp while (te) {
1455 5de5890b 2018-10-18 stsp char *id = NULL;
1457 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1460 5de5890b 2018-10-18 stsp if (show_ids) {
1461 5de5890b 2018-10-18 stsp char *id_str;
1462 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1464 5de5890b 2018-10-18 stsp goto done;
1465 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1466 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1467 5de5890b 2018-10-18 stsp free(id_str);
1468 5de5890b 2018-10-18 stsp goto done;
1470 5de5890b 2018-10-18 stsp free(id_str);
1472 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1475 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1476 c1669e2e 2019-01-09 stsp char *child_path;
1477 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1478 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1479 c1669e2e 2019-01-09 stsp te->name) == -1) {
1480 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1481 c1669e2e 2019-01-09 stsp goto done;
1483 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1484 c1669e2e 2019-01-09 stsp root_path, repo);
1485 c1669e2e 2019-01-09 stsp free(child_path);
1487 c1669e2e 2019-01-09 stsp goto done;
1490 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1494 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1495 5de5890b 2018-10-18 stsp free(tree_id);
1496 5de5890b 2018-10-18 stsp return err;
1499 5de5890b 2018-10-18 stsp static const struct got_error *
1500 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1502 5de5890b 2018-10-18 stsp const struct got_error *error;
1503 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1504 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1505 7a2c19d6 2019-02-05 stsp const char *path;
1506 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1507 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1508 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1509 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1512 5de5890b 2018-10-18 stsp #ifndef PROFILE
1513 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1514 0f8d269b 2019-01-04 stsp NULL) == -1)
1515 5de5890b 2018-10-18 stsp err(1, "pledge");
1518 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1519 5de5890b 2018-10-18 stsp switch (ch) {
1521 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1524 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1525 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1526 5de5890b 2018-10-18 stsp err(1, "-r option");
1527 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1530 5de5890b 2018-10-18 stsp show_ids = 1;
1533 c1669e2e 2019-01-09 stsp recurse = 1;
1536 2deda0b9 2019-03-07 stsp usage_tree();
1537 5de5890b 2018-10-18 stsp /* NOTREACHED */
1541 5de5890b 2018-10-18 stsp argc -= optind;
1542 5de5890b 2018-10-18 stsp argv += optind;
1544 5de5890b 2018-10-18 stsp if (argc == 1)
1545 5de5890b 2018-10-18 stsp path = argv[0];
1546 5de5890b 2018-10-18 stsp else if (argc > 1)
1547 5de5890b 2018-10-18 stsp usage_tree();
1549 7a2c19d6 2019-02-05 stsp path = NULL;
1551 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1552 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1553 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1554 9bf7a39b 2019-02-05 stsp goto done;
1556 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1557 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1558 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1559 7a2c19d6 2019-02-05 stsp goto done;
1561 7a2c19d6 2019-02-05 stsp error = NULL;
1562 7a2c19d6 2019-02-05 stsp if (worktree) {
1563 7a2c19d6 2019-02-05 stsp repo_path =
1564 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1565 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1566 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1567 7a2c19d6 2019-02-05 stsp if (error)
1568 7a2c19d6 2019-02-05 stsp goto done;
1570 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1571 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1572 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1573 7a2c19d6 2019-02-05 stsp goto done;
1578 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1579 5de5890b 2018-10-18 stsp if (error != NULL)
1580 c02c541e 2019-03-29 stsp goto done;
1582 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1583 c02c541e 2019-03-29 stsp if (error)
1584 5de5890b 2018-10-18 stsp goto done;
1586 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1587 9bf7a39b 2019-02-05 stsp if (worktree) {
1588 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1589 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1590 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1591 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1592 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1593 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1594 9bf7a39b 2019-02-05 stsp goto done;
1596 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1598 9bf7a39b 2019-02-05 stsp if (error)
1599 9bf7a39b 2019-02-05 stsp goto done;
1601 9bf7a39b 2019-02-05 stsp path = "/";
1603 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1604 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1605 9bf7a39b 2019-02-05 stsp if (error != NULL)
1606 9bf7a39b 2019-02-05 stsp goto done;
1609 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1610 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1611 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1612 5de5890b 2018-10-18 stsp if (error != NULL)
1613 5de5890b 2018-10-18 stsp goto done;
1614 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1615 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1616 5de5890b 2018-10-18 stsp if (error != NULL)
1617 5de5890b 2018-10-18 stsp goto done;
1619 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1620 15a94983 2018-12-23 stsp commit_id_str);
1621 5de5890b 2018-10-18 stsp if (error != NULL)
1622 5de5890b 2018-10-18 stsp goto done;
1625 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1626 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1628 5de5890b 2018-10-18 stsp free(in_repo_path);
1629 5de5890b 2018-10-18 stsp free(repo_path);
1630 5de5890b 2018-10-18 stsp free(cwd);
1631 5de5890b 2018-10-18 stsp free(commit_id);
1632 7a2c19d6 2019-02-05 stsp if (worktree)
1633 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1634 5de5890b 2018-10-18 stsp if (repo) {
1635 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1636 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1637 5de5890b 2018-10-18 stsp if (error == NULL)
1638 5de5890b 2018-10-18 stsp error = repo_error;
1640 5de5890b 2018-10-18 stsp return error;
1643 6bad629b 2019-02-04 stsp __dead static void
1644 6bad629b 2019-02-04 stsp usage_status(void)
1646 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1650 b72f483a 2019-02-05 stsp static const struct got_error *
1651 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1652 b72f483a 2019-02-05 stsp struct got_object_id *id)
1654 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1655 b72f483a 2019-02-05 stsp return NULL;
1658 6bad629b 2019-02-04 stsp static const struct got_error *
1659 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1661 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1662 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1663 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1664 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1667 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1668 6bad629b 2019-02-04 stsp switch (ch) {
1670 2deda0b9 2019-03-07 stsp usage_status();
1671 6bad629b 2019-02-04 stsp /* NOTREACHED */
1675 6bad629b 2019-02-04 stsp argc -= optind;
1676 6bad629b 2019-02-04 stsp argv += optind;
1678 6bad629b 2019-02-04 stsp #ifndef PROFILE
1679 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1680 6bad629b 2019-02-04 stsp NULL) == -1)
1681 6bad629b 2019-02-04 stsp err(1, "pledge");
1683 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1684 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1685 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1686 927df6b7 2019-02-10 stsp goto done;
1689 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1690 927df6b7 2019-02-10 stsp if (error != NULL)
1691 927df6b7 2019-02-10 stsp goto done;
1693 6bad629b 2019-02-04 stsp if (argc == 0) {
1694 927df6b7 2019-02-10 stsp path = strdup("");
1695 927df6b7 2019-02-10 stsp if (path == NULL) {
1696 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1697 6bad629b 2019-02-04 stsp goto done;
1699 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1700 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1701 927df6b7 2019-02-10 stsp if (error)
1702 6bad629b 2019-02-04 stsp goto done;
1704 6bad629b 2019-02-04 stsp usage_status();
1706 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1707 6bad629b 2019-02-04 stsp if (error != NULL)
1708 6bad629b 2019-02-04 stsp goto done;
1710 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1711 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1712 6bad629b 2019-02-04 stsp if (error)
1713 6bad629b 2019-02-04 stsp goto done;
1715 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1716 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1718 927df6b7 2019-02-10 stsp free(cwd);
1719 927df6b7 2019-02-10 stsp free(path);
1720 d0eebce4 2019-03-11 stsp return error;
1723 d0eebce4 2019-03-11 stsp __dead static void
1724 d0eebce4 2019-03-11 stsp usage_ref(void)
1726 d0eebce4 2019-03-11 stsp fprintf(stderr,
1727 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1728 d0eebce4 2019-03-11 stsp getprogname());
1732 d0eebce4 2019-03-11 stsp static const struct got_error *
1733 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1735 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1736 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1737 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1739 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1740 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1742 d0eebce4 2019-03-11 stsp return err;
1744 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1745 d0eebce4 2019-03-11 stsp char *refstr;
1746 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1747 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1748 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_ref_to_str");
1749 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1750 d0eebce4 2019-03-11 stsp free(refstr);
1753 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1754 d0eebce4 2019-03-11 stsp return NULL;
1757 d0eebce4 2019-03-11 stsp static const struct got_error *
1758 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1760 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1761 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1763 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
1765 d0eebce4 2019-03-11 stsp return err;
1767 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1768 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1769 d0eebce4 2019-03-11 stsp return err;
1772 d0eebce4 2019-03-11 stsp static const struct got_error *
1773 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1775 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1776 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1777 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1779 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1781 d0eebce4 2019-03-11 stsp return err;
1783 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1785 d0eebce4 2019-03-11 stsp goto done;
1787 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1790 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1792 d0eebce4 2019-03-11 stsp return err;
1795 d0eebce4 2019-03-11 stsp static const struct got_error *
1796 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1798 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1799 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1800 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1801 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1802 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1803 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1805 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1806 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1807 d0eebce4 2019-03-11 stsp switch (ch) {
1809 d0eebce4 2019-03-11 stsp delref = optarg;
1812 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1813 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1814 d0eebce4 2019-03-11 stsp err(1, "-r option");
1815 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1818 d0eebce4 2019-03-11 stsp do_list = 1;
1821 d0eebce4 2019-03-11 stsp usage_ref();
1822 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1826 d0eebce4 2019-03-11 stsp if (do_list && delref)
1827 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1829 d0eebce4 2019-03-11 stsp argc -= optind;
1830 d0eebce4 2019-03-11 stsp argv += optind;
1832 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1833 d0eebce4 2019-03-11 stsp if (argc > 0)
1834 d0eebce4 2019-03-11 stsp usage_ref();
1835 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1836 d0eebce4 2019-03-11 stsp usage_ref();
1838 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1839 e0b57350 2019-03-12 stsp if (do_list) {
1840 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1841 e0b57350 2019-03-12 stsp NULL) == -1)
1842 e0b57350 2019-03-12 stsp err(1, "pledge");
1844 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1845 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1846 e0b57350 2019-03-12 stsp err(1, "pledge");
1849 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1850 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1851 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1852 d0eebce4 2019-03-11 stsp goto done;
1855 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1856 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1857 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1858 d0eebce4 2019-03-11 stsp goto done;
1860 d0eebce4 2019-03-11 stsp error = NULL;
1861 d0eebce4 2019-03-11 stsp if (worktree) {
1862 d0eebce4 2019-03-11 stsp repo_path =
1863 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1864 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1865 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1866 d0eebce4 2019-03-11 stsp if (error)
1867 d0eebce4 2019-03-11 stsp goto done;
1869 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1870 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1871 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1872 d0eebce4 2019-03-11 stsp goto done;
1877 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1878 d0eebce4 2019-03-11 stsp if (error != NULL)
1879 d0eebce4 2019-03-11 stsp goto done;
1881 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1882 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1883 c02c541e 2019-03-29 stsp if (error)
1884 c02c541e 2019-03-29 stsp goto done;
1886 d0eebce4 2019-03-11 stsp if (do_list)
1887 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1888 d0eebce4 2019-03-11 stsp else if (delref)
1889 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1891 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1894 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1895 d0eebce4 2019-03-11 stsp if (worktree)
1896 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1897 d0eebce4 2019-03-11 stsp free(cwd);
1898 d0eebce4 2019-03-11 stsp free(repo_path);
1899 d00136be 2019-03-26 stsp return error;
1902 d00136be 2019-03-26 stsp __dead static void
1903 d00136be 2019-03-26 stsp usage_add(void)
1905 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1909 d00136be 2019-03-26 stsp static const struct got_error *
1910 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1912 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1913 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1914 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1915 1dd54920 2019-05-11 stsp char *cwd = NULL;
1916 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
1917 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
1920 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
1922 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1923 d00136be 2019-03-26 stsp switch (ch) {
1925 d00136be 2019-03-26 stsp usage_add();
1926 d00136be 2019-03-26 stsp /* NOTREACHED */
1930 d00136be 2019-03-26 stsp argc -= optind;
1931 d00136be 2019-03-26 stsp argv += optind;
1933 723c305c 2019-05-11 jcs if (argc < 1)
1934 d00136be 2019-03-26 stsp usage_add();
1936 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
1937 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1938 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1939 723c305c 2019-05-11 jcs if (path == NULL) {
1940 723c305c 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[x]);
1943 1dd54920 2019-05-11 stsp free(path);
1946 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1947 d00136be 2019-03-26 stsp if (cwd == NULL) {
1948 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1949 d00136be 2019-03-26 stsp goto done;
1952 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1953 d00136be 2019-03-26 stsp if (error)
1954 d00136be 2019-03-26 stsp goto done;
1956 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1957 031a5338 2019-03-26 stsp if (error != NULL)
1958 031a5338 2019-03-26 stsp goto done;
1960 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1961 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1962 d00136be 2019-03-26 stsp if (error)
1963 d00136be 2019-03-26 stsp goto done;
1965 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1966 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1967 723c305c 2019-05-11 jcs if (path == NULL) {
1968 723c305c 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[x]);
1972 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
1973 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
1974 1dd54920 2019-05-11 stsp if (error) {
1975 1dd54920 2019-05-11 stsp free(path);
1979 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
1980 1dd54920 2019-05-11 stsp NULL, repo);
1983 031a5338 2019-03-26 stsp got_repo_close(repo);
1984 d00136be 2019-03-26 stsp if (worktree)
1985 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1986 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
1987 1dd54920 2019-05-11 stsp free((char *)pe->path);
1988 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
1989 2ec1f75b 2019-03-26 stsp free(cwd);
1990 2ec1f75b 2019-03-26 stsp return error;
1993 2ec1f75b 2019-03-26 stsp __dead static void
1994 2ec1f75b 2019-03-26 stsp usage_rm(void)
1996 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2000 2ec1f75b 2019-03-26 stsp static const struct got_error *
2001 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2003 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2004 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2005 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2006 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
2007 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
2009 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2010 2ec1f75b 2019-03-26 stsp switch (ch) {
2012 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2015 2ec1f75b 2019-03-26 stsp usage_add();
2016 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2020 2ec1f75b 2019-03-26 stsp argc -= optind;
2021 2ec1f75b 2019-03-26 stsp argv += optind;
2023 2ec1f75b 2019-03-26 stsp if (argc != 1)
2024 2ec1f75b 2019-03-26 stsp usage_rm();
2026 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
2027 2ec1f75b 2019-03-26 stsp if (path == NULL) {
2028 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2029 2ec1f75b 2019-03-26 stsp goto done;
2031 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2033 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2034 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2035 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2036 2ec1f75b 2019-03-26 stsp goto done;
2038 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2039 2ec1f75b 2019-03-26 stsp if (error)
2040 2ec1f75b 2019-03-26 stsp goto done;
2042 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2044 2ec1f75b 2019-03-26 stsp goto done;
2046 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2047 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2048 2ec1f75b 2019-03-26 stsp if (error)
2049 2ec1f75b 2019-03-26 stsp goto done;
2051 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2052 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
2053 a129376b 2019-03-28 stsp if (error)
2054 a129376b 2019-03-28 stsp goto done;
2057 a129376b 2019-03-28 stsp got_repo_close(repo);
2058 a129376b 2019-03-28 stsp if (worktree)
2059 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2060 a129376b 2019-03-28 stsp free(path);
2061 a129376b 2019-03-28 stsp free(cwd);
2062 a129376b 2019-03-28 stsp return error;
2065 a129376b 2019-03-28 stsp __dead static void
2066 a129376b 2019-03-28 stsp usage_revert(void)
2068 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2072 a129376b 2019-03-28 stsp static void
2073 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2075 a129376b 2019-03-28 stsp while (path[0] == '/')
2077 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2080 a129376b 2019-03-28 stsp static const struct got_error *
2081 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2083 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2084 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2085 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2086 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2089 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2090 a129376b 2019-03-28 stsp switch (ch) {
2092 a129376b 2019-03-28 stsp usage_revert();
2093 a129376b 2019-03-28 stsp /* NOTREACHED */
2097 a129376b 2019-03-28 stsp argc -= optind;
2098 a129376b 2019-03-28 stsp argv += optind;
2100 a129376b 2019-03-28 stsp if (argc != 1)
2101 a129376b 2019-03-28 stsp usage_revert();
2103 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2104 a129376b 2019-03-28 stsp if (path == NULL) {
2105 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2106 a129376b 2019-03-28 stsp goto done;
2108 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2110 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2111 a129376b 2019-03-28 stsp if (cwd == NULL) {
2112 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2113 a129376b 2019-03-28 stsp goto done;
2115 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2116 2ec1f75b 2019-03-26 stsp if (error)
2117 2ec1f75b 2019-03-26 stsp goto done;
2119 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2120 a129376b 2019-03-28 stsp if (error != NULL)
2121 a129376b 2019-03-28 stsp goto done;
2123 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2124 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2125 a129376b 2019-03-28 stsp if (error)
2126 a129376b 2019-03-28 stsp goto done;
2128 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2129 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2130 a129376b 2019-03-28 stsp if (error)
2131 a129376b 2019-03-28 stsp goto done;
2134 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2135 2ec1f75b 2019-03-26 stsp if (worktree)
2136 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2137 2ec1f75b 2019-03-26 stsp free(path);
2138 d00136be 2019-03-26 stsp free(cwd);
2139 6bad629b 2019-02-04 stsp return error;
2142 c4296144 2019-05-09 stsp __dead static void
2143 c4296144 2019-05-09 stsp usage_commit(void)
2145 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2149 c4296144 2019-05-09 stsp static const struct got_error *
2150 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2152 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2153 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2154 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2155 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2156 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2157 c4296144 2019-05-09 stsp const char *logmsg = "<no log message was specified>";
2158 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2161 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2162 c4296144 2019-05-09 stsp switch (ch) {
2164 c4296144 2019-05-09 stsp logmsg = optarg;
2167 c4296144 2019-05-09 stsp usage_commit();
2168 c4296144 2019-05-09 stsp /* NOTREACHED */
2172 c4296144 2019-05-09 stsp argc -= optind;
2173 c4296144 2019-05-09 stsp argv += optind;
2175 c4296144 2019-05-09 stsp if (argc == 1) {
2176 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2177 c4296144 2019-05-09 stsp if (path == NULL) {
2178 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2179 c4296144 2019-05-09 stsp goto done;
2181 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2182 c4296144 2019-05-09 stsp } else if (argc != 0)
2183 c4296144 2019-05-09 stsp usage_commit();
2185 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2186 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2187 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2188 35bd8fed 2019-05-09 stsp goto done;
2191 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2192 c4296144 2019-05-09 stsp if (cwd == NULL) {
2193 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2194 c4296144 2019-05-09 stsp goto done;
2196 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2197 c4296144 2019-05-09 stsp if (error)
2198 c4296144 2019-05-09 stsp goto done;
2200 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2201 c4296144 2019-05-09 stsp if (error != NULL)
2202 c4296144 2019-05-09 stsp goto done;
2204 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2205 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2206 c4296144 2019-05-09 stsp if (error)
2207 c4296144 2019-05-09 stsp goto done;
2209 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2210 35bd8fed 2019-05-09 stsp logmsg, print_status, NULL, repo);
2211 c4296144 2019-05-09 stsp if (error)
2212 c4296144 2019-05-09 stsp goto done;
2214 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2215 c4296144 2019-05-09 stsp if (error)
2216 c4296144 2019-05-09 stsp goto done;
2217 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2220 c4296144 2019-05-09 stsp got_repo_close(repo);
2221 c4296144 2019-05-09 stsp if (worktree)
2222 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2223 c4296144 2019-05-09 stsp free(path);
2224 c4296144 2019-05-09 stsp free(cwd);
2225 c4296144 2019-05-09 stsp free(id_str);
2226 c4296144 2019-05-09 stsp return error;