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>
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 63219cd2 2019-01-04 stsp #include "got_privsep.h"
44 5c860e29 2018-03-12 stsp #ifndef nitems
45 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
52 99437157 2018-11-11 stsp catch_sigint(int signo)
54 99437157 2018-11-11 stsp sigint_received = 1;
58 99437157 2018-11-11 stsp catch_sigpipe(int signo)
60 99437157 2018-11-11 stsp sigpipe_received = 1;
64 5c860e29 2018-03-12 stsp struct cmd {
65 5c860e29 2018-03-12 stsp const char *cmd_name;
66 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
67 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
68 46a0db7d 2018-03-12 stsp const char *cmd_descr;
71 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
73 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
76 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
77 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
78 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
79 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
80 d00136be 2019-03-26 stsp __dead static void usage_add(void);
81 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
82 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
83 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
86 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
89 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
90 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
92 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
93 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
94 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
95 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
96 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
98 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
99 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
100 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
101 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
102 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
103 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
104 1b6b95a8 2018-03-12 stsp "show repository history" },
105 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
106 b00d56cd 2018-04-01 stsp "compare files and directories" },
107 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
108 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
109 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
110 a67e2392 2019-03-26 stsp "list files and directories in repository" },
111 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
112 1b6b95a8 2018-03-12 stsp "show modification status of files" },
113 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
114 d0eebce4 2019-03-11 stsp "manage references in repository" },
115 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
116 d00136be 2019-03-26 stsp "add a new file to version control" },
117 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
118 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
119 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
120 a129376b 2019-03-28 stsp "revert uncommitted changes" },
121 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
122 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
126 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
128 5c860e29 2018-03-12 stsp struct cmd *cmd;
129 5c860e29 2018-03-12 stsp unsigned int i;
131 1b6b95a8 2018-03-12 stsp int hflag = 0;
133 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
135 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
136 5c860e29 2018-03-12 stsp switch (ch) {
142 5c860e29 2018-03-12 stsp /* NOTREACHED */
146 5c860e29 2018-03-12 stsp argc -= optind;
147 5c860e29 2018-03-12 stsp argv += optind;
148 1e70621d 2018-03-27 stsp optind = 0;
150 5c860e29 2018-03-12 stsp if (argc <= 0)
153 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
154 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
156 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
157 d7d4f210 2018-03-12 stsp const struct got_error *error;
159 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
161 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
165 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
167 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
168 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
169 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
176 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
180 4ed7e80c 2018-05-20 stsp __dead static void
181 5c860e29 2018-03-12 stsp usage(void)
185 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
186 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
187 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
188 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
189 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
194 0266afb7 2019-01-04 stsp static const struct got_error *
195 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
196 d0eebce4 2019-03-11 stsp const char *worktree_path)
198 0266afb7 2019-01-04 stsp const struct got_error *error;
200 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
201 0266afb7 2019-01-04 stsp return got_error_from_errno();
203 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
204 0266afb7 2019-01-04 stsp return got_error_from_errno();
206 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
207 0266afb7 2019-01-04 stsp return got_error_from_errno();
209 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
210 0266afb7 2019-01-04 stsp if (error != NULL)
211 0266afb7 2019-01-04 stsp return error;
213 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
214 0266afb7 2019-01-04 stsp return got_error_from_errno();
216 0266afb7 2019-01-04 stsp return NULL;
219 4ed7e80c 2018-05-20 stsp __dead static void
220 c09a553d 2018-03-12 stsp usage_checkout(void)
222 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
223 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
227 92a684f4 2018-03-12 stsp static void
228 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
230 92a684f4 2018-03-12 stsp char *worktree_path = arg;
232 92a684f4 2018-03-12 stsp while (path[0] == '/')
235 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
238 99437157 2018-11-11 stsp static const struct got_error *
239 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
241 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
242 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
243 99437157 2018-11-11 stsp return NULL;
246 8069f636 2019-01-12 stsp static const struct got_error *
247 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
248 8069f636 2019-01-12 stsp struct got_repository *repo)
250 8069f636 2019-01-12 stsp const struct got_error *err;
251 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
252 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
253 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
255 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo,
256 36a38700 2019-05-10 stsp got_worktree_get_head_ref_name(worktree));
258 36a38700 2019-05-10 stsp return err;
260 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
261 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
265 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
269 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
272 8069f636 2019-01-12 stsp while (1) {
273 8069f636 2019-01-12 stsp struct got_object_id *id;
275 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
278 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
280 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
281 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
284 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
286 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
292 8069f636 2019-01-12 stsp if (id == NULL)
294 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
298 8069f636 2019-01-12 stsp if (head_ref)
299 8069f636 2019-01-12 stsp got_ref_close(head_ref);
301 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
302 8069f636 2019-01-12 stsp return err;
306 4ed7e80c 2018-05-20 stsp static const struct got_error *
307 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
309 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
310 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
311 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
312 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
313 c09a553d 2018-03-12 stsp char *repo_path = NULL;
314 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
315 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
316 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
317 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
319 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
320 0bb8a95e 2018-03-12 stsp switch (ch) {
322 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
323 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
324 8069f636 2019-01-12 stsp return got_error_from_errno();
327 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
330 2deda0b9 2019-03-07 stsp usage_checkout();
331 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
335 0bb8a95e 2018-03-12 stsp argc -= optind;
336 0bb8a95e 2018-03-12 stsp argv += optind;
338 6715a751 2018-03-16 stsp #ifndef PROFILE
339 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
340 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
341 c09a553d 2018-03-12 stsp err(1, "pledge");
343 0bb8a95e 2018-03-12 stsp if (argc == 1) {
344 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
345 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
346 76089277 2018-04-01 stsp if (repo_path == NULL)
347 76089277 2018-04-01 stsp return got_error_from_errno();
348 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
349 76089277 2018-04-01 stsp if (cwd == NULL) {
350 76089277 2018-04-01 stsp error = got_error_from_errno();
353 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
354 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
356 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
357 76089277 2018-04-01 stsp if (base == NULL) {
358 76089277 2018-04-01 stsp error = got_error_from_errno();
361 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
362 c09a553d 2018-03-12 stsp if (dotgit)
363 c09a553d 2018-03-12 stsp *dotgit = '\0';
364 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
365 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
370 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
371 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
372 76089277 2018-04-01 stsp if (repo_path == NULL) {
373 76089277 2018-04-01 stsp error = got_error_from_errno();
376 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
377 76089277 2018-04-01 stsp if (worktree_path == NULL) {
378 76089277 2018-04-01 stsp error = got_error_from_errno();
382 c09a553d 2018-03-12 stsp usage_checkout();
384 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
385 c09a553d 2018-03-12 stsp if (error != NULL)
388 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
392 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
393 c09a553d 2018-03-12 stsp if (error != NULL)
396 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
397 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
400 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
401 c09a553d 2018-03-12 stsp if (error != NULL)
404 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
405 e5dc7198 2018-12-29 stsp path_prefix);
406 e5dc7198 2018-12-29 stsp if (error != NULL)
408 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
409 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
413 8069f636 2019-01-12 stsp if (commit_id_str) {
414 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
415 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
416 8069f636 2019-01-12 stsp commit_id_str);
417 8069f636 2019-01-12 stsp if (error != NULL)
419 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
420 8069f636 2019-01-12 stsp if (error != NULL) {
421 8069f636 2019-01-12 stsp free(commit_id);
424 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
425 8069f636 2019-01-12 stsp commit_id);
426 8069f636 2019-01-12 stsp free(commit_id);
431 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
432 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
433 c09a553d 2018-03-12 stsp if (error != NULL)
436 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
439 8069f636 2019-01-12 stsp free(commit_id_str);
440 76089277 2018-04-01 stsp free(repo_path);
441 507dc3bb 2018-12-29 stsp free(worktree_path);
442 507dc3bb 2018-12-29 stsp return error;
445 507dc3bb 2018-12-29 stsp __dead static void
446 507dc3bb 2018-12-29 stsp usage_update(void)
448 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
449 507dc3bb 2018-12-29 stsp getprogname());
453 507dc3bb 2018-12-29 stsp static void
454 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
456 784955db 2019-01-12 stsp int *did_something = arg;
458 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
461 1545c615 2019-02-10 stsp *did_something = 1;
462 507dc3bb 2018-12-29 stsp while (path[0] == '/')
464 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
467 be7061eb 2018-12-30 stsp static const struct got_error *
468 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
470 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
471 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
472 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
473 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
474 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
475 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
476 784955db 2019-01-12 stsp int ch, did_something = 0;
478 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
479 507dc3bb 2018-12-29 stsp switch (ch) {
481 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
482 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
483 9c4b8182 2019-01-02 stsp return got_error_from_errno();
486 2deda0b9 2019-03-07 stsp usage_update();
487 507dc3bb 2018-12-29 stsp /* NOTREACHED */
491 507dc3bb 2018-12-29 stsp argc -= optind;
492 507dc3bb 2018-12-29 stsp argv += optind;
494 507dc3bb 2018-12-29 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 507dc3bb 2018-12-29 stsp err(1, "pledge");
499 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
500 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
501 c4cdcb68 2019-04-03 stsp error = got_error_from_errno();
504 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
508 507dc3bb 2018-12-29 stsp if (argc == 0) {
509 c4cdcb68 2019-04-03 stsp path = strdup("");
510 c4cdcb68 2019-04-03 stsp if (path == NULL) {
511 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
514 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
515 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
519 507dc3bb 2018-12-29 stsp usage_update();
521 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
522 507dc3bb 2018-12-29 stsp if (error != NULL)
525 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
526 8c02d095 2019-02-05 stsp got_worktree_get_root_path(worktree));
530 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
531 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
532 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
533 507dc3bb 2018-12-29 stsp if (error != NULL)
535 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
536 9c4b8182 2019-01-02 stsp if (error != NULL)
538 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
539 507dc3bb 2018-12-29 stsp if (error != NULL)
542 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
543 507dc3bb 2018-12-29 stsp commit_id_str);
544 507dc3bb 2018-12-29 stsp if (error != NULL)
548 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
549 be7061eb 2018-12-30 stsp if (error != NULL)
552 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
553 507dc3bb 2018-12-29 stsp commit_id) != 0) {
554 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
555 507dc3bb 2018-12-29 stsp commit_id);
560 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
561 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
562 507dc3bb 2018-12-29 stsp if (error != NULL)
565 784955db 2019-01-12 stsp if (did_something)
566 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
568 784955db 2019-01-12 stsp printf("Already up-to-date\n");
570 c09a553d 2018-03-12 stsp free(worktree_path);
571 c4cdcb68 2019-04-03 stsp free(path);
572 507dc3bb 2018-12-29 stsp free(commit_id);
573 9c4b8182 2019-01-02 stsp free(commit_id_str);
574 c09a553d 2018-03-12 stsp return error;
577 f42b1b34 2018-03-12 stsp static const struct got_error *
578 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
579 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
581 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
582 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
583 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
584 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
586 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
587 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
589 79109fed 2018-03-27 stsp return err;
591 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
592 79f35eb3 2018-06-11 stsp if (qid != NULL) {
593 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
595 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
597 79109fed 2018-03-27 stsp return err;
599 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
600 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
601 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
603 0f2b3dca 2018-12-22 stsp return err;
605 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
607 79109fed 2018-03-27 stsp return err;
610 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
614 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
615 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
618 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
619 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
620 0f2b3dca 2018-12-22 stsp free(id_str1);
621 0f2b3dca 2018-12-22 stsp free(id_str2);
622 79109fed 2018-03-27 stsp return err;
625 4bb494d5 2018-06-16 stsp static char *
626 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
628 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
629 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
635 79109fed 2018-03-27 stsp static const struct got_error *
636 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
637 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
638 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
640 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
641 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
642 6c281f94 2018-06-11 stsp char datebuf[26];
643 45d799e2 2018-12-23 stsp time_t committer_time;
644 45d799e2 2018-12-23 stsp const char *author, *committer;
645 199a4027 2019-02-02 stsp char *refs_str = NULL;
646 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
648 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
650 199a4027 2019-02-02 stsp const char *name;
651 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
653 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
654 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
656 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
658 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
660 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
662 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
664 199a4027 2019-02-02 stsp s = refs_str;
665 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
666 199a4027 2019-02-02 stsp name) == -1) {
667 199a4027 2019-02-02 stsp err = got_error_from_errno();
673 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
675 8bf5b3c9 2018-03-17 stsp return err;
677 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
678 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
679 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
680 832c249c 2018-06-10 stsp free(id_str);
681 d3d493d7 2019-02-21 stsp id_str = NULL;
682 d3d493d7 2019-02-21 stsp free(refs_str);
683 d3d493d7 2019-02-21 stsp refs_str = NULL;
684 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
685 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
686 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
687 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
688 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
689 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
690 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
691 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
692 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
693 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
694 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
696 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
697 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
698 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
700 a0603db2 2018-06-10 stsp return err;
701 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
702 a0603db2 2018-06-10 stsp free(id_str);
706 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
707 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
708 832c249c 2018-06-10 stsp return got_error_from_errno();
710 621015ac 2018-07-23 stsp logmsg = logmsg0;
712 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
714 832c249c 2018-06-10 stsp printf(" %s\n", line);
715 832c249c 2018-06-10 stsp } while (line);
716 621015ac 2018-07-23 stsp free(logmsg0);
718 971751ac 2018-03-27 stsp if (show_patch) {
719 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
720 971751ac 2018-03-27 stsp if (err == 0)
721 971751ac 2018-03-27 stsp printf("\n");
724 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
725 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
726 07862c20 2018-09-15 stsp return err;
729 f42b1b34 2018-03-12 stsp static const struct got_error *
730 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
731 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
732 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
734 f42b1b34 2018-03-12 stsp const struct got_error *err;
735 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
737 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
738 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
740 f42b1b34 2018-03-12 stsp return err;
741 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
744 31cedeaf 2018-09-15 stsp while (1) {
745 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
746 372ccdbb 2018-06-10 stsp struct got_object_id *id;
748 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
751 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
753 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
754 9ba79e04 2018-06-11 stsp err = NULL;
757 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
759 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
765 b43fbaa0 2018-06-11 stsp if (id == NULL)
768 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
771 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
773 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
774 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
778 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
779 f42b1b34 2018-03-12 stsp return err;
782 4ed7e80c 2018-05-20 stsp __dead static void
783 6f3d1eb0 2018-03-12 stsp usage_log(void)
785 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
786 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
790 4ed7e80c 2018-05-20 stsp static const struct got_error *
791 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
793 f42b1b34 2018-03-12 stsp const struct got_error *error;
794 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
795 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
796 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
797 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
798 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
799 d142fc45 2018-04-01 stsp char *start_commit = NULL;
800 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
801 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
802 64a96a6d 2018-04-01 stsp const char *errstr;
803 199a4027 2019-02-02 stsp struct got_reflist_head refs;
805 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
807 6715a751 2018-03-16 stsp #ifndef PROFILE
808 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
811 f42b1b34 2018-03-12 stsp err(1, "pledge");
814 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
815 79109fed 2018-03-27 stsp switch (ch) {
817 79109fed 2018-03-27 stsp show_patch = 1;
820 d142fc45 2018-04-01 stsp start_commit = optarg;
823 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
825 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
826 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
829 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
830 64a96a6d 2018-04-01 stsp if (errstr != NULL)
831 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
834 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
837 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
838 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
839 04ca23f4 2018-07-16 stsp err(1, "-r option");
842 2deda0b9 2019-03-07 stsp usage_log();
843 79109fed 2018-03-27 stsp /* NOTREACHED */
847 79109fed 2018-03-27 stsp argc -= optind;
848 79109fed 2018-03-27 stsp argv += optind;
850 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
851 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
852 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
856 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
857 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
859 cffc0aa4 2019-02-05 stsp error = NULL;
861 e7301579 2019-03-18 stsp if (argc == 0) {
862 cbd1af7a 2019-03-18 stsp path = strdup("");
863 e7301579 2019-03-18 stsp if (path == NULL) {
864 e7301579 2019-03-18 stsp error = got_error_from_errno();
867 e7301579 2019-03-18 stsp } else if (argc == 1) {
868 e7301579 2019-03-18 stsp if (worktree) {
869 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
874 e7301579 2019-03-18 stsp path = strdup(argv[0]);
875 e7301579 2019-03-18 stsp if (path == NULL) {
876 e7301579 2019-03-18 stsp error = got_error_from_errno();
881 cbd1af7a 2019-03-18 stsp usage_log();
883 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
884 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
885 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
886 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
890 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
891 d7d4f210 2018-03-12 stsp if (error != NULL)
894 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
895 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
899 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
900 3235492e 2018-04-01 stsp struct got_reference *head_ref;
901 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
902 3235492e 2018-04-01 stsp if (error != NULL)
903 3235492e 2018-04-01 stsp return error;
904 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
905 3235492e 2018-04-01 stsp got_ref_close(head_ref);
906 3235492e 2018-04-01 stsp if (error != NULL)
907 3235492e 2018-04-01 stsp return error;
908 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
910 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
911 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
912 3235492e 2018-04-01 stsp if (error == NULL) {
913 1caad61b 2019-02-01 stsp int obj_type;
914 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
915 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
916 d1f2edc9 2018-06-13 stsp if (error != NULL)
918 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
919 1caad61b 2019-02-01 stsp if (error != NULL)
921 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
922 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
923 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
924 1caad61b 2019-02-01 stsp if (error != NULL)
926 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
927 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
928 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
929 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
933 1caad61b 2019-02-01 stsp id = got_object_id_dup(
934 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
935 1caad61b 2019-02-01 stsp if (id == NULL)
936 1caad61b 2019-02-01 stsp error = got_error_from_errno();
937 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
940 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
941 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
944 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
945 d1f2edc9 2018-06-13 stsp if (error != NULL)
948 15a94983 2018-12-23 stsp if (commit == NULL) {
949 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
950 d1f2edc9 2018-06-13 stsp start_commit);
951 d1f2edc9 2018-06-13 stsp if (error != NULL)
952 d1f2edc9 2018-06-13 stsp return error;
955 d7d4f210 2018-03-12 stsp if (error != NULL)
958 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
959 04ca23f4 2018-07-16 stsp if (error != NULL)
961 04ca23f4 2018-07-16 stsp if (in_repo_path) {
962 04ca23f4 2018-07-16 stsp free(path);
963 04ca23f4 2018-07-16 stsp path = in_repo_path;
966 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
970 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
971 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
973 04ca23f4 2018-07-16 stsp free(path);
974 04ca23f4 2018-07-16 stsp free(repo_path);
977 cffc0aa4 2019-02-05 stsp if (worktree)
978 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
979 ad242220 2018-09-08 stsp if (repo) {
980 ad242220 2018-09-08 stsp const struct got_error *repo_error;
981 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
982 ad242220 2018-09-08 stsp if (error == NULL)
983 ad242220 2018-09-08 stsp error = repo_error;
985 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
986 8bf5b3c9 2018-03-17 stsp return error;
989 4ed7e80c 2018-05-20 stsp __dead static void
990 3f8b7d6a 2018-04-01 stsp usage_diff(void)
992 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
993 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
997 b72f483a 2019-02-05 stsp struct print_diff_arg {
998 b72f483a 2019-02-05 stsp struct got_repository *repo;
999 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1000 b72f483a 2019-02-05 stsp int diff_context;
1001 3fc0c068 2019-02-10 stsp const char *id_str;
1002 3fc0c068 2019-02-10 stsp int header_shown;
1005 4ed7e80c 2018-05-20 stsp static const struct got_error *
1006 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1007 b72f483a 2019-02-05 stsp struct got_object_id *id)
1009 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1010 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1011 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1012 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1013 b72f483a 2019-02-05 stsp char *abspath = NULL;
1014 b72f483a 2019-02-05 stsp struct stat sb;
1016 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1017 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1018 b72f483a 2019-02-05 stsp return NULL;
1020 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1021 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1022 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1023 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1026 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1027 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1029 d00136be 2019-03-26 stsp goto done;
1033 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1034 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1035 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1036 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1037 2ec1f75b 2019-03-26 stsp goto done;
1040 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1041 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1042 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1043 2ec1f75b 2019-03-26 stsp goto done;
1045 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1046 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1047 2ec1f75b 2019-03-26 stsp goto done;
1050 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1052 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1055 b72f483a 2019-02-05 stsp if (blob1)
1056 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1057 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1058 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1059 b72f483a 2019-02-05 stsp free(abspath);
1060 927df6b7 2019-02-10 stsp return err;
1063 927df6b7 2019-02-10 stsp static const struct got_error *
1064 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1066 b00d56cd 2018-04-01 stsp const struct got_error *error;
1067 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1068 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1069 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1070 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1071 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1072 15a94983 2018-12-23 stsp int type1, type2;
1073 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1074 c0cc5c62 2018-10-18 stsp const char *errstr;
1075 927df6b7 2019-02-10 stsp char *path = NULL;
1077 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1078 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1079 25eccc22 2019-01-04 stsp NULL) == -1)
1080 b00d56cd 2018-04-01 stsp err(1, "pledge");
1083 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1084 b00d56cd 2018-04-01 stsp switch (ch) {
1086 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1087 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1088 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1091 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1092 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1093 b72f483a 2019-02-05 stsp err(1, "-r option");
1096 2deda0b9 2019-03-07 stsp usage_diff();
1097 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1101 b00d56cd 2018-04-01 stsp argc -= optind;
1102 b00d56cd 2018-04-01 stsp argv += optind;
1104 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1105 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1106 b72f483a 2019-02-05 stsp error = got_error_from_errno();
1107 b72f483a 2019-02-05 stsp goto done;
1109 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1110 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1111 927df6b7 2019-02-10 stsp goto done;
1112 927df6b7 2019-02-10 stsp if (argc <= 1) {
1113 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1114 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1115 927df6b7 2019-02-10 stsp goto done;
1117 b72f483a 2019-02-05 stsp if (repo_path)
1119 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1120 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1121 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1122 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1123 927df6b7 2019-02-10 stsp goto done;
1125 927df6b7 2019-02-10 stsp if (argc == 1) {
1126 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1128 927df6b7 2019-02-10 stsp if (error)
1129 927df6b7 2019-02-10 stsp goto done;
1131 927df6b7 2019-02-10 stsp path = strdup("");
1132 927df6b7 2019-02-10 stsp if (path == NULL) {
1133 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1134 927df6b7 2019-02-10 stsp goto done;
1137 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1138 15a94983 2018-12-23 stsp id_str1 = argv[0];
1139 15a94983 2018-12-23 stsp id_str2 = argv[1];
1141 b00d56cd 2018-04-01 stsp usage_diff();
1143 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1144 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1145 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1146 b72f483a 2019-02-05 stsp return got_error_from_errno();
1149 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1150 76089277 2018-04-01 stsp free(repo_path);
1151 b00d56cd 2018-04-01 stsp if (error != NULL)
1152 b00d56cd 2018-04-01 stsp goto done;
1154 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1155 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1156 c02c541e 2019-03-29 stsp if (error)
1157 c02c541e 2019-03-29 stsp goto done;
1159 b72f483a 2019-02-05 stsp if (worktree) {
1160 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1161 b72f483a 2019-02-05 stsp char *id_str;
1162 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1163 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1164 b72f483a 2019-02-05 stsp if (error)
1165 b72f483a 2019-02-05 stsp goto done;
1166 b72f483a 2019-02-05 stsp arg.repo = repo;
1167 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1168 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1169 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1170 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1172 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1173 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1174 b72f483a 2019-02-05 stsp free(id_str);
1175 b72f483a 2019-02-05 stsp goto done;
1178 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1179 6402fb3c 2018-09-15 stsp if (error)
1180 b00d56cd 2018-04-01 stsp goto done;
1182 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1183 6402fb3c 2018-09-15 stsp if (error)
1184 b00d56cd 2018-04-01 stsp goto done;
1186 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1187 15a94983 2018-12-23 stsp if (error)
1188 15a94983 2018-12-23 stsp goto done;
1190 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1191 15a94983 2018-12-23 stsp if (error)
1192 15a94983 2018-12-23 stsp goto done;
1194 15a94983 2018-12-23 stsp if (type1 != type2) {
1195 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1196 b00d56cd 2018-04-01 stsp goto done;
1199 15a94983 2018-12-23 stsp switch (type1) {
1200 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1201 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1202 54156555 2018-12-24 stsp diff_context, repo, stdout);
1204 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1205 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1206 54156555 2018-12-24 stsp diff_context, repo, stdout);
1208 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1209 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1211 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1212 c0cc5c62 2018-10-18 stsp repo, stdout);
1215 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1219 15a94983 2018-12-23 stsp free(id1);
1220 15a94983 2018-12-23 stsp free(id2);
1221 927df6b7 2019-02-10 stsp free(path);
1222 b72f483a 2019-02-05 stsp if (worktree)
1223 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1224 ad242220 2018-09-08 stsp if (repo) {
1225 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1226 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1227 ad242220 2018-09-08 stsp if (error == NULL)
1228 ad242220 2018-09-08 stsp error = repo_error;
1230 b00d56cd 2018-04-01 stsp return error;
1233 404c43c4 2018-06-21 stsp __dead static void
1234 404c43c4 2018-06-21 stsp usage_blame(void)
1236 9270e621 2019-02-05 stsp fprintf(stderr,
1237 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1238 404c43c4 2018-06-21 stsp getprogname());
1242 404c43c4 2018-06-21 stsp static const struct got_error *
1243 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1245 404c43c4 2018-06-21 stsp const struct got_error *error;
1246 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1247 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1248 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1249 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1250 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1253 404c43c4 2018-06-21 stsp #ifndef PROFILE
1254 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1255 36e2fb66 2019-01-04 stsp NULL) == -1)
1256 404c43c4 2018-06-21 stsp err(1, "pledge");
1259 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1260 404c43c4 2018-06-21 stsp switch (ch) {
1262 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1265 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1266 66bea077 2018-08-02 stsp if (repo_path == NULL)
1267 66bea077 2018-08-02 stsp err(1, "-r option");
1270 2deda0b9 2019-03-07 stsp usage_blame();
1271 404c43c4 2018-06-21 stsp /* NOTREACHED */
1275 404c43c4 2018-06-21 stsp argc -= optind;
1276 404c43c4 2018-06-21 stsp argv += optind;
1278 a39318fd 2018-08-02 stsp if (argc == 1)
1279 404c43c4 2018-06-21 stsp path = argv[0];
1281 404c43c4 2018-06-21 stsp usage_blame();
1283 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1284 66bea077 2018-08-02 stsp if (cwd == NULL) {
1285 66bea077 2018-08-02 stsp error = got_error_from_errno();
1286 66bea077 2018-08-02 stsp goto done;
1288 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1289 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1290 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1291 66bea077 2018-08-02 stsp goto done;
1293 0c06baac 2019-02-05 stsp error = NULL;
1294 0c06baac 2019-02-05 stsp if (worktree) {
1295 0c06baac 2019-02-05 stsp repo_path =
1296 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1297 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1298 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1299 0c06baac 2019-02-05 stsp if (error)
1300 0c06baac 2019-02-05 stsp goto done;
1302 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1303 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1304 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1305 0c06baac 2019-02-05 stsp goto done;
1310 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1311 c02c541e 2019-03-29 stsp if (error != NULL)
1312 36e2fb66 2019-01-04 stsp goto done;
1314 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1315 c02c541e 2019-03-29 stsp if (error)
1316 404c43c4 2018-06-21 stsp goto done;
1318 0c06baac 2019-02-05 stsp if (worktree) {
1319 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1320 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1321 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1322 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1323 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1324 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1325 6efaaa2d 2019-02-05 stsp path) == -1) {
1326 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1327 0c06baac 2019-02-05 stsp goto done;
1329 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1332 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1334 0c06baac 2019-02-05 stsp if (error)
1335 66bea077 2018-08-02 stsp goto done;
1337 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1338 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1339 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1340 404c43c4 2018-06-21 stsp if (error != NULL)
1341 66bea077 2018-08-02 stsp goto done;
1342 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1343 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1344 404c43c4 2018-06-21 stsp if (error != NULL)
1345 66bea077 2018-08-02 stsp goto done;
1347 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1348 15a94983 2018-12-23 stsp commit_id_str);
1349 404c43c4 2018-06-21 stsp if (error != NULL)
1350 66bea077 2018-08-02 stsp goto done;
1353 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1355 66bea077 2018-08-02 stsp free(in_repo_path);
1356 66bea077 2018-08-02 stsp free(repo_path);
1357 66bea077 2018-08-02 stsp free(cwd);
1358 404c43c4 2018-06-21 stsp free(commit_id);
1359 0c06baac 2019-02-05 stsp if (worktree)
1360 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1361 ad242220 2018-09-08 stsp if (repo) {
1362 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1363 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1364 ad242220 2018-09-08 stsp if (error == NULL)
1365 ad242220 2018-09-08 stsp error = repo_error;
1367 404c43c4 2018-06-21 stsp return error;
1370 5de5890b 2018-10-18 stsp __dead static void
1371 5de5890b 2018-10-18 stsp usage_tree(void)
1373 c1669e2e 2019-01-09 stsp fprintf(stderr,
1374 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1375 5de5890b 2018-10-18 stsp getprogname());
1379 c1669e2e 2019-01-09 stsp static void
1380 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1381 c1669e2e 2019-01-09 stsp const char *root_path)
1383 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1385 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1386 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1389 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1390 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1391 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1394 5de5890b 2018-10-18 stsp static const struct got_error *
1395 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1396 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1397 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1399 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1400 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1401 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1402 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1403 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1405 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1407 5de5890b 2018-10-18 stsp goto done;
1409 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1411 5de5890b 2018-10-18 stsp goto done;
1412 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1413 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1414 5de5890b 2018-10-18 stsp while (te) {
1415 5de5890b 2018-10-18 stsp char *id = NULL;
1417 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1420 5de5890b 2018-10-18 stsp if (show_ids) {
1421 5de5890b 2018-10-18 stsp char *id_str;
1422 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1424 5de5890b 2018-10-18 stsp goto done;
1425 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1426 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1427 5de5890b 2018-10-18 stsp free(id_str);
1428 5de5890b 2018-10-18 stsp goto done;
1430 5de5890b 2018-10-18 stsp free(id_str);
1432 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1435 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1436 c1669e2e 2019-01-09 stsp char *child_path;
1437 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1438 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1439 c1669e2e 2019-01-09 stsp te->name) == -1) {
1440 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1441 c1669e2e 2019-01-09 stsp goto done;
1443 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1444 c1669e2e 2019-01-09 stsp root_path, repo);
1445 c1669e2e 2019-01-09 stsp free(child_path);
1447 c1669e2e 2019-01-09 stsp goto done;
1450 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1454 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1455 5de5890b 2018-10-18 stsp free(tree_id);
1456 5de5890b 2018-10-18 stsp return err;
1459 5de5890b 2018-10-18 stsp static const struct got_error *
1460 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1462 5de5890b 2018-10-18 stsp const struct got_error *error;
1463 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1464 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1465 7a2c19d6 2019-02-05 stsp const char *path;
1466 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1467 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1468 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1469 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1472 5de5890b 2018-10-18 stsp #ifndef PROFILE
1473 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1474 0f8d269b 2019-01-04 stsp NULL) == -1)
1475 5de5890b 2018-10-18 stsp err(1, "pledge");
1478 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1479 5de5890b 2018-10-18 stsp switch (ch) {
1481 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1484 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1485 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1486 5de5890b 2018-10-18 stsp err(1, "-r option");
1489 5de5890b 2018-10-18 stsp show_ids = 1;
1492 c1669e2e 2019-01-09 stsp recurse = 1;
1495 2deda0b9 2019-03-07 stsp usage_tree();
1496 5de5890b 2018-10-18 stsp /* NOTREACHED */
1500 5de5890b 2018-10-18 stsp argc -= optind;
1501 5de5890b 2018-10-18 stsp argv += optind;
1503 5de5890b 2018-10-18 stsp if (argc == 1)
1504 5de5890b 2018-10-18 stsp path = argv[0];
1505 5de5890b 2018-10-18 stsp else if (argc > 1)
1506 5de5890b 2018-10-18 stsp usage_tree();
1508 7a2c19d6 2019-02-05 stsp path = NULL;
1510 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1511 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1512 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1513 9bf7a39b 2019-02-05 stsp goto done;
1515 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1516 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1517 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1518 7a2c19d6 2019-02-05 stsp goto done;
1520 7a2c19d6 2019-02-05 stsp error = NULL;
1521 7a2c19d6 2019-02-05 stsp if (worktree) {
1522 7a2c19d6 2019-02-05 stsp repo_path =
1523 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1524 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1525 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1526 7a2c19d6 2019-02-05 stsp if (error)
1527 7a2c19d6 2019-02-05 stsp goto done;
1529 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1530 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1531 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1532 7a2c19d6 2019-02-05 stsp goto done;
1537 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1538 5de5890b 2018-10-18 stsp if (error != NULL)
1539 c02c541e 2019-03-29 stsp goto done;
1541 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1542 c02c541e 2019-03-29 stsp if (error)
1543 5de5890b 2018-10-18 stsp goto done;
1545 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1546 9bf7a39b 2019-02-05 stsp if (worktree) {
1547 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1548 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1549 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1550 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1551 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1552 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1553 9bf7a39b 2019-02-05 stsp goto done;
1555 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1557 9bf7a39b 2019-02-05 stsp if (error)
1558 9bf7a39b 2019-02-05 stsp goto done;
1560 9bf7a39b 2019-02-05 stsp path = "/";
1562 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1563 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1564 9bf7a39b 2019-02-05 stsp if (error != NULL)
1565 9bf7a39b 2019-02-05 stsp goto done;
1568 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1569 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1570 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1571 5de5890b 2018-10-18 stsp if (error != NULL)
1572 5de5890b 2018-10-18 stsp goto done;
1573 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1574 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1575 5de5890b 2018-10-18 stsp if (error != NULL)
1576 5de5890b 2018-10-18 stsp goto done;
1578 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1579 15a94983 2018-12-23 stsp commit_id_str);
1580 5de5890b 2018-10-18 stsp if (error != NULL)
1581 5de5890b 2018-10-18 stsp goto done;
1584 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1585 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1587 5de5890b 2018-10-18 stsp free(in_repo_path);
1588 5de5890b 2018-10-18 stsp free(repo_path);
1589 5de5890b 2018-10-18 stsp free(cwd);
1590 5de5890b 2018-10-18 stsp free(commit_id);
1591 7a2c19d6 2019-02-05 stsp if (worktree)
1592 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1593 5de5890b 2018-10-18 stsp if (repo) {
1594 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1595 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1596 5de5890b 2018-10-18 stsp if (error == NULL)
1597 5de5890b 2018-10-18 stsp error = repo_error;
1599 5de5890b 2018-10-18 stsp return error;
1602 6bad629b 2019-02-04 stsp __dead static void
1603 6bad629b 2019-02-04 stsp usage_status(void)
1605 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1609 b72f483a 2019-02-05 stsp static const struct got_error *
1610 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1611 b72f483a 2019-02-05 stsp struct got_object_id *id)
1613 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1614 b72f483a 2019-02-05 stsp return NULL;
1617 6bad629b 2019-02-04 stsp static const struct got_error *
1618 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1620 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1621 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1622 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1623 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1626 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1627 6bad629b 2019-02-04 stsp switch (ch) {
1629 2deda0b9 2019-03-07 stsp usage_status();
1630 6bad629b 2019-02-04 stsp /* NOTREACHED */
1634 6bad629b 2019-02-04 stsp argc -= optind;
1635 6bad629b 2019-02-04 stsp argv += optind;
1637 6bad629b 2019-02-04 stsp #ifndef PROFILE
1638 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1639 6bad629b 2019-02-04 stsp NULL) == -1)
1640 6bad629b 2019-02-04 stsp err(1, "pledge");
1642 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1643 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1644 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1645 927df6b7 2019-02-10 stsp goto done;
1648 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1649 927df6b7 2019-02-10 stsp if (error != NULL)
1650 927df6b7 2019-02-10 stsp goto done;
1652 6bad629b 2019-02-04 stsp if (argc == 0) {
1653 927df6b7 2019-02-10 stsp path = strdup("");
1654 927df6b7 2019-02-10 stsp if (path == NULL) {
1655 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1656 6bad629b 2019-02-04 stsp goto done;
1658 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1659 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1660 927df6b7 2019-02-10 stsp if (error)
1661 6bad629b 2019-02-04 stsp goto done;
1663 6bad629b 2019-02-04 stsp usage_status();
1665 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1666 6bad629b 2019-02-04 stsp if (error != NULL)
1667 6bad629b 2019-02-04 stsp goto done;
1669 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1670 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(worktree));
1671 6bad629b 2019-02-04 stsp if (error)
1672 6bad629b 2019-02-04 stsp goto done;
1674 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1675 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1677 927df6b7 2019-02-10 stsp free(cwd);
1678 927df6b7 2019-02-10 stsp free(path);
1679 d0eebce4 2019-03-11 stsp return error;
1682 d0eebce4 2019-03-11 stsp __dead static void
1683 d0eebce4 2019-03-11 stsp usage_ref(void)
1685 d0eebce4 2019-03-11 stsp fprintf(stderr,
1686 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1687 d0eebce4 2019-03-11 stsp getprogname());
1691 d0eebce4 2019-03-11 stsp static const struct got_error *
1692 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1694 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1695 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1696 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1698 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1699 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1701 d0eebce4 2019-03-11 stsp return err;
1703 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1704 d0eebce4 2019-03-11 stsp char *refstr;
1705 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1706 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1707 d0eebce4 2019-03-11 stsp return got_error_from_errno();
1708 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1709 d0eebce4 2019-03-11 stsp free(refstr);
1712 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1713 d0eebce4 2019-03-11 stsp return NULL;
1716 d0eebce4 2019-03-11 stsp static const struct got_error *
1717 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1719 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1720 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1722 d0eebce4 2019-03-11 stsp err = got_ref_open(&ref, repo, refname);
1724 d0eebce4 2019-03-11 stsp return err;
1726 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1727 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1728 d0eebce4 2019-03-11 stsp return err;
1731 d0eebce4 2019-03-11 stsp static const struct got_error *
1732 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1734 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1735 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1736 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1738 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1740 d0eebce4 2019-03-11 stsp return err;
1742 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1744 d0eebce4 2019-03-11 stsp goto done;
1746 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1749 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1751 d0eebce4 2019-03-11 stsp return err;
1754 d0eebce4 2019-03-11 stsp static const struct got_error *
1755 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1757 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1758 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1759 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1760 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1761 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1762 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1764 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1765 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1766 d0eebce4 2019-03-11 stsp switch (ch) {
1768 d0eebce4 2019-03-11 stsp delref = optarg;
1771 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1772 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1773 d0eebce4 2019-03-11 stsp err(1, "-r option");
1776 d0eebce4 2019-03-11 stsp do_list = 1;
1779 d0eebce4 2019-03-11 stsp usage_ref();
1780 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1784 d0eebce4 2019-03-11 stsp if (do_list && delref)
1785 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1787 d0eebce4 2019-03-11 stsp argc -= optind;
1788 d0eebce4 2019-03-11 stsp argv += optind;
1790 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1791 d0eebce4 2019-03-11 stsp if (argc > 0)
1792 d0eebce4 2019-03-11 stsp usage_ref();
1793 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1794 d0eebce4 2019-03-11 stsp usage_ref();
1796 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1797 e0b57350 2019-03-12 stsp if (do_list) {
1798 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1799 e0b57350 2019-03-12 stsp NULL) == -1)
1800 e0b57350 2019-03-12 stsp err(1, "pledge");
1802 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1803 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1804 e0b57350 2019-03-12 stsp err(1, "pledge");
1807 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1808 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1809 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1810 d0eebce4 2019-03-11 stsp goto done;
1813 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1814 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1815 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1816 d0eebce4 2019-03-11 stsp goto done;
1818 d0eebce4 2019-03-11 stsp error = NULL;
1819 d0eebce4 2019-03-11 stsp if (worktree) {
1820 d0eebce4 2019-03-11 stsp repo_path =
1821 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1822 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1823 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1824 d0eebce4 2019-03-11 stsp if (error)
1825 d0eebce4 2019-03-11 stsp goto done;
1827 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1828 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1829 d0eebce4 2019-03-11 stsp error = got_error_from_errno();
1830 d0eebce4 2019-03-11 stsp goto done;
1835 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1836 d0eebce4 2019-03-11 stsp if (error != NULL)
1837 d0eebce4 2019-03-11 stsp goto done;
1839 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1840 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1841 c02c541e 2019-03-29 stsp if (error)
1842 c02c541e 2019-03-29 stsp goto done;
1844 d0eebce4 2019-03-11 stsp if (do_list)
1845 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1846 d0eebce4 2019-03-11 stsp else if (delref)
1847 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1849 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1852 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1853 d0eebce4 2019-03-11 stsp if (worktree)
1854 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1855 d0eebce4 2019-03-11 stsp free(cwd);
1856 d0eebce4 2019-03-11 stsp free(repo_path);
1857 d00136be 2019-03-26 stsp return error;
1860 d00136be 2019-03-26 stsp __dead static void
1861 d00136be 2019-03-26 stsp usage_add(void)
1863 d00136be 2019-03-26 stsp fprintf(stderr, "usage: %s add file-path\n", getprogname());
1867 d00136be 2019-03-26 stsp static const struct got_error *
1868 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1870 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1871 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1872 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1873 d00136be 2019-03-26 stsp char *cwd = NULL, *path = NULL, *relpath = NULL;
1876 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1877 d00136be 2019-03-26 stsp switch (ch) {
1879 d00136be 2019-03-26 stsp usage_add();
1880 d00136be 2019-03-26 stsp /* NOTREACHED */
1884 d00136be 2019-03-26 stsp argc -= optind;
1885 d00136be 2019-03-26 stsp argv += optind;
1887 d00136be 2019-03-26 stsp if (argc != 1)
1888 d00136be 2019-03-26 stsp usage_add();
1890 d00136be 2019-03-26 stsp path = realpath(argv[0], NULL);
1891 d00136be 2019-03-26 stsp if (path == NULL) {
1892 d00136be 2019-03-26 stsp error = got_error_from_errno();
1893 d00136be 2019-03-26 stsp goto done;
1896 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1897 d00136be 2019-03-26 stsp if (cwd == NULL) {
1898 d00136be 2019-03-26 stsp error = got_error_from_errno();
1899 d00136be 2019-03-26 stsp goto done;
1901 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1902 d00136be 2019-03-26 stsp if (error)
1903 d00136be 2019-03-26 stsp goto done;
1905 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1906 031a5338 2019-03-26 stsp if (error != NULL)
1907 031a5338 2019-03-26 stsp goto done;
1909 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1910 031a5338 2019-03-26 stsp got_worktree_get_root_path(worktree));
1911 d00136be 2019-03-26 stsp if (error)
1912 d00136be 2019-03-26 stsp goto done;
1914 031a5338 2019-03-26 stsp error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1916 d00136be 2019-03-26 stsp if (error)
1917 d00136be 2019-03-26 stsp goto done;
1920 031a5338 2019-03-26 stsp got_repo_close(repo);
1921 d00136be 2019-03-26 stsp if (worktree)
1922 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1923 d00136be 2019-03-26 stsp free(path);
1924 d00136be 2019-03-26 stsp free(relpath);
1925 2ec1f75b 2019-03-26 stsp free(cwd);
1926 2ec1f75b 2019-03-26 stsp return error;
1929 2ec1f75b 2019-03-26 stsp __dead static void
1930 2ec1f75b 2019-03-26 stsp usage_rm(void)
1932 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1936 2ec1f75b 2019-03-26 stsp static const struct got_error *
1937 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
1939 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
1940 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
1941 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
1942 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
1943 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
1945 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
1946 2ec1f75b 2019-03-26 stsp switch (ch) {
1948 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
1951 2ec1f75b 2019-03-26 stsp usage_add();
1952 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
1956 2ec1f75b 2019-03-26 stsp argc -= optind;
1957 2ec1f75b 2019-03-26 stsp argv += optind;
1959 2ec1f75b 2019-03-26 stsp if (argc != 1)
1960 2ec1f75b 2019-03-26 stsp usage_rm();
1962 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
1963 2ec1f75b 2019-03-26 stsp if (path == NULL) {
1964 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1965 2ec1f75b 2019-03-26 stsp goto done;
1968 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
1969 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
1970 2ec1f75b 2019-03-26 stsp error = got_error_from_errno();
1971 2ec1f75b 2019-03-26 stsp goto done;
1973 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1974 2ec1f75b 2019-03-26 stsp if (error)
1975 2ec1f75b 2019-03-26 stsp goto done;
1977 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1978 2ec1f75b 2019-03-26 stsp if (error != NULL)
1979 2ec1f75b 2019-03-26 stsp goto done;
1981 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1982 c2253644 2019-03-26 stsp got_worktree_get_root_path(worktree));
1983 2ec1f75b 2019-03-26 stsp if (error)
1984 2ec1f75b 2019-03-26 stsp goto done;
1986 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1987 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
1988 a129376b 2019-03-28 stsp if (error)
1989 a129376b 2019-03-28 stsp goto done;
1992 a129376b 2019-03-28 stsp got_repo_close(repo);
1993 a129376b 2019-03-28 stsp if (worktree)
1994 a129376b 2019-03-28 stsp got_worktree_close(worktree);
1995 a129376b 2019-03-28 stsp free(path);
1996 a129376b 2019-03-28 stsp free(cwd);
1997 a129376b 2019-03-28 stsp return error;
2000 a129376b 2019-03-28 stsp __dead static void
2001 a129376b 2019-03-28 stsp usage_revert(void)
2003 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2007 a129376b 2019-03-28 stsp static void
2008 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2010 a129376b 2019-03-28 stsp while (path[0] == '/')
2012 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2015 a129376b 2019-03-28 stsp static const struct got_error *
2016 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2018 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2019 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2020 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2021 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2024 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2025 a129376b 2019-03-28 stsp switch (ch) {
2027 a129376b 2019-03-28 stsp usage_revert();
2028 a129376b 2019-03-28 stsp /* NOTREACHED */
2032 a129376b 2019-03-28 stsp argc -= optind;
2033 a129376b 2019-03-28 stsp argv += optind;
2035 a129376b 2019-03-28 stsp if (argc != 1)
2036 a129376b 2019-03-28 stsp usage_revert();
2038 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2039 a129376b 2019-03-28 stsp if (path == NULL) {
2040 a129376b 2019-03-28 stsp error = got_error_from_errno();
2041 a129376b 2019-03-28 stsp goto done;
2044 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2045 a129376b 2019-03-28 stsp if (cwd == NULL) {
2046 a129376b 2019-03-28 stsp error = got_error_from_errno();
2047 a129376b 2019-03-28 stsp goto done;
2049 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2050 2ec1f75b 2019-03-26 stsp if (error)
2051 2ec1f75b 2019-03-26 stsp goto done;
2053 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2054 a129376b 2019-03-28 stsp if (error != NULL)
2055 a129376b 2019-03-28 stsp goto done;
2057 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2058 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree));
2059 a129376b 2019-03-28 stsp if (error)
2060 a129376b 2019-03-28 stsp goto done;
2062 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2063 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2064 a129376b 2019-03-28 stsp if (error)
2065 a129376b 2019-03-28 stsp goto done;
2068 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2069 2ec1f75b 2019-03-26 stsp if (worktree)
2070 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2071 2ec1f75b 2019-03-26 stsp free(path);
2072 d00136be 2019-03-26 stsp free(cwd);
2073 6bad629b 2019-02-04 stsp return error;
2076 c4296144 2019-05-09 stsp __dead static void
2077 c4296144 2019-05-09 stsp usage_commit(void)
2079 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2083 c4296144 2019-05-09 stsp static const struct got_error *
2084 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2086 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2087 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2088 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2089 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2090 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2091 c4296144 2019-05-09 stsp const char *logmsg = "<no log message was specified>";
2092 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2095 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2096 c4296144 2019-05-09 stsp switch (ch) {
2098 c4296144 2019-05-09 stsp logmsg = optarg;
2101 c4296144 2019-05-09 stsp usage_commit();
2102 c4296144 2019-05-09 stsp /* NOTREACHED */
2106 c4296144 2019-05-09 stsp argc -= optind;
2107 c4296144 2019-05-09 stsp argv += optind;
2109 c4296144 2019-05-09 stsp if (argc == 1) {
2110 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2111 c4296144 2019-05-09 stsp if (path == NULL) {
2112 c4296144 2019-05-09 stsp error = got_error_from_errno();
2113 c4296144 2019-05-09 stsp goto done;
2115 c4296144 2019-05-09 stsp } else if (argc != 0)
2116 c4296144 2019-05-09 stsp usage_commit();
2118 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2119 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2120 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2121 35bd8fed 2019-05-09 stsp goto done;
2124 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2125 c4296144 2019-05-09 stsp if (cwd == NULL) {
2126 c4296144 2019-05-09 stsp error = got_error_from_errno();
2127 c4296144 2019-05-09 stsp goto done;
2129 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2130 c4296144 2019-05-09 stsp if (error)
2131 c4296144 2019-05-09 stsp goto done;
2133 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2134 c4296144 2019-05-09 stsp if (error != NULL)
2135 c4296144 2019-05-09 stsp goto done;
2137 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2138 c4296144 2019-05-09 stsp got_worktree_get_root_path(worktree));
2139 c4296144 2019-05-09 stsp if (error)
2140 c4296144 2019-05-09 stsp goto done;
2142 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2143 35bd8fed 2019-05-09 stsp logmsg, print_status, NULL, repo);
2144 c4296144 2019-05-09 stsp if (error)
2145 c4296144 2019-05-09 stsp goto done;
2147 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2148 c4296144 2019-05-09 stsp if (error)
2149 c4296144 2019-05-09 stsp goto done;
2150 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2153 c4296144 2019-05-09 stsp got_repo_close(repo);
2154 c4296144 2019-05-09 stsp if (worktree)
2155 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2156 c4296144 2019-05-09 stsp free(path);
2157 c4296144 2019-05-09 stsp free(cwd);
2158 c4296144 2019-05-09 stsp free(id_str);
2159 c4296144 2019-05-09 stsp return error;