2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 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);
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
80 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
84 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp #ifdef notyet
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
90 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
91 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
92 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
93 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
94 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
95 1b6b95a8 2018-03-12 stsp "show repository history" },
96 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
97 b00d56cd 2018-04-01 stsp "compare files and directories" },
98 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
99 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
100 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
101 5de5890b 2018-10-18 stsp " list files and directories in repository" },
102 f42b1b34 2018-03-12 stsp #ifdef notyet
103 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
104 1b6b95a8 2018-03-12 stsp "show modification status of files" },
109 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
111 5c860e29 2018-03-12 stsp struct cmd *cmd;
112 5c860e29 2018-03-12 stsp unsigned int i;
114 1b6b95a8 2018-03-12 stsp int hflag = 0;
116 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
118 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
119 5c860e29 2018-03-12 stsp switch (ch) {
125 5c860e29 2018-03-12 stsp /* NOTREACHED */
129 5c860e29 2018-03-12 stsp argc -= optind;
130 5c860e29 2018-03-12 stsp argv += optind;
131 1e70621d 2018-03-27 stsp optind = 0;
133 5c860e29 2018-03-12 stsp if (argc <= 0)
136 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
137 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
139 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
140 d7d4f210 2018-03-12 stsp const struct got_error *error;
142 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
144 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
148 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
150 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
151 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
152 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
159 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
163 4ed7e80c 2018-05-20 stsp __dead static void
164 5c860e29 2018-03-12 stsp usage(void)
168 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
169 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
170 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
171 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
172 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
177 0266afb7 2019-01-04 stsp static const struct got_error *
178 0266afb7 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
180 0266afb7 2019-01-04 stsp const struct got_error *error;
182 0266afb7 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
183 0266afb7 2019-01-04 stsp return got_error_from_errno();
185 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
186 0266afb7 2019-01-04 stsp return got_error_from_errno();
188 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
189 0266afb7 2019-01-04 stsp return got_error_from_errno();
191 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
192 0266afb7 2019-01-04 stsp if (error != NULL)
193 0266afb7 2019-01-04 stsp return error;
195 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
196 0266afb7 2019-01-04 stsp return got_error_from_errno();
198 0266afb7 2019-01-04 stsp return NULL;
201 4ed7e80c 2018-05-20 stsp __dead static void
202 c09a553d 2018-03-12 stsp usage_checkout(void)
204 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
205 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
209 92a684f4 2018-03-12 stsp static void
210 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
212 92a684f4 2018-03-12 stsp char *worktree_path = arg;
214 92a684f4 2018-03-12 stsp while (path[0] == '/')
217 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
220 99437157 2018-11-11 stsp static const struct got_error *
221 99437157 2018-11-11 stsp checkout_cancel(void *arg)
223 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
224 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
225 99437157 2018-11-11 stsp return NULL;
228 8069f636 2019-01-12 stsp static const struct got_error *
229 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
230 8069f636 2019-01-12 stsp struct got_repository *repo)
232 8069f636 2019-01-12 stsp const struct got_error *err;
233 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
234 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
235 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
237 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
238 8069f636 2019-01-12 stsp if (head_ref == NULL)
239 8069f636 2019-01-12 stsp return got_error_from_errno();
241 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
242 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
246 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
250 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
253 8069f636 2019-01-12 stsp while (1) {
254 8069f636 2019-01-12 stsp struct got_object_id *id;
256 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
259 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
261 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
262 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
265 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
267 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
273 8069f636 2019-01-12 stsp if (id == NULL)
275 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
279 8069f636 2019-01-12 stsp if (head_ref)
280 8069f636 2019-01-12 stsp got_ref_close(head_ref);
282 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
283 8069f636 2019-01-12 stsp return err;
287 4ed7e80c 2018-05-20 stsp static const struct got_error *
288 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
290 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
291 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
292 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
293 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
294 c09a553d 2018-03-12 stsp char *repo_path = NULL;
295 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
296 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
297 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
298 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
300 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
301 0bb8a95e 2018-03-12 stsp switch (ch) {
303 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
304 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
305 8069f636 2019-01-12 stsp return got_error_from_errno();
308 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
312 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
316 0bb8a95e 2018-03-12 stsp argc -= optind;
317 0bb8a95e 2018-03-12 stsp argv += optind;
319 6715a751 2018-03-16 stsp #ifndef PROFILE
320 63219cd2 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
321 63219cd2 2019-01-04 stsp NULL) == -1)
322 c09a553d 2018-03-12 stsp err(1, "pledge");
324 0bb8a95e 2018-03-12 stsp if (argc == 1) {
325 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
326 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
327 76089277 2018-04-01 stsp if (repo_path == NULL)
328 76089277 2018-04-01 stsp return got_error_from_errno();
329 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
330 76089277 2018-04-01 stsp if (cwd == NULL) {
331 76089277 2018-04-01 stsp error = got_error_from_errno();
334 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
335 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
337 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
338 76089277 2018-04-01 stsp if (base == NULL) {
339 76089277 2018-04-01 stsp error = got_error_from_errno();
342 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
343 c09a553d 2018-03-12 stsp if (dotgit)
344 c09a553d 2018-03-12 stsp *dotgit = '\0';
345 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
346 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
351 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
352 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
353 76089277 2018-04-01 stsp if (repo_path == NULL) {
354 76089277 2018-04-01 stsp error = got_error_from_errno();
357 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
358 76089277 2018-04-01 stsp if (worktree_path == NULL) {
359 76089277 2018-04-01 stsp error = got_error_from_errno();
363 c09a553d 2018-03-12 stsp usage_checkout();
365 0266afb7 2019-01-04 stsp error = apply_unveil(repo_path, worktree_path);
369 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
370 c09a553d 2018-03-12 stsp if (error != NULL)
373 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
374 c09a553d 2018-03-12 stsp if (error != NULL)
377 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
378 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
381 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
382 c09a553d 2018-03-12 stsp if (error != NULL)
385 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
386 e5dc7198 2018-12-29 stsp path_prefix);
387 e5dc7198 2018-12-29 stsp if (error != NULL)
389 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
390 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
394 8069f636 2019-01-12 stsp if (commit_id_str) {
395 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
396 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
397 8069f636 2019-01-12 stsp commit_id_str);
398 8069f636 2019-01-12 stsp if (error != NULL)
400 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
401 8069f636 2019-01-12 stsp if (error != NULL) {
402 8069f636 2019-01-12 stsp free(commit_id);
405 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
406 8069f636 2019-01-12 stsp commit_id);
407 8069f636 2019-01-12 stsp free(commit_id);
412 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
413 99437157 2018-11-11 stsp checkout_progress, worktree_path, checkout_cancel, NULL);
414 c09a553d 2018-03-12 stsp if (error != NULL)
417 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
420 8069f636 2019-01-12 stsp free(commit_id_str);
421 76089277 2018-04-01 stsp free(repo_path);
422 507dc3bb 2018-12-29 stsp free(worktree_path);
423 507dc3bb 2018-12-29 stsp return error;
426 507dc3bb 2018-12-29 stsp __dead static void
427 507dc3bb 2018-12-29 stsp usage_update(void)
429 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
430 507dc3bb 2018-12-29 stsp getprogname());
434 507dc3bb 2018-12-29 stsp static void
435 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
437 784955db 2019-01-12 stsp int *did_something = arg;
439 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
442 784955db 2019-01-12 stsp *did_something = 1;
443 507dc3bb 2018-12-29 stsp while (path[0] == '/')
445 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
448 be7061eb 2018-12-30 stsp static const struct got_error *
449 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
451 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
452 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
453 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
454 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
455 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
456 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
457 784955db 2019-01-12 stsp int ch, did_something = 0;
459 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
460 507dc3bb 2018-12-29 stsp switch (ch) {
462 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
463 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
464 9c4b8182 2019-01-02 stsp return got_error_from_errno();
468 507dc3bb 2018-12-29 stsp /* NOTREACHED */
472 507dc3bb 2018-12-29 stsp argc -= optind;
473 507dc3bb 2018-12-29 stsp argv += optind;
475 507dc3bb 2018-12-29 stsp #ifndef PROFILE
476 0266afb7 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
477 0266afb7 2019-01-04 stsp NULL) == -1)
478 507dc3bb 2018-12-29 stsp err(1, "pledge");
480 507dc3bb 2018-12-29 stsp if (argc == 0) {
481 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
482 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
483 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
486 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
487 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
488 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
489 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
493 507dc3bb 2018-12-29 stsp usage_update();
495 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
496 507dc3bb 2018-12-29 stsp if (error != NULL)
499 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
500 507dc3bb 2018-12-29 stsp if (error != NULL)
503 7839bc15 2019-01-06 stsp error = apply_unveil(got_repo_get_path(repo), worktree_path);
507 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
508 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
509 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
510 507dc3bb 2018-12-29 stsp if (error != NULL)
512 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
513 9c4b8182 2019-01-02 stsp if (error != NULL)
515 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
516 507dc3bb 2018-12-29 stsp if (error != NULL)
519 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
520 507dc3bb 2018-12-29 stsp commit_id_str);
521 507dc3bb 2018-12-29 stsp if (error != NULL)
525 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
526 be7061eb 2018-12-30 stsp if (error != NULL)
529 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
530 507dc3bb 2018-12-29 stsp commit_id) != 0) {
531 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
532 507dc3bb 2018-12-29 stsp commit_id);
537 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
538 784955db 2019-01-12 stsp update_progress, &did_something, checkout_cancel, NULL);
539 507dc3bb 2018-12-29 stsp if (error != NULL)
542 784955db 2019-01-12 stsp if (did_something)
543 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
545 784955db 2019-01-12 stsp printf("Already up-to-date\n");
547 c09a553d 2018-03-12 stsp free(worktree_path);
548 507dc3bb 2018-12-29 stsp free(commit_id);
549 9c4b8182 2019-01-02 stsp free(commit_id_str);
550 c09a553d 2018-03-12 stsp return error;
553 f42b1b34 2018-03-12 stsp static const struct got_error *
554 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
555 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
557 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
558 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
559 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
560 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
562 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
563 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
565 79109fed 2018-03-27 stsp return err;
567 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
568 79f35eb3 2018-06-11 stsp if (qid != NULL) {
569 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
571 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
573 79109fed 2018-03-27 stsp return err;
575 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
576 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
577 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
579 0f2b3dca 2018-12-22 stsp return err;
581 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
583 79109fed 2018-03-27 stsp return err;
586 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
590 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
591 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
594 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
595 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
596 0f2b3dca 2018-12-22 stsp free(id_str1);
597 0f2b3dca 2018-12-22 stsp free(id_str2);
598 79109fed 2018-03-27 stsp return err;
601 4bb494d5 2018-06-16 stsp static char *
602 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
604 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
605 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
611 79109fed 2018-03-27 stsp static const struct got_error *
612 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
613 c0cc5c62 2018-10-18 stsp struct got_repository *repo, int show_patch, int diff_context)
615 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
616 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
617 6c281f94 2018-06-11 stsp char datebuf[26];
618 45d799e2 2018-12-23 stsp time_t committer_time;
619 45d799e2 2018-12-23 stsp const char *author, *committer;
621 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
623 8bf5b3c9 2018-03-17 stsp return err;
625 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
626 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
627 832c249c 2018-06-10 stsp free(id_str);
628 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
629 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
630 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
631 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
632 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
633 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
634 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
635 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
636 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
637 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
638 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
640 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
641 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
642 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
644 a0603db2 2018-06-10 stsp return err;
645 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
646 a0603db2 2018-06-10 stsp free(id_str);
650 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
651 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
652 832c249c 2018-06-10 stsp return got_error_from_errno();
654 621015ac 2018-07-23 stsp logmsg = logmsg0;
656 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
658 832c249c 2018-06-10 stsp printf(" %s\n", line);
659 832c249c 2018-06-10 stsp } while (line);
660 621015ac 2018-07-23 stsp free(logmsg0);
662 971751ac 2018-03-27 stsp if (show_patch) {
663 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
664 971751ac 2018-03-27 stsp if (err == 0)
665 971751ac 2018-03-27 stsp printf("\n");
668 d82de447 2018-09-15 stsp fflush(stdout);
669 07862c20 2018-09-15 stsp return err;
672 f42b1b34 2018-03-12 stsp static const struct got_error *
673 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
674 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
675 15a94983 2018-12-23 stsp int first_parent_traversal)
677 f42b1b34 2018-03-12 stsp const struct got_error *err;
678 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
680 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
681 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
683 f42b1b34 2018-03-12 stsp return err;
684 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
687 31cedeaf 2018-09-15 stsp while (1) {
688 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
689 372ccdbb 2018-06-10 stsp struct got_object_id *id;
691 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
694 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
696 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
697 9ba79e04 2018-06-11 stsp err = NULL;
700 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
702 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
708 b43fbaa0 2018-06-11 stsp if (id == NULL)
711 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
714 c0cc5c62 2018-10-18 stsp err = print_commit(commit, id, repo, show_patch, diff_context);
715 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
716 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
720 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
721 f42b1b34 2018-03-12 stsp return err;
724 4ed7e80c 2018-05-20 stsp __dead static void
725 6f3d1eb0 2018-03-12 stsp usage_log(void)
727 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
728 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
732 4ed7e80c 2018-05-20 stsp static const struct got_error *
733 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
735 f42b1b34 2018-03-12 stsp const struct got_error *error;
736 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
737 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
738 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
739 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
740 d142fc45 2018-04-01 stsp char *start_commit = NULL;
741 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
742 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
743 64a96a6d 2018-04-01 stsp const char *errstr;
745 6715a751 2018-03-16 stsp #ifndef PROFILE
746 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
749 f42b1b34 2018-03-12 stsp err(1, "pledge");
752 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
753 79109fed 2018-03-27 stsp switch (ch) {
755 79109fed 2018-03-27 stsp show_patch = 1;
758 d142fc45 2018-04-01 stsp start_commit = optarg;
761 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
763 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
764 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
767 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
768 64a96a6d 2018-04-01 stsp if (errstr != NULL)
769 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
772 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
775 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
776 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
777 04ca23f4 2018-07-16 stsp err(1, "-r option");
781 79109fed 2018-03-27 stsp /* NOTREACHED */
785 79109fed 2018-03-27 stsp argc -= optind;
786 79109fed 2018-03-27 stsp argv += optind;
788 04ca23f4 2018-07-16 stsp if (argc == 0)
789 04ca23f4 2018-07-16 stsp path = strdup("");
790 04ca23f4 2018-07-16 stsp else if (argc == 1)
791 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
793 6f3d1eb0 2018-03-12 stsp usage_log();
794 04ca23f4 2018-07-16 stsp if (path == NULL)
795 04ca23f4 2018-07-16 stsp return got_error_from_errno();
797 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
798 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
799 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
802 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
803 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
804 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
805 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
810 6098196c 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
814 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
815 d7d4f210 2018-03-12 stsp if (error != NULL)
818 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
819 3235492e 2018-04-01 stsp struct got_reference *head_ref;
820 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
821 3235492e 2018-04-01 stsp if (error != NULL)
822 3235492e 2018-04-01 stsp return error;
823 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
824 3235492e 2018-04-01 stsp got_ref_close(head_ref);
825 3235492e 2018-04-01 stsp if (error != NULL)
826 3235492e 2018-04-01 stsp return error;
827 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
829 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
830 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
831 3235492e 2018-04-01 stsp if (error == NULL) {
832 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
833 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
834 d1f2edc9 2018-06-13 stsp if (error != NULL)
835 d1f2edc9 2018-06-13 stsp return error;
836 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
837 d1f2edc9 2018-06-13 stsp if (error != NULL)
838 d1f2edc9 2018-06-13 stsp return error;
840 15a94983 2018-12-23 stsp if (commit == NULL) {
841 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
842 d1f2edc9 2018-06-13 stsp start_commit);
843 d1f2edc9 2018-06-13 stsp if (error != NULL)
844 d1f2edc9 2018-06-13 stsp return error;
847 d7d4f210 2018-03-12 stsp if (error != NULL)
850 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
851 04ca23f4 2018-07-16 stsp if (error != NULL)
853 04ca23f4 2018-07-16 stsp if (in_repo_path) {
854 04ca23f4 2018-07-16 stsp free(path);
855 04ca23f4 2018-07-16 stsp path = in_repo_path;
858 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
859 c0cc5c62 2018-10-18 stsp diff_context, limit, first_parent_traversal);
861 04ca23f4 2018-07-16 stsp free(path);
862 04ca23f4 2018-07-16 stsp free(repo_path);
865 ad242220 2018-09-08 stsp if (repo) {
866 ad242220 2018-09-08 stsp const struct got_error *repo_error;
867 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
868 ad242220 2018-09-08 stsp if (error == NULL)
869 ad242220 2018-09-08 stsp error = repo_error;
871 8bf5b3c9 2018-03-17 stsp return error;
874 4ed7e80c 2018-05-20 stsp __dead static void
875 3f8b7d6a 2018-04-01 stsp usage_diff(void)
877 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
878 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
882 4ed7e80c 2018-05-20 stsp static const struct got_error *
883 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
885 b00d56cd 2018-04-01 stsp const struct got_error *error;
886 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
887 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
888 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
889 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
890 15a94983 2018-12-23 stsp int type1, type2;
891 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
892 c0cc5c62 2018-10-18 stsp const char *errstr;
894 b00d56cd 2018-04-01 stsp #ifndef PROFILE
895 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
896 25eccc22 2019-01-04 stsp NULL) == -1)
897 b00d56cd 2018-04-01 stsp err(1, "pledge");
900 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
901 b00d56cd 2018-04-01 stsp switch (ch) {
903 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
904 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
905 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
909 b00d56cd 2018-04-01 stsp /* NOTREACHED */
913 b00d56cd 2018-04-01 stsp argc -= optind;
914 b00d56cd 2018-04-01 stsp argv += optind;
916 b00d56cd 2018-04-01 stsp if (argc == 0) {
917 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
918 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
919 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
920 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
921 e1e3f570 2018-04-01 stsp return got_error_from_errno();
922 15a94983 2018-12-23 stsp id_str1 = argv[0];
923 15a94983 2018-12-23 stsp id_str2 = argv[1];
924 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
925 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
926 76089277 2018-04-01 stsp if (repo_path == NULL)
927 76089277 2018-04-01 stsp return got_error_from_errno();
928 15a94983 2018-12-23 stsp id_str1 = argv[1];
929 15a94983 2018-12-23 stsp id_str2 = argv[2];
931 b00d56cd 2018-04-01 stsp usage_diff();
933 25eccc22 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
937 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
938 76089277 2018-04-01 stsp free(repo_path);
939 b00d56cd 2018-04-01 stsp if (error != NULL)
942 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
946 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
950 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
954 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
958 15a94983 2018-12-23 stsp if (type1 != type2) {
959 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
963 15a94983 2018-12-23 stsp switch (type1) {
964 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
965 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
966 54156555 2018-12-24 stsp diff_context, repo, stdout);
968 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
969 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
970 54156555 2018-12-24 stsp diff_context, repo, stdout);
972 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
973 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
975 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
976 c0cc5c62 2018-10-18 stsp repo, stdout);
979 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
985 ad242220 2018-09-08 stsp if (repo) {
986 ad242220 2018-09-08 stsp const struct got_error *repo_error;
987 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
988 ad242220 2018-09-08 stsp if (error == NULL)
989 ad242220 2018-09-08 stsp error = repo_error;
991 b00d56cd 2018-04-01 stsp return error;
994 404c43c4 2018-06-21 stsp __dead static void
995 404c43c4 2018-06-21 stsp usage_blame(void)
997 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
998 404c43c4 2018-06-21 stsp getprogname());
1002 404c43c4 2018-06-21 stsp static const struct got_error *
1003 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1005 404c43c4 2018-06-21 stsp const struct got_error *error;
1006 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1007 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1008 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1009 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1012 404c43c4 2018-06-21 stsp #ifndef PROFILE
1013 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1014 36e2fb66 2019-01-04 stsp NULL) == -1)
1015 404c43c4 2018-06-21 stsp err(1, "pledge");
1018 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1019 404c43c4 2018-06-21 stsp switch (ch) {
1021 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1024 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1025 66bea077 2018-08-02 stsp if (repo_path == NULL)
1026 66bea077 2018-08-02 stsp err(1, "-r option");
1030 404c43c4 2018-06-21 stsp /* NOTREACHED */
1034 404c43c4 2018-06-21 stsp argc -= optind;
1035 404c43c4 2018-06-21 stsp argv += optind;
1037 a39318fd 2018-08-02 stsp if (argc == 1)
1038 404c43c4 2018-06-21 stsp path = argv[0];
1040 404c43c4 2018-06-21 stsp usage_blame();
1042 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1043 66bea077 2018-08-02 stsp if (cwd == NULL) {
1044 66bea077 2018-08-02 stsp error = got_error_from_errno();
1045 66bea077 2018-08-02 stsp goto done;
1047 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1048 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
1049 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1050 66bea077 2018-08-02 stsp error = got_error_from_errno();
1051 66bea077 2018-08-02 stsp goto done;
1055 36e2fb66 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1056 36e2fb66 2019-01-04 stsp if (error)
1057 36e2fb66 2019-01-04 stsp goto done;
1059 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1060 404c43c4 2018-06-21 stsp if (error != NULL)
1061 404c43c4 2018-06-21 stsp goto done;
1063 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1064 66bea077 2018-08-02 stsp if (error != NULL)
1065 66bea077 2018-08-02 stsp goto done;
1067 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1068 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1069 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1070 404c43c4 2018-06-21 stsp if (error != NULL)
1071 66bea077 2018-08-02 stsp goto done;
1072 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1073 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1074 404c43c4 2018-06-21 stsp if (error != NULL)
1075 66bea077 2018-08-02 stsp goto done;
1077 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1078 15a94983 2018-12-23 stsp commit_id_str);
1079 404c43c4 2018-06-21 stsp if (error != NULL)
1080 66bea077 2018-08-02 stsp goto done;
1083 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1085 66bea077 2018-08-02 stsp free(in_repo_path);
1086 66bea077 2018-08-02 stsp free(repo_path);
1087 66bea077 2018-08-02 stsp free(cwd);
1088 404c43c4 2018-06-21 stsp free(commit_id);
1089 ad242220 2018-09-08 stsp if (repo) {
1090 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1091 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1092 ad242220 2018-09-08 stsp if (error == NULL)
1093 ad242220 2018-09-08 stsp error = repo_error;
1095 404c43c4 2018-06-21 stsp return error;
1098 5de5890b 2018-10-18 stsp __dead static void
1099 5de5890b 2018-10-18 stsp usage_tree(void)
1101 c1669e2e 2019-01-09 stsp fprintf(stderr,
1102 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1103 5de5890b 2018-10-18 stsp getprogname());
1107 c1669e2e 2019-01-09 stsp static void
1108 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1109 c1669e2e 2019-01-09 stsp const char *root_path)
1111 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1113 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1114 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1117 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1118 c1669e2e 2019-01-09 stsp is_root_path ? "" : "/",
1119 c1669e2e 2019-01-09 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1122 5de5890b 2018-10-18 stsp static const struct got_error *
1123 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1124 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1125 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1127 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1128 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1129 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1130 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1131 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1133 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1135 5de5890b 2018-10-18 stsp goto done;
1137 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1139 5de5890b 2018-10-18 stsp goto done;
1140 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1141 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1142 5de5890b 2018-10-18 stsp while (te) {
1143 5de5890b 2018-10-18 stsp char *id = NULL;
1145 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1148 5de5890b 2018-10-18 stsp if (show_ids) {
1149 5de5890b 2018-10-18 stsp char *id_str;
1150 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1152 5de5890b 2018-10-18 stsp goto done;
1153 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1154 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1155 5de5890b 2018-10-18 stsp free(id_str);
1156 5de5890b 2018-10-18 stsp goto done;
1158 5de5890b 2018-10-18 stsp free(id_str);
1160 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1163 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1164 c1669e2e 2019-01-09 stsp char *child_path;
1165 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1166 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1167 c1669e2e 2019-01-09 stsp te->name) == -1) {
1168 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1169 c1669e2e 2019-01-09 stsp goto done;
1171 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1172 c1669e2e 2019-01-09 stsp root_path, repo);
1173 c1669e2e 2019-01-09 stsp free(child_path);
1175 c1669e2e 2019-01-09 stsp goto done;
1178 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1182 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1183 5de5890b 2018-10-18 stsp free(tree_id);
1184 5de5890b 2018-10-18 stsp return err;
1187 5de5890b 2018-10-18 stsp static const struct got_error *
1188 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1190 5de5890b 2018-10-18 stsp const struct got_error *error;
1191 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1192 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1193 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1194 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1195 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1198 5de5890b 2018-10-18 stsp #ifndef PROFILE
1199 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1200 0f8d269b 2019-01-04 stsp NULL) == -1)
1201 5de5890b 2018-10-18 stsp err(1, "pledge");
1204 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1205 5de5890b 2018-10-18 stsp switch (ch) {
1207 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1210 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1211 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1212 5de5890b 2018-10-18 stsp err(1, "-r option");
1215 5de5890b 2018-10-18 stsp show_ids = 1;
1218 c1669e2e 2019-01-09 stsp recurse = 1;
1222 5de5890b 2018-10-18 stsp /* NOTREACHED */
1226 5de5890b 2018-10-18 stsp argc -= optind;
1227 5de5890b 2018-10-18 stsp argv += optind;
1229 5de5890b 2018-10-18 stsp if (argc == 1)
1230 5de5890b 2018-10-18 stsp path = argv[0];
1231 5de5890b 2018-10-18 stsp else if (argc > 1)
1232 5de5890b 2018-10-18 stsp usage_tree();
1234 5de5890b 2018-10-18 stsp path = "/";
1236 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
1237 5de5890b 2018-10-18 stsp if (cwd == NULL) {
1238 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1239 5de5890b 2018-10-18 stsp goto done;
1241 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1242 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
1243 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1244 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1245 5de5890b 2018-10-18 stsp goto done;
1249 0f8d269b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1250 0f8d269b 2019-01-04 stsp if (error)
1251 0f8d269b 2019-01-04 stsp goto done;
1253 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1254 5de5890b 2018-10-18 stsp if (error != NULL)
1255 5de5890b 2018-10-18 stsp goto done;
1257 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1258 5de5890b 2018-10-18 stsp if (error != NULL)
1259 5de5890b 2018-10-18 stsp goto done;
1261 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1262 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1263 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1264 5de5890b 2018-10-18 stsp if (error != NULL)
1265 5de5890b 2018-10-18 stsp goto done;
1266 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1267 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1268 5de5890b 2018-10-18 stsp if (error != NULL)
1269 5de5890b 2018-10-18 stsp goto done;
1271 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1272 15a94983 2018-12-23 stsp commit_id_str);
1273 5de5890b 2018-10-18 stsp if (error != NULL)
1274 5de5890b 2018-10-18 stsp goto done;
1277 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1278 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1280 5de5890b 2018-10-18 stsp free(in_repo_path);
1281 5de5890b 2018-10-18 stsp free(repo_path);
1282 5de5890b 2018-10-18 stsp free(cwd);
1283 5de5890b 2018-10-18 stsp free(commit_id);
1284 5de5890b 2018-10-18 stsp if (repo) {
1285 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1286 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1287 5de5890b 2018-10-18 stsp if (error == NULL)
1288 5de5890b 2018-10-18 stsp error = repo_error;
1290 5de5890b 2018-10-18 stsp return error;
1293 f42b1b34 2018-03-12 stsp #ifdef notyet
1294 4ed7e80c 2018-05-20 stsp static const struct got_error *
1295 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
1297 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
1298 5c860e29 2018-03-12 stsp git_status_list *status;
1299 5c860e29 2018-03-12 stsp git_status_options statusopts;
1302 5c860e29 2018-03-12 stsp git_libgit2_init();
1304 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
1305 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
1307 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
1308 5c860e29 2018-03-12 stsp errx(1, "bar repository");
1310 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1311 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
1313 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1314 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1315 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1316 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1318 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
1319 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
1321 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
1322 5c860e29 2018-03-12 stsp const git_status_entry *se;
1324 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
1325 5c860e29 2018-03-12 stsp switch (se->status) {
1326 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
1327 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
1329 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
1330 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
1332 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
1333 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
1335 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
1336 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1337 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
1338 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
1340 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
1341 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
1343 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
1344 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
1346 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
1347 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
1349 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
1350 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
1352 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
1353 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1354 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
1355 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
1357 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
1358 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
1360 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
1366 5c860e29 2018-03-12 stsp git_status_list_free(status);
1367 5c860e29 2018-03-12 stsp git_repository_free(repo);
1368 5c860e29 2018-03-12 stsp git_libgit2_shutdown();