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