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