2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 63219cd2 2019-01-04 stsp #include "got_privsep.h"
43 a0937847 2019-05-11 stsp #include "got_path.h"
45 5c860e29 2018-03-12 stsp #ifndef nitems
46 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
50 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
53 99437157 2018-11-11 stsp catch_sigint(int signo)
55 99437157 2018-11-11 stsp sigint_received = 1;
59 99437157 2018-11-11 stsp catch_sigpipe(int signo)
61 99437157 2018-11-11 stsp sigpipe_received = 1;
65 5c860e29 2018-03-12 stsp struct cmd {
66 5c860e29 2018-03-12 stsp const char *cmd_name;
67 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
68 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
69 46a0db7d 2018-03-12 stsp const char *cmd_descr;
72 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
74 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
77 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
78 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
79 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
80 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
81 d00136be 2019-03-26 stsp __dead static void usage_add(void);
82 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
83 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
84 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
87 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
90 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
91 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
93 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
94 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
95 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
96 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
97 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
100 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
101 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
102 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
103 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
104 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
105 1b6b95a8 2018-03-12 stsp "show repository history" },
106 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
107 b00d56cd 2018-04-01 stsp "compare files and directories" },
108 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
109 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
110 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
111 a67e2392 2019-03-26 stsp "list files and directories in repository" },
112 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
113 1b6b95a8 2018-03-12 stsp "show modification status of files" },
114 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
115 d0eebce4 2019-03-11 stsp "manage references in repository" },
116 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
117 d00136be 2019-03-26 stsp "add a new file to version control" },
118 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
119 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
120 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
121 a129376b 2019-03-28 stsp "revert uncommitted changes" },
122 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
123 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
127 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
129 5c860e29 2018-03-12 stsp struct cmd *cmd;
130 5c860e29 2018-03-12 stsp unsigned int i;
132 1b6b95a8 2018-03-12 stsp int hflag = 0;
134 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
136 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
137 5c860e29 2018-03-12 stsp switch (ch) {
143 5c860e29 2018-03-12 stsp /* NOTREACHED */
147 5c860e29 2018-03-12 stsp argc -= optind;
148 5c860e29 2018-03-12 stsp argv += optind;
149 1e70621d 2018-03-27 stsp optind = 0;
151 5c860e29 2018-03-12 stsp if (argc <= 0)
154 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
155 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
157 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
158 d7d4f210 2018-03-12 stsp const struct got_error *error;
160 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
162 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
168 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
169 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
170 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
177 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
181 4ed7e80c 2018-05-20 stsp __dead static void
182 5c860e29 2018-03-12 stsp usage(void)
186 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
187 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
188 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
189 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
190 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
195 0266afb7 2019-01-04 stsp static const struct got_error *
196 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
197 a0937847 2019-05-11 stsp const char *worktree_path, int create_worktree)
199 0266afb7 2019-01-04 stsp const struct got_error *error;
201 a0937847 2019-05-11 stsp if (create_worktree) {
202 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
203 a0937847 2019-05-11 stsp error = got_path_mkdir(worktree_path);
204 a0937847 2019-05-11 stsp if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
205 a0937847 2019-05-11 stsp return error;
208 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
209 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", repo_path);
211 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", worktree_path);
214 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
215 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", "/tmp");
217 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
218 0266afb7 2019-01-04 stsp if (error != NULL)
219 0266afb7 2019-01-04 stsp return error;
221 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
222 230a42bd 2019-05-11 jcs return got_error_prefix_errno("unveil");
224 0266afb7 2019-01-04 stsp return NULL;
227 4ed7e80c 2018-05-20 stsp __dead static void
228 c09a553d 2018-03-12 stsp usage_checkout(void)
230 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
231 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
235 92a684f4 2018-03-12 stsp static void
236 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
238 92a684f4 2018-03-12 stsp char *worktree_path = arg;
240 92a684f4 2018-03-12 stsp while (path[0] == '/')
243 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
246 99437157 2018-11-11 stsp static const struct got_error *
247 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
249 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
250 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
251 99437157 2018-11-11 stsp return NULL;
254 8069f636 2019-01-12 stsp static const struct got_error *
255 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
256 8069f636 2019-01-12 stsp struct got_repository *repo)
258 8069f636 2019-01-12 stsp const struct got_error *err;
259 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
260 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
261 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
263 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo,
264 36a38700 2019-05-10 stsp got_worktree_get_head_ref_name(worktree));
266 36a38700 2019-05-10 stsp return err;
268 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
269 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
273 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
277 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
281 8069f636 2019-01-12 stsp struct got_object_id *id;
283 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
286 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
288 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
289 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
292 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
294 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
300 8069f636 2019-01-12 stsp if (id == NULL)
302 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
306 8069f636 2019-01-12 stsp if (head_ref)
307 8069f636 2019-01-12 stsp got_ref_close(head_ref);
309 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
310 8069f636 2019-01-12 stsp return err;
314 4ed7e80c 2018-05-20 stsp static const struct got_error *
315 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
317 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
318 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
319 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
320 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
321 c09a553d 2018-03-12 stsp char *repo_path = NULL;
322 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
323 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
324 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
325 72151b04 2019-05-11 stsp int ch, same_path_prefix;
327 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
328 0bb8a95e 2018-03-12 stsp switch (ch) {
330 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
331 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
332 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
335 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
338 2deda0b9 2019-03-07 stsp usage_checkout();
339 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
343 0bb8a95e 2018-03-12 stsp argc -= optind;
344 0bb8a95e 2018-03-12 stsp argv += optind;
346 6715a751 2018-03-16 stsp #ifndef PROFILE
347 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
348 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
349 c09a553d 2018-03-12 stsp err(1, "pledge");
351 0bb8a95e 2018-03-12 stsp if (argc == 1) {
352 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
353 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
354 76089277 2018-04-01 stsp if (repo_path == NULL)
355 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
356 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
357 76089277 2018-04-01 stsp if (cwd == NULL) {
358 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
361 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
362 230a42bd 2019-05-11 jcs base = basename(path_prefix);
363 230a42bd 2019-05-11 jcs if (base == NULL) {
364 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
365 230a42bd 2019-05-11 jcs path_prefix);
369 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
370 230a42bd 2019-05-11 jcs if (base == NULL) {
371 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
376 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
377 c09a553d 2018-03-12 stsp if (dotgit)
378 c09a553d 2018-03-12 stsp *dotgit = '\0';
379 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
380 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
385 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
386 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
387 76089277 2018-04-01 stsp if (repo_path == NULL) {
388 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
391 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
392 76089277 2018-04-01 stsp if (worktree_path == NULL) {
393 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[1]);
397 c09a553d 2018-03-12 stsp usage_checkout();
399 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
400 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
402 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
403 c09a553d 2018-03-12 stsp if (error != NULL)
406 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
410 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
411 c09a553d 2018-03-12 stsp if (error != NULL)
414 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
415 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
418 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
419 c09a553d 2018-03-12 stsp if (error != NULL)
422 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
423 e5dc7198 2018-12-29 stsp path_prefix);
424 e5dc7198 2018-12-29 stsp if (error != NULL)
426 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
427 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
431 8069f636 2019-01-12 stsp if (commit_id_str) {
432 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
433 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
434 8069f636 2019-01-12 stsp commit_id_str);
435 8069f636 2019-01-12 stsp if (error != NULL)
437 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
438 8069f636 2019-01-12 stsp if (error != NULL) {
439 8069f636 2019-01-12 stsp free(commit_id);
442 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
443 8069f636 2019-01-12 stsp commit_id);
444 8069f636 2019-01-12 stsp free(commit_id);
449 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
450 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
451 c09a553d 2018-03-12 stsp if (error != NULL)
454 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
457 8069f636 2019-01-12 stsp free(commit_id_str);
458 76089277 2018-04-01 stsp free(repo_path);
459 507dc3bb 2018-12-29 stsp free(worktree_path);
460 507dc3bb 2018-12-29 stsp return error;
463 507dc3bb 2018-12-29 stsp __dead static void
464 507dc3bb 2018-12-29 stsp usage_update(void)
466 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
467 507dc3bb 2018-12-29 stsp getprogname());
471 507dc3bb 2018-12-29 stsp static void
472 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
474 784955db 2019-01-12 stsp int *did_something = arg;
476 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
479 1545c615 2019-02-10 stsp *did_something = 1;
480 507dc3bb 2018-12-29 stsp while (path[0] == '/')
482 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
485 be7061eb 2018-12-30 stsp static const struct got_error *
486 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
488 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
489 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
490 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
491 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
492 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
493 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
494 784955db 2019-01-12 stsp int ch, did_something = 0;
496 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
497 507dc3bb 2018-12-29 stsp switch (ch) {
499 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
500 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
501 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
504 2deda0b9 2019-03-07 stsp usage_update();
505 507dc3bb 2018-12-29 stsp /* NOTREACHED */
509 507dc3bb 2018-12-29 stsp argc -= optind;
510 507dc3bb 2018-12-29 stsp argv += optind;
512 507dc3bb 2018-12-29 stsp #ifndef PROFILE
513 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
514 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
515 507dc3bb 2018-12-29 stsp err(1, "pledge");
517 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
518 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
519 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
522 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
526 507dc3bb 2018-12-29 stsp if (argc == 0) {
527 c4cdcb68 2019-04-03 stsp path = strdup("");
528 c4cdcb68 2019-04-03 stsp if (path == NULL) {
529 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
532 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
533 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
537 507dc3bb 2018-12-29 stsp usage_update();
539 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
540 507dc3bb 2018-12-29 stsp if (error != NULL)
543 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
544 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
548 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
549 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
550 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
551 507dc3bb 2018-12-29 stsp if (error != NULL)
553 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
554 9c4b8182 2019-01-02 stsp if (error != NULL)
556 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
557 507dc3bb 2018-12-29 stsp if (error != NULL)
560 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
561 507dc3bb 2018-12-29 stsp commit_id_str);
562 507dc3bb 2018-12-29 stsp if (error != NULL)
566 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
567 be7061eb 2018-12-30 stsp if (error != NULL)
570 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
571 507dc3bb 2018-12-29 stsp commit_id) != 0) {
572 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
573 507dc3bb 2018-12-29 stsp commit_id);
578 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
579 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
580 507dc3bb 2018-12-29 stsp if (error != NULL)
583 784955db 2019-01-12 stsp if (did_something)
584 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
586 784955db 2019-01-12 stsp printf("Already up-to-date\n");
588 c09a553d 2018-03-12 stsp free(worktree_path);
589 c4cdcb68 2019-04-03 stsp free(path);
590 507dc3bb 2018-12-29 stsp free(commit_id);
591 9c4b8182 2019-01-02 stsp free(commit_id_str);
592 c09a553d 2018-03-12 stsp return error;
595 f42b1b34 2018-03-12 stsp static const struct got_error *
596 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
597 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
599 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
600 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
601 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
602 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
604 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
605 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
607 79109fed 2018-03-27 stsp return err;
609 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
610 79f35eb3 2018-06-11 stsp if (qid != NULL) {
611 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
613 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
615 79109fed 2018-03-27 stsp return err;
617 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
618 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
619 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
621 0f2b3dca 2018-12-22 stsp return err;
623 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
625 79109fed 2018-03-27 stsp return err;
628 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
632 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
633 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
636 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
637 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
638 0f2b3dca 2018-12-22 stsp free(id_str1);
639 0f2b3dca 2018-12-22 stsp free(id_str2);
640 79109fed 2018-03-27 stsp return err;
643 4bb494d5 2018-06-16 stsp static char *
644 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
646 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
647 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
653 79109fed 2018-03-27 stsp static const struct got_error *
654 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
655 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
656 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
658 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
659 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
660 6c281f94 2018-06-11 stsp char datebuf[26];
661 45d799e2 2018-12-23 stsp time_t committer_time;
662 45d799e2 2018-12-23 stsp const char *author, *committer;
663 199a4027 2019-02-02 stsp char *refs_str = NULL;
664 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
666 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
668 199a4027 2019-02-02 stsp const char *name;
669 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
671 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
672 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
674 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
676 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
678 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
680 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
682 199a4027 2019-02-02 stsp s = refs_str;
683 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
684 199a4027 2019-02-02 stsp name) == -1) {
685 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
691 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
693 8bf5b3c9 2018-03-17 stsp return err;
695 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
696 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
697 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
698 832c249c 2018-06-10 stsp free(id_str);
699 d3d493d7 2019-02-21 stsp id_str = NULL;
700 d3d493d7 2019-02-21 stsp free(refs_str);
701 d3d493d7 2019-02-21 stsp refs_str = NULL;
702 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
703 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
704 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
705 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
706 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
707 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
708 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
709 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
710 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
711 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
712 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
714 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
715 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
716 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
718 a0603db2 2018-06-10 stsp return err;
719 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
720 a0603db2 2018-06-10 stsp free(id_str);
724 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
725 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
726 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
728 621015ac 2018-07-23 stsp logmsg = logmsg0;
730 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
732 832c249c 2018-06-10 stsp printf(" %s\n", line);
733 832c249c 2018-06-10 stsp } while (line);
734 621015ac 2018-07-23 stsp free(logmsg0);
736 971751ac 2018-03-27 stsp if (show_patch) {
737 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
738 971751ac 2018-03-27 stsp if (err == 0)
739 971751ac 2018-03-27 stsp printf("\n");
742 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
743 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fflush");
744 07862c20 2018-09-15 stsp return err;
747 f42b1b34 2018-03-12 stsp static const struct got_error *
748 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
749 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
750 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
752 f42b1b34 2018-03-12 stsp const struct got_error *err;
753 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
755 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
756 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
758 f42b1b34 2018-03-12 stsp return err;
759 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
763 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
764 372ccdbb 2018-06-10 stsp struct got_object_id *id;
766 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
769 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
771 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
772 9ba79e04 2018-06-11 stsp err = NULL;
775 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
777 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
783 b43fbaa0 2018-06-11 stsp if (id == NULL)
786 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
789 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
791 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
792 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
796 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
797 f42b1b34 2018-03-12 stsp return err;
800 4ed7e80c 2018-05-20 stsp __dead static void
801 6f3d1eb0 2018-03-12 stsp usage_log(void)
803 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
804 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
808 4ed7e80c 2018-05-20 stsp static const struct got_error *
809 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
811 f42b1b34 2018-03-12 stsp const struct got_error *error;
812 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
813 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
814 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
815 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
816 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
817 d142fc45 2018-04-01 stsp char *start_commit = NULL;
818 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
819 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
820 64a96a6d 2018-04-01 stsp const char *errstr;
821 199a4027 2019-02-02 stsp struct got_reflist_head refs;
823 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
825 6715a751 2018-03-16 stsp #ifndef PROFILE
826 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
829 f42b1b34 2018-03-12 stsp err(1, "pledge");
832 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
833 79109fed 2018-03-27 stsp switch (ch) {
835 79109fed 2018-03-27 stsp show_patch = 1;
838 d142fc45 2018-04-01 stsp start_commit = optarg;
841 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
843 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
844 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
847 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
848 64a96a6d 2018-04-01 stsp if (errstr != NULL)
849 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
852 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
855 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
856 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
857 04ca23f4 2018-07-16 stsp err(1, "-r option");
858 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
861 2deda0b9 2019-03-07 stsp usage_log();
862 79109fed 2018-03-27 stsp /* NOTREACHED */
866 79109fed 2018-03-27 stsp argc -= optind;
867 79109fed 2018-03-27 stsp argv += optind;
869 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
870 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
871 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
875 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
876 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
878 cffc0aa4 2019-02-05 stsp error = NULL;
880 e7301579 2019-03-18 stsp if (argc == 0) {
881 cbd1af7a 2019-03-18 stsp path = strdup("");
882 e7301579 2019-03-18 stsp if (path == NULL) {
883 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
886 e7301579 2019-03-18 stsp } else if (argc == 1) {
887 e7301579 2019-03-18 stsp if (worktree) {
888 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
893 e7301579 2019-03-18 stsp path = strdup(argv[0]);
894 e7301579 2019-03-18 stsp if (path == NULL) {
895 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
900 cbd1af7a 2019-03-18 stsp usage_log();
902 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
903 5486daa2 2019-05-11 stsp repo_path = worktree ?
904 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
906 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
907 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
911 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
912 d7d4f210 2018-03-12 stsp if (error != NULL)
915 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
916 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
920 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
921 3235492e 2018-04-01 stsp struct got_reference *head_ref;
922 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
923 3235492e 2018-04-01 stsp if (error != NULL)
924 3235492e 2018-04-01 stsp return error;
925 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
926 3235492e 2018-04-01 stsp got_ref_close(head_ref);
927 3235492e 2018-04-01 stsp if (error != NULL)
928 3235492e 2018-04-01 stsp return error;
929 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
931 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
932 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
933 3235492e 2018-04-01 stsp if (error == NULL) {
934 1caad61b 2019-02-01 stsp int obj_type;
935 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
936 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
937 d1f2edc9 2018-06-13 stsp if (error != NULL)
939 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
940 1caad61b 2019-02-01 stsp if (error != NULL)
942 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
943 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
944 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
945 1caad61b 2019-02-01 stsp if (error != NULL)
947 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
948 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
949 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
950 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
954 1caad61b 2019-02-01 stsp id = got_object_id_dup(
955 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
956 1caad61b 2019-02-01 stsp if (id == NULL)
957 230a42bd 2019-05-11 jcs error = got_error_prefix_errno(
958 230a42bd 2019-05-11 jcs "got_object_id_dup");
959 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
962 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
963 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
966 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
967 d1f2edc9 2018-06-13 stsp if (error != NULL)
970 15a94983 2018-12-23 stsp if (commit == NULL) {
971 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
972 d1f2edc9 2018-06-13 stsp start_commit);
973 d1f2edc9 2018-06-13 stsp if (error != NULL)
974 d1f2edc9 2018-06-13 stsp return error;
977 d7d4f210 2018-03-12 stsp if (error != NULL)
980 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
981 04ca23f4 2018-07-16 stsp if (error != NULL)
983 04ca23f4 2018-07-16 stsp if (in_repo_path) {
984 04ca23f4 2018-07-16 stsp free(path);
985 04ca23f4 2018-07-16 stsp path = in_repo_path;
988 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
992 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
993 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
995 04ca23f4 2018-07-16 stsp free(path);
996 04ca23f4 2018-07-16 stsp free(repo_path);
999 cffc0aa4 2019-02-05 stsp if (worktree)
1000 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1001 ad242220 2018-09-08 stsp if (repo) {
1002 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1003 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1004 ad242220 2018-09-08 stsp if (error == NULL)
1005 ad242220 2018-09-08 stsp error = repo_error;
1007 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1008 8bf5b3c9 2018-03-17 stsp return error;
1011 4ed7e80c 2018-05-20 stsp __dead static void
1012 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1014 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1015 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1019 b72f483a 2019-02-05 stsp struct print_diff_arg {
1020 b72f483a 2019-02-05 stsp struct got_repository *repo;
1021 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1022 b72f483a 2019-02-05 stsp int diff_context;
1023 3fc0c068 2019-02-10 stsp const char *id_str;
1024 3fc0c068 2019-02-10 stsp int header_shown;
1027 4ed7e80c 2018-05-20 stsp static const struct got_error *
1028 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1029 b72f483a 2019-02-05 stsp struct got_object_id *id)
1031 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1032 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1033 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1034 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1035 b72f483a 2019-02-05 stsp char *abspath = NULL;
1036 b72f483a 2019-02-05 stsp struct stat sb;
1038 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1039 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1040 b72f483a 2019-02-05 stsp return NULL;
1042 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1043 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1044 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1045 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1048 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1049 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1051 d00136be 2019-03-26 stsp goto done;
1055 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1056 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1057 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1058 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1059 2ec1f75b 2019-03-26 stsp goto done;
1062 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1063 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1064 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", abspath);
1065 2ec1f75b 2019-03-26 stsp goto done;
1067 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1068 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", abspath);
1069 2ec1f75b 2019-03-26 stsp goto done;
1072 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1074 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1077 b72f483a 2019-02-05 stsp if (blob1)
1078 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1079 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1080 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
1081 b72f483a 2019-02-05 stsp free(abspath);
1082 927df6b7 2019-02-10 stsp return err;
1085 927df6b7 2019-02-10 stsp static const struct got_error *
1086 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1088 b00d56cd 2018-04-01 stsp const struct got_error *error;
1089 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1090 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1091 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1092 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1093 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1094 15a94983 2018-12-23 stsp int type1, type2;
1095 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1096 c0cc5c62 2018-10-18 stsp const char *errstr;
1097 927df6b7 2019-02-10 stsp char *path = NULL;
1099 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1100 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1101 25eccc22 2019-01-04 stsp NULL) == -1)
1102 b00d56cd 2018-04-01 stsp err(1, "pledge");
1105 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1106 b00d56cd 2018-04-01 stsp switch (ch) {
1108 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1109 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1110 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1113 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1114 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1115 b72f483a 2019-02-05 stsp err(1, "-r option");
1116 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1119 2deda0b9 2019-03-07 stsp usage_diff();
1120 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1124 b00d56cd 2018-04-01 stsp argc -= optind;
1125 b00d56cd 2018-04-01 stsp argv += optind;
1127 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1128 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1129 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1130 b72f483a 2019-02-05 stsp goto done;
1132 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1133 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1134 927df6b7 2019-02-10 stsp goto done;
1135 927df6b7 2019-02-10 stsp if (argc <= 1) {
1136 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1137 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1138 927df6b7 2019-02-10 stsp goto done;
1140 b72f483a 2019-02-05 stsp if (repo_path)
1142 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1143 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1144 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1145 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1146 927df6b7 2019-02-10 stsp goto done;
1148 927df6b7 2019-02-10 stsp if (argc == 1) {
1149 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1151 927df6b7 2019-02-10 stsp if (error)
1152 927df6b7 2019-02-10 stsp goto done;
1154 927df6b7 2019-02-10 stsp path = strdup("");
1155 927df6b7 2019-02-10 stsp if (path == NULL) {
1156 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1157 927df6b7 2019-02-10 stsp goto done;
1160 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1161 15a94983 2018-12-23 stsp id_str1 = argv[0];
1162 15a94983 2018-12-23 stsp id_str2 = argv[1];
1164 b00d56cd 2018-04-01 stsp usage_diff();
1166 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1167 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1168 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1169 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
1172 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1173 76089277 2018-04-01 stsp free(repo_path);
1174 b00d56cd 2018-04-01 stsp if (error != NULL)
1175 b00d56cd 2018-04-01 stsp goto done;
1177 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1178 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1179 c02c541e 2019-03-29 stsp if (error)
1180 c02c541e 2019-03-29 stsp goto done;
1182 b72f483a 2019-02-05 stsp if (worktree) {
1183 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1184 b72f483a 2019-02-05 stsp char *id_str;
1185 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1186 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1187 b72f483a 2019-02-05 stsp if (error)
1188 b72f483a 2019-02-05 stsp goto done;
1189 b72f483a 2019-02-05 stsp arg.repo = repo;
1190 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1191 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1192 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1193 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1195 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1196 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1197 b72f483a 2019-02-05 stsp free(id_str);
1198 b72f483a 2019-02-05 stsp goto done;
1201 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1202 6402fb3c 2018-09-15 stsp if (error)
1203 b00d56cd 2018-04-01 stsp goto done;
1205 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1206 6402fb3c 2018-09-15 stsp if (error)
1207 b00d56cd 2018-04-01 stsp goto done;
1209 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1210 15a94983 2018-12-23 stsp if (error)
1211 15a94983 2018-12-23 stsp goto done;
1213 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1214 15a94983 2018-12-23 stsp if (error)
1215 15a94983 2018-12-23 stsp goto done;
1217 15a94983 2018-12-23 stsp if (type1 != type2) {
1218 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1219 b00d56cd 2018-04-01 stsp goto done;
1222 15a94983 2018-12-23 stsp switch (type1) {
1223 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1224 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1225 54156555 2018-12-24 stsp diff_context, repo, stdout);
1227 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1228 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1229 54156555 2018-12-24 stsp diff_context, repo, stdout);
1231 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1232 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1234 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1235 c0cc5c62 2018-10-18 stsp repo, stdout);
1238 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1242 15a94983 2018-12-23 stsp free(id1);
1243 15a94983 2018-12-23 stsp free(id2);
1244 927df6b7 2019-02-10 stsp free(path);
1245 b72f483a 2019-02-05 stsp if (worktree)
1246 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1247 ad242220 2018-09-08 stsp if (repo) {
1248 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1249 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1250 ad242220 2018-09-08 stsp if (error == NULL)
1251 ad242220 2018-09-08 stsp error = repo_error;
1253 b00d56cd 2018-04-01 stsp return error;
1256 404c43c4 2018-06-21 stsp __dead static void
1257 404c43c4 2018-06-21 stsp usage_blame(void)
1259 9270e621 2019-02-05 stsp fprintf(stderr,
1260 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1261 404c43c4 2018-06-21 stsp getprogname());
1265 404c43c4 2018-06-21 stsp static const struct got_error *
1266 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1268 404c43c4 2018-06-21 stsp const struct got_error *error;
1269 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1270 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1271 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1272 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1273 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1276 404c43c4 2018-06-21 stsp #ifndef PROFILE
1277 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1278 36e2fb66 2019-01-04 stsp NULL) == -1)
1279 404c43c4 2018-06-21 stsp err(1, "pledge");
1282 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1283 404c43c4 2018-06-21 stsp switch (ch) {
1285 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1288 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1289 66bea077 2018-08-02 stsp if (repo_path == NULL)
1290 66bea077 2018-08-02 stsp err(1, "-r option");
1291 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1294 2deda0b9 2019-03-07 stsp usage_blame();
1295 404c43c4 2018-06-21 stsp /* NOTREACHED */
1299 404c43c4 2018-06-21 stsp argc -= optind;
1300 404c43c4 2018-06-21 stsp argv += optind;
1302 a39318fd 2018-08-02 stsp if (argc == 1)
1303 404c43c4 2018-06-21 stsp path = argv[0];
1305 404c43c4 2018-06-21 stsp usage_blame();
1307 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1308 66bea077 2018-08-02 stsp if (cwd == NULL) {
1309 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1310 66bea077 2018-08-02 stsp goto done;
1312 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1313 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1314 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1315 66bea077 2018-08-02 stsp goto done;
1317 0c06baac 2019-02-05 stsp error = NULL;
1318 0c06baac 2019-02-05 stsp if (worktree) {
1319 0c06baac 2019-02-05 stsp repo_path =
1320 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1321 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1322 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1323 0c06baac 2019-02-05 stsp if (error)
1324 0c06baac 2019-02-05 stsp goto done;
1326 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1327 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1328 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1329 0c06baac 2019-02-05 stsp goto done;
1334 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1335 c02c541e 2019-03-29 stsp if (error != NULL)
1336 36e2fb66 2019-01-04 stsp goto done;
1338 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1339 c02c541e 2019-03-29 stsp if (error)
1340 404c43c4 2018-06-21 stsp goto done;
1342 0c06baac 2019-02-05 stsp if (worktree) {
1343 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1344 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1345 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1346 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1347 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1348 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1349 6efaaa2d 2019-02-05 stsp path) == -1) {
1350 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1351 0c06baac 2019-02-05 stsp goto done;
1353 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1356 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1358 0c06baac 2019-02-05 stsp if (error)
1359 66bea077 2018-08-02 stsp goto done;
1361 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1362 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1363 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1364 404c43c4 2018-06-21 stsp if (error != NULL)
1365 66bea077 2018-08-02 stsp goto done;
1366 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1367 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1368 404c43c4 2018-06-21 stsp if (error != NULL)
1369 66bea077 2018-08-02 stsp goto done;
1371 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1372 15a94983 2018-12-23 stsp commit_id_str);
1373 404c43c4 2018-06-21 stsp if (error != NULL)
1374 66bea077 2018-08-02 stsp goto done;
1377 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1379 66bea077 2018-08-02 stsp free(in_repo_path);
1380 66bea077 2018-08-02 stsp free(repo_path);
1381 66bea077 2018-08-02 stsp free(cwd);
1382 404c43c4 2018-06-21 stsp free(commit_id);
1383 0c06baac 2019-02-05 stsp if (worktree)
1384 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1385 ad242220 2018-09-08 stsp if (repo) {
1386 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1387 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1388 ad242220 2018-09-08 stsp if (error == NULL)
1389 ad242220 2018-09-08 stsp error = repo_error;
1391 404c43c4 2018-06-21 stsp return error;
1394 5de5890b 2018-10-18 stsp __dead static void
1395 5de5890b 2018-10-18 stsp usage_tree(void)
1397 c1669e2e 2019-01-09 stsp fprintf(stderr,
1398 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1399 5de5890b 2018-10-18 stsp getprogname());
1403 c1669e2e 2019-01-09 stsp static void
1404 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1405 c1669e2e 2019-01-09 stsp const char *root_path)
1407 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1409 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1410 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1413 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1414 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1415 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1418 5de5890b 2018-10-18 stsp static const struct got_error *
1419 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1420 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1421 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1423 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1424 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1425 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1426 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1427 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1429 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1431 5de5890b 2018-10-18 stsp goto done;
1433 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1435 5de5890b 2018-10-18 stsp goto done;
1436 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1437 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1438 5de5890b 2018-10-18 stsp while (te) {
1439 5de5890b 2018-10-18 stsp char *id = NULL;
1441 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1444 5de5890b 2018-10-18 stsp if (show_ids) {
1445 5de5890b 2018-10-18 stsp char *id_str;
1446 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1448 5de5890b 2018-10-18 stsp goto done;
1449 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1450 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1451 5de5890b 2018-10-18 stsp free(id_str);
1452 5de5890b 2018-10-18 stsp goto done;
1454 5de5890b 2018-10-18 stsp free(id_str);
1456 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1459 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1460 c1669e2e 2019-01-09 stsp char *child_path;
1461 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1462 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1463 c1669e2e 2019-01-09 stsp te->name) == -1) {
1464 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1465 c1669e2e 2019-01-09 stsp goto done;
1467 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1468 c1669e2e 2019-01-09 stsp root_path, repo);
1469 c1669e2e 2019-01-09 stsp free(child_path);
1471 c1669e2e 2019-01-09 stsp goto done;
1474 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1478 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1479 5de5890b 2018-10-18 stsp free(tree_id);
1480 5de5890b 2018-10-18 stsp return err;
1483 5de5890b 2018-10-18 stsp static const struct got_error *
1484 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1486 5de5890b 2018-10-18 stsp const struct got_error *error;
1487 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1488 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1489 7a2c19d6 2019-02-05 stsp const char *path;
1490 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1491 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1492 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1493 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1496 5de5890b 2018-10-18 stsp #ifndef PROFILE
1497 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1498 0f8d269b 2019-01-04 stsp NULL) == -1)
1499 5de5890b 2018-10-18 stsp err(1, "pledge");
1502 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1503 5de5890b 2018-10-18 stsp switch (ch) {
1505 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1508 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1509 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1510 5de5890b 2018-10-18 stsp err(1, "-r option");
1511 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1514 5de5890b 2018-10-18 stsp show_ids = 1;
1517 c1669e2e 2019-01-09 stsp recurse = 1;
1520 2deda0b9 2019-03-07 stsp usage_tree();
1521 5de5890b 2018-10-18 stsp /* NOTREACHED */
1525 5de5890b 2018-10-18 stsp argc -= optind;
1526 5de5890b 2018-10-18 stsp argv += optind;
1528 5de5890b 2018-10-18 stsp if (argc == 1)
1529 5de5890b 2018-10-18 stsp path = argv[0];
1530 5de5890b 2018-10-18 stsp else if (argc > 1)
1531 5de5890b 2018-10-18 stsp usage_tree();
1533 7a2c19d6 2019-02-05 stsp path = NULL;
1535 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1536 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1537 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1538 9bf7a39b 2019-02-05 stsp goto done;
1540 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1541 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1542 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1543 7a2c19d6 2019-02-05 stsp goto done;
1545 7a2c19d6 2019-02-05 stsp error = NULL;
1546 7a2c19d6 2019-02-05 stsp if (worktree) {
1547 7a2c19d6 2019-02-05 stsp repo_path =
1548 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1549 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1550 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1551 7a2c19d6 2019-02-05 stsp if (error)
1552 7a2c19d6 2019-02-05 stsp goto done;
1554 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1555 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1556 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1557 7a2c19d6 2019-02-05 stsp goto done;
1562 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1563 5de5890b 2018-10-18 stsp if (error != NULL)
1564 c02c541e 2019-03-29 stsp goto done;
1566 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1567 c02c541e 2019-03-29 stsp if (error)
1568 5de5890b 2018-10-18 stsp goto done;
1570 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1571 9bf7a39b 2019-02-05 stsp if (worktree) {
1572 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1573 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1574 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1575 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1576 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1577 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1578 9bf7a39b 2019-02-05 stsp goto done;
1580 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1582 9bf7a39b 2019-02-05 stsp if (error)
1583 9bf7a39b 2019-02-05 stsp goto done;
1585 9bf7a39b 2019-02-05 stsp path = "/";
1587 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1588 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1589 9bf7a39b 2019-02-05 stsp if (error != NULL)
1590 9bf7a39b 2019-02-05 stsp goto done;
1593 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1594 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1595 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1596 5de5890b 2018-10-18 stsp if (error != NULL)
1597 5de5890b 2018-10-18 stsp goto done;
1598 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1599 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1600 5de5890b 2018-10-18 stsp if (error != NULL)
1601 5de5890b 2018-10-18 stsp goto done;
1603 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1604 15a94983 2018-12-23 stsp commit_id_str);
1605 5de5890b 2018-10-18 stsp if (error != NULL)
1606 5de5890b 2018-10-18 stsp goto done;
1609 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1610 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1612 5de5890b 2018-10-18 stsp free(in_repo_path);
1613 5de5890b 2018-10-18 stsp free(repo_path);
1614 5de5890b 2018-10-18 stsp free(cwd);
1615 5de5890b 2018-10-18 stsp free(commit_id);
1616 7a2c19d6 2019-02-05 stsp if (worktree)
1617 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1618 5de5890b 2018-10-18 stsp if (repo) {
1619 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1620 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1621 5de5890b 2018-10-18 stsp if (error == NULL)
1622 5de5890b 2018-10-18 stsp error = repo_error;
1624 5de5890b 2018-10-18 stsp return error;
1627 6bad629b 2019-02-04 stsp __dead static void
1628 6bad629b 2019-02-04 stsp usage_status(void)
1630 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1634 b72f483a 2019-02-05 stsp static const struct got_error *
1635 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1636 b72f483a 2019-02-05 stsp struct got_object_id *id)
1638 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1639 b72f483a 2019-02-05 stsp return NULL;
1642 6bad629b 2019-02-04 stsp static const struct got_error *
1643 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1645 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1646 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1647 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1648 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1651 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1652 6bad629b 2019-02-04 stsp switch (ch) {
1654 2deda0b9 2019-03-07 stsp usage_status();
1655 6bad629b 2019-02-04 stsp /* NOTREACHED */
1659 6bad629b 2019-02-04 stsp argc -= optind;
1660 6bad629b 2019-02-04 stsp argv += optind;
1662 6bad629b 2019-02-04 stsp #ifndef PROFILE
1663 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1664 6bad629b 2019-02-04 stsp NULL) == -1)
1665 6bad629b 2019-02-04 stsp err(1, "pledge");
1667 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1668 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1669 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1670 927df6b7 2019-02-10 stsp goto done;
1673 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1674 927df6b7 2019-02-10 stsp if (error != NULL)
1675 927df6b7 2019-02-10 stsp goto done;
1677 6bad629b 2019-02-04 stsp if (argc == 0) {
1678 927df6b7 2019-02-10 stsp path = strdup("");
1679 927df6b7 2019-02-10 stsp if (path == NULL) {
1680 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1681 6bad629b 2019-02-04 stsp goto done;
1683 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1684 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1685 927df6b7 2019-02-10 stsp if (error)
1686 6bad629b 2019-02-04 stsp goto done;
1688 6bad629b 2019-02-04 stsp usage_status();
1690 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1691 6bad629b 2019-02-04 stsp if (error != NULL)
1692 6bad629b 2019-02-04 stsp goto done;
1694 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1695 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1696 6bad629b 2019-02-04 stsp if (error)
1697 6bad629b 2019-02-04 stsp goto done;
1699 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1700 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1702 927df6b7 2019-02-10 stsp free(cwd);
1703 927df6b7 2019-02-10 stsp free(path);
1704 d0eebce4 2019-03-11 stsp return error;
1707 d0eebce4 2019-03-11 stsp __dead static void
1708 d0eebce4 2019-03-11 stsp usage_ref(void)
1710 d0eebce4 2019-03-11 stsp fprintf(stderr,
1711 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1712 d0eebce4 2019-03-11 stsp getprogname());
1716 d0eebce4 2019-03-11 stsp static const struct got_error *
1717 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1719 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1720 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1721 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1723 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1724 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1726 d0eebce4 2019-03-11 stsp return err;
1728 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1729 d0eebce4 2019-03-11 stsp char *refstr;
1730 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1731 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1732 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_ref_to_str");
1733 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1734 d0eebce4 2019-03-11 stsp free(refstr);
1737 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1738 d0eebce4 2019-03-11 stsp return NULL;
1741 d0eebce4 2019-03-11 stsp static const struct got_error *
1742 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1744 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1745 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1747 d0eebce4 2019-03-11 stsp err = got_ref_open(&ref, repo, refname);
1749 d0eebce4 2019-03-11 stsp return err;
1751 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1752 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1753 d0eebce4 2019-03-11 stsp return err;
1756 d0eebce4 2019-03-11 stsp static const struct got_error *
1757 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1759 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1760 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1761 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1763 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1765 d0eebce4 2019-03-11 stsp return err;
1767 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1769 d0eebce4 2019-03-11 stsp goto done;
1771 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1774 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1776 d0eebce4 2019-03-11 stsp return err;
1779 d0eebce4 2019-03-11 stsp static const struct got_error *
1780 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1782 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1783 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1784 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1785 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1786 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1787 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1789 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1790 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1791 d0eebce4 2019-03-11 stsp switch (ch) {
1793 d0eebce4 2019-03-11 stsp delref = optarg;
1796 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1797 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1798 d0eebce4 2019-03-11 stsp err(1, "-r option");
1799 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1802 d0eebce4 2019-03-11 stsp do_list = 1;
1805 d0eebce4 2019-03-11 stsp usage_ref();
1806 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1810 d0eebce4 2019-03-11 stsp if (do_list && delref)
1811 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1813 d0eebce4 2019-03-11 stsp argc -= optind;
1814 d0eebce4 2019-03-11 stsp argv += optind;
1816 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1817 d0eebce4 2019-03-11 stsp if (argc > 0)
1818 d0eebce4 2019-03-11 stsp usage_ref();
1819 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1820 d0eebce4 2019-03-11 stsp usage_ref();
1822 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1823 e0b57350 2019-03-12 stsp if (do_list) {
1824 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1825 e0b57350 2019-03-12 stsp NULL) == -1)
1826 e0b57350 2019-03-12 stsp err(1, "pledge");
1828 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1829 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1830 e0b57350 2019-03-12 stsp err(1, "pledge");
1833 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1834 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1835 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1836 d0eebce4 2019-03-11 stsp goto done;
1839 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1840 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1841 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1842 d0eebce4 2019-03-11 stsp goto done;
1844 d0eebce4 2019-03-11 stsp error = NULL;
1845 d0eebce4 2019-03-11 stsp if (worktree) {
1846 d0eebce4 2019-03-11 stsp repo_path =
1847 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1848 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1849 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1850 d0eebce4 2019-03-11 stsp if (error)
1851 d0eebce4 2019-03-11 stsp goto done;
1853 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1854 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1855 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1856 d0eebce4 2019-03-11 stsp goto done;
1861 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1862 d0eebce4 2019-03-11 stsp if (error != NULL)
1863 d0eebce4 2019-03-11 stsp goto done;
1865 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1866 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1867 c02c541e 2019-03-29 stsp if (error)
1868 c02c541e 2019-03-29 stsp goto done;
1870 d0eebce4 2019-03-11 stsp if (do_list)
1871 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1872 d0eebce4 2019-03-11 stsp else if (delref)
1873 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1875 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1878 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1879 d0eebce4 2019-03-11 stsp if (worktree)
1880 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1881 d0eebce4 2019-03-11 stsp free(cwd);
1882 d0eebce4 2019-03-11 stsp free(repo_path);
1883 d00136be 2019-03-26 stsp return error;
1886 d00136be 2019-03-26 stsp __dead static void
1887 d00136be 2019-03-26 stsp usage_add(void)
1889 d00136be 2019-03-26 stsp fprintf(stderr, "usage: %s add file-path\n", getprogname());
1893 d00136be 2019-03-26 stsp static const struct got_error *
1894 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1896 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1897 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1898 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1899 d00136be 2019-03-26 stsp char *cwd = NULL, *path = NULL, *relpath = NULL;
1902 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1903 d00136be 2019-03-26 stsp switch (ch) {
1905 d00136be 2019-03-26 stsp usage_add();
1906 d00136be 2019-03-26 stsp /* NOTREACHED */
1910 d00136be 2019-03-26 stsp argc -= optind;
1911 d00136be 2019-03-26 stsp argv += optind;
1913 d00136be 2019-03-26 stsp if (argc != 1)
1914 d00136be 2019-03-26 stsp usage_add();
1916 d00136be 2019-03-26 stsp path = realpath(argv[0], NULL);
1917 d00136be 2019-03-26 stsp if (path == NULL) {
1918 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
1919 d00136be 2019-03-26 stsp goto done;
1921 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
1923 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1924 d00136be 2019-03-26 stsp if (cwd == NULL) {
1925 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1926 d00136be 2019-03-26 stsp goto done;
1928 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1929 d00136be 2019-03-26 stsp if (error)
1930 d00136be 2019-03-26 stsp goto done;
1932 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1933 031a5338 2019-03-26 stsp if (error != NULL)
1934 031a5338 2019-03-26 stsp goto done;
1936 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1937 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1938 d00136be 2019-03-26 stsp if (error)
1939 d00136be 2019-03-26 stsp goto done;
1941 031a5338 2019-03-26 stsp error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1943 d00136be 2019-03-26 stsp if (error)
1944 d00136be 2019-03-26 stsp goto done;
1947 031a5338 2019-03-26 stsp got_repo_close(repo);
1948 d00136be 2019-03-26 stsp if (worktree)
1949 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1950 d00136be 2019-03-26 stsp free(path);
1951 d00136be 2019-03-26 stsp free(relpath);
1952 2ec1f75b 2019-03-26 stsp free(cwd);
1953 2ec1f75b 2019-03-26 stsp return error;
1956 2ec1f75b 2019-03-26 stsp __dead static void
1957 2ec1f75b 2019-03-26 stsp usage_rm(void)
1959 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1963 2ec1f75b 2019-03-26 stsp static const struct got_error *
1964 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
1966 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
1967 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
1968 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
1969 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
1970 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
1972 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
1973 2ec1f75b 2019-03-26 stsp switch (ch) {
1975 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
1978 2ec1f75b 2019-03-26 stsp usage_add();
1979 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
1983 2ec1f75b 2019-03-26 stsp argc -= optind;
1984 2ec1f75b 2019-03-26 stsp argv += optind;
1986 2ec1f75b 2019-03-26 stsp if (argc != 1)
1987 2ec1f75b 2019-03-26 stsp usage_rm();
1989 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
1990 2ec1f75b 2019-03-26 stsp if (path == NULL) {
1991 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
1992 2ec1f75b 2019-03-26 stsp goto done;
1994 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
1996 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
1997 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
1998 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1999 2ec1f75b 2019-03-26 stsp goto done;
2001 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2002 2ec1f75b 2019-03-26 stsp if (error)
2003 2ec1f75b 2019-03-26 stsp goto done;
2005 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2006 2ec1f75b 2019-03-26 stsp if (error != NULL)
2007 2ec1f75b 2019-03-26 stsp goto done;
2009 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2010 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2011 2ec1f75b 2019-03-26 stsp if (error)
2012 2ec1f75b 2019-03-26 stsp goto done;
2014 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2015 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
2016 a129376b 2019-03-28 stsp if (error)
2017 a129376b 2019-03-28 stsp goto done;
2020 a129376b 2019-03-28 stsp got_repo_close(repo);
2021 a129376b 2019-03-28 stsp if (worktree)
2022 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2023 a129376b 2019-03-28 stsp free(path);
2024 a129376b 2019-03-28 stsp free(cwd);
2025 a129376b 2019-03-28 stsp return error;
2028 a129376b 2019-03-28 stsp __dead static void
2029 a129376b 2019-03-28 stsp usage_revert(void)
2031 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2035 a129376b 2019-03-28 stsp static void
2036 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2038 a129376b 2019-03-28 stsp while (path[0] == '/')
2040 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2043 a129376b 2019-03-28 stsp static const struct got_error *
2044 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2046 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2047 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2048 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2049 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2052 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2053 a129376b 2019-03-28 stsp switch (ch) {
2055 a129376b 2019-03-28 stsp usage_revert();
2056 a129376b 2019-03-28 stsp /* NOTREACHED */
2060 a129376b 2019-03-28 stsp argc -= optind;
2061 a129376b 2019-03-28 stsp argv += optind;
2063 a129376b 2019-03-28 stsp if (argc != 1)
2064 a129376b 2019-03-28 stsp usage_revert();
2066 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2067 a129376b 2019-03-28 stsp if (path == NULL) {
2068 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2069 a129376b 2019-03-28 stsp goto done;
2071 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2073 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2074 a129376b 2019-03-28 stsp if (cwd == NULL) {
2075 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2076 a129376b 2019-03-28 stsp goto done;
2078 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2079 2ec1f75b 2019-03-26 stsp if (error)
2080 2ec1f75b 2019-03-26 stsp goto done;
2082 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2083 a129376b 2019-03-28 stsp if (error != NULL)
2084 a129376b 2019-03-28 stsp goto done;
2086 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2087 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2088 a129376b 2019-03-28 stsp if (error)
2089 a129376b 2019-03-28 stsp goto done;
2091 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2092 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2093 a129376b 2019-03-28 stsp if (error)
2094 a129376b 2019-03-28 stsp goto done;
2097 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2098 2ec1f75b 2019-03-26 stsp if (worktree)
2099 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2100 2ec1f75b 2019-03-26 stsp free(path);
2101 d00136be 2019-03-26 stsp free(cwd);
2102 6bad629b 2019-02-04 stsp return error;
2105 c4296144 2019-05-09 stsp __dead static void
2106 c4296144 2019-05-09 stsp usage_commit(void)
2108 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2112 c4296144 2019-05-09 stsp static const struct got_error *
2113 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2115 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2116 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2117 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2118 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2119 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2120 c4296144 2019-05-09 stsp const char *logmsg = "<no log message was specified>";
2121 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2124 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2125 c4296144 2019-05-09 stsp switch (ch) {
2127 c4296144 2019-05-09 stsp logmsg = optarg;
2130 c4296144 2019-05-09 stsp usage_commit();
2131 c4296144 2019-05-09 stsp /* NOTREACHED */
2135 c4296144 2019-05-09 stsp argc -= optind;
2136 c4296144 2019-05-09 stsp argv += optind;
2138 c4296144 2019-05-09 stsp if (argc == 1) {
2139 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2140 c4296144 2019-05-09 stsp if (path == NULL) {
2141 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2142 c4296144 2019-05-09 stsp goto done;
2144 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2145 c4296144 2019-05-09 stsp } else if (argc != 0)
2146 c4296144 2019-05-09 stsp usage_commit();
2148 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2149 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2150 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2151 35bd8fed 2019-05-09 stsp goto done;
2154 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2155 c4296144 2019-05-09 stsp if (cwd == NULL) {
2156 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2157 c4296144 2019-05-09 stsp goto done;
2159 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2160 c4296144 2019-05-09 stsp if (error)
2161 c4296144 2019-05-09 stsp goto done;
2163 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2164 c4296144 2019-05-09 stsp if (error != NULL)
2165 c4296144 2019-05-09 stsp goto done;
2167 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2168 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2169 c4296144 2019-05-09 stsp if (error)
2170 c4296144 2019-05-09 stsp goto done;
2172 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2173 35bd8fed 2019-05-09 stsp logmsg, print_status, NULL, repo);
2174 c4296144 2019-05-09 stsp if (error)
2175 c4296144 2019-05-09 stsp goto done;
2177 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2178 c4296144 2019-05-09 stsp if (error)
2179 c4296144 2019-05-09 stsp goto done;
2180 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2183 c4296144 2019-05-09 stsp got_repo_close(repo);
2184 c4296144 2019-05-09 stsp if (worktree)
2185 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2186 c4296144 2019-05-09 stsp free(path);
2187 c4296144 2019-05-09 stsp free(cwd);
2188 c4296144 2019-05-09 stsp free(id_str);
2189 c4296144 2019-05-09 stsp return error;