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);
89 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
90 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
93 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
96 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
97 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
98 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
99 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
100 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
101 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
102 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
103 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
104 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
105 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
108 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
109 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
110 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
111 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
112 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
113 1b6b95a8 2018-03-12 stsp "show repository history" },
114 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
115 b00d56cd 2018-04-01 stsp "compare files and directories" },
116 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
117 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
118 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
119 a67e2392 2019-03-26 stsp "list files and directories in repository" },
120 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
121 1b6b95a8 2018-03-12 stsp "show modification status of files" },
122 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
123 d0eebce4 2019-03-11 stsp "manage references in repository" },
124 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
125 bd14628f 2019-05-12 stsp "add new files to version control" },
126 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
127 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
128 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
129 a129376b 2019-03-28 stsp "revert uncommitted changes" },
130 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
131 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
132 234035bc 2019-06-01 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick,
133 234035bc 2019-06-01 stsp "merge a single commit from another branch into a work tree" },
134 5ef14e63 2019-06-02 stsp { "backout", cmd_backout, usage_backout,
135 5ef14e63 2019-06-02 stsp "reverse-merge changes from a commit into a work tree" },
139 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
141 5c860e29 2018-03-12 stsp struct cmd *cmd;
142 5c860e29 2018-03-12 stsp unsigned int i;
144 1b6b95a8 2018-03-12 stsp int hflag = 0;
146 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
148 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
149 5c860e29 2018-03-12 stsp switch (ch) {
155 5c860e29 2018-03-12 stsp /* NOTREACHED */
159 5c860e29 2018-03-12 stsp argc -= optind;
160 5c860e29 2018-03-12 stsp argv += optind;
161 1e70621d 2018-03-27 stsp optind = 0;
163 5c860e29 2018-03-12 stsp if (argc <= 0)
166 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
167 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
169 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
170 d7d4f210 2018-03-12 stsp const struct got_error *error;
172 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
174 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
178 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
180 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
181 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
182 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
189 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
193 4ed7e80c 2018-05-20 stsp __dead static void
194 5c860e29 2018-03-12 stsp usage(void)
198 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
199 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
200 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
201 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
202 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
207 0266afb7 2019-01-04 stsp static const struct got_error *
208 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
210 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
211 e2ba3d07 2019-05-13 stsp const char *editor;
213 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
214 e2ba3d07 2019-05-13 stsp if (editor == NULL)
215 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
217 0ee7065d 2019-05-13 stsp if (editor) {
218 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
220 0ee7065d 2019-05-13 stsp return err;
223 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
224 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
225 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
226 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
229 e2ba3d07 2019-05-13 stsp return NULL;
232 e2ba3d07 2019-05-13 stsp static const struct got_error *
233 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
234 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
236 163ce85a 2019-05-13 stsp const struct got_error *err;
238 a0937847 2019-05-11 stsp if (create_worktree) {
239 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
240 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
242 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
243 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
245 163ce85a 2019-05-13 stsp err = NULL;
247 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
248 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
252 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
253 163ce85a 2019-05-13 stsp return err;
256 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
257 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
259 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
260 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
262 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
263 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
265 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
266 163ce85a 2019-05-13 stsp if (err != NULL)
267 163ce85a 2019-05-13 stsp return err;
269 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
270 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
272 0266afb7 2019-01-04 stsp return NULL;
275 4ed7e80c 2018-05-20 stsp __dead static void
276 c09a553d 2018-03-12 stsp usage_checkout(void)
278 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
279 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
283 92a684f4 2018-03-12 stsp static void
284 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
286 92a684f4 2018-03-12 stsp char *worktree_path = arg;
288 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
289 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
292 92a684f4 2018-03-12 stsp while (path[0] == '/')
295 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
298 99437157 2018-11-11 stsp static const struct got_error *
299 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
301 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
302 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
303 99437157 2018-11-11 stsp return NULL;
306 8069f636 2019-01-12 stsp static const struct got_error *
307 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
308 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
310 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
311 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
313 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
314 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
316 36a38700 2019-05-10 stsp return err;
318 d5bea539 2019-05-13 stsp if (yca_id == NULL)
319 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
322 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
323 d5bea539 2019-05-13 stsp * and the work tree's base commit.
325 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
327 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
335 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
336 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
337 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
339 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
340 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
341 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
343 d5bea539 2019-05-13 stsp free(yca_id);
344 d5bea539 2019-05-13 stsp return NULL;
347 a367ff0f 2019-05-14 stsp static const struct got_error *
348 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
349 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
351 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
352 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
353 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
354 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
356 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
360 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
364 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
369 a367ff0f 2019-05-14 stsp struct got_object_id *id;
370 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
372 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
373 a367ff0f 2019-05-14 stsp err = NULL;
376 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
378 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
385 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
386 a367ff0f 2019-05-14 stsp is_same_branch = 1;
393 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
394 a367ff0f 2019-05-14 stsp free(head_commit_id);
395 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
396 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
397 a367ff0f 2019-05-14 stsp return err;
400 4ed7e80c 2018-05-20 stsp static const struct got_error *
401 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
403 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
404 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
405 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
406 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
407 c09a553d 2018-03-12 stsp char *repo_path = NULL;
408 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
409 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
410 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
411 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
412 72151b04 2019-05-11 stsp int ch, same_path_prefix;
414 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
415 0bb8a95e 2018-03-12 stsp switch (ch) {
417 08573d5b 2019-05-14 stsp branch_name = optarg;
420 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
421 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
422 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
425 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
428 2deda0b9 2019-03-07 stsp usage_checkout();
429 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
433 0bb8a95e 2018-03-12 stsp argc -= optind;
434 0bb8a95e 2018-03-12 stsp argv += optind;
436 6715a751 2018-03-16 stsp #ifndef PROFILE
437 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
438 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
439 c09a553d 2018-03-12 stsp err(1, "pledge");
441 0bb8a95e 2018-03-12 stsp if (argc == 1) {
442 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
443 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
444 76089277 2018-04-01 stsp if (repo_path == NULL)
445 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
446 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
447 76089277 2018-04-01 stsp if (cwd == NULL) {
448 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
451 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
452 230a42bd 2019-05-11 jcs base = basename(path_prefix);
453 230a42bd 2019-05-11 jcs if (base == NULL) {
454 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
455 230a42bd 2019-05-11 jcs path_prefix);
459 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
460 230a42bd 2019-05-11 jcs if (base == NULL) {
461 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
466 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
467 c09a553d 2018-03-12 stsp if (dotgit)
468 c09a553d 2018-03-12 stsp *dotgit = '\0';
469 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
470 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
475 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
476 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
477 76089277 2018-04-01 stsp if (repo_path == NULL) {
478 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
481 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
482 76089277 2018-04-01 stsp if (worktree_path == NULL) {
483 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
487 c09a553d 2018-03-12 stsp usage_checkout();
489 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
490 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
492 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
493 c09a553d 2018-03-12 stsp if (error != NULL)
496 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
500 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
501 c09a553d 2018-03-12 stsp if (error != NULL)
504 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
505 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
508 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
509 c09a553d 2018-03-12 stsp if (error != NULL)
512 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
513 e5dc7198 2018-12-29 stsp path_prefix);
514 e5dc7198 2018-12-29 stsp if (error != NULL)
516 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
517 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
521 8069f636 2019-01-12 stsp if (commit_id_str) {
522 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
523 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
524 8069f636 2019-01-12 stsp commit_id_str);
525 8069f636 2019-01-12 stsp if (error != NULL)
527 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
528 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
529 8069f636 2019-01-12 stsp if (error != NULL) {
530 8069f636 2019-01-12 stsp free(commit_id);
533 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
536 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
537 8069f636 2019-01-12 stsp commit_id);
538 8069f636 2019-01-12 stsp free(commit_id);
543 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
544 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
545 c09a553d 2018-03-12 stsp if (error != NULL)
548 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
551 8069f636 2019-01-12 stsp free(commit_id_str);
552 76089277 2018-04-01 stsp free(repo_path);
553 507dc3bb 2018-12-29 stsp free(worktree_path);
554 507dc3bb 2018-12-29 stsp return error;
557 507dc3bb 2018-12-29 stsp __dead static void
558 507dc3bb 2018-12-29 stsp usage_update(void)
560 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
561 507dc3bb 2018-12-29 stsp getprogname());
565 507dc3bb 2018-12-29 stsp static void
566 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
568 784955db 2019-01-12 stsp int *did_something = arg;
570 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
573 1545c615 2019-02-10 stsp *did_something = 1;
575 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
576 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
579 507dc3bb 2018-12-29 stsp while (path[0] == '/')
581 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
584 be7061eb 2018-12-30 stsp static const struct got_error *
585 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
586 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
587 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
589 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
590 a1fb16d8 2019-05-24 stsp char *base_id_str;
591 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
593 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
594 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
595 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
596 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
597 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
598 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
599 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
602 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
603 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
605 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
606 a1fb16d8 2019-05-24 stsp return err;
607 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
609 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
610 a1fb16d8 2019-05-24 stsp return NULL;
612 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
613 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
614 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
616 a1fb16d8 2019-05-24 stsp return err;
617 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
618 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
619 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
620 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
621 a1fb16d8 2019-05-24 stsp return NULL;
624 a1fb16d8 2019-05-24 stsp static const struct got_error *
625 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
627 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
628 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
629 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
630 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
631 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
632 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
633 024e9686 2019-05-14 stsp const char *branch_name = NULL;
634 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
635 784955db 2019-01-12 stsp int ch, did_something = 0;
637 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
638 507dc3bb 2018-12-29 stsp switch (ch) {
640 024e9686 2019-05-14 stsp branch_name = optarg;
643 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
644 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
645 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
648 2deda0b9 2019-03-07 stsp usage_update();
649 507dc3bb 2018-12-29 stsp /* NOTREACHED */
653 507dc3bb 2018-12-29 stsp argc -= optind;
654 507dc3bb 2018-12-29 stsp argv += optind;
656 507dc3bb 2018-12-29 stsp #ifndef PROFILE
657 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
658 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
659 507dc3bb 2018-12-29 stsp err(1, "pledge");
661 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
662 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
663 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
666 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
670 507dc3bb 2018-12-29 stsp if (argc == 0) {
671 c4cdcb68 2019-04-03 stsp path = strdup("");
672 c4cdcb68 2019-04-03 stsp if (path == NULL) {
673 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
676 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
677 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
681 507dc3bb 2018-12-29 stsp usage_update();
683 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
684 507dc3bb 2018-12-29 stsp if (error != NULL)
687 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
688 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
692 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
693 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
694 024e9686 2019-05-14 stsp if (error != NULL)
696 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
697 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
698 9c4b8182 2019-01-02 stsp if (error != NULL)
700 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
701 507dc3bb 2018-12-29 stsp if (error != NULL)
704 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
705 507dc3bb 2018-12-29 stsp commit_id_str);
706 507dc3bb 2018-12-29 stsp if (error != NULL)
710 a1fb16d8 2019-05-24 stsp if (branch_name) {
711 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
712 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
713 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
714 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
715 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
716 024e9686 2019-05-14 stsp getprogname(), path);
717 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
720 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
723 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
724 a367ff0f 2019-05-14 stsp free(head_commit_id);
725 024e9686 2019-05-14 stsp if (error != NULL)
727 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
730 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
734 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
735 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
736 a367ff0f 2019-05-14 stsp if (error != NULL) {
737 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
738 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
741 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
746 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
747 507dc3bb 2018-12-29 stsp commit_id) != 0) {
748 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
749 507dc3bb 2018-12-29 stsp commit_id);
754 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
755 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
756 507dc3bb 2018-12-29 stsp if (error != NULL)
759 784955db 2019-01-12 stsp if (did_something)
760 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
762 784955db 2019-01-12 stsp printf("Already up-to-date\n");
764 c09a553d 2018-03-12 stsp free(worktree_path);
765 c4cdcb68 2019-04-03 stsp free(path);
766 507dc3bb 2018-12-29 stsp free(commit_id);
767 9c4b8182 2019-01-02 stsp free(commit_id_str);
768 c09a553d 2018-03-12 stsp return error;
771 f42b1b34 2018-03-12 stsp static const struct got_error *
772 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
773 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
775 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
776 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
777 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
778 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
779 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
781 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
782 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
784 79109fed 2018-03-27 stsp return err;
786 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
787 79f35eb3 2018-06-11 stsp if (qid != NULL) {
788 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
790 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
792 79109fed 2018-03-27 stsp return err;
794 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
795 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
796 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
798 0f2b3dca 2018-12-22 stsp return err;
800 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
802 79109fed 2018-03-27 stsp return err;
805 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
809 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
810 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
811 aaa13589 2019-06-01 stsp arg.outfile = stdout;
812 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
813 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
816 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
817 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
818 0f2b3dca 2018-12-22 stsp free(id_str1);
819 0f2b3dca 2018-12-22 stsp free(id_str2);
820 79109fed 2018-03-27 stsp return err;
823 4bb494d5 2018-06-16 stsp static char *
824 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
826 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
827 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
833 79109fed 2018-03-27 stsp static const struct got_error *
834 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
835 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
836 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
838 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
839 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
840 6c281f94 2018-06-11 stsp char datebuf[26];
841 45d799e2 2018-12-23 stsp time_t committer_time;
842 45d799e2 2018-12-23 stsp const char *author, *committer;
843 199a4027 2019-02-02 stsp char *refs_str = NULL;
844 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
846 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
848 199a4027 2019-02-02 stsp const char *name;
849 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
851 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
852 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
854 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
856 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
858 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
860 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
862 199a4027 2019-02-02 stsp s = refs_str;
863 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
864 199a4027 2019-02-02 stsp name) == -1) {
865 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
871 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
873 8bf5b3c9 2018-03-17 stsp return err;
875 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
876 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
877 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
878 832c249c 2018-06-10 stsp free(id_str);
879 d3d493d7 2019-02-21 stsp id_str = NULL;
880 d3d493d7 2019-02-21 stsp free(refs_str);
881 d3d493d7 2019-02-21 stsp refs_str = NULL;
882 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
883 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
884 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
885 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
886 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
887 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
888 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
889 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
890 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
891 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
892 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
894 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
895 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
896 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
898 a0603db2 2018-06-10 stsp return err;
899 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
900 a0603db2 2018-06-10 stsp free(id_str);
904 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
905 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
906 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
908 621015ac 2018-07-23 stsp logmsg = logmsg0;
910 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
912 832c249c 2018-06-10 stsp printf(" %s\n", line);
913 832c249c 2018-06-10 stsp } while (line);
914 621015ac 2018-07-23 stsp free(logmsg0);
916 971751ac 2018-03-27 stsp if (show_patch) {
917 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
918 971751ac 2018-03-27 stsp if (err == 0)
919 971751ac 2018-03-27 stsp printf("\n");
922 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
923 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
924 07862c20 2018-09-15 stsp return err;
927 f42b1b34 2018-03-12 stsp static const struct got_error *
928 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
929 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
930 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
932 f42b1b34 2018-03-12 stsp const struct got_error *err;
933 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
935 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
936 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
938 f42b1b34 2018-03-12 stsp return err;
939 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
943 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
944 372ccdbb 2018-06-10 stsp struct got_object_id *id;
946 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
949 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
951 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
952 9ba79e04 2018-06-11 stsp err = NULL;
955 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
957 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
963 b43fbaa0 2018-06-11 stsp if (id == NULL)
966 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
969 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
971 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
972 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
976 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
977 f42b1b34 2018-03-12 stsp return err;
980 4ed7e80c 2018-05-20 stsp __dead static void
981 6f3d1eb0 2018-03-12 stsp usage_log(void)
983 499d7ecc 2019-05-22 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
984 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
988 4ed7e80c 2018-05-20 stsp static const struct got_error *
989 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
991 f42b1b34 2018-03-12 stsp const struct got_error *error;
992 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
993 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
994 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
995 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
996 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
997 d142fc45 2018-04-01 stsp char *start_commit = NULL;
998 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
999 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1000 64a96a6d 2018-04-01 stsp const char *errstr;
1001 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1003 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1005 6715a751 2018-03-16 stsp #ifndef PROFILE
1006 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1009 f42b1b34 2018-03-12 stsp err(1, "pledge");
1012 499d7ecc 2019-05-22 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1013 79109fed 2018-03-27 stsp switch (ch) {
1015 499d7ecc 2019-05-22 stsp first_parent_traversal = 1;
1018 79109fed 2018-03-27 stsp show_patch = 1;
1021 d142fc45 2018-04-01 stsp start_commit = optarg;
1024 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1026 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1027 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1030 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1031 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1032 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1035 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1036 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1037 04ca23f4 2018-07-16 stsp err(1, "-r option");
1038 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1041 2deda0b9 2019-03-07 stsp usage_log();
1042 79109fed 2018-03-27 stsp /* NOTREACHED */
1046 79109fed 2018-03-27 stsp argc -= optind;
1047 79109fed 2018-03-27 stsp argv += optind;
1049 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1050 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1051 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1052 04ca23f4 2018-07-16 stsp goto done;
1055 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1056 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1057 cffc0aa4 2019-02-05 stsp goto done;
1058 cffc0aa4 2019-02-05 stsp error = NULL;
1060 e7301579 2019-03-18 stsp if (argc == 0) {
1061 cbd1af7a 2019-03-18 stsp path = strdup("");
1062 e7301579 2019-03-18 stsp if (path == NULL) {
1063 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1064 cbd1af7a 2019-03-18 stsp goto done;
1066 e7301579 2019-03-18 stsp } else if (argc == 1) {
1067 e7301579 2019-03-18 stsp if (worktree) {
1068 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1070 e7301579 2019-03-18 stsp if (error)
1071 e7301579 2019-03-18 stsp goto done;
1073 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1074 e7301579 2019-03-18 stsp if (path == NULL) {
1075 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1076 e7301579 2019-03-18 stsp goto done;
1080 cbd1af7a 2019-03-18 stsp usage_log();
1082 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1083 5486daa2 2019-05-11 stsp repo_path = worktree ?
1084 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1086 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1087 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1088 cffc0aa4 2019-02-05 stsp goto done;
1091 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1092 d7d4f210 2018-03-12 stsp if (error != NULL)
1093 04ca23f4 2018-07-16 stsp goto done;
1095 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1096 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1097 c02c541e 2019-03-29 stsp if (error)
1098 c02c541e 2019-03-29 stsp goto done;
1100 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1101 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1102 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1103 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1104 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1105 3235492e 2018-04-01 stsp if (error != NULL)
1106 3235492e 2018-04-01 stsp return error;
1107 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1108 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1109 3235492e 2018-04-01 stsp if (error != NULL)
1110 3235492e 2018-04-01 stsp return error;
1111 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1113 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1114 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1115 3235492e 2018-04-01 stsp if (error == NULL) {
1116 1caad61b 2019-02-01 stsp int obj_type;
1117 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1118 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1119 d1f2edc9 2018-06-13 stsp if (error != NULL)
1120 1caad61b 2019-02-01 stsp goto done;
1121 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1122 1caad61b 2019-02-01 stsp if (error != NULL)
1123 1caad61b 2019-02-01 stsp goto done;
1124 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1125 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1126 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1127 1caad61b 2019-02-01 stsp if (error != NULL)
1128 1caad61b 2019-02-01 stsp goto done;
1129 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1130 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1131 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1132 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1133 1caad61b 2019-02-01 stsp goto done;
1136 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1137 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1138 1caad61b 2019-02-01 stsp if (id == NULL)
1139 638f9024 2019-05-13 stsp error = got_error_from_errno(
1140 230a42bd 2019-05-11 jcs "got_object_id_dup");
1141 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1142 1caad61b 2019-02-01 stsp if (error)
1143 1caad61b 2019-02-01 stsp goto done;
1144 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1145 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1146 1caad61b 2019-02-01 stsp goto done;
1148 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1149 d1f2edc9 2018-06-13 stsp if (error != NULL)
1150 1caad61b 2019-02-01 stsp goto done;
1152 15a94983 2018-12-23 stsp if (commit == NULL) {
1153 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
1154 d1f2edc9 2018-06-13 stsp start_commit);
1155 d1f2edc9 2018-06-13 stsp if (error != NULL)
1156 d1f2edc9 2018-06-13 stsp return error;
1159 d7d4f210 2018-03-12 stsp if (error != NULL)
1160 04ca23f4 2018-07-16 stsp goto done;
1162 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1163 04ca23f4 2018-07-16 stsp if (error != NULL)
1164 04ca23f4 2018-07-16 stsp goto done;
1165 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1166 04ca23f4 2018-07-16 stsp free(path);
1167 04ca23f4 2018-07-16 stsp path = in_repo_path;
1170 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1171 199a4027 2019-02-02 stsp if (error)
1172 199a4027 2019-02-02 stsp goto done;
1174 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1175 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1177 04ca23f4 2018-07-16 stsp free(path);
1178 04ca23f4 2018-07-16 stsp free(repo_path);
1179 04ca23f4 2018-07-16 stsp free(cwd);
1181 cffc0aa4 2019-02-05 stsp if (worktree)
1182 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1183 ad242220 2018-09-08 stsp if (repo) {
1184 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1185 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1186 ad242220 2018-09-08 stsp if (error == NULL)
1187 ad242220 2018-09-08 stsp error = repo_error;
1189 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1190 8bf5b3c9 2018-03-17 stsp return error;
1193 4ed7e80c 2018-05-20 stsp __dead static void
1194 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1196 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1197 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1201 b72f483a 2019-02-05 stsp struct print_diff_arg {
1202 b72f483a 2019-02-05 stsp struct got_repository *repo;
1203 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1204 b72f483a 2019-02-05 stsp int diff_context;
1205 3fc0c068 2019-02-10 stsp const char *id_str;
1206 3fc0c068 2019-02-10 stsp int header_shown;
1209 4ed7e80c 2018-05-20 stsp static const struct got_error *
1210 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1211 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1213 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1214 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1215 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1216 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1217 b72f483a 2019-02-05 stsp char *abspath = NULL;
1218 b72f483a 2019-02-05 stsp struct stat sb;
1220 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1221 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1222 b72f483a 2019-02-05 stsp return NULL;
1224 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1225 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1226 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1227 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1230 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1231 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1233 d00136be 2019-03-26 stsp goto done;
1237 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1238 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1239 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1241 2ec1f75b 2019-03-26 stsp goto done;
1244 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1245 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1246 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1247 2ec1f75b 2019-03-26 stsp goto done;
1249 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1250 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1251 2ec1f75b 2019-03-26 stsp goto done;
1254 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1256 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1259 b72f483a 2019-02-05 stsp if (blob1)
1260 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1261 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1262 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1263 b72f483a 2019-02-05 stsp free(abspath);
1264 927df6b7 2019-02-10 stsp return err;
1267 927df6b7 2019-02-10 stsp static const struct got_error *
1268 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1270 b00d56cd 2018-04-01 stsp const struct got_error *error;
1271 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1272 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1273 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1274 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1275 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1276 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1277 15a94983 2018-12-23 stsp int type1, type2;
1278 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1279 c0cc5c62 2018-10-18 stsp const char *errstr;
1280 927df6b7 2019-02-10 stsp char *path = NULL;
1282 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1283 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1284 25eccc22 2019-01-04 stsp NULL) == -1)
1285 b00d56cd 2018-04-01 stsp err(1, "pledge");
1288 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1289 b00d56cd 2018-04-01 stsp switch (ch) {
1291 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1292 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1293 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1296 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1297 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1298 b72f483a 2019-02-05 stsp err(1, "-r option");
1299 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1302 2deda0b9 2019-03-07 stsp usage_diff();
1303 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1307 b00d56cd 2018-04-01 stsp argc -= optind;
1308 b00d56cd 2018-04-01 stsp argv += optind;
1310 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1311 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1312 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1313 b72f483a 2019-02-05 stsp goto done;
1315 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1316 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1317 927df6b7 2019-02-10 stsp goto done;
1318 927df6b7 2019-02-10 stsp if (argc <= 1) {
1319 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1320 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1321 927df6b7 2019-02-10 stsp goto done;
1323 b72f483a 2019-02-05 stsp if (repo_path)
1325 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1326 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1327 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1328 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1329 927df6b7 2019-02-10 stsp goto done;
1331 927df6b7 2019-02-10 stsp if (argc == 1) {
1332 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1334 927df6b7 2019-02-10 stsp if (error)
1335 927df6b7 2019-02-10 stsp goto done;
1337 927df6b7 2019-02-10 stsp path = strdup("");
1338 927df6b7 2019-02-10 stsp if (path == NULL) {
1339 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1340 927df6b7 2019-02-10 stsp goto done;
1343 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1344 15a94983 2018-12-23 stsp id_str1 = argv[0];
1345 15a94983 2018-12-23 stsp id_str2 = argv[1];
1346 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1347 30db809c 2019-06-05 stsp repo_path =
1348 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1349 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1350 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1351 30db809c 2019-06-05 stsp goto done;
1355 b00d56cd 2018-04-01 stsp usage_diff();
1357 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1358 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1359 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1360 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1363 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1364 76089277 2018-04-01 stsp free(repo_path);
1365 b00d56cd 2018-04-01 stsp if (error != NULL)
1366 b00d56cd 2018-04-01 stsp goto done;
1368 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1369 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1370 c02c541e 2019-03-29 stsp if (error)
1371 c02c541e 2019-03-29 stsp goto done;
1373 30db809c 2019-06-05 stsp if (argc <= 1) {
1374 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1375 b72f483a 2019-02-05 stsp char *id_str;
1376 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1377 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1378 b72f483a 2019-02-05 stsp if (error)
1379 b72f483a 2019-02-05 stsp goto done;
1380 b72f483a 2019-02-05 stsp arg.repo = repo;
1381 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1382 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1383 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1384 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1386 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1387 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1388 b72f483a 2019-02-05 stsp free(id_str);
1389 b72f483a 2019-02-05 stsp goto done;
1392 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1393 e02e74af 2019-05-28 stsp if (error) {
1394 e02e74af 2019-05-28 stsp struct got_reference *ref;
1395 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1396 e02e74af 2019-05-28 stsp goto done;
1397 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1398 e02e74af 2019-05-28 stsp if (error != NULL)
1399 e02e74af 2019-05-28 stsp goto done;
1400 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1401 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1402 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1403 5e70831e 2019-05-28 stsp goto done;
1405 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1406 e02e74af 2019-05-28 stsp got_ref_close(ref);
1407 e02e74af 2019-05-28 stsp if (error != NULL)
1408 5e70831e 2019-05-28 stsp goto done;
1410 5e70831e 2019-05-28 stsp label1 = strdup(id_str1);
1411 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1412 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1413 e02e74af 2019-05-28 stsp goto done;
1417 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1418 e02e74af 2019-05-28 stsp if (error) {
1419 e02e74af 2019-05-28 stsp struct got_reference *ref;
1420 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1421 e02e74af 2019-05-28 stsp goto done;
1422 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1423 e02e74af 2019-05-28 stsp if (error != NULL)
1424 5e70831e 2019-05-28 stsp goto done;
1425 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1426 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1427 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1428 e02e74af 2019-05-28 stsp goto done;
1430 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1431 e02e74af 2019-05-28 stsp got_ref_close(ref);
1432 e02e74af 2019-05-28 stsp if (error != NULL)
1433 e02e74af 2019-05-28 stsp goto done;
1435 5e70831e 2019-05-28 stsp label2 = strdup(id_str2);
1436 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1437 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1438 5e70831e 2019-05-28 stsp goto done;
1442 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1443 15a94983 2018-12-23 stsp if (error)
1444 15a94983 2018-12-23 stsp goto done;
1446 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1447 15a94983 2018-12-23 stsp if (error)
1448 15a94983 2018-12-23 stsp goto done;
1450 15a94983 2018-12-23 stsp if (type1 != type2) {
1451 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1452 b00d56cd 2018-04-01 stsp goto done;
1455 15a94983 2018-12-23 stsp switch (type1) {
1456 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1457 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1458 54156555 2018-12-24 stsp diff_context, repo, stdout);
1460 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1461 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1462 54156555 2018-12-24 stsp diff_context, repo, stdout);
1464 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1465 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1466 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1467 c0cc5c62 2018-10-18 stsp repo, stdout);
1470 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1474 5e70831e 2019-05-28 stsp free(label1);
1475 5e70831e 2019-05-28 stsp free(label2);
1476 15a94983 2018-12-23 stsp free(id1);
1477 15a94983 2018-12-23 stsp free(id2);
1478 927df6b7 2019-02-10 stsp free(path);
1479 b72f483a 2019-02-05 stsp if (worktree)
1480 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1481 ad242220 2018-09-08 stsp if (repo) {
1482 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1483 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1484 ad242220 2018-09-08 stsp if (error == NULL)
1485 ad242220 2018-09-08 stsp error = repo_error;
1487 b00d56cd 2018-04-01 stsp return error;
1490 404c43c4 2018-06-21 stsp __dead static void
1491 404c43c4 2018-06-21 stsp usage_blame(void)
1493 9270e621 2019-02-05 stsp fprintf(stderr,
1494 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1495 404c43c4 2018-06-21 stsp getprogname());
1499 404c43c4 2018-06-21 stsp static const struct got_error *
1500 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1502 404c43c4 2018-06-21 stsp const struct got_error *error;
1503 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1504 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1505 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1506 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1507 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1510 404c43c4 2018-06-21 stsp #ifndef PROFILE
1511 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1512 36e2fb66 2019-01-04 stsp NULL) == -1)
1513 404c43c4 2018-06-21 stsp err(1, "pledge");
1516 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1517 404c43c4 2018-06-21 stsp switch (ch) {
1519 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1522 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1523 66bea077 2018-08-02 stsp if (repo_path == NULL)
1524 66bea077 2018-08-02 stsp err(1, "-r option");
1525 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1528 2deda0b9 2019-03-07 stsp usage_blame();
1529 404c43c4 2018-06-21 stsp /* NOTREACHED */
1533 404c43c4 2018-06-21 stsp argc -= optind;
1534 404c43c4 2018-06-21 stsp argv += optind;
1536 a39318fd 2018-08-02 stsp if (argc == 1)
1537 404c43c4 2018-06-21 stsp path = argv[0];
1539 404c43c4 2018-06-21 stsp usage_blame();
1541 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1542 66bea077 2018-08-02 stsp if (cwd == NULL) {
1543 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1544 66bea077 2018-08-02 stsp goto done;
1546 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1547 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1548 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1549 66bea077 2018-08-02 stsp goto done;
1551 0c06baac 2019-02-05 stsp error = NULL;
1552 0c06baac 2019-02-05 stsp if (worktree) {
1553 0c06baac 2019-02-05 stsp repo_path =
1554 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1555 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1556 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1557 0c06baac 2019-02-05 stsp if (error)
1558 0c06baac 2019-02-05 stsp goto done;
1560 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1561 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1562 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1563 0c06baac 2019-02-05 stsp goto done;
1568 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1569 c02c541e 2019-03-29 stsp if (error != NULL)
1570 36e2fb66 2019-01-04 stsp goto done;
1572 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1573 c02c541e 2019-03-29 stsp if (error)
1574 404c43c4 2018-06-21 stsp goto done;
1576 0c06baac 2019-02-05 stsp if (worktree) {
1577 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1578 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1579 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1580 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1581 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1582 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1583 6efaaa2d 2019-02-05 stsp path) == -1) {
1584 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1585 0c06baac 2019-02-05 stsp goto done;
1587 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1590 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1592 0c06baac 2019-02-05 stsp if (error)
1593 66bea077 2018-08-02 stsp goto done;
1595 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1596 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1597 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1598 404c43c4 2018-06-21 stsp if (error != NULL)
1599 66bea077 2018-08-02 stsp goto done;
1600 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1601 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1602 404c43c4 2018-06-21 stsp if (error != NULL)
1603 66bea077 2018-08-02 stsp goto done;
1605 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1606 15a94983 2018-12-23 stsp commit_id_str);
1607 404c43c4 2018-06-21 stsp if (error != NULL)
1608 66bea077 2018-08-02 stsp goto done;
1611 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1613 66bea077 2018-08-02 stsp free(in_repo_path);
1614 66bea077 2018-08-02 stsp free(repo_path);
1615 66bea077 2018-08-02 stsp free(cwd);
1616 404c43c4 2018-06-21 stsp free(commit_id);
1617 0c06baac 2019-02-05 stsp if (worktree)
1618 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1619 ad242220 2018-09-08 stsp if (repo) {
1620 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1621 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1622 ad242220 2018-09-08 stsp if (error == NULL)
1623 ad242220 2018-09-08 stsp error = repo_error;
1625 404c43c4 2018-06-21 stsp return error;
1628 5de5890b 2018-10-18 stsp __dead static void
1629 5de5890b 2018-10-18 stsp usage_tree(void)
1631 c1669e2e 2019-01-09 stsp fprintf(stderr,
1632 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1633 5de5890b 2018-10-18 stsp getprogname());
1637 c1669e2e 2019-01-09 stsp static void
1638 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1639 c1669e2e 2019-01-09 stsp const char *root_path)
1641 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1643 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1644 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1647 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1648 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1649 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1652 5de5890b 2018-10-18 stsp static const struct got_error *
1653 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1654 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1655 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1657 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1658 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1659 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1660 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1661 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1663 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1665 5de5890b 2018-10-18 stsp goto done;
1667 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1669 5de5890b 2018-10-18 stsp goto done;
1670 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1671 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1672 5de5890b 2018-10-18 stsp while (te) {
1673 5de5890b 2018-10-18 stsp char *id = NULL;
1675 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1678 5de5890b 2018-10-18 stsp if (show_ids) {
1679 5de5890b 2018-10-18 stsp char *id_str;
1680 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1682 5de5890b 2018-10-18 stsp goto done;
1683 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1685 5de5890b 2018-10-18 stsp free(id_str);
1686 5de5890b 2018-10-18 stsp goto done;
1688 5de5890b 2018-10-18 stsp free(id_str);
1690 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1693 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1694 c1669e2e 2019-01-09 stsp char *child_path;
1695 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1696 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1697 c1669e2e 2019-01-09 stsp te->name) == -1) {
1698 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1699 c1669e2e 2019-01-09 stsp goto done;
1701 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1702 c1669e2e 2019-01-09 stsp root_path, repo);
1703 c1669e2e 2019-01-09 stsp free(child_path);
1705 c1669e2e 2019-01-09 stsp goto done;
1708 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1712 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1713 5de5890b 2018-10-18 stsp free(tree_id);
1714 5de5890b 2018-10-18 stsp return err;
1717 5de5890b 2018-10-18 stsp static const struct got_error *
1718 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1720 5de5890b 2018-10-18 stsp const struct got_error *error;
1721 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1722 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1723 7a2c19d6 2019-02-05 stsp const char *path;
1724 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1725 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1726 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1727 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1730 5de5890b 2018-10-18 stsp #ifndef PROFILE
1731 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1732 0f8d269b 2019-01-04 stsp NULL) == -1)
1733 5de5890b 2018-10-18 stsp err(1, "pledge");
1736 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1737 5de5890b 2018-10-18 stsp switch (ch) {
1739 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1742 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1743 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1744 5de5890b 2018-10-18 stsp err(1, "-r option");
1745 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1748 5de5890b 2018-10-18 stsp show_ids = 1;
1751 c1669e2e 2019-01-09 stsp recurse = 1;
1754 2deda0b9 2019-03-07 stsp usage_tree();
1755 5de5890b 2018-10-18 stsp /* NOTREACHED */
1759 5de5890b 2018-10-18 stsp argc -= optind;
1760 5de5890b 2018-10-18 stsp argv += optind;
1762 5de5890b 2018-10-18 stsp if (argc == 1)
1763 5de5890b 2018-10-18 stsp path = argv[0];
1764 5de5890b 2018-10-18 stsp else if (argc > 1)
1765 5de5890b 2018-10-18 stsp usage_tree();
1767 7a2c19d6 2019-02-05 stsp path = NULL;
1769 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1770 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1771 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1772 9bf7a39b 2019-02-05 stsp goto done;
1774 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1775 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1776 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1777 7a2c19d6 2019-02-05 stsp goto done;
1779 7a2c19d6 2019-02-05 stsp error = NULL;
1780 7a2c19d6 2019-02-05 stsp if (worktree) {
1781 7a2c19d6 2019-02-05 stsp repo_path =
1782 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1783 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1784 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1785 7a2c19d6 2019-02-05 stsp if (error)
1786 7a2c19d6 2019-02-05 stsp goto done;
1788 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1789 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1790 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1791 7a2c19d6 2019-02-05 stsp goto done;
1796 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1797 5de5890b 2018-10-18 stsp if (error != NULL)
1798 c02c541e 2019-03-29 stsp goto done;
1800 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1801 c02c541e 2019-03-29 stsp if (error)
1802 5de5890b 2018-10-18 stsp goto done;
1804 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1805 9bf7a39b 2019-02-05 stsp if (worktree) {
1806 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1807 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1808 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1809 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1810 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1811 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1812 9bf7a39b 2019-02-05 stsp goto done;
1814 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1816 9bf7a39b 2019-02-05 stsp if (error)
1817 9bf7a39b 2019-02-05 stsp goto done;
1819 9bf7a39b 2019-02-05 stsp path = "/";
1821 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1822 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1823 9bf7a39b 2019-02-05 stsp if (error != NULL)
1824 9bf7a39b 2019-02-05 stsp goto done;
1827 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1828 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1829 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1830 5de5890b 2018-10-18 stsp if (error != NULL)
1831 5de5890b 2018-10-18 stsp goto done;
1832 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1833 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1834 5de5890b 2018-10-18 stsp if (error != NULL)
1835 5de5890b 2018-10-18 stsp goto done;
1837 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1838 15a94983 2018-12-23 stsp commit_id_str);
1839 5de5890b 2018-10-18 stsp if (error != NULL)
1840 5de5890b 2018-10-18 stsp goto done;
1843 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1844 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1846 5de5890b 2018-10-18 stsp free(in_repo_path);
1847 5de5890b 2018-10-18 stsp free(repo_path);
1848 5de5890b 2018-10-18 stsp free(cwd);
1849 5de5890b 2018-10-18 stsp free(commit_id);
1850 7a2c19d6 2019-02-05 stsp if (worktree)
1851 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1852 5de5890b 2018-10-18 stsp if (repo) {
1853 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1854 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1855 5de5890b 2018-10-18 stsp if (error == NULL)
1856 5de5890b 2018-10-18 stsp error = repo_error;
1858 5de5890b 2018-10-18 stsp return error;
1861 6bad629b 2019-02-04 stsp __dead static void
1862 6bad629b 2019-02-04 stsp usage_status(void)
1864 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1868 b72f483a 2019-02-05 stsp static const struct got_error *
1869 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1870 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1872 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1873 b72f483a 2019-02-05 stsp return NULL;
1876 6bad629b 2019-02-04 stsp static const struct got_error *
1877 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1879 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1880 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1881 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1882 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1885 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1886 6bad629b 2019-02-04 stsp switch (ch) {
1888 2deda0b9 2019-03-07 stsp usage_status();
1889 6bad629b 2019-02-04 stsp /* NOTREACHED */
1893 6bad629b 2019-02-04 stsp argc -= optind;
1894 6bad629b 2019-02-04 stsp argv += optind;
1896 6bad629b 2019-02-04 stsp #ifndef PROFILE
1897 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1898 6bad629b 2019-02-04 stsp NULL) == -1)
1899 6bad629b 2019-02-04 stsp err(1, "pledge");
1901 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1902 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1903 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1904 927df6b7 2019-02-10 stsp goto done;
1907 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1908 927df6b7 2019-02-10 stsp if (error != NULL)
1909 927df6b7 2019-02-10 stsp goto done;
1911 6bad629b 2019-02-04 stsp if (argc == 0) {
1912 927df6b7 2019-02-10 stsp path = strdup("");
1913 927df6b7 2019-02-10 stsp if (path == NULL) {
1914 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1915 6bad629b 2019-02-04 stsp goto done;
1917 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1918 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1919 927df6b7 2019-02-10 stsp if (error)
1920 6bad629b 2019-02-04 stsp goto done;
1922 6bad629b 2019-02-04 stsp usage_status();
1924 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1925 6bad629b 2019-02-04 stsp if (error != NULL)
1926 6bad629b 2019-02-04 stsp goto done;
1928 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1929 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1930 6bad629b 2019-02-04 stsp if (error)
1931 6bad629b 2019-02-04 stsp goto done;
1933 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1934 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1936 927df6b7 2019-02-10 stsp free(cwd);
1937 927df6b7 2019-02-10 stsp free(path);
1938 d0eebce4 2019-03-11 stsp return error;
1941 d0eebce4 2019-03-11 stsp __dead static void
1942 d0eebce4 2019-03-11 stsp usage_ref(void)
1944 d0eebce4 2019-03-11 stsp fprintf(stderr,
1945 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
1946 d0eebce4 2019-03-11 stsp getprogname());
1950 d0eebce4 2019-03-11 stsp static const struct got_error *
1951 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1953 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1954 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1955 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1957 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1958 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1960 d0eebce4 2019-03-11 stsp return err;
1962 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1963 d0eebce4 2019-03-11 stsp char *refstr;
1964 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1965 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1966 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
1967 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1968 d0eebce4 2019-03-11 stsp free(refstr);
1971 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1972 d0eebce4 2019-03-11 stsp return NULL;
1975 d0eebce4 2019-03-11 stsp static const struct got_error *
1976 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1978 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1979 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1981 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
1983 d0eebce4 2019-03-11 stsp return err;
1985 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1986 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1987 d0eebce4 2019-03-11 stsp return err;
1990 d0eebce4 2019-03-11 stsp static const struct got_error *
1991 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
1993 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1994 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1995 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1997 d83d9d5c 2019-05-13 stsp err = got_object_resolve_id_str(&id, repo, target);
1998 d83d9d5c 2019-05-13 stsp if (err) {
1999 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2001 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2002 d83d9d5c 2019-05-13 stsp return err;
2003 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2005 d83d9d5c 2019-05-13 stsp return err;
2006 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2007 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2009 d83d9d5c 2019-05-13 stsp return err;
2012 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2014 d0eebce4 2019-03-11 stsp goto done;
2016 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2019 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2021 d0eebce4 2019-03-11 stsp return err;
2024 d0eebce4 2019-03-11 stsp static const struct got_error *
2025 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2027 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2028 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2029 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2030 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2031 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2032 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2034 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2035 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2036 d0eebce4 2019-03-11 stsp switch (ch) {
2038 d0eebce4 2019-03-11 stsp delref = optarg;
2041 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2042 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2043 d0eebce4 2019-03-11 stsp err(1, "-r option");
2044 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2047 d0eebce4 2019-03-11 stsp do_list = 1;
2050 d0eebce4 2019-03-11 stsp usage_ref();
2051 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2055 d0eebce4 2019-03-11 stsp if (do_list && delref)
2056 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2058 d0eebce4 2019-03-11 stsp argc -= optind;
2059 d0eebce4 2019-03-11 stsp argv += optind;
2061 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2062 d0eebce4 2019-03-11 stsp if (argc > 0)
2063 d0eebce4 2019-03-11 stsp usage_ref();
2064 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2065 d0eebce4 2019-03-11 stsp usage_ref();
2067 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2068 e0b57350 2019-03-12 stsp if (do_list) {
2069 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2070 e0b57350 2019-03-12 stsp NULL) == -1)
2071 e0b57350 2019-03-12 stsp err(1, "pledge");
2073 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2074 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2075 e0b57350 2019-03-12 stsp err(1, "pledge");
2078 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2079 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2080 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2081 d0eebce4 2019-03-11 stsp goto done;
2084 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2085 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2086 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2087 d0eebce4 2019-03-11 stsp goto done;
2089 d0eebce4 2019-03-11 stsp error = NULL;
2090 d0eebce4 2019-03-11 stsp if (worktree) {
2091 d0eebce4 2019-03-11 stsp repo_path =
2092 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2093 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2094 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2095 d0eebce4 2019-03-11 stsp if (error)
2096 d0eebce4 2019-03-11 stsp goto done;
2098 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2099 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2100 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2101 d0eebce4 2019-03-11 stsp goto done;
2106 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2107 d0eebce4 2019-03-11 stsp if (error != NULL)
2108 d0eebce4 2019-03-11 stsp goto done;
2110 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2111 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2112 c02c541e 2019-03-29 stsp if (error)
2113 c02c541e 2019-03-29 stsp goto done;
2115 d0eebce4 2019-03-11 stsp if (do_list)
2116 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2117 d0eebce4 2019-03-11 stsp else if (delref)
2118 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2120 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2123 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2124 d0eebce4 2019-03-11 stsp if (worktree)
2125 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2126 d0eebce4 2019-03-11 stsp free(cwd);
2127 d0eebce4 2019-03-11 stsp free(repo_path);
2128 d00136be 2019-03-26 stsp return error;
2131 d00136be 2019-03-26 stsp __dead static void
2132 d00136be 2019-03-26 stsp usage_add(void)
2134 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2138 d00136be 2019-03-26 stsp static const struct got_error *
2139 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2141 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2142 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2143 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2144 1dd54920 2019-05-11 stsp char *cwd = NULL;
2145 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2146 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2149 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2151 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2152 d00136be 2019-03-26 stsp switch (ch) {
2154 d00136be 2019-03-26 stsp usage_add();
2155 d00136be 2019-03-26 stsp /* NOTREACHED */
2159 d00136be 2019-03-26 stsp argc -= optind;
2160 d00136be 2019-03-26 stsp argv += optind;
2162 723c305c 2019-05-11 jcs if (argc < 1)
2163 d00136be 2019-03-26 stsp usage_add();
2165 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2166 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2167 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2168 723c305c 2019-05-11 jcs if (path == NULL) {
2169 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2172 1dd54920 2019-05-11 stsp free(path);
2175 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2176 d00136be 2019-03-26 stsp if (cwd == NULL) {
2177 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2178 d00136be 2019-03-26 stsp goto done;
2181 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2182 d00136be 2019-03-26 stsp if (error)
2183 d00136be 2019-03-26 stsp goto done;
2185 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2186 031a5338 2019-03-26 stsp if (error != NULL)
2187 031a5338 2019-03-26 stsp goto done;
2189 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2190 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2191 d00136be 2019-03-26 stsp if (error)
2192 d00136be 2019-03-26 stsp goto done;
2194 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2195 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2196 723c305c 2019-05-11 jcs if (path == NULL) {
2197 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2201 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2202 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2203 1dd54920 2019-05-11 stsp if (error) {
2204 1dd54920 2019-05-11 stsp free(path);
2208 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2209 1dd54920 2019-05-11 stsp NULL, repo);
2212 031a5338 2019-03-26 stsp got_repo_close(repo);
2213 d00136be 2019-03-26 stsp if (worktree)
2214 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2215 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2216 1dd54920 2019-05-11 stsp free((char *)pe->path);
2217 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2218 2ec1f75b 2019-03-26 stsp free(cwd);
2219 2ec1f75b 2019-03-26 stsp return error;
2222 2ec1f75b 2019-03-26 stsp __dead static void
2223 2ec1f75b 2019-03-26 stsp usage_rm(void)
2225 cc11e7e3 2019-06-04 stsp fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2229 2ec1f75b 2019-03-26 stsp static const struct got_error *
2230 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2232 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2233 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2234 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2235 17ed4618 2019-06-02 stsp char *cwd = NULL;
2236 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2237 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2238 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2240 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2242 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2243 2ec1f75b 2019-03-26 stsp switch (ch) {
2245 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2248 2ec1f75b 2019-03-26 stsp usage_add();
2249 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2253 2ec1f75b 2019-03-26 stsp argc -= optind;
2254 2ec1f75b 2019-03-26 stsp argv += optind;
2256 17ed4618 2019-06-02 stsp if (argc < 1)
2257 2ec1f75b 2019-03-26 stsp usage_rm();
2259 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2260 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2261 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2262 17ed4618 2019-06-02 stsp if (path == NULL) {
2263 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2264 17ed4618 2019-06-02 stsp goto done;
2266 17ed4618 2019-06-02 stsp free(path);
2269 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2270 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2271 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2272 2ec1f75b 2019-03-26 stsp goto done;
2274 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2275 2ec1f75b 2019-03-26 stsp if (error)
2276 2ec1f75b 2019-03-26 stsp goto done;
2278 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2280 2ec1f75b 2019-03-26 stsp goto done;
2282 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2283 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2284 2ec1f75b 2019-03-26 stsp if (error)
2285 2ec1f75b 2019-03-26 stsp goto done;
2287 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2288 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2289 17ed4618 2019-06-02 stsp if (path == NULL) {
2290 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2291 17ed4618 2019-06-02 stsp goto done;
2294 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2295 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2296 17ed4618 2019-06-02 stsp if (error) {
2297 17ed4618 2019-06-02 stsp free(path);
2298 17ed4618 2019-06-02 stsp goto done;
2301 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2302 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2303 a129376b 2019-03-28 stsp if (error)
2304 a129376b 2019-03-28 stsp goto done;
2307 a129376b 2019-03-28 stsp got_repo_close(repo);
2308 a129376b 2019-03-28 stsp if (worktree)
2309 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2310 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2311 17ed4618 2019-06-02 stsp free((char *)pe->path);
2312 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2313 a129376b 2019-03-28 stsp free(cwd);
2314 a129376b 2019-03-28 stsp return error;
2317 a129376b 2019-03-28 stsp __dead static void
2318 a129376b 2019-03-28 stsp usage_revert(void)
2320 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2324 a129376b 2019-03-28 stsp static void
2325 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2327 a129376b 2019-03-28 stsp while (path[0] == '/')
2329 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2332 a129376b 2019-03-28 stsp static const struct got_error *
2333 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2335 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2336 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2337 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2338 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2339 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2340 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2341 e20a8b6f 2019-06-04 stsp int ch, i;
2343 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2345 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2346 a129376b 2019-03-28 stsp switch (ch) {
2348 a129376b 2019-03-28 stsp usage_revert();
2349 a129376b 2019-03-28 stsp /* NOTREACHED */
2353 a129376b 2019-03-28 stsp argc -= optind;
2354 a129376b 2019-03-28 stsp argv += optind;
2356 e20a8b6f 2019-06-04 stsp if (argc < 1)
2357 a129376b 2019-03-28 stsp usage_revert();
2359 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
2360 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
2361 e20a8b6f 2019-06-04 stsp if (path == NULL) {
2362 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
2363 e20a8b6f 2019-06-04 stsp goto done;
2366 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
2367 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2368 e20a8b6f 2019-06-04 stsp if (error) {
2369 e20a8b6f 2019-06-04 stsp free(path);
2370 e20a8b6f 2019-06-04 stsp goto done;
2374 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2375 a129376b 2019-03-28 stsp if (cwd == NULL) {
2376 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2377 a129376b 2019-03-28 stsp goto done;
2379 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2380 2ec1f75b 2019-03-26 stsp if (error)
2381 2ec1f75b 2019-03-26 stsp goto done;
2383 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2384 a129376b 2019-03-28 stsp if (error != NULL)
2385 a129376b 2019-03-28 stsp goto done;
2387 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2388 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2389 a129376b 2019-03-28 stsp if (error)
2390 a129376b 2019-03-28 stsp goto done;
2392 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
2393 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2394 a129376b 2019-03-28 stsp if (error)
2395 a129376b 2019-03-28 stsp goto done;
2398 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2399 2ec1f75b 2019-03-26 stsp if (worktree)
2400 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2401 2ec1f75b 2019-03-26 stsp free(path);
2402 d00136be 2019-03-26 stsp free(cwd);
2403 6bad629b 2019-02-04 stsp return error;
2406 c4296144 2019-05-09 stsp __dead static void
2407 c4296144 2019-05-09 stsp usage_commit(void)
2409 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2414 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2417 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2418 33ad4cbe 2019-05-12 jcs int st = -1;
2420 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2421 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2422 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2424 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2426 33ad4cbe 2019-05-12 jcs goto doneediting;
2428 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2429 33ad4cbe 2019-05-12 jcs _exit(127);
2432 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2433 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2436 33ad4cbe 2019-05-12 jcs doneediting:
2437 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2438 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2439 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2441 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2442 33ad4cbe 2019-05-12 jcs errno = EINTR;
2446 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2449 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2450 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2451 e2ba3d07 2019-05-13 stsp const char *editor;
2452 e0870e44 2019-05-13 stsp const char *worktree_path;
2453 76d98825 2019-06-03 stsp const char *branch_name;
2454 314a6357 2019-05-13 stsp const char *repo_path;
2455 e0870e44 2019-05-13 stsp char *logmsg_path;
2459 33ad4cbe 2019-05-12 jcs static const struct got_error *
2460 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2463 76d98825 2019-06-03 stsp char *initial_content = NULL;
2464 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2465 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2466 e0870e44 2019-05-13 stsp char *template = NULL;
2467 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2468 33ad4cbe 2019-05-12 jcs char buf[1024];
2469 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2471 33ad4cbe 2019-05-12 jcs size_t len;
2472 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2474 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2475 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2476 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2477 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2478 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2479 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2480 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2481 33ad4cbe 2019-05-12 jcs return NULL;
2484 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2485 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
2487 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
2488 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
2489 76d98825 2019-06-03 stsp a->branch_name) == -1)
2490 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2492 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2494 e0870e44 2019-05-13 stsp goto done;
2496 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2498 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2499 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2500 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
2501 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
2502 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
2506 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2507 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2511 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2512 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2516 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2517 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2521 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2522 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2523 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2524 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2525 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2526 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2530 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2531 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2532 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2533 fcde04d9 2019-05-13 stsp goto done;
2535 cc79381d 2019-05-22 stsp (*logmsg)[0] = '\0';
2538 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2539 d4592c7c 2019-05-22 stsp if (fp == NULL) {
2540 d4592c7c 2019-05-22 stsp err = got_error_from_errno("fopen");
2541 d4592c7c 2019-05-22 stsp goto done;
2543 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2544 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2545 e0870e44 2019-05-13 stsp content_changed = 1;
2546 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2547 78527a0a 2019-05-22 stsp continue; /* remove comments and leading empty lines */
2548 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2550 33ad4cbe 2019-05-12 jcs fclose(fp);
2552 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2553 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2557 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2558 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2559 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2560 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2561 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2562 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2566 76d98825 2019-06-03 stsp free(initial_content);
2567 e0870e44 2019-05-13 stsp free(template);
2569 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2570 314a6357 2019-05-13 stsp if (err == NULL)
2571 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2572 33ad4cbe 2019-05-12 jcs return err;
2575 c4296144 2019-05-09 stsp static const struct got_error *
2576 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2578 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2579 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2580 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2581 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2582 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2583 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2584 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2585 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2586 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2589 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2590 c4296144 2019-05-09 stsp switch (ch) {
2592 c4296144 2019-05-09 stsp logmsg = optarg;
2595 c4296144 2019-05-09 stsp usage_commit();
2596 c4296144 2019-05-09 stsp /* NOTREACHED */
2600 c4296144 2019-05-09 stsp argc -= optind;
2601 c4296144 2019-05-09 stsp argv += optind;
2603 c4296144 2019-05-09 stsp if (argc == 1) {
2604 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2605 c4296144 2019-05-09 stsp if (path == NULL) {
2606 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2607 c4296144 2019-05-09 stsp goto done;
2609 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2610 c4296144 2019-05-09 stsp } else if (argc != 0)
2611 c4296144 2019-05-09 stsp usage_commit();
2613 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2614 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2615 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2616 35bd8fed 2019-05-09 stsp goto done;
2619 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2620 c4296144 2019-05-09 stsp if (cwd == NULL) {
2621 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2622 c4296144 2019-05-09 stsp goto done;
2624 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2625 c4296144 2019-05-09 stsp if (error)
2626 c4296144 2019-05-09 stsp goto done;
2628 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2629 c4296144 2019-05-09 stsp if (error != NULL)
2630 c4296144 2019-05-09 stsp goto done;
2633 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2634 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2636 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2637 314a6357 2019-05-13 stsp error = get_editor(&editor);
2639 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2640 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2641 c4296144 2019-05-09 stsp if (error)
2642 c4296144 2019-05-09 stsp goto done;
2644 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2645 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2646 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2647 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2648 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2649 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
2650 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2651 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
2652 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2653 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2654 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2655 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2656 e0870e44 2019-05-13 stsp if (error) {
2657 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2658 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2659 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2660 c4296144 2019-05-09 stsp goto done;
2663 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2664 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2666 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2667 c4296144 2019-05-09 stsp if (error)
2668 c4296144 2019-05-09 stsp goto done;
2669 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
2672 c4296144 2019-05-09 stsp got_repo_close(repo);
2673 c4296144 2019-05-09 stsp if (worktree)
2674 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2675 c4296144 2019-05-09 stsp free(path);
2676 c4296144 2019-05-09 stsp free(cwd);
2677 c4296144 2019-05-09 stsp free(id_str);
2678 e2ba3d07 2019-05-13 stsp free(editor);
2679 234035bc 2019-06-01 stsp return error;
2682 234035bc 2019-06-01 stsp __dead static void
2683 234035bc 2019-06-01 stsp usage_cherrypick(void)
2685 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2689 234035bc 2019-06-01 stsp static const struct got_error *
2690 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
2692 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
2693 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
2694 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
2695 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
2696 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
2697 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
2698 234035bc 2019-06-01 stsp struct got_object_qid *pid;
2699 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
2700 234035bc 2019-06-01 stsp int ch, did_something = 0;
2702 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2703 234035bc 2019-06-01 stsp switch (ch) {
2705 234035bc 2019-06-01 stsp usage_cherrypick();
2706 234035bc 2019-06-01 stsp /* NOTREACHED */
2710 234035bc 2019-06-01 stsp argc -= optind;
2711 234035bc 2019-06-01 stsp argv += optind;
2713 234035bc 2019-06-01 stsp if (argc != 1)
2714 234035bc 2019-06-01 stsp usage_cherrypick();
2716 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
2717 234035bc 2019-06-01 stsp if (cwd == NULL) {
2718 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
2719 234035bc 2019-06-01 stsp goto done;
2721 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
2722 234035bc 2019-06-01 stsp if (error)
2723 234035bc 2019-06-01 stsp goto done;
2725 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2726 234035bc 2019-06-01 stsp if (error != NULL)
2727 234035bc 2019-06-01 stsp goto done;
2729 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2730 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
2731 234035bc 2019-06-01 stsp if (error)
2732 234035bc 2019-06-01 stsp goto done;
2734 234035bc 2019-06-01 stsp error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2735 234035bc 2019-06-01 stsp if (error != NULL) {
2736 234035bc 2019-06-01 stsp struct got_reference *ref;
2737 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2738 234035bc 2019-06-01 stsp goto done;
2739 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
2740 234035bc 2019-06-01 stsp if (error != NULL)
2741 234035bc 2019-06-01 stsp goto done;
2742 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
2743 234035bc 2019-06-01 stsp got_ref_close(ref);
2744 234035bc 2019-06-01 stsp if (error != NULL)
2745 234035bc 2019-06-01 stsp goto done;
2747 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
2748 234035bc 2019-06-01 stsp if (error)
2749 234035bc 2019-06-01 stsp goto done;
2751 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
2752 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
2753 234035bc 2019-06-01 stsp if (error != NULL)
2754 234035bc 2019-06-01 stsp goto done;
2756 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
2757 234035bc 2019-06-01 stsp if (error) {
2758 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
2759 234035bc 2019-06-01 stsp goto done;
2760 234035bc 2019-06-01 stsp error = NULL;
2762 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
2763 234035bc 2019-06-01 stsp goto done;
2766 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2767 234035bc 2019-06-01 stsp if (error)
2768 234035bc 2019-06-01 stsp goto done;
2769 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2770 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2771 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
2773 234035bc 2019-06-01 stsp if (error != NULL)
2774 234035bc 2019-06-01 stsp goto done;
2776 234035bc 2019-06-01 stsp if (did_something)
2777 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
2779 234035bc 2019-06-01 stsp if (commit)
2780 234035bc 2019-06-01 stsp got_object_commit_close(commit);
2781 234035bc 2019-06-01 stsp free(commit_id_str);
2782 234035bc 2019-06-01 stsp if (head_ref)
2783 234035bc 2019-06-01 stsp got_ref_close(head_ref);
2784 234035bc 2019-06-01 stsp if (worktree)
2785 234035bc 2019-06-01 stsp got_worktree_close(worktree);
2787 234035bc 2019-06-01 stsp got_repo_close(repo);
2788 c4296144 2019-05-09 stsp return error;
2791 5ef14e63 2019-06-02 stsp __dead static void
2792 5ef14e63 2019-06-02 stsp usage_backout(void)
2794 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2798 5ef14e63 2019-06-02 stsp static const struct got_error *
2799 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
2801 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
2802 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
2803 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
2804 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
2805 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
2806 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
2807 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
2808 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
2809 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
2811 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2812 5ef14e63 2019-06-02 stsp switch (ch) {
2814 5ef14e63 2019-06-02 stsp usage_backout();
2815 5ef14e63 2019-06-02 stsp /* NOTREACHED */
2819 5ef14e63 2019-06-02 stsp argc -= optind;
2820 5ef14e63 2019-06-02 stsp argv += optind;
2822 5ef14e63 2019-06-02 stsp if (argc != 1)
2823 5ef14e63 2019-06-02 stsp usage_backout();
2825 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
2826 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
2827 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
2828 5ef14e63 2019-06-02 stsp goto done;
2830 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
2831 5ef14e63 2019-06-02 stsp if (error)
2832 5ef14e63 2019-06-02 stsp goto done;
2834 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2835 5ef14e63 2019-06-02 stsp if (error != NULL)
2836 5ef14e63 2019-06-02 stsp goto done;
2838 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2839 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
2840 5ef14e63 2019-06-02 stsp if (error)
2841 5ef14e63 2019-06-02 stsp goto done;
2843 5ef14e63 2019-06-02 stsp error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2844 5ef14e63 2019-06-02 stsp if (error != NULL) {
2845 5ef14e63 2019-06-02 stsp struct got_reference *ref;
2846 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2847 5ef14e63 2019-06-02 stsp goto done;
2848 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
2849 5ef14e63 2019-06-02 stsp if (error != NULL)
2850 5ef14e63 2019-06-02 stsp goto done;
2851 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
2852 5ef14e63 2019-06-02 stsp got_ref_close(ref);
2853 5ef14e63 2019-06-02 stsp if (error != NULL)
2854 5ef14e63 2019-06-02 stsp goto done;
2856 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2857 5ef14e63 2019-06-02 stsp if (error)
2858 5ef14e63 2019-06-02 stsp goto done;
2860 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
2861 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
2862 5ef14e63 2019-06-02 stsp if (error != NULL)
2863 5ef14e63 2019-06-02 stsp goto done;
2865 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
2866 5ef14e63 2019-06-02 stsp if (error)
2867 5ef14e63 2019-06-02 stsp goto done;
2869 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2870 5ef14e63 2019-06-02 stsp if (error)
2871 5ef14e63 2019-06-02 stsp goto done;
2872 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2873 5ef14e63 2019-06-02 stsp if (pid == NULL) {
2874 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
2875 5ef14e63 2019-06-02 stsp goto done;
2878 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2879 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
2880 5ef14e63 2019-06-02 stsp if (error != NULL)
2881 5ef14e63 2019-06-02 stsp goto done;
2883 5ef14e63 2019-06-02 stsp if (did_something)
2884 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
2886 5ef14e63 2019-06-02 stsp if (commit)
2887 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
2888 5ef14e63 2019-06-02 stsp free(commit_id_str);
2889 5ef14e63 2019-06-02 stsp if (head_ref)
2890 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
2891 5ef14e63 2019-06-02 stsp if (worktree)
2892 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
2894 5ef14e63 2019-06-02 stsp got_repo_close(repo);
2895 5ef14e63 2019-06-02 stsp return error;