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