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