2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 99437157 2018-11-11 stsp #include <signal.h>
29 5c860e29 2018-03-12 stsp #include <stdio.h>
30 5c860e29 2018-03-12 stsp #include <stdlib.h>
31 5c860e29 2018-03-12 stsp #include <string.h>
32 5c860e29 2018-03-12 stsp #include <unistd.h>
33 c09a553d 2018-03-12 stsp #include <libgen.h>
34 c0768b0f 2018-06-10 stsp #include <time.h>
35 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 f42b1b34 2018-03-12 stsp #include "got_error.h"
38 f42b1b34 2018-03-12 stsp #include "got_object.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 f42b1b34 2018-03-12 stsp #include "got_repository.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 c09a553d 2018-03-12 stsp #include "got_worktree.h"
43 79109fed 2018-03-27 stsp #include "got_diff.h"
44 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
45 404c43c4 2018-06-21 stsp #include "got_blame.h"
46 63219cd2 2019-01-04 stsp #include "got_privsep.h"
47 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
49 5c860e29 2018-03-12 stsp #ifndef nitems
50 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
57 99437157 2018-11-11 stsp catch_sigint(int signo)
59 99437157 2018-11-11 stsp sigint_received = 1;
63 99437157 2018-11-11 stsp catch_sigpipe(int signo)
65 99437157 2018-11-11 stsp sigpipe_received = 1;
69 8cfb4057 2019-07-09 stsp struct got_cmd {
70 5c860e29 2018-03-12 stsp const char *cmd_name;
71 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
72 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
73 97b3a7be 2019-07-09 stsp const char *cmd_alias;
76 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
77 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
79 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
83 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
84 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
85 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
86 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
87 d00136be 2019-03-26 stsp __dead static void usage_add(void);
88 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
89 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
90 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
91 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
92 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
94 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
96 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
97 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
98 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
99 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
100 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
101 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
102 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
103 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
104 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
105 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
106 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
107 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
108 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
109 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
111 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
112 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
113 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
114 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
115 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
116 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
117 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
118 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
119 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
120 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
121 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
122 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
123 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
124 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
125 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
126 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
127 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
130 ce5b7c56 2019-07-09 stsp static void
131 ce5b7c56 2019-07-09 stsp list_commands(void)
135 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
136 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
137 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
138 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
140 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
144 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
146 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
147 5c860e29 2018-03-12 stsp unsigned int i;
149 1b6b95a8 2018-03-12 stsp int hflag = 0;
151 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
153 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
154 5c860e29 2018-03-12 stsp switch (ch) {
159 ce5b7c56 2019-07-09 stsp usage(hflag);
160 5c860e29 2018-03-12 stsp /* NOTREACHED */
164 5c860e29 2018-03-12 stsp argc -= optind;
165 5c860e29 2018-03-12 stsp argv += optind;
166 1e70621d 2018-03-27 stsp optind = 0;
168 5c860e29 2018-03-12 stsp if (argc <= 0)
169 ce5b7c56 2019-07-09 stsp usage(hflag);
171 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
172 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
174 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
175 d7d4f210 2018-03-12 stsp const struct got_error *error;
177 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
179 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
180 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
184 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
186 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
187 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
188 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
195 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
196 ce5b7c56 2019-07-09 stsp list_commands();
200 4ed7e80c 2018-05-20 stsp __dead static void
201 ce5b7c56 2019-07-09 stsp usage(int hflag)
203 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
205 ce5b7c56 2019-07-09 stsp list_commands();
209 0266afb7 2019-01-04 stsp static const struct got_error *
210 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
212 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
213 e2ba3d07 2019-05-13 stsp const char *editor;
215 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
216 e2ba3d07 2019-05-13 stsp if (editor == NULL)
217 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
219 0ee7065d 2019-05-13 stsp if (editor) {
220 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
222 0ee7065d 2019-05-13 stsp return err;
225 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
226 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
227 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
228 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
231 e2ba3d07 2019-05-13 stsp return NULL;
234 e2ba3d07 2019-05-13 stsp static const struct got_error *
235 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
236 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
238 163ce85a 2019-05-13 stsp const struct got_error *err;
240 a0937847 2019-05-11 stsp if (create_worktree) {
241 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
242 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
244 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
245 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
247 163ce85a 2019-05-13 stsp err = NULL;
249 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
250 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
254 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
255 163ce85a 2019-05-13 stsp return err;
258 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
259 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
261 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
262 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
264 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
265 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
267 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
268 163ce85a 2019-05-13 stsp if (err != NULL)
269 163ce85a 2019-05-13 stsp return err;
271 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
272 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
274 0266afb7 2019-01-04 stsp return NULL;
277 2c7829a4 2019-06-17 stsp __dead static void
278 2c7829a4 2019-06-17 stsp usage_init(void)
280 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
284 2c7829a4 2019-06-17 stsp static const struct got_error *
285 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
287 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
288 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
291 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
292 2c7829a4 2019-06-17 stsp switch (ch) {
294 2c7829a4 2019-06-17 stsp usage_init();
295 2c7829a4 2019-06-17 stsp /* NOTREACHED */
299 2c7829a4 2019-06-17 stsp argc -= optind;
300 2c7829a4 2019-06-17 stsp argv += optind;
302 2c7829a4 2019-06-17 stsp #ifndef PROFILE
303 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
304 2c7829a4 2019-06-17 stsp err(1, "pledge");
306 2c7829a4 2019-06-17 stsp if (argc != 1)
307 bc20e173 2019-06-17 stsp usage_init();
309 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
310 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
311 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
313 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
315 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
316 2c7829a4 2019-06-17 stsp if (error &&
317 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
320 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
324 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
325 2c7829a4 2019-06-17 stsp if (error != NULL)
329 2c7829a4 2019-06-17 stsp free(repo_path);
330 2c7829a4 2019-06-17 stsp return error;
333 4ed7e80c 2018-05-20 stsp __dead static void
334 c09a553d 2018-03-12 stsp usage_checkout(void)
336 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
337 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
341 92a684f4 2018-03-12 stsp static void
342 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
344 92a684f4 2018-03-12 stsp char *worktree_path = arg;
346 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
347 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
350 92a684f4 2018-03-12 stsp while (path[0] == '/')
353 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
356 99437157 2018-11-11 stsp static const struct got_error *
357 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
359 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
360 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
361 99437157 2018-11-11 stsp return NULL;
364 8069f636 2019-01-12 stsp static const struct got_error *
365 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
366 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
368 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
369 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
371 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
372 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
374 36a38700 2019-05-10 stsp return err;
376 d5bea539 2019-05-13 stsp if (yca_id == NULL)
377 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
380 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
381 d5bea539 2019-05-13 stsp * and the work tree's base commit.
383 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
385 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
393 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
394 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
395 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
397 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
398 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
399 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
401 d5bea539 2019-05-13 stsp free(yca_id);
402 d5bea539 2019-05-13 stsp return NULL;
405 a367ff0f 2019-05-14 stsp static const struct got_error *
406 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
407 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
409 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
410 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
411 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
412 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
414 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
418 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
422 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
427 a367ff0f 2019-05-14 stsp struct got_object_id *id;
428 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
430 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
431 a367ff0f 2019-05-14 stsp err = NULL;
434 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
436 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
443 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
444 a367ff0f 2019-05-14 stsp is_same_branch = 1;
451 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
452 a367ff0f 2019-05-14 stsp free(head_commit_id);
453 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
454 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
455 a367ff0f 2019-05-14 stsp return err;
458 4ed7e80c 2018-05-20 stsp static const struct got_error *
459 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
461 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
462 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
463 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
464 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
465 c09a553d 2018-03-12 stsp char *repo_path = NULL;
466 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
467 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
468 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
469 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
470 72151b04 2019-05-11 stsp int ch, same_path_prefix;
472 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
473 0bb8a95e 2018-03-12 stsp switch (ch) {
475 08573d5b 2019-05-14 stsp branch_name = optarg;
478 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
479 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
480 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
483 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
486 2deda0b9 2019-03-07 stsp usage_checkout();
487 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
491 0bb8a95e 2018-03-12 stsp argc -= optind;
492 0bb8a95e 2018-03-12 stsp argv += optind;
494 6715a751 2018-03-16 stsp #ifndef PROFILE
495 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
496 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
497 c09a553d 2018-03-12 stsp err(1, "pledge");
499 0bb8a95e 2018-03-12 stsp if (argc == 1) {
500 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
501 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
502 76089277 2018-04-01 stsp if (repo_path == NULL)
503 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
504 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
505 76089277 2018-04-01 stsp if (cwd == NULL) {
506 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
509 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
510 230a42bd 2019-05-11 jcs base = basename(path_prefix);
511 230a42bd 2019-05-11 jcs if (base == NULL) {
512 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
513 230a42bd 2019-05-11 jcs path_prefix);
517 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
518 230a42bd 2019-05-11 jcs if (base == NULL) {
519 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
524 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
525 c09a553d 2018-03-12 stsp if (dotgit)
526 c09a553d 2018-03-12 stsp *dotgit = '\0';
527 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
528 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
533 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
534 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
535 76089277 2018-04-01 stsp if (repo_path == NULL) {
536 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
539 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
540 76089277 2018-04-01 stsp if (worktree_path == NULL) {
541 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
545 c09a553d 2018-03-12 stsp usage_checkout();
547 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
548 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
550 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
551 c09a553d 2018-03-12 stsp if (error != NULL)
554 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
558 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
559 c09a553d 2018-03-12 stsp if (error != NULL)
562 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
563 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
566 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
567 c09a553d 2018-03-12 stsp if (error != NULL)
570 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
571 e5dc7198 2018-12-29 stsp path_prefix);
572 e5dc7198 2018-12-29 stsp if (error != NULL)
574 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
575 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
579 8069f636 2019-01-12 stsp if (commit_id_str) {
580 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
581 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
582 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
583 8069f636 2019-01-12 stsp if (error != NULL)
585 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
586 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
587 8069f636 2019-01-12 stsp if (error != NULL) {
588 8069f636 2019-01-12 stsp free(commit_id);
591 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
594 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
595 8069f636 2019-01-12 stsp commit_id);
596 8069f636 2019-01-12 stsp free(commit_id);
601 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
602 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
603 c09a553d 2018-03-12 stsp if (error != NULL)
606 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
609 8069f636 2019-01-12 stsp free(commit_id_str);
610 76089277 2018-04-01 stsp free(repo_path);
611 507dc3bb 2018-12-29 stsp free(worktree_path);
612 507dc3bb 2018-12-29 stsp return error;
615 507dc3bb 2018-12-29 stsp __dead static void
616 507dc3bb 2018-12-29 stsp usage_update(void)
618 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
619 507dc3bb 2018-12-29 stsp getprogname());
623 507dc3bb 2018-12-29 stsp static void
624 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
626 784955db 2019-01-12 stsp int *did_something = arg;
628 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
631 1545c615 2019-02-10 stsp *did_something = 1;
633 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
634 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
637 507dc3bb 2018-12-29 stsp while (path[0] == '/')
639 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
642 be7061eb 2018-12-30 stsp static const struct got_error *
643 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
644 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
645 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
647 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
648 a1fb16d8 2019-05-24 stsp char *base_id_str;
649 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
651 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
652 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
653 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
654 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
655 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
656 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
657 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
660 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
661 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
663 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
664 a1fb16d8 2019-05-24 stsp return err;
665 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
667 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
668 a1fb16d8 2019-05-24 stsp return NULL;
670 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
671 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
672 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
674 a1fb16d8 2019-05-24 stsp return err;
675 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
676 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
677 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
678 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
679 a1fb16d8 2019-05-24 stsp return NULL;
682 a1fb16d8 2019-05-24 stsp static const struct got_error *
683 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
685 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
686 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
687 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
688 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
689 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
690 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
691 024e9686 2019-05-14 stsp const char *branch_name = NULL;
692 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
693 784955db 2019-01-12 stsp int ch, did_something = 0;
695 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
696 507dc3bb 2018-12-29 stsp switch (ch) {
698 024e9686 2019-05-14 stsp branch_name = optarg;
701 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
702 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
703 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
706 2deda0b9 2019-03-07 stsp usage_update();
707 507dc3bb 2018-12-29 stsp /* NOTREACHED */
711 507dc3bb 2018-12-29 stsp argc -= optind;
712 507dc3bb 2018-12-29 stsp argv += optind;
714 507dc3bb 2018-12-29 stsp #ifndef PROFILE
715 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
716 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
717 507dc3bb 2018-12-29 stsp err(1, "pledge");
719 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
720 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
721 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
724 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
728 507dc3bb 2018-12-29 stsp if (argc == 0) {
729 c4cdcb68 2019-04-03 stsp path = strdup("");
730 c4cdcb68 2019-04-03 stsp if (path == NULL) {
731 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
734 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
735 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
739 507dc3bb 2018-12-29 stsp usage_update();
741 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
742 507dc3bb 2018-12-29 stsp if (error != NULL)
745 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
746 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
750 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
751 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
752 024e9686 2019-05-14 stsp if (error != NULL)
754 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
755 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
756 9c4b8182 2019-01-02 stsp if (error != NULL)
758 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
759 507dc3bb 2018-12-29 stsp if (error != NULL)
762 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
763 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
764 507dc3bb 2018-12-29 stsp if (error != NULL)
768 a1fb16d8 2019-05-24 stsp if (branch_name) {
769 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
770 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
771 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
772 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
773 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
774 024e9686 2019-05-14 stsp getprogname(), path);
775 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
778 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
781 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
782 a367ff0f 2019-05-14 stsp free(head_commit_id);
783 024e9686 2019-05-14 stsp if (error != NULL)
785 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
788 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
792 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
793 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
794 a367ff0f 2019-05-14 stsp if (error != NULL) {
795 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
796 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
799 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
804 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
805 507dc3bb 2018-12-29 stsp commit_id) != 0) {
806 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
807 507dc3bb 2018-12-29 stsp commit_id);
812 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
813 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
814 507dc3bb 2018-12-29 stsp if (error != NULL)
817 784955db 2019-01-12 stsp if (did_something)
818 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
820 784955db 2019-01-12 stsp printf("Already up-to-date\n");
822 c09a553d 2018-03-12 stsp free(worktree_path);
823 c4cdcb68 2019-04-03 stsp free(path);
824 507dc3bb 2018-12-29 stsp free(commit_id);
825 9c4b8182 2019-01-02 stsp free(commit_id_str);
826 c09a553d 2018-03-12 stsp return error;
829 f42b1b34 2018-03-12 stsp static const struct got_error *
830 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
831 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
833 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
834 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
835 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
836 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
837 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
839 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
840 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
842 79109fed 2018-03-27 stsp return err;
844 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
845 79f35eb3 2018-06-11 stsp if (qid != NULL) {
846 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
848 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
850 79109fed 2018-03-27 stsp return err;
852 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
853 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
854 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
856 0f2b3dca 2018-12-22 stsp return err;
858 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
860 79109fed 2018-03-27 stsp return err;
863 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
867 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
868 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
869 aaa13589 2019-06-01 stsp arg.outfile = stdout;
870 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
871 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
874 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
875 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
876 0f2b3dca 2018-12-22 stsp free(id_str1);
877 0f2b3dca 2018-12-22 stsp free(id_str2);
878 79109fed 2018-03-27 stsp return err;
881 4bb494d5 2018-06-16 stsp static char *
882 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
884 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
885 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
891 79109fed 2018-03-27 stsp static const struct got_error *
892 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
893 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
894 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
896 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
897 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
898 6c281f94 2018-06-11 stsp char datebuf[26];
899 45d799e2 2018-12-23 stsp time_t committer_time;
900 45d799e2 2018-12-23 stsp const char *author, *committer;
901 199a4027 2019-02-02 stsp char *refs_str = NULL;
902 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
904 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
906 199a4027 2019-02-02 stsp const char *name;
907 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
909 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
910 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
912 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
914 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
916 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
918 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
920 199a4027 2019-02-02 stsp s = refs_str;
921 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
922 199a4027 2019-02-02 stsp name) == -1) {
923 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
929 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
931 8bf5b3c9 2018-03-17 stsp return err;
933 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
934 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
935 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
936 832c249c 2018-06-10 stsp free(id_str);
937 d3d493d7 2019-02-21 stsp id_str = NULL;
938 d3d493d7 2019-02-21 stsp free(refs_str);
939 d3d493d7 2019-02-21 stsp refs_str = NULL;
940 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
941 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
942 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
943 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
944 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
945 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
946 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
947 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
948 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
949 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
950 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
952 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
953 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
954 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
956 a0603db2 2018-06-10 stsp return err;
957 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
958 a0603db2 2018-06-10 stsp free(id_str);
962 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
963 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
964 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
966 621015ac 2018-07-23 stsp logmsg = logmsg0;
968 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
970 832c249c 2018-06-10 stsp printf(" %s\n", line);
971 832c249c 2018-06-10 stsp } while (line);
972 621015ac 2018-07-23 stsp free(logmsg0);
974 971751ac 2018-03-27 stsp if (show_patch) {
975 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
976 971751ac 2018-03-27 stsp if (err == 0)
977 971751ac 2018-03-27 stsp printf("\n");
980 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
981 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
982 07862c20 2018-09-15 stsp return err;
985 f42b1b34 2018-03-12 stsp static const struct got_error *
986 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
987 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
988 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
990 f42b1b34 2018-03-12 stsp const struct got_error *err;
991 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
993 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
994 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
996 f42b1b34 2018-03-12 stsp return err;
997 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1001 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1002 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1004 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1007 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1008 372ccdbb 2018-06-10 stsp if (err) {
1009 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1010 9ba79e04 2018-06-11 stsp err = NULL;
1013 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1015 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1021 b43fbaa0 2018-06-11 stsp if (id == NULL)
1024 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1027 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1029 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1030 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1034 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1035 f42b1b34 2018-03-12 stsp return err;
1038 4ed7e80c 2018-05-20 stsp __dead static void
1039 6f3d1eb0 2018-03-12 stsp usage_log(void)
1041 499d7ecc 2019-05-22 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1042 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1046 4ed7e80c 2018-05-20 stsp static const struct got_error *
1047 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1049 f42b1b34 2018-03-12 stsp const struct got_error *error;
1050 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1051 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1052 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1053 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1054 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1055 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1056 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1057 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1058 64a96a6d 2018-04-01 stsp const char *errstr;
1059 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1061 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1063 6715a751 2018-03-16 stsp #ifndef PROFILE
1064 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1067 f42b1b34 2018-03-12 stsp err(1, "pledge");
1070 499d7ecc 2019-05-22 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1071 79109fed 2018-03-27 stsp switch (ch) {
1073 499d7ecc 2019-05-22 stsp first_parent_traversal = 1;
1076 79109fed 2018-03-27 stsp show_patch = 1;
1079 d142fc45 2018-04-01 stsp start_commit = optarg;
1082 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1084 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1085 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1088 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1089 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1090 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1093 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1094 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1095 04ca23f4 2018-07-16 stsp err(1, "-r option");
1096 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1099 2deda0b9 2019-03-07 stsp usage_log();
1100 79109fed 2018-03-27 stsp /* NOTREACHED */
1104 79109fed 2018-03-27 stsp argc -= optind;
1105 79109fed 2018-03-27 stsp argv += optind;
1107 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1108 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1109 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1110 04ca23f4 2018-07-16 stsp goto done;
1113 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1114 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1115 cffc0aa4 2019-02-05 stsp goto done;
1116 cffc0aa4 2019-02-05 stsp error = NULL;
1118 e7301579 2019-03-18 stsp if (argc == 0) {
1119 cbd1af7a 2019-03-18 stsp path = strdup("");
1120 e7301579 2019-03-18 stsp if (path == NULL) {
1121 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1122 cbd1af7a 2019-03-18 stsp goto done;
1124 e7301579 2019-03-18 stsp } else if (argc == 1) {
1125 e7301579 2019-03-18 stsp if (worktree) {
1126 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1128 e7301579 2019-03-18 stsp if (error)
1129 e7301579 2019-03-18 stsp goto done;
1131 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1132 e7301579 2019-03-18 stsp if (path == NULL) {
1133 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1134 e7301579 2019-03-18 stsp goto done;
1138 cbd1af7a 2019-03-18 stsp usage_log();
1140 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1141 5486daa2 2019-05-11 stsp repo_path = worktree ?
1142 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1144 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1145 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1146 cffc0aa4 2019-02-05 stsp goto done;
1149 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1150 d7d4f210 2018-03-12 stsp if (error != NULL)
1151 04ca23f4 2018-07-16 stsp goto done;
1153 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1154 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1155 c02c541e 2019-03-29 stsp if (error)
1156 c02c541e 2019-03-29 stsp goto done;
1158 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1159 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1160 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1161 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1162 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1163 3235492e 2018-04-01 stsp if (error != NULL)
1164 3235492e 2018-04-01 stsp return error;
1165 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1166 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1167 3235492e 2018-04-01 stsp if (error != NULL)
1168 3235492e 2018-04-01 stsp return error;
1169 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1171 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1172 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1173 3235492e 2018-04-01 stsp if (error == NULL) {
1174 1caad61b 2019-02-01 stsp int obj_type;
1175 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1176 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1177 d1f2edc9 2018-06-13 stsp if (error != NULL)
1178 1caad61b 2019-02-01 stsp goto done;
1179 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1180 1caad61b 2019-02-01 stsp if (error != NULL)
1181 1caad61b 2019-02-01 stsp goto done;
1182 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1183 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1184 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1185 1caad61b 2019-02-01 stsp if (error != NULL)
1186 1caad61b 2019-02-01 stsp goto done;
1187 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1188 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1189 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1190 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1191 1caad61b 2019-02-01 stsp goto done;
1194 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1195 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1196 1caad61b 2019-02-01 stsp if (id == NULL)
1197 638f9024 2019-05-13 stsp error = got_error_from_errno(
1198 230a42bd 2019-05-11 jcs "got_object_id_dup");
1199 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1200 1caad61b 2019-02-01 stsp if (error)
1201 1caad61b 2019-02-01 stsp goto done;
1202 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1203 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1204 1caad61b 2019-02-01 stsp goto done;
1206 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1207 d1f2edc9 2018-06-13 stsp if (error != NULL)
1208 1caad61b 2019-02-01 stsp goto done;
1210 15a94983 2018-12-23 stsp if (commit == NULL) {
1211 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1212 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1213 d1f2edc9 2018-06-13 stsp if (error != NULL)
1214 d1f2edc9 2018-06-13 stsp return error;
1217 d7d4f210 2018-03-12 stsp if (error != NULL)
1218 04ca23f4 2018-07-16 stsp goto done;
1220 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1221 04ca23f4 2018-07-16 stsp if (error != NULL)
1222 04ca23f4 2018-07-16 stsp goto done;
1223 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1224 04ca23f4 2018-07-16 stsp free(path);
1225 04ca23f4 2018-07-16 stsp path = in_repo_path;
1228 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1229 199a4027 2019-02-02 stsp if (error)
1230 199a4027 2019-02-02 stsp goto done;
1232 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1233 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1235 04ca23f4 2018-07-16 stsp free(path);
1236 04ca23f4 2018-07-16 stsp free(repo_path);
1237 04ca23f4 2018-07-16 stsp free(cwd);
1239 cffc0aa4 2019-02-05 stsp if (worktree)
1240 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1241 ad242220 2018-09-08 stsp if (repo) {
1242 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1243 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1244 ad242220 2018-09-08 stsp if (error == NULL)
1245 ad242220 2018-09-08 stsp error = repo_error;
1247 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1248 8bf5b3c9 2018-03-17 stsp return error;
1251 4ed7e80c 2018-05-20 stsp __dead static void
1252 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1254 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1255 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1259 b72f483a 2019-02-05 stsp struct print_diff_arg {
1260 b72f483a 2019-02-05 stsp struct got_repository *repo;
1261 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1262 b72f483a 2019-02-05 stsp int diff_context;
1263 3fc0c068 2019-02-10 stsp const char *id_str;
1264 3fc0c068 2019-02-10 stsp int header_shown;
1267 4ed7e80c 2018-05-20 stsp static const struct got_error *
1268 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1269 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1271 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1272 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1273 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1274 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1275 b72f483a 2019-02-05 stsp char *abspath = NULL;
1276 b72f483a 2019-02-05 stsp struct stat sb;
1278 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1279 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1280 b72f483a 2019-02-05 stsp return NULL;
1282 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1283 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1284 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1285 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1288 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1289 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1291 d00136be 2019-03-26 stsp goto done;
1295 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1296 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1297 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1298 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1299 2ec1f75b 2019-03-26 stsp goto done;
1302 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1303 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1304 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1305 2ec1f75b 2019-03-26 stsp goto done;
1307 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1308 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1309 2ec1f75b 2019-03-26 stsp goto done;
1312 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1314 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1317 b72f483a 2019-02-05 stsp if (blob1)
1318 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1319 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1320 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1321 b72f483a 2019-02-05 stsp free(abspath);
1322 927df6b7 2019-02-10 stsp return err;
1325 927df6b7 2019-02-10 stsp static const struct got_error *
1326 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1328 b00d56cd 2018-04-01 stsp const struct got_error *error;
1329 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1330 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1331 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1332 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1333 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1334 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1335 15a94983 2018-12-23 stsp int type1, type2;
1336 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1337 c0cc5c62 2018-10-18 stsp const char *errstr;
1338 927df6b7 2019-02-10 stsp char *path = NULL;
1340 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1341 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1342 25eccc22 2019-01-04 stsp NULL) == -1)
1343 b00d56cd 2018-04-01 stsp err(1, "pledge");
1346 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1347 b00d56cd 2018-04-01 stsp switch (ch) {
1349 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1350 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1351 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1354 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1355 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1356 b72f483a 2019-02-05 stsp err(1, "-r option");
1357 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1360 2deda0b9 2019-03-07 stsp usage_diff();
1361 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1365 b00d56cd 2018-04-01 stsp argc -= optind;
1366 b00d56cd 2018-04-01 stsp argv += optind;
1368 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1369 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1370 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1371 b72f483a 2019-02-05 stsp goto done;
1373 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1374 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1375 927df6b7 2019-02-10 stsp goto done;
1376 927df6b7 2019-02-10 stsp if (argc <= 1) {
1377 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1378 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1379 927df6b7 2019-02-10 stsp goto done;
1381 b72f483a 2019-02-05 stsp if (repo_path)
1383 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1384 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1385 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1386 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1387 927df6b7 2019-02-10 stsp goto done;
1389 927df6b7 2019-02-10 stsp if (argc == 1) {
1390 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1392 927df6b7 2019-02-10 stsp if (error)
1393 927df6b7 2019-02-10 stsp goto done;
1395 927df6b7 2019-02-10 stsp path = strdup("");
1396 927df6b7 2019-02-10 stsp if (path == NULL) {
1397 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1398 927df6b7 2019-02-10 stsp goto done;
1401 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1402 15a94983 2018-12-23 stsp id_str1 = argv[0];
1403 15a94983 2018-12-23 stsp id_str2 = argv[1];
1404 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1405 30db809c 2019-06-05 stsp repo_path =
1406 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1407 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1408 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1409 30db809c 2019-06-05 stsp goto done;
1413 b00d56cd 2018-04-01 stsp usage_diff();
1415 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1416 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1417 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1418 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1421 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1422 76089277 2018-04-01 stsp free(repo_path);
1423 b00d56cd 2018-04-01 stsp if (error != NULL)
1424 b00d56cd 2018-04-01 stsp goto done;
1426 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1427 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1428 c02c541e 2019-03-29 stsp if (error)
1429 c02c541e 2019-03-29 stsp goto done;
1431 30db809c 2019-06-05 stsp if (argc <= 1) {
1432 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1433 b72f483a 2019-02-05 stsp char *id_str;
1434 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1435 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1436 b72f483a 2019-02-05 stsp if (error)
1437 b72f483a 2019-02-05 stsp goto done;
1438 b72f483a 2019-02-05 stsp arg.repo = repo;
1439 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1440 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1441 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1442 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1444 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1445 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1446 b72f483a 2019-02-05 stsp free(id_str);
1447 b72f483a 2019-02-05 stsp goto done;
1450 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1451 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1452 e02e74af 2019-05-28 stsp if (error) {
1453 e02e74af 2019-05-28 stsp struct got_reference *ref;
1454 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1455 e02e74af 2019-05-28 stsp goto done;
1456 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1457 e02e74af 2019-05-28 stsp if (error != NULL)
1458 e02e74af 2019-05-28 stsp goto done;
1459 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1460 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1461 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1462 5e70831e 2019-05-28 stsp goto done;
1464 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1465 e02e74af 2019-05-28 stsp got_ref_close(ref);
1466 e02e74af 2019-05-28 stsp if (error != NULL)
1467 5e70831e 2019-05-28 stsp goto done;
1469 5e70831e 2019-05-28 stsp label1 = strdup(id_str1);
1470 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1471 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1472 e02e74af 2019-05-28 stsp goto done;
1476 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1477 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1478 e02e74af 2019-05-28 stsp if (error) {
1479 e02e74af 2019-05-28 stsp struct got_reference *ref;
1480 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1481 e02e74af 2019-05-28 stsp goto done;
1482 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1483 e02e74af 2019-05-28 stsp if (error != NULL)
1484 5e70831e 2019-05-28 stsp goto done;
1485 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1486 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1487 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1488 e02e74af 2019-05-28 stsp goto done;
1490 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1491 e02e74af 2019-05-28 stsp got_ref_close(ref);
1492 e02e74af 2019-05-28 stsp if (error != NULL)
1493 e02e74af 2019-05-28 stsp goto done;
1495 5e70831e 2019-05-28 stsp label2 = strdup(id_str2);
1496 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1497 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1498 5e70831e 2019-05-28 stsp goto done;
1502 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1503 15a94983 2018-12-23 stsp if (error)
1504 15a94983 2018-12-23 stsp goto done;
1506 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1507 15a94983 2018-12-23 stsp if (error)
1508 15a94983 2018-12-23 stsp goto done;
1510 15a94983 2018-12-23 stsp if (type1 != type2) {
1511 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1512 b00d56cd 2018-04-01 stsp goto done;
1515 15a94983 2018-12-23 stsp switch (type1) {
1516 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1517 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 54156555 2018-12-24 stsp diff_context, repo, stdout);
1520 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1521 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1522 54156555 2018-12-24 stsp diff_context, repo, stdout);
1524 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1525 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1526 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1527 c0cc5c62 2018-10-18 stsp repo, stdout);
1530 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1534 5e70831e 2019-05-28 stsp free(label1);
1535 5e70831e 2019-05-28 stsp free(label2);
1536 15a94983 2018-12-23 stsp free(id1);
1537 15a94983 2018-12-23 stsp free(id2);
1538 927df6b7 2019-02-10 stsp free(path);
1539 b72f483a 2019-02-05 stsp if (worktree)
1540 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1541 ad242220 2018-09-08 stsp if (repo) {
1542 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1543 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1544 ad242220 2018-09-08 stsp if (error == NULL)
1545 ad242220 2018-09-08 stsp error = repo_error;
1547 b00d56cd 2018-04-01 stsp return error;
1550 404c43c4 2018-06-21 stsp __dead static void
1551 404c43c4 2018-06-21 stsp usage_blame(void)
1553 9270e621 2019-02-05 stsp fprintf(stderr,
1554 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1555 404c43c4 2018-06-21 stsp getprogname());
1559 404c43c4 2018-06-21 stsp static const struct got_error *
1560 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1562 404c43c4 2018-06-21 stsp const struct got_error *error;
1563 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1564 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1565 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1567 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1570 404c43c4 2018-06-21 stsp #ifndef PROFILE
1571 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1572 36e2fb66 2019-01-04 stsp NULL) == -1)
1573 404c43c4 2018-06-21 stsp err(1, "pledge");
1576 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1577 404c43c4 2018-06-21 stsp switch (ch) {
1579 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1582 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1583 66bea077 2018-08-02 stsp if (repo_path == NULL)
1584 66bea077 2018-08-02 stsp err(1, "-r option");
1585 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1588 2deda0b9 2019-03-07 stsp usage_blame();
1589 404c43c4 2018-06-21 stsp /* NOTREACHED */
1593 404c43c4 2018-06-21 stsp argc -= optind;
1594 404c43c4 2018-06-21 stsp argv += optind;
1596 a39318fd 2018-08-02 stsp if (argc == 1)
1597 404c43c4 2018-06-21 stsp path = argv[0];
1599 404c43c4 2018-06-21 stsp usage_blame();
1601 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1602 66bea077 2018-08-02 stsp if (cwd == NULL) {
1603 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1604 66bea077 2018-08-02 stsp goto done;
1606 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1607 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1608 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 66bea077 2018-08-02 stsp goto done;
1611 0c06baac 2019-02-05 stsp error = NULL;
1612 0c06baac 2019-02-05 stsp if (worktree) {
1613 0c06baac 2019-02-05 stsp repo_path =
1614 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1615 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1616 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1617 0c06baac 2019-02-05 stsp if (error)
1618 0c06baac 2019-02-05 stsp goto done;
1620 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1621 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1622 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1623 0c06baac 2019-02-05 stsp goto done;
1628 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1629 c02c541e 2019-03-29 stsp if (error != NULL)
1630 36e2fb66 2019-01-04 stsp goto done;
1632 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1633 c02c541e 2019-03-29 stsp if (error)
1634 404c43c4 2018-06-21 stsp goto done;
1636 0c06baac 2019-02-05 stsp if (worktree) {
1637 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1638 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1639 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1640 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1641 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1643 6efaaa2d 2019-02-05 stsp path) == -1) {
1644 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1645 0c06baac 2019-02-05 stsp goto done;
1647 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1650 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 0c06baac 2019-02-05 stsp if (error)
1653 66bea077 2018-08-02 stsp goto done;
1655 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1656 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1657 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1658 404c43c4 2018-06-21 stsp if (error != NULL)
1659 66bea077 2018-08-02 stsp goto done;
1660 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1661 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1662 404c43c4 2018-06-21 stsp if (error != NULL)
1663 66bea077 2018-08-02 stsp goto done;
1665 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1666 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1667 404c43c4 2018-06-21 stsp if (error != NULL)
1668 66bea077 2018-08-02 stsp goto done;
1671 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1673 66bea077 2018-08-02 stsp free(in_repo_path);
1674 66bea077 2018-08-02 stsp free(repo_path);
1675 66bea077 2018-08-02 stsp free(cwd);
1676 404c43c4 2018-06-21 stsp free(commit_id);
1677 0c06baac 2019-02-05 stsp if (worktree)
1678 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1679 ad242220 2018-09-08 stsp if (repo) {
1680 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1681 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1682 ad242220 2018-09-08 stsp if (error == NULL)
1683 ad242220 2018-09-08 stsp error = repo_error;
1685 404c43c4 2018-06-21 stsp return error;
1688 5de5890b 2018-10-18 stsp __dead static void
1689 5de5890b 2018-10-18 stsp usage_tree(void)
1691 c1669e2e 2019-01-09 stsp fprintf(stderr,
1692 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1693 5de5890b 2018-10-18 stsp getprogname());
1697 c1669e2e 2019-01-09 stsp static void
1698 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 c1669e2e 2019-01-09 stsp const char *root_path)
1701 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1703 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1704 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1707 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1708 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1709 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 5de5890b 2018-10-18 stsp static const struct got_error *
1713 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1714 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1715 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1717 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1718 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1719 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1720 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1721 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1723 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1725 5de5890b 2018-10-18 stsp goto done;
1727 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1729 5de5890b 2018-10-18 stsp goto done;
1730 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1731 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1732 5de5890b 2018-10-18 stsp while (te) {
1733 5de5890b 2018-10-18 stsp char *id = NULL;
1735 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1738 5de5890b 2018-10-18 stsp if (show_ids) {
1739 5de5890b 2018-10-18 stsp char *id_str;
1740 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1742 5de5890b 2018-10-18 stsp goto done;
1743 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1744 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1745 5de5890b 2018-10-18 stsp free(id_str);
1746 5de5890b 2018-10-18 stsp goto done;
1748 5de5890b 2018-10-18 stsp free(id_str);
1750 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1753 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1754 c1669e2e 2019-01-09 stsp char *child_path;
1755 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1756 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1757 c1669e2e 2019-01-09 stsp te->name) == -1) {
1758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1759 c1669e2e 2019-01-09 stsp goto done;
1761 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1762 c1669e2e 2019-01-09 stsp root_path, repo);
1763 c1669e2e 2019-01-09 stsp free(child_path);
1765 c1669e2e 2019-01-09 stsp goto done;
1768 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1772 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1773 5de5890b 2018-10-18 stsp free(tree_id);
1774 5de5890b 2018-10-18 stsp return err;
1777 5de5890b 2018-10-18 stsp static const struct got_error *
1778 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1780 5de5890b 2018-10-18 stsp const struct got_error *error;
1781 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1782 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1783 7a2c19d6 2019-02-05 stsp const char *path;
1784 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1786 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1787 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1790 5de5890b 2018-10-18 stsp #ifndef PROFILE
1791 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1792 0f8d269b 2019-01-04 stsp NULL) == -1)
1793 5de5890b 2018-10-18 stsp err(1, "pledge");
1796 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1797 5de5890b 2018-10-18 stsp switch (ch) {
1799 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1802 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1803 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1804 5de5890b 2018-10-18 stsp err(1, "-r option");
1805 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1808 5de5890b 2018-10-18 stsp show_ids = 1;
1811 c1669e2e 2019-01-09 stsp recurse = 1;
1814 2deda0b9 2019-03-07 stsp usage_tree();
1815 5de5890b 2018-10-18 stsp /* NOTREACHED */
1819 5de5890b 2018-10-18 stsp argc -= optind;
1820 5de5890b 2018-10-18 stsp argv += optind;
1822 5de5890b 2018-10-18 stsp if (argc == 1)
1823 5de5890b 2018-10-18 stsp path = argv[0];
1824 5de5890b 2018-10-18 stsp else if (argc > 1)
1825 5de5890b 2018-10-18 stsp usage_tree();
1827 7a2c19d6 2019-02-05 stsp path = NULL;
1829 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1830 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1831 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1832 9bf7a39b 2019-02-05 stsp goto done;
1834 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1835 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1836 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1837 7a2c19d6 2019-02-05 stsp goto done;
1839 7a2c19d6 2019-02-05 stsp error = NULL;
1840 7a2c19d6 2019-02-05 stsp if (worktree) {
1841 7a2c19d6 2019-02-05 stsp repo_path =
1842 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1843 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1844 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1845 7a2c19d6 2019-02-05 stsp if (error)
1846 7a2c19d6 2019-02-05 stsp goto done;
1848 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1849 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1850 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1851 7a2c19d6 2019-02-05 stsp goto done;
1856 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1857 5de5890b 2018-10-18 stsp if (error != NULL)
1858 c02c541e 2019-03-29 stsp goto done;
1860 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1861 c02c541e 2019-03-29 stsp if (error)
1862 5de5890b 2018-10-18 stsp goto done;
1864 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1865 9bf7a39b 2019-02-05 stsp if (worktree) {
1866 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1867 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1868 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1869 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1870 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1871 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1872 9bf7a39b 2019-02-05 stsp goto done;
1874 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1876 9bf7a39b 2019-02-05 stsp if (error)
1877 9bf7a39b 2019-02-05 stsp goto done;
1879 9bf7a39b 2019-02-05 stsp path = "/";
1881 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1882 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1883 9bf7a39b 2019-02-05 stsp if (error != NULL)
1884 9bf7a39b 2019-02-05 stsp goto done;
1887 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1888 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1889 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1890 5de5890b 2018-10-18 stsp if (error != NULL)
1891 5de5890b 2018-10-18 stsp goto done;
1892 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1893 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1894 5de5890b 2018-10-18 stsp if (error != NULL)
1895 5de5890b 2018-10-18 stsp goto done;
1897 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1898 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1899 5de5890b 2018-10-18 stsp if (error != NULL)
1900 5de5890b 2018-10-18 stsp goto done;
1903 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1906 5de5890b 2018-10-18 stsp free(in_repo_path);
1907 5de5890b 2018-10-18 stsp free(repo_path);
1908 5de5890b 2018-10-18 stsp free(cwd);
1909 5de5890b 2018-10-18 stsp free(commit_id);
1910 7a2c19d6 2019-02-05 stsp if (worktree)
1911 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1912 5de5890b 2018-10-18 stsp if (repo) {
1913 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1914 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1915 5de5890b 2018-10-18 stsp if (error == NULL)
1916 5de5890b 2018-10-18 stsp error = repo_error;
1918 5de5890b 2018-10-18 stsp return error;
1921 6bad629b 2019-02-04 stsp __dead static void
1922 6bad629b 2019-02-04 stsp usage_status(void)
1924 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1928 b72f483a 2019-02-05 stsp static const struct got_error *
1929 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1930 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1933 b72f483a 2019-02-05 stsp return NULL;
1936 6bad629b 2019-02-04 stsp static const struct got_error *
1937 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1939 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1940 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1941 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1942 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1945 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1946 6bad629b 2019-02-04 stsp switch (ch) {
1948 2deda0b9 2019-03-07 stsp usage_status();
1949 6bad629b 2019-02-04 stsp /* NOTREACHED */
1953 6bad629b 2019-02-04 stsp argc -= optind;
1954 6bad629b 2019-02-04 stsp argv += optind;
1956 6bad629b 2019-02-04 stsp #ifndef PROFILE
1957 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1958 6bad629b 2019-02-04 stsp NULL) == -1)
1959 6bad629b 2019-02-04 stsp err(1, "pledge");
1961 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1962 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1963 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1964 927df6b7 2019-02-10 stsp goto done;
1967 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1968 927df6b7 2019-02-10 stsp if (error != NULL)
1969 927df6b7 2019-02-10 stsp goto done;
1971 6bad629b 2019-02-04 stsp if (argc == 0) {
1972 927df6b7 2019-02-10 stsp path = strdup("");
1973 927df6b7 2019-02-10 stsp if (path == NULL) {
1974 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1975 6bad629b 2019-02-04 stsp goto done;
1977 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1978 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1979 927df6b7 2019-02-10 stsp if (error)
1980 6bad629b 2019-02-04 stsp goto done;
1982 6bad629b 2019-02-04 stsp usage_status();
1984 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1985 6bad629b 2019-02-04 stsp if (error != NULL)
1986 6bad629b 2019-02-04 stsp goto done;
1988 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1989 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1990 6bad629b 2019-02-04 stsp if (error)
1991 6bad629b 2019-02-04 stsp goto done;
1993 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1996 927df6b7 2019-02-10 stsp free(cwd);
1997 927df6b7 2019-02-10 stsp free(path);
1998 d0eebce4 2019-03-11 stsp return error;
2001 d0eebce4 2019-03-11 stsp __dead static void
2002 d0eebce4 2019-03-11 stsp usage_ref(void)
2004 d0eebce4 2019-03-11 stsp fprintf(stderr,
2005 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2006 d0eebce4 2019-03-11 stsp getprogname());
2010 d0eebce4 2019-03-11 stsp static const struct got_error *
2011 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2013 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2014 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2015 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2017 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2018 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2020 d0eebce4 2019-03-11 stsp return err;
2022 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2023 d0eebce4 2019-03-11 stsp char *refstr;
2024 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2025 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2026 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2027 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2028 d0eebce4 2019-03-11 stsp free(refstr);
2031 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2032 d0eebce4 2019-03-11 stsp return NULL;
2035 d0eebce4 2019-03-11 stsp static const struct got_error *
2036 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2038 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2039 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2041 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2043 d0eebce4 2019-03-11 stsp return err;
2045 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2046 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2047 d0eebce4 2019-03-11 stsp return err;
2050 d0eebce4 2019-03-11 stsp static const struct got_error *
2051 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2054 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2055 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2057 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2059 d83d9d5c 2019-05-13 stsp if (err) {
2060 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2062 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2063 d83d9d5c 2019-05-13 stsp return err;
2064 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2066 d83d9d5c 2019-05-13 stsp return err;
2067 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2068 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2070 d83d9d5c 2019-05-13 stsp return err;
2073 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2075 d0eebce4 2019-03-11 stsp goto done;
2077 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2080 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2082 d0eebce4 2019-03-11 stsp return err;
2085 d0eebce4 2019-03-11 stsp static const struct got_error *
2086 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2088 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2089 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2090 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2091 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2092 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2093 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2095 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2096 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2097 d0eebce4 2019-03-11 stsp switch (ch) {
2099 d0eebce4 2019-03-11 stsp delref = optarg;
2102 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2103 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2104 d0eebce4 2019-03-11 stsp err(1, "-r option");
2105 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2108 d0eebce4 2019-03-11 stsp do_list = 1;
2111 d0eebce4 2019-03-11 stsp usage_ref();
2112 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2116 d0eebce4 2019-03-11 stsp if (do_list && delref)
2117 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2119 d0eebce4 2019-03-11 stsp argc -= optind;
2120 d0eebce4 2019-03-11 stsp argv += optind;
2122 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2123 d0eebce4 2019-03-11 stsp if (argc > 0)
2124 d0eebce4 2019-03-11 stsp usage_ref();
2125 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2126 d0eebce4 2019-03-11 stsp usage_ref();
2128 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2129 e0b57350 2019-03-12 stsp if (do_list) {
2130 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2131 e0b57350 2019-03-12 stsp NULL) == -1)
2132 e0b57350 2019-03-12 stsp err(1, "pledge");
2134 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2135 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2136 e0b57350 2019-03-12 stsp err(1, "pledge");
2139 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2140 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2141 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2142 d0eebce4 2019-03-11 stsp goto done;
2145 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2146 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2147 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2148 d0eebce4 2019-03-11 stsp goto done;
2150 d0eebce4 2019-03-11 stsp error = NULL;
2151 d0eebce4 2019-03-11 stsp if (worktree) {
2152 d0eebce4 2019-03-11 stsp repo_path =
2153 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2154 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2155 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2156 d0eebce4 2019-03-11 stsp if (error)
2157 d0eebce4 2019-03-11 stsp goto done;
2159 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2160 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2161 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2162 d0eebce4 2019-03-11 stsp goto done;
2167 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2168 d0eebce4 2019-03-11 stsp if (error != NULL)
2169 d0eebce4 2019-03-11 stsp goto done;
2171 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2172 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2173 c02c541e 2019-03-29 stsp if (error)
2174 c02c541e 2019-03-29 stsp goto done;
2176 d0eebce4 2019-03-11 stsp if (do_list)
2177 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2178 d0eebce4 2019-03-11 stsp else if (delref)
2179 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2181 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2184 4e759de4 2019-06-26 stsp got_repo_close(repo);
2185 4e759de4 2019-06-26 stsp if (worktree)
2186 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2187 4e759de4 2019-06-26 stsp free(cwd);
2188 4e759de4 2019-06-26 stsp free(repo_path);
2189 4e759de4 2019-06-26 stsp return error;
2192 4e759de4 2019-06-26 stsp __dead static void
2193 4e759de4 2019-06-26 stsp usage_branch(void)
2195 4e759de4 2019-06-26 stsp fprintf(stderr,
2196 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2197 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2201 4e759de4 2019-06-26 stsp static const struct got_error *
2202 4e759de4 2019-06-26 stsp list_branches(struct got_repository *repo)
2204 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2205 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2206 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2208 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2209 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2211 4e759de4 2019-06-26 stsp return err;
2213 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2214 4e759de4 2019-06-26 stsp const char *refname;
2215 4e759de4 2019-06-26 stsp char *refstr;
2216 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2217 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2219 4e759de4 2019-06-26 stsp refname += 11;
2220 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2221 4e759de4 2019-06-26 stsp if (refstr == NULL)
2222 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2223 4e759de4 2019-06-26 stsp printf("%s: %s\n", refname, refstr);
2224 4e759de4 2019-06-26 stsp free(refstr);
2227 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2228 4e759de4 2019-06-26 stsp return NULL;
2231 4e759de4 2019-06-26 stsp static const struct got_error *
2232 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2234 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2235 4e759de4 2019-06-26 stsp struct got_reference *ref;
2236 4e759de4 2019-06-26 stsp char *refname;
2238 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2239 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2241 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2243 4e759de4 2019-06-26 stsp goto done;
2245 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2246 4e759de4 2019-06-26 stsp got_ref_close(ref);
2248 4e759de4 2019-06-26 stsp free(refname);
2249 4e759de4 2019-06-26 stsp return err;
2252 4e759de4 2019-06-26 stsp static const struct got_error *
2253 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2254 4e759de4 2019-06-26 stsp const char *base_branch)
2256 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2257 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2258 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2259 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2260 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2262 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2263 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2264 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2265 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2266 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2267 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2269 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2271 4e759de4 2019-06-26 stsp goto done;
2272 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2273 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2275 4e759de4 2019-06-26 stsp goto done;
2277 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2278 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2279 4e759de4 2019-06-26 stsp goto done;
2282 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2283 4e759de4 2019-06-26 stsp if (err == NULL) {
2284 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2285 4e759de4 2019-06-26 stsp goto done;
2286 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2287 4e759de4 2019-06-26 stsp goto done;
2289 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2291 4e759de4 2019-06-26 stsp goto done;
2293 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2296 4e759de4 2019-06-26 stsp got_ref_close(ref);
2298 4e759de4 2019-06-26 stsp free(base_refname);
2299 4e759de4 2019-06-26 stsp free(refname);
2300 4e759de4 2019-06-26 stsp return err;
2303 4e759de4 2019-06-26 stsp static const struct got_error *
2304 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2306 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2307 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2308 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2309 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2310 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2311 4e759de4 2019-06-26 stsp const char *delref = NULL;
2313 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2314 4e759de4 2019-06-26 stsp switch (ch) {
2316 4e759de4 2019-06-26 stsp delref = optarg;
2319 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2320 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2321 4e759de4 2019-06-26 stsp err(1, "-r option");
2322 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2325 4e759de4 2019-06-26 stsp do_list = 1;
2328 4e759de4 2019-06-26 stsp usage_branch();
2329 4e759de4 2019-06-26 stsp /* NOTREACHED */
2333 4e759de4 2019-06-26 stsp if (do_list && delref)
2334 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2336 4e759de4 2019-06-26 stsp argc -= optind;
2337 4e759de4 2019-06-26 stsp argv += optind;
2339 4e759de4 2019-06-26 stsp if (do_list || delref) {
2340 4e759de4 2019-06-26 stsp if (argc > 0)
2341 4e759de4 2019-06-26 stsp usage_branch();
2342 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2343 4e759de4 2019-06-26 stsp usage_branch();
2345 4e759de4 2019-06-26 stsp #ifndef PROFILE
2346 4e759de4 2019-06-26 stsp if (do_list) {
2347 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2348 4e759de4 2019-06-26 stsp NULL) == -1)
2349 4e759de4 2019-06-26 stsp err(1, "pledge");
2351 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2352 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2353 4e759de4 2019-06-26 stsp err(1, "pledge");
2356 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2357 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2358 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2359 4e759de4 2019-06-26 stsp goto done;
2362 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2363 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2364 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2365 4e759de4 2019-06-26 stsp goto done;
2367 4e759de4 2019-06-26 stsp error = NULL;
2368 4e759de4 2019-06-26 stsp if (worktree) {
2369 4e759de4 2019-06-26 stsp repo_path =
2370 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2371 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2372 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2373 4e759de4 2019-06-26 stsp if (error)
2374 4e759de4 2019-06-26 stsp goto done;
2376 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2377 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2378 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2379 4e759de4 2019-06-26 stsp goto done;
2384 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2385 4e759de4 2019-06-26 stsp if (error != NULL)
2386 4e759de4 2019-06-26 stsp goto done;
2388 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2389 4e759de4 2019-06-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2390 4e759de4 2019-06-26 stsp if (error)
2391 4e759de4 2019-06-26 stsp goto done;
2393 4e759de4 2019-06-26 stsp if (do_list)
2394 4e759de4 2019-06-26 stsp error = list_branches(repo);
2395 4e759de4 2019-06-26 stsp else if (delref)
2396 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2398 4e759de4 2019-06-26 stsp const char *base_branch;
2399 4e759de4 2019-06-26 stsp if (argc == 1) {
2400 4e759de4 2019-06-26 stsp base_branch = worktree ?
2401 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2402 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2404 4e759de4 2019-06-26 stsp base_branch = argv[1];
2405 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2409 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2410 d0eebce4 2019-03-11 stsp if (worktree)
2411 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2412 d0eebce4 2019-03-11 stsp free(cwd);
2413 d0eebce4 2019-03-11 stsp free(repo_path);
2414 d00136be 2019-03-26 stsp return error;
2417 d00136be 2019-03-26 stsp __dead static void
2418 d00136be 2019-03-26 stsp usage_add(void)
2420 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2424 d00136be 2019-03-26 stsp static const struct got_error *
2425 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2427 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2428 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2429 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2430 1dd54920 2019-05-11 stsp char *cwd = NULL;
2431 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2432 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2435 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2437 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2438 d00136be 2019-03-26 stsp switch (ch) {
2440 d00136be 2019-03-26 stsp usage_add();
2441 d00136be 2019-03-26 stsp /* NOTREACHED */
2445 d00136be 2019-03-26 stsp argc -= optind;
2446 d00136be 2019-03-26 stsp argv += optind;
2448 723c305c 2019-05-11 jcs if (argc < 1)
2449 d00136be 2019-03-26 stsp usage_add();
2451 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2452 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2453 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2454 723c305c 2019-05-11 jcs if (path == NULL) {
2455 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2458 1dd54920 2019-05-11 stsp free(path);
2461 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2462 d00136be 2019-03-26 stsp if (cwd == NULL) {
2463 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2464 d00136be 2019-03-26 stsp goto done;
2467 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2468 d00136be 2019-03-26 stsp if (error)
2469 d00136be 2019-03-26 stsp goto done;
2471 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2472 031a5338 2019-03-26 stsp if (error != NULL)
2473 031a5338 2019-03-26 stsp goto done;
2475 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2476 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2477 d00136be 2019-03-26 stsp if (error)
2478 d00136be 2019-03-26 stsp goto done;
2480 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2481 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2482 723c305c 2019-05-11 jcs if (path == NULL) {
2483 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2487 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2488 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2489 1dd54920 2019-05-11 stsp if (error) {
2490 1dd54920 2019-05-11 stsp free(path);
2494 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2495 1dd54920 2019-05-11 stsp NULL, repo);
2498 031a5338 2019-03-26 stsp got_repo_close(repo);
2499 d00136be 2019-03-26 stsp if (worktree)
2500 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2501 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2502 1dd54920 2019-05-11 stsp free((char *)pe->path);
2503 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2504 2ec1f75b 2019-03-26 stsp free(cwd);
2505 2ec1f75b 2019-03-26 stsp return error;
2508 2ec1f75b 2019-03-26 stsp __dead static void
2509 648e4ef7 2019-07-09 stsp usage_remove(void)
2511 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2515 2ec1f75b 2019-03-26 stsp static const struct got_error *
2516 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2518 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2519 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2520 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2521 17ed4618 2019-06-02 stsp char *cwd = NULL;
2522 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2523 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2524 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2526 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2528 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2529 2ec1f75b 2019-03-26 stsp switch (ch) {
2531 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2534 2ec1f75b 2019-03-26 stsp usage_add();
2535 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2539 2ec1f75b 2019-03-26 stsp argc -= optind;
2540 2ec1f75b 2019-03-26 stsp argv += optind;
2542 17ed4618 2019-06-02 stsp if (argc < 1)
2543 648e4ef7 2019-07-09 stsp usage_remove();
2545 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2546 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2547 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2548 17ed4618 2019-06-02 stsp if (path == NULL) {
2549 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2550 17ed4618 2019-06-02 stsp goto done;
2552 17ed4618 2019-06-02 stsp free(path);
2555 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2556 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2557 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2558 2ec1f75b 2019-03-26 stsp goto done;
2560 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2561 2ec1f75b 2019-03-26 stsp if (error)
2562 2ec1f75b 2019-03-26 stsp goto done;
2564 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2566 2ec1f75b 2019-03-26 stsp goto done;
2568 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2569 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2570 2ec1f75b 2019-03-26 stsp if (error)
2571 2ec1f75b 2019-03-26 stsp goto done;
2573 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2574 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2575 17ed4618 2019-06-02 stsp if (path == NULL) {
2576 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2577 17ed4618 2019-06-02 stsp goto done;
2580 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2581 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2582 17ed4618 2019-06-02 stsp if (error) {
2583 17ed4618 2019-06-02 stsp free(path);
2584 17ed4618 2019-06-02 stsp goto done;
2587 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2588 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2589 a129376b 2019-03-28 stsp if (error)
2590 a129376b 2019-03-28 stsp goto done;
2593 a129376b 2019-03-28 stsp got_repo_close(repo);
2594 a129376b 2019-03-28 stsp if (worktree)
2595 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2596 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2597 17ed4618 2019-06-02 stsp free((char *)pe->path);
2598 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2599 a129376b 2019-03-28 stsp free(cwd);
2600 a129376b 2019-03-28 stsp return error;
2603 a129376b 2019-03-28 stsp __dead static void
2604 a129376b 2019-03-28 stsp usage_revert(void)
2606 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2610 a129376b 2019-03-28 stsp static void
2611 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2613 a129376b 2019-03-28 stsp while (path[0] == '/')
2615 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2618 a129376b 2019-03-28 stsp static const struct got_error *
2619 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2621 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2622 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2623 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2624 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2625 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2626 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2627 e20a8b6f 2019-06-04 stsp int ch, i;
2629 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2631 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2632 a129376b 2019-03-28 stsp switch (ch) {
2634 a129376b 2019-03-28 stsp usage_revert();
2635 a129376b 2019-03-28 stsp /* NOTREACHED */
2639 a129376b 2019-03-28 stsp argc -= optind;
2640 a129376b 2019-03-28 stsp argv += optind;
2642 e20a8b6f 2019-06-04 stsp if (argc < 1)
2643 a129376b 2019-03-28 stsp usage_revert();
2645 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
2646 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
2647 e20a8b6f 2019-06-04 stsp if (path == NULL) {
2648 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
2649 e20a8b6f 2019-06-04 stsp goto done;
2652 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
2653 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2654 e20a8b6f 2019-06-04 stsp if (error) {
2655 e20a8b6f 2019-06-04 stsp free(path);
2656 e20a8b6f 2019-06-04 stsp goto done;
2660 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2661 a129376b 2019-03-28 stsp if (cwd == NULL) {
2662 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2663 a129376b 2019-03-28 stsp goto done;
2665 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2666 2ec1f75b 2019-03-26 stsp if (error)
2667 2ec1f75b 2019-03-26 stsp goto done;
2669 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2670 a129376b 2019-03-28 stsp if (error != NULL)
2671 a129376b 2019-03-28 stsp goto done;
2673 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2674 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2675 a129376b 2019-03-28 stsp if (error)
2676 a129376b 2019-03-28 stsp goto done;
2678 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
2679 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2680 a129376b 2019-03-28 stsp if (error)
2681 a129376b 2019-03-28 stsp goto done;
2684 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2685 2ec1f75b 2019-03-26 stsp if (worktree)
2686 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2687 2ec1f75b 2019-03-26 stsp free(path);
2688 d00136be 2019-03-26 stsp free(cwd);
2689 6bad629b 2019-02-04 stsp return error;
2692 c4296144 2019-05-09 stsp __dead static void
2693 c4296144 2019-05-09 stsp usage_commit(void)
2695 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2700 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2703 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2704 33ad4cbe 2019-05-12 jcs int st = -1;
2706 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2707 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2708 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2710 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2712 33ad4cbe 2019-05-12 jcs goto doneediting;
2714 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2715 33ad4cbe 2019-05-12 jcs _exit(127);
2718 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2719 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2722 33ad4cbe 2019-05-12 jcs doneediting:
2723 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2724 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2725 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2727 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2728 33ad4cbe 2019-05-12 jcs errno = EINTR;
2732 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2735 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2736 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2737 e2ba3d07 2019-05-13 stsp const char *editor;
2738 e0870e44 2019-05-13 stsp const char *worktree_path;
2739 76d98825 2019-06-03 stsp const char *branch_name;
2740 314a6357 2019-05-13 stsp const char *repo_path;
2741 e0870e44 2019-05-13 stsp char *logmsg_path;
2745 33ad4cbe 2019-05-12 jcs static const struct got_error *
2746 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2749 76d98825 2019-06-03 stsp char *initial_content = NULL;
2750 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2751 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2752 e0870e44 2019-05-13 stsp char *template = NULL;
2753 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2754 33ad4cbe 2019-05-12 jcs char buf[1024];
2755 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2757 33ad4cbe 2019-05-12 jcs size_t len;
2758 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2760 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2761 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2762 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2763 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2764 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2765 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2766 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2767 33ad4cbe 2019-05-12 jcs return NULL;
2770 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2771 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
2773 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
2774 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
2775 76d98825 2019-06-03 stsp a->branch_name) == -1)
2776 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2778 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2780 e0870e44 2019-05-13 stsp goto done;
2782 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2784 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2785 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2786 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
2787 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
2788 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
2792 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2793 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2797 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2798 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2802 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2803 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2807 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2808 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2809 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2810 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2811 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2812 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2816 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2817 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2818 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2819 fcde04d9 2019-05-13 stsp goto done;
2821 cc79381d 2019-05-22 stsp (*logmsg)[0] = '\0';
2824 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2825 d4592c7c 2019-05-22 stsp if (fp == NULL) {
2826 d4592c7c 2019-05-22 stsp err = got_error_from_errno("fopen");
2827 d4592c7c 2019-05-22 stsp goto done;
2829 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2830 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2831 e0870e44 2019-05-13 stsp content_changed = 1;
2832 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2833 78527a0a 2019-05-22 stsp continue; /* remove comments and leading empty lines */
2834 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2836 33ad4cbe 2019-05-12 jcs fclose(fp);
2838 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2839 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2843 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2844 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2845 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2846 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2847 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2848 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2852 76d98825 2019-06-03 stsp free(initial_content);
2853 e0870e44 2019-05-13 stsp free(template);
2855 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2856 314a6357 2019-05-13 stsp if (err == NULL)
2857 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2858 33ad4cbe 2019-05-12 jcs return err;
2861 c4296144 2019-05-09 stsp static const struct got_error *
2862 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2864 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2865 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2866 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2867 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2868 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2869 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2870 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2871 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2872 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2875 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2876 c4296144 2019-05-09 stsp switch (ch) {
2878 c4296144 2019-05-09 stsp logmsg = optarg;
2881 c4296144 2019-05-09 stsp usage_commit();
2882 c4296144 2019-05-09 stsp /* NOTREACHED */
2886 c4296144 2019-05-09 stsp argc -= optind;
2887 c4296144 2019-05-09 stsp argv += optind;
2889 c4296144 2019-05-09 stsp if (argc == 1) {
2890 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2891 c4296144 2019-05-09 stsp if (path == NULL) {
2892 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2893 c4296144 2019-05-09 stsp goto done;
2895 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2896 c4296144 2019-05-09 stsp } else if (argc != 0)
2897 c4296144 2019-05-09 stsp usage_commit();
2899 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2900 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2901 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2902 35bd8fed 2019-05-09 stsp goto done;
2905 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2906 c4296144 2019-05-09 stsp if (cwd == NULL) {
2907 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2908 c4296144 2019-05-09 stsp goto done;
2910 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2911 c4296144 2019-05-09 stsp if (error)
2912 c4296144 2019-05-09 stsp goto done;
2914 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2915 c4296144 2019-05-09 stsp if (error != NULL)
2916 c4296144 2019-05-09 stsp goto done;
2919 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2920 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2922 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2923 314a6357 2019-05-13 stsp error = get_editor(&editor);
2925 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2926 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2927 c4296144 2019-05-09 stsp if (error)
2928 c4296144 2019-05-09 stsp goto done;
2930 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2931 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2932 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2933 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2934 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2935 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
2936 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2937 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
2938 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2939 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2940 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2941 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2942 e0870e44 2019-05-13 stsp if (error) {
2943 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2944 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2945 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2946 c4296144 2019-05-09 stsp goto done;
2949 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2950 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2952 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2953 c4296144 2019-05-09 stsp if (error)
2954 c4296144 2019-05-09 stsp goto done;
2955 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
2958 c4296144 2019-05-09 stsp got_repo_close(repo);
2959 c4296144 2019-05-09 stsp if (worktree)
2960 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2961 c4296144 2019-05-09 stsp free(path);
2962 c4296144 2019-05-09 stsp free(cwd);
2963 c4296144 2019-05-09 stsp free(id_str);
2964 e2ba3d07 2019-05-13 stsp free(editor);
2965 234035bc 2019-06-01 stsp return error;
2968 234035bc 2019-06-01 stsp __dead static void
2969 234035bc 2019-06-01 stsp usage_cherrypick(void)
2971 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2975 234035bc 2019-06-01 stsp static const struct got_error *
2976 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
2978 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
2979 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
2980 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
2981 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
2982 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
2983 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
2984 234035bc 2019-06-01 stsp struct got_object_qid *pid;
2985 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
2986 234035bc 2019-06-01 stsp int ch, did_something = 0;
2988 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2989 234035bc 2019-06-01 stsp switch (ch) {
2991 234035bc 2019-06-01 stsp usage_cherrypick();
2992 234035bc 2019-06-01 stsp /* NOTREACHED */
2996 234035bc 2019-06-01 stsp argc -= optind;
2997 234035bc 2019-06-01 stsp argv += optind;
2999 234035bc 2019-06-01 stsp if (argc != 1)
3000 234035bc 2019-06-01 stsp usage_cherrypick();
3002 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3003 234035bc 2019-06-01 stsp if (cwd == NULL) {
3004 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3005 234035bc 2019-06-01 stsp goto done;
3007 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3008 234035bc 2019-06-01 stsp if (error)
3009 234035bc 2019-06-01 stsp goto done;
3011 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3012 234035bc 2019-06-01 stsp if (error != NULL)
3013 234035bc 2019-06-01 stsp goto done;
3015 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3016 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
3017 234035bc 2019-06-01 stsp if (error)
3018 234035bc 2019-06-01 stsp goto done;
3020 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3021 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3022 234035bc 2019-06-01 stsp if (error != NULL) {
3023 234035bc 2019-06-01 stsp struct got_reference *ref;
3024 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3025 234035bc 2019-06-01 stsp goto done;
3026 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3027 234035bc 2019-06-01 stsp if (error != NULL)
3028 234035bc 2019-06-01 stsp goto done;
3029 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3030 234035bc 2019-06-01 stsp got_ref_close(ref);
3031 234035bc 2019-06-01 stsp if (error != NULL)
3032 234035bc 2019-06-01 stsp goto done;
3034 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3035 234035bc 2019-06-01 stsp if (error)
3036 234035bc 2019-06-01 stsp goto done;
3038 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3039 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3040 234035bc 2019-06-01 stsp if (error != NULL)
3041 234035bc 2019-06-01 stsp goto done;
3043 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3044 234035bc 2019-06-01 stsp if (error) {
3045 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3046 234035bc 2019-06-01 stsp goto done;
3047 234035bc 2019-06-01 stsp error = NULL;
3049 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3050 234035bc 2019-06-01 stsp goto done;
3053 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3054 234035bc 2019-06-01 stsp if (error)
3055 234035bc 2019-06-01 stsp goto done;
3056 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3057 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3058 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3060 234035bc 2019-06-01 stsp if (error != NULL)
3061 234035bc 2019-06-01 stsp goto done;
3063 234035bc 2019-06-01 stsp if (did_something)
3064 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3066 234035bc 2019-06-01 stsp if (commit)
3067 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3068 234035bc 2019-06-01 stsp free(commit_id_str);
3069 234035bc 2019-06-01 stsp if (head_ref)
3070 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3071 234035bc 2019-06-01 stsp if (worktree)
3072 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3074 234035bc 2019-06-01 stsp got_repo_close(repo);
3075 c4296144 2019-05-09 stsp return error;
3078 5ef14e63 2019-06-02 stsp __dead static void
3079 5ef14e63 2019-06-02 stsp usage_backout(void)
3081 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3085 5ef14e63 2019-06-02 stsp static const struct got_error *
3086 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3088 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3089 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3090 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3091 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3092 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3093 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3094 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3095 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3096 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3098 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3099 5ef14e63 2019-06-02 stsp switch (ch) {
3101 5ef14e63 2019-06-02 stsp usage_backout();
3102 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3106 5ef14e63 2019-06-02 stsp argc -= optind;
3107 5ef14e63 2019-06-02 stsp argv += optind;
3109 5ef14e63 2019-06-02 stsp if (argc != 1)
3110 5ef14e63 2019-06-02 stsp usage_backout();
3112 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3113 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3114 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3115 5ef14e63 2019-06-02 stsp goto done;
3117 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3118 5ef14e63 2019-06-02 stsp if (error)
3119 5ef14e63 2019-06-02 stsp goto done;
3121 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3122 5ef14e63 2019-06-02 stsp if (error != NULL)
3123 5ef14e63 2019-06-02 stsp goto done;
3125 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3126 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
3127 5ef14e63 2019-06-02 stsp if (error)
3128 5ef14e63 2019-06-02 stsp goto done;
3130 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3131 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3132 5ef14e63 2019-06-02 stsp if (error != NULL) {
3133 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3134 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3135 5ef14e63 2019-06-02 stsp goto done;
3136 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3137 5ef14e63 2019-06-02 stsp if (error != NULL)
3138 5ef14e63 2019-06-02 stsp goto done;
3139 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3140 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3141 5ef14e63 2019-06-02 stsp if (error != NULL)
3142 5ef14e63 2019-06-02 stsp goto done;
3144 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3145 5ef14e63 2019-06-02 stsp if (error)
3146 5ef14e63 2019-06-02 stsp goto done;
3148 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3149 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3150 5ef14e63 2019-06-02 stsp if (error != NULL)
3151 5ef14e63 2019-06-02 stsp goto done;
3153 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3154 5ef14e63 2019-06-02 stsp if (error)
3155 5ef14e63 2019-06-02 stsp goto done;
3157 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3158 5ef14e63 2019-06-02 stsp if (error)
3159 5ef14e63 2019-06-02 stsp goto done;
3160 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3161 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3162 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3163 5ef14e63 2019-06-02 stsp goto done;
3166 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3167 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3168 5ef14e63 2019-06-02 stsp if (error != NULL)
3169 5ef14e63 2019-06-02 stsp goto done;
3171 5ef14e63 2019-06-02 stsp if (did_something)
3172 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3174 5ef14e63 2019-06-02 stsp if (commit)
3175 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3176 5ef14e63 2019-06-02 stsp free(commit_id_str);
3177 5ef14e63 2019-06-02 stsp if (head_ref)
3178 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3179 5ef14e63 2019-06-02 stsp if (worktree)
3180 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3182 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3183 5ef14e63 2019-06-02 stsp return error;