Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 f42b1b34 2018-03-12 stsp
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
33 5c860e29 2018-03-12 stsp
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 63219cd2 2019-01-04 stsp #include "got_privsep.h"
43 5c860e29 2018-03-12 stsp
44 5c860e29 2018-03-12 stsp #ifndef nitems
45 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 5c860e29 2018-03-12 stsp #endif
47 99437157 2018-11-11 stsp
48 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
50 99437157 2018-11-11 stsp
51 99437157 2018-11-11 stsp static void
52 99437157 2018-11-11 stsp catch_sigint(int signo)
53 99437157 2018-11-11 stsp {
54 99437157 2018-11-11 stsp sigint_received = 1;
55 99437157 2018-11-11 stsp }
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigpipe(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigpipe_received = 1;
61 99437157 2018-11-11 stsp }
62 5c860e29 2018-03-12 stsp
63 99437157 2018-11-11 stsp
64 5c860e29 2018-03-12 stsp struct cmd {
65 5c860e29 2018-03-12 stsp const char *cmd_name;
66 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
67 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
68 46a0db7d 2018-03-12 stsp const char *cmd_descr;
69 5c860e29 2018-03-12 stsp };
70 5c860e29 2018-03-12 stsp
71 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
73 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
76 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
77 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
78 5c860e29 2018-03-12 stsp
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
80 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
84 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp #ifdef notyet
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
87 4ed7e80c 2018-05-20 stsp #endif
88 5c860e29 2018-03-12 stsp
89 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
90 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
91 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
92 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
93 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
94 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
95 1b6b95a8 2018-03-12 stsp "show repository history" },
96 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
97 b00d56cd 2018-04-01 stsp "compare files and directories" },
98 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
99 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
100 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
101 5de5890b 2018-10-18 stsp " list files and directories in repository" },
102 f42b1b34 2018-03-12 stsp #ifdef notyet
103 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
104 1b6b95a8 2018-03-12 stsp "show modification status of files" },
105 f42b1b34 2018-03-12 stsp #endif
106 5c860e29 2018-03-12 stsp };
107 5c860e29 2018-03-12 stsp
108 5c860e29 2018-03-12 stsp int
109 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
110 5c860e29 2018-03-12 stsp {
111 5c860e29 2018-03-12 stsp struct cmd *cmd;
112 5c860e29 2018-03-12 stsp unsigned int i;
113 5c860e29 2018-03-12 stsp int ch;
114 1b6b95a8 2018-03-12 stsp int hflag = 0;
115 5c860e29 2018-03-12 stsp
116 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
117 5c860e29 2018-03-12 stsp
118 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
119 5c860e29 2018-03-12 stsp switch (ch) {
120 1b6b95a8 2018-03-12 stsp case 'h':
121 1b6b95a8 2018-03-12 stsp hflag = 1;
122 1b6b95a8 2018-03-12 stsp break;
123 5c860e29 2018-03-12 stsp default:
124 5c860e29 2018-03-12 stsp usage();
125 5c860e29 2018-03-12 stsp /* NOTREACHED */
126 5c860e29 2018-03-12 stsp }
127 5c860e29 2018-03-12 stsp }
128 5c860e29 2018-03-12 stsp
129 5c860e29 2018-03-12 stsp argc -= optind;
130 5c860e29 2018-03-12 stsp argv += optind;
131 1e70621d 2018-03-27 stsp optind = 0;
132 5c860e29 2018-03-12 stsp
133 5c860e29 2018-03-12 stsp if (argc <= 0)
134 5c860e29 2018-03-12 stsp usage();
135 5c860e29 2018-03-12 stsp
136 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
137 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
138 99437157 2018-11-11 stsp
139 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
140 d7d4f210 2018-03-12 stsp const struct got_error *error;
141 d7d4f210 2018-03-12 stsp
142 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
143 5c860e29 2018-03-12 stsp
144 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
145 5c860e29 2018-03-12 stsp continue;
146 5c860e29 2018-03-12 stsp
147 1b6b95a8 2018-03-12 stsp if (hflag)
148 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
149 1b6b95a8 2018-03-12 stsp
150 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
151 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
152 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
153 d7d4f210 2018-03-12 stsp return 1;
154 d7d4f210 2018-03-12 stsp }
155 d7d4f210 2018-03-12 stsp
156 d7d4f210 2018-03-12 stsp return 0;
157 5c860e29 2018-03-12 stsp }
158 5c860e29 2018-03-12 stsp
159 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
160 5c860e29 2018-03-12 stsp return 1;
161 5c860e29 2018-03-12 stsp }
162 5c860e29 2018-03-12 stsp
163 4ed7e80c 2018-05-20 stsp __dead static void
164 5c860e29 2018-03-12 stsp usage(void)
165 5c860e29 2018-03-12 stsp {
166 46a0db7d 2018-03-12 stsp int i;
167 46a0db7d 2018-03-12 stsp
168 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
169 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
170 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
171 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
172 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
173 46a0db7d 2018-03-12 stsp }
174 5c860e29 2018-03-12 stsp exit(1);
175 5c860e29 2018-03-12 stsp }
176 5c860e29 2018-03-12 stsp
177 4ed7e80c 2018-05-20 stsp __dead static void
178 c09a553d 2018-03-12 stsp usage_checkout(void)
179 c09a553d 2018-03-12 stsp {
180 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
181 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
182 c09a553d 2018-03-12 stsp exit(1);
183 92a684f4 2018-03-12 stsp }
184 92a684f4 2018-03-12 stsp
185 92a684f4 2018-03-12 stsp static void
186 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
187 92a684f4 2018-03-12 stsp {
188 92a684f4 2018-03-12 stsp char *worktree_path = arg;
189 92a684f4 2018-03-12 stsp
190 92a684f4 2018-03-12 stsp while (path[0] == '/')
191 92a684f4 2018-03-12 stsp path++;
192 92a684f4 2018-03-12 stsp
193 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
194 99437157 2018-11-11 stsp }
195 99437157 2018-11-11 stsp
196 99437157 2018-11-11 stsp static const struct got_error *
197 99437157 2018-11-11 stsp checkout_cancel(void *arg)
198 99437157 2018-11-11 stsp {
199 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
200 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
201 99437157 2018-11-11 stsp return NULL;
202 c09a553d 2018-03-12 stsp }
203 c09a553d 2018-03-12 stsp
204 4ed7e80c 2018-05-20 stsp static const struct got_error *
205 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
206 c09a553d 2018-03-12 stsp {
207 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
208 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
209 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
210 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
211 c09a553d 2018-03-12 stsp char *repo_path = NULL;
212 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
213 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
214 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
215 c09a553d 2018-03-12 stsp
216 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
217 0bb8a95e 2018-03-12 stsp switch (ch) {
218 0bb8a95e 2018-03-12 stsp case 'p':
219 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
220 0bb8a95e 2018-03-12 stsp break;
221 0bb8a95e 2018-03-12 stsp default:
222 0bb8a95e 2018-03-12 stsp usage();
223 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
224 0bb8a95e 2018-03-12 stsp }
225 0bb8a95e 2018-03-12 stsp }
226 0bb8a95e 2018-03-12 stsp
227 0bb8a95e 2018-03-12 stsp argc -= optind;
228 0bb8a95e 2018-03-12 stsp argv += optind;
229 0bb8a95e 2018-03-12 stsp
230 6715a751 2018-03-16 stsp #ifndef PROFILE
231 63219cd2 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
232 63219cd2 2019-01-04 stsp NULL) == -1)
233 c09a553d 2018-03-12 stsp err(1, "pledge");
234 6715a751 2018-03-16 stsp #endif
235 0bb8a95e 2018-03-12 stsp if (argc == 1) {
236 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
237 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
238 76089277 2018-04-01 stsp if (repo_path == NULL)
239 76089277 2018-04-01 stsp return got_error_from_errno();
240 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
241 76089277 2018-04-01 stsp if (cwd == NULL) {
242 76089277 2018-04-01 stsp error = got_error_from_errno();
243 76089277 2018-04-01 stsp goto done;
244 76089277 2018-04-01 stsp }
245 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
246 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
247 5d7c1dab 2018-04-01 stsp else
248 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
249 76089277 2018-04-01 stsp if (base == NULL) {
250 76089277 2018-04-01 stsp error = got_error_from_errno();
251 76089277 2018-04-01 stsp goto done;
252 76089277 2018-04-01 stsp }
253 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
254 c09a553d 2018-03-12 stsp if (dotgit)
255 c09a553d 2018-03-12 stsp *dotgit = '\0';
256 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
257 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
258 c09a553d 2018-03-12 stsp free(cwd);
259 76089277 2018-04-01 stsp goto done;
260 c09a553d 2018-03-12 stsp }
261 c09a553d 2018-03-12 stsp free(cwd);
262 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
263 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
264 76089277 2018-04-01 stsp if (repo_path == NULL) {
265 76089277 2018-04-01 stsp error = got_error_from_errno();
266 76089277 2018-04-01 stsp goto done;
267 76089277 2018-04-01 stsp }
268 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
269 76089277 2018-04-01 stsp if (worktree_path == NULL) {
270 76089277 2018-04-01 stsp error = got_error_from_errno();
271 76089277 2018-04-01 stsp goto done;
272 76089277 2018-04-01 stsp }
273 c09a553d 2018-03-12 stsp } else
274 c09a553d 2018-03-12 stsp usage_checkout();
275 63219cd2 2019-01-04 stsp
276 63219cd2 2019-01-04 stsp if (unveil(repo_path, "r") != 0 ||
277 63219cd2 2019-01-04 stsp unveil(worktree_path, "rwc") != 0 ||
278 63219cd2 2019-01-04 stsp unveil("/tmp", "rwc") != 0) {
279 63219cd2 2019-01-04 stsp error = got_error_from_errno();
280 63219cd2 2019-01-04 stsp goto done;
281 63219cd2 2019-01-04 stsp }
282 63219cd2 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
283 63219cd2 2019-01-04 stsp if (error != NULL)
284 63219cd2 2019-01-04 stsp goto done;
285 c09a553d 2018-03-12 stsp
286 63219cd2 2019-01-04 stsp if (unveil(NULL, NULL) != 0) {
287 63219cd2 2019-01-04 stsp error = got_error_from_errno();
288 63219cd2 2019-01-04 stsp goto done;
289 63219cd2 2019-01-04 stsp }
290 63219cd2 2019-01-04 stsp
291 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
292 c09a553d 2018-03-12 stsp if (error != NULL)
293 c09a553d 2018-03-12 stsp goto done;
294 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
295 c09a553d 2018-03-12 stsp if (error != NULL)
296 c09a553d 2018-03-12 stsp goto done;
297 c09a553d 2018-03-12 stsp
298 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
299 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
300 c09a553d 2018-03-12 stsp goto done;
301 c09a553d 2018-03-12 stsp
302 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
303 c09a553d 2018-03-12 stsp if (error != NULL)
304 c09a553d 2018-03-12 stsp goto done;
305 c09a553d 2018-03-12 stsp
306 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
307 e5dc7198 2018-12-29 stsp path_prefix);
308 e5dc7198 2018-12-29 stsp if (error != NULL)
309 e5dc7198 2018-12-29 stsp goto done;
310 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
311 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
312 49520a32 2018-12-29 stsp goto done;
313 49520a32 2018-12-29 stsp }
314 49520a32 2018-12-29 stsp
315 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
316 99437157 2018-11-11 stsp checkout_progress, worktree_path, checkout_cancel, NULL);
317 c09a553d 2018-03-12 stsp if (error != NULL)
318 c09a553d 2018-03-12 stsp goto done;
319 c09a553d 2018-03-12 stsp
320 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
321 c09a553d 2018-03-12 stsp
322 c09a553d 2018-03-12 stsp done:
323 76089277 2018-04-01 stsp free(repo_path);
324 507dc3bb 2018-12-29 stsp free(worktree_path);
325 507dc3bb 2018-12-29 stsp return error;
326 507dc3bb 2018-12-29 stsp }
327 507dc3bb 2018-12-29 stsp
328 507dc3bb 2018-12-29 stsp __dead static void
329 507dc3bb 2018-12-29 stsp usage_update(void)
330 507dc3bb 2018-12-29 stsp {
331 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
332 507dc3bb 2018-12-29 stsp getprogname());
333 507dc3bb 2018-12-29 stsp exit(1);
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp static void
337 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
338 507dc3bb 2018-12-29 stsp {
339 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
340 507dc3bb 2018-12-29 stsp return;
341 507dc3bb 2018-12-29 stsp
342 507dc3bb 2018-12-29 stsp while (path[0] == '/')
343 507dc3bb 2018-12-29 stsp path++;
344 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp static const struct got_error *
348 be7061eb 2018-12-30 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
349 be7061eb 2018-12-30 stsp struct got_repository *repo)
350 be7061eb 2018-12-30 stsp {
351 be7061eb 2018-12-30 stsp const struct got_error *err;
352 be7061eb 2018-12-30 stsp struct got_reference *head_ref = NULL;
353 be7061eb 2018-12-30 stsp struct got_object_id *head_commit_id = NULL;
354 be7061eb 2018-12-30 stsp struct got_commit_graph *graph = NULL;
355 be7061eb 2018-12-30 stsp
356 be7061eb 2018-12-30 stsp head_ref = got_worktree_get_head_ref(worktree);
357 be7061eb 2018-12-30 stsp if (head_ref == NULL)
358 be7061eb 2018-12-30 stsp return got_error_from_errno();
359 be7061eb 2018-12-30 stsp
360 2944241d 2018-12-30 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
361 be7061eb 2018-12-30 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
362 be7061eb 2018-12-30 stsp if (err)
363 be7061eb 2018-12-30 stsp goto done;
364 be7061eb 2018-12-30 stsp
365 be7061eb 2018-12-30 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
366 be7061eb 2018-12-30 stsp if (err)
367 be7061eb 2018-12-30 stsp goto done;
368 be7061eb 2018-12-30 stsp
369 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
370 be7061eb 2018-12-30 stsp if (err)
371 be7061eb 2018-12-30 stsp goto done;
372 be7061eb 2018-12-30 stsp while (1) {
373 be7061eb 2018-12-30 stsp struct got_object_id *id;
374 be7061eb 2018-12-30 stsp
375 be7061eb 2018-12-30 stsp if (sigint_received || sigpipe_received)
376 be7061eb 2018-12-30 stsp break;
377 be7061eb 2018-12-30 stsp
378 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_next(&id, graph);
379 be7061eb 2018-12-30 stsp if (err) {
380 be7061eb 2018-12-30 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
381 be7061eb 2018-12-30 stsp err = got_error(GOT_ERR_ANCESTRY);
382 be7061eb 2018-12-30 stsp break;
383 be7061eb 2018-12-30 stsp }
384 be7061eb 2018-12-30 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
385 be7061eb 2018-12-30 stsp break;
386 be7061eb 2018-12-30 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
387 be7061eb 2018-12-30 stsp if (err)
388 be7061eb 2018-12-30 stsp break;
389 be7061eb 2018-12-30 stsp else
390 be7061eb 2018-12-30 stsp continue;
391 be7061eb 2018-12-30 stsp }
392 be7061eb 2018-12-30 stsp if (id == NULL)
393 be7061eb 2018-12-30 stsp break;
394 be7061eb 2018-12-30 stsp if (got_object_id_cmp(id, commit_id) == 0)
395 be7061eb 2018-12-30 stsp break;
396 be7061eb 2018-12-30 stsp }
397 be7061eb 2018-12-30 stsp done:
398 be7061eb 2018-12-30 stsp if (head_ref)
399 be7061eb 2018-12-30 stsp got_ref_close(head_ref);
400 be7061eb 2018-12-30 stsp if (graph)
401 be7061eb 2018-12-30 stsp got_commit_graph_close(graph);
402 be7061eb 2018-12-30 stsp return err;
403 be7061eb 2018-12-30 stsp }
404 be7061eb 2018-12-30 stsp
405 be7061eb 2018-12-30 stsp static const struct got_error *
406 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
407 507dc3bb 2018-12-29 stsp {
408 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
409 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
410 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
411 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
412 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
413 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
414 507dc3bb 2018-12-29 stsp int ch;
415 507dc3bb 2018-12-29 stsp
416 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
417 507dc3bb 2018-12-29 stsp switch (ch) {
418 507dc3bb 2018-12-29 stsp case 'c':
419 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
420 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
421 9c4b8182 2019-01-02 stsp return got_error_from_errno();
422 507dc3bb 2018-12-29 stsp break;
423 507dc3bb 2018-12-29 stsp default:
424 507dc3bb 2018-12-29 stsp usage();
425 507dc3bb 2018-12-29 stsp /* NOTREACHED */
426 507dc3bb 2018-12-29 stsp }
427 507dc3bb 2018-12-29 stsp }
428 507dc3bb 2018-12-29 stsp
429 507dc3bb 2018-12-29 stsp argc -= optind;
430 507dc3bb 2018-12-29 stsp argv += optind;
431 507dc3bb 2018-12-29 stsp
432 507dc3bb 2018-12-29 stsp #ifndef PROFILE
433 507dc3bb 2018-12-29 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
434 507dc3bb 2018-12-29 stsp == -1)
435 507dc3bb 2018-12-29 stsp err(1, "pledge");
436 507dc3bb 2018-12-29 stsp #endif
437 507dc3bb 2018-12-29 stsp if (argc == 0) {
438 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
439 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
440 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
441 507dc3bb 2018-12-29 stsp goto done;
442 507dc3bb 2018-12-29 stsp }
443 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
444 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
445 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
446 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
447 507dc3bb 2018-12-29 stsp goto done;
448 507dc3bb 2018-12-29 stsp }
449 507dc3bb 2018-12-29 stsp } else
450 507dc3bb 2018-12-29 stsp usage_update();
451 507dc3bb 2018-12-29 stsp
452 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
453 507dc3bb 2018-12-29 stsp if (error != NULL)
454 507dc3bb 2018-12-29 stsp goto done;
455 507dc3bb 2018-12-29 stsp
456 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
457 507dc3bb 2018-12-29 stsp if (error != NULL)
458 507dc3bb 2018-12-29 stsp goto done;
459 507dc3bb 2018-12-29 stsp
460 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
461 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
462 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
463 507dc3bb 2018-12-29 stsp if (error != NULL)
464 507dc3bb 2018-12-29 stsp goto done;
465 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
466 9c4b8182 2019-01-02 stsp if (error != NULL)
467 9c4b8182 2019-01-02 stsp goto done;
468 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
469 507dc3bb 2018-12-29 stsp if (error != NULL)
470 507dc3bb 2018-12-29 stsp goto done;
471 507dc3bb 2018-12-29 stsp } else {
472 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
473 507dc3bb 2018-12-29 stsp commit_id_str);
474 507dc3bb 2018-12-29 stsp if (error != NULL)
475 507dc3bb 2018-12-29 stsp goto done;
476 507dc3bb 2018-12-29 stsp }
477 35c965b2 2018-12-29 stsp
478 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
479 be7061eb 2018-12-30 stsp if (error != NULL)
480 be7061eb 2018-12-30 stsp goto done;
481 507dc3bb 2018-12-29 stsp
482 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
483 507dc3bb 2018-12-29 stsp commit_id) != 0) {
484 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
485 507dc3bb 2018-12-29 stsp commit_id);
486 507dc3bb 2018-12-29 stsp if (error)
487 507dc3bb 2018-12-29 stsp goto done;
488 507dc3bb 2018-12-29 stsp }
489 507dc3bb 2018-12-29 stsp
490 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
491 507dc3bb 2018-12-29 stsp update_progress, NULL, checkout_cancel, NULL);
492 507dc3bb 2018-12-29 stsp if (error != NULL)
493 507dc3bb 2018-12-29 stsp goto done;
494 9c4b8182 2019-01-02 stsp
495 9c4b8182 2019-01-02 stsp printf("Updated to commit %s\n", commit_id_str);
496 507dc3bb 2018-12-29 stsp done:
497 c09a553d 2018-03-12 stsp free(worktree_path);
498 507dc3bb 2018-12-29 stsp free(commit_id);
499 9c4b8182 2019-01-02 stsp free(commit_id_str);
500 c09a553d 2018-03-12 stsp return error;
501 c09a553d 2018-03-12 stsp }
502 c09a553d 2018-03-12 stsp
503 f42b1b34 2018-03-12 stsp static const struct got_error *
504 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
505 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
506 5c860e29 2018-03-12 stsp {
507 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
508 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
509 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
510 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
511 79109fed 2018-03-27 stsp
512 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
513 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
514 79109fed 2018-03-27 stsp if (err)
515 79109fed 2018-03-27 stsp return err;
516 79109fed 2018-03-27 stsp
517 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
518 79f35eb3 2018-06-11 stsp if (qid != NULL) {
519 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
520 79109fed 2018-03-27 stsp
521 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
522 79109fed 2018-03-27 stsp if (err)
523 79109fed 2018-03-27 stsp return err;
524 79109fed 2018-03-27 stsp
525 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
526 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
527 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
528 0f2b3dca 2018-12-22 stsp if (err)
529 0f2b3dca 2018-12-22 stsp return err;
530 0f2b3dca 2018-12-22 stsp
531 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
532 79109fed 2018-03-27 stsp if (err)
533 79109fed 2018-03-27 stsp return err;
534 79109fed 2018-03-27 stsp }
535 79109fed 2018-03-27 stsp
536 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
537 0f2b3dca 2018-12-22 stsp if (err)
538 0f2b3dca 2018-12-22 stsp goto done;
539 0f2b3dca 2018-12-22 stsp
540 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
541 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
542 0f2b3dca 2018-12-22 stsp done:
543 79109fed 2018-03-27 stsp if (tree1)
544 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
545 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
546 0f2b3dca 2018-12-22 stsp free(id_str1);
547 0f2b3dca 2018-12-22 stsp free(id_str2);
548 79109fed 2018-03-27 stsp return err;
549 79109fed 2018-03-27 stsp }
550 79109fed 2018-03-27 stsp
551 4bb494d5 2018-06-16 stsp static char *
552 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
553 6c281f94 2018-06-11 stsp {
554 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
555 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
556 6c281f94 2018-06-11 stsp if (p)
557 6c281f94 2018-06-11 stsp *p = '\0';
558 6c281f94 2018-06-11 stsp return s;
559 6c281f94 2018-06-11 stsp }
560 6c281f94 2018-06-11 stsp
561 79109fed 2018-03-27 stsp static const struct got_error *
562 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
563 c0cc5c62 2018-10-18 stsp struct got_repository *repo, int show_patch, int diff_context)
564 79109fed 2018-03-27 stsp {
565 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
566 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
567 6c281f94 2018-06-11 stsp char datebuf[26];
568 45d799e2 2018-12-23 stsp time_t committer_time;
569 45d799e2 2018-12-23 stsp const char *author, *committer;
570 5c860e29 2018-03-12 stsp
571 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
572 8bf5b3c9 2018-03-17 stsp if (err)
573 8bf5b3c9 2018-03-17 stsp return err;
574 788c352e 2018-06-16 stsp
575 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
576 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
577 832c249c 2018-06-10 stsp free(id_str);
578 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
579 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
580 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
581 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
582 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
583 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
584 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
585 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
586 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
587 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
588 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
589 3fe1abad 2018-06-10 stsp int n = 1;
590 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
591 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
592 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
593 a0603db2 2018-06-10 stsp if (err)
594 a0603db2 2018-06-10 stsp return err;
595 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
596 a0603db2 2018-06-10 stsp free(id_str);
597 a0603db2 2018-06-10 stsp }
598 a0603db2 2018-06-10 stsp }
599 832c249c 2018-06-10 stsp
600 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
601 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
602 832c249c 2018-06-10 stsp return got_error_from_errno();
603 8bf5b3c9 2018-03-17 stsp
604 621015ac 2018-07-23 stsp logmsg = logmsg0;
605 832c249c 2018-06-10 stsp do {
606 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
607 832c249c 2018-06-10 stsp if (line)
608 832c249c 2018-06-10 stsp printf(" %s\n", line);
609 832c249c 2018-06-10 stsp } while (line);
610 621015ac 2018-07-23 stsp free(logmsg0);
611 832c249c 2018-06-10 stsp
612 971751ac 2018-03-27 stsp if (show_patch) {
613 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
614 971751ac 2018-03-27 stsp if (err == 0)
615 971751ac 2018-03-27 stsp printf("\n");
616 971751ac 2018-03-27 stsp }
617 07862c20 2018-09-15 stsp
618 d82de447 2018-09-15 stsp fflush(stdout);
619 07862c20 2018-09-15 stsp return err;
620 f42b1b34 2018-03-12 stsp }
621 5c860e29 2018-03-12 stsp
622 f42b1b34 2018-03-12 stsp static const struct got_error *
623 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
624 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
625 15a94983 2018-12-23 stsp int first_parent_traversal)
626 f42b1b34 2018-03-12 stsp {
627 f42b1b34 2018-03-12 stsp const struct got_error *err;
628 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
629 372ccdbb 2018-06-10 stsp
630 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
631 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
632 f42b1b34 2018-03-12 stsp if (err)
633 f42b1b34 2018-03-12 stsp return err;
634 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
635 372ccdbb 2018-06-10 stsp if (err)
636 fcc85cad 2018-07-22 stsp goto done;
637 31cedeaf 2018-09-15 stsp while (1) {
638 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
639 372ccdbb 2018-06-10 stsp struct got_object_id *id;
640 8bf5b3c9 2018-03-17 stsp
641 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
642 84453469 2018-11-11 stsp break;
643 84453469 2018-11-11 stsp
644 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
645 372ccdbb 2018-06-10 stsp if (err) {
646 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
647 9ba79e04 2018-06-11 stsp err = NULL;
648 9ba79e04 2018-06-11 stsp break;
649 9ba79e04 2018-06-11 stsp }
650 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
651 372ccdbb 2018-06-10 stsp break;
652 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
653 372ccdbb 2018-06-10 stsp if (err)
654 372ccdbb 2018-06-10 stsp break;
655 372ccdbb 2018-06-10 stsp else
656 372ccdbb 2018-06-10 stsp continue;
657 7e665116 2018-04-02 stsp }
658 b43fbaa0 2018-06-11 stsp if (id == NULL)
659 7e665116 2018-04-02 stsp break;
660 b43fbaa0 2018-06-11 stsp
661 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
662 b43fbaa0 2018-06-11 stsp if (err)
663 fcc85cad 2018-07-22 stsp break;
664 c0cc5c62 2018-10-18 stsp err = print_commit(commit, id, repo, show_patch, diff_context);
665 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
666 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
667 7e665116 2018-04-02 stsp break;
668 31cedeaf 2018-09-15 stsp }
669 fcc85cad 2018-07-22 stsp done:
670 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
671 f42b1b34 2018-03-12 stsp return err;
672 f42b1b34 2018-03-12 stsp }
673 5c860e29 2018-03-12 stsp
674 4ed7e80c 2018-05-20 stsp __dead static void
675 6f3d1eb0 2018-03-12 stsp usage_log(void)
676 6f3d1eb0 2018-03-12 stsp {
677 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
678 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
679 6f3d1eb0 2018-03-12 stsp exit(1);
680 6f3d1eb0 2018-03-12 stsp }
681 6f3d1eb0 2018-03-12 stsp
682 4ed7e80c 2018-05-20 stsp static const struct got_error *
683 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
684 f42b1b34 2018-03-12 stsp {
685 f42b1b34 2018-03-12 stsp const struct got_error *error;
686 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
687 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
688 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
689 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
690 d142fc45 2018-04-01 stsp char *start_commit = NULL;
691 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
692 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
693 64a96a6d 2018-04-01 stsp const char *errstr;
694 5c860e29 2018-03-12 stsp
695 6715a751 2018-03-16 stsp #ifndef PROFILE
696 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
697 ad242220 2018-09-08 stsp == -1)
698 f42b1b34 2018-03-12 stsp err(1, "pledge");
699 6715a751 2018-03-16 stsp #endif
700 79109fed 2018-03-27 stsp
701 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
702 79109fed 2018-03-27 stsp switch (ch) {
703 79109fed 2018-03-27 stsp case 'p':
704 79109fed 2018-03-27 stsp show_patch = 1;
705 d142fc45 2018-04-01 stsp break;
706 d142fc45 2018-04-01 stsp case 'c':
707 d142fc45 2018-04-01 stsp start_commit = optarg;
708 64a96a6d 2018-04-01 stsp break;
709 c0cc5c62 2018-10-18 stsp case 'C':
710 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
711 4a8520aa 2018-10-18 stsp &errstr);
712 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
713 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
714 c0cc5c62 2018-10-18 stsp break;
715 64a96a6d 2018-04-01 stsp case 'l':
716 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
717 64a96a6d 2018-04-01 stsp if (errstr != NULL)
718 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
719 79109fed 2018-03-27 stsp break;
720 0ed6ed4c 2018-06-13 stsp case 'f':
721 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
722 a0603db2 2018-06-10 stsp break;
723 04ca23f4 2018-07-16 stsp case 'r':
724 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
725 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
726 04ca23f4 2018-07-16 stsp err(1, "-r option");
727 04ca23f4 2018-07-16 stsp break;
728 79109fed 2018-03-27 stsp default:
729 79109fed 2018-03-27 stsp usage();
730 79109fed 2018-03-27 stsp /* NOTREACHED */
731 79109fed 2018-03-27 stsp }
732 79109fed 2018-03-27 stsp }
733 79109fed 2018-03-27 stsp
734 79109fed 2018-03-27 stsp argc -= optind;
735 79109fed 2018-03-27 stsp argv += optind;
736 79109fed 2018-03-27 stsp
737 04ca23f4 2018-07-16 stsp if (argc == 0)
738 04ca23f4 2018-07-16 stsp path = strdup("");
739 04ca23f4 2018-07-16 stsp else if (argc == 1)
740 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
741 04ca23f4 2018-07-16 stsp else
742 6f3d1eb0 2018-03-12 stsp usage_log();
743 04ca23f4 2018-07-16 stsp if (path == NULL)
744 04ca23f4 2018-07-16 stsp return got_error_from_errno();
745 f42b1b34 2018-03-12 stsp
746 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
747 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
748 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
749 04ca23f4 2018-07-16 stsp goto done;
750 04ca23f4 2018-07-16 stsp }
751 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
752 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
753 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
754 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
755 04ca23f4 2018-07-16 stsp goto done;
756 04ca23f4 2018-07-16 stsp }
757 04ca23f4 2018-07-16 stsp }
758 04ca23f4 2018-07-16 stsp
759 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
760 d7d4f210 2018-03-12 stsp if (error != NULL)
761 04ca23f4 2018-07-16 stsp goto done;
762 f42b1b34 2018-03-12 stsp
763 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
764 3235492e 2018-04-01 stsp struct got_reference *head_ref;
765 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
766 3235492e 2018-04-01 stsp if (error != NULL)
767 3235492e 2018-04-01 stsp return error;
768 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
769 3235492e 2018-04-01 stsp got_ref_close(head_ref);
770 3235492e 2018-04-01 stsp if (error != NULL)
771 3235492e 2018-04-01 stsp return error;
772 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
773 3235492e 2018-04-01 stsp } else {
774 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
775 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
776 3235492e 2018-04-01 stsp if (error == NULL) {
777 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
778 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
779 d1f2edc9 2018-06-13 stsp if (error != NULL)
780 d1f2edc9 2018-06-13 stsp return error;
781 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
782 d1f2edc9 2018-06-13 stsp if (error != NULL)
783 d1f2edc9 2018-06-13 stsp return error;
784 d1f2edc9 2018-06-13 stsp }
785 15a94983 2018-12-23 stsp if (commit == NULL) {
786 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
787 d1f2edc9 2018-06-13 stsp start_commit);
788 d1f2edc9 2018-06-13 stsp if (error != NULL)
789 d1f2edc9 2018-06-13 stsp return error;
790 3235492e 2018-04-01 stsp }
791 3235492e 2018-04-01 stsp }
792 d7d4f210 2018-03-12 stsp if (error != NULL)
793 04ca23f4 2018-07-16 stsp goto done;
794 04ca23f4 2018-07-16 stsp
795 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
796 04ca23f4 2018-07-16 stsp if (error != NULL)
797 04ca23f4 2018-07-16 stsp goto done;
798 04ca23f4 2018-07-16 stsp if (in_repo_path) {
799 04ca23f4 2018-07-16 stsp free(path);
800 04ca23f4 2018-07-16 stsp path = in_repo_path;
801 04ca23f4 2018-07-16 stsp }
802 04ca23f4 2018-07-16 stsp
803 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
804 c0cc5c62 2018-10-18 stsp diff_context, limit, first_parent_traversal);
805 04ca23f4 2018-07-16 stsp done:
806 04ca23f4 2018-07-16 stsp free(path);
807 04ca23f4 2018-07-16 stsp free(repo_path);
808 04ca23f4 2018-07-16 stsp free(cwd);
809 f42b1b34 2018-03-12 stsp free(id);
810 ad242220 2018-09-08 stsp if (repo) {
811 ad242220 2018-09-08 stsp const struct got_error *repo_error;
812 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
813 ad242220 2018-09-08 stsp if (error == NULL)
814 ad242220 2018-09-08 stsp error = repo_error;
815 ad242220 2018-09-08 stsp }
816 8bf5b3c9 2018-03-17 stsp return error;
817 5c860e29 2018-03-12 stsp }
818 5c860e29 2018-03-12 stsp
819 4ed7e80c 2018-05-20 stsp __dead static void
820 3f8b7d6a 2018-04-01 stsp usage_diff(void)
821 3f8b7d6a 2018-04-01 stsp {
822 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
823 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
824 3f8b7d6a 2018-04-01 stsp exit(1);
825 b00d56cd 2018-04-01 stsp }
826 b00d56cd 2018-04-01 stsp
827 4ed7e80c 2018-05-20 stsp static const struct got_error *
828 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
829 b00d56cd 2018-04-01 stsp {
830 b00d56cd 2018-04-01 stsp const struct got_error *error;
831 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
832 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
833 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
834 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
835 15a94983 2018-12-23 stsp int type1, type2;
836 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
837 c0cc5c62 2018-10-18 stsp const char *errstr;
838 b00d56cd 2018-04-01 stsp
839 b00d56cd 2018-04-01 stsp #ifndef PROFILE
840 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
841 ad242220 2018-09-08 stsp == -1)
842 b00d56cd 2018-04-01 stsp err(1, "pledge");
843 b00d56cd 2018-04-01 stsp #endif
844 b00d56cd 2018-04-01 stsp
845 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
846 b00d56cd 2018-04-01 stsp switch (ch) {
847 c0cc5c62 2018-10-18 stsp case 'C':
848 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
849 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
850 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
851 c0cc5c62 2018-10-18 stsp break;
852 b00d56cd 2018-04-01 stsp default:
853 b00d56cd 2018-04-01 stsp usage();
854 b00d56cd 2018-04-01 stsp /* NOTREACHED */
855 b00d56cd 2018-04-01 stsp }
856 b00d56cd 2018-04-01 stsp }
857 b00d56cd 2018-04-01 stsp
858 b00d56cd 2018-04-01 stsp argc -= optind;
859 b00d56cd 2018-04-01 stsp argv += optind;
860 b00d56cd 2018-04-01 stsp
861 b00d56cd 2018-04-01 stsp if (argc == 0) {
862 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
863 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
864 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
865 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
866 e1e3f570 2018-04-01 stsp return got_error_from_errno();
867 15a94983 2018-12-23 stsp id_str1 = argv[0];
868 15a94983 2018-12-23 stsp id_str2 = argv[1];
869 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
870 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
871 76089277 2018-04-01 stsp if (repo_path == NULL)
872 76089277 2018-04-01 stsp return got_error_from_errno();
873 15a94983 2018-12-23 stsp id_str1 = argv[1];
874 15a94983 2018-12-23 stsp id_str2 = argv[2];
875 b00d56cd 2018-04-01 stsp } else
876 b00d56cd 2018-04-01 stsp usage_diff();
877 b00d56cd 2018-04-01 stsp
878 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
879 76089277 2018-04-01 stsp free(repo_path);
880 b00d56cd 2018-04-01 stsp if (error != NULL)
881 b00d56cd 2018-04-01 stsp goto done;
882 b00d56cd 2018-04-01 stsp
883 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
884 6402fb3c 2018-09-15 stsp if (error)
885 b00d56cd 2018-04-01 stsp goto done;
886 b00d56cd 2018-04-01 stsp
887 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
888 6402fb3c 2018-09-15 stsp if (error)
889 b00d56cd 2018-04-01 stsp goto done;
890 b00d56cd 2018-04-01 stsp
891 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
892 15a94983 2018-12-23 stsp if (error)
893 15a94983 2018-12-23 stsp goto done;
894 15a94983 2018-12-23 stsp
895 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
896 15a94983 2018-12-23 stsp if (error)
897 15a94983 2018-12-23 stsp goto done;
898 15a94983 2018-12-23 stsp
899 15a94983 2018-12-23 stsp if (type1 != type2) {
900 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
901 b00d56cd 2018-04-01 stsp goto done;
902 b00d56cd 2018-04-01 stsp }
903 b00d56cd 2018-04-01 stsp
904 15a94983 2018-12-23 stsp switch (type1) {
905 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
906 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
907 54156555 2018-12-24 stsp diff_context, repo, stdout);
908 b00d56cd 2018-04-01 stsp break;
909 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
910 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
911 54156555 2018-12-24 stsp diff_context, repo, stdout);
912 b00d56cd 2018-04-01 stsp break;
913 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
914 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
915 15a94983 2018-12-23 stsp id_str2);
916 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
917 c0cc5c62 2018-10-18 stsp repo, stdout);
918 b00d56cd 2018-04-01 stsp break;
919 b00d56cd 2018-04-01 stsp default:
920 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
921 b00d56cd 2018-04-01 stsp }
922 b00d56cd 2018-04-01 stsp
923 b00d56cd 2018-04-01 stsp done:
924 15a94983 2018-12-23 stsp free(id1);
925 15a94983 2018-12-23 stsp free(id2);
926 ad242220 2018-09-08 stsp if (repo) {
927 ad242220 2018-09-08 stsp const struct got_error *repo_error;
928 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
929 ad242220 2018-09-08 stsp if (error == NULL)
930 ad242220 2018-09-08 stsp error = repo_error;
931 ad242220 2018-09-08 stsp }
932 b00d56cd 2018-04-01 stsp return error;
933 404c43c4 2018-06-21 stsp }
934 404c43c4 2018-06-21 stsp
935 404c43c4 2018-06-21 stsp __dead static void
936 404c43c4 2018-06-21 stsp usage_blame(void)
937 404c43c4 2018-06-21 stsp {
938 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
939 404c43c4 2018-06-21 stsp getprogname());
940 404c43c4 2018-06-21 stsp exit(1);
941 b00d56cd 2018-04-01 stsp }
942 b00d56cd 2018-04-01 stsp
943 404c43c4 2018-06-21 stsp static const struct got_error *
944 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
945 404c43c4 2018-06-21 stsp {
946 404c43c4 2018-06-21 stsp const struct got_error *error;
947 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
948 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
949 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
950 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
951 404c43c4 2018-06-21 stsp int ch;
952 404c43c4 2018-06-21 stsp
953 404c43c4 2018-06-21 stsp #ifndef PROFILE
954 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
955 ad242220 2018-09-08 stsp == -1)
956 404c43c4 2018-06-21 stsp err(1, "pledge");
957 404c43c4 2018-06-21 stsp #endif
958 404c43c4 2018-06-21 stsp
959 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
960 404c43c4 2018-06-21 stsp switch (ch) {
961 404c43c4 2018-06-21 stsp case 'c':
962 404c43c4 2018-06-21 stsp commit_id_str = optarg;
963 404c43c4 2018-06-21 stsp break;
964 66bea077 2018-08-02 stsp case 'r':
965 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
966 66bea077 2018-08-02 stsp if (repo_path == NULL)
967 66bea077 2018-08-02 stsp err(1, "-r option");
968 66bea077 2018-08-02 stsp break;
969 404c43c4 2018-06-21 stsp default:
970 404c43c4 2018-06-21 stsp usage();
971 404c43c4 2018-06-21 stsp /* NOTREACHED */
972 404c43c4 2018-06-21 stsp }
973 404c43c4 2018-06-21 stsp }
974 404c43c4 2018-06-21 stsp
975 404c43c4 2018-06-21 stsp argc -= optind;
976 404c43c4 2018-06-21 stsp argv += optind;
977 404c43c4 2018-06-21 stsp
978 a39318fd 2018-08-02 stsp if (argc == 1)
979 404c43c4 2018-06-21 stsp path = argv[0];
980 a39318fd 2018-08-02 stsp else
981 404c43c4 2018-06-21 stsp usage_blame();
982 404c43c4 2018-06-21 stsp
983 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
984 66bea077 2018-08-02 stsp if (cwd == NULL) {
985 66bea077 2018-08-02 stsp error = got_error_from_errno();
986 66bea077 2018-08-02 stsp goto done;
987 66bea077 2018-08-02 stsp }
988 66bea077 2018-08-02 stsp if (repo_path == NULL) {
989 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
990 66bea077 2018-08-02 stsp if (repo_path == NULL) {
991 66bea077 2018-08-02 stsp error = got_error_from_errno();
992 66bea077 2018-08-02 stsp goto done;
993 66bea077 2018-08-02 stsp }
994 66bea077 2018-08-02 stsp }
995 66bea077 2018-08-02 stsp
996 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
997 404c43c4 2018-06-21 stsp if (error != NULL)
998 404c43c4 2018-06-21 stsp goto done;
999 404c43c4 2018-06-21 stsp
1000 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1001 66bea077 2018-08-02 stsp if (error != NULL)
1002 66bea077 2018-08-02 stsp goto done;
1003 66bea077 2018-08-02 stsp
1004 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1005 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1006 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1007 404c43c4 2018-06-21 stsp if (error != NULL)
1008 66bea077 2018-08-02 stsp goto done;
1009 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1010 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1011 404c43c4 2018-06-21 stsp if (error != NULL)
1012 66bea077 2018-08-02 stsp goto done;
1013 404c43c4 2018-06-21 stsp } else {
1014 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1015 15a94983 2018-12-23 stsp commit_id_str);
1016 404c43c4 2018-06-21 stsp if (error != NULL)
1017 66bea077 2018-08-02 stsp goto done;
1018 404c43c4 2018-06-21 stsp }
1019 404c43c4 2018-06-21 stsp
1020 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1021 404c43c4 2018-06-21 stsp done:
1022 66bea077 2018-08-02 stsp free(in_repo_path);
1023 66bea077 2018-08-02 stsp free(repo_path);
1024 66bea077 2018-08-02 stsp free(cwd);
1025 404c43c4 2018-06-21 stsp free(commit_id);
1026 ad242220 2018-09-08 stsp if (repo) {
1027 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1028 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1029 ad242220 2018-09-08 stsp if (error == NULL)
1030 ad242220 2018-09-08 stsp error = repo_error;
1031 ad242220 2018-09-08 stsp }
1032 404c43c4 2018-06-21 stsp return error;
1033 5de5890b 2018-10-18 stsp }
1034 5de5890b 2018-10-18 stsp
1035 5de5890b 2018-10-18 stsp __dead static void
1036 5de5890b 2018-10-18 stsp usage_tree(void)
1037 5de5890b 2018-10-18 stsp {
1038 5de5890b 2018-10-18 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1039 5de5890b 2018-10-18 stsp getprogname());
1040 5de5890b 2018-10-18 stsp exit(1);
1041 5de5890b 2018-10-18 stsp }
1042 5de5890b 2018-10-18 stsp
1043 5de5890b 2018-10-18 stsp
1044 5de5890b 2018-10-18 stsp static const struct got_error *
1045 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1046 5de5890b 2018-10-18 stsp int show_ids, struct got_repository *repo)
1047 5de5890b 2018-10-18 stsp {
1048 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1049 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1050 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1051 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1052 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1053 5de5890b 2018-10-18 stsp
1054 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1055 5de5890b 2018-10-18 stsp if (err)
1056 5de5890b 2018-10-18 stsp goto done;
1057 5de5890b 2018-10-18 stsp
1058 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1059 5de5890b 2018-10-18 stsp if (err)
1060 5de5890b 2018-10-18 stsp goto done;
1061 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1062 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1063 5de5890b 2018-10-18 stsp while (te) {
1064 5de5890b 2018-10-18 stsp char *id = NULL;
1065 84453469 2018-11-11 stsp
1066 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1067 84453469 2018-11-11 stsp break;
1068 84453469 2018-11-11 stsp
1069 5de5890b 2018-10-18 stsp if (show_ids) {
1070 5de5890b 2018-10-18 stsp char *id_str;
1071 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1072 5de5890b 2018-10-18 stsp if (err)
1073 5de5890b 2018-10-18 stsp goto done;
1074 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1075 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1076 5de5890b 2018-10-18 stsp free(id_str);
1077 5de5890b 2018-10-18 stsp goto done;
1078 5de5890b 2018-10-18 stsp }
1079 5de5890b 2018-10-18 stsp free(id_str);
1080 5de5890b 2018-10-18 stsp }
1081 5de5890b 2018-10-18 stsp printf("%s%s%s\n", id ? id : "",
1082 5de5890b 2018-10-18 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1083 5de5890b 2018-10-18 stsp te = SIMPLEQ_NEXT(te, entry);
1084 5de5890b 2018-10-18 stsp free(id);
1085 5de5890b 2018-10-18 stsp }
1086 5de5890b 2018-10-18 stsp done:
1087 5de5890b 2018-10-18 stsp if (tree)
1088 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1089 5de5890b 2018-10-18 stsp free(tree_id);
1090 5de5890b 2018-10-18 stsp return err;
1091 404c43c4 2018-06-21 stsp }
1092 404c43c4 2018-06-21 stsp
1093 5de5890b 2018-10-18 stsp static const struct got_error *
1094 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1095 5de5890b 2018-10-18 stsp {
1096 5de5890b 2018-10-18 stsp const struct got_error *error;
1097 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1098 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1099 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1100 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1101 5de5890b 2018-10-18 stsp int show_ids = 0;
1102 5de5890b 2018-10-18 stsp int ch;
1103 5de5890b 2018-10-18 stsp
1104 5de5890b 2018-10-18 stsp #ifndef PROFILE
1105 5de5890b 2018-10-18 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1106 5de5890b 2018-10-18 stsp == -1)
1107 5de5890b 2018-10-18 stsp err(1, "pledge");
1108 5de5890b 2018-10-18 stsp #endif
1109 5de5890b 2018-10-18 stsp
1110 5de5890b 2018-10-18 stsp while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1111 5de5890b 2018-10-18 stsp switch (ch) {
1112 5de5890b 2018-10-18 stsp case 'c':
1113 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1114 5de5890b 2018-10-18 stsp break;
1115 5de5890b 2018-10-18 stsp case 'r':
1116 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1117 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1118 5de5890b 2018-10-18 stsp err(1, "-r option");
1119 5de5890b 2018-10-18 stsp break;
1120 5de5890b 2018-10-18 stsp case 'i':
1121 5de5890b 2018-10-18 stsp show_ids = 1;
1122 5de5890b 2018-10-18 stsp break;
1123 5de5890b 2018-10-18 stsp default:
1124 5de5890b 2018-10-18 stsp usage();
1125 5de5890b 2018-10-18 stsp /* NOTREACHED */
1126 5de5890b 2018-10-18 stsp }
1127 5de5890b 2018-10-18 stsp }
1128 5de5890b 2018-10-18 stsp
1129 5de5890b 2018-10-18 stsp argc -= optind;
1130 5de5890b 2018-10-18 stsp argv += optind;
1131 5de5890b 2018-10-18 stsp
1132 5de5890b 2018-10-18 stsp if (argc == 1)
1133 5de5890b 2018-10-18 stsp path = argv[0];
1134 5de5890b 2018-10-18 stsp else if (argc > 1)
1135 5de5890b 2018-10-18 stsp usage_tree();
1136 5de5890b 2018-10-18 stsp else
1137 5de5890b 2018-10-18 stsp path = "/";
1138 5de5890b 2018-10-18 stsp
1139 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
1140 5de5890b 2018-10-18 stsp if (cwd == NULL) {
1141 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1142 5de5890b 2018-10-18 stsp goto done;
1143 5de5890b 2018-10-18 stsp }
1144 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1145 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
1146 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1147 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1148 5de5890b 2018-10-18 stsp goto done;
1149 5de5890b 2018-10-18 stsp }
1150 5de5890b 2018-10-18 stsp }
1151 5de5890b 2018-10-18 stsp
1152 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1153 5de5890b 2018-10-18 stsp if (error != NULL)
1154 5de5890b 2018-10-18 stsp goto done;
1155 5de5890b 2018-10-18 stsp
1156 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1157 5de5890b 2018-10-18 stsp if (error != NULL)
1158 5de5890b 2018-10-18 stsp goto done;
1159 5de5890b 2018-10-18 stsp
1160 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1161 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1162 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1163 5de5890b 2018-10-18 stsp if (error != NULL)
1164 5de5890b 2018-10-18 stsp goto done;
1165 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1166 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1167 5de5890b 2018-10-18 stsp if (error != NULL)
1168 5de5890b 2018-10-18 stsp goto done;
1169 5de5890b 2018-10-18 stsp } else {
1170 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1171 15a94983 2018-12-23 stsp commit_id_str);
1172 5de5890b 2018-10-18 stsp if (error != NULL)
1173 5de5890b 2018-10-18 stsp goto done;
1174 5de5890b 2018-10-18 stsp }
1175 5de5890b 2018-10-18 stsp
1176 5de5890b 2018-10-18 stsp error = print_tree(in_repo_path, commit_id, show_ids, repo);
1177 5de5890b 2018-10-18 stsp done:
1178 5de5890b 2018-10-18 stsp free(in_repo_path);
1179 5de5890b 2018-10-18 stsp free(repo_path);
1180 5de5890b 2018-10-18 stsp free(cwd);
1181 5de5890b 2018-10-18 stsp free(commit_id);
1182 5de5890b 2018-10-18 stsp if (repo) {
1183 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1184 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1185 5de5890b 2018-10-18 stsp if (error == NULL)
1186 5de5890b 2018-10-18 stsp error = repo_error;
1187 5de5890b 2018-10-18 stsp }
1188 5de5890b 2018-10-18 stsp return error;
1189 5de5890b 2018-10-18 stsp }
1190 5de5890b 2018-10-18 stsp
1191 f42b1b34 2018-03-12 stsp #ifdef notyet
1192 4ed7e80c 2018-05-20 stsp static const struct got_error *
1193 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
1194 5c860e29 2018-03-12 stsp {
1195 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
1196 5c860e29 2018-03-12 stsp git_status_list *status;
1197 5c860e29 2018-03-12 stsp git_status_options statusopts;
1198 5c860e29 2018-03-12 stsp size_t i;
1199 5c860e29 2018-03-12 stsp
1200 5c860e29 2018-03-12 stsp git_libgit2_init();
1201 5c860e29 2018-03-12 stsp
1202 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
1203 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
1204 5c860e29 2018-03-12 stsp
1205 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
1206 5c860e29 2018-03-12 stsp errx(1, "bar repository");
1207 5c860e29 2018-03-12 stsp
1208 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1209 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
1210 5c860e29 2018-03-12 stsp
1211 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1212 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1213 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1214 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1215 5c860e29 2018-03-12 stsp
1216 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
1217 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
1218 5c860e29 2018-03-12 stsp
1219 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
1220 5c860e29 2018-03-12 stsp const git_status_entry *se;
1221 5c860e29 2018-03-12 stsp
1222 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
1223 5c860e29 2018-03-12 stsp switch (se->status) {
1224 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
1225 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
1226 5c860e29 2018-03-12 stsp break;
1227 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
1228 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
1229 5c860e29 2018-03-12 stsp break;
1230 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
1231 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
1232 5c860e29 2018-03-12 stsp break;
1233 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
1234 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1235 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
1236 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
1237 5c860e29 2018-03-12 stsp break;
1238 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
1239 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
1240 5c860e29 2018-03-12 stsp break;
1241 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
1242 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
1243 5c860e29 2018-03-12 stsp break;
1244 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
1245 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
1246 5c860e29 2018-03-12 stsp break;
1247 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
1248 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
1249 5c860e29 2018-03-12 stsp break;
1250 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
1251 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1252 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
1253 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
1254 5c860e29 2018-03-12 stsp break;
1255 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
1256 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
1257 5c860e29 2018-03-12 stsp break;
1258 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
1259 5c860e29 2018-03-12 stsp default:
1260 5c860e29 2018-03-12 stsp break;
1261 5c860e29 2018-03-12 stsp }
1262 5c860e29 2018-03-12 stsp }
1263 5c860e29 2018-03-12 stsp
1264 5c860e29 2018-03-12 stsp git_status_list_free(status);
1265 5c860e29 2018-03-12 stsp git_repository_free(repo);
1266 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
1267 5c860e29 2018-03-12 stsp
1268 5c860e29 2018-03-12 stsp return 0;
1269 5c860e29 2018-03-12 stsp }
1270 f42b1b34 2018-03-12 stsp #endif