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