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