Blame


1 5c860e29 2018-03-12 stsp /*
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>
4 5c860e29 2018-03-12 stsp *
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.
8 5c860e29 2018-03-12 stsp *
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.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 f42b1b34 2018-03-12 stsp
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>
33 5c860e29 2018-03-12 stsp
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"
43 5c860e29 2018-03-12 stsp
44 5c860e29 2018-03-12 stsp #ifndef nitems
45 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 5c860e29 2018-03-12 stsp #endif
47 99437157 2018-11-11 stsp
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;
50 99437157 2018-11-11 stsp
51 99437157 2018-11-11 stsp static void
52 99437157 2018-11-11 stsp catch_sigint(int signo)
53 99437157 2018-11-11 stsp {
54 99437157 2018-11-11 stsp sigint_received = 1;
55 99437157 2018-11-11 stsp }
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigpipe(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigpipe_received = 1;
61 99437157 2018-11-11 stsp }
62 5c860e29 2018-03-12 stsp
63 99437157 2018-11-11 stsp
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;
69 5c860e29 2018-03-12 stsp };
70 5c860e29 2018-03-12 stsp
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 5c860e29 2018-03-12 stsp
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
81 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
84 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
85 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
87 5c860e29 2018-03-12 stsp
88 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
89 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
90 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
91 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
92 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
93 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
94 1b6b95a8 2018-03-12 stsp "show repository history" },
95 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
96 b00d56cd 2018-04-01 stsp "compare files and directories" },
97 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
98 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
99 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
100 5de5890b 2018-10-18 stsp " list files and directories in repository" },
101 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
102 1b6b95a8 2018-03-12 stsp "show modification status of files" },
103 5c860e29 2018-03-12 stsp };
104 5c860e29 2018-03-12 stsp
105 5c860e29 2018-03-12 stsp int
106 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
107 5c860e29 2018-03-12 stsp {
108 5c860e29 2018-03-12 stsp struct cmd *cmd;
109 5c860e29 2018-03-12 stsp unsigned int i;
110 5c860e29 2018-03-12 stsp int ch;
111 1b6b95a8 2018-03-12 stsp int hflag = 0;
112 5c860e29 2018-03-12 stsp
113 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
114 5c860e29 2018-03-12 stsp
115 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
116 5c860e29 2018-03-12 stsp switch (ch) {
117 1b6b95a8 2018-03-12 stsp case 'h':
118 1b6b95a8 2018-03-12 stsp hflag = 1;
119 1b6b95a8 2018-03-12 stsp break;
120 5c860e29 2018-03-12 stsp default:
121 5c860e29 2018-03-12 stsp usage();
122 5c860e29 2018-03-12 stsp /* NOTREACHED */
123 5c860e29 2018-03-12 stsp }
124 5c860e29 2018-03-12 stsp }
125 5c860e29 2018-03-12 stsp
126 5c860e29 2018-03-12 stsp argc -= optind;
127 5c860e29 2018-03-12 stsp argv += optind;
128 1e70621d 2018-03-27 stsp optind = 0;
129 5c860e29 2018-03-12 stsp
130 5c860e29 2018-03-12 stsp if (argc <= 0)
131 5c860e29 2018-03-12 stsp usage();
132 5c860e29 2018-03-12 stsp
133 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
134 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
135 99437157 2018-11-11 stsp
136 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
137 d7d4f210 2018-03-12 stsp const struct got_error *error;
138 d7d4f210 2018-03-12 stsp
139 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
140 5c860e29 2018-03-12 stsp
141 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
142 5c860e29 2018-03-12 stsp continue;
143 5c860e29 2018-03-12 stsp
144 1b6b95a8 2018-03-12 stsp if (hflag)
145 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
146 1b6b95a8 2018-03-12 stsp
147 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
148 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
149 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
150 d7d4f210 2018-03-12 stsp return 1;
151 d7d4f210 2018-03-12 stsp }
152 d7d4f210 2018-03-12 stsp
153 d7d4f210 2018-03-12 stsp return 0;
154 5c860e29 2018-03-12 stsp }
155 5c860e29 2018-03-12 stsp
156 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
157 5c860e29 2018-03-12 stsp return 1;
158 5c860e29 2018-03-12 stsp }
159 5c860e29 2018-03-12 stsp
160 4ed7e80c 2018-05-20 stsp __dead static void
161 5c860e29 2018-03-12 stsp usage(void)
162 5c860e29 2018-03-12 stsp {
163 46a0db7d 2018-03-12 stsp int i;
164 46a0db7d 2018-03-12 stsp
165 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
166 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
167 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
168 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
169 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
170 46a0db7d 2018-03-12 stsp }
171 5c860e29 2018-03-12 stsp exit(1);
172 5c860e29 2018-03-12 stsp }
173 5c860e29 2018-03-12 stsp
174 0266afb7 2019-01-04 stsp static const struct got_error *
175 0266afb7 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
176 0266afb7 2019-01-04 stsp {
177 0266afb7 2019-01-04 stsp const struct got_error *error;
178 0266afb7 2019-01-04 stsp
179 0266afb7 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
180 0266afb7 2019-01-04 stsp return got_error_from_errno();
181 0266afb7 2019-01-04 stsp
182 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
183 0266afb7 2019-01-04 stsp return got_error_from_errno();
184 0266afb7 2019-01-04 stsp
185 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
186 0266afb7 2019-01-04 stsp return got_error_from_errno();
187 0266afb7 2019-01-04 stsp
188 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
189 0266afb7 2019-01-04 stsp if (error != NULL)
190 0266afb7 2019-01-04 stsp return error;
191 0266afb7 2019-01-04 stsp
192 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
193 0266afb7 2019-01-04 stsp return got_error_from_errno();
194 0266afb7 2019-01-04 stsp
195 0266afb7 2019-01-04 stsp return NULL;
196 0266afb7 2019-01-04 stsp }
197 0266afb7 2019-01-04 stsp
198 4ed7e80c 2018-05-20 stsp __dead static void
199 c09a553d 2018-03-12 stsp usage_checkout(void)
200 c09a553d 2018-03-12 stsp {
201 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
203 c09a553d 2018-03-12 stsp exit(1);
204 92a684f4 2018-03-12 stsp }
205 92a684f4 2018-03-12 stsp
206 92a684f4 2018-03-12 stsp static void
207 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
208 92a684f4 2018-03-12 stsp {
209 92a684f4 2018-03-12 stsp char *worktree_path = arg;
210 92a684f4 2018-03-12 stsp
211 92a684f4 2018-03-12 stsp while (path[0] == '/')
212 92a684f4 2018-03-12 stsp path++;
213 92a684f4 2018-03-12 stsp
214 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
215 99437157 2018-11-11 stsp }
216 99437157 2018-11-11 stsp
217 99437157 2018-11-11 stsp static const struct got_error *
218 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
219 99437157 2018-11-11 stsp {
220 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
221 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
222 99437157 2018-11-11 stsp return NULL;
223 8069f636 2019-01-12 stsp }
224 8069f636 2019-01-12 stsp
225 8069f636 2019-01-12 stsp static const struct got_error *
226 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
227 8069f636 2019-01-12 stsp struct got_repository *repo)
228 8069f636 2019-01-12 stsp {
229 8069f636 2019-01-12 stsp const struct got_error *err;
230 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
231 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
232 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
233 8069f636 2019-01-12 stsp
234 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
235 8069f636 2019-01-12 stsp if (head_ref == NULL)
236 8069f636 2019-01-12 stsp return got_error_from_errno();
237 8069f636 2019-01-12 stsp
238 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
239 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
240 8069f636 2019-01-12 stsp if (err)
241 8069f636 2019-01-12 stsp goto done;
242 8069f636 2019-01-12 stsp
243 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
244 8069f636 2019-01-12 stsp if (err)
245 8069f636 2019-01-12 stsp goto done;
246 8069f636 2019-01-12 stsp
247 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
248 8069f636 2019-01-12 stsp if (err)
249 8069f636 2019-01-12 stsp goto done;
250 8069f636 2019-01-12 stsp while (1) {
251 8069f636 2019-01-12 stsp struct got_object_id *id;
252 8069f636 2019-01-12 stsp
253 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
254 8069f636 2019-01-12 stsp break;
255 8069f636 2019-01-12 stsp
256 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
257 8069f636 2019-01-12 stsp if (err) {
258 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
259 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
260 8069f636 2019-01-12 stsp break;
261 8069f636 2019-01-12 stsp }
262 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
263 8069f636 2019-01-12 stsp break;
264 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
265 8069f636 2019-01-12 stsp if (err)
266 8069f636 2019-01-12 stsp break;
267 8069f636 2019-01-12 stsp else
268 8069f636 2019-01-12 stsp continue;
269 8069f636 2019-01-12 stsp }
270 8069f636 2019-01-12 stsp if (id == NULL)
271 8069f636 2019-01-12 stsp break;
272 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
273 8069f636 2019-01-12 stsp break;
274 8069f636 2019-01-12 stsp }
275 8069f636 2019-01-12 stsp done:
276 8069f636 2019-01-12 stsp if (head_ref)
277 8069f636 2019-01-12 stsp got_ref_close(head_ref);
278 8069f636 2019-01-12 stsp if (graph)
279 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
280 8069f636 2019-01-12 stsp return err;
281 c09a553d 2018-03-12 stsp }
282 c09a553d 2018-03-12 stsp
283 8069f636 2019-01-12 stsp
284 4ed7e80c 2018-05-20 stsp static const struct got_error *
285 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
286 c09a553d 2018-03-12 stsp {
287 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
288 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
289 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
290 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
291 c09a553d 2018-03-12 stsp char *repo_path = NULL;
292 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
293 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
294 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
295 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
296 c09a553d 2018-03-12 stsp
297 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
298 0bb8a95e 2018-03-12 stsp switch (ch) {
299 8069f636 2019-01-12 stsp case 'c':
300 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
301 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
302 8069f636 2019-01-12 stsp return got_error_from_errno();
303 8069f636 2019-01-12 stsp break;
304 0bb8a95e 2018-03-12 stsp case 'p':
305 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
306 0bb8a95e 2018-03-12 stsp break;
307 0bb8a95e 2018-03-12 stsp default:
308 0bb8a95e 2018-03-12 stsp usage();
309 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
310 0bb8a95e 2018-03-12 stsp }
311 0bb8a95e 2018-03-12 stsp }
312 0bb8a95e 2018-03-12 stsp
313 0bb8a95e 2018-03-12 stsp argc -= optind;
314 0bb8a95e 2018-03-12 stsp argv += optind;
315 0bb8a95e 2018-03-12 stsp
316 6715a751 2018-03-16 stsp #ifndef PROFILE
317 63219cd2 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
318 63219cd2 2019-01-04 stsp NULL) == -1)
319 c09a553d 2018-03-12 stsp err(1, "pledge");
320 6715a751 2018-03-16 stsp #endif
321 0bb8a95e 2018-03-12 stsp if (argc == 1) {
322 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
323 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
324 76089277 2018-04-01 stsp if (repo_path == NULL)
325 76089277 2018-04-01 stsp return got_error_from_errno();
326 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
327 76089277 2018-04-01 stsp if (cwd == NULL) {
328 76089277 2018-04-01 stsp error = got_error_from_errno();
329 76089277 2018-04-01 stsp goto done;
330 76089277 2018-04-01 stsp }
331 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
332 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
333 5d7c1dab 2018-04-01 stsp else
334 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
335 76089277 2018-04-01 stsp if (base == NULL) {
336 76089277 2018-04-01 stsp error = got_error_from_errno();
337 76089277 2018-04-01 stsp goto done;
338 76089277 2018-04-01 stsp }
339 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
340 c09a553d 2018-03-12 stsp if (dotgit)
341 c09a553d 2018-03-12 stsp *dotgit = '\0';
342 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
344 c09a553d 2018-03-12 stsp free(cwd);
345 76089277 2018-04-01 stsp goto done;
346 c09a553d 2018-03-12 stsp }
347 c09a553d 2018-03-12 stsp free(cwd);
348 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
349 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
350 76089277 2018-04-01 stsp if (repo_path == NULL) {
351 76089277 2018-04-01 stsp error = got_error_from_errno();
352 76089277 2018-04-01 stsp goto done;
353 76089277 2018-04-01 stsp }
354 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
355 76089277 2018-04-01 stsp if (worktree_path == NULL) {
356 76089277 2018-04-01 stsp error = got_error_from_errno();
357 76089277 2018-04-01 stsp goto done;
358 76089277 2018-04-01 stsp }
359 c09a553d 2018-03-12 stsp } else
360 c09a553d 2018-03-12 stsp usage_checkout();
361 63219cd2 2019-01-04 stsp
362 0266afb7 2019-01-04 stsp error = apply_unveil(repo_path, worktree_path);
363 0266afb7 2019-01-04 stsp if (error)
364 63219cd2 2019-01-04 stsp goto done;
365 c09a553d 2018-03-12 stsp
366 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
367 c09a553d 2018-03-12 stsp if (error != NULL)
368 c09a553d 2018-03-12 stsp goto done;
369 8069f636 2019-01-12 stsp
370 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 c09a553d 2018-03-12 stsp if (error != NULL)
372 c09a553d 2018-03-12 stsp goto done;
373 c09a553d 2018-03-12 stsp
374 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 c09a553d 2018-03-12 stsp goto done;
377 c09a553d 2018-03-12 stsp
378 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
379 c09a553d 2018-03-12 stsp if (error != NULL)
380 c09a553d 2018-03-12 stsp goto done;
381 c09a553d 2018-03-12 stsp
382 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
383 e5dc7198 2018-12-29 stsp path_prefix);
384 e5dc7198 2018-12-29 stsp if (error != NULL)
385 e5dc7198 2018-12-29 stsp goto done;
386 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
387 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
388 49520a32 2018-12-29 stsp goto done;
389 49520a32 2018-12-29 stsp }
390 49520a32 2018-12-29 stsp
391 8069f636 2019-01-12 stsp if (commit_id_str) {
392 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
393 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
394 8069f636 2019-01-12 stsp commit_id_str);
395 8069f636 2019-01-12 stsp if (error != NULL)
396 8069f636 2019-01-12 stsp goto done;
397 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
398 8069f636 2019-01-12 stsp if (error != NULL) {
399 8069f636 2019-01-12 stsp free(commit_id);
400 8069f636 2019-01-12 stsp goto done;
401 8069f636 2019-01-12 stsp }
402 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
403 8069f636 2019-01-12 stsp commit_id);
404 8069f636 2019-01-12 stsp free(commit_id);
405 8069f636 2019-01-12 stsp if (error)
406 8069f636 2019-01-12 stsp goto done;
407 8069f636 2019-01-12 stsp }
408 8069f636 2019-01-12 stsp
409 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
410 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
411 c09a553d 2018-03-12 stsp if (error != NULL)
412 c09a553d 2018-03-12 stsp goto done;
413 c09a553d 2018-03-12 stsp
414 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
415 c09a553d 2018-03-12 stsp
416 c09a553d 2018-03-12 stsp done:
417 8069f636 2019-01-12 stsp free(commit_id_str);
418 76089277 2018-04-01 stsp free(repo_path);
419 507dc3bb 2018-12-29 stsp free(worktree_path);
420 507dc3bb 2018-12-29 stsp return error;
421 507dc3bb 2018-12-29 stsp }
422 507dc3bb 2018-12-29 stsp
423 507dc3bb 2018-12-29 stsp __dead static void
424 507dc3bb 2018-12-29 stsp usage_update(void)
425 507dc3bb 2018-12-29 stsp {
426 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
427 507dc3bb 2018-12-29 stsp getprogname());
428 507dc3bb 2018-12-29 stsp exit(1);
429 507dc3bb 2018-12-29 stsp }
430 507dc3bb 2018-12-29 stsp
431 507dc3bb 2018-12-29 stsp static void
432 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
433 507dc3bb 2018-12-29 stsp {
434 784955db 2019-01-12 stsp int *did_something = arg;
435 784955db 2019-01-12 stsp
436 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
437 507dc3bb 2018-12-29 stsp return;
438 507dc3bb 2018-12-29 stsp
439 784955db 2019-01-12 stsp *did_something = 1;
440 507dc3bb 2018-12-29 stsp while (path[0] == '/')
441 507dc3bb 2018-12-29 stsp path++;
442 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
443 be7061eb 2018-12-30 stsp }
444 be7061eb 2018-12-30 stsp
445 be7061eb 2018-12-30 stsp static const struct got_error *
446 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
447 507dc3bb 2018-12-29 stsp {
448 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
449 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
450 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
451 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
452 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
453 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
454 784955db 2019-01-12 stsp int ch, did_something = 0;
455 507dc3bb 2018-12-29 stsp
456 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
457 507dc3bb 2018-12-29 stsp switch (ch) {
458 507dc3bb 2018-12-29 stsp case 'c':
459 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
460 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
461 9c4b8182 2019-01-02 stsp return got_error_from_errno();
462 507dc3bb 2018-12-29 stsp break;
463 507dc3bb 2018-12-29 stsp default:
464 507dc3bb 2018-12-29 stsp usage();
465 507dc3bb 2018-12-29 stsp /* NOTREACHED */
466 507dc3bb 2018-12-29 stsp }
467 507dc3bb 2018-12-29 stsp }
468 507dc3bb 2018-12-29 stsp
469 507dc3bb 2018-12-29 stsp argc -= optind;
470 507dc3bb 2018-12-29 stsp argv += optind;
471 507dc3bb 2018-12-29 stsp
472 507dc3bb 2018-12-29 stsp #ifndef PROFILE
473 0266afb7 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
474 0266afb7 2019-01-04 stsp NULL) == -1)
475 507dc3bb 2018-12-29 stsp err(1, "pledge");
476 507dc3bb 2018-12-29 stsp #endif
477 507dc3bb 2018-12-29 stsp if (argc == 0) {
478 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
479 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
480 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
481 507dc3bb 2018-12-29 stsp goto done;
482 507dc3bb 2018-12-29 stsp }
483 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
484 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
485 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
486 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
487 507dc3bb 2018-12-29 stsp goto done;
488 507dc3bb 2018-12-29 stsp }
489 507dc3bb 2018-12-29 stsp } else
490 507dc3bb 2018-12-29 stsp usage_update();
491 507dc3bb 2018-12-29 stsp
492 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
493 507dc3bb 2018-12-29 stsp if (error != NULL)
494 507dc3bb 2018-12-29 stsp goto done;
495 507dc3bb 2018-12-29 stsp
496 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
497 507dc3bb 2018-12-29 stsp if (error != NULL)
498 507dc3bb 2018-12-29 stsp goto done;
499 507dc3bb 2018-12-29 stsp
500 8c02d095 2019-02-05 stsp error = apply_unveil(got_repo_get_path(repo),
501 8c02d095 2019-02-05 stsp got_worktree_get_root_path(worktree));
502 0266afb7 2019-01-04 stsp if (error)
503 0266afb7 2019-01-04 stsp goto done;
504 0266afb7 2019-01-04 stsp
505 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
506 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
507 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 507dc3bb 2018-12-29 stsp if (error != NULL)
509 507dc3bb 2018-12-29 stsp goto done;
510 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
511 9c4b8182 2019-01-02 stsp if (error != NULL)
512 9c4b8182 2019-01-02 stsp goto done;
513 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
514 507dc3bb 2018-12-29 stsp if (error != NULL)
515 507dc3bb 2018-12-29 stsp goto done;
516 507dc3bb 2018-12-29 stsp } else {
517 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
518 507dc3bb 2018-12-29 stsp commit_id_str);
519 507dc3bb 2018-12-29 stsp if (error != NULL)
520 507dc3bb 2018-12-29 stsp goto done;
521 507dc3bb 2018-12-29 stsp }
522 35c965b2 2018-12-29 stsp
523 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
524 be7061eb 2018-12-30 stsp if (error != NULL)
525 be7061eb 2018-12-30 stsp goto done;
526 507dc3bb 2018-12-29 stsp
527 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 507dc3bb 2018-12-29 stsp commit_id) != 0) {
529 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
530 507dc3bb 2018-12-29 stsp commit_id);
531 507dc3bb 2018-12-29 stsp if (error)
532 507dc3bb 2018-12-29 stsp goto done;
533 507dc3bb 2018-12-29 stsp }
534 507dc3bb 2018-12-29 stsp
535 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
536 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
537 507dc3bb 2018-12-29 stsp if (error != NULL)
538 507dc3bb 2018-12-29 stsp goto done;
539 9c4b8182 2019-01-02 stsp
540 784955db 2019-01-12 stsp if (did_something)
541 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
542 784955db 2019-01-12 stsp else
543 784955db 2019-01-12 stsp printf("Already up-to-date\n");
544 507dc3bb 2018-12-29 stsp done:
545 c09a553d 2018-03-12 stsp free(worktree_path);
546 507dc3bb 2018-12-29 stsp free(commit_id);
547 9c4b8182 2019-01-02 stsp free(commit_id_str);
548 c09a553d 2018-03-12 stsp return error;
549 c09a553d 2018-03-12 stsp }
550 c09a553d 2018-03-12 stsp
551 f42b1b34 2018-03-12 stsp static const struct got_error *
552 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
553 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
554 5c860e29 2018-03-12 stsp {
555 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
556 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
557 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
558 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
559 79109fed 2018-03-27 stsp
560 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
561 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
562 79109fed 2018-03-27 stsp if (err)
563 79109fed 2018-03-27 stsp return err;
564 79109fed 2018-03-27 stsp
565 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
566 79f35eb3 2018-06-11 stsp if (qid != NULL) {
567 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
568 79109fed 2018-03-27 stsp
569 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
570 79109fed 2018-03-27 stsp if (err)
571 79109fed 2018-03-27 stsp return err;
572 79109fed 2018-03-27 stsp
573 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
574 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
575 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
576 0f2b3dca 2018-12-22 stsp if (err)
577 0f2b3dca 2018-12-22 stsp return err;
578 0f2b3dca 2018-12-22 stsp
579 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
580 79109fed 2018-03-27 stsp if (err)
581 79109fed 2018-03-27 stsp return err;
582 79109fed 2018-03-27 stsp }
583 79109fed 2018-03-27 stsp
584 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
585 0f2b3dca 2018-12-22 stsp if (err)
586 0f2b3dca 2018-12-22 stsp goto done;
587 0f2b3dca 2018-12-22 stsp
588 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
589 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
590 0f2b3dca 2018-12-22 stsp done:
591 79109fed 2018-03-27 stsp if (tree1)
592 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
593 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
594 0f2b3dca 2018-12-22 stsp free(id_str1);
595 0f2b3dca 2018-12-22 stsp free(id_str2);
596 79109fed 2018-03-27 stsp return err;
597 79109fed 2018-03-27 stsp }
598 79109fed 2018-03-27 stsp
599 4bb494d5 2018-06-16 stsp static char *
600 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
601 6c281f94 2018-06-11 stsp {
602 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
603 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
604 6c281f94 2018-06-11 stsp if (p)
605 6c281f94 2018-06-11 stsp *p = '\0';
606 6c281f94 2018-06-11 stsp return s;
607 6c281f94 2018-06-11 stsp }
608 6c281f94 2018-06-11 stsp
609 79109fed 2018-03-27 stsp static const struct got_error *
610 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
611 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
612 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
613 79109fed 2018-03-27 stsp {
614 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
615 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
616 6c281f94 2018-06-11 stsp char datebuf[26];
617 45d799e2 2018-12-23 stsp time_t committer_time;
618 45d799e2 2018-12-23 stsp const char *author, *committer;
619 199a4027 2019-02-02 stsp char *refs_str = NULL;
620 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
621 5c860e29 2018-03-12 stsp
622 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
623 199a4027 2019-02-02 stsp char *s;
624 199a4027 2019-02-02 stsp const char *name;
625 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
626 199a4027 2019-02-02 stsp continue;
627 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
628 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
629 199a4027 2019-02-02 stsp name += 5;
630 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
631 e34f9ed6 2019-02-02 stsp name += 6;
632 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
633 141c2bff 2019-02-04 stsp name += 8;
634 199a4027 2019-02-02 stsp s = refs_str;
635 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
636 199a4027 2019-02-02 stsp name) == -1) {
637 199a4027 2019-02-02 stsp err = got_error_from_errno();
638 199a4027 2019-02-02 stsp free(s);
639 199a4027 2019-02-02 stsp break;
640 199a4027 2019-02-02 stsp }
641 199a4027 2019-02-02 stsp free(s);
642 199a4027 2019-02-02 stsp }
643 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
644 8bf5b3c9 2018-03-17 stsp if (err)
645 8bf5b3c9 2018-03-17 stsp return err;
646 788c352e 2018-06-16 stsp
647 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
648 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
649 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
650 832c249c 2018-06-10 stsp free(id_str);
651 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
652 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
653 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
654 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
655 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
656 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
657 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
658 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
659 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
660 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
661 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
662 3fe1abad 2018-06-10 stsp int n = 1;
663 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
664 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
665 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
666 a0603db2 2018-06-10 stsp if (err)
667 a0603db2 2018-06-10 stsp return err;
668 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
669 a0603db2 2018-06-10 stsp free(id_str);
670 a0603db2 2018-06-10 stsp }
671 a0603db2 2018-06-10 stsp }
672 832c249c 2018-06-10 stsp
673 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
674 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
675 832c249c 2018-06-10 stsp return got_error_from_errno();
676 8bf5b3c9 2018-03-17 stsp
677 621015ac 2018-07-23 stsp logmsg = logmsg0;
678 832c249c 2018-06-10 stsp do {
679 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
680 832c249c 2018-06-10 stsp if (line)
681 832c249c 2018-06-10 stsp printf(" %s\n", line);
682 832c249c 2018-06-10 stsp } while (line);
683 621015ac 2018-07-23 stsp free(logmsg0);
684 832c249c 2018-06-10 stsp
685 971751ac 2018-03-27 stsp if (show_patch) {
686 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
687 971751ac 2018-03-27 stsp if (err == 0)
688 971751ac 2018-03-27 stsp printf("\n");
689 971751ac 2018-03-27 stsp }
690 07862c20 2018-09-15 stsp
691 d82de447 2018-09-15 stsp fflush(stdout);
692 07862c20 2018-09-15 stsp return err;
693 f42b1b34 2018-03-12 stsp }
694 5c860e29 2018-03-12 stsp
695 f42b1b34 2018-03-12 stsp static const struct got_error *
696 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
697 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
698 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
699 f42b1b34 2018-03-12 stsp {
700 f42b1b34 2018-03-12 stsp const struct got_error *err;
701 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
702 372ccdbb 2018-06-10 stsp
703 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
704 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
705 f42b1b34 2018-03-12 stsp if (err)
706 f42b1b34 2018-03-12 stsp return err;
707 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
708 372ccdbb 2018-06-10 stsp if (err)
709 fcc85cad 2018-07-22 stsp goto done;
710 31cedeaf 2018-09-15 stsp while (1) {
711 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
712 372ccdbb 2018-06-10 stsp struct got_object_id *id;
713 8bf5b3c9 2018-03-17 stsp
714 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
715 84453469 2018-11-11 stsp break;
716 84453469 2018-11-11 stsp
717 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
718 372ccdbb 2018-06-10 stsp if (err) {
719 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
720 9ba79e04 2018-06-11 stsp err = NULL;
721 9ba79e04 2018-06-11 stsp break;
722 9ba79e04 2018-06-11 stsp }
723 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
724 372ccdbb 2018-06-10 stsp break;
725 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
726 372ccdbb 2018-06-10 stsp if (err)
727 372ccdbb 2018-06-10 stsp break;
728 372ccdbb 2018-06-10 stsp else
729 372ccdbb 2018-06-10 stsp continue;
730 7e665116 2018-04-02 stsp }
731 b43fbaa0 2018-06-11 stsp if (id == NULL)
732 7e665116 2018-04-02 stsp break;
733 b43fbaa0 2018-06-11 stsp
734 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
735 b43fbaa0 2018-06-11 stsp if (err)
736 fcc85cad 2018-07-22 stsp break;
737 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
738 199a4027 2019-02-02 stsp refs);
739 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
740 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
741 7e665116 2018-04-02 stsp break;
742 31cedeaf 2018-09-15 stsp }
743 fcc85cad 2018-07-22 stsp done:
744 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
745 f42b1b34 2018-03-12 stsp return err;
746 f42b1b34 2018-03-12 stsp }
747 5c860e29 2018-03-12 stsp
748 4ed7e80c 2018-05-20 stsp __dead static void
749 6f3d1eb0 2018-03-12 stsp usage_log(void)
750 6f3d1eb0 2018-03-12 stsp {
751 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
752 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
753 6f3d1eb0 2018-03-12 stsp exit(1);
754 6f3d1eb0 2018-03-12 stsp }
755 6f3d1eb0 2018-03-12 stsp
756 4ed7e80c 2018-05-20 stsp static const struct got_error *
757 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
758 f42b1b34 2018-03-12 stsp {
759 f42b1b34 2018-03-12 stsp const struct got_error *error;
760 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
761 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
762 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
763 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
764 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
765 d142fc45 2018-04-01 stsp char *start_commit = NULL;
766 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
767 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
768 64a96a6d 2018-04-01 stsp const char *errstr;
769 199a4027 2019-02-02 stsp struct got_reflist_head refs;
770 5c860e29 2018-03-12 stsp
771 6715a751 2018-03-16 stsp #ifndef PROFILE
772 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
773 6098196c 2019-01-04 stsp NULL)
774 ad242220 2018-09-08 stsp == -1)
775 f42b1b34 2018-03-12 stsp err(1, "pledge");
776 6715a751 2018-03-16 stsp #endif
777 79109fed 2018-03-27 stsp
778 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
779 79109fed 2018-03-27 stsp switch (ch) {
780 79109fed 2018-03-27 stsp case 'p':
781 79109fed 2018-03-27 stsp show_patch = 1;
782 d142fc45 2018-04-01 stsp break;
783 d142fc45 2018-04-01 stsp case 'c':
784 d142fc45 2018-04-01 stsp start_commit = optarg;
785 64a96a6d 2018-04-01 stsp break;
786 c0cc5c62 2018-10-18 stsp case 'C':
787 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
788 4a8520aa 2018-10-18 stsp &errstr);
789 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
790 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
791 c0cc5c62 2018-10-18 stsp break;
792 64a96a6d 2018-04-01 stsp case 'l':
793 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
794 64a96a6d 2018-04-01 stsp if (errstr != NULL)
795 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
796 79109fed 2018-03-27 stsp break;
797 0ed6ed4c 2018-06-13 stsp case 'f':
798 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
799 a0603db2 2018-06-10 stsp break;
800 04ca23f4 2018-07-16 stsp case 'r':
801 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
802 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
803 04ca23f4 2018-07-16 stsp err(1, "-r option");
804 04ca23f4 2018-07-16 stsp break;
805 79109fed 2018-03-27 stsp default:
806 79109fed 2018-03-27 stsp usage();
807 79109fed 2018-03-27 stsp /* NOTREACHED */
808 79109fed 2018-03-27 stsp }
809 79109fed 2018-03-27 stsp }
810 79109fed 2018-03-27 stsp
811 79109fed 2018-03-27 stsp argc -= optind;
812 79109fed 2018-03-27 stsp argv += optind;
813 79109fed 2018-03-27 stsp
814 04ca23f4 2018-07-16 stsp if (argc == 0)
815 04ca23f4 2018-07-16 stsp path = strdup("");
816 04ca23f4 2018-07-16 stsp else if (argc == 1)
817 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
818 04ca23f4 2018-07-16 stsp else
819 6f3d1eb0 2018-03-12 stsp usage_log();
820 04ca23f4 2018-07-16 stsp if (path == NULL)
821 04ca23f4 2018-07-16 stsp return got_error_from_errno();
822 f42b1b34 2018-03-12 stsp
823 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
824 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
825 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
826 04ca23f4 2018-07-16 stsp goto done;
827 04ca23f4 2018-07-16 stsp }
828 cffc0aa4 2019-02-05 stsp
829 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
830 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
831 cffc0aa4 2019-02-05 stsp goto done;
832 cffc0aa4 2019-02-05 stsp error = NULL;
833 cffc0aa4 2019-02-05 stsp
834 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
835 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
836 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
837 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
838 cffc0aa4 2019-02-05 stsp goto done;
839 04ca23f4 2018-07-16 stsp }
840 04ca23f4 2018-07-16 stsp
841 6098196c 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
842 6098196c 2019-01-04 stsp if (error)
843 6098196c 2019-01-04 stsp goto done;
844 6098196c 2019-01-04 stsp
845 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
846 d7d4f210 2018-03-12 stsp if (error != NULL)
847 04ca23f4 2018-07-16 stsp goto done;
848 f42b1b34 2018-03-12 stsp
849 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
850 3235492e 2018-04-01 stsp struct got_reference *head_ref;
851 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
852 3235492e 2018-04-01 stsp if (error != NULL)
853 3235492e 2018-04-01 stsp return error;
854 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
855 3235492e 2018-04-01 stsp got_ref_close(head_ref);
856 3235492e 2018-04-01 stsp if (error != NULL)
857 3235492e 2018-04-01 stsp return error;
858 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
859 3235492e 2018-04-01 stsp } else {
860 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
861 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
862 3235492e 2018-04-01 stsp if (error == NULL) {
863 1caad61b 2019-02-01 stsp int obj_type;
864 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
865 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
866 d1f2edc9 2018-06-13 stsp if (error != NULL)
867 1caad61b 2019-02-01 stsp goto done;
868 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
869 1caad61b 2019-02-01 stsp if (error != NULL)
870 1caad61b 2019-02-01 stsp goto done;
871 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
872 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
873 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
874 1caad61b 2019-02-01 stsp if (error != NULL)
875 1caad61b 2019-02-01 stsp goto done;
876 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
877 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
878 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
879 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
880 1caad61b 2019-02-01 stsp goto done;
881 1caad61b 2019-02-01 stsp }
882 1caad61b 2019-02-01 stsp free(id);
883 1caad61b 2019-02-01 stsp id = got_object_id_dup(
884 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
885 1caad61b 2019-02-01 stsp if (id == NULL)
886 1caad61b 2019-02-01 stsp error = got_error_from_errno();
887 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
888 1caad61b 2019-02-01 stsp if (error)
889 1caad61b 2019-02-01 stsp goto done;
890 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
891 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
892 1caad61b 2019-02-01 stsp goto done;
893 1caad61b 2019-02-01 stsp }
894 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
895 d1f2edc9 2018-06-13 stsp if (error != NULL)
896 1caad61b 2019-02-01 stsp goto done;
897 d1f2edc9 2018-06-13 stsp }
898 15a94983 2018-12-23 stsp if (commit == NULL) {
899 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
900 d1f2edc9 2018-06-13 stsp start_commit);
901 d1f2edc9 2018-06-13 stsp if (error != NULL)
902 d1f2edc9 2018-06-13 stsp return error;
903 3235492e 2018-04-01 stsp }
904 3235492e 2018-04-01 stsp }
905 d7d4f210 2018-03-12 stsp if (error != NULL)
906 04ca23f4 2018-07-16 stsp goto done;
907 04ca23f4 2018-07-16 stsp
908 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
909 04ca23f4 2018-07-16 stsp if (error != NULL)
910 04ca23f4 2018-07-16 stsp goto done;
911 04ca23f4 2018-07-16 stsp if (in_repo_path) {
912 04ca23f4 2018-07-16 stsp free(path);
913 04ca23f4 2018-07-16 stsp path = in_repo_path;
914 04ca23f4 2018-07-16 stsp }
915 199a4027 2019-02-02 stsp
916 199a4027 2019-02-02 stsp SIMPLEQ_INIT(&refs);
917 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
918 199a4027 2019-02-02 stsp if (error)
919 199a4027 2019-02-02 stsp goto done;
920 04ca23f4 2018-07-16 stsp
921 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
922 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
923 04ca23f4 2018-07-16 stsp done:
924 04ca23f4 2018-07-16 stsp free(path);
925 04ca23f4 2018-07-16 stsp free(repo_path);
926 04ca23f4 2018-07-16 stsp free(cwd);
927 f42b1b34 2018-03-12 stsp free(id);
928 cffc0aa4 2019-02-05 stsp if (worktree)
929 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
930 ad242220 2018-09-08 stsp if (repo) {
931 ad242220 2018-09-08 stsp const struct got_error *repo_error;
932 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
933 ad242220 2018-09-08 stsp if (error == NULL)
934 ad242220 2018-09-08 stsp error = repo_error;
935 ad242220 2018-09-08 stsp }
936 8bf5b3c9 2018-03-17 stsp return error;
937 5c860e29 2018-03-12 stsp }
938 5c860e29 2018-03-12 stsp
939 4ed7e80c 2018-05-20 stsp __dead static void
940 3f8b7d6a 2018-04-01 stsp usage_diff(void)
941 3f8b7d6a 2018-04-01 stsp {
942 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
943 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
944 3f8b7d6a 2018-04-01 stsp exit(1);
945 b00d56cd 2018-04-01 stsp }
946 b00d56cd 2018-04-01 stsp
947 4ed7e80c 2018-05-20 stsp static const struct got_error *
948 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
949 b00d56cd 2018-04-01 stsp {
950 b00d56cd 2018-04-01 stsp const struct got_error *error;
951 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
952 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
953 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
954 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
955 15a94983 2018-12-23 stsp int type1, type2;
956 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
957 c0cc5c62 2018-10-18 stsp const char *errstr;
958 b00d56cd 2018-04-01 stsp
959 b00d56cd 2018-04-01 stsp #ifndef PROFILE
960 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
961 25eccc22 2019-01-04 stsp NULL) == -1)
962 b00d56cd 2018-04-01 stsp err(1, "pledge");
963 b00d56cd 2018-04-01 stsp #endif
964 b00d56cd 2018-04-01 stsp
965 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
966 b00d56cd 2018-04-01 stsp switch (ch) {
967 c0cc5c62 2018-10-18 stsp case 'C':
968 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
969 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
970 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
971 c0cc5c62 2018-10-18 stsp break;
972 b00d56cd 2018-04-01 stsp default:
973 b00d56cd 2018-04-01 stsp usage();
974 b00d56cd 2018-04-01 stsp /* NOTREACHED */
975 b00d56cd 2018-04-01 stsp }
976 b00d56cd 2018-04-01 stsp }
977 b00d56cd 2018-04-01 stsp
978 b00d56cd 2018-04-01 stsp argc -= optind;
979 b00d56cd 2018-04-01 stsp argv += optind;
980 b00d56cd 2018-04-01 stsp
981 b00d56cd 2018-04-01 stsp if (argc == 0) {
982 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
983 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
984 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
985 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
986 e1e3f570 2018-04-01 stsp return got_error_from_errno();
987 15a94983 2018-12-23 stsp id_str1 = argv[0];
988 15a94983 2018-12-23 stsp id_str2 = argv[1];
989 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
990 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
991 76089277 2018-04-01 stsp if (repo_path == NULL)
992 76089277 2018-04-01 stsp return got_error_from_errno();
993 15a94983 2018-12-23 stsp id_str1 = argv[1];
994 15a94983 2018-12-23 stsp id_str2 = argv[2];
995 b00d56cd 2018-04-01 stsp } else
996 b00d56cd 2018-04-01 stsp usage_diff();
997 25eccc22 2019-01-04 stsp
998 25eccc22 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
999 25eccc22 2019-01-04 stsp if (error)
1000 25eccc22 2019-01-04 stsp goto done;
1001 b00d56cd 2018-04-01 stsp
1002 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1003 76089277 2018-04-01 stsp free(repo_path);
1004 b00d56cd 2018-04-01 stsp if (error != NULL)
1005 b00d56cd 2018-04-01 stsp goto done;
1006 b00d56cd 2018-04-01 stsp
1007 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1008 6402fb3c 2018-09-15 stsp if (error)
1009 b00d56cd 2018-04-01 stsp goto done;
1010 b00d56cd 2018-04-01 stsp
1011 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1012 6402fb3c 2018-09-15 stsp if (error)
1013 b00d56cd 2018-04-01 stsp goto done;
1014 b00d56cd 2018-04-01 stsp
1015 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1016 15a94983 2018-12-23 stsp if (error)
1017 15a94983 2018-12-23 stsp goto done;
1018 15a94983 2018-12-23 stsp
1019 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1020 15a94983 2018-12-23 stsp if (error)
1021 15a94983 2018-12-23 stsp goto done;
1022 15a94983 2018-12-23 stsp
1023 15a94983 2018-12-23 stsp if (type1 != type2) {
1024 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1025 b00d56cd 2018-04-01 stsp goto done;
1026 b00d56cd 2018-04-01 stsp }
1027 b00d56cd 2018-04-01 stsp
1028 15a94983 2018-12-23 stsp switch (type1) {
1029 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1030 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1031 54156555 2018-12-24 stsp diff_context, repo, stdout);
1032 b00d56cd 2018-04-01 stsp break;
1033 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1034 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1035 54156555 2018-12-24 stsp diff_context, repo, stdout);
1036 b00d56cd 2018-04-01 stsp break;
1037 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1038 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1039 15a94983 2018-12-23 stsp id_str2);
1040 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1041 c0cc5c62 2018-10-18 stsp repo, stdout);
1042 b00d56cd 2018-04-01 stsp break;
1043 b00d56cd 2018-04-01 stsp default:
1044 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1045 b00d56cd 2018-04-01 stsp }
1046 b00d56cd 2018-04-01 stsp
1047 b00d56cd 2018-04-01 stsp done:
1048 15a94983 2018-12-23 stsp free(id1);
1049 15a94983 2018-12-23 stsp free(id2);
1050 ad242220 2018-09-08 stsp if (repo) {
1051 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1052 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1053 ad242220 2018-09-08 stsp if (error == NULL)
1054 ad242220 2018-09-08 stsp error = repo_error;
1055 ad242220 2018-09-08 stsp }
1056 b00d56cd 2018-04-01 stsp return error;
1057 404c43c4 2018-06-21 stsp }
1058 404c43c4 2018-06-21 stsp
1059 404c43c4 2018-06-21 stsp __dead static void
1060 404c43c4 2018-06-21 stsp usage_blame(void)
1061 404c43c4 2018-06-21 stsp {
1062 9270e621 2019-02-05 stsp fprintf(stderr,
1063 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1064 404c43c4 2018-06-21 stsp getprogname());
1065 404c43c4 2018-06-21 stsp exit(1);
1066 b00d56cd 2018-04-01 stsp }
1067 b00d56cd 2018-04-01 stsp
1068 404c43c4 2018-06-21 stsp static const struct got_error *
1069 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1070 404c43c4 2018-06-21 stsp {
1071 404c43c4 2018-06-21 stsp const struct got_error *error;
1072 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1073 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1074 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1075 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1076 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1077 404c43c4 2018-06-21 stsp int ch;
1078 404c43c4 2018-06-21 stsp
1079 404c43c4 2018-06-21 stsp #ifndef PROFILE
1080 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1081 36e2fb66 2019-01-04 stsp NULL) == -1)
1082 404c43c4 2018-06-21 stsp err(1, "pledge");
1083 404c43c4 2018-06-21 stsp #endif
1084 404c43c4 2018-06-21 stsp
1085 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1086 404c43c4 2018-06-21 stsp switch (ch) {
1087 404c43c4 2018-06-21 stsp case 'c':
1088 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1089 404c43c4 2018-06-21 stsp break;
1090 66bea077 2018-08-02 stsp case 'r':
1091 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1092 66bea077 2018-08-02 stsp if (repo_path == NULL)
1093 66bea077 2018-08-02 stsp err(1, "-r option");
1094 66bea077 2018-08-02 stsp break;
1095 404c43c4 2018-06-21 stsp default:
1096 404c43c4 2018-06-21 stsp usage();
1097 404c43c4 2018-06-21 stsp /* NOTREACHED */
1098 404c43c4 2018-06-21 stsp }
1099 404c43c4 2018-06-21 stsp }
1100 404c43c4 2018-06-21 stsp
1101 404c43c4 2018-06-21 stsp argc -= optind;
1102 404c43c4 2018-06-21 stsp argv += optind;
1103 404c43c4 2018-06-21 stsp
1104 a39318fd 2018-08-02 stsp if (argc == 1)
1105 404c43c4 2018-06-21 stsp path = argv[0];
1106 a39318fd 2018-08-02 stsp else
1107 404c43c4 2018-06-21 stsp usage_blame();
1108 404c43c4 2018-06-21 stsp
1109 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1110 66bea077 2018-08-02 stsp if (cwd == NULL) {
1111 66bea077 2018-08-02 stsp error = got_error_from_errno();
1112 66bea077 2018-08-02 stsp goto done;
1113 66bea077 2018-08-02 stsp }
1114 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1115 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1116 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1117 66bea077 2018-08-02 stsp goto done;
1118 0c06baac 2019-02-05 stsp else
1119 0c06baac 2019-02-05 stsp error = NULL;
1120 0c06baac 2019-02-05 stsp if (worktree) {
1121 0c06baac 2019-02-05 stsp repo_path =
1122 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1123 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1124 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1125 0c06baac 2019-02-05 stsp if (error)
1126 0c06baac 2019-02-05 stsp goto done;
1127 0c06baac 2019-02-05 stsp } else {
1128 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1129 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1130 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1131 0c06baac 2019-02-05 stsp goto done;
1132 0c06baac 2019-02-05 stsp }
1133 66bea077 2018-08-02 stsp }
1134 66bea077 2018-08-02 stsp }
1135 36e2fb66 2019-01-04 stsp
1136 36e2fb66 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1137 36e2fb66 2019-01-04 stsp if (error)
1138 36e2fb66 2019-01-04 stsp goto done;
1139 66bea077 2018-08-02 stsp
1140 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1141 404c43c4 2018-06-21 stsp if (error != NULL)
1142 404c43c4 2018-06-21 stsp goto done;
1143 404c43c4 2018-06-21 stsp
1144 0c06baac 2019-02-05 stsp if (worktree) {
1145 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1146 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1147 0c06baac 2019-02-05 stsp if (asprintf(&p, "%s/%s/%s",
1148 0c06baac 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1149 0c06baac 2019-02-05 stsp worktree_subdir, path) == -1) {
1150 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1151 0c06baac 2019-02-05 stsp goto done;
1152 0c06baac 2019-02-05 stsp }
1153 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1154 0c06baac 2019-02-05 stsp free(p);
1155 0c06baac 2019-02-05 stsp } else {
1156 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1157 0c06baac 2019-02-05 stsp }
1158 0c06baac 2019-02-05 stsp if (error)
1159 66bea077 2018-08-02 stsp goto done;
1160 66bea077 2018-08-02 stsp
1161 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1162 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1163 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1164 404c43c4 2018-06-21 stsp if (error != NULL)
1165 66bea077 2018-08-02 stsp goto done;
1166 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1167 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1168 404c43c4 2018-06-21 stsp if (error != NULL)
1169 66bea077 2018-08-02 stsp goto done;
1170 404c43c4 2018-06-21 stsp } else {
1171 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1172 15a94983 2018-12-23 stsp commit_id_str);
1173 404c43c4 2018-06-21 stsp if (error != NULL)
1174 66bea077 2018-08-02 stsp goto done;
1175 404c43c4 2018-06-21 stsp }
1176 404c43c4 2018-06-21 stsp
1177 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1178 404c43c4 2018-06-21 stsp done:
1179 66bea077 2018-08-02 stsp free(in_repo_path);
1180 66bea077 2018-08-02 stsp free(repo_path);
1181 66bea077 2018-08-02 stsp free(cwd);
1182 404c43c4 2018-06-21 stsp free(commit_id);
1183 0c06baac 2019-02-05 stsp if (worktree)
1184 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1185 ad242220 2018-09-08 stsp if (repo) {
1186 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1187 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1188 ad242220 2018-09-08 stsp if (error == NULL)
1189 ad242220 2018-09-08 stsp error = repo_error;
1190 ad242220 2018-09-08 stsp }
1191 404c43c4 2018-06-21 stsp return error;
1192 5de5890b 2018-10-18 stsp }
1193 5de5890b 2018-10-18 stsp
1194 5de5890b 2018-10-18 stsp __dead static void
1195 5de5890b 2018-10-18 stsp usage_tree(void)
1196 5de5890b 2018-10-18 stsp {
1197 c1669e2e 2019-01-09 stsp fprintf(stderr,
1198 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1199 5de5890b 2018-10-18 stsp getprogname());
1200 5de5890b 2018-10-18 stsp exit(1);
1201 5de5890b 2018-10-18 stsp }
1202 5de5890b 2018-10-18 stsp
1203 c1669e2e 2019-01-09 stsp static void
1204 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1205 c1669e2e 2019-01-09 stsp const char *root_path)
1206 c1669e2e 2019-01-09 stsp {
1207 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1208 5de5890b 2018-10-18 stsp
1209 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1210 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1211 c1669e2e 2019-01-09 stsp path++;
1212 c1669e2e 2019-01-09 stsp
1213 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1214 c1669e2e 2019-01-09 stsp is_root_path ? "" : "/",
1215 c1669e2e 2019-01-09 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1216 c1669e2e 2019-01-09 stsp }
1217 c1669e2e 2019-01-09 stsp
1218 5de5890b 2018-10-18 stsp static const struct got_error *
1219 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1220 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1221 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1222 5de5890b 2018-10-18 stsp {
1223 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1224 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1225 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1226 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1227 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1228 5de5890b 2018-10-18 stsp
1229 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1230 5de5890b 2018-10-18 stsp if (err)
1231 5de5890b 2018-10-18 stsp goto done;
1232 5de5890b 2018-10-18 stsp
1233 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1234 5de5890b 2018-10-18 stsp if (err)
1235 5de5890b 2018-10-18 stsp goto done;
1236 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1237 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1238 5de5890b 2018-10-18 stsp while (te) {
1239 5de5890b 2018-10-18 stsp char *id = NULL;
1240 84453469 2018-11-11 stsp
1241 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1242 84453469 2018-11-11 stsp break;
1243 84453469 2018-11-11 stsp
1244 5de5890b 2018-10-18 stsp if (show_ids) {
1245 5de5890b 2018-10-18 stsp char *id_str;
1246 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1247 5de5890b 2018-10-18 stsp if (err)
1248 5de5890b 2018-10-18 stsp goto done;
1249 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1250 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1251 5de5890b 2018-10-18 stsp free(id_str);
1252 5de5890b 2018-10-18 stsp goto done;
1253 5de5890b 2018-10-18 stsp }
1254 5de5890b 2018-10-18 stsp free(id_str);
1255 5de5890b 2018-10-18 stsp }
1256 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1257 5de5890b 2018-10-18 stsp free(id);
1258 c1669e2e 2019-01-09 stsp
1259 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1260 c1669e2e 2019-01-09 stsp char *child_path;
1261 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1262 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1263 c1669e2e 2019-01-09 stsp te->name) == -1) {
1264 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1265 c1669e2e 2019-01-09 stsp goto done;
1266 c1669e2e 2019-01-09 stsp }
1267 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1268 c1669e2e 2019-01-09 stsp root_path, repo);
1269 c1669e2e 2019-01-09 stsp free(child_path);
1270 c1669e2e 2019-01-09 stsp if (err)
1271 c1669e2e 2019-01-09 stsp goto done;
1272 c1669e2e 2019-01-09 stsp }
1273 c1669e2e 2019-01-09 stsp
1274 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1275 5de5890b 2018-10-18 stsp }
1276 5de5890b 2018-10-18 stsp done:
1277 5de5890b 2018-10-18 stsp if (tree)
1278 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1279 5de5890b 2018-10-18 stsp free(tree_id);
1280 5de5890b 2018-10-18 stsp return err;
1281 404c43c4 2018-06-21 stsp }
1282 404c43c4 2018-06-21 stsp
1283 5de5890b 2018-10-18 stsp static const struct got_error *
1284 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1285 5de5890b 2018-10-18 stsp {
1286 5de5890b 2018-10-18 stsp const struct got_error *error;
1287 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1288 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1289 7a2c19d6 2019-02-05 stsp const char *path;
1290 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1291 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1292 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1293 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1294 5de5890b 2018-10-18 stsp int ch;
1295 5de5890b 2018-10-18 stsp
1296 5de5890b 2018-10-18 stsp #ifndef PROFILE
1297 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1298 0f8d269b 2019-01-04 stsp NULL) == -1)
1299 5de5890b 2018-10-18 stsp err(1, "pledge");
1300 5de5890b 2018-10-18 stsp #endif
1301 5de5890b 2018-10-18 stsp
1302 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1303 5de5890b 2018-10-18 stsp switch (ch) {
1304 5de5890b 2018-10-18 stsp case 'c':
1305 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1306 5de5890b 2018-10-18 stsp break;
1307 5de5890b 2018-10-18 stsp case 'r':
1308 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1309 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1310 5de5890b 2018-10-18 stsp err(1, "-r option");
1311 5de5890b 2018-10-18 stsp break;
1312 5de5890b 2018-10-18 stsp case 'i':
1313 5de5890b 2018-10-18 stsp show_ids = 1;
1314 5de5890b 2018-10-18 stsp break;
1315 c1669e2e 2019-01-09 stsp case 'R':
1316 c1669e2e 2019-01-09 stsp recurse = 1;
1317 c1669e2e 2019-01-09 stsp break;
1318 5de5890b 2018-10-18 stsp default:
1319 5de5890b 2018-10-18 stsp usage();
1320 5de5890b 2018-10-18 stsp /* NOTREACHED */
1321 5de5890b 2018-10-18 stsp }
1322 5de5890b 2018-10-18 stsp }
1323 5de5890b 2018-10-18 stsp
1324 5de5890b 2018-10-18 stsp argc -= optind;
1325 5de5890b 2018-10-18 stsp argv += optind;
1326 5de5890b 2018-10-18 stsp
1327 5de5890b 2018-10-18 stsp if (argc == 1)
1328 5de5890b 2018-10-18 stsp path = argv[0];
1329 5de5890b 2018-10-18 stsp else if (argc > 1)
1330 5de5890b 2018-10-18 stsp usage_tree();
1331 5de5890b 2018-10-18 stsp else
1332 7a2c19d6 2019-02-05 stsp path = NULL;
1333 7a2c19d6 2019-02-05 stsp
1334 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1335 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1336 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1337 9bf7a39b 2019-02-05 stsp goto done;
1338 9bf7a39b 2019-02-05 stsp }
1339 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1340 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1341 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1342 7a2c19d6 2019-02-05 stsp goto done;
1343 7a2c19d6 2019-02-05 stsp else
1344 7a2c19d6 2019-02-05 stsp error = NULL;
1345 7a2c19d6 2019-02-05 stsp if (worktree) {
1346 7a2c19d6 2019-02-05 stsp repo_path =
1347 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1348 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1349 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1350 7a2c19d6 2019-02-05 stsp if (error)
1351 7a2c19d6 2019-02-05 stsp goto done;
1352 7a2c19d6 2019-02-05 stsp } else {
1353 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1354 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1355 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1356 7a2c19d6 2019-02-05 stsp goto done;
1357 7a2c19d6 2019-02-05 stsp }
1358 5de5890b 2018-10-18 stsp }
1359 5de5890b 2018-10-18 stsp }
1360 5de5890b 2018-10-18 stsp
1361 0f8d269b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1362 0f8d269b 2019-01-04 stsp if (error)
1363 0f8d269b 2019-01-04 stsp goto done;
1364 0f8d269b 2019-01-04 stsp
1365 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1366 5de5890b 2018-10-18 stsp if (error != NULL)
1367 5de5890b 2018-10-18 stsp goto done;
1368 5de5890b 2018-10-18 stsp
1369 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1370 9bf7a39b 2019-02-05 stsp if (worktree) {
1371 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1372 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1373 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1374 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1375 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1376 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1377 9bf7a39b 2019-02-05 stsp goto done;
1378 9bf7a39b 2019-02-05 stsp }
1379 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1380 9bf7a39b 2019-02-05 stsp free(p);
1381 9bf7a39b 2019-02-05 stsp if (error)
1382 9bf7a39b 2019-02-05 stsp goto done;
1383 9bf7a39b 2019-02-05 stsp } else
1384 9bf7a39b 2019-02-05 stsp path = "/";
1385 9bf7a39b 2019-02-05 stsp }
1386 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1387 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1388 9bf7a39b 2019-02-05 stsp if (error != NULL)
1389 9bf7a39b 2019-02-05 stsp goto done;
1390 9bf7a39b 2019-02-05 stsp }
1391 5de5890b 2018-10-18 stsp
1392 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1393 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1394 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1395 5de5890b 2018-10-18 stsp if (error != NULL)
1396 5de5890b 2018-10-18 stsp goto done;
1397 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1398 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1399 5de5890b 2018-10-18 stsp if (error != NULL)
1400 5de5890b 2018-10-18 stsp goto done;
1401 5de5890b 2018-10-18 stsp } else {
1402 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1403 15a94983 2018-12-23 stsp commit_id_str);
1404 5de5890b 2018-10-18 stsp if (error != NULL)
1405 5de5890b 2018-10-18 stsp goto done;
1406 5de5890b 2018-10-18 stsp }
1407 5de5890b 2018-10-18 stsp
1408 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1409 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1410 5de5890b 2018-10-18 stsp done:
1411 5de5890b 2018-10-18 stsp free(in_repo_path);
1412 5de5890b 2018-10-18 stsp free(repo_path);
1413 5de5890b 2018-10-18 stsp free(cwd);
1414 5de5890b 2018-10-18 stsp free(commit_id);
1415 7a2c19d6 2019-02-05 stsp if (worktree)
1416 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1417 5de5890b 2018-10-18 stsp if (repo) {
1418 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1419 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1420 5de5890b 2018-10-18 stsp if (error == NULL)
1421 5de5890b 2018-10-18 stsp error = repo_error;
1422 5de5890b 2018-10-18 stsp }
1423 5de5890b 2018-10-18 stsp return error;
1424 5de5890b 2018-10-18 stsp }
1425 5de5890b 2018-10-18 stsp
1426 6bad629b 2019-02-04 stsp __dead static void
1427 6bad629b 2019-02-04 stsp usage_status(void)
1428 6bad629b 2019-02-04 stsp {
1429 6bad629b 2019-02-04 stsp fprintf(stderr, "usage: %s status [worktree-path]\n", getprogname());
1430 6bad629b 2019-02-04 stsp exit(1);
1431 6bad629b 2019-02-04 stsp }
1432 5c860e29 2018-03-12 stsp
1433 6bad629b 2019-02-04 stsp static void
1434 6bad629b 2019-02-04 stsp print_status(void *arg, unsigned char status, const char *path)
1435 6bad629b 2019-02-04 stsp {
1436 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1437 6bad629b 2019-02-04 stsp }
1438 5c860e29 2018-03-12 stsp
1439 6bad629b 2019-02-04 stsp static const struct got_error *
1440 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1441 6bad629b 2019-02-04 stsp {
1442 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1443 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1444 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1445 6bad629b 2019-02-04 stsp char *worktree_path = NULL;
1446 6bad629b 2019-02-04 stsp int ch;
1447 5c860e29 2018-03-12 stsp
1448 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1449 6bad629b 2019-02-04 stsp switch (ch) {
1450 5c860e29 2018-03-12 stsp default:
1451 6bad629b 2019-02-04 stsp usage();
1452 6bad629b 2019-02-04 stsp /* NOTREACHED */
1453 5c860e29 2018-03-12 stsp }
1454 5c860e29 2018-03-12 stsp }
1455 5c860e29 2018-03-12 stsp
1456 6bad629b 2019-02-04 stsp argc -= optind;
1457 6bad629b 2019-02-04 stsp argv += optind;
1458 5c860e29 2018-03-12 stsp
1459 6bad629b 2019-02-04 stsp #ifndef PROFILE
1460 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1461 6bad629b 2019-02-04 stsp NULL) == -1)
1462 6bad629b 2019-02-04 stsp err(1, "pledge");
1463 f42b1b34 2018-03-12 stsp #endif
1464 6bad629b 2019-02-04 stsp if (argc == 0) {
1465 6bad629b 2019-02-04 stsp worktree_path = getcwd(NULL, 0);
1466 6bad629b 2019-02-04 stsp if (worktree_path == NULL) {
1467 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1468 6bad629b 2019-02-04 stsp goto done;
1469 6bad629b 2019-02-04 stsp }
1470 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1471 6bad629b 2019-02-04 stsp worktree_path = realpath(argv[0], NULL);
1472 6bad629b 2019-02-04 stsp if (worktree_path == NULL) {
1473 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1474 6bad629b 2019-02-04 stsp goto done;
1475 6bad629b 2019-02-04 stsp }
1476 6bad629b 2019-02-04 stsp } else
1477 6bad629b 2019-02-04 stsp usage_status();
1478 6bad629b 2019-02-04 stsp
1479 6bad629b 2019-02-04 stsp error = got_worktree_open(&worktree, worktree_path);
1480 6bad629b 2019-02-04 stsp if (error != NULL)
1481 6bad629b 2019-02-04 stsp goto done;
1482 6bad629b 2019-02-04 stsp
1483 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1484 6bad629b 2019-02-04 stsp if (error != NULL)
1485 6bad629b 2019-02-04 stsp goto done;
1486 6bad629b 2019-02-04 stsp
1487 c7f4312f 2019-02-05 stsp error = apply_unveil(got_repo_get_path(repo),
1488 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(worktree));
1489 6bad629b 2019-02-04 stsp if (error)
1490 6bad629b 2019-02-04 stsp goto done;
1491 6bad629b 2019-02-04 stsp
1492 6bad629b 2019-02-04 stsp error = got_worktree_status(worktree, repo, print_status, NULL,
1493 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1494 6bad629b 2019-02-04 stsp done:
1495 6bad629b 2019-02-04 stsp free(worktree_path);
1496 6bad629b 2019-02-04 stsp return error;
1497 6bad629b 2019-02-04 stsp }