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