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 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
79 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
83 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
84 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
85 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
86 d00136be 2019-03-26 stsp __dead static void usage_add(void);
87 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
88 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
89 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
90 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
91 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
93 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
95 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
96 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
97 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
98 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
99 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
100 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
101 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
102 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
103 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
104 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
105 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
106 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
107 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
110 2c7829a4 2019-06-17 stsp { "init", cmd_init, usage_init,
111 2c7829a4 2019-06-17 stsp "create a new empty repository" },
112 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
113 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
114 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
115 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
116 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
117 1b6b95a8 2018-03-12 stsp "show repository history" },
118 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
119 b00d56cd 2018-04-01 stsp "compare files and directories" },
120 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
121 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
122 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
123 a67e2392 2019-03-26 stsp "list files and directories in repository" },
124 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
125 1b6b95a8 2018-03-12 stsp "show modification status of files" },
126 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
127 d0eebce4 2019-03-11 stsp "manage references in repository" },
128 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
129 bd14628f 2019-05-12 stsp "add new files to version control" },
130 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
131 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
132 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
133 a129376b 2019-03-28 stsp "revert uncommitted changes" },
134 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
135 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
136 234035bc 2019-06-01 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick,
137 234035bc 2019-06-01 stsp "merge a single commit from another branch into a work tree" },
138 5ef14e63 2019-06-02 stsp { "backout", cmd_backout, usage_backout,
139 5ef14e63 2019-06-02 stsp "reverse-merge changes from a commit into a work tree" },
143 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
145 5c860e29 2018-03-12 stsp struct cmd *cmd;
146 5c860e29 2018-03-12 stsp unsigned int i;
148 1b6b95a8 2018-03-12 stsp int hflag = 0;
150 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
152 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
153 5c860e29 2018-03-12 stsp switch (ch) {
159 5c860e29 2018-03-12 stsp /* NOTREACHED */
163 5c860e29 2018-03-12 stsp argc -= optind;
164 5c860e29 2018-03-12 stsp argv += optind;
165 1e70621d 2018-03-27 stsp optind = 0;
167 5c860e29 2018-03-12 stsp if (argc <= 0)
170 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
171 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
173 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
174 d7d4f210 2018-03-12 stsp const struct got_error *error;
176 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
178 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
182 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
184 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
185 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
186 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
193 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
197 4ed7e80c 2018-05-20 stsp __dead static void
198 5c860e29 2018-03-12 stsp usage(void)
202 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
203 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
204 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
205 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
206 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
211 0266afb7 2019-01-04 stsp static const struct got_error *
212 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
214 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
215 e2ba3d07 2019-05-13 stsp const char *editor;
217 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
218 e2ba3d07 2019-05-13 stsp if (editor == NULL)
219 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
221 0ee7065d 2019-05-13 stsp if (editor) {
222 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
224 0ee7065d 2019-05-13 stsp return err;
227 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
228 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
229 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
230 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
233 e2ba3d07 2019-05-13 stsp return NULL;
236 e2ba3d07 2019-05-13 stsp static const struct got_error *
237 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
238 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
240 163ce85a 2019-05-13 stsp const struct got_error *err;
242 a0937847 2019-05-11 stsp if (create_worktree) {
243 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
244 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
246 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
247 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
249 163ce85a 2019-05-13 stsp err = NULL;
251 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
252 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
256 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
257 163ce85a 2019-05-13 stsp return err;
260 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
261 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
263 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
264 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
266 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
267 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
269 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
270 163ce85a 2019-05-13 stsp if (err != NULL)
271 163ce85a 2019-05-13 stsp return err;
273 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
274 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
276 0266afb7 2019-01-04 stsp return NULL;
279 2c7829a4 2019-06-17 stsp __dead static void
280 2c7829a4 2019-06-17 stsp usage_init(void)
282 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
286 2c7829a4 2019-06-17 stsp static const struct got_error *
287 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
289 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
290 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
293 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
294 2c7829a4 2019-06-17 stsp switch (ch) {
296 2c7829a4 2019-06-17 stsp usage_init();
297 2c7829a4 2019-06-17 stsp /* NOTREACHED */
301 2c7829a4 2019-06-17 stsp argc -= optind;
302 2c7829a4 2019-06-17 stsp argv += optind;
304 2c7829a4 2019-06-17 stsp #ifndef PROFILE
305 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
306 2c7829a4 2019-06-17 stsp err(1, "pledge");
308 2c7829a4 2019-06-17 stsp if (argc != 1)
309 bc20e173 2019-06-17 stsp usage_init();
311 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
312 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
313 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
315 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
317 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
318 2c7829a4 2019-06-17 stsp if (error &&
319 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
322 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
326 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
327 2c7829a4 2019-06-17 stsp if (error != NULL)
331 2c7829a4 2019-06-17 stsp free(repo_path);
332 2c7829a4 2019-06-17 stsp return error;
335 4ed7e80c 2018-05-20 stsp __dead static void
336 c09a553d 2018-03-12 stsp usage_checkout(void)
338 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
339 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
343 92a684f4 2018-03-12 stsp static void
344 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
346 92a684f4 2018-03-12 stsp char *worktree_path = arg;
348 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
349 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
352 92a684f4 2018-03-12 stsp while (path[0] == '/')
355 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
358 99437157 2018-11-11 stsp static const struct got_error *
359 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
361 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
362 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
363 99437157 2018-11-11 stsp return NULL;
366 8069f636 2019-01-12 stsp static const struct got_error *
367 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
368 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
370 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
371 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
373 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
374 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
376 36a38700 2019-05-10 stsp return err;
378 d5bea539 2019-05-13 stsp if (yca_id == NULL)
379 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
382 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
383 d5bea539 2019-05-13 stsp * and the work tree's base commit.
385 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
387 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
395 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
396 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
397 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
399 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
400 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
401 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
403 d5bea539 2019-05-13 stsp free(yca_id);
404 d5bea539 2019-05-13 stsp return NULL;
407 a367ff0f 2019-05-14 stsp static const struct got_error *
408 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
409 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
411 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
412 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
413 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
414 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
416 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
420 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
424 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
429 a367ff0f 2019-05-14 stsp struct got_object_id *id;
430 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
432 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
433 a367ff0f 2019-05-14 stsp err = NULL;
436 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
438 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
445 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
446 a367ff0f 2019-05-14 stsp is_same_branch = 1;
453 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
454 a367ff0f 2019-05-14 stsp free(head_commit_id);
455 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
456 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
457 a367ff0f 2019-05-14 stsp return err;
460 4ed7e80c 2018-05-20 stsp static const struct got_error *
461 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
463 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
464 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
465 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
466 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
467 c09a553d 2018-03-12 stsp char *repo_path = NULL;
468 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
469 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
470 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
471 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
472 72151b04 2019-05-11 stsp int ch, same_path_prefix;
474 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
475 0bb8a95e 2018-03-12 stsp switch (ch) {
477 08573d5b 2019-05-14 stsp branch_name = optarg;
480 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
481 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
482 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
485 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
488 2deda0b9 2019-03-07 stsp usage_checkout();
489 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
493 0bb8a95e 2018-03-12 stsp argc -= optind;
494 0bb8a95e 2018-03-12 stsp argv += optind;
496 6715a751 2018-03-16 stsp #ifndef PROFILE
497 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
498 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
499 c09a553d 2018-03-12 stsp err(1, "pledge");
501 0bb8a95e 2018-03-12 stsp if (argc == 1) {
502 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
503 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
504 76089277 2018-04-01 stsp if (repo_path == NULL)
505 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
506 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
507 76089277 2018-04-01 stsp if (cwd == NULL) {
508 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
511 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
512 230a42bd 2019-05-11 jcs base = basename(path_prefix);
513 230a42bd 2019-05-11 jcs if (base == NULL) {
514 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
515 230a42bd 2019-05-11 jcs path_prefix);
519 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
520 230a42bd 2019-05-11 jcs if (base == NULL) {
521 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
526 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
527 c09a553d 2018-03-12 stsp if (dotgit)
528 c09a553d 2018-03-12 stsp *dotgit = '\0';
529 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
530 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
535 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
536 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
537 76089277 2018-04-01 stsp if (repo_path == NULL) {
538 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
541 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
542 76089277 2018-04-01 stsp if (worktree_path == NULL) {
543 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
547 c09a553d 2018-03-12 stsp usage_checkout();
549 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
550 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
552 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
553 c09a553d 2018-03-12 stsp if (error != NULL)
556 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
560 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
561 c09a553d 2018-03-12 stsp if (error != NULL)
564 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
565 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
568 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
569 c09a553d 2018-03-12 stsp if (error != NULL)
572 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
573 e5dc7198 2018-12-29 stsp path_prefix);
574 e5dc7198 2018-12-29 stsp if (error != NULL)
576 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
577 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
581 8069f636 2019-01-12 stsp if (commit_id_str) {
582 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
583 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
584 8069f636 2019-01-12 stsp commit_id_str);
585 8069f636 2019-01-12 stsp if (error != NULL)
587 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
588 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
589 8069f636 2019-01-12 stsp if (error != NULL) {
590 8069f636 2019-01-12 stsp free(commit_id);
593 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
596 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
597 8069f636 2019-01-12 stsp commit_id);
598 8069f636 2019-01-12 stsp free(commit_id);
603 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
604 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
605 c09a553d 2018-03-12 stsp if (error != NULL)
608 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
611 8069f636 2019-01-12 stsp free(commit_id_str);
612 76089277 2018-04-01 stsp free(repo_path);
613 507dc3bb 2018-12-29 stsp free(worktree_path);
614 507dc3bb 2018-12-29 stsp return error;
617 507dc3bb 2018-12-29 stsp __dead static void
618 507dc3bb 2018-12-29 stsp usage_update(void)
620 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
621 507dc3bb 2018-12-29 stsp getprogname());
625 507dc3bb 2018-12-29 stsp static void
626 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
628 784955db 2019-01-12 stsp int *did_something = arg;
630 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
633 1545c615 2019-02-10 stsp *did_something = 1;
635 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
636 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
639 507dc3bb 2018-12-29 stsp while (path[0] == '/')
641 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
644 be7061eb 2018-12-30 stsp static const struct got_error *
645 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
646 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
647 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
649 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
650 a1fb16d8 2019-05-24 stsp char *base_id_str;
651 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
653 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
654 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
655 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
656 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
657 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
658 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
659 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
662 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
663 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
665 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
666 a1fb16d8 2019-05-24 stsp return err;
667 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
669 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
670 a1fb16d8 2019-05-24 stsp return NULL;
672 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
673 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
674 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
676 a1fb16d8 2019-05-24 stsp return err;
677 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
678 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
679 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
680 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
681 a1fb16d8 2019-05-24 stsp return NULL;
684 a1fb16d8 2019-05-24 stsp static const struct got_error *
685 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
687 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
688 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
689 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
690 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
691 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
692 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
693 024e9686 2019-05-14 stsp const char *branch_name = NULL;
694 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
695 784955db 2019-01-12 stsp int ch, did_something = 0;
697 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
698 507dc3bb 2018-12-29 stsp switch (ch) {
700 024e9686 2019-05-14 stsp branch_name = optarg;
703 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
704 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
705 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
708 2deda0b9 2019-03-07 stsp usage_update();
709 507dc3bb 2018-12-29 stsp /* NOTREACHED */
713 507dc3bb 2018-12-29 stsp argc -= optind;
714 507dc3bb 2018-12-29 stsp argv += optind;
716 507dc3bb 2018-12-29 stsp #ifndef PROFILE
717 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
718 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
719 507dc3bb 2018-12-29 stsp err(1, "pledge");
721 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
722 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
723 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
726 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
730 507dc3bb 2018-12-29 stsp if (argc == 0) {
731 c4cdcb68 2019-04-03 stsp path = strdup("");
732 c4cdcb68 2019-04-03 stsp if (path == NULL) {
733 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
736 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
737 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
741 507dc3bb 2018-12-29 stsp usage_update();
743 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
744 507dc3bb 2018-12-29 stsp if (error != NULL)
747 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
748 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
752 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
753 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
754 024e9686 2019-05-14 stsp if (error != NULL)
756 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
757 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
758 9c4b8182 2019-01-02 stsp if (error != NULL)
760 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
761 507dc3bb 2018-12-29 stsp if (error != NULL)
764 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
765 507dc3bb 2018-12-29 stsp commit_id_str);
766 507dc3bb 2018-12-29 stsp if (error != NULL)
770 a1fb16d8 2019-05-24 stsp if (branch_name) {
771 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
772 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
773 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
774 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
775 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
776 024e9686 2019-05-14 stsp getprogname(), path);
777 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
780 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
783 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
784 a367ff0f 2019-05-14 stsp free(head_commit_id);
785 024e9686 2019-05-14 stsp if (error != NULL)
787 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
790 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
794 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
795 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
796 a367ff0f 2019-05-14 stsp if (error != NULL) {
797 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
798 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
801 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
806 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
807 507dc3bb 2018-12-29 stsp commit_id) != 0) {
808 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
809 507dc3bb 2018-12-29 stsp commit_id);
814 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
815 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
816 507dc3bb 2018-12-29 stsp if (error != NULL)
819 784955db 2019-01-12 stsp if (did_something)
820 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
822 784955db 2019-01-12 stsp printf("Already up-to-date\n");
824 c09a553d 2018-03-12 stsp free(worktree_path);
825 c4cdcb68 2019-04-03 stsp free(path);
826 507dc3bb 2018-12-29 stsp free(commit_id);
827 9c4b8182 2019-01-02 stsp free(commit_id_str);
828 c09a553d 2018-03-12 stsp return error;
831 f42b1b34 2018-03-12 stsp static const struct got_error *
832 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
833 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
835 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
836 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
837 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
838 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
839 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
841 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
842 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
844 79109fed 2018-03-27 stsp return err;
846 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
847 79f35eb3 2018-06-11 stsp if (qid != NULL) {
848 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
850 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
852 79109fed 2018-03-27 stsp return err;
854 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
855 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
856 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
858 0f2b3dca 2018-12-22 stsp return err;
860 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
862 79109fed 2018-03-27 stsp return err;
865 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
869 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
870 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
871 aaa13589 2019-06-01 stsp arg.outfile = stdout;
872 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
873 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
876 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
877 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
878 0f2b3dca 2018-12-22 stsp free(id_str1);
879 0f2b3dca 2018-12-22 stsp free(id_str2);
880 79109fed 2018-03-27 stsp return err;
883 4bb494d5 2018-06-16 stsp static char *
884 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
886 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
887 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
893 79109fed 2018-03-27 stsp static const struct got_error *
894 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
895 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
896 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
898 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
899 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
900 6c281f94 2018-06-11 stsp char datebuf[26];
901 45d799e2 2018-12-23 stsp time_t committer_time;
902 45d799e2 2018-12-23 stsp const char *author, *committer;
903 199a4027 2019-02-02 stsp char *refs_str = NULL;
904 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
906 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
908 199a4027 2019-02-02 stsp const char *name;
909 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
911 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
912 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
914 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
916 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
918 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
920 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
922 199a4027 2019-02-02 stsp s = refs_str;
923 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
924 199a4027 2019-02-02 stsp name) == -1) {
925 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
931 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
933 8bf5b3c9 2018-03-17 stsp return err;
935 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
936 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
937 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
938 832c249c 2018-06-10 stsp free(id_str);
939 d3d493d7 2019-02-21 stsp id_str = NULL;
940 d3d493d7 2019-02-21 stsp free(refs_str);
941 d3d493d7 2019-02-21 stsp refs_str = NULL;
942 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
943 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
944 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
945 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
946 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
947 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
948 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
949 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
950 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
951 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
952 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
954 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
955 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
956 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
958 a0603db2 2018-06-10 stsp return err;
959 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
960 a0603db2 2018-06-10 stsp free(id_str);
964 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
965 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
966 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
968 621015ac 2018-07-23 stsp logmsg = logmsg0;
970 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
972 832c249c 2018-06-10 stsp printf(" %s\n", line);
973 832c249c 2018-06-10 stsp } while (line);
974 621015ac 2018-07-23 stsp free(logmsg0);
976 971751ac 2018-03-27 stsp if (show_patch) {
977 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
978 971751ac 2018-03-27 stsp if (err == 0)
979 971751ac 2018-03-27 stsp printf("\n");
982 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
983 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
984 07862c20 2018-09-15 stsp return err;
987 f42b1b34 2018-03-12 stsp static const struct got_error *
988 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
989 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
990 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
992 f42b1b34 2018-03-12 stsp const struct got_error *err;
993 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
995 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
996 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
998 f42b1b34 2018-03-12 stsp return err;
999 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1001 fcc85cad 2018-07-22 stsp goto done;
1003 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1004 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1006 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1009 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1010 372ccdbb 2018-06-10 stsp if (err) {
1011 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1012 9ba79e04 2018-06-11 stsp err = NULL;
1015 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1017 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1023 b43fbaa0 2018-06-11 stsp if (id == NULL)
1026 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1029 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1031 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1032 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1036 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1037 f42b1b34 2018-03-12 stsp return err;
1040 4ed7e80c 2018-05-20 stsp __dead static void
1041 6f3d1eb0 2018-03-12 stsp usage_log(void)
1043 499d7ecc 2019-05-22 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1044 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1048 4ed7e80c 2018-05-20 stsp static const struct got_error *
1049 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1051 f42b1b34 2018-03-12 stsp const struct got_error *error;
1052 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1053 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1054 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1055 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1056 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1057 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1058 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1059 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1060 64a96a6d 2018-04-01 stsp const char *errstr;
1061 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1063 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1065 6715a751 2018-03-16 stsp #ifndef PROFILE
1066 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1069 f42b1b34 2018-03-12 stsp err(1, "pledge");
1072 499d7ecc 2019-05-22 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1073 79109fed 2018-03-27 stsp switch (ch) {
1075 499d7ecc 2019-05-22 stsp first_parent_traversal = 1;
1078 79109fed 2018-03-27 stsp show_patch = 1;
1081 d142fc45 2018-04-01 stsp start_commit = optarg;
1084 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1086 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1087 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1090 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1091 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1092 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1095 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1096 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1097 04ca23f4 2018-07-16 stsp err(1, "-r option");
1098 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1101 2deda0b9 2019-03-07 stsp usage_log();
1102 79109fed 2018-03-27 stsp /* NOTREACHED */
1106 79109fed 2018-03-27 stsp argc -= optind;
1107 79109fed 2018-03-27 stsp argv += optind;
1109 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1110 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1111 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1112 04ca23f4 2018-07-16 stsp goto done;
1115 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1116 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1117 cffc0aa4 2019-02-05 stsp goto done;
1118 cffc0aa4 2019-02-05 stsp error = NULL;
1120 e7301579 2019-03-18 stsp if (argc == 0) {
1121 cbd1af7a 2019-03-18 stsp path = strdup("");
1122 e7301579 2019-03-18 stsp if (path == NULL) {
1123 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1124 cbd1af7a 2019-03-18 stsp goto done;
1126 e7301579 2019-03-18 stsp } else if (argc == 1) {
1127 e7301579 2019-03-18 stsp if (worktree) {
1128 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1130 e7301579 2019-03-18 stsp if (error)
1131 e7301579 2019-03-18 stsp goto done;
1133 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1134 e7301579 2019-03-18 stsp if (path == NULL) {
1135 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1136 e7301579 2019-03-18 stsp goto done;
1140 cbd1af7a 2019-03-18 stsp usage_log();
1142 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1143 5486daa2 2019-05-11 stsp repo_path = worktree ?
1144 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1146 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1147 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1148 cffc0aa4 2019-02-05 stsp goto done;
1151 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1152 d7d4f210 2018-03-12 stsp if (error != NULL)
1153 04ca23f4 2018-07-16 stsp goto done;
1155 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1156 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1157 c02c541e 2019-03-29 stsp if (error)
1158 c02c541e 2019-03-29 stsp goto done;
1160 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1161 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1162 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1163 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1164 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1165 3235492e 2018-04-01 stsp if (error != NULL)
1166 3235492e 2018-04-01 stsp return error;
1167 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1168 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1169 3235492e 2018-04-01 stsp if (error != NULL)
1170 3235492e 2018-04-01 stsp return error;
1171 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1173 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1174 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1175 3235492e 2018-04-01 stsp if (error == NULL) {
1176 1caad61b 2019-02-01 stsp int obj_type;
1177 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1178 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1179 d1f2edc9 2018-06-13 stsp if (error != NULL)
1180 1caad61b 2019-02-01 stsp goto done;
1181 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1182 1caad61b 2019-02-01 stsp if (error != NULL)
1183 1caad61b 2019-02-01 stsp goto done;
1184 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1185 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1186 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1187 1caad61b 2019-02-01 stsp if (error != NULL)
1188 1caad61b 2019-02-01 stsp goto done;
1189 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1190 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1191 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1192 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1193 1caad61b 2019-02-01 stsp goto done;
1196 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1197 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1198 1caad61b 2019-02-01 stsp if (id == NULL)
1199 638f9024 2019-05-13 stsp error = got_error_from_errno(
1200 230a42bd 2019-05-11 jcs "got_object_id_dup");
1201 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1202 1caad61b 2019-02-01 stsp if (error)
1203 1caad61b 2019-02-01 stsp goto done;
1204 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1205 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1206 1caad61b 2019-02-01 stsp goto done;
1208 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1209 d1f2edc9 2018-06-13 stsp if (error != NULL)
1210 1caad61b 2019-02-01 stsp goto done;
1212 15a94983 2018-12-23 stsp if (commit == NULL) {
1213 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
1214 d1f2edc9 2018-06-13 stsp start_commit);
1215 d1f2edc9 2018-06-13 stsp if (error != NULL)
1216 d1f2edc9 2018-06-13 stsp return error;
1219 d7d4f210 2018-03-12 stsp if (error != NULL)
1220 04ca23f4 2018-07-16 stsp goto done;
1222 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1223 04ca23f4 2018-07-16 stsp if (error != NULL)
1224 04ca23f4 2018-07-16 stsp goto done;
1225 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1226 04ca23f4 2018-07-16 stsp free(path);
1227 04ca23f4 2018-07-16 stsp path = in_repo_path;
1230 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1231 199a4027 2019-02-02 stsp if (error)
1232 199a4027 2019-02-02 stsp goto done;
1234 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1235 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1237 04ca23f4 2018-07-16 stsp free(path);
1238 04ca23f4 2018-07-16 stsp free(repo_path);
1239 04ca23f4 2018-07-16 stsp free(cwd);
1241 cffc0aa4 2019-02-05 stsp if (worktree)
1242 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1243 ad242220 2018-09-08 stsp if (repo) {
1244 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1245 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1246 ad242220 2018-09-08 stsp if (error == NULL)
1247 ad242220 2018-09-08 stsp error = repo_error;
1249 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1250 8bf5b3c9 2018-03-17 stsp return error;
1253 4ed7e80c 2018-05-20 stsp __dead static void
1254 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1256 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1257 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1261 b72f483a 2019-02-05 stsp struct print_diff_arg {
1262 b72f483a 2019-02-05 stsp struct got_repository *repo;
1263 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1264 b72f483a 2019-02-05 stsp int diff_context;
1265 3fc0c068 2019-02-10 stsp const char *id_str;
1266 3fc0c068 2019-02-10 stsp int header_shown;
1269 4ed7e80c 2018-05-20 stsp static const struct got_error *
1270 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1271 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1273 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1274 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1275 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1276 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1277 b72f483a 2019-02-05 stsp char *abspath = NULL;
1278 b72f483a 2019-02-05 stsp struct stat sb;
1280 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1281 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1282 b72f483a 2019-02-05 stsp return NULL;
1284 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1285 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1286 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1287 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1290 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1291 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1293 d00136be 2019-03-26 stsp goto done;
1297 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1298 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1299 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1301 2ec1f75b 2019-03-26 stsp goto done;
1304 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1305 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1306 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1307 2ec1f75b 2019-03-26 stsp goto done;
1309 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1310 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1311 2ec1f75b 2019-03-26 stsp goto done;
1314 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1316 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1319 b72f483a 2019-02-05 stsp if (blob1)
1320 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1321 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1322 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1323 b72f483a 2019-02-05 stsp free(abspath);
1324 927df6b7 2019-02-10 stsp return err;
1327 927df6b7 2019-02-10 stsp static const struct got_error *
1328 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1330 b00d56cd 2018-04-01 stsp const struct got_error *error;
1331 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1332 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1333 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1334 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1335 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1336 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1337 15a94983 2018-12-23 stsp int type1, type2;
1338 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1339 c0cc5c62 2018-10-18 stsp const char *errstr;
1340 927df6b7 2019-02-10 stsp char *path = NULL;
1342 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1343 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1344 25eccc22 2019-01-04 stsp NULL) == -1)
1345 b00d56cd 2018-04-01 stsp err(1, "pledge");
1348 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1349 b00d56cd 2018-04-01 stsp switch (ch) {
1351 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1352 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1353 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1356 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1357 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1358 b72f483a 2019-02-05 stsp err(1, "-r option");
1359 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1362 2deda0b9 2019-03-07 stsp usage_diff();
1363 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1367 b00d56cd 2018-04-01 stsp argc -= optind;
1368 b00d56cd 2018-04-01 stsp argv += optind;
1370 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1371 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1372 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1373 b72f483a 2019-02-05 stsp goto done;
1375 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1376 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1377 927df6b7 2019-02-10 stsp goto done;
1378 927df6b7 2019-02-10 stsp if (argc <= 1) {
1379 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1380 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1381 927df6b7 2019-02-10 stsp goto done;
1383 b72f483a 2019-02-05 stsp if (repo_path)
1385 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1386 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1387 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1388 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1389 927df6b7 2019-02-10 stsp goto done;
1391 927df6b7 2019-02-10 stsp if (argc == 1) {
1392 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1394 927df6b7 2019-02-10 stsp if (error)
1395 927df6b7 2019-02-10 stsp goto done;
1397 927df6b7 2019-02-10 stsp path = strdup("");
1398 927df6b7 2019-02-10 stsp if (path == NULL) {
1399 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1400 927df6b7 2019-02-10 stsp goto done;
1403 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1404 15a94983 2018-12-23 stsp id_str1 = argv[0];
1405 15a94983 2018-12-23 stsp id_str2 = argv[1];
1406 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1407 30db809c 2019-06-05 stsp repo_path =
1408 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1409 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1410 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1411 30db809c 2019-06-05 stsp goto done;
1415 b00d56cd 2018-04-01 stsp usage_diff();
1417 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1418 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1419 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1420 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1423 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1424 76089277 2018-04-01 stsp free(repo_path);
1425 b00d56cd 2018-04-01 stsp if (error != NULL)
1426 b00d56cd 2018-04-01 stsp goto done;
1428 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1429 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1430 c02c541e 2019-03-29 stsp if (error)
1431 c02c541e 2019-03-29 stsp goto done;
1433 30db809c 2019-06-05 stsp if (argc <= 1) {
1434 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1435 b72f483a 2019-02-05 stsp char *id_str;
1436 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1437 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1438 b72f483a 2019-02-05 stsp if (error)
1439 b72f483a 2019-02-05 stsp goto done;
1440 b72f483a 2019-02-05 stsp arg.repo = repo;
1441 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1442 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1443 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1444 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1446 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1447 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1448 b72f483a 2019-02-05 stsp free(id_str);
1449 b72f483a 2019-02-05 stsp goto done;
1452 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1453 e02e74af 2019-05-28 stsp if (error) {
1454 e02e74af 2019-05-28 stsp struct got_reference *ref;
1455 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1456 e02e74af 2019-05-28 stsp goto done;
1457 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1458 e02e74af 2019-05-28 stsp if (error != NULL)
1459 e02e74af 2019-05-28 stsp goto done;
1460 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1461 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1462 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1463 5e70831e 2019-05-28 stsp goto done;
1465 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1466 e02e74af 2019-05-28 stsp got_ref_close(ref);
1467 e02e74af 2019-05-28 stsp if (error != NULL)
1468 5e70831e 2019-05-28 stsp goto done;
1470 5e70831e 2019-05-28 stsp label1 = strdup(id_str1);
1471 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1472 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1473 e02e74af 2019-05-28 stsp goto done;
1477 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1478 e02e74af 2019-05-28 stsp if (error) {
1479 e02e74af 2019-05-28 stsp struct got_reference *ref;
1480 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1481 e02e74af 2019-05-28 stsp goto done;
1482 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1483 e02e74af 2019-05-28 stsp if (error != NULL)
1484 5e70831e 2019-05-28 stsp goto done;
1485 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1486 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1487 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1488 e02e74af 2019-05-28 stsp goto done;
1490 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1491 e02e74af 2019-05-28 stsp got_ref_close(ref);
1492 e02e74af 2019-05-28 stsp if (error != NULL)
1493 e02e74af 2019-05-28 stsp goto done;
1495 5e70831e 2019-05-28 stsp label2 = strdup(id_str2);
1496 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1497 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1498 5e70831e 2019-05-28 stsp goto done;
1502 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1503 15a94983 2018-12-23 stsp if (error)
1504 15a94983 2018-12-23 stsp goto done;
1506 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1507 15a94983 2018-12-23 stsp if (error)
1508 15a94983 2018-12-23 stsp goto done;
1510 15a94983 2018-12-23 stsp if (type1 != type2) {
1511 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1512 b00d56cd 2018-04-01 stsp goto done;
1515 15a94983 2018-12-23 stsp switch (type1) {
1516 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1517 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 54156555 2018-12-24 stsp diff_context, repo, stdout);
1520 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1521 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1522 54156555 2018-12-24 stsp diff_context, repo, stdout);
1524 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1525 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1526 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1527 c0cc5c62 2018-10-18 stsp repo, stdout);
1530 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1534 5e70831e 2019-05-28 stsp free(label1);
1535 5e70831e 2019-05-28 stsp free(label2);
1536 15a94983 2018-12-23 stsp free(id1);
1537 15a94983 2018-12-23 stsp free(id2);
1538 927df6b7 2019-02-10 stsp free(path);
1539 b72f483a 2019-02-05 stsp if (worktree)
1540 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1541 ad242220 2018-09-08 stsp if (repo) {
1542 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1543 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1544 ad242220 2018-09-08 stsp if (error == NULL)
1545 ad242220 2018-09-08 stsp error = repo_error;
1547 b00d56cd 2018-04-01 stsp return error;
1550 404c43c4 2018-06-21 stsp __dead static void
1551 404c43c4 2018-06-21 stsp usage_blame(void)
1553 9270e621 2019-02-05 stsp fprintf(stderr,
1554 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1555 404c43c4 2018-06-21 stsp getprogname());
1559 404c43c4 2018-06-21 stsp static const struct got_error *
1560 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1562 404c43c4 2018-06-21 stsp const struct got_error *error;
1563 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1564 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1565 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1567 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1570 404c43c4 2018-06-21 stsp #ifndef PROFILE
1571 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1572 36e2fb66 2019-01-04 stsp NULL) == -1)
1573 404c43c4 2018-06-21 stsp err(1, "pledge");
1576 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1577 404c43c4 2018-06-21 stsp switch (ch) {
1579 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1582 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1583 66bea077 2018-08-02 stsp if (repo_path == NULL)
1584 66bea077 2018-08-02 stsp err(1, "-r option");
1585 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1588 2deda0b9 2019-03-07 stsp usage_blame();
1589 404c43c4 2018-06-21 stsp /* NOTREACHED */
1593 404c43c4 2018-06-21 stsp argc -= optind;
1594 404c43c4 2018-06-21 stsp argv += optind;
1596 a39318fd 2018-08-02 stsp if (argc == 1)
1597 404c43c4 2018-06-21 stsp path = argv[0];
1599 404c43c4 2018-06-21 stsp usage_blame();
1601 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1602 66bea077 2018-08-02 stsp if (cwd == NULL) {
1603 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1604 66bea077 2018-08-02 stsp goto done;
1606 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1607 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1608 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 66bea077 2018-08-02 stsp goto done;
1611 0c06baac 2019-02-05 stsp error = NULL;
1612 0c06baac 2019-02-05 stsp if (worktree) {
1613 0c06baac 2019-02-05 stsp repo_path =
1614 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1615 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1616 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1617 0c06baac 2019-02-05 stsp if (error)
1618 0c06baac 2019-02-05 stsp goto done;
1620 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1621 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1622 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1623 0c06baac 2019-02-05 stsp goto done;
1628 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1629 c02c541e 2019-03-29 stsp if (error != NULL)
1630 36e2fb66 2019-01-04 stsp goto done;
1632 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1633 c02c541e 2019-03-29 stsp if (error)
1634 404c43c4 2018-06-21 stsp goto done;
1636 0c06baac 2019-02-05 stsp if (worktree) {
1637 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1638 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1639 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1640 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1641 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1643 6efaaa2d 2019-02-05 stsp path) == -1) {
1644 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1645 0c06baac 2019-02-05 stsp goto done;
1647 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1650 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 0c06baac 2019-02-05 stsp if (error)
1653 66bea077 2018-08-02 stsp goto done;
1655 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1656 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1657 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1658 404c43c4 2018-06-21 stsp if (error != NULL)
1659 66bea077 2018-08-02 stsp goto done;
1660 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1661 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1662 404c43c4 2018-06-21 stsp if (error != NULL)
1663 66bea077 2018-08-02 stsp goto done;
1665 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1666 15a94983 2018-12-23 stsp commit_id_str);
1667 404c43c4 2018-06-21 stsp if (error != NULL)
1668 66bea077 2018-08-02 stsp goto done;
1671 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1673 66bea077 2018-08-02 stsp free(in_repo_path);
1674 66bea077 2018-08-02 stsp free(repo_path);
1675 66bea077 2018-08-02 stsp free(cwd);
1676 404c43c4 2018-06-21 stsp free(commit_id);
1677 0c06baac 2019-02-05 stsp if (worktree)
1678 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1679 ad242220 2018-09-08 stsp if (repo) {
1680 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1681 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1682 ad242220 2018-09-08 stsp if (error == NULL)
1683 ad242220 2018-09-08 stsp error = repo_error;
1685 404c43c4 2018-06-21 stsp return error;
1688 5de5890b 2018-10-18 stsp __dead static void
1689 5de5890b 2018-10-18 stsp usage_tree(void)
1691 c1669e2e 2019-01-09 stsp fprintf(stderr,
1692 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1693 5de5890b 2018-10-18 stsp getprogname());
1697 c1669e2e 2019-01-09 stsp static void
1698 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 c1669e2e 2019-01-09 stsp const char *root_path)
1701 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1703 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1704 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1707 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1708 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1709 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 5de5890b 2018-10-18 stsp static const struct got_error *
1713 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1714 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1715 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1717 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1718 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1719 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1720 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1721 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1723 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1725 5de5890b 2018-10-18 stsp goto done;
1727 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1729 5de5890b 2018-10-18 stsp goto done;
1730 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1731 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1732 5de5890b 2018-10-18 stsp while (te) {
1733 5de5890b 2018-10-18 stsp char *id = NULL;
1735 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1738 5de5890b 2018-10-18 stsp if (show_ids) {
1739 5de5890b 2018-10-18 stsp char *id_str;
1740 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1742 5de5890b 2018-10-18 stsp goto done;
1743 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1744 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1745 5de5890b 2018-10-18 stsp free(id_str);
1746 5de5890b 2018-10-18 stsp goto done;
1748 5de5890b 2018-10-18 stsp free(id_str);
1750 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1753 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1754 c1669e2e 2019-01-09 stsp char *child_path;
1755 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1756 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1757 c1669e2e 2019-01-09 stsp te->name) == -1) {
1758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1759 c1669e2e 2019-01-09 stsp goto done;
1761 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1762 c1669e2e 2019-01-09 stsp root_path, repo);
1763 c1669e2e 2019-01-09 stsp free(child_path);
1765 c1669e2e 2019-01-09 stsp goto done;
1768 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1772 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1773 5de5890b 2018-10-18 stsp free(tree_id);
1774 5de5890b 2018-10-18 stsp return err;
1777 5de5890b 2018-10-18 stsp static const struct got_error *
1778 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1780 5de5890b 2018-10-18 stsp const struct got_error *error;
1781 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1782 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1783 7a2c19d6 2019-02-05 stsp const char *path;
1784 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1786 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1787 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1790 5de5890b 2018-10-18 stsp #ifndef PROFILE
1791 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1792 0f8d269b 2019-01-04 stsp NULL) == -1)
1793 5de5890b 2018-10-18 stsp err(1, "pledge");
1796 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1797 5de5890b 2018-10-18 stsp switch (ch) {
1799 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1802 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1803 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1804 5de5890b 2018-10-18 stsp err(1, "-r option");
1805 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1808 5de5890b 2018-10-18 stsp show_ids = 1;
1811 c1669e2e 2019-01-09 stsp recurse = 1;
1814 2deda0b9 2019-03-07 stsp usage_tree();
1815 5de5890b 2018-10-18 stsp /* NOTREACHED */
1819 5de5890b 2018-10-18 stsp argc -= optind;
1820 5de5890b 2018-10-18 stsp argv += optind;
1822 5de5890b 2018-10-18 stsp if (argc == 1)
1823 5de5890b 2018-10-18 stsp path = argv[0];
1824 5de5890b 2018-10-18 stsp else if (argc > 1)
1825 5de5890b 2018-10-18 stsp usage_tree();
1827 7a2c19d6 2019-02-05 stsp path = NULL;
1829 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1830 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1831 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1832 9bf7a39b 2019-02-05 stsp goto done;
1834 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1835 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1836 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1837 7a2c19d6 2019-02-05 stsp goto done;
1839 7a2c19d6 2019-02-05 stsp error = NULL;
1840 7a2c19d6 2019-02-05 stsp if (worktree) {
1841 7a2c19d6 2019-02-05 stsp repo_path =
1842 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1843 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1844 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1845 7a2c19d6 2019-02-05 stsp if (error)
1846 7a2c19d6 2019-02-05 stsp goto done;
1848 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1849 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1850 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1851 7a2c19d6 2019-02-05 stsp goto done;
1856 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1857 5de5890b 2018-10-18 stsp if (error != NULL)
1858 c02c541e 2019-03-29 stsp goto done;
1860 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1861 c02c541e 2019-03-29 stsp if (error)
1862 5de5890b 2018-10-18 stsp goto done;
1864 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1865 9bf7a39b 2019-02-05 stsp if (worktree) {
1866 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1867 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1868 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1869 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1870 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1871 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1872 9bf7a39b 2019-02-05 stsp goto done;
1874 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1876 9bf7a39b 2019-02-05 stsp if (error)
1877 9bf7a39b 2019-02-05 stsp goto done;
1879 9bf7a39b 2019-02-05 stsp path = "/";
1881 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1882 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1883 9bf7a39b 2019-02-05 stsp if (error != NULL)
1884 9bf7a39b 2019-02-05 stsp goto done;
1887 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1888 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1889 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1890 5de5890b 2018-10-18 stsp if (error != NULL)
1891 5de5890b 2018-10-18 stsp goto done;
1892 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1893 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1894 5de5890b 2018-10-18 stsp if (error != NULL)
1895 5de5890b 2018-10-18 stsp goto done;
1897 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1898 15a94983 2018-12-23 stsp commit_id_str);
1899 5de5890b 2018-10-18 stsp if (error != NULL)
1900 5de5890b 2018-10-18 stsp goto done;
1903 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1906 5de5890b 2018-10-18 stsp free(in_repo_path);
1907 5de5890b 2018-10-18 stsp free(repo_path);
1908 5de5890b 2018-10-18 stsp free(cwd);
1909 5de5890b 2018-10-18 stsp free(commit_id);
1910 7a2c19d6 2019-02-05 stsp if (worktree)
1911 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1912 5de5890b 2018-10-18 stsp if (repo) {
1913 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1914 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1915 5de5890b 2018-10-18 stsp if (error == NULL)
1916 5de5890b 2018-10-18 stsp error = repo_error;
1918 5de5890b 2018-10-18 stsp return error;
1921 6bad629b 2019-02-04 stsp __dead static void
1922 6bad629b 2019-02-04 stsp usage_status(void)
1924 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1928 b72f483a 2019-02-05 stsp static const struct got_error *
1929 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1930 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1933 b72f483a 2019-02-05 stsp return NULL;
1936 6bad629b 2019-02-04 stsp static const struct got_error *
1937 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1939 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1940 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1941 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1942 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1945 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1946 6bad629b 2019-02-04 stsp switch (ch) {
1948 2deda0b9 2019-03-07 stsp usage_status();
1949 6bad629b 2019-02-04 stsp /* NOTREACHED */
1953 6bad629b 2019-02-04 stsp argc -= optind;
1954 6bad629b 2019-02-04 stsp argv += optind;
1956 6bad629b 2019-02-04 stsp #ifndef PROFILE
1957 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1958 6bad629b 2019-02-04 stsp NULL) == -1)
1959 6bad629b 2019-02-04 stsp err(1, "pledge");
1961 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1962 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1963 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1964 927df6b7 2019-02-10 stsp goto done;
1967 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1968 927df6b7 2019-02-10 stsp if (error != NULL)
1969 927df6b7 2019-02-10 stsp goto done;
1971 6bad629b 2019-02-04 stsp if (argc == 0) {
1972 927df6b7 2019-02-10 stsp path = strdup("");
1973 927df6b7 2019-02-10 stsp if (path == NULL) {
1974 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1975 6bad629b 2019-02-04 stsp goto done;
1977 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1978 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1979 927df6b7 2019-02-10 stsp if (error)
1980 6bad629b 2019-02-04 stsp goto done;
1982 6bad629b 2019-02-04 stsp usage_status();
1984 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1985 6bad629b 2019-02-04 stsp if (error != NULL)
1986 6bad629b 2019-02-04 stsp goto done;
1988 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1989 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1990 6bad629b 2019-02-04 stsp if (error)
1991 6bad629b 2019-02-04 stsp goto done;
1993 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1996 927df6b7 2019-02-10 stsp free(cwd);
1997 927df6b7 2019-02-10 stsp free(path);
1998 d0eebce4 2019-03-11 stsp return error;
2001 d0eebce4 2019-03-11 stsp __dead static void
2002 d0eebce4 2019-03-11 stsp usage_ref(void)
2004 d0eebce4 2019-03-11 stsp fprintf(stderr,
2005 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2006 d0eebce4 2019-03-11 stsp getprogname());
2010 d0eebce4 2019-03-11 stsp static const struct got_error *
2011 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2013 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2014 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2015 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2017 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2018 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2020 d0eebce4 2019-03-11 stsp return err;
2022 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2023 d0eebce4 2019-03-11 stsp char *refstr;
2024 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2025 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2026 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2027 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2028 d0eebce4 2019-03-11 stsp free(refstr);
2031 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2032 d0eebce4 2019-03-11 stsp return NULL;
2035 d0eebce4 2019-03-11 stsp static const struct got_error *
2036 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2038 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2039 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2041 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2043 d0eebce4 2019-03-11 stsp return err;
2045 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2046 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2047 d0eebce4 2019-03-11 stsp return err;
2050 d0eebce4 2019-03-11 stsp static const struct got_error *
2051 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2054 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2055 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2057 d83d9d5c 2019-05-13 stsp err = got_object_resolve_id_str(&id, repo, target);
2058 d83d9d5c 2019-05-13 stsp if (err) {
2059 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2061 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2062 d83d9d5c 2019-05-13 stsp return err;
2063 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2065 d83d9d5c 2019-05-13 stsp return err;
2066 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2067 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2069 d83d9d5c 2019-05-13 stsp return err;
2072 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2074 d0eebce4 2019-03-11 stsp goto done;
2076 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2079 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2081 d0eebce4 2019-03-11 stsp return err;
2084 d0eebce4 2019-03-11 stsp static const struct got_error *
2085 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2087 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2088 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2089 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2090 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2091 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2092 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2094 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2095 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2096 d0eebce4 2019-03-11 stsp switch (ch) {
2098 d0eebce4 2019-03-11 stsp delref = optarg;
2101 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2102 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2103 d0eebce4 2019-03-11 stsp err(1, "-r option");
2104 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2107 d0eebce4 2019-03-11 stsp do_list = 1;
2110 d0eebce4 2019-03-11 stsp usage_ref();
2111 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2115 d0eebce4 2019-03-11 stsp if (do_list && delref)
2116 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2118 d0eebce4 2019-03-11 stsp argc -= optind;
2119 d0eebce4 2019-03-11 stsp argv += optind;
2121 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2122 d0eebce4 2019-03-11 stsp if (argc > 0)
2123 d0eebce4 2019-03-11 stsp usage_ref();
2124 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2125 d0eebce4 2019-03-11 stsp usage_ref();
2127 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2128 e0b57350 2019-03-12 stsp if (do_list) {
2129 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2130 e0b57350 2019-03-12 stsp NULL) == -1)
2131 e0b57350 2019-03-12 stsp err(1, "pledge");
2133 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2134 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2135 e0b57350 2019-03-12 stsp err(1, "pledge");
2138 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2139 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2140 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2141 d0eebce4 2019-03-11 stsp goto done;
2144 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2145 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2146 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2147 d0eebce4 2019-03-11 stsp goto done;
2149 d0eebce4 2019-03-11 stsp error = NULL;
2150 d0eebce4 2019-03-11 stsp if (worktree) {
2151 d0eebce4 2019-03-11 stsp repo_path =
2152 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2153 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2154 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2155 d0eebce4 2019-03-11 stsp if (error)
2156 d0eebce4 2019-03-11 stsp goto done;
2158 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2159 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2160 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2161 d0eebce4 2019-03-11 stsp goto done;
2166 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2167 d0eebce4 2019-03-11 stsp if (error != NULL)
2168 d0eebce4 2019-03-11 stsp goto done;
2170 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2171 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2172 c02c541e 2019-03-29 stsp if (error)
2173 c02c541e 2019-03-29 stsp goto done;
2175 d0eebce4 2019-03-11 stsp if (do_list)
2176 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2177 d0eebce4 2019-03-11 stsp else if (delref)
2178 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2180 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2183 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2184 d0eebce4 2019-03-11 stsp if (worktree)
2185 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2186 d0eebce4 2019-03-11 stsp free(cwd);
2187 d0eebce4 2019-03-11 stsp free(repo_path);
2188 d00136be 2019-03-26 stsp return error;
2191 d00136be 2019-03-26 stsp __dead static void
2192 d00136be 2019-03-26 stsp usage_add(void)
2194 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2198 d00136be 2019-03-26 stsp static const struct got_error *
2199 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2201 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2202 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2203 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2204 1dd54920 2019-05-11 stsp char *cwd = NULL;
2205 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2206 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2209 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2211 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2212 d00136be 2019-03-26 stsp switch (ch) {
2214 d00136be 2019-03-26 stsp usage_add();
2215 d00136be 2019-03-26 stsp /* NOTREACHED */
2219 d00136be 2019-03-26 stsp argc -= optind;
2220 d00136be 2019-03-26 stsp argv += optind;
2222 723c305c 2019-05-11 jcs if (argc < 1)
2223 d00136be 2019-03-26 stsp usage_add();
2225 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2226 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2227 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2228 723c305c 2019-05-11 jcs if (path == NULL) {
2229 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2232 1dd54920 2019-05-11 stsp free(path);
2235 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2236 d00136be 2019-03-26 stsp if (cwd == NULL) {
2237 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2238 d00136be 2019-03-26 stsp goto done;
2241 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2242 d00136be 2019-03-26 stsp if (error)
2243 d00136be 2019-03-26 stsp goto done;
2245 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2246 031a5338 2019-03-26 stsp if (error != NULL)
2247 031a5338 2019-03-26 stsp goto done;
2249 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2250 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2251 d00136be 2019-03-26 stsp if (error)
2252 d00136be 2019-03-26 stsp goto done;
2254 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2255 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2256 723c305c 2019-05-11 jcs if (path == NULL) {
2257 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2261 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2262 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2263 1dd54920 2019-05-11 stsp if (error) {
2264 1dd54920 2019-05-11 stsp free(path);
2268 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2269 1dd54920 2019-05-11 stsp NULL, repo);
2272 031a5338 2019-03-26 stsp got_repo_close(repo);
2273 d00136be 2019-03-26 stsp if (worktree)
2274 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2275 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2276 1dd54920 2019-05-11 stsp free((char *)pe->path);
2277 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2278 2ec1f75b 2019-03-26 stsp free(cwd);
2279 2ec1f75b 2019-03-26 stsp return error;
2282 2ec1f75b 2019-03-26 stsp __dead static void
2283 2ec1f75b 2019-03-26 stsp usage_rm(void)
2285 cc11e7e3 2019-06-04 stsp fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2289 2ec1f75b 2019-03-26 stsp static const struct got_error *
2290 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2292 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2293 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2294 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2295 17ed4618 2019-06-02 stsp char *cwd = NULL;
2296 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2297 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2298 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2300 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2302 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2303 2ec1f75b 2019-03-26 stsp switch (ch) {
2305 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2308 2ec1f75b 2019-03-26 stsp usage_add();
2309 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2313 2ec1f75b 2019-03-26 stsp argc -= optind;
2314 2ec1f75b 2019-03-26 stsp argv += optind;
2316 17ed4618 2019-06-02 stsp if (argc < 1)
2317 2ec1f75b 2019-03-26 stsp usage_rm();
2319 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2320 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2321 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2322 17ed4618 2019-06-02 stsp if (path == NULL) {
2323 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2324 17ed4618 2019-06-02 stsp goto done;
2326 17ed4618 2019-06-02 stsp free(path);
2329 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2330 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2331 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2332 2ec1f75b 2019-03-26 stsp goto done;
2334 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2335 2ec1f75b 2019-03-26 stsp if (error)
2336 2ec1f75b 2019-03-26 stsp goto done;
2338 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2340 2ec1f75b 2019-03-26 stsp goto done;
2342 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2343 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2344 2ec1f75b 2019-03-26 stsp if (error)
2345 2ec1f75b 2019-03-26 stsp goto done;
2347 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2348 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2349 17ed4618 2019-06-02 stsp if (path == NULL) {
2350 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2351 17ed4618 2019-06-02 stsp goto done;
2354 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2355 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2356 17ed4618 2019-06-02 stsp if (error) {
2357 17ed4618 2019-06-02 stsp free(path);
2358 17ed4618 2019-06-02 stsp goto done;
2361 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2362 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2363 a129376b 2019-03-28 stsp if (error)
2364 a129376b 2019-03-28 stsp goto done;
2367 a129376b 2019-03-28 stsp got_repo_close(repo);
2368 a129376b 2019-03-28 stsp if (worktree)
2369 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2370 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2371 17ed4618 2019-06-02 stsp free((char *)pe->path);
2372 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2373 a129376b 2019-03-28 stsp free(cwd);
2374 a129376b 2019-03-28 stsp return error;
2377 a129376b 2019-03-28 stsp __dead static void
2378 a129376b 2019-03-28 stsp usage_revert(void)
2380 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2384 a129376b 2019-03-28 stsp static void
2385 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2387 a129376b 2019-03-28 stsp while (path[0] == '/')
2389 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2392 a129376b 2019-03-28 stsp static const struct got_error *
2393 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2395 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2396 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2397 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2398 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2399 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2400 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2401 e20a8b6f 2019-06-04 stsp int ch, i;
2403 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2405 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2406 a129376b 2019-03-28 stsp switch (ch) {
2408 a129376b 2019-03-28 stsp usage_revert();
2409 a129376b 2019-03-28 stsp /* NOTREACHED */
2413 a129376b 2019-03-28 stsp argc -= optind;
2414 a129376b 2019-03-28 stsp argv += optind;
2416 e20a8b6f 2019-06-04 stsp if (argc < 1)
2417 a129376b 2019-03-28 stsp usage_revert();
2419 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
2420 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
2421 e20a8b6f 2019-06-04 stsp if (path == NULL) {
2422 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
2423 e20a8b6f 2019-06-04 stsp goto done;
2426 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
2427 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2428 e20a8b6f 2019-06-04 stsp if (error) {
2429 e20a8b6f 2019-06-04 stsp free(path);
2430 e20a8b6f 2019-06-04 stsp goto done;
2434 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2435 a129376b 2019-03-28 stsp if (cwd == NULL) {
2436 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2437 a129376b 2019-03-28 stsp goto done;
2439 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2440 2ec1f75b 2019-03-26 stsp if (error)
2441 2ec1f75b 2019-03-26 stsp goto done;
2443 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2444 a129376b 2019-03-28 stsp if (error != NULL)
2445 a129376b 2019-03-28 stsp goto done;
2447 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2448 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2449 a129376b 2019-03-28 stsp if (error)
2450 a129376b 2019-03-28 stsp goto done;
2452 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
2453 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2454 a129376b 2019-03-28 stsp if (error)
2455 a129376b 2019-03-28 stsp goto done;
2458 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2459 2ec1f75b 2019-03-26 stsp if (worktree)
2460 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2461 2ec1f75b 2019-03-26 stsp free(path);
2462 d00136be 2019-03-26 stsp free(cwd);
2463 6bad629b 2019-02-04 stsp return error;
2466 c4296144 2019-05-09 stsp __dead static void
2467 c4296144 2019-05-09 stsp usage_commit(void)
2469 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2474 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2477 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2478 33ad4cbe 2019-05-12 jcs int st = -1;
2480 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2481 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2482 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2484 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2486 33ad4cbe 2019-05-12 jcs goto doneediting;
2488 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2489 33ad4cbe 2019-05-12 jcs _exit(127);
2492 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2493 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2496 33ad4cbe 2019-05-12 jcs doneediting:
2497 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2498 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2499 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2501 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2502 33ad4cbe 2019-05-12 jcs errno = EINTR;
2506 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2509 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2510 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2511 e2ba3d07 2019-05-13 stsp const char *editor;
2512 e0870e44 2019-05-13 stsp const char *worktree_path;
2513 76d98825 2019-06-03 stsp const char *branch_name;
2514 314a6357 2019-05-13 stsp const char *repo_path;
2515 e0870e44 2019-05-13 stsp char *logmsg_path;
2519 33ad4cbe 2019-05-12 jcs static const struct got_error *
2520 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2523 76d98825 2019-06-03 stsp char *initial_content = NULL;
2524 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2525 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2526 e0870e44 2019-05-13 stsp char *template = NULL;
2527 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2528 33ad4cbe 2019-05-12 jcs char buf[1024];
2529 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2531 33ad4cbe 2019-05-12 jcs size_t len;
2532 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2534 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2535 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2536 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2537 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2538 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2539 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2540 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2541 33ad4cbe 2019-05-12 jcs return NULL;
2544 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2545 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
2547 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
2548 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
2549 76d98825 2019-06-03 stsp a->branch_name) == -1)
2550 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2552 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2554 e0870e44 2019-05-13 stsp goto done;
2556 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2558 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2559 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2560 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
2561 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
2562 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
2566 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2567 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2571 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2572 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2576 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2577 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2581 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2582 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2583 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2584 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2585 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2586 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2590 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2591 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2592 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2593 fcde04d9 2019-05-13 stsp goto done;
2595 cc79381d 2019-05-22 stsp (*logmsg)[0] = '\0';
2598 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2599 d4592c7c 2019-05-22 stsp if (fp == NULL) {
2600 d4592c7c 2019-05-22 stsp err = got_error_from_errno("fopen");
2601 d4592c7c 2019-05-22 stsp goto done;
2603 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2604 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2605 e0870e44 2019-05-13 stsp content_changed = 1;
2606 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2607 78527a0a 2019-05-22 stsp continue; /* remove comments and leading empty lines */
2608 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2610 33ad4cbe 2019-05-12 jcs fclose(fp);
2612 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2613 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2617 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2618 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2619 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2620 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2621 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2622 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2626 76d98825 2019-06-03 stsp free(initial_content);
2627 e0870e44 2019-05-13 stsp free(template);
2629 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2630 314a6357 2019-05-13 stsp if (err == NULL)
2631 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2632 33ad4cbe 2019-05-12 jcs return err;
2635 c4296144 2019-05-09 stsp static const struct got_error *
2636 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2638 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2639 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2640 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2641 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2642 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2643 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2644 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2645 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2646 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2649 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2650 c4296144 2019-05-09 stsp switch (ch) {
2652 c4296144 2019-05-09 stsp logmsg = optarg;
2655 c4296144 2019-05-09 stsp usage_commit();
2656 c4296144 2019-05-09 stsp /* NOTREACHED */
2660 c4296144 2019-05-09 stsp argc -= optind;
2661 c4296144 2019-05-09 stsp argv += optind;
2663 c4296144 2019-05-09 stsp if (argc == 1) {
2664 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2665 c4296144 2019-05-09 stsp if (path == NULL) {
2666 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2667 c4296144 2019-05-09 stsp goto done;
2669 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2670 c4296144 2019-05-09 stsp } else if (argc != 0)
2671 c4296144 2019-05-09 stsp usage_commit();
2673 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2674 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2675 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2676 35bd8fed 2019-05-09 stsp goto done;
2679 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2680 c4296144 2019-05-09 stsp if (cwd == NULL) {
2681 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2682 c4296144 2019-05-09 stsp goto done;
2684 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2685 c4296144 2019-05-09 stsp if (error)
2686 c4296144 2019-05-09 stsp goto done;
2688 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2689 c4296144 2019-05-09 stsp if (error != NULL)
2690 c4296144 2019-05-09 stsp goto done;
2693 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2694 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2696 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2697 314a6357 2019-05-13 stsp error = get_editor(&editor);
2699 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2700 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2701 c4296144 2019-05-09 stsp if (error)
2702 c4296144 2019-05-09 stsp goto done;
2704 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2705 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2706 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2707 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2708 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2709 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
2710 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2711 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
2712 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2713 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2714 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2715 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2716 e0870e44 2019-05-13 stsp if (error) {
2717 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2718 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2719 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2720 c4296144 2019-05-09 stsp goto done;
2723 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2724 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2726 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2727 c4296144 2019-05-09 stsp if (error)
2728 c4296144 2019-05-09 stsp goto done;
2729 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
2732 c4296144 2019-05-09 stsp got_repo_close(repo);
2733 c4296144 2019-05-09 stsp if (worktree)
2734 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2735 c4296144 2019-05-09 stsp free(path);
2736 c4296144 2019-05-09 stsp free(cwd);
2737 c4296144 2019-05-09 stsp free(id_str);
2738 e2ba3d07 2019-05-13 stsp free(editor);
2739 234035bc 2019-06-01 stsp return error;
2742 234035bc 2019-06-01 stsp __dead static void
2743 234035bc 2019-06-01 stsp usage_cherrypick(void)
2745 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2749 234035bc 2019-06-01 stsp static const struct got_error *
2750 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
2752 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
2753 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
2754 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
2755 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
2756 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
2757 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
2758 234035bc 2019-06-01 stsp struct got_object_qid *pid;
2759 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
2760 234035bc 2019-06-01 stsp int ch, did_something = 0;
2762 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2763 234035bc 2019-06-01 stsp switch (ch) {
2765 234035bc 2019-06-01 stsp usage_cherrypick();
2766 234035bc 2019-06-01 stsp /* NOTREACHED */
2770 234035bc 2019-06-01 stsp argc -= optind;
2771 234035bc 2019-06-01 stsp argv += optind;
2773 234035bc 2019-06-01 stsp if (argc != 1)
2774 234035bc 2019-06-01 stsp usage_cherrypick();
2776 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
2777 234035bc 2019-06-01 stsp if (cwd == NULL) {
2778 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
2779 234035bc 2019-06-01 stsp goto done;
2781 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
2782 234035bc 2019-06-01 stsp if (error)
2783 234035bc 2019-06-01 stsp goto done;
2785 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2786 234035bc 2019-06-01 stsp if (error != NULL)
2787 234035bc 2019-06-01 stsp goto done;
2789 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2790 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
2791 234035bc 2019-06-01 stsp if (error)
2792 234035bc 2019-06-01 stsp goto done;
2794 234035bc 2019-06-01 stsp error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2795 234035bc 2019-06-01 stsp if (error != NULL) {
2796 234035bc 2019-06-01 stsp struct got_reference *ref;
2797 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2798 234035bc 2019-06-01 stsp goto done;
2799 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
2800 234035bc 2019-06-01 stsp if (error != NULL)
2801 234035bc 2019-06-01 stsp goto done;
2802 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
2803 234035bc 2019-06-01 stsp got_ref_close(ref);
2804 234035bc 2019-06-01 stsp if (error != NULL)
2805 234035bc 2019-06-01 stsp goto done;
2807 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
2808 234035bc 2019-06-01 stsp if (error)
2809 234035bc 2019-06-01 stsp goto done;
2811 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
2812 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
2813 234035bc 2019-06-01 stsp if (error != NULL)
2814 234035bc 2019-06-01 stsp goto done;
2816 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
2817 234035bc 2019-06-01 stsp if (error) {
2818 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
2819 234035bc 2019-06-01 stsp goto done;
2820 234035bc 2019-06-01 stsp error = NULL;
2822 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
2823 234035bc 2019-06-01 stsp goto done;
2826 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2827 234035bc 2019-06-01 stsp if (error)
2828 234035bc 2019-06-01 stsp goto done;
2829 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2830 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2831 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
2833 234035bc 2019-06-01 stsp if (error != NULL)
2834 234035bc 2019-06-01 stsp goto done;
2836 234035bc 2019-06-01 stsp if (did_something)
2837 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
2839 234035bc 2019-06-01 stsp if (commit)
2840 234035bc 2019-06-01 stsp got_object_commit_close(commit);
2841 234035bc 2019-06-01 stsp free(commit_id_str);
2842 234035bc 2019-06-01 stsp if (head_ref)
2843 234035bc 2019-06-01 stsp got_ref_close(head_ref);
2844 234035bc 2019-06-01 stsp if (worktree)
2845 234035bc 2019-06-01 stsp got_worktree_close(worktree);
2847 234035bc 2019-06-01 stsp got_repo_close(repo);
2848 c4296144 2019-05-09 stsp return error;
2851 5ef14e63 2019-06-02 stsp __dead static void
2852 5ef14e63 2019-06-02 stsp usage_backout(void)
2854 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2858 5ef14e63 2019-06-02 stsp static const struct got_error *
2859 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
2861 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
2862 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
2863 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
2864 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
2865 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
2866 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
2867 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
2868 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
2869 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
2871 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2872 5ef14e63 2019-06-02 stsp switch (ch) {
2874 5ef14e63 2019-06-02 stsp usage_backout();
2875 5ef14e63 2019-06-02 stsp /* NOTREACHED */
2879 5ef14e63 2019-06-02 stsp argc -= optind;
2880 5ef14e63 2019-06-02 stsp argv += optind;
2882 5ef14e63 2019-06-02 stsp if (argc != 1)
2883 5ef14e63 2019-06-02 stsp usage_backout();
2885 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
2886 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
2887 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
2888 5ef14e63 2019-06-02 stsp goto done;
2890 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
2891 5ef14e63 2019-06-02 stsp if (error)
2892 5ef14e63 2019-06-02 stsp goto done;
2894 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2895 5ef14e63 2019-06-02 stsp if (error != NULL)
2896 5ef14e63 2019-06-02 stsp goto done;
2898 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2899 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
2900 5ef14e63 2019-06-02 stsp if (error)
2901 5ef14e63 2019-06-02 stsp goto done;
2903 5ef14e63 2019-06-02 stsp error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2904 5ef14e63 2019-06-02 stsp if (error != NULL) {
2905 5ef14e63 2019-06-02 stsp struct got_reference *ref;
2906 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2907 5ef14e63 2019-06-02 stsp goto done;
2908 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
2909 5ef14e63 2019-06-02 stsp if (error != NULL)
2910 5ef14e63 2019-06-02 stsp goto done;
2911 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
2912 5ef14e63 2019-06-02 stsp got_ref_close(ref);
2913 5ef14e63 2019-06-02 stsp if (error != NULL)
2914 5ef14e63 2019-06-02 stsp goto done;
2916 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2917 5ef14e63 2019-06-02 stsp if (error)
2918 5ef14e63 2019-06-02 stsp goto done;
2920 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
2921 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
2922 5ef14e63 2019-06-02 stsp if (error != NULL)
2923 5ef14e63 2019-06-02 stsp goto done;
2925 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
2926 5ef14e63 2019-06-02 stsp if (error)
2927 5ef14e63 2019-06-02 stsp goto done;
2929 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2930 5ef14e63 2019-06-02 stsp if (error)
2931 5ef14e63 2019-06-02 stsp goto done;
2932 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2933 5ef14e63 2019-06-02 stsp if (pid == NULL) {
2934 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
2935 5ef14e63 2019-06-02 stsp goto done;
2938 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2939 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
2940 5ef14e63 2019-06-02 stsp if (error != NULL)
2941 5ef14e63 2019-06-02 stsp goto done;
2943 5ef14e63 2019-06-02 stsp if (did_something)
2944 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
2946 5ef14e63 2019-06-02 stsp if (commit)
2947 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
2948 5ef14e63 2019-06-02 stsp free(commit_id_str);
2949 5ef14e63 2019-06-02 stsp if (head_ref)
2950 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
2951 5ef14e63 2019-06-02 stsp if (worktree)
2952 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
2954 5ef14e63 2019-06-02 stsp got_repo_close(repo);
2955 5ef14e63 2019-06-02 stsp return error;