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>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 99437157 2018-11-11 stsp #include <signal.h>
29 5c860e29 2018-03-12 stsp #include <stdio.h>
30 5c860e29 2018-03-12 stsp #include <stdlib.h>
31 5c860e29 2018-03-12 stsp #include <string.h>
32 5c860e29 2018-03-12 stsp #include <unistd.h>
33 c09a553d 2018-03-12 stsp #include <libgen.h>
34 c0768b0f 2018-06-10 stsp #include <time.h>
35 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 f42b1b34 2018-03-12 stsp #include "got_error.h"
38 f42b1b34 2018-03-12 stsp #include "got_object.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 f42b1b34 2018-03-12 stsp #include "got_repository.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 c09a553d 2018-03-12 stsp #include "got_worktree.h"
43 79109fed 2018-03-27 stsp #include "got_diff.h"
44 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
45 404c43c4 2018-06-21 stsp #include "got_blame.h"
46 63219cd2 2019-01-04 stsp #include "got_privsep.h"
47 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
49 5c860e29 2018-03-12 stsp #ifndef nitems
50 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
57 99437157 2018-11-11 stsp catch_sigint(int signo)
59 99437157 2018-11-11 stsp sigint_received = 1;
63 99437157 2018-11-11 stsp catch_sigpipe(int signo)
65 99437157 2018-11-11 stsp sigpipe_received = 1;
69 5c860e29 2018-03-12 stsp struct cmd {
70 5c860e29 2018-03-12 stsp const char *cmd_name;
71 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
72 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
73 46a0db7d 2018-03-12 stsp const char *cmd_descr;
76 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
78 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
81 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
82 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
83 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
84 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
85 d00136be 2019-03-26 stsp __dead static void usage_add(void);
86 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
87 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
88 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
90 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
91 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
94 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
95 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
96 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
97 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
98 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
99 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
100 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
101 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
104 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
105 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
106 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
107 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
108 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
109 1b6b95a8 2018-03-12 stsp "show repository history" },
110 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
111 b00d56cd 2018-04-01 stsp "compare files and directories" },
112 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
113 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
114 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
115 a67e2392 2019-03-26 stsp "list files and directories in repository" },
116 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
117 1b6b95a8 2018-03-12 stsp "show modification status of files" },
118 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
119 d0eebce4 2019-03-11 stsp "manage references in repository" },
120 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
121 bd14628f 2019-05-12 stsp "add new files to version control" },
122 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
123 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
124 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
125 a129376b 2019-03-28 stsp "revert uncommitted changes" },
126 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
127 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
131 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
133 5c860e29 2018-03-12 stsp struct cmd *cmd;
134 5c860e29 2018-03-12 stsp unsigned int i;
136 1b6b95a8 2018-03-12 stsp int hflag = 0;
138 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
140 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
141 5c860e29 2018-03-12 stsp switch (ch) {
147 5c860e29 2018-03-12 stsp /* NOTREACHED */
151 5c860e29 2018-03-12 stsp argc -= optind;
152 5c860e29 2018-03-12 stsp argv += optind;
153 1e70621d 2018-03-27 stsp optind = 0;
155 5c860e29 2018-03-12 stsp if (argc <= 0)
158 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
159 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
161 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
162 d7d4f210 2018-03-12 stsp const struct got_error *error;
164 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
166 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
170 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
172 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
173 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
174 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
181 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 4ed7e80c 2018-05-20 stsp __dead static void
186 5c860e29 2018-03-12 stsp usage(void)
190 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
192 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
193 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
194 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
199 0266afb7 2019-01-04 stsp static const struct got_error *
200 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
202 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
203 e2ba3d07 2019-05-13 stsp const char *editor;
205 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
206 e2ba3d07 2019-05-13 stsp if (editor == NULL)
207 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
209 0ee7065d 2019-05-13 stsp if (editor) {
210 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
212 0ee7065d 2019-05-13 stsp return err;
215 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
216 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
217 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
218 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
221 e2ba3d07 2019-05-13 stsp return NULL;
224 e2ba3d07 2019-05-13 stsp static const struct got_error *
225 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
226 0ee7065d 2019-05-13 stsp const char *worktree_path, int create_worktree, char **editor)
228 163ce85a 2019-05-13 stsp const struct got_error *err;
229 3c45a30a 2019-05-12 jcs static char err_msg[MAXPATHLEN + 36];
231 a0937847 2019-05-11 stsp if (create_worktree) {
232 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
233 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
235 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
236 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
238 163ce85a 2019-05-13 stsp err = NULL;
240 3c45a30a 2019-05-12 jcs snprintf(err_msg, sizeof(err_msg),
241 3c45a30a 2019-05-12 jcs "%s: directory exists but is not empty",
242 3c45a30a 2019-05-12 jcs worktree_path);
243 163ce85a 2019-05-13 stsp err = got_error_msg(GOT_ERR_BAD_PATH,
248 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
249 163ce85a 2019-05-13 stsp return err;
252 0ee7065d 2019-05-13 stsp if (editor) {
253 0ee7065d 2019-05-13 stsp err = get_editor(editor);
255 163ce85a 2019-05-13 stsp return err;
256 0ee7065d 2019-05-13 stsp if (unveil(*editor, "x") != 0) {
257 0ee7065d 2019-05-13 stsp err = got_error_from_errno2("unveil", *editor);
258 0ee7065d 2019-05-13 stsp free(*editor);
259 0ee7065d 2019-05-13 stsp *editor = NULL;
260 163ce85a 2019-05-13 stsp return err;
264 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
267 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
270 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
271 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
273 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
274 163ce85a 2019-05-13 stsp if (err != NULL)
275 163ce85a 2019-05-13 stsp return err;
277 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
278 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
280 0266afb7 2019-01-04 stsp return NULL;
283 4ed7e80c 2018-05-20 stsp __dead static void
284 c09a553d 2018-03-12 stsp usage_checkout(void)
286 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
287 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
291 92a684f4 2018-03-12 stsp static void
292 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
294 92a684f4 2018-03-12 stsp char *worktree_path = arg;
296 92a684f4 2018-03-12 stsp while (path[0] == '/')
299 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
302 99437157 2018-11-11 stsp static const struct got_error *
303 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
305 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
306 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
307 99437157 2018-11-11 stsp return NULL;
310 8069f636 2019-01-12 stsp static const struct got_error *
311 d5bea539 2019-05-13 stsp check_linear_ancestry(struct got_worktree *worktree,
312 d5bea539 2019-05-13 stsp struct got_object_id *commit_id, struct got_repository *repo)
314 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
315 d5bea539 2019-05-13 stsp struct got_object_id *yca_id, *base_commit_id;
317 d5bea539 2019-05-13 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
318 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
319 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
321 36a38700 2019-05-10 stsp return err;
323 d5bea539 2019-05-13 stsp if (yca_id == NULL)
324 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
327 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
328 d5bea539 2019-05-13 stsp * and the work tree's base commit.
330 d5bea539 2019-05-13 stsp * Non-linear situation such as the this require a rebase:
332 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
340 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
341 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
342 d5bea539 2019-05-13 stsp * Update backwards in time: D (base) - C - D - A (commit/yca)
344 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
345 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
346 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
348 d5bea539 2019-05-13 stsp free(yca_id);
349 d5bea539 2019-05-13 stsp return NULL;
353 4ed7e80c 2018-05-20 stsp static const struct got_error *
354 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
356 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
357 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
358 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
359 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
360 c09a553d 2018-03-12 stsp char *repo_path = NULL;
361 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
362 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
363 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
364 72151b04 2019-05-11 stsp int ch, same_path_prefix;
366 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
367 0bb8a95e 2018-03-12 stsp switch (ch) {
369 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
370 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
371 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
374 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
377 2deda0b9 2019-03-07 stsp usage_checkout();
378 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
382 0bb8a95e 2018-03-12 stsp argc -= optind;
383 0bb8a95e 2018-03-12 stsp argv += optind;
385 6715a751 2018-03-16 stsp #ifndef PROFILE
386 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
387 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
388 c09a553d 2018-03-12 stsp err(1, "pledge");
390 0bb8a95e 2018-03-12 stsp if (argc == 1) {
391 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
392 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
393 76089277 2018-04-01 stsp if (repo_path == NULL)
394 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
395 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
396 76089277 2018-04-01 stsp if (cwd == NULL) {
397 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
400 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
401 230a42bd 2019-05-11 jcs base = basename(path_prefix);
402 230a42bd 2019-05-11 jcs if (base == NULL) {
403 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
404 230a42bd 2019-05-11 jcs path_prefix);
408 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
409 230a42bd 2019-05-11 jcs if (base == NULL) {
410 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
415 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
416 c09a553d 2018-03-12 stsp if (dotgit)
417 c09a553d 2018-03-12 stsp *dotgit = '\0';
418 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
419 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
424 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
425 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
426 76089277 2018-04-01 stsp if (repo_path == NULL) {
427 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
430 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
431 76089277 2018-04-01 stsp if (worktree_path == NULL) {
432 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
436 c09a553d 2018-03-12 stsp usage_checkout();
438 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
439 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
441 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
442 c09a553d 2018-03-12 stsp if (error != NULL)
445 0ee7065d 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1,
450 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
451 c09a553d 2018-03-12 stsp if (error != NULL)
454 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
455 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
458 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
459 c09a553d 2018-03-12 stsp if (error != NULL)
462 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
463 e5dc7198 2018-12-29 stsp path_prefix);
464 e5dc7198 2018-12-29 stsp if (error != NULL)
466 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
467 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
471 8069f636 2019-01-12 stsp if (commit_id_str) {
472 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
473 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
474 8069f636 2019-01-12 stsp commit_id_str);
475 8069f636 2019-01-12 stsp if (error != NULL)
477 d5bea539 2019-05-13 stsp error = check_linear_ancestry(worktree, commit_id, repo);
478 8069f636 2019-01-12 stsp if (error != NULL) {
479 8069f636 2019-01-12 stsp free(commit_id);
482 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
483 8069f636 2019-01-12 stsp commit_id);
484 8069f636 2019-01-12 stsp free(commit_id);
489 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
490 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
491 c09a553d 2018-03-12 stsp if (error != NULL)
494 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
497 8069f636 2019-01-12 stsp free(commit_id_str);
498 76089277 2018-04-01 stsp free(repo_path);
499 507dc3bb 2018-12-29 stsp free(worktree_path);
500 507dc3bb 2018-12-29 stsp return error;
503 507dc3bb 2018-12-29 stsp __dead static void
504 507dc3bb 2018-12-29 stsp usage_update(void)
506 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
507 507dc3bb 2018-12-29 stsp getprogname());
511 507dc3bb 2018-12-29 stsp static void
512 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
514 784955db 2019-01-12 stsp int *did_something = arg;
516 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
519 1545c615 2019-02-10 stsp *did_something = 1;
520 507dc3bb 2018-12-29 stsp while (path[0] == '/')
522 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
525 be7061eb 2018-12-30 stsp static const struct got_error *
526 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
528 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
529 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
530 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
531 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
532 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
533 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
534 784955db 2019-01-12 stsp int ch, did_something = 0;
536 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
537 507dc3bb 2018-12-29 stsp switch (ch) {
539 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
540 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
541 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
544 2deda0b9 2019-03-07 stsp usage_update();
545 507dc3bb 2018-12-29 stsp /* NOTREACHED */
549 507dc3bb 2018-12-29 stsp argc -= optind;
550 507dc3bb 2018-12-29 stsp argv += optind;
552 507dc3bb 2018-12-29 stsp #ifndef PROFILE
553 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
554 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
555 507dc3bb 2018-12-29 stsp err(1, "pledge");
557 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
558 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
559 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
562 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
566 507dc3bb 2018-12-29 stsp if (argc == 0) {
567 c4cdcb68 2019-04-03 stsp path = strdup("");
568 c4cdcb68 2019-04-03 stsp if (path == NULL) {
569 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
572 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
573 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
577 507dc3bb 2018-12-29 stsp usage_update();
579 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
580 507dc3bb 2018-12-29 stsp if (error != NULL)
583 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
584 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, NULL);
588 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
589 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
590 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
591 507dc3bb 2018-12-29 stsp if (error != NULL)
593 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
594 9c4b8182 2019-01-02 stsp if (error != NULL)
596 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
597 507dc3bb 2018-12-29 stsp if (error != NULL)
600 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
601 507dc3bb 2018-12-29 stsp commit_id_str);
602 507dc3bb 2018-12-29 stsp if (error != NULL)
606 d5bea539 2019-05-13 stsp error = check_linear_ancestry(worktree, commit_id, repo);
607 be7061eb 2018-12-30 stsp if (error != NULL)
610 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
611 507dc3bb 2018-12-29 stsp commit_id) != 0) {
612 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
613 507dc3bb 2018-12-29 stsp commit_id);
618 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
619 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
620 507dc3bb 2018-12-29 stsp if (error != NULL)
623 784955db 2019-01-12 stsp if (did_something)
624 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
626 784955db 2019-01-12 stsp printf("Already up-to-date\n");
628 c09a553d 2018-03-12 stsp free(worktree_path);
629 c4cdcb68 2019-04-03 stsp free(path);
630 507dc3bb 2018-12-29 stsp free(commit_id);
631 9c4b8182 2019-01-02 stsp free(commit_id_str);
632 c09a553d 2018-03-12 stsp return error;
635 f42b1b34 2018-03-12 stsp static const struct got_error *
636 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
637 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
639 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
640 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
641 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
642 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
644 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
645 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
647 79109fed 2018-03-27 stsp return err;
649 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
650 79f35eb3 2018-06-11 stsp if (qid != NULL) {
651 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
653 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
655 79109fed 2018-03-27 stsp return err;
657 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
658 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
659 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
661 0f2b3dca 2018-12-22 stsp return err;
663 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
665 79109fed 2018-03-27 stsp return err;
668 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
672 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
673 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
676 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
677 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
678 0f2b3dca 2018-12-22 stsp free(id_str1);
679 0f2b3dca 2018-12-22 stsp free(id_str2);
680 79109fed 2018-03-27 stsp return err;
683 4bb494d5 2018-06-16 stsp static char *
684 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
686 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
687 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
693 79109fed 2018-03-27 stsp static const struct got_error *
694 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
695 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
696 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
698 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
699 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
700 6c281f94 2018-06-11 stsp char datebuf[26];
701 45d799e2 2018-12-23 stsp time_t committer_time;
702 45d799e2 2018-12-23 stsp const char *author, *committer;
703 199a4027 2019-02-02 stsp char *refs_str = NULL;
704 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
706 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
708 199a4027 2019-02-02 stsp const char *name;
709 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
711 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
712 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
714 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
716 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
718 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
720 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
722 199a4027 2019-02-02 stsp s = refs_str;
723 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
724 199a4027 2019-02-02 stsp name) == -1) {
725 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
731 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
733 8bf5b3c9 2018-03-17 stsp return err;
735 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
736 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
737 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
738 832c249c 2018-06-10 stsp free(id_str);
739 d3d493d7 2019-02-21 stsp id_str = NULL;
740 d3d493d7 2019-02-21 stsp free(refs_str);
741 d3d493d7 2019-02-21 stsp refs_str = NULL;
742 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
743 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
744 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
745 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
746 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
747 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
748 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
749 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
750 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
751 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
752 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
754 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
755 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
756 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
758 a0603db2 2018-06-10 stsp return err;
759 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
760 a0603db2 2018-06-10 stsp free(id_str);
764 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
765 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
766 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
768 621015ac 2018-07-23 stsp logmsg = logmsg0;
770 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
772 832c249c 2018-06-10 stsp printf(" %s\n", line);
773 832c249c 2018-06-10 stsp } while (line);
774 621015ac 2018-07-23 stsp free(logmsg0);
776 971751ac 2018-03-27 stsp if (show_patch) {
777 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
778 971751ac 2018-03-27 stsp if (err == 0)
779 971751ac 2018-03-27 stsp printf("\n");
782 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
783 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
784 07862c20 2018-09-15 stsp return err;
787 f42b1b34 2018-03-12 stsp static const struct got_error *
788 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
789 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
790 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
792 f42b1b34 2018-03-12 stsp const struct got_error *err;
793 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
795 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
796 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
798 f42b1b34 2018-03-12 stsp return err;
799 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
803 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
804 372ccdbb 2018-06-10 stsp struct got_object_id *id;
806 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
809 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
811 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
812 9ba79e04 2018-06-11 stsp err = NULL;
815 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
817 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
823 b43fbaa0 2018-06-11 stsp if (id == NULL)
826 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
829 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
831 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
832 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
836 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
837 f42b1b34 2018-03-12 stsp return err;
840 4ed7e80c 2018-05-20 stsp __dead static void
841 6f3d1eb0 2018-03-12 stsp usage_log(void)
843 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
844 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
848 4ed7e80c 2018-05-20 stsp static const struct got_error *
849 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
851 f42b1b34 2018-03-12 stsp const struct got_error *error;
852 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
853 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
854 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
855 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
856 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
857 d142fc45 2018-04-01 stsp char *start_commit = NULL;
858 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
859 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
860 64a96a6d 2018-04-01 stsp const char *errstr;
861 199a4027 2019-02-02 stsp struct got_reflist_head refs;
863 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
865 6715a751 2018-03-16 stsp #ifndef PROFILE
866 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
869 f42b1b34 2018-03-12 stsp err(1, "pledge");
872 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
873 79109fed 2018-03-27 stsp switch (ch) {
875 79109fed 2018-03-27 stsp show_patch = 1;
878 d142fc45 2018-04-01 stsp start_commit = optarg;
881 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
883 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
884 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
887 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
888 64a96a6d 2018-04-01 stsp if (errstr != NULL)
889 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
892 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
895 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
896 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
897 04ca23f4 2018-07-16 stsp err(1, "-r option");
898 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
901 2deda0b9 2019-03-07 stsp usage_log();
902 79109fed 2018-03-27 stsp /* NOTREACHED */
906 79109fed 2018-03-27 stsp argc -= optind;
907 79109fed 2018-03-27 stsp argv += optind;
909 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
910 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
911 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
915 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
916 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
918 cffc0aa4 2019-02-05 stsp error = NULL;
920 e7301579 2019-03-18 stsp if (argc == 0) {
921 cbd1af7a 2019-03-18 stsp path = strdup("");
922 e7301579 2019-03-18 stsp if (path == NULL) {
923 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
926 e7301579 2019-03-18 stsp } else if (argc == 1) {
927 e7301579 2019-03-18 stsp if (worktree) {
928 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
933 e7301579 2019-03-18 stsp path = strdup(argv[0]);
934 e7301579 2019-03-18 stsp if (path == NULL) {
935 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
940 cbd1af7a 2019-03-18 stsp usage_log();
942 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
943 5486daa2 2019-05-11 stsp repo_path = worktree ?
944 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
946 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
947 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
951 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
952 d7d4f210 2018-03-12 stsp if (error != NULL)
955 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
956 0ee7065d 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
960 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
961 3235492e 2018-04-01 stsp struct got_reference *head_ref;
962 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
963 3235492e 2018-04-01 stsp if (error != NULL)
964 3235492e 2018-04-01 stsp return error;
965 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
966 3235492e 2018-04-01 stsp got_ref_close(head_ref);
967 3235492e 2018-04-01 stsp if (error != NULL)
968 3235492e 2018-04-01 stsp return error;
969 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
971 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
972 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
973 3235492e 2018-04-01 stsp if (error == NULL) {
974 1caad61b 2019-02-01 stsp int obj_type;
975 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
976 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
977 d1f2edc9 2018-06-13 stsp if (error != NULL)
979 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
980 1caad61b 2019-02-01 stsp if (error != NULL)
982 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
983 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
984 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
985 1caad61b 2019-02-01 stsp if (error != NULL)
987 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
988 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
989 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
990 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
994 1caad61b 2019-02-01 stsp id = got_object_id_dup(
995 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
996 1caad61b 2019-02-01 stsp if (id == NULL)
997 638f9024 2019-05-13 stsp error = got_error_from_errno(
998 230a42bd 2019-05-11 jcs "got_object_id_dup");
999 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1000 1caad61b 2019-02-01 stsp if (error)
1001 1caad61b 2019-02-01 stsp goto done;
1002 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1003 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1004 1caad61b 2019-02-01 stsp goto done;
1006 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1007 d1f2edc9 2018-06-13 stsp if (error != NULL)
1008 1caad61b 2019-02-01 stsp goto done;
1010 15a94983 2018-12-23 stsp if (commit == NULL) {
1011 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
1012 d1f2edc9 2018-06-13 stsp start_commit);
1013 d1f2edc9 2018-06-13 stsp if (error != NULL)
1014 d1f2edc9 2018-06-13 stsp return error;
1017 d7d4f210 2018-03-12 stsp if (error != NULL)
1018 04ca23f4 2018-07-16 stsp goto done;
1020 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1021 04ca23f4 2018-07-16 stsp if (error != NULL)
1022 04ca23f4 2018-07-16 stsp goto done;
1023 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1024 04ca23f4 2018-07-16 stsp free(path);
1025 04ca23f4 2018-07-16 stsp path = in_repo_path;
1028 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1029 199a4027 2019-02-02 stsp if (error)
1030 199a4027 2019-02-02 stsp goto done;
1032 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1033 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1035 04ca23f4 2018-07-16 stsp free(path);
1036 04ca23f4 2018-07-16 stsp free(repo_path);
1037 04ca23f4 2018-07-16 stsp free(cwd);
1039 cffc0aa4 2019-02-05 stsp if (worktree)
1040 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1041 ad242220 2018-09-08 stsp if (repo) {
1042 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1043 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1044 ad242220 2018-09-08 stsp if (error == NULL)
1045 ad242220 2018-09-08 stsp error = repo_error;
1047 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1048 8bf5b3c9 2018-03-17 stsp return error;
1051 4ed7e80c 2018-05-20 stsp __dead static void
1052 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1054 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1055 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1059 b72f483a 2019-02-05 stsp struct print_diff_arg {
1060 b72f483a 2019-02-05 stsp struct got_repository *repo;
1061 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1062 b72f483a 2019-02-05 stsp int diff_context;
1063 3fc0c068 2019-02-10 stsp const char *id_str;
1064 3fc0c068 2019-02-10 stsp int header_shown;
1067 4ed7e80c 2018-05-20 stsp static const struct got_error *
1068 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1069 b72f483a 2019-02-05 stsp struct got_object_id *id)
1071 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1072 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1073 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1074 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1075 b72f483a 2019-02-05 stsp char *abspath = NULL;
1076 b72f483a 2019-02-05 stsp struct stat sb;
1078 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1079 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1080 b72f483a 2019-02-05 stsp return NULL;
1082 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1083 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1084 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1085 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1088 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1089 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1091 d00136be 2019-03-26 stsp goto done;
1095 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1096 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1097 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1098 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1099 2ec1f75b 2019-03-26 stsp goto done;
1102 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1103 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1105 2ec1f75b 2019-03-26 stsp goto done;
1107 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1108 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1109 2ec1f75b 2019-03-26 stsp goto done;
1112 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1114 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1117 b72f483a 2019-02-05 stsp if (blob1)
1118 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1119 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1120 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1121 b72f483a 2019-02-05 stsp free(abspath);
1122 927df6b7 2019-02-10 stsp return err;
1125 927df6b7 2019-02-10 stsp static const struct got_error *
1126 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1128 b00d56cd 2018-04-01 stsp const struct got_error *error;
1129 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1130 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1131 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1132 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1133 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1134 15a94983 2018-12-23 stsp int type1, type2;
1135 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1136 c0cc5c62 2018-10-18 stsp const char *errstr;
1137 927df6b7 2019-02-10 stsp char *path = NULL;
1139 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1140 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1141 25eccc22 2019-01-04 stsp NULL) == -1)
1142 b00d56cd 2018-04-01 stsp err(1, "pledge");
1145 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1146 b00d56cd 2018-04-01 stsp switch (ch) {
1148 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1149 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1150 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1153 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1154 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1155 b72f483a 2019-02-05 stsp err(1, "-r option");
1156 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1159 2deda0b9 2019-03-07 stsp usage_diff();
1160 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1164 b00d56cd 2018-04-01 stsp argc -= optind;
1165 b00d56cd 2018-04-01 stsp argv += optind;
1167 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1168 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1169 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1170 b72f483a 2019-02-05 stsp goto done;
1172 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1173 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1174 927df6b7 2019-02-10 stsp goto done;
1175 927df6b7 2019-02-10 stsp if (argc <= 1) {
1176 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1177 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1178 927df6b7 2019-02-10 stsp goto done;
1180 b72f483a 2019-02-05 stsp if (repo_path)
1182 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1183 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1184 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1185 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1186 927df6b7 2019-02-10 stsp goto done;
1188 927df6b7 2019-02-10 stsp if (argc == 1) {
1189 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1191 927df6b7 2019-02-10 stsp if (error)
1192 927df6b7 2019-02-10 stsp goto done;
1194 927df6b7 2019-02-10 stsp path = strdup("");
1195 927df6b7 2019-02-10 stsp if (path == NULL) {
1196 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1197 927df6b7 2019-02-10 stsp goto done;
1200 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1201 15a94983 2018-12-23 stsp id_str1 = argv[0];
1202 15a94983 2018-12-23 stsp id_str2 = argv[1];
1204 b00d56cd 2018-04-01 stsp usage_diff();
1206 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1207 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1208 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1209 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1212 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1213 76089277 2018-04-01 stsp free(repo_path);
1214 b00d56cd 2018-04-01 stsp if (error != NULL)
1215 b00d56cd 2018-04-01 stsp goto done;
1217 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1218 0ee7065d 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
1219 c02c541e 2019-03-29 stsp if (error)
1220 c02c541e 2019-03-29 stsp goto done;
1222 b72f483a 2019-02-05 stsp if (worktree) {
1223 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1224 b72f483a 2019-02-05 stsp char *id_str;
1225 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1226 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1227 b72f483a 2019-02-05 stsp if (error)
1228 b72f483a 2019-02-05 stsp goto done;
1229 b72f483a 2019-02-05 stsp arg.repo = repo;
1230 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1231 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1232 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1233 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1235 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1236 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1237 b72f483a 2019-02-05 stsp free(id_str);
1238 b72f483a 2019-02-05 stsp goto done;
1241 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1242 6402fb3c 2018-09-15 stsp if (error)
1243 b00d56cd 2018-04-01 stsp goto done;
1245 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1246 6402fb3c 2018-09-15 stsp if (error)
1247 b00d56cd 2018-04-01 stsp goto done;
1249 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1250 15a94983 2018-12-23 stsp if (error)
1251 15a94983 2018-12-23 stsp goto done;
1253 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1254 15a94983 2018-12-23 stsp if (error)
1255 15a94983 2018-12-23 stsp goto done;
1257 15a94983 2018-12-23 stsp if (type1 != type2) {
1258 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1259 b00d56cd 2018-04-01 stsp goto done;
1262 15a94983 2018-12-23 stsp switch (type1) {
1263 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1264 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1265 54156555 2018-12-24 stsp diff_context, repo, stdout);
1267 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1268 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1269 54156555 2018-12-24 stsp diff_context, repo, stdout);
1271 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1272 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1274 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1275 c0cc5c62 2018-10-18 stsp repo, stdout);
1278 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1282 15a94983 2018-12-23 stsp free(id1);
1283 15a94983 2018-12-23 stsp free(id2);
1284 927df6b7 2019-02-10 stsp free(path);
1285 b72f483a 2019-02-05 stsp if (worktree)
1286 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1287 ad242220 2018-09-08 stsp if (repo) {
1288 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1289 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1290 ad242220 2018-09-08 stsp if (error == NULL)
1291 ad242220 2018-09-08 stsp error = repo_error;
1293 b00d56cd 2018-04-01 stsp return error;
1296 404c43c4 2018-06-21 stsp __dead static void
1297 404c43c4 2018-06-21 stsp usage_blame(void)
1299 9270e621 2019-02-05 stsp fprintf(stderr,
1300 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1301 404c43c4 2018-06-21 stsp getprogname());
1305 404c43c4 2018-06-21 stsp static const struct got_error *
1306 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1308 404c43c4 2018-06-21 stsp const struct got_error *error;
1309 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1310 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1311 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1312 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1313 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1316 404c43c4 2018-06-21 stsp #ifndef PROFILE
1317 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1318 36e2fb66 2019-01-04 stsp NULL) == -1)
1319 404c43c4 2018-06-21 stsp err(1, "pledge");
1322 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1323 404c43c4 2018-06-21 stsp switch (ch) {
1325 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1328 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1329 66bea077 2018-08-02 stsp if (repo_path == NULL)
1330 66bea077 2018-08-02 stsp err(1, "-r option");
1331 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1334 2deda0b9 2019-03-07 stsp usage_blame();
1335 404c43c4 2018-06-21 stsp /* NOTREACHED */
1339 404c43c4 2018-06-21 stsp argc -= optind;
1340 404c43c4 2018-06-21 stsp argv += optind;
1342 a39318fd 2018-08-02 stsp if (argc == 1)
1343 404c43c4 2018-06-21 stsp path = argv[0];
1345 404c43c4 2018-06-21 stsp usage_blame();
1347 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1348 66bea077 2018-08-02 stsp if (cwd == NULL) {
1349 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1350 66bea077 2018-08-02 stsp goto done;
1352 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1353 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1354 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1355 66bea077 2018-08-02 stsp goto done;
1357 0c06baac 2019-02-05 stsp error = NULL;
1358 0c06baac 2019-02-05 stsp if (worktree) {
1359 0c06baac 2019-02-05 stsp repo_path =
1360 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1361 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1362 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1363 0c06baac 2019-02-05 stsp if (error)
1364 0c06baac 2019-02-05 stsp goto done;
1366 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1367 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1368 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1369 0c06baac 2019-02-05 stsp goto done;
1374 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1375 c02c541e 2019-03-29 stsp if (error != NULL)
1376 36e2fb66 2019-01-04 stsp goto done;
1378 0ee7065d 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, NULL);
1379 c02c541e 2019-03-29 stsp if (error)
1380 404c43c4 2018-06-21 stsp goto done;
1382 0c06baac 2019-02-05 stsp if (worktree) {
1383 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1384 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1385 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1386 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1387 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1388 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1389 6efaaa2d 2019-02-05 stsp path) == -1) {
1390 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1391 0c06baac 2019-02-05 stsp goto done;
1393 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1396 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1398 0c06baac 2019-02-05 stsp if (error)
1399 66bea077 2018-08-02 stsp goto done;
1401 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1402 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1403 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1404 404c43c4 2018-06-21 stsp if (error != NULL)
1405 66bea077 2018-08-02 stsp goto done;
1406 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1407 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1408 404c43c4 2018-06-21 stsp if (error != NULL)
1409 66bea077 2018-08-02 stsp goto done;
1411 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1412 15a94983 2018-12-23 stsp commit_id_str);
1413 404c43c4 2018-06-21 stsp if (error != NULL)
1414 66bea077 2018-08-02 stsp goto done;
1417 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1419 66bea077 2018-08-02 stsp free(in_repo_path);
1420 66bea077 2018-08-02 stsp free(repo_path);
1421 66bea077 2018-08-02 stsp free(cwd);
1422 404c43c4 2018-06-21 stsp free(commit_id);
1423 0c06baac 2019-02-05 stsp if (worktree)
1424 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1425 ad242220 2018-09-08 stsp if (repo) {
1426 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1427 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1428 ad242220 2018-09-08 stsp if (error == NULL)
1429 ad242220 2018-09-08 stsp error = repo_error;
1431 404c43c4 2018-06-21 stsp return error;
1434 5de5890b 2018-10-18 stsp __dead static void
1435 5de5890b 2018-10-18 stsp usage_tree(void)
1437 c1669e2e 2019-01-09 stsp fprintf(stderr,
1438 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1439 5de5890b 2018-10-18 stsp getprogname());
1443 c1669e2e 2019-01-09 stsp static void
1444 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1445 c1669e2e 2019-01-09 stsp const char *root_path)
1447 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1449 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1450 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1453 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1454 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1455 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1458 5de5890b 2018-10-18 stsp static const struct got_error *
1459 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1460 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1461 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1463 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1464 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1465 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1466 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1467 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1469 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1471 5de5890b 2018-10-18 stsp goto done;
1473 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1475 5de5890b 2018-10-18 stsp goto done;
1476 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1477 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1478 5de5890b 2018-10-18 stsp while (te) {
1479 5de5890b 2018-10-18 stsp char *id = NULL;
1481 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1484 5de5890b 2018-10-18 stsp if (show_ids) {
1485 5de5890b 2018-10-18 stsp char *id_str;
1486 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1488 5de5890b 2018-10-18 stsp goto done;
1489 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1490 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1491 5de5890b 2018-10-18 stsp free(id_str);
1492 5de5890b 2018-10-18 stsp goto done;
1494 5de5890b 2018-10-18 stsp free(id_str);
1496 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1499 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1500 c1669e2e 2019-01-09 stsp char *child_path;
1501 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1502 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1503 c1669e2e 2019-01-09 stsp te->name) == -1) {
1504 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1505 c1669e2e 2019-01-09 stsp goto done;
1507 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1508 c1669e2e 2019-01-09 stsp root_path, repo);
1509 c1669e2e 2019-01-09 stsp free(child_path);
1511 c1669e2e 2019-01-09 stsp goto done;
1514 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1518 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1519 5de5890b 2018-10-18 stsp free(tree_id);
1520 5de5890b 2018-10-18 stsp return err;
1523 5de5890b 2018-10-18 stsp static const struct got_error *
1524 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1526 5de5890b 2018-10-18 stsp const struct got_error *error;
1527 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1528 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1529 7a2c19d6 2019-02-05 stsp const char *path;
1530 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1531 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1532 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1533 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1536 5de5890b 2018-10-18 stsp #ifndef PROFILE
1537 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1538 0f8d269b 2019-01-04 stsp NULL) == -1)
1539 5de5890b 2018-10-18 stsp err(1, "pledge");
1542 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1543 5de5890b 2018-10-18 stsp switch (ch) {
1545 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1548 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1549 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1550 5de5890b 2018-10-18 stsp err(1, "-r option");
1551 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1554 5de5890b 2018-10-18 stsp show_ids = 1;
1557 c1669e2e 2019-01-09 stsp recurse = 1;
1560 2deda0b9 2019-03-07 stsp usage_tree();
1561 5de5890b 2018-10-18 stsp /* NOTREACHED */
1565 5de5890b 2018-10-18 stsp argc -= optind;
1566 5de5890b 2018-10-18 stsp argv += optind;
1568 5de5890b 2018-10-18 stsp if (argc == 1)
1569 5de5890b 2018-10-18 stsp path = argv[0];
1570 5de5890b 2018-10-18 stsp else if (argc > 1)
1571 5de5890b 2018-10-18 stsp usage_tree();
1573 7a2c19d6 2019-02-05 stsp path = NULL;
1575 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1576 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1577 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1578 9bf7a39b 2019-02-05 stsp goto done;
1580 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1581 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1582 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1583 7a2c19d6 2019-02-05 stsp goto done;
1585 7a2c19d6 2019-02-05 stsp error = NULL;
1586 7a2c19d6 2019-02-05 stsp if (worktree) {
1587 7a2c19d6 2019-02-05 stsp repo_path =
1588 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1589 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1590 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1591 7a2c19d6 2019-02-05 stsp if (error)
1592 7a2c19d6 2019-02-05 stsp goto done;
1594 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1595 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1596 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1597 7a2c19d6 2019-02-05 stsp goto done;
1602 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1603 5de5890b 2018-10-18 stsp if (error != NULL)
1604 c02c541e 2019-03-29 stsp goto done;
1606 0ee7065d 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, NULL);
1607 c02c541e 2019-03-29 stsp if (error)
1608 5de5890b 2018-10-18 stsp goto done;
1610 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1611 9bf7a39b 2019-02-05 stsp if (worktree) {
1612 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1613 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1614 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1615 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1616 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1617 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1618 9bf7a39b 2019-02-05 stsp goto done;
1620 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1622 9bf7a39b 2019-02-05 stsp if (error)
1623 9bf7a39b 2019-02-05 stsp goto done;
1625 9bf7a39b 2019-02-05 stsp path = "/";
1627 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1628 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1629 9bf7a39b 2019-02-05 stsp if (error != NULL)
1630 9bf7a39b 2019-02-05 stsp goto done;
1633 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1634 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1635 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1636 5de5890b 2018-10-18 stsp if (error != NULL)
1637 5de5890b 2018-10-18 stsp goto done;
1638 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1639 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1640 5de5890b 2018-10-18 stsp if (error != NULL)
1641 5de5890b 2018-10-18 stsp goto done;
1643 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1644 15a94983 2018-12-23 stsp commit_id_str);
1645 5de5890b 2018-10-18 stsp if (error != NULL)
1646 5de5890b 2018-10-18 stsp goto done;
1649 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1650 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1652 5de5890b 2018-10-18 stsp free(in_repo_path);
1653 5de5890b 2018-10-18 stsp free(repo_path);
1654 5de5890b 2018-10-18 stsp free(cwd);
1655 5de5890b 2018-10-18 stsp free(commit_id);
1656 7a2c19d6 2019-02-05 stsp if (worktree)
1657 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1658 5de5890b 2018-10-18 stsp if (repo) {
1659 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1660 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1661 5de5890b 2018-10-18 stsp if (error == NULL)
1662 5de5890b 2018-10-18 stsp error = repo_error;
1664 5de5890b 2018-10-18 stsp return error;
1667 6bad629b 2019-02-04 stsp __dead static void
1668 6bad629b 2019-02-04 stsp usage_status(void)
1670 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1674 b72f483a 2019-02-05 stsp static const struct got_error *
1675 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1676 b72f483a 2019-02-05 stsp struct got_object_id *id)
1678 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1679 b72f483a 2019-02-05 stsp return NULL;
1682 6bad629b 2019-02-04 stsp static const struct got_error *
1683 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1685 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1686 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1687 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1688 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1691 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1692 6bad629b 2019-02-04 stsp switch (ch) {
1694 2deda0b9 2019-03-07 stsp usage_status();
1695 6bad629b 2019-02-04 stsp /* NOTREACHED */
1699 6bad629b 2019-02-04 stsp argc -= optind;
1700 6bad629b 2019-02-04 stsp argv += optind;
1702 6bad629b 2019-02-04 stsp #ifndef PROFILE
1703 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1704 6bad629b 2019-02-04 stsp NULL) == -1)
1705 6bad629b 2019-02-04 stsp err(1, "pledge");
1707 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1708 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1709 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1710 927df6b7 2019-02-10 stsp goto done;
1713 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1714 927df6b7 2019-02-10 stsp if (error != NULL)
1715 927df6b7 2019-02-10 stsp goto done;
1717 6bad629b 2019-02-04 stsp if (argc == 0) {
1718 927df6b7 2019-02-10 stsp path = strdup("");
1719 927df6b7 2019-02-10 stsp if (path == NULL) {
1720 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1721 6bad629b 2019-02-04 stsp goto done;
1723 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1724 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1725 927df6b7 2019-02-10 stsp if (error)
1726 6bad629b 2019-02-04 stsp goto done;
1728 6bad629b 2019-02-04 stsp usage_status();
1730 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1731 6bad629b 2019-02-04 stsp if (error != NULL)
1732 6bad629b 2019-02-04 stsp goto done;
1734 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1735 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, NULL);
1736 6bad629b 2019-02-04 stsp if (error)
1737 6bad629b 2019-02-04 stsp goto done;
1739 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1740 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1742 927df6b7 2019-02-10 stsp free(cwd);
1743 927df6b7 2019-02-10 stsp free(path);
1744 d0eebce4 2019-03-11 stsp return error;
1747 d0eebce4 2019-03-11 stsp __dead static void
1748 d0eebce4 2019-03-11 stsp usage_ref(void)
1750 d0eebce4 2019-03-11 stsp fprintf(stderr,
1751 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1752 d0eebce4 2019-03-11 stsp getprogname());
1756 d0eebce4 2019-03-11 stsp static const struct got_error *
1757 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1759 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1760 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1761 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1763 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1764 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1766 d0eebce4 2019-03-11 stsp return err;
1768 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1769 d0eebce4 2019-03-11 stsp char *refstr;
1770 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1771 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1772 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
1773 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1774 d0eebce4 2019-03-11 stsp free(refstr);
1777 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1778 d0eebce4 2019-03-11 stsp return NULL;
1781 d0eebce4 2019-03-11 stsp static const struct got_error *
1782 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1784 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1785 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1787 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
1789 d0eebce4 2019-03-11 stsp return err;
1791 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1792 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1793 d0eebce4 2019-03-11 stsp return err;
1796 d0eebce4 2019-03-11 stsp static const struct got_error *
1797 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1799 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1800 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1801 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1803 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1805 d0eebce4 2019-03-11 stsp return err;
1807 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1809 d0eebce4 2019-03-11 stsp goto done;
1811 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1814 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1816 d0eebce4 2019-03-11 stsp return err;
1819 d0eebce4 2019-03-11 stsp static const struct got_error *
1820 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1822 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1823 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1824 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1825 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1826 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1827 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1829 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1830 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1831 d0eebce4 2019-03-11 stsp switch (ch) {
1833 d0eebce4 2019-03-11 stsp delref = optarg;
1836 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1837 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1838 d0eebce4 2019-03-11 stsp err(1, "-r option");
1839 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1842 d0eebce4 2019-03-11 stsp do_list = 1;
1845 d0eebce4 2019-03-11 stsp usage_ref();
1846 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1850 d0eebce4 2019-03-11 stsp if (do_list && delref)
1851 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1853 d0eebce4 2019-03-11 stsp argc -= optind;
1854 d0eebce4 2019-03-11 stsp argv += optind;
1856 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1857 d0eebce4 2019-03-11 stsp if (argc > 0)
1858 d0eebce4 2019-03-11 stsp usage_ref();
1859 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1860 d0eebce4 2019-03-11 stsp usage_ref();
1862 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1863 e0b57350 2019-03-12 stsp if (do_list) {
1864 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1865 e0b57350 2019-03-12 stsp NULL) == -1)
1866 e0b57350 2019-03-12 stsp err(1, "pledge");
1868 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1869 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1870 e0b57350 2019-03-12 stsp err(1, "pledge");
1873 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1874 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1875 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1876 d0eebce4 2019-03-11 stsp goto done;
1879 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1880 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1881 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1882 d0eebce4 2019-03-11 stsp goto done;
1884 d0eebce4 2019-03-11 stsp error = NULL;
1885 d0eebce4 2019-03-11 stsp if (worktree) {
1886 d0eebce4 2019-03-11 stsp repo_path =
1887 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1888 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1889 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1890 d0eebce4 2019-03-11 stsp if (error)
1891 d0eebce4 2019-03-11 stsp goto done;
1893 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1894 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1895 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1896 d0eebce4 2019-03-11 stsp goto done;
1901 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1902 d0eebce4 2019-03-11 stsp if (error != NULL)
1903 d0eebce4 2019-03-11 stsp goto done;
1905 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1906 0ee7065d 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
1907 c02c541e 2019-03-29 stsp if (error)
1908 c02c541e 2019-03-29 stsp goto done;
1910 d0eebce4 2019-03-11 stsp if (do_list)
1911 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1912 d0eebce4 2019-03-11 stsp else if (delref)
1913 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1915 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1918 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1919 d0eebce4 2019-03-11 stsp if (worktree)
1920 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1921 d0eebce4 2019-03-11 stsp free(cwd);
1922 d0eebce4 2019-03-11 stsp free(repo_path);
1923 d00136be 2019-03-26 stsp return error;
1926 d00136be 2019-03-26 stsp __dead static void
1927 d00136be 2019-03-26 stsp usage_add(void)
1929 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1933 d00136be 2019-03-26 stsp static const struct got_error *
1934 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1936 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1937 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1938 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1939 1dd54920 2019-05-11 stsp char *cwd = NULL;
1940 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
1941 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
1944 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
1946 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1947 d00136be 2019-03-26 stsp switch (ch) {
1949 d00136be 2019-03-26 stsp usage_add();
1950 d00136be 2019-03-26 stsp /* NOTREACHED */
1954 d00136be 2019-03-26 stsp argc -= optind;
1955 d00136be 2019-03-26 stsp argv += optind;
1957 723c305c 2019-05-11 jcs if (argc < 1)
1958 d00136be 2019-03-26 stsp usage_add();
1960 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
1961 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1962 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1963 723c305c 2019-05-11 jcs if (path == NULL) {
1964 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
1967 1dd54920 2019-05-11 stsp free(path);
1970 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1971 d00136be 2019-03-26 stsp if (cwd == NULL) {
1972 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1973 d00136be 2019-03-26 stsp goto done;
1976 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1977 d00136be 2019-03-26 stsp if (error)
1978 d00136be 2019-03-26 stsp goto done;
1980 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1981 031a5338 2019-03-26 stsp if (error != NULL)
1982 031a5338 2019-03-26 stsp goto done;
1984 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1985 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, NULL);
1986 d00136be 2019-03-26 stsp if (error)
1987 d00136be 2019-03-26 stsp goto done;
1989 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1990 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1991 723c305c 2019-05-11 jcs if (path == NULL) {
1992 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
1996 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
1997 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
1998 1dd54920 2019-05-11 stsp if (error) {
1999 1dd54920 2019-05-11 stsp free(path);
2003 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2004 1dd54920 2019-05-11 stsp NULL, repo);
2007 031a5338 2019-03-26 stsp got_repo_close(repo);
2008 d00136be 2019-03-26 stsp if (worktree)
2009 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2010 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2011 1dd54920 2019-05-11 stsp free((char *)pe->path);
2012 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2013 2ec1f75b 2019-03-26 stsp free(cwd);
2014 2ec1f75b 2019-03-26 stsp return error;
2017 2ec1f75b 2019-03-26 stsp __dead static void
2018 2ec1f75b 2019-03-26 stsp usage_rm(void)
2020 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2024 2ec1f75b 2019-03-26 stsp static const struct got_error *
2025 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2027 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2028 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2029 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2030 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
2031 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
2033 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2034 2ec1f75b 2019-03-26 stsp switch (ch) {
2036 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2039 2ec1f75b 2019-03-26 stsp usage_add();
2040 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2044 2ec1f75b 2019-03-26 stsp argc -= optind;
2045 2ec1f75b 2019-03-26 stsp argv += optind;
2047 2ec1f75b 2019-03-26 stsp if (argc != 1)
2048 2ec1f75b 2019-03-26 stsp usage_rm();
2050 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
2051 2ec1f75b 2019-03-26 stsp if (path == NULL) {
2052 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2053 2ec1f75b 2019-03-26 stsp goto done;
2055 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2057 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2058 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2059 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2060 2ec1f75b 2019-03-26 stsp goto done;
2062 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2063 2ec1f75b 2019-03-26 stsp if (error)
2064 2ec1f75b 2019-03-26 stsp goto done;
2066 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2068 2ec1f75b 2019-03-26 stsp goto done;
2070 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2071 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, NULL);
2072 2ec1f75b 2019-03-26 stsp if (error)
2073 2ec1f75b 2019-03-26 stsp goto done;
2075 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2076 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
2077 a129376b 2019-03-28 stsp if (error)
2078 a129376b 2019-03-28 stsp goto done;
2081 a129376b 2019-03-28 stsp got_repo_close(repo);
2082 a129376b 2019-03-28 stsp if (worktree)
2083 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2084 a129376b 2019-03-28 stsp free(path);
2085 a129376b 2019-03-28 stsp free(cwd);
2086 a129376b 2019-03-28 stsp return error;
2089 a129376b 2019-03-28 stsp __dead static void
2090 a129376b 2019-03-28 stsp usage_revert(void)
2092 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2096 a129376b 2019-03-28 stsp static void
2097 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2099 a129376b 2019-03-28 stsp while (path[0] == '/')
2101 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2104 a129376b 2019-03-28 stsp static const struct got_error *
2105 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2107 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2108 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2109 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2110 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2113 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2114 a129376b 2019-03-28 stsp switch (ch) {
2116 a129376b 2019-03-28 stsp usage_revert();
2117 a129376b 2019-03-28 stsp /* NOTREACHED */
2121 a129376b 2019-03-28 stsp argc -= optind;
2122 a129376b 2019-03-28 stsp argv += optind;
2124 a129376b 2019-03-28 stsp if (argc != 1)
2125 a129376b 2019-03-28 stsp usage_revert();
2127 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2128 a129376b 2019-03-28 stsp if (path == NULL) {
2129 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2130 a129376b 2019-03-28 stsp goto done;
2132 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2134 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2135 a129376b 2019-03-28 stsp if (cwd == NULL) {
2136 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2137 a129376b 2019-03-28 stsp goto done;
2139 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2140 2ec1f75b 2019-03-26 stsp if (error)
2141 2ec1f75b 2019-03-26 stsp goto done;
2143 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2144 a129376b 2019-03-28 stsp if (error != NULL)
2145 a129376b 2019-03-28 stsp goto done;
2147 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2148 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, NULL);
2149 a129376b 2019-03-28 stsp if (error)
2150 a129376b 2019-03-28 stsp goto done;
2152 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2153 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2154 a129376b 2019-03-28 stsp if (error)
2155 a129376b 2019-03-28 stsp goto done;
2158 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2159 2ec1f75b 2019-03-26 stsp if (worktree)
2160 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2161 2ec1f75b 2019-03-26 stsp free(path);
2162 d00136be 2019-03-26 stsp free(cwd);
2163 6bad629b 2019-02-04 stsp return error;
2166 c4296144 2019-05-09 stsp __dead static void
2167 c4296144 2019-05-09 stsp usage_commit(void)
2169 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2174 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2177 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2178 33ad4cbe 2019-05-12 jcs int st = -1;
2180 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2181 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2182 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2184 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2186 33ad4cbe 2019-05-12 jcs goto doneediting;
2188 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2189 33ad4cbe 2019-05-12 jcs _exit(127);
2192 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2193 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2196 33ad4cbe 2019-05-12 jcs doneediting:
2197 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2198 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2199 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2201 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2202 33ad4cbe 2019-05-12 jcs errno = EINTR;
2206 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2209 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2210 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2211 e2ba3d07 2019-05-13 stsp const char *editor;
2212 e0870e44 2019-05-13 stsp const char *worktree_path;
2213 e0870e44 2019-05-13 stsp char *logmsg_path;
2217 33ad4cbe 2019-05-12 jcs static const struct got_error *
2218 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2221 e0870e44 2019-05-13 stsp const char *initial_content = "\n# changes to be committed:\n";
2222 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2223 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2224 e0870e44 2019-05-13 stsp char *template = NULL;
2225 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2226 33ad4cbe 2019-05-12 jcs char buf[1024];
2227 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2229 33ad4cbe 2019-05-12 jcs size_t len;
2230 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2232 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2233 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2234 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2235 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2236 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2237 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2238 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2239 33ad4cbe 2019-05-12 jcs return NULL;
2242 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2243 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2245 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2247 e0870e44 2019-05-13 stsp goto done;
2249 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2251 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2252 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2253 33ad4cbe 2019-05-12 jcs dprintf(fd, "# %c %s\n", ct->status, pe->path);
2257 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2258 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2262 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2263 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2267 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2268 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2272 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2273 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2274 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2275 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2276 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2277 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2281 33ad4cbe 2019-05-12 jcs /* remove comments */
2282 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2283 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2285 fcde04d9 2019-05-13 stsp goto done;
2289 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2290 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2291 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2292 e0870e44 2019-05-13 stsp content_changed = 1;
2293 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2295 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2297 33ad4cbe 2019-05-12 jcs fclose(fp);
2299 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2300 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2304 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2305 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2306 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2307 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2308 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2309 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2313 e0870e44 2019-05-13 stsp free(template);
2314 33ad4cbe 2019-05-12 jcs return err;
2317 c4296144 2019-05-09 stsp static const struct got_error *
2318 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2320 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2321 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2322 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2323 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2324 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2325 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2326 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2327 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2328 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2331 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2332 c4296144 2019-05-09 stsp switch (ch) {
2334 c4296144 2019-05-09 stsp logmsg = optarg;
2337 c4296144 2019-05-09 stsp usage_commit();
2338 c4296144 2019-05-09 stsp /* NOTREACHED */
2342 c4296144 2019-05-09 stsp argc -= optind;
2343 c4296144 2019-05-09 stsp argv += optind;
2345 c4296144 2019-05-09 stsp if (argc == 1) {
2346 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2347 c4296144 2019-05-09 stsp if (path == NULL) {
2348 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2349 c4296144 2019-05-09 stsp goto done;
2351 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2352 c4296144 2019-05-09 stsp } else if (argc != 0)
2353 c4296144 2019-05-09 stsp usage_commit();
2355 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2356 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2357 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2358 35bd8fed 2019-05-09 stsp goto done;
2361 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2362 c4296144 2019-05-09 stsp if (cwd == NULL) {
2363 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2364 c4296144 2019-05-09 stsp goto done;
2366 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2367 c4296144 2019-05-09 stsp if (error)
2368 c4296144 2019-05-09 stsp goto done;
2370 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2371 c4296144 2019-05-09 stsp if (error != NULL)
2372 c4296144 2019-05-09 stsp goto done;
2374 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2375 0ee7065d 2019-05-13 stsp got_worktree_get_root_path(worktree), 0, &editor);
2376 c4296144 2019-05-09 stsp if (error)
2377 c4296144 2019-05-09 stsp goto done;
2379 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2380 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2381 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2382 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2383 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2384 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2385 e0870e44 2019-05-13 stsp if (error) {
2386 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2387 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2388 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2389 c4296144 2019-05-09 stsp goto done;
2392 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2393 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2395 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2396 c4296144 2019-05-09 stsp if (error)
2397 c4296144 2019-05-09 stsp goto done;
2398 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2401 c4296144 2019-05-09 stsp got_repo_close(repo);
2402 c4296144 2019-05-09 stsp if (worktree)
2403 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2404 c4296144 2019-05-09 stsp free(path);
2405 c4296144 2019-05-09 stsp free(cwd);
2406 c4296144 2019-05-09 stsp free(id_str);
2407 e2ba3d07 2019-05-13 stsp free(editor);
2408 c4296144 2019-05-09 stsp return error;