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 f42b1b34 2018-03-12 stsp
21 5c860e29 2018-03-12 stsp #include <err.h>
22 5c860e29 2018-03-12 stsp #include <errno.h>
23 5c860e29 2018-03-12 stsp #include <locale.h>
24 5c860e29 2018-03-12 stsp #include <stdio.h>
25 5c860e29 2018-03-12 stsp #include <stdlib.h>
26 5c860e29 2018-03-12 stsp #include <string.h>
27 5c860e29 2018-03-12 stsp #include <unistd.h>
28 c09a553d 2018-03-12 stsp #include <libgen.h>
29 5c860e29 2018-03-12 stsp
30 f42b1b34 2018-03-12 stsp #include "got_error.h"
31 f42b1b34 2018-03-12 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_reference.h"
33 f42b1b34 2018-03-12 stsp #include "got_repository.h"
34 c09a553d 2018-03-12 stsp #include "got_worktree.h"
35 79109fed 2018-03-27 stsp #include "got_diff.h"
36 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
37 5c860e29 2018-03-12 stsp
38 5c860e29 2018-03-12 stsp #ifndef nitems
39 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 5c860e29 2018-03-12 stsp #endif
41 5c860e29 2018-03-12 stsp
42 5c860e29 2018-03-12 stsp struct cmd {
43 5c860e29 2018-03-12 stsp const char *cmd_name;
44 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
45 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
46 46a0db7d 2018-03-12 stsp const char *cmd_descr;
47 5c860e29 2018-03-12 stsp };
48 5c860e29 2018-03-12 stsp
49 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
50 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
51 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
52 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
53 5c860e29 2018-03-12 stsp
54 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
55 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
56 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
57 4ed7e80c 2018-05-20 stsp #ifdef notyet
58 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
59 4ed7e80c 2018-05-20 stsp #endif
60 5c860e29 2018-03-12 stsp
61 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
62 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
63 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
64 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
65 1b6b95a8 2018-03-12 stsp "show repository history" },
66 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
67 b00d56cd 2018-04-01 stsp "compare files and directories" },
68 f42b1b34 2018-03-12 stsp #ifdef notyet
69 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
70 1b6b95a8 2018-03-12 stsp "show modification status of files" },
71 f42b1b34 2018-03-12 stsp #endif
72 5c860e29 2018-03-12 stsp };
73 5c860e29 2018-03-12 stsp
74 5c860e29 2018-03-12 stsp int
75 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
76 5c860e29 2018-03-12 stsp {
77 5c860e29 2018-03-12 stsp struct cmd *cmd;
78 5c860e29 2018-03-12 stsp unsigned int i;
79 5c860e29 2018-03-12 stsp int ch;
80 1b6b95a8 2018-03-12 stsp int hflag = 0;
81 5c860e29 2018-03-12 stsp
82 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
83 5c860e29 2018-03-12 stsp
84 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
85 5c860e29 2018-03-12 stsp switch (ch) {
86 1b6b95a8 2018-03-12 stsp case 'h':
87 1b6b95a8 2018-03-12 stsp hflag = 1;
88 1b6b95a8 2018-03-12 stsp break;
89 5c860e29 2018-03-12 stsp default:
90 5c860e29 2018-03-12 stsp usage();
91 5c860e29 2018-03-12 stsp /* NOTREACHED */
92 5c860e29 2018-03-12 stsp }
93 5c860e29 2018-03-12 stsp }
94 5c860e29 2018-03-12 stsp
95 5c860e29 2018-03-12 stsp argc -= optind;
96 5c860e29 2018-03-12 stsp argv += optind;
97 1e70621d 2018-03-27 stsp optind = 0;
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp if (argc <= 0)
100 5c860e29 2018-03-12 stsp usage();
101 5c860e29 2018-03-12 stsp
102 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
103 d7d4f210 2018-03-12 stsp const struct got_error *error;
104 d7d4f210 2018-03-12 stsp
105 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
106 5c860e29 2018-03-12 stsp
107 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
108 5c860e29 2018-03-12 stsp continue;
109 5c860e29 2018-03-12 stsp
110 1b6b95a8 2018-03-12 stsp if (hflag)
111 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
112 1b6b95a8 2018-03-12 stsp
113 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
114 d7d4f210 2018-03-12 stsp if (error) {
115 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
116 d7d4f210 2018-03-12 stsp return 1;
117 d7d4f210 2018-03-12 stsp }
118 d7d4f210 2018-03-12 stsp
119 d7d4f210 2018-03-12 stsp return 0;
120 5c860e29 2018-03-12 stsp }
121 5c860e29 2018-03-12 stsp
122 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 5c860e29 2018-03-12 stsp return 1;
124 5c860e29 2018-03-12 stsp }
125 5c860e29 2018-03-12 stsp
126 4ed7e80c 2018-05-20 stsp __dead static void
127 5c860e29 2018-03-12 stsp usage(void)
128 5c860e29 2018-03-12 stsp {
129 46a0db7d 2018-03-12 stsp int i;
130 46a0db7d 2018-03-12 stsp
131 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
132 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
133 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
134 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
135 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
136 46a0db7d 2018-03-12 stsp }
137 5c860e29 2018-03-12 stsp exit(1);
138 5c860e29 2018-03-12 stsp }
139 5c860e29 2018-03-12 stsp
140 4ed7e80c 2018-05-20 stsp __dead static void
141 c09a553d 2018-03-12 stsp usage_checkout(void)
142 c09a553d 2018-03-12 stsp {
143 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
145 c09a553d 2018-03-12 stsp exit(1);
146 92a684f4 2018-03-12 stsp }
147 92a684f4 2018-03-12 stsp
148 92a684f4 2018-03-12 stsp static void
149 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
150 92a684f4 2018-03-12 stsp {
151 92a684f4 2018-03-12 stsp char *worktree_path = arg;
152 92a684f4 2018-03-12 stsp
153 92a684f4 2018-03-12 stsp while (path[0] == '/')
154 92a684f4 2018-03-12 stsp path++;
155 92a684f4 2018-03-12 stsp
156 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
157 c09a553d 2018-03-12 stsp }
158 c09a553d 2018-03-12 stsp
159 4ed7e80c 2018-05-20 stsp static const struct got_error *
160 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
161 c09a553d 2018-03-12 stsp {
162 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
163 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
164 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
165 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
166 c09a553d 2018-03-12 stsp char *repo_path = NULL;
167 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
168 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
169 0bb8a95e 2018-03-12 stsp int ch;
170 c09a553d 2018-03-12 stsp
171 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
172 0bb8a95e 2018-03-12 stsp switch (ch) {
173 0bb8a95e 2018-03-12 stsp case 'p':
174 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
175 0bb8a95e 2018-03-12 stsp break;
176 0bb8a95e 2018-03-12 stsp default:
177 0bb8a95e 2018-03-12 stsp usage();
178 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
179 0bb8a95e 2018-03-12 stsp }
180 0bb8a95e 2018-03-12 stsp }
181 0bb8a95e 2018-03-12 stsp
182 0bb8a95e 2018-03-12 stsp argc -= optind;
183 0bb8a95e 2018-03-12 stsp argv += optind;
184 0bb8a95e 2018-03-12 stsp
185 6715a751 2018-03-16 stsp #ifndef PROFILE
186 2178c42e 2018-04-22 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
187 c09a553d 2018-03-12 stsp err(1, "pledge");
188 6715a751 2018-03-16 stsp #endif
189 0bb8a95e 2018-03-12 stsp if (argc == 1) {
190 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
191 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
192 76089277 2018-04-01 stsp if (repo_path == NULL)
193 76089277 2018-04-01 stsp return got_error_from_errno();
194 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
195 76089277 2018-04-01 stsp if (cwd == NULL) {
196 76089277 2018-04-01 stsp error = got_error_from_errno();
197 76089277 2018-04-01 stsp goto done;
198 76089277 2018-04-01 stsp }
199 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
200 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
201 5d7c1dab 2018-04-01 stsp else
202 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
203 76089277 2018-04-01 stsp if (base == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
205 76089277 2018-04-01 stsp goto done;
206 76089277 2018-04-01 stsp }
207 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
208 c09a553d 2018-03-12 stsp if (dotgit)
209 c09a553d 2018-03-12 stsp *dotgit = '\0';
210 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
212 c09a553d 2018-03-12 stsp free(cwd);
213 76089277 2018-04-01 stsp goto done;
214 c09a553d 2018-03-12 stsp }
215 c09a553d 2018-03-12 stsp free(cwd);
216 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
217 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
218 76089277 2018-04-01 stsp if (repo_path == NULL) {
219 76089277 2018-04-01 stsp error = got_error_from_errno();
220 76089277 2018-04-01 stsp goto done;
221 76089277 2018-04-01 stsp }
222 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
223 76089277 2018-04-01 stsp if (worktree_path == NULL) {
224 76089277 2018-04-01 stsp error = got_error_from_errno();
225 76089277 2018-04-01 stsp goto done;
226 76089277 2018-04-01 stsp }
227 c09a553d 2018-03-12 stsp } else
228 c09a553d 2018-03-12 stsp usage_checkout();
229 c09a553d 2018-03-12 stsp
230 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
231 c09a553d 2018-03-12 stsp if (error != NULL)
232 c09a553d 2018-03-12 stsp goto done;
233 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
234 c09a553d 2018-03-12 stsp if (error != NULL)
235 c09a553d 2018-03-12 stsp goto done;
236 c09a553d 2018-03-12 stsp
237 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
238 c09a553d 2018-03-12 stsp if (error != NULL)
239 c09a553d 2018-03-12 stsp goto done;
240 c09a553d 2018-03-12 stsp
241 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
242 c09a553d 2018-03-12 stsp if (error != NULL)
243 c09a553d 2018-03-12 stsp goto done;
244 c09a553d 2018-03-12 stsp
245 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
246 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
247 c09a553d 2018-03-12 stsp if (error != NULL)
248 c09a553d 2018-03-12 stsp goto done;
249 c09a553d 2018-03-12 stsp
250 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
251 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
252 c09a553d 2018-03-12 stsp
253 c09a553d 2018-03-12 stsp done:
254 76089277 2018-04-01 stsp free(repo_path);
255 c09a553d 2018-03-12 stsp free(worktree_path);
256 c09a553d 2018-03-12 stsp return error;
257 c09a553d 2018-03-12 stsp }
258 c09a553d 2018-03-12 stsp
259 f42b1b34 2018-03-12 stsp static const struct got_error *
260 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
261 f42b1b34 2018-03-12 stsp struct got_repository *repo)
262 5c860e29 2018-03-12 stsp {
263 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
264 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
265 79109fed 2018-03-27 stsp struct got_object *obj;
266 79109fed 2018-03-27 stsp struct got_parent_id *pid;
267 79109fed 2018-03-27 stsp
268 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
269 79109fed 2018-03-27 stsp if (err)
270 79109fed 2018-03-27 stsp return err;
271 79109fed 2018-03-27 stsp
272 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
273 79109fed 2018-03-27 stsp got_object_close(obj);
274 79109fed 2018-03-27 stsp if (err)
275 79109fed 2018-03-27 stsp return err;
276 79109fed 2018-03-27 stsp
277 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 79109fed 2018-03-27 stsp if (pid != NULL) {
279 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
280 79109fed 2018-03-27 stsp
281 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
282 79109fed 2018-03-27 stsp if (err)
283 79109fed 2018-03-27 stsp return err;
284 79109fed 2018-03-27 stsp
285 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
286 79109fed 2018-03-27 stsp got_object_close(obj);
287 79109fed 2018-03-27 stsp if (err)
288 79109fed 2018-03-27 stsp return err;
289 79109fed 2018-03-27 stsp
290 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
291 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
292 79109fed 2018-03-27 stsp if (err)
293 79109fed 2018-03-27 stsp return err;
294 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
295 79109fed 2018-03-27 stsp got_object_close(obj);
296 79109fed 2018-03-27 stsp if (err)
297 79109fed 2018-03-27 stsp return err;
298 79109fed 2018-03-27 stsp }
299 79109fed 2018-03-27 stsp
300 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
301 79109fed 2018-03-27 stsp if (tree1)
302 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
303 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
304 79109fed 2018-03-27 stsp return err;
305 79109fed 2018-03-27 stsp }
306 79109fed 2018-03-27 stsp
307 79109fed 2018-03-27 stsp static const struct got_error *
308 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
309 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
310 79109fed 2018-03-27 stsp {
311 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
312 8bf5b3c9 2018-03-17 stsp char *buf;
313 5c860e29 2018-03-12 stsp
314 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
315 8bf5b3c9 2018-03-17 stsp if (err)
316 8bf5b3c9 2018-03-17 stsp return err;
317 5c860e29 2018-03-12 stsp
318 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
319 b65ae19a 2018-04-24 stsp printf("commit %s\n", buf);
320 b65ae19a 2018-04-24 stsp printf("author: %s\n", commit->author);
321 f3d135e1 2018-04-01 stsp if (strcmp(commit->author, commit->committer) != 0)
322 b65ae19a 2018-04-24 stsp printf("committer: %s\n", commit->committer);
323 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
324 8bf5b3c9 2018-03-17 stsp
325 971751ac 2018-03-27 stsp if (show_patch) {
326 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
327 971751ac 2018-03-27 stsp if (err == 0)
328 971751ac 2018-03-27 stsp printf("\n");
329 971751ac 2018-03-27 stsp }
330 79109fed 2018-03-27 stsp
331 8bf5b3c9 2018-03-17 stsp free(buf);
332 79109fed 2018-03-27 stsp return err;
333 f42b1b34 2018-03-12 stsp }
334 5c860e29 2018-03-12 stsp
335 f42b1b34 2018-03-12 stsp static const struct got_error *
336 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
337 64a96a6d 2018-04-01 stsp struct got_repository *repo, int show_patch, int limit)
338 f42b1b34 2018-03-12 stsp {
339 f42b1b34 2018-03-12 stsp const struct got_error *err;
340 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
341 372ccdbb 2018-06-10 stsp int ncommits;
342 372ccdbb 2018-06-10 stsp
343 372ccdbb 2018-06-10 stsp err = got_commit_graph_open(&graph, root_id, repo);
344 f42b1b34 2018-03-12 stsp if (err)
345 f42b1b34 2018-03-12 stsp return err;
346 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
347 372ccdbb 2018-06-10 stsp if (err)
348 0a585a0d 2018-03-17 stsp return err;
349 372ccdbb 2018-06-10 stsp do {
350 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
351 372ccdbb 2018-06-10 stsp struct got_object_id *id;
352 8bf5b3c9 2018-03-17 stsp
353 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_next(&commit, &id, graph);
354 372ccdbb 2018-06-10 stsp if (err) {
355 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
356 372ccdbb 2018-06-10 stsp break;
357 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
358 372ccdbb 2018-06-10 stsp graph, 1, repo);
359 372ccdbb 2018-06-10 stsp if (err)
360 372ccdbb 2018-06-10 stsp break;
361 372ccdbb 2018-06-10 stsp else
362 372ccdbb 2018-06-10 stsp continue;
363 7e665116 2018-04-02 stsp }
364 372ccdbb 2018-06-10 stsp if (commit == NULL)
365 7e665116 2018-04-02 stsp break;
366 372ccdbb 2018-06-10 stsp err = print_commit(commit, id, repo, show_patch);
367 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
368 7e665116 2018-04-02 stsp break;
369 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
370 8bf5b3c9 2018-03-17 stsp
371 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
372 f42b1b34 2018-03-12 stsp return err;
373 f42b1b34 2018-03-12 stsp }
374 5c860e29 2018-03-12 stsp
375 4ed7e80c 2018-05-20 stsp __dead static void
376 6f3d1eb0 2018-03-12 stsp usage_log(void)
377 6f3d1eb0 2018-03-12 stsp {
378 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
379 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
380 6f3d1eb0 2018-03-12 stsp exit(1);
381 6f3d1eb0 2018-03-12 stsp }
382 6f3d1eb0 2018-03-12 stsp
383 4ed7e80c 2018-05-20 stsp static const struct got_error *
384 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
385 f42b1b34 2018-03-12 stsp {
386 f42b1b34 2018-03-12 stsp const struct got_error *error;
387 f42b1b34 2018-03-12 stsp struct got_repository *repo;
388 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
389 f42b1b34 2018-03-12 stsp struct got_object *obj;
390 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
391 d142fc45 2018-04-01 stsp char *start_commit = NULL;
392 79109fed 2018-03-27 stsp int ch;
393 64a96a6d 2018-04-01 stsp int show_patch = 0, limit = 0;
394 64a96a6d 2018-04-01 stsp const char *errstr;
395 5c860e29 2018-03-12 stsp
396 6715a751 2018-03-16 stsp #ifndef PROFILE
397 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
398 f42b1b34 2018-03-12 stsp err(1, "pledge");
399 6715a751 2018-03-16 stsp #endif
400 79109fed 2018-03-27 stsp
401 64a96a6d 2018-04-01 stsp while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
402 79109fed 2018-03-27 stsp switch (ch) {
403 79109fed 2018-03-27 stsp case 'p':
404 79109fed 2018-03-27 stsp show_patch = 1;
405 d142fc45 2018-04-01 stsp break;
406 d142fc45 2018-04-01 stsp case 'c':
407 d142fc45 2018-04-01 stsp start_commit = optarg;
408 64a96a6d 2018-04-01 stsp break;
409 64a96a6d 2018-04-01 stsp case 'l':
410 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
411 64a96a6d 2018-04-01 stsp if (errstr != NULL)
412 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
413 79109fed 2018-03-27 stsp break;
414 79109fed 2018-03-27 stsp default:
415 79109fed 2018-03-27 stsp usage();
416 79109fed 2018-03-27 stsp /* NOTREACHED */
417 79109fed 2018-03-27 stsp }
418 79109fed 2018-03-27 stsp }
419 79109fed 2018-03-27 stsp
420 79109fed 2018-03-27 stsp argc -= optind;
421 79109fed 2018-03-27 stsp argv += optind;
422 79109fed 2018-03-27 stsp
423 79109fed 2018-03-27 stsp if (argc == 0) {
424 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
425 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
426 e1e3f570 2018-04-01 stsp return got_error_from_errno();
427 3235492e 2018-04-01 stsp } else if (argc == 1) {
428 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
429 76089277 2018-04-01 stsp if (repo_path == NULL)
430 76089277 2018-04-01 stsp return got_error_from_errno();
431 3235492e 2018-04-01 stsp } else
432 6f3d1eb0 2018-03-12 stsp usage_log();
433 f42b1b34 2018-03-12 stsp
434 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
435 76089277 2018-04-01 stsp free(repo_path);
436 d7d4f210 2018-03-12 stsp if (error != NULL)
437 d7d4f210 2018-03-12 stsp return error;
438 f42b1b34 2018-03-12 stsp
439 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
440 3235492e 2018-04-01 stsp struct got_reference *head_ref;
441 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
442 3235492e 2018-04-01 stsp if (error != NULL)
443 3235492e 2018-04-01 stsp return error;
444 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
445 3235492e 2018-04-01 stsp got_ref_close(head_ref);
446 3235492e 2018-04-01 stsp if (error != NULL)
447 3235492e 2018-04-01 stsp return error;
448 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
449 3235492e 2018-04-01 stsp } else {
450 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
451 3235492e 2018-04-01 stsp if (error == NULL) {
452 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
453 3235492e 2018-04-01 stsp if (id == NULL)
454 3235492e 2018-04-01 stsp error = got_error_from_errno();
455 3235492e 2018-04-01 stsp }
456 3235492e 2018-04-01 stsp }
457 d7d4f210 2018-03-12 stsp if (error != NULL)
458 d7d4f210 2018-03-12 stsp return error;
459 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
460 64a96a6d 2018-04-01 stsp error = print_commits(obj, id, repo, show_patch, limit);
461 8bf5b3c9 2018-03-17 stsp else
462 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
463 f42b1b34 2018-03-12 stsp got_object_close(obj);
464 f42b1b34 2018-03-12 stsp free(id);
465 f42b1b34 2018-03-12 stsp got_repo_close(repo);
466 8bf5b3c9 2018-03-17 stsp return error;
467 5c860e29 2018-03-12 stsp }
468 5c860e29 2018-03-12 stsp
469 4ed7e80c 2018-05-20 stsp __dead static void
470 3f8b7d6a 2018-04-01 stsp usage_diff(void)
471 3f8b7d6a 2018-04-01 stsp {
472 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
473 3f8b7d6a 2018-04-01 stsp getprogname());
474 3f8b7d6a 2018-04-01 stsp exit(1);
475 b00d56cd 2018-04-01 stsp }
476 b00d56cd 2018-04-01 stsp
477 4ed7e80c 2018-05-20 stsp static const struct got_error *
478 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
479 b00d56cd 2018-04-01 stsp {
480 b00d56cd 2018-04-01 stsp const struct got_error *error;
481 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
482 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
483 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
484 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
485 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
486 b00d56cd 2018-04-01 stsp int ch;
487 b00d56cd 2018-04-01 stsp
488 b00d56cd 2018-04-01 stsp #ifndef PROFILE
489 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
490 b00d56cd 2018-04-01 stsp err(1, "pledge");
491 b00d56cd 2018-04-01 stsp #endif
492 b00d56cd 2018-04-01 stsp
493 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
494 b00d56cd 2018-04-01 stsp switch (ch) {
495 b00d56cd 2018-04-01 stsp default:
496 b00d56cd 2018-04-01 stsp usage();
497 b00d56cd 2018-04-01 stsp /* NOTREACHED */
498 b00d56cd 2018-04-01 stsp }
499 b00d56cd 2018-04-01 stsp }
500 b00d56cd 2018-04-01 stsp
501 b00d56cd 2018-04-01 stsp argc -= optind;
502 b00d56cd 2018-04-01 stsp argv += optind;
503 b00d56cd 2018-04-01 stsp
504 b00d56cd 2018-04-01 stsp if (argc == 0) {
505 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
506 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
507 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
508 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
509 e1e3f570 2018-04-01 stsp return got_error_from_errno();
510 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
511 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
512 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
513 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
514 76089277 2018-04-01 stsp if (repo_path == NULL)
515 76089277 2018-04-01 stsp return got_error_from_errno();
516 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
517 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
518 b00d56cd 2018-04-01 stsp } else
519 b00d56cd 2018-04-01 stsp usage_diff();
520 b00d56cd 2018-04-01 stsp
521 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
522 76089277 2018-04-01 stsp free(repo_path);
523 b00d56cd 2018-04-01 stsp if (error != NULL)
524 b00d56cd 2018-04-01 stsp goto done;
525 b00d56cd 2018-04-01 stsp
526 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
527 b00d56cd 2018-04-01 stsp if (error == NULL) {
528 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
529 b00d56cd 2018-04-01 stsp if (id1 == NULL)
530 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
531 b00d56cd 2018-04-01 stsp }
532 b00d56cd 2018-04-01 stsp if (error != NULL)
533 b00d56cd 2018-04-01 stsp goto done;
534 b00d56cd 2018-04-01 stsp
535 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
536 b00d56cd 2018-04-01 stsp if (error == NULL) {
537 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
538 b00d56cd 2018-04-01 stsp if (id2 == NULL)
539 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
540 b00d56cd 2018-04-01 stsp }
541 b00d56cd 2018-04-01 stsp if (error != NULL)
542 b00d56cd 2018-04-01 stsp goto done;
543 b00d56cd 2018-04-01 stsp
544 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
545 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
546 b00d56cd 2018-04-01 stsp goto done;
547 b00d56cd 2018-04-01 stsp }
548 b00d56cd 2018-04-01 stsp
549 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
550 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
551 11528a82 2018-05-19 stsp error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
552 b00d56cd 2018-04-01 stsp break;
553 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
554 11528a82 2018-05-19 stsp error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
555 b00d56cd 2018-04-01 stsp break;
556 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
557 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
558 b00d56cd 2018-04-01 stsp break;
559 b00d56cd 2018-04-01 stsp default:
560 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
561 b00d56cd 2018-04-01 stsp }
562 b00d56cd 2018-04-01 stsp
563 b00d56cd 2018-04-01 stsp done:
564 b00d56cd 2018-04-01 stsp if (obj1)
565 b00d56cd 2018-04-01 stsp got_object_close(obj1);
566 b00d56cd 2018-04-01 stsp if (obj2)
567 b00d56cd 2018-04-01 stsp got_object_close(obj2);
568 b00d56cd 2018-04-01 stsp if (id1)
569 b00d56cd 2018-04-01 stsp free(id1);
570 b00d56cd 2018-04-01 stsp if (id2)
571 b00d56cd 2018-04-01 stsp free(id2);
572 b00d56cd 2018-04-01 stsp if (repo)
573 b00d56cd 2018-04-01 stsp got_repo_close(repo);
574 b00d56cd 2018-04-01 stsp return error;
575 b00d56cd 2018-04-01 stsp }
576 b00d56cd 2018-04-01 stsp
577 f42b1b34 2018-03-12 stsp #ifdef notyet
578 4ed7e80c 2018-05-20 stsp static const struct got_error *
579 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
580 5c860e29 2018-03-12 stsp {
581 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
582 5c860e29 2018-03-12 stsp git_status_list *status;
583 5c860e29 2018-03-12 stsp git_status_options statusopts;
584 5c860e29 2018-03-12 stsp size_t i;
585 5c860e29 2018-03-12 stsp
586 5c860e29 2018-03-12 stsp git_libgit2_init();
587 5c860e29 2018-03-12 stsp
588 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
589 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
590 5c860e29 2018-03-12 stsp
591 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
592 5c860e29 2018-03-12 stsp errx(1, "bar repository");
593 5c860e29 2018-03-12 stsp
594 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
595 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
596 5c860e29 2018-03-12 stsp
597 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
598 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
599 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
600 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
601 5c860e29 2018-03-12 stsp
602 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
603 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
604 5c860e29 2018-03-12 stsp
605 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
606 5c860e29 2018-03-12 stsp const git_status_entry *se;
607 5c860e29 2018-03-12 stsp
608 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
609 5c860e29 2018-03-12 stsp switch (se->status) {
610 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
611 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
612 5c860e29 2018-03-12 stsp break;
613 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
614 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
615 5c860e29 2018-03-12 stsp break;
616 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
617 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
618 5c860e29 2018-03-12 stsp break;
619 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
620 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
621 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
622 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
623 5c860e29 2018-03-12 stsp break;
624 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
625 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
626 5c860e29 2018-03-12 stsp break;
627 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
628 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
629 5c860e29 2018-03-12 stsp break;
630 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
631 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
632 5c860e29 2018-03-12 stsp break;
633 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
634 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
635 5c860e29 2018-03-12 stsp break;
636 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
637 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
638 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
639 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
640 5c860e29 2018-03-12 stsp break;
641 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
642 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
643 5c860e29 2018-03-12 stsp break;
644 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
645 5c860e29 2018-03-12 stsp default:
646 5c860e29 2018-03-12 stsp break;
647 5c860e29 2018-03-12 stsp }
648 5c860e29 2018-03-12 stsp }
649 5c860e29 2018-03-12 stsp
650 5c860e29 2018-03-12 stsp git_status_list_free(status);
651 5c860e29 2018-03-12 stsp git_repository_free(repo);
652 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
653 5c860e29 2018-03-12 stsp
654 5c860e29 2018-03-12 stsp return 0;
655 5c860e29 2018-03-12 stsp }
656 f42b1b34 2018-03-12 stsp #endif