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