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 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 f42b1b34 2018-03-12 stsp #include "got_error.h"
39 f42b1b34 2018-03-12 stsp #include "got_object.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 f42b1b34 2018-03-12 stsp #include "got_repository.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 c09a553d 2018-03-12 stsp #include "got_worktree.h"
44 79109fed 2018-03-27 stsp #include "got_diff.h"
45 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
46 404c43c4 2018-06-21 stsp #include "got_blame.h"
47 63219cd2 2019-01-04 stsp #include "got_privsep.h"
48 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
50 5c860e29 2018-03-12 stsp #ifndef nitems
51 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
58 99437157 2018-11-11 stsp catch_sigint(int signo)
60 99437157 2018-11-11 stsp sigint_received = 1;
64 99437157 2018-11-11 stsp catch_sigpipe(int signo)
66 99437157 2018-11-11 stsp sigpipe_received = 1;
70 8cfb4057 2019-07-09 stsp struct got_cmd {
71 5c860e29 2018-03-12 stsp const char *cmd_name;
72 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
74 97b3a7be 2019-07-09 stsp const char *cmd_alias;
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
80 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
83 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
84 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
85 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
86 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
87 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
88 d00136be 2019-03-26 stsp __dead static void usage_add(void);
89 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
90 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
91 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
92 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
93 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
94 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
96 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
97 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
98 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
100 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
101 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
102 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
104 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
105 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
106 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
107 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
108 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
109 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
110 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
111 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
112 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
114 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
115 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
116 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
117 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
118 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
119 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
120 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
121 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
122 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
123 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
124 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
125 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
126 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
127 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
128 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
129 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
130 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
131 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
134 ce5b7c56 2019-07-09 stsp static void
135 ce5b7c56 2019-07-09 stsp list_commands(void)
139 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
140 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
141 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
142 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
144 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
148 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
150 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
151 5c860e29 2018-03-12 stsp unsigned int i;
153 1b6b95a8 2018-03-12 stsp int hflag = 0;
155 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
157 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
158 5c860e29 2018-03-12 stsp switch (ch) {
163 ce5b7c56 2019-07-09 stsp usage(hflag);
164 5c860e29 2018-03-12 stsp /* NOTREACHED */
168 5c860e29 2018-03-12 stsp argc -= optind;
169 5c860e29 2018-03-12 stsp argv += optind;
170 1e70621d 2018-03-27 stsp optind = 0;
172 5c860e29 2018-03-12 stsp if (argc <= 0)
173 ce5b7c56 2019-07-09 stsp usage(hflag);
175 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
176 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
178 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
179 d7d4f210 2018-03-12 stsp const struct got_error *error;
181 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
183 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
184 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
188 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
190 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
191 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
192 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
200 ce5b7c56 2019-07-09 stsp list_commands();
204 4ed7e80c 2018-05-20 stsp __dead static void
205 ce5b7c56 2019-07-09 stsp usage(int hflag)
207 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
209 ce5b7c56 2019-07-09 stsp list_commands();
213 0266afb7 2019-01-04 stsp static const struct got_error *
214 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
216 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
217 e2ba3d07 2019-05-13 stsp const char *editor;
219 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
220 e2ba3d07 2019-05-13 stsp if (editor == NULL)
221 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
223 0ee7065d 2019-05-13 stsp if (editor) {
224 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
226 0ee7065d 2019-05-13 stsp return err;
229 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
230 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
231 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
232 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
235 e2ba3d07 2019-05-13 stsp return NULL;
238 e2ba3d07 2019-05-13 stsp static const struct got_error *
239 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
240 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
242 163ce85a 2019-05-13 stsp const struct got_error *err;
244 a0937847 2019-05-11 stsp if (create_worktree) {
245 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
246 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
248 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
249 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
251 163ce85a 2019-05-13 stsp err = NULL;
253 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
254 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
258 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
259 163ce85a 2019-05-13 stsp return err;
262 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
263 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
265 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
266 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
268 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
269 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
271 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
272 163ce85a 2019-05-13 stsp if (err != NULL)
273 163ce85a 2019-05-13 stsp return err;
275 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
276 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
278 0266afb7 2019-01-04 stsp return NULL;
281 2c7829a4 2019-06-17 stsp __dead static void
282 2c7829a4 2019-06-17 stsp usage_init(void)
284 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
288 2c7829a4 2019-06-17 stsp static const struct got_error *
289 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
291 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
292 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
295 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
296 2c7829a4 2019-06-17 stsp switch (ch) {
298 2c7829a4 2019-06-17 stsp usage_init();
299 2c7829a4 2019-06-17 stsp /* NOTREACHED */
303 2c7829a4 2019-06-17 stsp argc -= optind;
304 2c7829a4 2019-06-17 stsp argv += optind;
306 2c7829a4 2019-06-17 stsp #ifndef PROFILE
307 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
308 2c7829a4 2019-06-17 stsp err(1, "pledge");
310 2c7829a4 2019-06-17 stsp if (argc != 1)
311 bc20e173 2019-06-17 stsp usage_init();
313 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
314 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
315 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
317 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
319 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
320 2c7829a4 2019-06-17 stsp if (error &&
321 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
324 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
328 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
329 2c7829a4 2019-06-17 stsp if (error != NULL)
333 2c7829a4 2019-06-17 stsp free(repo_path);
334 2c7829a4 2019-06-17 stsp return error;
337 4ed7e80c 2018-05-20 stsp __dead static void
338 c09a553d 2018-03-12 stsp usage_checkout(void)
340 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
341 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
345 92a684f4 2018-03-12 stsp static void
346 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
348 92a684f4 2018-03-12 stsp char *worktree_path = arg;
350 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
351 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
354 92a684f4 2018-03-12 stsp while (path[0] == '/')
357 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
360 99437157 2018-11-11 stsp static const struct got_error *
361 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
363 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
364 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
365 99437157 2018-11-11 stsp return NULL;
368 8069f636 2019-01-12 stsp static const struct got_error *
369 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
370 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
372 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
373 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
375 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
376 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
378 36a38700 2019-05-10 stsp return err;
380 d5bea539 2019-05-13 stsp if (yca_id == NULL)
381 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
384 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
385 d5bea539 2019-05-13 stsp * and the work tree's base commit.
387 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
389 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
397 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
398 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
399 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
401 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
402 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
403 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
405 d5bea539 2019-05-13 stsp free(yca_id);
406 d5bea539 2019-05-13 stsp return NULL;
409 a367ff0f 2019-05-14 stsp static const struct got_error *
410 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
411 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
413 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
414 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
415 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
416 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
418 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
422 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
426 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
431 a367ff0f 2019-05-14 stsp struct got_object_id *id;
432 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
434 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
435 a367ff0f 2019-05-14 stsp err = NULL;
438 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
440 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
447 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
448 a367ff0f 2019-05-14 stsp is_same_branch = 1;
455 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
456 a367ff0f 2019-05-14 stsp free(head_commit_id);
457 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
458 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
459 a367ff0f 2019-05-14 stsp return err;
462 4ed7e80c 2018-05-20 stsp static const struct got_error *
463 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
465 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
466 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
467 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
468 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
469 c09a553d 2018-03-12 stsp char *repo_path = NULL;
470 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
471 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
472 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
473 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
474 72151b04 2019-05-11 stsp int ch, same_path_prefix;
476 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
477 0bb8a95e 2018-03-12 stsp switch (ch) {
479 08573d5b 2019-05-14 stsp branch_name = optarg;
482 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
483 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
484 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
487 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
490 2deda0b9 2019-03-07 stsp usage_checkout();
491 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
495 0bb8a95e 2018-03-12 stsp argc -= optind;
496 0bb8a95e 2018-03-12 stsp argv += optind;
498 6715a751 2018-03-16 stsp #ifndef PROFILE
499 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
500 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
501 c09a553d 2018-03-12 stsp err(1, "pledge");
503 0bb8a95e 2018-03-12 stsp if (argc == 1) {
504 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
505 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
506 76089277 2018-04-01 stsp if (repo_path == NULL)
507 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
508 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
509 76089277 2018-04-01 stsp if (cwd == NULL) {
510 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
513 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
514 230a42bd 2019-05-11 jcs base = basename(path_prefix);
515 230a42bd 2019-05-11 jcs if (base == NULL) {
516 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
517 230a42bd 2019-05-11 jcs path_prefix);
521 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
522 230a42bd 2019-05-11 jcs if (base == NULL) {
523 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
528 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
529 c09a553d 2018-03-12 stsp if (dotgit)
530 c09a553d 2018-03-12 stsp *dotgit = '\0';
531 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
532 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
537 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
538 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
539 76089277 2018-04-01 stsp if (repo_path == NULL) {
540 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
543 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
544 76089277 2018-04-01 stsp if (worktree_path == NULL) {
545 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
549 c09a553d 2018-03-12 stsp usage_checkout();
551 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
552 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
554 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
555 c09a553d 2018-03-12 stsp if (error != NULL)
558 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
562 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
563 c09a553d 2018-03-12 stsp if (error != NULL)
566 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
567 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
570 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
571 c09a553d 2018-03-12 stsp if (error != NULL)
574 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
575 e5dc7198 2018-12-29 stsp path_prefix);
576 e5dc7198 2018-12-29 stsp if (error != NULL)
578 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
579 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
583 8069f636 2019-01-12 stsp if (commit_id_str) {
584 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
585 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
586 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
587 8069f636 2019-01-12 stsp if (error != NULL)
589 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
590 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
591 8069f636 2019-01-12 stsp if (error != NULL) {
592 8069f636 2019-01-12 stsp free(commit_id);
595 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
598 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
599 8069f636 2019-01-12 stsp commit_id);
600 8069f636 2019-01-12 stsp free(commit_id);
605 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
606 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
607 c09a553d 2018-03-12 stsp if (error != NULL)
610 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
613 8069f636 2019-01-12 stsp free(commit_id_str);
614 76089277 2018-04-01 stsp free(repo_path);
615 507dc3bb 2018-12-29 stsp free(worktree_path);
616 507dc3bb 2018-12-29 stsp return error;
619 507dc3bb 2018-12-29 stsp __dead static void
620 507dc3bb 2018-12-29 stsp usage_update(void)
622 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
623 507dc3bb 2018-12-29 stsp getprogname());
627 507dc3bb 2018-12-29 stsp static void
628 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
630 784955db 2019-01-12 stsp int *did_something = arg;
632 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
635 1545c615 2019-02-10 stsp *did_something = 1;
637 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
638 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
641 507dc3bb 2018-12-29 stsp while (path[0] == '/')
643 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
646 be7061eb 2018-12-30 stsp static const struct got_error *
647 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
648 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
649 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
651 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
652 a1fb16d8 2019-05-24 stsp char *base_id_str;
653 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
655 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
656 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
657 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
658 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
659 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
660 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
661 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
664 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
665 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
667 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
668 a1fb16d8 2019-05-24 stsp return err;
669 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
671 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
672 a1fb16d8 2019-05-24 stsp return NULL;
674 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
675 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
676 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
678 a1fb16d8 2019-05-24 stsp return err;
679 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
680 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
681 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
682 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
683 a1fb16d8 2019-05-24 stsp return NULL;
686 a1fb16d8 2019-05-24 stsp static const struct got_error *
687 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
689 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
690 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
691 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
692 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
693 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
694 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
695 024e9686 2019-05-14 stsp const char *branch_name = NULL;
696 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
697 7d5807f4 2019-07-11 stsp int ch, did_something = 0, rebase_in_progress;
699 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
700 507dc3bb 2018-12-29 stsp switch (ch) {
702 024e9686 2019-05-14 stsp branch_name = optarg;
705 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
706 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
707 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
710 2deda0b9 2019-03-07 stsp usage_update();
711 507dc3bb 2018-12-29 stsp /* NOTREACHED */
715 507dc3bb 2018-12-29 stsp argc -= optind;
716 507dc3bb 2018-12-29 stsp argv += optind;
718 507dc3bb 2018-12-29 stsp #ifndef PROFILE
719 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
720 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
721 507dc3bb 2018-12-29 stsp err(1, "pledge");
723 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
724 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
725 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
728 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
732 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
735 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
736 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
740 507dc3bb 2018-12-29 stsp if (argc == 0) {
741 c4cdcb68 2019-04-03 stsp path = strdup("");
742 c4cdcb68 2019-04-03 stsp if (path == NULL) {
743 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
746 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
747 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
751 507dc3bb 2018-12-29 stsp usage_update();
753 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
754 507dc3bb 2018-12-29 stsp if (error != NULL)
757 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
758 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
762 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
763 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
764 024e9686 2019-05-14 stsp if (error != NULL)
766 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
767 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
768 9c4b8182 2019-01-02 stsp if (error != NULL)
770 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
771 507dc3bb 2018-12-29 stsp if (error != NULL)
774 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
775 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
776 507dc3bb 2018-12-29 stsp if (error != NULL)
780 a1fb16d8 2019-05-24 stsp if (branch_name) {
781 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
782 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
783 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
784 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
785 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
786 024e9686 2019-05-14 stsp getprogname(), path);
787 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
790 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
793 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
794 a367ff0f 2019-05-14 stsp free(head_commit_id);
795 024e9686 2019-05-14 stsp if (error != NULL)
797 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
800 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
804 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
805 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
806 a367ff0f 2019-05-14 stsp if (error != NULL) {
807 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
808 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
811 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
816 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
817 507dc3bb 2018-12-29 stsp commit_id) != 0) {
818 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
819 507dc3bb 2018-12-29 stsp commit_id);
824 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
825 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
826 507dc3bb 2018-12-29 stsp if (error != NULL)
829 784955db 2019-01-12 stsp if (did_something)
830 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
832 784955db 2019-01-12 stsp printf("Already up-to-date\n");
834 c09a553d 2018-03-12 stsp free(worktree_path);
835 c4cdcb68 2019-04-03 stsp free(path);
836 507dc3bb 2018-12-29 stsp free(commit_id);
837 9c4b8182 2019-01-02 stsp free(commit_id_str);
838 c09a553d 2018-03-12 stsp return error;
841 f42b1b34 2018-03-12 stsp static const struct got_error *
842 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
843 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
845 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
846 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
847 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
848 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
849 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
851 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
852 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
854 79109fed 2018-03-27 stsp return err;
856 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
857 79f35eb3 2018-06-11 stsp if (qid != NULL) {
858 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
860 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
862 79109fed 2018-03-27 stsp return err;
864 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
865 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
866 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
868 0f2b3dca 2018-12-22 stsp return err;
870 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
872 79109fed 2018-03-27 stsp return err;
875 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
879 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
880 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
881 aaa13589 2019-06-01 stsp arg.outfile = stdout;
882 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
883 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
886 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
887 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
888 0f2b3dca 2018-12-22 stsp free(id_str1);
889 0f2b3dca 2018-12-22 stsp free(id_str2);
890 79109fed 2018-03-27 stsp return err;
893 4bb494d5 2018-06-16 stsp static char *
894 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
896 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
897 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
903 79109fed 2018-03-27 stsp static const struct got_error *
904 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
905 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
906 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
908 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
909 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
910 6c281f94 2018-06-11 stsp char datebuf[26];
911 45d799e2 2018-12-23 stsp time_t committer_time;
912 45d799e2 2018-12-23 stsp const char *author, *committer;
913 199a4027 2019-02-02 stsp char *refs_str = NULL;
914 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
916 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
918 199a4027 2019-02-02 stsp const char *name;
919 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
921 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
922 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
924 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
926 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
928 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
930 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
932 199a4027 2019-02-02 stsp s = refs_str;
933 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
934 199a4027 2019-02-02 stsp name) == -1) {
935 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
941 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
943 8bf5b3c9 2018-03-17 stsp return err;
945 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
946 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
947 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
948 832c249c 2018-06-10 stsp free(id_str);
949 d3d493d7 2019-02-21 stsp id_str = NULL;
950 d3d493d7 2019-02-21 stsp free(refs_str);
951 d3d493d7 2019-02-21 stsp refs_str = NULL;
952 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
953 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
954 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
955 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
956 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
957 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
958 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
959 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
960 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
961 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
962 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
964 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
965 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
966 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
968 a0603db2 2018-06-10 stsp return err;
969 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
970 a0603db2 2018-06-10 stsp free(id_str);
974 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
975 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
976 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
978 621015ac 2018-07-23 stsp logmsg = logmsg0;
980 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
982 832c249c 2018-06-10 stsp printf(" %s\n", line);
983 832c249c 2018-06-10 stsp } while (line);
984 621015ac 2018-07-23 stsp free(logmsg0);
986 971751ac 2018-03-27 stsp if (show_patch) {
987 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
988 971751ac 2018-03-27 stsp if (err == 0)
989 971751ac 2018-03-27 stsp printf("\n");
992 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
993 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
994 07862c20 2018-09-15 stsp return err;
997 f42b1b34 2018-03-12 stsp static const struct got_error *
998 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
999 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1000 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1002 f42b1b34 2018-03-12 stsp const struct got_error *err;
1003 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1005 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1006 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1008 f42b1b34 2018-03-12 stsp return err;
1009 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1011 fcc85cad 2018-07-22 stsp goto done;
1013 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1014 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1016 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1019 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1020 372ccdbb 2018-06-10 stsp if (err) {
1021 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1022 9ba79e04 2018-06-11 stsp err = NULL;
1025 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1027 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1033 b43fbaa0 2018-06-11 stsp if (id == NULL)
1036 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1039 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1041 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1042 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1046 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1047 f42b1b34 2018-03-12 stsp return err;
1050 4ed7e80c 2018-05-20 stsp __dead static void
1051 6f3d1eb0 2018-03-12 stsp usage_log(void)
1053 499d7ecc 2019-05-22 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1054 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1058 4ed7e80c 2018-05-20 stsp static const struct got_error *
1059 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1061 f42b1b34 2018-03-12 stsp const struct got_error *error;
1062 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1063 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1064 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1065 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1066 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1067 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1068 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1069 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1070 64a96a6d 2018-04-01 stsp const char *errstr;
1071 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1073 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1075 6715a751 2018-03-16 stsp #ifndef PROFILE
1076 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1079 f42b1b34 2018-03-12 stsp err(1, "pledge");
1082 499d7ecc 2019-05-22 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1083 79109fed 2018-03-27 stsp switch (ch) {
1085 499d7ecc 2019-05-22 stsp first_parent_traversal = 1;
1088 79109fed 2018-03-27 stsp show_patch = 1;
1091 d142fc45 2018-04-01 stsp start_commit = optarg;
1094 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1096 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1097 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1100 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1101 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1102 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1105 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1106 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1107 04ca23f4 2018-07-16 stsp err(1, "-r option");
1108 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1111 2deda0b9 2019-03-07 stsp usage_log();
1112 79109fed 2018-03-27 stsp /* NOTREACHED */
1116 79109fed 2018-03-27 stsp argc -= optind;
1117 79109fed 2018-03-27 stsp argv += optind;
1119 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1120 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1121 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1122 04ca23f4 2018-07-16 stsp goto done;
1125 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1126 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1127 cffc0aa4 2019-02-05 stsp goto done;
1128 cffc0aa4 2019-02-05 stsp error = NULL;
1130 e7301579 2019-03-18 stsp if (argc == 0) {
1131 cbd1af7a 2019-03-18 stsp path = strdup("");
1132 e7301579 2019-03-18 stsp if (path == NULL) {
1133 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1134 cbd1af7a 2019-03-18 stsp goto done;
1136 e7301579 2019-03-18 stsp } else if (argc == 1) {
1137 e7301579 2019-03-18 stsp if (worktree) {
1138 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1140 e7301579 2019-03-18 stsp if (error)
1141 e7301579 2019-03-18 stsp goto done;
1143 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1144 e7301579 2019-03-18 stsp if (path == NULL) {
1145 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1146 e7301579 2019-03-18 stsp goto done;
1150 cbd1af7a 2019-03-18 stsp usage_log();
1152 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1153 5486daa2 2019-05-11 stsp repo_path = worktree ?
1154 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1156 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1157 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1158 cffc0aa4 2019-02-05 stsp goto done;
1161 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1162 d7d4f210 2018-03-12 stsp if (error != NULL)
1163 04ca23f4 2018-07-16 stsp goto done;
1165 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1166 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1167 c02c541e 2019-03-29 stsp if (error)
1168 c02c541e 2019-03-29 stsp goto done;
1170 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1171 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1172 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1173 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1174 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1175 3235492e 2018-04-01 stsp if (error != NULL)
1176 3235492e 2018-04-01 stsp return error;
1177 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1178 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1179 3235492e 2018-04-01 stsp if (error != NULL)
1180 3235492e 2018-04-01 stsp return error;
1181 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1183 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1184 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1185 3235492e 2018-04-01 stsp if (error == NULL) {
1186 1caad61b 2019-02-01 stsp int obj_type;
1187 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1188 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1189 d1f2edc9 2018-06-13 stsp if (error != NULL)
1190 1caad61b 2019-02-01 stsp goto done;
1191 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1192 1caad61b 2019-02-01 stsp if (error != NULL)
1193 1caad61b 2019-02-01 stsp goto done;
1194 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1195 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1196 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1197 1caad61b 2019-02-01 stsp if (error != NULL)
1198 1caad61b 2019-02-01 stsp goto done;
1199 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1200 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1201 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1202 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1203 1caad61b 2019-02-01 stsp goto done;
1206 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1207 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1208 1caad61b 2019-02-01 stsp if (id == NULL)
1209 638f9024 2019-05-13 stsp error = got_error_from_errno(
1210 230a42bd 2019-05-11 jcs "got_object_id_dup");
1211 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1212 1caad61b 2019-02-01 stsp if (error)
1213 1caad61b 2019-02-01 stsp goto done;
1214 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1215 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1216 1caad61b 2019-02-01 stsp goto done;
1218 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1219 d1f2edc9 2018-06-13 stsp if (error != NULL)
1220 1caad61b 2019-02-01 stsp goto done;
1222 15a94983 2018-12-23 stsp if (commit == NULL) {
1223 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1224 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1225 d1f2edc9 2018-06-13 stsp if (error != NULL)
1226 d1f2edc9 2018-06-13 stsp return error;
1229 d7d4f210 2018-03-12 stsp if (error != NULL)
1230 04ca23f4 2018-07-16 stsp goto done;
1232 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1233 04ca23f4 2018-07-16 stsp if (error != NULL)
1234 04ca23f4 2018-07-16 stsp goto done;
1235 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1236 04ca23f4 2018-07-16 stsp free(path);
1237 04ca23f4 2018-07-16 stsp path = in_repo_path;
1240 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1241 199a4027 2019-02-02 stsp if (error)
1242 199a4027 2019-02-02 stsp goto done;
1244 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1245 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1247 04ca23f4 2018-07-16 stsp free(path);
1248 04ca23f4 2018-07-16 stsp free(repo_path);
1249 04ca23f4 2018-07-16 stsp free(cwd);
1251 cffc0aa4 2019-02-05 stsp if (worktree)
1252 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1253 ad242220 2018-09-08 stsp if (repo) {
1254 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1255 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1256 ad242220 2018-09-08 stsp if (error == NULL)
1257 ad242220 2018-09-08 stsp error = repo_error;
1259 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1260 8bf5b3c9 2018-03-17 stsp return error;
1263 4ed7e80c 2018-05-20 stsp __dead static void
1264 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1266 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1267 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1271 b72f483a 2019-02-05 stsp struct print_diff_arg {
1272 b72f483a 2019-02-05 stsp struct got_repository *repo;
1273 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1274 b72f483a 2019-02-05 stsp int diff_context;
1275 3fc0c068 2019-02-10 stsp const char *id_str;
1276 3fc0c068 2019-02-10 stsp int header_shown;
1279 4ed7e80c 2018-05-20 stsp static const struct got_error *
1280 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1281 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1283 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1284 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1285 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1286 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1287 b72f483a 2019-02-05 stsp char *abspath = NULL;
1288 b72f483a 2019-02-05 stsp struct stat sb;
1290 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1291 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1292 b72f483a 2019-02-05 stsp return NULL;
1294 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1295 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1296 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1297 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1300 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1301 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1303 d00136be 2019-03-26 stsp goto done;
1307 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1308 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1309 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1311 2ec1f75b 2019-03-26 stsp goto done;
1314 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1315 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1316 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1317 2ec1f75b 2019-03-26 stsp goto done;
1319 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1320 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1321 2ec1f75b 2019-03-26 stsp goto done;
1324 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1326 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1329 b72f483a 2019-02-05 stsp if (blob1)
1330 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1331 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1332 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1333 b72f483a 2019-02-05 stsp free(abspath);
1334 927df6b7 2019-02-10 stsp return err;
1337 927df6b7 2019-02-10 stsp static const struct got_error *
1338 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1340 b00d56cd 2018-04-01 stsp const struct got_error *error;
1341 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1342 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1343 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1344 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1345 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1346 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1347 15a94983 2018-12-23 stsp int type1, type2;
1348 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1349 c0cc5c62 2018-10-18 stsp const char *errstr;
1350 927df6b7 2019-02-10 stsp char *path = NULL;
1352 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1353 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1354 25eccc22 2019-01-04 stsp NULL) == -1)
1355 b00d56cd 2018-04-01 stsp err(1, "pledge");
1358 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1359 b00d56cd 2018-04-01 stsp switch (ch) {
1361 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1362 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1363 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1366 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1367 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1368 b72f483a 2019-02-05 stsp err(1, "-r option");
1369 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1372 2deda0b9 2019-03-07 stsp usage_diff();
1373 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1377 b00d56cd 2018-04-01 stsp argc -= optind;
1378 b00d56cd 2018-04-01 stsp argv += optind;
1380 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1381 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1382 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1383 b72f483a 2019-02-05 stsp goto done;
1385 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1386 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1387 927df6b7 2019-02-10 stsp goto done;
1388 927df6b7 2019-02-10 stsp if (argc <= 1) {
1389 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1390 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1391 927df6b7 2019-02-10 stsp goto done;
1393 b72f483a 2019-02-05 stsp if (repo_path)
1395 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1396 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1397 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1398 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1399 927df6b7 2019-02-10 stsp goto done;
1401 927df6b7 2019-02-10 stsp if (argc == 1) {
1402 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1404 927df6b7 2019-02-10 stsp if (error)
1405 927df6b7 2019-02-10 stsp goto done;
1407 927df6b7 2019-02-10 stsp path = strdup("");
1408 927df6b7 2019-02-10 stsp if (path == NULL) {
1409 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1410 927df6b7 2019-02-10 stsp goto done;
1413 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1414 15a94983 2018-12-23 stsp id_str1 = argv[0];
1415 15a94983 2018-12-23 stsp id_str2 = argv[1];
1416 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1417 30db809c 2019-06-05 stsp repo_path =
1418 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1419 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1420 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1421 30db809c 2019-06-05 stsp goto done;
1425 b00d56cd 2018-04-01 stsp usage_diff();
1427 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1428 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1429 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1430 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1433 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1434 76089277 2018-04-01 stsp free(repo_path);
1435 b00d56cd 2018-04-01 stsp if (error != NULL)
1436 b00d56cd 2018-04-01 stsp goto done;
1438 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1439 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1440 c02c541e 2019-03-29 stsp if (error)
1441 c02c541e 2019-03-29 stsp goto done;
1443 30db809c 2019-06-05 stsp if (argc <= 1) {
1444 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1445 b72f483a 2019-02-05 stsp char *id_str;
1446 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1447 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1448 b72f483a 2019-02-05 stsp if (error)
1449 b72f483a 2019-02-05 stsp goto done;
1450 b72f483a 2019-02-05 stsp arg.repo = repo;
1451 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1452 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1453 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1454 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1456 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1457 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1458 b72f483a 2019-02-05 stsp free(id_str);
1459 b72f483a 2019-02-05 stsp goto done;
1462 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1463 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1464 e02e74af 2019-05-28 stsp if (error) {
1465 e02e74af 2019-05-28 stsp struct got_reference *ref;
1466 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1467 e02e74af 2019-05-28 stsp goto done;
1468 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1469 e02e74af 2019-05-28 stsp if (error != NULL)
1470 e02e74af 2019-05-28 stsp goto done;
1471 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1472 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1473 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1474 5e70831e 2019-05-28 stsp goto done;
1476 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1477 e02e74af 2019-05-28 stsp got_ref_close(ref);
1478 e02e74af 2019-05-28 stsp if (error != NULL)
1479 5e70831e 2019-05-28 stsp goto done;
1481 5e70831e 2019-05-28 stsp label1 = strdup(id_str1);
1482 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1483 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1484 e02e74af 2019-05-28 stsp goto done;
1488 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1489 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1490 e02e74af 2019-05-28 stsp if (error) {
1491 e02e74af 2019-05-28 stsp struct got_reference *ref;
1492 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1493 e02e74af 2019-05-28 stsp goto done;
1494 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1495 e02e74af 2019-05-28 stsp if (error != NULL)
1496 5e70831e 2019-05-28 stsp goto done;
1497 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1498 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1499 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1500 e02e74af 2019-05-28 stsp goto done;
1502 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1503 e02e74af 2019-05-28 stsp got_ref_close(ref);
1504 e02e74af 2019-05-28 stsp if (error != NULL)
1505 e02e74af 2019-05-28 stsp goto done;
1507 5e70831e 2019-05-28 stsp label2 = strdup(id_str2);
1508 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1509 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1510 5e70831e 2019-05-28 stsp goto done;
1514 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1515 15a94983 2018-12-23 stsp if (error)
1516 15a94983 2018-12-23 stsp goto done;
1518 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1519 15a94983 2018-12-23 stsp if (error)
1520 15a94983 2018-12-23 stsp goto done;
1522 15a94983 2018-12-23 stsp if (type1 != type2) {
1523 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1524 b00d56cd 2018-04-01 stsp goto done;
1527 15a94983 2018-12-23 stsp switch (type1) {
1528 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1529 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1530 54156555 2018-12-24 stsp diff_context, repo, stdout);
1532 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1533 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1534 54156555 2018-12-24 stsp diff_context, repo, stdout);
1536 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1537 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1538 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1539 c0cc5c62 2018-10-18 stsp repo, stdout);
1542 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1546 5e70831e 2019-05-28 stsp free(label1);
1547 5e70831e 2019-05-28 stsp free(label2);
1548 15a94983 2018-12-23 stsp free(id1);
1549 15a94983 2018-12-23 stsp free(id2);
1550 927df6b7 2019-02-10 stsp free(path);
1551 b72f483a 2019-02-05 stsp if (worktree)
1552 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1553 ad242220 2018-09-08 stsp if (repo) {
1554 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1555 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1556 ad242220 2018-09-08 stsp if (error == NULL)
1557 ad242220 2018-09-08 stsp error = repo_error;
1559 b00d56cd 2018-04-01 stsp return error;
1562 404c43c4 2018-06-21 stsp __dead static void
1563 404c43c4 2018-06-21 stsp usage_blame(void)
1565 9270e621 2019-02-05 stsp fprintf(stderr,
1566 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1567 404c43c4 2018-06-21 stsp getprogname());
1571 404c43c4 2018-06-21 stsp static const struct got_error *
1572 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1574 404c43c4 2018-06-21 stsp const struct got_error *error;
1575 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1576 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1577 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1578 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1579 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1582 404c43c4 2018-06-21 stsp #ifndef PROFILE
1583 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1584 36e2fb66 2019-01-04 stsp NULL) == -1)
1585 404c43c4 2018-06-21 stsp err(1, "pledge");
1588 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1589 404c43c4 2018-06-21 stsp switch (ch) {
1591 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1594 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1595 66bea077 2018-08-02 stsp if (repo_path == NULL)
1596 66bea077 2018-08-02 stsp err(1, "-r option");
1597 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1600 2deda0b9 2019-03-07 stsp usage_blame();
1601 404c43c4 2018-06-21 stsp /* NOTREACHED */
1605 404c43c4 2018-06-21 stsp argc -= optind;
1606 404c43c4 2018-06-21 stsp argv += optind;
1608 a39318fd 2018-08-02 stsp if (argc == 1)
1609 404c43c4 2018-06-21 stsp path = argv[0];
1611 404c43c4 2018-06-21 stsp usage_blame();
1613 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1614 66bea077 2018-08-02 stsp if (cwd == NULL) {
1615 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1616 66bea077 2018-08-02 stsp goto done;
1618 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1619 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1620 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1621 66bea077 2018-08-02 stsp goto done;
1623 0c06baac 2019-02-05 stsp error = NULL;
1624 0c06baac 2019-02-05 stsp if (worktree) {
1625 0c06baac 2019-02-05 stsp repo_path =
1626 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1627 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1628 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1629 0c06baac 2019-02-05 stsp if (error)
1630 0c06baac 2019-02-05 stsp goto done;
1632 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1633 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1634 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1635 0c06baac 2019-02-05 stsp goto done;
1640 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1641 c02c541e 2019-03-29 stsp if (error != NULL)
1642 36e2fb66 2019-01-04 stsp goto done;
1644 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1645 c02c541e 2019-03-29 stsp if (error)
1646 404c43c4 2018-06-21 stsp goto done;
1648 0c06baac 2019-02-05 stsp if (worktree) {
1649 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1650 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1651 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1652 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1653 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1654 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1655 6efaaa2d 2019-02-05 stsp path) == -1) {
1656 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1657 0c06baac 2019-02-05 stsp goto done;
1659 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1662 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1664 0c06baac 2019-02-05 stsp if (error)
1665 66bea077 2018-08-02 stsp goto done;
1667 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1668 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1669 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1670 404c43c4 2018-06-21 stsp if (error != NULL)
1671 66bea077 2018-08-02 stsp goto done;
1672 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1673 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1674 404c43c4 2018-06-21 stsp if (error != NULL)
1675 66bea077 2018-08-02 stsp goto done;
1677 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1678 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1679 404c43c4 2018-06-21 stsp if (error != NULL)
1680 66bea077 2018-08-02 stsp goto done;
1683 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1685 66bea077 2018-08-02 stsp free(in_repo_path);
1686 66bea077 2018-08-02 stsp free(repo_path);
1687 66bea077 2018-08-02 stsp free(cwd);
1688 404c43c4 2018-06-21 stsp free(commit_id);
1689 0c06baac 2019-02-05 stsp if (worktree)
1690 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1691 ad242220 2018-09-08 stsp if (repo) {
1692 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1693 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1694 ad242220 2018-09-08 stsp if (error == NULL)
1695 ad242220 2018-09-08 stsp error = repo_error;
1697 404c43c4 2018-06-21 stsp return error;
1700 5de5890b 2018-10-18 stsp __dead static void
1701 5de5890b 2018-10-18 stsp usage_tree(void)
1703 c1669e2e 2019-01-09 stsp fprintf(stderr,
1704 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1705 5de5890b 2018-10-18 stsp getprogname());
1709 c1669e2e 2019-01-09 stsp static void
1710 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1711 c1669e2e 2019-01-09 stsp const char *root_path)
1713 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1715 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1716 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1719 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1720 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1721 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1724 5de5890b 2018-10-18 stsp static const struct got_error *
1725 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1726 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1727 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1729 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1730 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1731 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1732 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1733 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1735 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1737 5de5890b 2018-10-18 stsp goto done;
1739 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1741 5de5890b 2018-10-18 stsp goto done;
1742 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1743 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1744 5de5890b 2018-10-18 stsp while (te) {
1745 5de5890b 2018-10-18 stsp char *id = NULL;
1747 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1750 5de5890b 2018-10-18 stsp if (show_ids) {
1751 5de5890b 2018-10-18 stsp char *id_str;
1752 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1754 5de5890b 2018-10-18 stsp goto done;
1755 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1757 5de5890b 2018-10-18 stsp free(id_str);
1758 5de5890b 2018-10-18 stsp goto done;
1760 5de5890b 2018-10-18 stsp free(id_str);
1762 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1765 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1766 c1669e2e 2019-01-09 stsp char *child_path;
1767 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1768 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1769 c1669e2e 2019-01-09 stsp te->name) == -1) {
1770 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1771 c1669e2e 2019-01-09 stsp goto done;
1773 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1774 c1669e2e 2019-01-09 stsp root_path, repo);
1775 c1669e2e 2019-01-09 stsp free(child_path);
1777 c1669e2e 2019-01-09 stsp goto done;
1780 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1784 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1785 5de5890b 2018-10-18 stsp free(tree_id);
1786 5de5890b 2018-10-18 stsp return err;
1789 5de5890b 2018-10-18 stsp static const struct got_error *
1790 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1792 5de5890b 2018-10-18 stsp const struct got_error *error;
1793 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1794 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1795 7a2c19d6 2019-02-05 stsp const char *path;
1796 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1797 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1798 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1799 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1802 5de5890b 2018-10-18 stsp #ifndef PROFILE
1803 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1804 0f8d269b 2019-01-04 stsp NULL) == -1)
1805 5de5890b 2018-10-18 stsp err(1, "pledge");
1808 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1809 5de5890b 2018-10-18 stsp switch (ch) {
1811 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1814 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1815 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1816 5de5890b 2018-10-18 stsp err(1, "-r option");
1817 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1820 5de5890b 2018-10-18 stsp show_ids = 1;
1823 c1669e2e 2019-01-09 stsp recurse = 1;
1826 2deda0b9 2019-03-07 stsp usage_tree();
1827 5de5890b 2018-10-18 stsp /* NOTREACHED */
1831 5de5890b 2018-10-18 stsp argc -= optind;
1832 5de5890b 2018-10-18 stsp argv += optind;
1834 5de5890b 2018-10-18 stsp if (argc == 1)
1835 5de5890b 2018-10-18 stsp path = argv[0];
1836 5de5890b 2018-10-18 stsp else if (argc > 1)
1837 5de5890b 2018-10-18 stsp usage_tree();
1839 7a2c19d6 2019-02-05 stsp path = NULL;
1841 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1842 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1843 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1844 9bf7a39b 2019-02-05 stsp goto done;
1846 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1847 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1848 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 7a2c19d6 2019-02-05 stsp goto done;
1851 7a2c19d6 2019-02-05 stsp error = NULL;
1852 7a2c19d6 2019-02-05 stsp if (worktree) {
1853 7a2c19d6 2019-02-05 stsp repo_path =
1854 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1855 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1856 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1857 7a2c19d6 2019-02-05 stsp if (error)
1858 7a2c19d6 2019-02-05 stsp goto done;
1860 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1861 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1862 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1863 7a2c19d6 2019-02-05 stsp goto done;
1868 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1869 5de5890b 2018-10-18 stsp if (error != NULL)
1870 c02c541e 2019-03-29 stsp goto done;
1872 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1873 c02c541e 2019-03-29 stsp if (error)
1874 5de5890b 2018-10-18 stsp goto done;
1876 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1877 9bf7a39b 2019-02-05 stsp if (worktree) {
1878 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1879 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1880 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1881 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1882 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1883 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1884 9bf7a39b 2019-02-05 stsp goto done;
1886 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1888 9bf7a39b 2019-02-05 stsp if (error)
1889 9bf7a39b 2019-02-05 stsp goto done;
1891 9bf7a39b 2019-02-05 stsp path = "/";
1893 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1894 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1895 9bf7a39b 2019-02-05 stsp if (error != NULL)
1896 9bf7a39b 2019-02-05 stsp goto done;
1899 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1900 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1901 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1902 5de5890b 2018-10-18 stsp if (error != NULL)
1903 5de5890b 2018-10-18 stsp goto done;
1904 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1905 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1906 5de5890b 2018-10-18 stsp if (error != NULL)
1907 5de5890b 2018-10-18 stsp goto done;
1909 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1910 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1911 5de5890b 2018-10-18 stsp if (error != NULL)
1912 5de5890b 2018-10-18 stsp goto done;
1915 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1916 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1918 5de5890b 2018-10-18 stsp free(in_repo_path);
1919 5de5890b 2018-10-18 stsp free(repo_path);
1920 5de5890b 2018-10-18 stsp free(cwd);
1921 5de5890b 2018-10-18 stsp free(commit_id);
1922 7a2c19d6 2019-02-05 stsp if (worktree)
1923 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1924 5de5890b 2018-10-18 stsp if (repo) {
1925 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1926 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1927 5de5890b 2018-10-18 stsp if (error == NULL)
1928 5de5890b 2018-10-18 stsp error = repo_error;
1930 5de5890b 2018-10-18 stsp return error;
1933 6bad629b 2019-02-04 stsp __dead static void
1934 6bad629b 2019-02-04 stsp usage_status(void)
1936 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1940 b72f483a 2019-02-05 stsp static const struct got_error *
1941 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1942 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1944 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1945 b72f483a 2019-02-05 stsp return NULL;
1948 6bad629b 2019-02-04 stsp static const struct got_error *
1949 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1951 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1952 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1953 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1954 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1957 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1958 6bad629b 2019-02-04 stsp switch (ch) {
1960 2deda0b9 2019-03-07 stsp usage_status();
1961 6bad629b 2019-02-04 stsp /* NOTREACHED */
1965 6bad629b 2019-02-04 stsp argc -= optind;
1966 6bad629b 2019-02-04 stsp argv += optind;
1968 6bad629b 2019-02-04 stsp #ifndef PROFILE
1969 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1970 6bad629b 2019-02-04 stsp NULL) == -1)
1971 6bad629b 2019-02-04 stsp err(1, "pledge");
1973 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1974 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1975 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1976 927df6b7 2019-02-10 stsp goto done;
1979 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1980 927df6b7 2019-02-10 stsp if (error != NULL)
1981 927df6b7 2019-02-10 stsp goto done;
1983 6bad629b 2019-02-04 stsp if (argc == 0) {
1984 927df6b7 2019-02-10 stsp path = strdup("");
1985 927df6b7 2019-02-10 stsp if (path == NULL) {
1986 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1987 6bad629b 2019-02-04 stsp goto done;
1989 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1990 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1991 927df6b7 2019-02-10 stsp if (error)
1992 6bad629b 2019-02-04 stsp goto done;
1994 6bad629b 2019-02-04 stsp usage_status();
1996 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1997 6bad629b 2019-02-04 stsp if (error != NULL)
1998 6bad629b 2019-02-04 stsp goto done;
2000 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2001 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2002 6bad629b 2019-02-04 stsp if (error)
2003 6bad629b 2019-02-04 stsp goto done;
2005 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
2006 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2008 927df6b7 2019-02-10 stsp free(cwd);
2009 927df6b7 2019-02-10 stsp free(path);
2010 d0eebce4 2019-03-11 stsp return error;
2013 d0eebce4 2019-03-11 stsp __dead static void
2014 d0eebce4 2019-03-11 stsp usage_ref(void)
2016 d0eebce4 2019-03-11 stsp fprintf(stderr,
2017 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2018 d0eebce4 2019-03-11 stsp getprogname());
2022 d0eebce4 2019-03-11 stsp static const struct got_error *
2023 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2025 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2026 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2027 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2029 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2030 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2032 d0eebce4 2019-03-11 stsp return err;
2034 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2035 d0eebce4 2019-03-11 stsp char *refstr;
2036 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2037 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2038 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2039 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2040 d0eebce4 2019-03-11 stsp free(refstr);
2043 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2044 d0eebce4 2019-03-11 stsp return NULL;
2047 d0eebce4 2019-03-11 stsp static const struct got_error *
2048 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2050 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2051 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2053 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2055 d0eebce4 2019-03-11 stsp return err;
2057 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2058 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2059 d0eebce4 2019-03-11 stsp return err;
2062 d0eebce4 2019-03-11 stsp static const struct got_error *
2063 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2065 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2066 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2067 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2069 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2071 d83d9d5c 2019-05-13 stsp if (err) {
2072 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2074 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2075 d83d9d5c 2019-05-13 stsp return err;
2076 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2078 d83d9d5c 2019-05-13 stsp return err;
2079 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2080 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2082 d83d9d5c 2019-05-13 stsp return err;
2085 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2087 d0eebce4 2019-03-11 stsp goto done;
2089 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2092 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2094 d0eebce4 2019-03-11 stsp return err;
2097 d0eebce4 2019-03-11 stsp static const struct got_error *
2098 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2100 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2101 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2102 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2103 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2104 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2105 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2107 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2108 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2109 d0eebce4 2019-03-11 stsp switch (ch) {
2111 d0eebce4 2019-03-11 stsp delref = optarg;
2114 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2115 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2116 d0eebce4 2019-03-11 stsp err(1, "-r option");
2117 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2120 d0eebce4 2019-03-11 stsp do_list = 1;
2123 d0eebce4 2019-03-11 stsp usage_ref();
2124 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2128 d0eebce4 2019-03-11 stsp if (do_list && delref)
2129 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2131 d0eebce4 2019-03-11 stsp argc -= optind;
2132 d0eebce4 2019-03-11 stsp argv += optind;
2134 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2135 d0eebce4 2019-03-11 stsp if (argc > 0)
2136 d0eebce4 2019-03-11 stsp usage_ref();
2137 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2138 d0eebce4 2019-03-11 stsp usage_ref();
2140 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2141 e0b57350 2019-03-12 stsp if (do_list) {
2142 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2143 e0b57350 2019-03-12 stsp NULL) == -1)
2144 e0b57350 2019-03-12 stsp err(1, "pledge");
2146 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2147 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2148 e0b57350 2019-03-12 stsp err(1, "pledge");
2151 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2152 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2153 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2154 d0eebce4 2019-03-11 stsp goto done;
2157 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2158 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2159 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2160 d0eebce4 2019-03-11 stsp goto done;
2162 d0eebce4 2019-03-11 stsp error = NULL;
2163 d0eebce4 2019-03-11 stsp if (worktree) {
2164 d0eebce4 2019-03-11 stsp repo_path =
2165 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2166 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2167 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2168 d0eebce4 2019-03-11 stsp if (error)
2169 d0eebce4 2019-03-11 stsp goto done;
2171 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2172 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2173 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2174 d0eebce4 2019-03-11 stsp goto done;
2179 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2180 d0eebce4 2019-03-11 stsp if (error != NULL)
2181 d0eebce4 2019-03-11 stsp goto done;
2183 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2184 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2185 c02c541e 2019-03-29 stsp if (error)
2186 c02c541e 2019-03-29 stsp goto done;
2188 d0eebce4 2019-03-11 stsp if (do_list)
2189 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2190 d0eebce4 2019-03-11 stsp else if (delref)
2191 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2193 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2196 4e759de4 2019-06-26 stsp got_repo_close(repo);
2197 4e759de4 2019-06-26 stsp if (worktree)
2198 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2199 4e759de4 2019-06-26 stsp free(cwd);
2200 4e759de4 2019-06-26 stsp free(repo_path);
2201 4e759de4 2019-06-26 stsp return error;
2204 4e759de4 2019-06-26 stsp __dead static void
2205 4e759de4 2019-06-26 stsp usage_branch(void)
2207 4e759de4 2019-06-26 stsp fprintf(stderr,
2208 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2209 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2213 4e759de4 2019-06-26 stsp static const struct got_error *
2214 4e759de4 2019-06-26 stsp list_branches(struct got_repository *repo)
2216 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2217 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2218 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2220 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2221 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2223 4e759de4 2019-06-26 stsp return err;
2225 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2226 4e759de4 2019-06-26 stsp const char *refname;
2227 4e759de4 2019-06-26 stsp char *refstr;
2228 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2229 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2231 4e759de4 2019-06-26 stsp refname += 11;
2232 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2233 4e759de4 2019-06-26 stsp if (refstr == NULL)
2234 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2235 4e759de4 2019-06-26 stsp printf("%s: %s\n", refname, refstr);
2236 4e759de4 2019-06-26 stsp free(refstr);
2239 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2240 4e759de4 2019-06-26 stsp return NULL;
2243 4e759de4 2019-06-26 stsp static const struct got_error *
2244 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2246 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2247 4e759de4 2019-06-26 stsp struct got_reference *ref;
2248 4e759de4 2019-06-26 stsp char *refname;
2250 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2251 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2253 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2255 4e759de4 2019-06-26 stsp goto done;
2257 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2258 4e759de4 2019-06-26 stsp got_ref_close(ref);
2260 4e759de4 2019-06-26 stsp free(refname);
2261 4e759de4 2019-06-26 stsp return err;
2264 4e759de4 2019-06-26 stsp static const struct got_error *
2265 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2266 4e759de4 2019-06-26 stsp const char *base_branch)
2268 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2269 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2270 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2271 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2272 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2274 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2275 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2276 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2277 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2278 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2279 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2281 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2283 4e759de4 2019-06-26 stsp goto done;
2284 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2285 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2287 4e759de4 2019-06-26 stsp goto done;
2289 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2290 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2291 4e759de4 2019-06-26 stsp goto done;
2294 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2295 4e759de4 2019-06-26 stsp if (err == NULL) {
2296 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2297 4e759de4 2019-06-26 stsp goto done;
2298 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2299 4e759de4 2019-06-26 stsp goto done;
2301 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2303 4e759de4 2019-06-26 stsp goto done;
2305 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2308 4e759de4 2019-06-26 stsp got_ref_close(ref);
2310 4e759de4 2019-06-26 stsp free(base_refname);
2311 4e759de4 2019-06-26 stsp free(refname);
2312 4e759de4 2019-06-26 stsp return err;
2315 4e759de4 2019-06-26 stsp static const struct got_error *
2316 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2318 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2319 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2320 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2321 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2322 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2323 4e759de4 2019-06-26 stsp const char *delref = NULL;
2325 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2326 4e759de4 2019-06-26 stsp switch (ch) {
2328 4e759de4 2019-06-26 stsp delref = optarg;
2331 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2332 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2333 4e759de4 2019-06-26 stsp err(1, "-r option");
2334 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2337 4e759de4 2019-06-26 stsp do_list = 1;
2340 4e759de4 2019-06-26 stsp usage_branch();
2341 4e759de4 2019-06-26 stsp /* NOTREACHED */
2345 4e759de4 2019-06-26 stsp if (do_list && delref)
2346 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2348 4e759de4 2019-06-26 stsp argc -= optind;
2349 4e759de4 2019-06-26 stsp argv += optind;
2351 4e759de4 2019-06-26 stsp if (do_list || delref) {
2352 4e759de4 2019-06-26 stsp if (argc > 0)
2353 4e759de4 2019-06-26 stsp usage_branch();
2354 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2355 4e759de4 2019-06-26 stsp usage_branch();
2357 4e759de4 2019-06-26 stsp #ifndef PROFILE
2358 4e759de4 2019-06-26 stsp if (do_list) {
2359 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2360 4e759de4 2019-06-26 stsp NULL) == -1)
2361 4e759de4 2019-06-26 stsp err(1, "pledge");
2363 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2364 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2365 4e759de4 2019-06-26 stsp err(1, "pledge");
2368 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2369 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2370 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2371 4e759de4 2019-06-26 stsp goto done;
2374 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2375 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2376 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2377 4e759de4 2019-06-26 stsp goto done;
2379 4e759de4 2019-06-26 stsp error = NULL;
2380 4e759de4 2019-06-26 stsp if (worktree) {
2381 4e759de4 2019-06-26 stsp repo_path =
2382 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2383 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2384 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2385 4e759de4 2019-06-26 stsp if (error)
2386 4e759de4 2019-06-26 stsp goto done;
2388 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2389 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2390 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2391 4e759de4 2019-06-26 stsp goto done;
2396 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2397 4e759de4 2019-06-26 stsp if (error != NULL)
2398 4e759de4 2019-06-26 stsp goto done;
2400 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2401 4e759de4 2019-06-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2402 4e759de4 2019-06-26 stsp if (error)
2403 4e759de4 2019-06-26 stsp goto done;
2405 4e759de4 2019-06-26 stsp if (do_list)
2406 4e759de4 2019-06-26 stsp error = list_branches(repo);
2407 4e759de4 2019-06-26 stsp else if (delref)
2408 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2410 4e759de4 2019-06-26 stsp const char *base_branch;
2411 4e759de4 2019-06-26 stsp if (argc == 1) {
2412 4e759de4 2019-06-26 stsp base_branch = worktree ?
2413 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2414 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2416 4e759de4 2019-06-26 stsp base_branch = argv[1];
2417 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2421 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2422 d0eebce4 2019-03-11 stsp if (worktree)
2423 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2424 d0eebce4 2019-03-11 stsp free(cwd);
2425 d0eebce4 2019-03-11 stsp free(repo_path);
2426 d00136be 2019-03-26 stsp return error;
2429 d00136be 2019-03-26 stsp __dead static void
2430 d00136be 2019-03-26 stsp usage_add(void)
2432 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2436 d00136be 2019-03-26 stsp static const struct got_error *
2437 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2439 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2440 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2441 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2442 1dd54920 2019-05-11 stsp char *cwd = NULL;
2443 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2444 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2447 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2449 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2450 d00136be 2019-03-26 stsp switch (ch) {
2452 d00136be 2019-03-26 stsp usage_add();
2453 d00136be 2019-03-26 stsp /* NOTREACHED */
2457 d00136be 2019-03-26 stsp argc -= optind;
2458 d00136be 2019-03-26 stsp argv += optind;
2460 723c305c 2019-05-11 jcs if (argc < 1)
2461 d00136be 2019-03-26 stsp usage_add();
2463 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2464 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2465 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2466 723c305c 2019-05-11 jcs if (path == NULL) {
2467 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2470 1dd54920 2019-05-11 stsp free(path);
2473 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2474 d00136be 2019-03-26 stsp if (cwd == NULL) {
2475 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2476 d00136be 2019-03-26 stsp goto done;
2479 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2480 d00136be 2019-03-26 stsp if (error)
2481 d00136be 2019-03-26 stsp goto done;
2483 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2484 031a5338 2019-03-26 stsp if (error != NULL)
2485 031a5338 2019-03-26 stsp goto done;
2487 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2488 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2489 d00136be 2019-03-26 stsp if (error)
2490 d00136be 2019-03-26 stsp goto done;
2492 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2493 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2494 723c305c 2019-05-11 jcs if (path == NULL) {
2495 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2499 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2500 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2501 1dd54920 2019-05-11 stsp if (error) {
2502 1dd54920 2019-05-11 stsp free(path);
2506 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2507 1dd54920 2019-05-11 stsp NULL, repo);
2510 031a5338 2019-03-26 stsp got_repo_close(repo);
2511 d00136be 2019-03-26 stsp if (worktree)
2512 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2513 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2514 1dd54920 2019-05-11 stsp free((char *)pe->path);
2515 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2516 2ec1f75b 2019-03-26 stsp free(cwd);
2517 2ec1f75b 2019-03-26 stsp return error;
2520 2ec1f75b 2019-03-26 stsp __dead static void
2521 648e4ef7 2019-07-09 stsp usage_remove(void)
2523 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2527 2ec1f75b 2019-03-26 stsp static const struct got_error *
2528 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2530 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2531 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2532 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2533 17ed4618 2019-06-02 stsp char *cwd = NULL;
2534 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2535 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2536 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2538 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2540 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2541 2ec1f75b 2019-03-26 stsp switch (ch) {
2543 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2546 2ec1f75b 2019-03-26 stsp usage_add();
2547 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2551 2ec1f75b 2019-03-26 stsp argc -= optind;
2552 2ec1f75b 2019-03-26 stsp argv += optind;
2554 17ed4618 2019-06-02 stsp if (argc < 1)
2555 648e4ef7 2019-07-09 stsp usage_remove();
2557 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2558 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2559 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2560 17ed4618 2019-06-02 stsp if (path == NULL) {
2561 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2562 17ed4618 2019-06-02 stsp goto done;
2564 17ed4618 2019-06-02 stsp free(path);
2567 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2568 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2569 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2570 2ec1f75b 2019-03-26 stsp goto done;
2572 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2573 2ec1f75b 2019-03-26 stsp if (error)
2574 2ec1f75b 2019-03-26 stsp goto done;
2576 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2578 2ec1f75b 2019-03-26 stsp goto done;
2580 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2581 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2582 2ec1f75b 2019-03-26 stsp if (error)
2583 2ec1f75b 2019-03-26 stsp goto done;
2585 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2586 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2587 17ed4618 2019-06-02 stsp if (path == NULL) {
2588 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2589 17ed4618 2019-06-02 stsp goto done;
2592 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2593 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2594 17ed4618 2019-06-02 stsp if (error) {
2595 17ed4618 2019-06-02 stsp free(path);
2596 17ed4618 2019-06-02 stsp goto done;
2599 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2600 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2601 a129376b 2019-03-28 stsp if (error)
2602 a129376b 2019-03-28 stsp goto done;
2605 a129376b 2019-03-28 stsp got_repo_close(repo);
2606 a129376b 2019-03-28 stsp if (worktree)
2607 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2608 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2609 17ed4618 2019-06-02 stsp free((char *)pe->path);
2610 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2611 a129376b 2019-03-28 stsp free(cwd);
2612 a129376b 2019-03-28 stsp return error;
2615 a129376b 2019-03-28 stsp __dead static void
2616 a129376b 2019-03-28 stsp usage_revert(void)
2618 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2622 a129376b 2019-03-28 stsp static void
2623 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2625 a129376b 2019-03-28 stsp while (path[0] == '/')
2627 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2630 a129376b 2019-03-28 stsp static const struct got_error *
2631 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2633 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2634 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2635 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2636 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2637 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2638 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2639 e20a8b6f 2019-06-04 stsp int ch, i;
2641 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2643 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2644 a129376b 2019-03-28 stsp switch (ch) {
2646 a129376b 2019-03-28 stsp usage_revert();
2647 a129376b 2019-03-28 stsp /* NOTREACHED */
2651 a129376b 2019-03-28 stsp argc -= optind;
2652 a129376b 2019-03-28 stsp argv += optind;
2654 e20a8b6f 2019-06-04 stsp if (argc < 1)
2655 a129376b 2019-03-28 stsp usage_revert();
2657 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
2658 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
2659 e20a8b6f 2019-06-04 stsp if (path == NULL) {
2660 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
2661 e20a8b6f 2019-06-04 stsp goto done;
2664 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
2665 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2666 e20a8b6f 2019-06-04 stsp if (error) {
2667 e20a8b6f 2019-06-04 stsp free(path);
2668 e20a8b6f 2019-06-04 stsp goto done;
2672 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2673 a129376b 2019-03-28 stsp if (cwd == NULL) {
2674 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2675 a129376b 2019-03-28 stsp goto done;
2677 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2678 2ec1f75b 2019-03-26 stsp if (error)
2679 2ec1f75b 2019-03-26 stsp goto done;
2681 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2682 a129376b 2019-03-28 stsp if (error != NULL)
2683 a129376b 2019-03-28 stsp goto done;
2685 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2686 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2687 a129376b 2019-03-28 stsp if (error)
2688 a129376b 2019-03-28 stsp goto done;
2690 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
2691 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2692 a129376b 2019-03-28 stsp if (error)
2693 a129376b 2019-03-28 stsp goto done;
2696 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2697 2ec1f75b 2019-03-26 stsp if (worktree)
2698 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2699 2ec1f75b 2019-03-26 stsp free(path);
2700 d00136be 2019-03-26 stsp free(cwd);
2701 6bad629b 2019-02-04 stsp return error;
2704 c4296144 2019-05-09 stsp __dead static void
2705 c4296144 2019-05-09 stsp usage_commit(void)
2707 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2712 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2715 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2716 33ad4cbe 2019-05-12 jcs int st = -1;
2718 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2719 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2720 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2722 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2724 33ad4cbe 2019-05-12 jcs goto doneediting;
2726 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2727 33ad4cbe 2019-05-12 jcs _exit(127);
2730 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2731 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2734 33ad4cbe 2019-05-12 jcs doneediting:
2735 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2736 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2737 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2739 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2740 33ad4cbe 2019-05-12 jcs errno = EINTR;
2744 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2747 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2748 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2749 e2ba3d07 2019-05-13 stsp const char *editor;
2750 e0870e44 2019-05-13 stsp const char *worktree_path;
2751 76d98825 2019-06-03 stsp const char *branch_name;
2752 314a6357 2019-05-13 stsp const char *repo_path;
2753 e0870e44 2019-05-13 stsp char *logmsg_path;
2757 33ad4cbe 2019-05-12 jcs static const struct got_error *
2758 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2761 76d98825 2019-06-03 stsp char *initial_content = NULL;
2762 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2763 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2764 e0870e44 2019-05-13 stsp char *template = NULL;
2765 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2766 33ad4cbe 2019-05-12 jcs char buf[1024];
2767 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2769 33ad4cbe 2019-05-12 jcs size_t len;
2770 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2772 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2773 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2774 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2775 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2776 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2777 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2778 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2779 33ad4cbe 2019-05-12 jcs return NULL;
2782 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2783 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
2785 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
2786 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
2787 76d98825 2019-06-03 stsp a->branch_name) == -1)
2788 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2790 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2792 e0870e44 2019-05-13 stsp goto done;
2794 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2796 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2797 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2798 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
2799 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
2800 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
2804 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2805 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2809 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2810 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2814 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2815 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2819 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2820 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2821 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2822 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2823 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2824 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2828 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2829 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2830 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2831 fcde04d9 2019-05-13 stsp goto done;
2833 cc79381d 2019-05-22 stsp (*logmsg)[0] = '\0';
2836 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2837 d4592c7c 2019-05-22 stsp if (fp == NULL) {
2838 d4592c7c 2019-05-22 stsp err = got_error_from_errno("fopen");
2839 d4592c7c 2019-05-22 stsp goto done;
2841 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2842 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2843 e0870e44 2019-05-13 stsp content_changed = 1;
2844 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2845 78527a0a 2019-05-22 stsp continue; /* remove comments and leading empty lines */
2846 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2848 33ad4cbe 2019-05-12 jcs fclose(fp);
2850 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2851 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2855 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2856 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2857 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2858 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2859 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2860 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2864 76d98825 2019-06-03 stsp free(initial_content);
2865 e0870e44 2019-05-13 stsp free(template);
2867 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2868 314a6357 2019-05-13 stsp if (err == NULL)
2869 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2870 33ad4cbe 2019-05-12 jcs return err;
2873 c4296144 2019-05-09 stsp static const struct got_error *
2874 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2876 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2877 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2878 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2879 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2880 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2881 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2882 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2883 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2884 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2885 7d5807f4 2019-07-11 stsp int ch, rebase_in_progress;
2887 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2888 c4296144 2019-05-09 stsp switch (ch) {
2890 c4296144 2019-05-09 stsp logmsg = optarg;
2893 c4296144 2019-05-09 stsp usage_commit();
2894 c4296144 2019-05-09 stsp /* NOTREACHED */
2898 c4296144 2019-05-09 stsp argc -= optind;
2899 c4296144 2019-05-09 stsp argv += optind;
2901 c4296144 2019-05-09 stsp if (argc == 1) {
2902 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2903 c4296144 2019-05-09 stsp if (path == NULL) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2905 c4296144 2019-05-09 stsp goto done;
2907 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2908 c4296144 2019-05-09 stsp } else if (argc != 0)
2909 c4296144 2019-05-09 stsp usage_commit();
2911 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2912 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2913 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2914 35bd8fed 2019-05-09 stsp goto done;
2917 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2918 c4296144 2019-05-09 stsp if (cwd == NULL) {
2919 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2920 c4296144 2019-05-09 stsp goto done;
2922 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2923 7d5807f4 2019-07-11 stsp if (error)
2924 7d5807f4 2019-07-11 stsp goto done;
2926 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2927 c4296144 2019-05-09 stsp if (error)
2928 7d5807f4 2019-07-11 stsp goto done;
2929 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
2930 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
2931 c4296144 2019-05-09 stsp goto done;
2934 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2935 c4296144 2019-05-09 stsp if (error != NULL)
2936 c4296144 2019-05-09 stsp goto done;
2939 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2940 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2942 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2943 314a6357 2019-05-13 stsp error = get_editor(&editor);
2945 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2946 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2947 c4296144 2019-05-09 stsp if (error)
2948 c4296144 2019-05-09 stsp goto done;
2950 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2951 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2952 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2953 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2954 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2955 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
2956 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2957 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
2958 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2959 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2960 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2961 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2962 e0870e44 2019-05-13 stsp if (error) {
2963 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2964 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2965 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2966 c4296144 2019-05-09 stsp goto done;
2969 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2970 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2972 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2973 c4296144 2019-05-09 stsp if (error)
2974 c4296144 2019-05-09 stsp goto done;
2975 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
2978 c4296144 2019-05-09 stsp got_repo_close(repo);
2979 c4296144 2019-05-09 stsp if (worktree)
2980 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2981 c4296144 2019-05-09 stsp free(path);
2982 c4296144 2019-05-09 stsp free(cwd);
2983 c4296144 2019-05-09 stsp free(id_str);
2984 e2ba3d07 2019-05-13 stsp free(editor);
2985 234035bc 2019-06-01 stsp return error;
2988 234035bc 2019-06-01 stsp __dead static void
2989 234035bc 2019-06-01 stsp usage_cherrypick(void)
2991 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2995 234035bc 2019-06-01 stsp static const struct got_error *
2996 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
2998 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
2999 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3000 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3001 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3002 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3003 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3004 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3005 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3006 234035bc 2019-06-01 stsp int ch, did_something = 0;
3008 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3009 234035bc 2019-06-01 stsp switch (ch) {
3011 234035bc 2019-06-01 stsp usage_cherrypick();
3012 234035bc 2019-06-01 stsp /* NOTREACHED */
3016 234035bc 2019-06-01 stsp argc -= optind;
3017 234035bc 2019-06-01 stsp argv += optind;
3019 234035bc 2019-06-01 stsp if (argc != 1)
3020 234035bc 2019-06-01 stsp usage_cherrypick();
3022 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3023 234035bc 2019-06-01 stsp if (cwd == NULL) {
3024 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3025 234035bc 2019-06-01 stsp goto done;
3027 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3028 234035bc 2019-06-01 stsp if (error)
3029 234035bc 2019-06-01 stsp goto done;
3031 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3032 234035bc 2019-06-01 stsp if (error != NULL)
3033 234035bc 2019-06-01 stsp goto done;
3035 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3036 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
3037 234035bc 2019-06-01 stsp if (error)
3038 234035bc 2019-06-01 stsp goto done;
3040 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3041 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3042 234035bc 2019-06-01 stsp if (error != NULL) {
3043 234035bc 2019-06-01 stsp struct got_reference *ref;
3044 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3045 234035bc 2019-06-01 stsp goto done;
3046 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3047 234035bc 2019-06-01 stsp if (error != NULL)
3048 234035bc 2019-06-01 stsp goto done;
3049 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3050 234035bc 2019-06-01 stsp got_ref_close(ref);
3051 234035bc 2019-06-01 stsp if (error != NULL)
3052 234035bc 2019-06-01 stsp goto done;
3054 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3055 234035bc 2019-06-01 stsp if (error)
3056 234035bc 2019-06-01 stsp goto done;
3058 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3059 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3060 234035bc 2019-06-01 stsp if (error != NULL)
3061 234035bc 2019-06-01 stsp goto done;
3063 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3064 234035bc 2019-06-01 stsp if (error) {
3065 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3066 234035bc 2019-06-01 stsp goto done;
3067 234035bc 2019-06-01 stsp error = NULL;
3069 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3070 234035bc 2019-06-01 stsp goto done;
3073 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3074 234035bc 2019-06-01 stsp if (error)
3075 234035bc 2019-06-01 stsp goto done;
3076 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3077 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3078 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3080 234035bc 2019-06-01 stsp if (error != NULL)
3081 234035bc 2019-06-01 stsp goto done;
3083 234035bc 2019-06-01 stsp if (did_something)
3084 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3086 234035bc 2019-06-01 stsp if (commit)
3087 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3088 234035bc 2019-06-01 stsp free(commit_id_str);
3089 234035bc 2019-06-01 stsp if (head_ref)
3090 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3091 234035bc 2019-06-01 stsp if (worktree)
3092 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3094 234035bc 2019-06-01 stsp got_repo_close(repo);
3095 c4296144 2019-05-09 stsp return error;
3098 5ef14e63 2019-06-02 stsp __dead static void
3099 5ef14e63 2019-06-02 stsp usage_backout(void)
3101 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3105 5ef14e63 2019-06-02 stsp static const struct got_error *
3106 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3108 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3109 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3110 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3111 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3112 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3113 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3114 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3115 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3116 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3118 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3119 5ef14e63 2019-06-02 stsp switch (ch) {
3121 5ef14e63 2019-06-02 stsp usage_backout();
3122 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3126 5ef14e63 2019-06-02 stsp argc -= optind;
3127 5ef14e63 2019-06-02 stsp argv += optind;
3129 5ef14e63 2019-06-02 stsp if (argc != 1)
3130 5ef14e63 2019-06-02 stsp usage_backout();
3132 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3133 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3134 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3135 5ef14e63 2019-06-02 stsp goto done;
3137 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3138 5ef14e63 2019-06-02 stsp if (error)
3139 5ef14e63 2019-06-02 stsp goto done;
3141 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3142 5ef14e63 2019-06-02 stsp if (error != NULL)
3143 5ef14e63 2019-06-02 stsp goto done;
3145 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3146 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
3147 5ef14e63 2019-06-02 stsp if (error)
3148 5ef14e63 2019-06-02 stsp goto done;
3150 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3151 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3152 5ef14e63 2019-06-02 stsp if (error != NULL) {
3153 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3154 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3155 5ef14e63 2019-06-02 stsp goto done;
3156 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3157 5ef14e63 2019-06-02 stsp if (error != NULL)
3158 5ef14e63 2019-06-02 stsp goto done;
3159 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3160 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3161 5ef14e63 2019-06-02 stsp if (error != NULL)
3162 5ef14e63 2019-06-02 stsp goto done;
3164 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3165 5ef14e63 2019-06-02 stsp if (error)
3166 5ef14e63 2019-06-02 stsp goto done;
3168 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3169 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3170 5ef14e63 2019-06-02 stsp if (error != NULL)
3171 5ef14e63 2019-06-02 stsp goto done;
3173 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3174 5ef14e63 2019-06-02 stsp if (error)
3175 5ef14e63 2019-06-02 stsp goto done;
3177 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3178 5ef14e63 2019-06-02 stsp if (error)
3179 5ef14e63 2019-06-02 stsp goto done;
3180 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3181 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3182 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3183 5ef14e63 2019-06-02 stsp goto done;
3186 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3187 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3188 5ef14e63 2019-06-02 stsp if (error != NULL)
3189 5ef14e63 2019-06-02 stsp goto done;
3191 5ef14e63 2019-06-02 stsp if (did_something)
3192 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3194 5ef14e63 2019-06-02 stsp if (commit)
3195 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3196 5ef14e63 2019-06-02 stsp free(commit_id_str);
3197 5ef14e63 2019-06-02 stsp if (head_ref)
3198 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3199 818c7501 2019-07-11 stsp if (worktree)
3200 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3202 818c7501 2019-07-11 stsp got_repo_close(repo);
3203 818c7501 2019-07-11 stsp return error;
3206 818c7501 2019-07-11 stsp __dead static void
3207 818c7501 2019-07-11 stsp usage_rebase(void)
3209 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3210 af54c8f8 2019-07-11 stsp getprogname());
3214 818c7501 2019-07-11 stsp static const struct got_error *
3215 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3216 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3218 818c7501 2019-07-11 stsp const struct got_error *err;
3219 818c7501 2019-07-11 stsp char *old_id_str = NULL, *new_id_str = NULL;
3220 818c7501 2019-07-11 stsp char *logmsg0 = NULL, *logmsg, *nl;
3221 818c7501 2019-07-11 stsp size_t len;
3223 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3225 818c7501 2019-07-11 stsp goto done;
3227 ff0d2220 2019-07-11 stsp if (new_id) {
3228 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3230 ff0d2220 2019-07-11 stsp goto done;
3233 818c7501 2019-07-11 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3234 818c7501 2019-07-11 stsp if (logmsg0 == NULL) {
3235 818c7501 2019-07-11 stsp err = got_error_from_errno("strdup");
3236 818c7501 2019-07-11 stsp goto done;
3238 818c7501 2019-07-11 stsp logmsg = logmsg0;
3240 818c7501 2019-07-11 stsp while (isspace(logmsg[0]))
3243 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3244 ff0d2220 2019-07-11 stsp if (new_id_str)
3245 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3246 818c7501 2019-07-11 stsp len = strlen(logmsg);
3247 818c7501 2019-07-11 stsp if (len > 42)
3249 818c7501 2019-07-11 stsp logmsg[len] = '\0';
3250 818c7501 2019-07-11 stsp nl = strchr(logmsg, '\n');
3252 818c7501 2019-07-11 stsp *nl = '\0';
3253 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3254 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3256 818c7501 2019-07-11 stsp free(old_id_str);
3257 818c7501 2019-07-11 stsp free(new_id_str);
3258 818c7501 2019-07-11 stsp free(logmsg0);
3259 818c7501 2019-07-11 stsp return err;
3262 818c7501 2019-07-11 stsp static void
3263 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3265 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3267 818c7501 2019-07-11 stsp while (path[0] == '/')
3269 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3271 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3273 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3274 818c7501 2019-07-11 stsp *rebase_status = status;
3277 818c7501 2019-07-11 stsp static const struct got_error *
3278 818c7501 2019-07-11 stsp rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3279 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3280 818c7501 2019-07-11 stsp struct got_repository *repo)
3282 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3283 818c7501 2019-07-11 stsp return got_worktree_rebase_complete(worktree,
3284 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3287 818c7501 2019-07-11 stsp static const struct got_error *
3288 ff0d2220 2019-07-11 stsp rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3289 ff0d2220 2019-07-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
3291 ff0d2220 2019-07-11 stsp const struct got_error *error;
3292 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3293 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3295 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3296 ff0d2220 2019-07-11 stsp if (error)
3297 ff0d2220 2019-07-11 stsp return error;
3299 ff0d2220 2019-07-11 stsp error = got_worktree_rebase_commit(&new_commit_id, worktree,
3300 ff0d2220 2019-07-11 stsp tmp_branch, commit, commit_id, repo);
3301 ff0d2220 2019-07-11 stsp if (error) {
3302 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3303 ff0d2220 2019-07-11 stsp goto done;
3304 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3306 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3307 ff0d2220 2019-07-11 stsp free(new_commit_id);
3310 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3311 ff0d2220 2019-07-11 stsp return error;
3314 ff0d2220 2019-07-11 stsp static const struct got_error *
3315 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3317 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3318 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3319 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3320 818c7501 2019-07-11 stsp char *cwd = NULL;
3321 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3322 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3323 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3324 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3325 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3326 818c7501 2019-07-11 stsp struct got_commit_graph *graph = NULL;
3327 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3328 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3329 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3330 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3331 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3332 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3334 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3336 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3337 818c7501 2019-07-11 stsp switch (ch) {
3339 818c7501 2019-07-11 stsp abort_rebase = 1;
3342 818c7501 2019-07-11 stsp continue_rebase = 1;
3345 818c7501 2019-07-11 stsp usage_rebase();
3346 818c7501 2019-07-11 stsp /* NOTREACHED */
3350 818c7501 2019-07-11 stsp argc -= optind;
3351 818c7501 2019-07-11 stsp argv += optind;
3353 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3354 818c7501 2019-07-11 stsp usage_rebase();
3355 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3356 818c7501 2019-07-11 stsp if (argc != 0)
3357 818c7501 2019-07-11 stsp usage_rebase();
3358 818c7501 2019-07-11 stsp } else if (argc != 1)
3359 818c7501 2019-07-11 stsp usage_rebase();
3361 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3362 818c7501 2019-07-11 stsp if (cwd == NULL) {
3363 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3364 818c7501 2019-07-11 stsp goto done;
3366 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3367 818c7501 2019-07-11 stsp if (error)
3368 818c7501 2019-07-11 stsp goto done;
3370 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3371 818c7501 2019-07-11 stsp if (error != NULL)
3372 818c7501 2019-07-11 stsp goto done;
3374 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3375 818c7501 2019-07-11 stsp got_worktree_get_root_path(worktree), 0);
3376 818c7501 2019-07-11 stsp if (error)
3377 818c7501 2019-07-11 stsp goto done;
3379 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3380 818c7501 2019-07-11 stsp if (error)
3381 818c7501 2019-07-11 stsp goto done;
3383 818c7501 2019-07-11 stsp if (rebase_in_progress && abort_rebase) {
3384 818c7501 2019-07-11 stsp int did_something;
3385 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3386 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3387 818c7501 2019-07-11 stsp if (error)
3388 818c7501 2019-07-11 stsp goto done;
3389 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3390 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3391 818c7501 2019-07-11 stsp error = got_worktree_rebase_abort(worktree, repo,
3392 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3393 818c7501 2019-07-11 stsp if (error)
3394 818c7501 2019-07-11 stsp goto done;
3395 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3396 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3397 818c7501 2019-07-11 stsp } else if (abort_rebase) {
3398 818c7501 2019-07-11 stsp error = got_error(GOT_ERR_NOT_REBASING);
3399 818c7501 2019-07-11 stsp goto done;
3402 818c7501 2019-07-11 stsp if (continue_rebase) {
3403 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3404 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3405 818c7501 2019-07-11 stsp if (error)
3406 818c7501 2019-07-11 stsp goto done;
3408 ff0d2220 2019-07-11 stsp error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3410 818c7501 2019-07-11 stsp if (error)
3411 818c7501 2019-07-11 stsp goto done;
3413 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3414 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3415 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3416 818c7501 2019-07-11 stsp goto done;
3419 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3420 818c7501 2019-07-11 stsp if (error != NULL)
3421 818c7501 2019-07-11 stsp goto done;
3423 818c7501 2019-07-11 stsp error = check_same_branch(
3424 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree), branch, repo);
3425 818c7501 2019-07-11 stsp if (error) {
3426 818c7501 2019-07-11 stsp if (error->code != GOT_ERR_ANCESTRY)
3427 818c7501 2019-07-11 stsp goto done;
3428 818c7501 2019-07-11 stsp error = NULL;
3430 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3431 818c7501 2019-07-11 stsp "specified branch resolves to a commit which "
3432 818c7501 2019-07-11 stsp "is already contained in work tree's branch");
3433 818c7501 2019-07-11 stsp goto done;
3437 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3438 ff0d2220 2019-07-11 stsp if (error)
3439 ff0d2220 2019-07-11 stsp goto done;
3441 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3442 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3443 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree),
3444 818c7501 2019-07-11 stsp branch_head_commit_id, repo);
3445 818c7501 2019-07-11 stsp if (error)
3446 818c7501 2019-07-11 stsp goto done;
3447 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3448 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3449 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3450 818c7501 2019-07-11 stsp "with work tree's branch");
3451 818c7501 2019-07-11 stsp goto done;
3454 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3455 818c7501 2019-07-11 stsp &tmp_branch, worktree, branch, repo);
3456 818c7501 2019-07-11 stsp if (error)
3457 818c7501 2019-07-11 stsp goto done;
3460 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3461 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3462 818c7501 2019-07-11 stsp if (error)
3463 818c7501 2019-07-11 stsp goto done;
3465 818c7501 2019-07-11 stsp error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3466 818c7501 2019-07-11 stsp if (error)
3467 818c7501 2019-07-11 stsp goto done;
3468 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3469 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3470 818c7501 2019-07-11 stsp error = got_commit_graph_iter_start(graph, pid->id, repo);
3471 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3472 818c7501 2019-07-11 stsp commit = NULL;
3473 818c7501 2019-07-11 stsp if (error)
3474 818c7501 2019-07-11 stsp goto done;
3475 818c7501 2019-07-11 stsp while (got_object_id_cmp(commit_id, yca_id) != 0) {
3476 818c7501 2019-07-11 stsp error = got_commit_graph_iter_next(&parent_id, graph);
3477 818c7501 2019-07-11 stsp if (error) {
3478 818c7501 2019-07-11 stsp if (error->code == GOT_ERR_ITER_COMPLETED) {
3479 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3480 818c7501 2019-07-11 stsp "ran out of commits to rebase before "
3481 818c7501 2019-07-11 stsp "youngest common ancestor commit has "
3482 818c7501 2019-07-11 stsp "been reached?!?");
3483 818c7501 2019-07-11 stsp goto done;
3484 818c7501 2019-07-11 stsp } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3485 818c7501 2019-07-11 stsp goto done;
3486 818c7501 2019-07-11 stsp error = got_commit_graph_fetch_commits(graph, 1, repo);
3487 818c7501 2019-07-11 stsp if (error)
3488 818c7501 2019-07-11 stsp goto done;
3490 818c7501 2019-07-11 stsp error = got_object_qid_alloc(&qid, commit_id);
3491 818c7501 2019-07-11 stsp if (error)
3492 818c7501 2019-07-11 stsp goto done;
3493 818c7501 2019-07-11 stsp SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3494 818c7501 2019-07-11 stsp commit_id = parent_id;
3498 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3499 ff0d2220 2019-07-11 stsp if (continue_rebase)
3500 ff0d2220 2019-07-11 stsp error = rebase_complete(worktree, branch,
3501 ff0d2220 2019-07-11 stsp new_base_branch, tmp_branch, repo);
3503 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3504 818c7501 2019-07-11 stsp goto done;
3507 818c7501 2019-07-11 stsp pid = NULL;
3508 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
3509 818c7501 2019-07-11 stsp commit_id = qid->id;
3510 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
3511 818c7501 2019-07-11 stsp pid = qid;
3513 818c7501 2019-07-11 stsp error = got_worktree_rebase_merge_files(worktree, parent_id,
3514 818c7501 2019-07-11 stsp commit_id, repo, rebase_progress, &rebase_status,
3515 818c7501 2019-07-11 stsp check_cancelled, NULL);
3516 818c7501 2019-07-11 stsp if (error)
3517 818c7501 2019-07-11 stsp goto done;
3519 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT)
3522 ff0d2220 2019-07-11 stsp error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3523 818c7501 2019-07-11 stsp if (error)
3524 818c7501 2019-07-11 stsp goto done;
3527 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3528 818c7501 2019-07-11 stsp error = got_worktree_rebase_postpone(worktree);
3529 818c7501 2019-07-11 stsp if (error)
3530 818c7501 2019-07-11 stsp goto done;
3531 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
3532 818c7501 2019-07-11 stsp "conflicts must be resolved before rebase can be resumed");
3534 818c7501 2019-07-11 stsp error = rebase_complete(worktree, branch, new_base_branch,
3535 818c7501 2019-07-11 stsp tmp_branch, repo);
3537 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
3538 818c7501 2019-07-11 stsp free(branch_head_commit_id);
3539 818c7501 2019-07-11 stsp free(resume_commit_id);
3540 818c7501 2019-07-11 stsp free(yca_id);
3541 818c7501 2019-07-11 stsp if (graph)
3542 818c7501 2019-07-11 stsp got_commit_graph_close(graph);
3543 818c7501 2019-07-11 stsp if (commit)
3544 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3545 818c7501 2019-07-11 stsp if (branch)
3546 818c7501 2019-07-11 stsp got_ref_close(branch);
3547 818c7501 2019-07-11 stsp if (new_base_branch)
3548 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
3549 818c7501 2019-07-11 stsp if (tmp_branch)
3550 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
3551 5ef14e63 2019-06-02 stsp if (worktree)
3552 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3554 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3555 5ef14e63 2019-06-02 stsp return error;