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 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
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 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
253 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
255 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
256 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
258 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
259 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
261 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
262 163ce85a 2019-05-13 stsp if (err != NULL)
263 163ce85a 2019-05-13 stsp return err;
265 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
266 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
268 0266afb7 2019-01-04 stsp return NULL;
271 4ed7e80c 2018-05-20 stsp __dead static void
272 c09a553d 2018-03-12 stsp usage_checkout(void)
274 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
275 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
279 92a684f4 2018-03-12 stsp static void
280 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
282 92a684f4 2018-03-12 stsp char *worktree_path = arg;
284 92a684f4 2018-03-12 stsp while (path[0] == '/')
287 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
290 99437157 2018-11-11 stsp static const struct got_error *
291 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
293 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
294 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
295 99437157 2018-11-11 stsp return NULL;
298 8069f636 2019-01-12 stsp static const struct got_error *
299 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
300 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
302 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
303 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
305 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
306 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
308 36a38700 2019-05-10 stsp return err;
310 d5bea539 2019-05-13 stsp if (yca_id == NULL)
311 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
314 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
315 d5bea539 2019-05-13 stsp * and the work tree's base commit.
317 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
319 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
327 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
328 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
329 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
331 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
332 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
333 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
335 d5bea539 2019-05-13 stsp free(yca_id);
336 d5bea539 2019-05-13 stsp return NULL;
339 a367ff0f 2019-05-14 stsp static const struct got_error *
340 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
341 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
343 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
344 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
345 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
346 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
348 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
352 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
356 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
361 a367ff0f 2019-05-14 stsp struct got_object_id *id;
362 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
364 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
365 a367ff0f 2019-05-14 stsp err = NULL;
368 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
370 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
377 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
378 a367ff0f 2019-05-14 stsp is_same_branch = 1;
385 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
386 a367ff0f 2019-05-14 stsp free(head_commit_id);
387 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
388 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
389 a367ff0f 2019-05-14 stsp return err;
392 4ed7e80c 2018-05-20 stsp static const struct got_error *
393 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
395 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
396 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
397 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
398 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
399 c09a553d 2018-03-12 stsp char *repo_path = NULL;
400 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
401 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
402 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
403 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
404 72151b04 2019-05-11 stsp int ch, same_path_prefix;
406 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
407 0bb8a95e 2018-03-12 stsp switch (ch) {
409 08573d5b 2019-05-14 stsp branch_name = optarg;
412 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
413 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
414 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
417 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
420 2deda0b9 2019-03-07 stsp usage_checkout();
421 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
425 0bb8a95e 2018-03-12 stsp argc -= optind;
426 0bb8a95e 2018-03-12 stsp argv += optind;
428 6715a751 2018-03-16 stsp #ifndef PROFILE
429 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
430 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
431 c09a553d 2018-03-12 stsp err(1, "pledge");
433 0bb8a95e 2018-03-12 stsp if (argc == 1) {
434 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
435 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
436 76089277 2018-04-01 stsp if (repo_path == NULL)
437 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
438 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
439 76089277 2018-04-01 stsp if (cwd == NULL) {
440 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
443 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
444 230a42bd 2019-05-11 jcs base = basename(path_prefix);
445 230a42bd 2019-05-11 jcs if (base == NULL) {
446 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
447 230a42bd 2019-05-11 jcs path_prefix);
451 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
452 230a42bd 2019-05-11 jcs if (base == NULL) {
453 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
458 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
459 c09a553d 2018-03-12 stsp if (dotgit)
460 c09a553d 2018-03-12 stsp *dotgit = '\0';
461 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
462 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
467 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
468 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
469 76089277 2018-04-01 stsp if (repo_path == NULL) {
470 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
473 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
474 76089277 2018-04-01 stsp if (worktree_path == NULL) {
475 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
479 c09a553d 2018-03-12 stsp usage_checkout();
481 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
482 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
484 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
485 c09a553d 2018-03-12 stsp if (error != NULL)
488 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
492 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
493 c09a553d 2018-03-12 stsp if (error != NULL)
496 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
497 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
500 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
501 c09a553d 2018-03-12 stsp if (error != NULL)
504 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
505 e5dc7198 2018-12-29 stsp path_prefix);
506 e5dc7198 2018-12-29 stsp if (error != NULL)
508 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
509 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
513 8069f636 2019-01-12 stsp if (commit_id_str) {
514 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
515 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
516 8069f636 2019-01-12 stsp commit_id_str);
517 8069f636 2019-01-12 stsp if (error != NULL)
519 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
520 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
521 8069f636 2019-01-12 stsp if (error != NULL) {
522 8069f636 2019-01-12 stsp free(commit_id);
525 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
528 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
529 8069f636 2019-01-12 stsp commit_id);
530 8069f636 2019-01-12 stsp free(commit_id);
535 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
536 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
537 c09a553d 2018-03-12 stsp if (error != NULL)
540 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
543 8069f636 2019-01-12 stsp free(commit_id_str);
544 76089277 2018-04-01 stsp free(repo_path);
545 507dc3bb 2018-12-29 stsp free(worktree_path);
546 507dc3bb 2018-12-29 stsp return error;
549 507dc3bb 2018-12-29 stsp __dead static void
550 507dc3bb 2018-12-29 stsp usage_update(void)
552 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
553 507dc3bb 2018-12-29 stsp getprogname());
557 507dc3bb 2018-12-29 stsp static void
558 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
560 784955db 2019-01-12 stsp int *did_something = arg;
562 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
565 1545c615 2019-02-10 stsp *did_something = 1;
566 507dc3bb 2018-12-29 stsp while (path[0] == '/')
568 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
571 be7061eb 2018-12-30 stsp static const struct got_error *
572 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
574 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
575 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
576 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
577 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
578 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
579 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
580 024e9686 2019-05-14 stsp const char *branch_name = NULL;
581 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
582 784955db 2019-01-12 stsp int ch, did_something = 0;
584 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
585 507dc3bb 2018-12-29 stsp switch (ch) {
587 024e9686 2019-05-14 stsp branch_name = optarg;
590 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
591 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
592 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
595 2deda0b9 2019-03-07 stsp usage_update();
596 507dc3bb 2018-12-29 stsp /* NOTREACHED */
600 507dc3bb 2018-12-29 stsp argc -= optind;
601 507dc3bb 2018-12-29 stsp argv += optind;
603 507dc3bb 2018-12-29 stsp #ifndef PROFILE
604 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
605 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
606 507dc3bb 2018-12-29 stsp err(1, "pledge");
608 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
609 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
613 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
617 507dc3bb 2018-12-29 stsp if (argc == 0) {
618 c4cdcb68 2019-04-03 stsp path = strdup("");
619 c4cdcb68 2019-04-03 stsp if (path == NULL) {
620 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
623 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
624 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
628 507dc3bb 2018-12-29 stsp usage_update();
630 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
631 507dc3bb 2018-12-29 stsp if (error != NULL)
634 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
635 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
639 024e9686 2019-05-14 stsp if (branch_name == NULL)
640 024e9686 2019-05-14 stsp branch_name = got_worktree_get_head_ref_name(worktree);
641 024e9686 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
642 024e9686 2019-05-14 stsp if (error != NULL)
644 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
645 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
646 9c4b8182 2019-01-02 stsp if (error != NULL)
648 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
649 507dc3bb 2018-12-29 stsp if (error != NULL)
652 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
653 507dc3bb 2018-12-29 stsp commit_id_str);
654 507dc3bb 2018-12-29 stsp if (error != NULL)
658 024e9686 2019-05-14 stsp if (strcmp(got_ref_get_name(head_ref),
659 024e9686 2019-05-14 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
660 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
661 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
662 024e9686 2019-05-14 stsp fprintf(stderr, "%s: switching to a different "
663 024e9686 2019-05-14 stsp "branch requires that the entire work tree "
664 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
665 024e9686 2019-05-14 stsp getprogname(), path);
666 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
669 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
672 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
673 a367ff0f 2019-05-14 stsp free(head_commit_id);
674 024e9686 2019-05-14 stsp if (error != NULL)
676 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
679 024e9686 2019-05-14 stsp error = got_worktree_set_head_ref(worktree, head_ref);
683 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
684 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
685 a367ff0f 2019-05-14 stsp if (error != NULL) {
686 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
687 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
690 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
695 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
696 507dc3bb 2018-12-29 stsp commit_id) != 0) {
697 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
698 507dc3bb 2018-12-29 stsp commit_id);
703 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
704 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
705 507dc3bb 2018-12-29 stsp if (error != NULL)
708 784955db 2019-01-12 stsp if (did_something)
709 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
711 784955db 2019-01-12 stsp printf("Already up-to-date\n");
713 c09a553d 2018-03-12 stsp free(worktree_path);
714 c4cdcb68 2019-04-03 stsp free(path);
715 507dc3bb 2018-12-29 stsp free(commit_id);
716 9c4b8182 2019-01-02 stsp free(commit_id_str);
717 c09a553d 2018-03-12 stsp return error;
720 f42b1b34 2018-03-12 stsp static const struct got_error *
721 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
722 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
724 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
725 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
726 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
727 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
729 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
730 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
732 79109fed 2018-03-27 stsp return err;
734 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
735 79f35eb3 2018-06-11 stsp if (qid != NULL) {
736 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
738 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
740 79109fed 2018-03-27 stsp return err;
742 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
743 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
744 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
746 0f2b3dca 2018-12-22 stsp return err;
748 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
750 79109fed 2018-03-27 stsp return err;
753 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
757 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
758 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
761 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
762 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
763 0f2b3dca 2018-12-22 stsp free(id_str1);
764 0f2b3dca 2018-12-22 stsp free(id_str2);
765 79109fed 2018-03-27 stsp return err;
768 4bb494d5 2018-06-16 stsp static char *
769 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
771 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
772 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
778 79109fed 2018-03-27 stsp static const struct got_error *
779 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
780 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
781 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
783 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
784 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
785 6c281f94 2018-06-11 stsp char datebuf[26];
786 45d799e2 2018-12-23 stsp time_t committer_time;
787 45d799e2 2018-12-23 stsp const char *author, *committer;
788 199a4027 2019-02-02 stsp char *refs_str = NULL;
789 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
791 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
793 199a4027 2019-02-02 stsp const char *name;
794 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
796 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
797 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
799 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
801 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
803 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
805 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
807 199a4027 2019-02-02 stsp s = refs_str;
808 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
809 199a4027 2019-02-02 stsp name) == -1) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
816 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
818 8bf5b3c9 2018-03-17 stsp return err;
820 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
821 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
822 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
823 832c249c 2018-06-10 stsp free(id_str);
824 d3d493d7 2019-02-21 stsp id_str = NULL;
825 d3d493d7 2019-02-21 stsp free(refs_str);
826 d3d493d7 2019-02-21 stsp refs_str = NULL;
827 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
828 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
829 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
830 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
831 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
832 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
833 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
834 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
835 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
836 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
837 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
839 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
840 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
841 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
843 a0603db2 2018-06-10 stsp return err;
844 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
845 a0603db2 2018-06-10 stsp free(id_str);
849 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
850 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
851 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
853 621015ac 2018-07-23 stsp logmsg = logmsg0;
855 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
857 832c249c 2018-06-10 stsp printf(" %s\n", line);
858 832c249c 2018-06-10 stsp } while (line);
859 621015ac 2018-07-23 stsp free(logmsg0);
861 971751ac 2018-03-27 stsp if (show_patch) {
862 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
863 971751ac 2018-03-27 stsp if (err == 0)
864 971751ac 2018-03-27 stsp printf("\n");
867 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
868 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
869 07862c20 2018-09-15 stsp return err;
872 f42b1b34 2018-03-12 stsp static const struct got_error *
873 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
874 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
875 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
877 f42b1b34 2018-03-12 stsp const struct got_error *err;
878 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
880 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
881 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
883 f42b1b34 2018-03-12 stsp return err;
884 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
888 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
889 372ccdbb 2018-06-10 stsp struct got_object_id *id;
891 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
894 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
896 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
897 9ba79e04 2018-06-11 stsp err = NULL;
900 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
902 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
908 b43fbaa0 2018-06-11 stsp if (id == NULL)
911 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
914 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
916 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
917 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
921 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
922 f42b1b34 2018-03-12 stsp return err;
925 4ed7e80c 2018-05-20 stsp __dead static void
926 6f3d1eb0 2018-03-12 stsp usage_log(void)
928 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
929 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
933 4ed7e80c 2018-05-20 stsp static const struct got_error *
934 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
936 f42b1b34 2018-03-12 stsp const struct got_error *error;
937 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
938 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
939 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
940 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
941 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
942 d142fc45 2018-04-01 stsp char *start_commit = NULL;
943 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
944 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
945 64a96a6d 2018-04-01 stsp const char *errstr;
946 199a4027 2019-02-02 stsp struct got_reflist_head refs;
948 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
950 6715a751 2018-03-16 stsp #ifndef PROFILE
951 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
954 f42b1b34 2018-03-12 stsp err(1, "pledge");
957 1cc14b9f 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
958 79109fed 2018-03-27 stsp switch (ch) {
960 79109fed 2018-03-27 stsp show_patch = 1;
963 d142fc45 2018-04-01 stsp start_commit = optarg;
966 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
968 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
969 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
972 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
973 64a96a6d 2018-04-01 stsp if (errstr != NULL)
974 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
977 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
980 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
981 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
982 04ca23f4 2018-07-16 stsp err(1, "-r option");
983 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
986 2deda0b9 2019-03-07 stsp usage_log();
987 79109fed 2018-03-27 stsp /* NOTREACHED */
991 79109fed 2018-03-27 stsp argc -= optind;
992 79109fed 2018-03-27 stsp argv += optind;
994 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
995 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
996 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1000 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1001 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1002 cffc0aa4 2019-02-05 stsp goto done;
1003 cffc0aa4 2019-02-05 stsp error = NULL;
1005 e7301579 2019-03-18 stsp if (argc == 0) {
1006 cbd1af7a 2019-03-18 stsp path = strdup("");
1007 e7301579 2019-03-18 stsp if (path == NULL) {
1008 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1009 cbd1af7a 2019-03-18 stsp goto done;
1011 e7301579 2019-03-18 stsp } else if (argc == 1) {
1012 e7301579 2019-03-18 stsp if (worktree) {
1013 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1015 e7301579 2019-03-18 stsp if (error)
1016 e7301579 2019-03-18 stsp goto done;
1018 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1019 e7301579 2019-03-18 stsp if (path == NULL) {
1020 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1021 e7301579 2019-03-18 stsp goto done;
1025 cbd1af7a 2019-03-18 stsp usage_log();
1027 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1028 5486daa2 2019-05-11 stsp repo_path = worktree ?
1029 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1031 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1032 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1033 cffc0aa4 2019-02-05 stsp goto done;
1036 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1037 d7d4f210 2018-03-12 stsp if (error != NULL)
1038 04ca23f4 2018-07-16 stsp goto done;
1040 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1041 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1042 c02c541e 2019-03-29 stsp if (error)
1043 c02c541e 2019-03-29 stsp goto done;
1045 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1046 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1047 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1048 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1049 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1050 3235492e 2018-04-01 stsp if (error != NULL)
1051 3235492e 2018-04-01 stsp return error;
1052 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1053 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1054 3235492e 2018-04-01 stsp if (error != NULL)
1055 3235492e 2018-04-01 stsp return error;
1056 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1058 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1059 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1060 3235492e 2018-04-01 stsp if (error == NULL) {
1061 1caad61b 2019-02-01 stsp int obj_type;
1062 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1063 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1064 d1f2edc9 2018-06-13 stsp if (error != NULL)
1065 1caad61b 2019-02-01 stsp goto done;
1066 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1067 1caad61b 2019-02-01 stsp if (error != NULL)
1068 1caad61b 2019-02-01 stsp goto done;
1069 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1070 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1071 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1072 1caad61b 2019-02-01 stsp if (error != NULL)
1073 1caad61b 2019-02-01 stsp goto done;
1074 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1075 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1076 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1077 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1078 1caad61b 2019-02-01 stsp goto done;
1081 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1082 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1083 1caad61b 2019-02-01 stsp if (id == NULL)
1084 638f9024 2019-05-13 stsp error = got_error_from_errno(
1085 230a42bd 2019-05-11 jcs "got_object_id_dup");
1086 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1087 1caad61b 2019-02-01 stsp if (error)
1088 1caad61b 2019-02-01 stsp goto done;
1089 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1090 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1091 1caad61b 2019-02-01 stsp goto done;
1093 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1094 d1f2edc9 2018-06-13 stsp if (error != NULL)
1095 1caad61b 2019-02-01 stsp goto done;
1097 15a94983 2018-12-23 stsp if (commit == NULL) {
1098 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
1099 d1f2edc9 2018-06-13 stsp start_commit);
1100 d1f2edc9 2018-06-13 stsp if (error != NULL)
1101 d1f2edc9 2018-06-13 stsp return error;
1104 d7d4f210 2018-03-12 stsp if (error != NULL)
1105 04ca23f4 2018-07-16 stsp goto done;
1107 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1108 04ca23f4 2018-07-16 stsp if (error != NULL)
1109 04ca23f4 2018-07-16 stsp goto done;
1110 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1111 04ca23f4 2018-07-16 stsp free(path);
1112 04ca23f4 2018-07-16 stsp path = in_repo_path;
1115 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1116 199a4027 2019-02-02 stsp if (error)
1117 199a4027 2019-02-02 stsp goto done;
1119 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1120 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1122 04ca23f4 2018-07-16 stsp free(path);
1123 04ca23f4 2018-07-16 stsp free(repo_path);
1124 04ca23f4 2018-07-16 stsp free(cwd);
1126 cffc0aa4 2019-02-05 stsp if (worktree)
1127 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1128 ad242220 2018-09-08 stsp if (repo) {
1129 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1130 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1131 ad242220 2018-09-08 stsp if (error == NULL)
1132 ad242220 2018-09-08 stsp error = repo_error;
1134 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1135 8bf5b3c9 2018-03-17 stsp return error;
1138 4ed7e80c 2018-05-20 stsp __dead static void
1139 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1141 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1142 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1146 b72f483a 2019-02-05 stsp struct print_diff_arg {
1147 b72f483a 2019-02-05 stsp struct got_repository *repo;
1148 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1149 b72f483a 2019-02-05 stsp int diff_context;
1150 3fc0c068 2019-02-10 stsp const char *id_str;
1151 3fc0c068 2019-02-10 stsp int header_shown;
1154 4ed7e80c 2018-05-20 stsp static const struct got_error *
1155 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1156 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1158 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1159 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1160 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1161 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1162 b72f483a 2019-02-05 stsp char *abspath = NULL;
1163 b72f483a 2019-02-05 stsp struct stat sb;
1165 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1166 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1167 b72f483a 2019-02-05 stsp return NULL;
1169 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1170 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1171 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1172 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1175 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1176 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1178 d00136be 2019-03-26 stsp goto done;
1182 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1183 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1184 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1186 2ec1f75b 2019-03-26 stsp goto done;
1189 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1190 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1191 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1192 2ec1f75b 2019-03-26 stsp goto done;
1194 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1195 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1196 2ec1f75b 2019-03-26 stsp goto done;
1199 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1201 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1204 b72f483a 2019-02-05 stsp if (blob1)
1205 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1206 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1207 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1208 b72f483a 2019-02-05 stsp free(abspath);
1209 927df6b7 2019-02-10 stsp return err;
1212 927df6b7 2019-02-10 stsp static const struct got_error *
1213 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1215 b00d56cd 2018-04-01 stsp const struct got_error *error;
1216 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1217 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1218 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1219 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1220 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1221 15a94983 2018-12-23 stsp int type1, type2;
1222 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1223 c0cc5c62 2018-10-18 stsp const char *errstr;
1224 927df6b7 2019-02-10 stsp char *path = NULL;
1226 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1227 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1228 25eccc22 2019-01-04 stsp NULL) == -1)
1229 b00d56cd 2018-04-01 stsp err(1, "pledge");
1232 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1233 b00d56cd 2018-04-01 stsp switch (ch) {
1235 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1236 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1237 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1240 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1241 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1242 b72f483a 2019-02-05 stsp err(1, "-r option");
1243 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1246 2deda0b9 2019-03-07 stsp usage_diff();
1247 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1251 b00d56cd 2018-04-01 stsp argc -= optind;
1252 b00d56cd 2018-04-01 stsp argv += optind;
1254 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1255 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1256 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1257 b72f483a 2019-02-05 stsp goto done;
1259 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1260 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1261 927df6b7 2019-02-10 stsp goto done;
1262 927df6b7 2019-02-10 stsp if (argc <= 1) {
1263 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1264 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1265 927df6b7 2019-02-10 stsp goto done;
1267 b72f483a 2019-02-05 stsp if (repo_path)
1269 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1270 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1271 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1272 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1273 927df6b7 2019-02-10 stsp goto done;
1275 927df6b7 2019-02-10 stsp if (argc == 1) {
1276 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1278 927df6b7 2019-02-10 stsp if (error)
1279 927df6b7 2019-02-10 stsp goto done;
1281 927df6b7 2019-02-10 stsp path = strdup("");
1282 927df6b7 2019-02-10 stsp if (path == NULL) {
1283 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1284 927df6b7 2019-02-10 stsp goto done;
1287 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1288 15a94983 2018-12-23 stsp id_str1 = argv[0];
1289 15a94983 2018-12-23 stsp id_str2 = argv[1];
1291 b00d56cd 2018-04-01 stsp usage_diff();
1293 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1294 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1295 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1296 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1299 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1300 76089277 2018-04-01 stsp free(repo_path);
1301 b00d56cd 2018-04-01 stsp if (error != NULL)
1302 b00d56cd 2018-04-01 stsp goto done;
1304 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1305 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1306 c02c541e 2019-03-29 stsp if (error)
1307 c02c541e 2019-03-29 stsp goto done;
1309 b72f483a 2019-02-05 stsp if (worktree) {
1310 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1311 b72f483a 2019-02-05 stsp char *id_str;
1312 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1313 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1314 b72f483a 2019-02-05 stsp if (error)
1315 b72f483a 2019-02-05 stsp goto done;
1316 b72f483a 2019-02-05 stsp arg.repo = repo;
1317 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1318 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1319 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1320 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1322 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1323 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1324 b72f483a 2019-02-05 stsp free(id_str);
1325 b72f483a 2019-02-05 stsp goto done;
1328 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1329 6402fb3c 2018-09-15 stsp if (error)
1330 b00d56cd 2018-04-01 stsp goto done;
1332 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1333 6402fb3c 2018-09-15 stsp if (error)
1334 b00d56cd 2018-04-01 stsp goto done;
1336 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1337 15a94983 2018-12-23 stsp if (error)
1338 15a94983 2018-12-23 stsp goto done;
1340 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1341 15a94983 2018-12-23 stsp if (error)
1342 15a94983 2018-12-23 stsp goto done;
1344 15a94983 2018-12-23 stsp if (type1 != type2) {
1345 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1346 b00d56cd 2018-04-01 stsp goto done;
1349 15a94983 2018-12-23 stsp switch (type1) {
1350 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1351 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1352 54156555 2018-12-24 stsp diff_context, repo, stdout);
1354 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1355 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1356 54156555 2018-12-24 stsp diff_context, repo, stdout);
1358 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1359 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1361 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1362 c0cc5c62 2018-10-18 stsp repo, stdout);
1365 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1369 15a94983 2018-12-23 stsp free(id1);
1370 15a94983 2018-12-23 stsp free(id2);
1371 927df6b7 2019-02-10 stsp free(path);
1372 b72f483a 2019-02-05 stsp if (worktree)
1373 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1374 ad242220 2018-09-08 stsp if (repo) {
1375 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1376 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1377 ad242220 2018-09-08 stsp if (error == NULL)
1378 ad242220 2018-09-08 stsp error = repo_error;
1380 b00d56cd 2018-04-01 stsp return error;
1383 404c43c4 2018-06-21 stsp __dead static void
1384 404c43c4 2018-06-21 stsp usage_blame(void)
1386 9270e621 2019-02-05 stsp fprintf(stderr,
1387 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1388 404c43c4 2018-06-21 stsp getprogname());
1392 404c43c4 2018-06-21 stsp static const struct got_error *
1393 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1395 404c43c4 2018-06-21 stsp const struct got_error *error;
1396 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1397 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1398 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1399 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1400 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1403 404c43c4 2018-06-21 stsp #ifndef PROFILE
1404 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1405 36e2fb66 2019-01-04 stsp NULL) == -1)
1406 404c43c4 2018-06-21 stsp err(1, "pledge");
1409 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1410 404c43c4 2018-06-21 stsp switch (ch) {
1412 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1415 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1416 66bea077 2018-08-02 stsp if (repo_path == NULL)
1417 66bea077 2018-08-02 stsp err(1, "-r option");
1418 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1421 2deda0b9 2019-03-07 stsp usage_blame();
1422 404c43c4 2018-06-21 stsp /* NOTREACHED */
1426 404c43c4 2018-06-21 stsp argc -= optind;
1427 404c43c4 2018-06-21 stsp argv += optind;
1429 a39318fd 2018-08-02 stsp if (argc == 1)
1430 404c43c4 2018-06-21 stsp path = argv[0];
1432 404c43c4 2018-06-21 stsp usage_blame();
1434 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1435 66bea077 2018-08-02 stsp if (cwd == NULL) {
1436 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1437 66bea077 2018-08-02 stsp goto done;
1439 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1440 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1441 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1442 66bea077 2018-08-02 stsp goto done;
1444 0c06baac 2019-02-05 stsp error = NULL;
1445 0c06baac 2019-02-05 stsp if (worktree) {
1446 0c06baac 2019-02-05 stsp repo_path =
1447 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1448 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1449 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1450 0c06baac 2019-02-05 stsp if (error)
1451 0c06baac 2019-02-05 stsp goto done;
1453 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1454 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1455 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1456 0c06baac 2019-02-05 stsp goto done;
1461 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1462 c02c541e 2019-03-29 stsp if (error != NULL)
1463 36e2fb66 2019-01-04 stsp goto done;
1465 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1466 c02c541e 2019-03-29 stsp if (error)
1467 404c43c4 2018-06-21 stsp goto done;
1469 0c06baac 2019-02-05 stsp if (worktree) {
1470 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1471 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1472 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1473 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1474 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1475 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1476 6efaaa2d 2019-02-05 stsp path) == -1) {
1477 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1478 0c06baac 2019-02-05 stsp goto done;
1480 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1483 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1485 0c06baac 2019-02-05 stsp if (error)
1486 66bea077 2018-08-02 stsp goto done;
1488 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1489 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1490 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1491 404c43c4 2018-06-21 stsp if (error != NULL)
1492 66bea077 2018-08-02 stsp goto done;
1493 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1494 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1495 404c43c4 2018-06-21 stsp if (error != NULL)
1496 66bea077 2018-08-02 stsp goto done;
1498 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1499 15a94983 2018-12-23 stsp commit_id_str);
1500 404c43c4 2018-06-21 stsp if (error != NULL)
1501 66bea077 2018-08-02 stsp goto done;
1504 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1506 66bea077 2018-08-02 stsp free(in_repo_path);
1507 66bea077 2018-08-02 stsp free(repo_path);
1508 66bea077 2018-08-02 stsp free(cwd);
1509 404c43c4 2018-06-21 stsp free(commit_id);
1510 0c06baac 2019-02-05 stsp if (worktree)
1511 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1512 ad242220 2018-09-08 stsp if (repo) {
1513 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1514 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1515 ad242220 2018-09-08 stsp if (error == NULL)
1516 ad242220 2018-09-08 stsp error = repo_error;
1518 404c43c4 2018-06-21 stsp return error;
1521 5de5890b 2018-10-18 stsp __dead static void
1522 5de5890b 2018-10-18 stsp usage_tree(void)
1524 c1669e2e 2019-01-09 stsp fprintf(stderr,
1525 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1526 5de5890b 2018-10-18 stsp getprogname());
1530 c1669e2e 2019-01-09 stsp static void
1531 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1532 c1669e2e 2019-01-09 stsp const char *root_path)
1534 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1536 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1537 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1540 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1541 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1542 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1545 5de5890b 2018-10-18 stsp static const struct got_error *
1546 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1547 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1548 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1550 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1551 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1552 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1553 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1554 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1556 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1558 5de5890b 2018-10-18 stsp goto done;
1560 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1562 5de5890b 2018-10-18 stsp goto done;
1563 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1564 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1565 5de5890b 2018-10-18 stsp while (te) {
1566 5de5890b 2018-10-18 stsp char *id = NULL;
1568 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1571 5de5890b 2018-10-18 stsp if (show_ids) {
1572 5de5890b 2018-10-18 stsp char *id_str;
1573 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1575 5de5890b 2018-10-18 stsp goto done;
1576 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1577 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1578 5de5890b 2018-10-18 stsp free(id_str);
1579 5de5890b 2018-10-18 stsp goto done;
1581 5de5890b 2018-10-18 stsp free(id_str);
1583 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1586 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1587 c1669e2e 2019-01-09 stsp char *child_path;
1588 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1589 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1590 c1669e2e 2019-01-09 stsp te->name) == -1) {
1591 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1592 c1669e2e 2019-01-09 stsp goto done;
1594 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1595 c1669e2e 2019-01-09 stsp root_path, repo);
1596 c1669e2e 2019-01-09 stsp free(child_path);
1598 c1669e2e 2019-01-09 stsp goto done;
1601 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1605 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1606 5de5890b 2018-10-18 stsp free(tree_id);
1607 5de5890b 2018-10-18 stsp return err;
1610 5de5890b 2018-10-18 stsp static const struct got_error *
1611 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1613 5de5890b 2018-10-18 stsp const struct got_error *error;
1614 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1615 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1616 7a2c19d6 2019-02-05 stsp const char *path;
1617 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1618 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1619 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1620 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1623 5de5890b 2018-10-18 stsp #ifndef PROFILE
1624 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1625 0f8d269b 2019-01-04 stsp NULL) == -1)
1626 5de5890b 2018-10-18 stsp err(1, "pledge");
1629 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1630 5de5890b 2018-10-18 stsp switch (ch) {
1632 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1635 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1636 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1637 5de5890b 2018-10-18 stsp err(1, "-r option");
1638 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1641 5de5890b 2018-10-18 stsp show_ids = 1;
1644 c1669e2e 2019-01-09 stsp recurse = 1;
1647 2deda0b9 2019-03-07 stsp usage_tree();
1648 5de5890b 2018-10-18 stsp /* NOTREACHED */
1652 5de5890b 2018-10-18 stsp argc -= optind;
1653 5de5890b 2018-10-18 stsp argv += optind;
1655 5de5890b 2018-10-18 stsp if (argc == 1)
1656 5de5890b 2018-10-18 stsp path = argv[0];
1657 5de5890b 2018-10-18 stsp else if (argc > 1)
1658 5de5890b 2018-10-18 stsp usage_tree();
1660 7a2c19d6 2019-02-05 stsp path = NULL;
1662 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1663 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1664 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1665 9bf7a39b 2019-02-05 stsp goto done;
1667 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1668 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1669 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1670 7a2c19d6 2019-02-05 stsp goto done;
1672 7a2c19d6 2019-02-05 stsp error = NULL;
1673 7a2c19d6 2019-02-05 stsp if (worktree) {
1674 7a2c19d6 2019-02-05 stsp repo_path =
1675 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1676 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1677 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1678 7a2c19d6 2019-02-05 stsp if (error)
1679 7a2c19d6 2019-02-05 stsp goto done;
1681 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1682 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1683 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1684 7a2c19d6 2019-02-05 stsp goto done;
1689 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1690 5de5890b 2018-10-18 stsp if (error != NULL)
1691 c02c541e 2019-03-29 stsp goto done;
1693 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1694 c02c541e 2019-03-29 stsp if (error)
1695 5de5890b 2018-10-18 stsp goto done;
1697 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1698 9bf7a39b 2019-02-05 stsp if (worktree) {
1699 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1700 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1701 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1702 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1703 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1704 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1705 9bf7a39b 2019-02-05 stsp goto done;
1707 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1709 9bf7a39b 2019-02-05 stsp if (error)
1710 9bf7a39b 2019-02-05 stsp goto done;
1712 9bf7a39b 2019-02-05 stsp path = "/";
1714 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1715 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1716 9bf7a39b 2019-02-05 stsp if (error != NULL)
1717 9bf7a39b 2019-02-05 stsp goto done;
1720 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1721 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1722 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1723 5de5890b 2018-10-18 stsp if (error != NULL)
1724 5de5890b 2018-10-18 stsp goto done;
1725 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1726 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1727 5de5890b 2018-10-18 stsp if (error != NULL)
1728 5de5890b 2018-10-18 stsp goto done;
1730 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1731 15a94983 2018-12-23 stsp commit_id_str);
1732 5de5890b 2018-10-18 stsp if (error != NULL)
1733 5de5890b 2018-10-18 stsp goto done;
1736 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1737 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1739 5de5890b 2018-10-18 stsp free(in_repo_path);
1740 5de5890b 2018-10-18 stsp free(repo_path);
1741 5de5890b 2018-10-18 stsp free(cwd);
1742 5de5890b 2018-10-18 stsp free(commit_id);
1743 7a2c19d6 2019-02-05 stsp if (worktree)
1744 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1745 5de5890b 2018-10-18 stsp if (repo) {
1746 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1747 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1748 5de5890b 2018-10-18 stsp if (error == NULL)
1749 5de5890b 2018-10-18 stsp error = repo_error;
1751 5de5890b 2018-10-18 stsp return error;
1754 6bad629b 2019-02-04 stsp __dead static void
1755 6bad629b 2019-02-04 stsp usage_status(void)
1757 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1761 b72f483a 2019-02-05 stsp static const struct got_error *
1762 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1763 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1765 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1766 b72f483a 2019-02-05 stsp return NULL;
1769 6bad629b 2019-02-04 stsp static const struct got_error *
1770 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1772 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1773 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1774 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1775 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1778 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1779 6bad629b 2019-02-04 stsp switch (ch) {
1781 2deda0b9 2019-03-07 stsp usage_status();
1782 6bad629b 2019-02-04 stsp /* NOTREACHED */
1786 6bad629b 2019-02-04 stsp argc -= optind;
1787 6bad629b 2019-02-04 stsp argv += optind;
1789 6bad629b 2019-02-04 stsp #ifndef PROFILE
1790 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1791 6bad629b 2019-02-04 stsp NULL) == -1)
1792 6bad629b 2019-02-04 stsp err(1, "pledge");
1794 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1795 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1796 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1797 927df6b7 2019-02-10 stsp goto done;
1800 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1801 927df6b7 2019-02-10 stsp if (error != NULL)
1802 927df6b7 2019-02-10 stsp goto done;
1804 6bad629b 2019-02-04 stsp if (argc == 0) {
1805 927df6b7 2019-02-10 stsp path = strdup("");
1806 927df6b7 2019-02-10 stsp if (path == NULL) {
1807 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1808 6bad629b 2019-02-04 stsp goto done;
1810 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1811 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1812 927df6b7 2019-02-10 stsp if (error)
1813 6bad629b 2019-02-04 stsp goto done;
1815 6bad629b 2019-02-04 stsp usage_status();
1817 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1818 6bad629b 2019-02-04 stsp if (error != NULL)
1819 6bad629b 2019-02-04 stsp goto done;
1821 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1822 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1823 6bad629b 2019-02-04 stsp if (error)
1824 6bad629b 2019-02-04 stsp goto done;
1826 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1827 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1829 927df6b7 2019-02-10 stsp free(cwd);
1830 927df6b7 2019-02-10 stsp free(path);
1831 d0eebce4 2019-03-11 stsp return error;
1834 d0eebce4 2019-03-11 stsp __dead static void
1835 d0eebce4 2019-03-11 stsp usage_ref(void)
1837 d0eebce4 2019-03-11 stsp fprintf(stderr,
1838 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
1839 d0eebce4 2019-03-11 stsp getprogname());
1843 d0eebce4 2019-03-11 stsp static const struct got_error *
1844 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1846 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1847 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1848 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1850 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1851 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1853 d0eebce4 2019-03-11 stsp return err;
1855 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1856 d0eebce4 2019-03-11 stsp char *refstr;
1857 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1858 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1859 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
1860 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1861 d0eebce4 2019-03-11 stsp free(refstr);
1864 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1865 d0eebce4 2019-03-11 stsp return NULL;
1868 d0eebce4 2019-03-11 stsp static const struct got_error *
1869 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1871 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1872 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1874 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
1876 d0eebce4 2019-03-11 stsp return err;
1878 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1879 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1880 d0eebce4 2019-03-11 stsp return err;
1883 d0eebce4 2019-03-11 stsp static const struct got_error *
1884 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
1886 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1887 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1888 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1890 d83d9d5c 2019-05-13 stsp err = got_object_resolve_id_str(&id, repo, target);
1891 d83d9d5c 2019-05-13 stsp if (err) {
1892 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
1894 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 d83d9d5c 2019-05-13 stsp return err;
1896 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
1898 d83d9d5c 2019-05-13 stsp return err;
1899 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
1900 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
1902 d83d9d5c 2019-05-13 stsp return err;
1905 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1907 d0eebce4 2019-03-11 stsp goto done;
1909 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1912 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1914 d0eebce4 2019-03-11 stsp return err;
1917 d0eebce4 2019-03-11 stsp static const struct got_error *
1918 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1920 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1921 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1922 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1923 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1924 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1925 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1927 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1928 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1929 d0eebce4 2019-03-11 stsp switch (ch) {
1931 d0eebce4 2019-03-11 stsp delref = optarg;
1934 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1935 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1936 d0eebce4 2019-03-11 stsp err(1, "-r option");
1937 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1940 d0eebce4 2019-03-11 stsp do_list = 1;
1943 d0eebce4 2019-03-11 stsp usage_ref();
1944 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1948 d0eebce4 2019-03-11 stsp if (do_list && delref)
1949 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1951 d0eebce4 2019-03-11 stsp argc -= optind;
1952 d0eebce4 2019-03-11 stsp argv += optind;
1954 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1955 d0eebce4 2019-03-11 stsp if (argc > 0)
1956 d0eebce4 2019-03-11 stsp usage_ref();
1957 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1958 d0eebce4 2019-03-11 stsp usage_ref();
1960 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1961 e0b57350 2019-03-12 stsp if (do_list) {
1962 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1963 e0b57350 2019-03-12 stsp NULL) == -1)
1964 e0b57350 2019-03-12 stsp err(1, "pledge");
1966 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1967 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1968 e0b57350 2019-03-12 stsp err(1, "pledge");
1971 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1972 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1973 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1974 d0eebce4 2019-03-11 stsp goto done;
1977 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1978 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1979 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1980 d0eebce4 2019-03-11 stsp goto done;
1982 d0eebce4 2019-03-11 stsp error = NULL;
1983 d0eebce4 2019-03-11 stsp if (worktree) {
1984 d0eebce4 2019-03-11 stsp repo_path =
1985 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1986 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1987 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1988 d0eebce4 2019-03-11 stsp if (error)
1989 d0eebce4 2019-03-11 stsp goto done;
1991 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1992 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1993 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1994 d0eebce4 2019-03-11 stsp goto done;
1999 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2000 d0eebce4 2019-03-11 stsp if (error != NULL)
2001 d0eebce4 2019-03-11 stsp goto done;
2003 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2004 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2005 c02c541e 2019-03-29 stsp if (error)
2006 c02c541e 2019-03-29 stsp goto done;
2008 d0eebce4 2019-03-11 stsp if (do_list)
2009 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2010 d0eebce4 2019-03-11 stsp else if (delref)
2011 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2013 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2016 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2017 d0eebce4 2019-03-11 stsp if (worktree)
2018 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2019 d0eebce4 2019-03-11 stsp free(cwd);
2020 d0eebce4 2019-03-11 stsp free(repo_path);
2021 d00136be 2019-03-26 stsp return error;
2024 d00136be 2019-03-26 stsp __dead static void
2025 d00136be 2019-03-26 stsp usage_add(void)
2027 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2031 d00136be 2019-03-26 stsp static const struct got_error *
2032 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2034 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2035 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2036 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2037 1dd54920 2019-05-11 stsp char *cwd = NULL;
2038 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2039 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2042 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2044 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2045 d00136be 2019-03-26 stsp switch (ch) {
2047 d00136be 2019-03-26 stsp usage_add();
2048 d00136be 2019-03-26 stsp /* NOTREACHED */
2052 d00136be 2019-03-26 stsp argc -= optind;
2053 d00136be 2019-03-26 stsp argv += optind;
2055 723c305c 2019-05-11 jcs if (argc < 1)
2056 d00136be 2019-03-26 stsp usage_add();
2058 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2059 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2060 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2061 723c305c 2019-05-11 jcs if (path == NULL) {
2062 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2065 1dd54920 2019-05-11 stsp free(path);
2068 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2069 d00136be 2019-03-26 stsp if (cwd == NULL) {
2070 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2071 d00136be 2019-03-26 stsp goto done;
2074 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2075 d00136be 2019-03-26 stsp if (error)
2076 d00136be 2019-03-26 stsp goto done;
2078 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2079 031a5338 2019-03-26 stsp if (error != NULL)
2080 031a5338 2019-03-26 stsp goto done;
2082 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2083 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2084 d00136be 2019-03-26 stsp if (error)
2085 d00136be 2019-03-26 stsp goto done;
2087 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2088 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2089 723c305c 2019-05-11 jcs if (path == NULL) {
2090 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2094 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2095 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2096 1dd54920 2019-05-11 stsp if (error) {
2097 1dd54920 2019-05-11 stsp free(path);
2101 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2102 1dd54920 2019-05-11 stsp NULL, repo);
2105 031a5338 2019-03-26 stsp got_repo_close(repo);
2106 d00136be 2019-03-26 stsp if (worktree)
2107 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2108 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2109 1dd54920 2019-05-11 stsp free((char *)pe->path);
2110 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2111 2ec1f75b 2019-03-26 stsp free(cwd);
2112 2ec1f75b 2019-03-26 stsp return error;
2115 2ec1f75b 2019-03-26 stsp __dead static void
2116 2ec1f75b 2019-03-26 stsp usage_rm(void)
2118 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2122 2ec1f75b 2019-03-26 stsp static const struct got_error *
2123 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2125 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2126 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2127 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2128 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
2129 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
2131 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2132 2ec1f75b 2019-03-26 stsp switch (ch) {
2134 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2137 2ec1f75b 2019-03-26 stsp usage_add();
2138 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2142 2ec1f75b 2019-03-26 stsp argc -= optind;
2143 2ec1f75b 2019-03-26 stsp argv += optind;
2145 2ec1f75b 2019-03-26 stsp if (argc != 1)
2146 2ec1f75b 2019-03-26 stsp usage_rm();
2148 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
2149 2ec1f75b 2019-03-26 stsp if (path == NULL) {
2150 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2151 2ec1f75b 2019-03-26 stsp goto done;
2153 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2155 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2156 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2157 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2158 2ec1f75b 2019-03-26 stsp goto done;
2160 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2161 2ec1f75b 2019-03-26 stsp if (error)
2162 2ec1f75b 2019-03-26 stsp goto done;
2164 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2166 2ec1f75b 2019-03-26 stsp goto done;
2168 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2169 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2170 2ec1f75b 2019-03-26 stsp if (error)
2171 2ec1f75b 2019-03-26 stsp goto done;
2173 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2174 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
2175 a129376b 2019-03-28 stsp if (error)
2176 a129376b 2019-03-28 stsp goto done;
2179 a129376b 2019-03-28 stsp got_repo_close(repo);
2180 a129376b 2019-03-28 stsp if (worktree)
2181 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2182 a129376b 2019-03-28 stsp free(path);
2183 a129376b 2019-03-28 stsp free(cwd);
2184 a129376b 2019-03-28 stsp return error;
2187 a129376b 2019-03-28 stsp __dead static void
2188 a129376b 2019-03-28 stsp usage_revert(void)
2190 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2194 a129376b 2019-03-28 stsp static void
2195 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2197 a129376b 2019-03-28 stsp while (path[0] == '/')
2199 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2202 a129376b 2019-03-28 stsp static const struct got_error *
2203 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2205 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2206 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2207 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2208 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2211 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2212 a129376b 2019-03-28 stsp switch (ch) {
2214 a129376b 2019-03-28 stsp usage_revert();
2215 a129376b 2019-03-28 stsp /* NOTREACHED */
2219 a129376b 2019-03-28 stsp argc -= optind;
2220 a129376b 2019-03-28 stsp argv += optind;
2222 a129376b 2019-03-28 stsp if (argc != 1)
2223 a129376b 2019-03-28 stsp usage_revert();
2225 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2226 a129376b 2019-03-28 stsp if (path == NULL) {
2227 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2228 a129376b 2019-03-28 stsp goto done;
2230 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2232 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2233 a129376b 2019-03-28 stsp if (cwd == NULL) {
2234 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2235 a129376b 2019-03-28 stsp goto done;
2237 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2238 2ec1f75b 2019-03-26 stsp if (error)
2239 2ec1f75b 2019-03-26 stsp goto done;
2241 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2242 a129376b 2019-03-28 stsp if (error != NULL)
2243 a129376b 2019-03-28 stsp goto done;
2245 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2246 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2247 a129376b 2019-03-28 stsp if (error)
2248 a129376b 2019-03-28 stsp goto done;
2250 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2251 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2252 a129376b 2019-03-28 stsp if (error)
2253 a129376b 2019-03-28 stsp goto done;
2256 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2257 2ec1f75b 2019-03-26 stsp if (worktree)
2258 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2259 2ec1f75b 2019-03-26 stsp free(path);
2260 d00136be 2019-03-26 stsp free(cwd);
2261 6bad629b 2019-02-04 stsp return error;
2264 c4296144 2019-05-09 stsp __dead static void
2265 c4296144 2019-05-09 stsp usage_commit(void)
2267 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2272 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2275 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2276 33ad4cbe 2019-05-12 jcs int st = -1;
2278 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2279 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2280 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2282 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2284 33ad4cbe 2019-05-12 jcs goto doneediting;
2286 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2287 33ad4cbe 2019-05-12 jcs _exit(127);
2290 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2291 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2294 33ad4cbe 2019-05-12 jcs doneediting:
2295 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2296 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2297 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2299 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2300 33ad4cbe 2019-05-12 jcs errno = EINTR;
2304 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2307 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2308 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2309 e2ba3d07 2019-05-13 stsp const char *editor;
2310 e0870e44 2019-05-13 stsp const char *worktree_path;
2311 314a6357 2019-05-13 stsp const char *repo_path;
2312 e0870e44 2019-05-13 stsp char *logmsg_path;
2316 33ad4cbe 2019-05-12 jcs static const struct got_error *
2317 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2320 e0870e44 2019-05-13 stsp const char *initial_content = "\n# changes to be committed:\n";
2321 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2322 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2323 e0870e44 2019-05-13 stsp char *template = NULL;
2324 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2325 33ad4cbe 2019-05-12 jcs char buf[1024];
2326 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2328 33ad4cbe 2019-05-12 jcs size_t len;
2329 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2331 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2332 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2333 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2334 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2335 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2336 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2337 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2338 33ad4cbe 2019-05-12 jcs return NULL;
2341 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2342 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2344 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2346 e0870e44 2019-05-13 stsp goto done;
2348 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2350 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2351 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2352 33ad4cbe 2019-05-12 jcs dprintf(fd, "# %c %s\n", ct->status, pe->path);
2356 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2357 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2361 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2362 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2366 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2367 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2371 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2372 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2373 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2374 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2375 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2376 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2380 33ad4cbe 2019-05-12 jcs /* remove comments */
2381 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2382 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2383 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2384 fcde04d9 2019-05-13 stsp goto done;
2388 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2389 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2390 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2391 e0870e44 2019-05-13 stsp content_changed = 1;
2392 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2394 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2396 33ad4cbe 2019-05-12 jcs fclose(fp);
2398 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2399 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2403 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2404 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2405 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2406 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2407 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2408 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2412 e0870e44 2019-05-13 stsp free(template);
2414 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2415 314a6357 2019-05-13 stsp if (err == NULL)
2416 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2417 33ad4cbe 2019-05-12 jcs return err;
2420 c4296144 2019-05-09 stsp static const struct got_error *
2421 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2423 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2424 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2425 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2426 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2427 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2428 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2429 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2430 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2431 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2434 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2435 c4296144 2019-05-09 stsp switch (ch) {
2437 c4296144 2019-05-09 stsp logmsg = optarg;
2440 c4296144 2019-05-09 stsp usage_commit();
2441 c4296144 2019-05-09 stsp /* NOTREACHED */
2445 c4296144 2019-05-09 stsp argc -= optind;
2446 c4296144 2019-05-09 stsp argv += optind;
2448 c4296144 2019-05-09 stsp if (argc == 1) {
2449 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2450 c4296144 2019-05-09 stsp if (path == NULL) {
2451 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2452 c4296144 2019-05-09 stsp goto done;
2454 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2455 c4296144 2019-05-09 stsp } else if (argc != 0)
2456 c4296144 2019-05-09 stsp usage_commit();
2458 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2459 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2460 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2461 35bd8fed 2019-05-09 stsp goto done;
2464 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2465 c4296144 2019-05-09 stsp if (cwd == NULL) {
2466 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2467 c4296144 2019-05-09 stsp goto done;
2469 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2470 c4296144 2019-05-09 stsp if (error)
2471 c4296144 2019-05-09 stsp goto done;
2473 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2474 c4296144 2019-05-09 stsp if (error != NULL)
2475 c4296144 2019-05-09 stsp goto done;
2478 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2479 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2481 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2482 314a6357 2019-05-13 stsp error = get_editor(&editor);
2484 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2485 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2486 c4296144 2019-05-09 stsp if (error)
2487 c4296144 2019-05-09 stsp goto done;
2489 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2490 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2491 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2492 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2493 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2494 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2495 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2496 e0870e44 2019-05-13 stsp if (error) {
2497 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2498 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2499 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2500 c4296144 2019-05-09 stsp goto done;
2503 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2504 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2506 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2507 c4296144 2019-05-09 stsp if (error)
2508 c4296144 2019-05-09 stsp goto done;
2509 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2512 c4296144 2019-05-09 stsp got_repo_close(repo);
2513 c4296144 2019-05-09 stsp if (worktree)
2514 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2515 c4296144 2019-05-09 stsp free(path);
2516 c4296144 2019-05-09 stsp free(cwd);
2517 c4296144 2019-05-09 stsp free(id_str);
2518 e2ba3d07 2019-05-13 stsp free(editor);
2519 c4296144 2019-05-09 stsp return error;