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 f42b1b34 2018-03-12 stsp
20 5c860e29 2018-03-12 stsp #include <err.h>
21 5c860e29 2018-03-12 stsp #include <errno.h>
22 5c860e29 2018-03-12 stsp #include <locale.h>
23 5c860e29 2018-03-12 stsp #include <stdio.h>
24 5c860e29 2018-03-12 stsp #include <stdlib.h>
25 5c860e29 2018-03-12 stsp #include <string.h>
26 5c860e29 2018-03-12 stsp #include <unistd.h>
27 c09a553d 2018-03-12 stsp #include <libgen.h>
28 5c860e29 2018-03-12 stsp
29 f42b1b34 2018-03-12 stsp #include "got_error.h"
30 f42b1b34 2018-03-12 stsp #include "got_object.h"
31 f42b1b34 2018-03-12 stsp #include "got_refs.h"
32 f42b1b34 2018-03-12 stsp #include "got_repository.h"
33 c09a553d 2018-03-12 stsp #include "got_worktree.h"
34 79109fed 2018-03-27 stsp #include "got_diff.h"
35 5c860e29 2018-03-12 stsp
36 5c860e29 2018-03-12 stsp #ifndef nitems
37 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 5c860e29 2018-03-12 stsp #endif
39 5c860e29 2018-03-12 stsp
40 5c860e29 2018-03-12 stsp struct cmd {
41 5c860e29 2018-03-12 stsp const char *cmd_name;
42 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
43 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
44 46a0db7d 2018-03-12 stsp const char *cmd_descr;
45 5c860e29 2018-03-12 stsp };
46 5c860e29 2018-03-12 stsp
47 5c860e29 2018-03-12 stsp __dead void usage(void);
48 c09a553d 2018-03-12 stsp __dead void usage_checkout(void);
49 6f3d1eb0 2018-03-12 stsp __dead void usage_log(void);
50 5c860e29 2018-03-12 stsp
51 c09a553d 2018-03-12 stsp const struct got_error* cmd_checkout(int, char *[]);
52 d7d4f210 2018-03-12 stsp const struct got_error* cmd_log(int, char *[]);
53 d7d4f210 2018-03-12 stsp const struct got_error* cmd_status(int, char *[]);
54 5c860e29 2018-03-12 stsp
55 5c860e29 2018-03-12 stsp struct cmd got_commands[] = {
56 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
57 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
58 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
59 1b6b95a8 2018-03-12 stsp "show repository history" },
60 f42b1b34 2018-03-12 stsp #ifdef notyet
61 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
62 1b6b95a8 2018-03-12 stsp "show modification status of files" },
63 f42b1b34 2018-03-12 stsp #endif
64 5c860e29 2018-03-12 stsp };
65 5c860e29 2018-03-12 stsp
66 5c860e29 2018-03-12 stsp int
67 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
68 5c860e29 2018-03-12 stsp {
69 5c860e29 2018-03-12 stsp struct cmd *cmd;
70 5c860e29 2018-03-12 stsp unsigned int i;
71 5c860e29 2018-03-12 stsp int ch;
72 1b6b95a8 2018-03-12 stsp int hflag = 0;
73 5c860e29 2018-03-12 stsp
74 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
75 5c860e29 2018-03-12 stsp
76 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
77 5c860e29 2018-03-12 stsp switch (ch) {
78 1b6b95a8 2018-03-12 stsp case 'h':
79 1b6b95a8 2018-03-12 stsp hflag = 1;
80 1b6b95a8 2018-03-12 stsp break;
81 5c860e29 2018-03-12 stsp default:
82 5c860e29 2018-03-12 stsp usage();
83 5c860e29 2018-03-12 stsp /* NOTREACHED */
84 5c860e29 2018-03-12 stsp }
85 5c860e29 2018-03-12 stsp }
86 5c860e29 2018-03-12 stsp
87 5c860e29 2018-03-12 stsp argc -= optind;
88 5c860e29 2018-03-12 stsp argv += optind;
89 1e70621d 2018-03-27 stsp optind = 0;
90 5c860e29 2018-03-12 stsp
91 5c860e29 2018-03-12 stsp if (argc <= 0)
92 5c860e29 2018-03-12 stsp usage();
93 5c860e29 2018-03-12 stsp
94 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
95 d7d4f210 2018-03-12 stsp const struct got_error *error;
96 d7d4f210 2018-03-12 stsp
97 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
100 5c860e29 2018-03-12 stsp continue;
101 5c860e29 2018-03-12 stsp
102 1b6b95a8 2018-03-12 stsp if (hflag)
103 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
104 1b6b95a8 2018-03-12 stsp
105 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
106 d7d4f210 2018-03-12 stsp if (error) {
107 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
108 d7d4f210 2018-03-12 stsp return 1;
109 d7d4f210 2018-03-12 stsp }
110 d7d4f210 2018-03-12 stsp
111 d7d4f210 2018-03-12 stsp return 0;
112 5c860e29 2018-03-12 stsp }
113 5c860e29 2018-03-12 stsp
114 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
115 5c860e29 2018-03-12 stsp return 1;
116 5c860e29 2018-03-12 stsp }
117 5c860e29 2018-03-12 stsp
118 5c860e29 2018-03-12 stsp __dead void
119 5c860e29 2018-03-12 stsp usage(void)
120 5c860e29 2018-03-12 stsp {
121 46a0db7d 2018-03-12 stsp int i;
122 46a0db7d 2018-03-12 stsp
123 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
124 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
125 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
126 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
127 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
128 46a0db7d 2018-03-12 stsp }
129 5c860e29 2018-03-12 stsp exit(1);
130 5c860e29 2018-03-12 stsp }
131 5c860e29 2018-03-12 stsp
132 c09a553d 2018-03-12 stsp __dead void
133 c09a553d 2018-03-12 stsp usage_checkout(void)
134 c09a553d 2018-03-12 stsp {
135 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
136 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
137 c09a553d 2018-03-12 stsp exit(1);
138 92a684f4 2018-03-12 stsp }
139 92a684f4 2018-03-12 stsp
140 92a684f4 2018-03-12 stsp static void
141 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
142 92a684f4 2018-03-12 stsp {
143 92a684f4 2018-03-12 stsp char *worktree_path = arg;
144 92a684f4 2018-03-12 stsp
145 92a684f4 2018-03-12 stsp while (path[0] == '/')
146 92a684f4 2018-03-12 stsp path++;
147 92a684f4 2018-03-12 stsp
148 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
149 c09a553d 2018-03-12 stsp }
150 c09a553d 2018-03-12 stsp
151 c09a553d 2018-03-12 stsp const struct got_error *
152 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
153 c09a553d 2018-03-12 stsp {
154 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
155 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
156 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
157 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
158 c09a553d 2018-03-12 stsp char *repo_path = NULL;
159 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
160 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
161 0bb8a95e 2018-03-12 stsp int ch;
162 c09a553d 2018-03-12 stsp
163 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
164 0bb8a95e 2018-03-12 stsp switch (ch) {
165 0bb8a95e 2018-03-12 stsp case 'p':
166 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
167 0bb8a95e 2018-03-12 stsp break;
168 0bb8a95e 2018-03-12 stsp default:
169 0bb8a95e 2018-03-12 stsp usage();
170 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
171 0bb8a95e 2018-03-12 stsp }
172 0bb8a95e 2018-03-12 stsp }
173 0bb8a95e 2018-03-12 stsp
174 0bb8a95e 2018-03-12 stsp argc -= optind;
175 0bb8a95e 2018-03-12 stsp argv += optind;
176 0bb8a95e 2018-03-12 stsp
177 6715a751 2018-03-16 stsp #ifndef PROFILE
178 c09a553d 2018-03-12 stsp if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
179 c09a553d 2018-03-12 stsp err(1, "pledge");
180 6715a751 2018-03-16 stsp #endif
181 0bb8a95e 2018-03-12 stsp if (argc == 1) {
182 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
183 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
184 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
185 c09a553d 2018-03-12 stsp if (cwd == NULL)
186 c09a553d 2018-03-12 stsp err(1, "getcwd");
187 c09a553d 2018-03-12 stsp base = basename(repo_path);
188 c09a553d 2018-03-12 stsp if (base == NULL)
189 c09a553d 2018-03-12 stsp err(1, "basename");
190 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
191 c09a553d 2018-03-12 stsp if (dotgit)
192 c09a553d 2018-03-12 stsp *dotgit = '\0';
193 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
195 c09a553d 2018-03-12 stsp free(cwd);
196 0a585a0d 2018-03-17 stsp return error;
197 c09a553d 2018-03-12 stsp }
198 c09a553d 2018-03-12 stsp free(cwd);
199 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
200 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
201 0bb8a95e 2018-03-12 stsp worktree_path = strdup(argv[1]);
202 c09a553d 2018-03-12 stsp if (worktree_path == NULL)
203 0a585a0d 2018-03-17 stsp return got_error_from_errno();
204 c09a553d 2018-03-12 stsp } else
205 c09a553d 2018-03-12 stsp usage_checkout();
206 c09a553d 2018-03-12 stsp
207 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
208 c09a553d 2018-03-12 stsp if (error != NULL)
209 c09a553d 2018-03-12 stsp goto done;
210 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
211 c09a553d 2018-03-12 stsp if (error != NULL)
212 c09a553d 2018-03-12 stsp goto done;
213 c09a553d 2018-03-12 stsp
214 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
215 c09a553d 2018-03-12 stsp if (error != NULL)
216 c09a553d 2018-03-12 stsp goto done;
217 c09a553d 2018-03-12 stsp
218 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
219 c09a553d 2018-03-12 stsp if (error != NULL)
220 c09a553d 2018-03-12 stsp goto done;
221 c09a553d 2018-03-12 stsp
222 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
223 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
224 c09a553d 2018-03-12 stsp if (error != NULL)
225 c09a553d 2018-03-12 stsp goto done;
226 c09a553d 2018-03-12 stsp
227 c09a553d 2018-03-12 stsp printf("checked out %s\n", worktree_path);
228 c09a553d 2018-03-12 stsp
229 c09a553d 2018-03-12 stsp done:
230 c09a553d 2018-03-12 stsp free(worktree_path);
231 c09a553d 2018-03-12 stsp return error;
232 c09a553d 2018-03-12 stsp }
233 c09a553d 2018-03-12 stsp
234 f42b1b34 2018-03-12 stsp static const struct got_error *
235 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
236 f42b1b34 2018-03-12 stsp struct got_repository *repo)
237 5c860e29 2018-03-12 stsp {
238 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
239 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
240 79109fed 2018-03-27 stsp struct got_object *obj;
241 79109fed 2018-03-27 stsp struct got_parent_id *pid;
242 79109fed 2018-03-27 stsp
243 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
244 79109fed 2018-03-27 stsp if (err)
245 79109fed 2018-03-27 stsp return err;
246 79109fed 2018-03-27 stsp
247 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
248 79109fed 2018-03-27 stsp got_object_close(obj);
249 79109fed 2018-03-27 stsp if (err)
250 79109fed 2018-03-27 stsp return err;
251 79109fed 2018-03-27 stsp
252 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
253 79109fed 2018-03-27 stsp if (pid != NULL) {
254 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
255 79109fed 2018-03-27 stsp
256 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
257 79109fed 2018-03-27 stsp if (err)
258 79109fed 2018-03-27 stsp return err;
259 79109fed 2018-03-27 stsp
260 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
261 79109fed 2018-03-27 stsp got_object_close(obj);
262 79109fed 2018-03-27 stsp if (err)
263 79109fed 2018-03-27 stsp return err;
264 79109fed 2018-03-27 stsp
265 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
266 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
267 79109fed 2018-03-27 stsp if (err)
268 79109fed 2018-03-27 stsp return err;
269 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
270 79109fed 2018-03-27 stsp got_object_close(obj);
271 79109fed 2018-03-27 stsp if (err)
272 79109fed 2018-03-27 stsp return err;
273 79109fed 2018-03-27 stsp }
274 79109fed 2018-03-27 stsp
275 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
276 79109fed 2018-03-27 stsp if (tree1)
277 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
278 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
279 79109fed 2018-03-27 stsp return err;
280 79109fed 2018-03-27 stsp }
281 79109fed 2018-03-27 stsp
282 79109fed 2018-03-27 stsp static const struct got_error *
283 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
284 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
285 79109fed 2018-03-27 stsp {
286 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
287 8bf5b3c9 2018-03-17 stsp char *buf;
288 5c860e29 2018-03-12 stsp
289 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
290 8bf5b3c9 2018-03-17 stsp if (err)
291 8bf5b3c9 2018-03-17 stsp return err;
292 5c860e29 2018-03-12 stsp
293 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
294 8bf5b3c9 2018-03-17 stsp printf("commit: %s\n", buf);
295 8bf5b3c9 2018-03-17 stsp printf("Author: %s\n", commit->author);
296 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
297 8bf5b3c9 2018-03-17 stsp
298 971751ac 2018-03-27 stsp if (show_patch) {
299 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
300 971751ac 2018-03-27 stsp if (err == 0)
301 971751ac 2018-03-27 stsp printf("\n");
302 971751ac 2018-03-27 stsp }
303 79109fed 2018-03-27 stsp
304 8bf5b3c9 2018-03-17 stsp free(buf);
305 79109fed 2018-03-27 stsp return err;
306 f42b1b34 2018-03-12 stsp }
307 8bf5b3c9 2018-03-17 stsp
308 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry {
309 8bf5b3c9 2018-03-17 stsp TAILQ_ENTRY(commit_queue_entry) entry;
310 8bf5b3c9 2018-03-17 stsp struct got_object_id *id;
311 8bf5b3c9 2018-03-17 stsp struct got_commit_object *commit;
312 8bf5b3c9 2018-03-17 stsp };
313 5c860e29 2018-03-12 stsp
314 f42b1b34 2018-03-12 stsp static const struct got_error *
315 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
316 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
317 f42b1b34 2018-03-12 stsp {
318 f42b1b34 2018-03-12 stsp const struct got_error *err;
319 8bf5b3c9 2018-03-17 stsp struct got_commit_object *root_commit;
320 8bf5b3c9 2018-03-17 stsp TAILQ_HEAD(, commit_queue_entry) commits;
321 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *entry;
322 5c860e29 2018-03-12 stsp
323 8bf5b3c9 2018-03-17 stsp TAILQ_INIT(&commits);
324 5c860e29 2018-03-12 stsp
325 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&root_commit, repo, root_obj);
326 f42b1b34 2018-03-12 stsp if (err)
327 f42b1b34 2018-03-12 stsp return err;
328 5c860e29 2018-03-12 stsp
329 8bf5b3c9 2018-03-17 stsp entry = calloc(1, sizeof(*entry));
330 8bf5b3c9 2018-03-17 stsp if (entry == NULL)
331 0a585a0d 2018-03-17 stsp return got_error_from_errno();
332 8bf5b3c9 2018-03-17 stsp entry->id = got_object_id_dup(root_id);
333 8bf5b3c9 2018-03-17 stsp if (entry->id == NULL) {
334 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
335 8bf5b3c9 2018-03-17 stsp free(entry);
336 0a585a0d 2018-03-17 stsp return err;
337 8bf5b3c9 2018-03-17 stsp }
338 8bf5b3c9 2018-03-17 stsp entry->commit = root_commit;
339 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_HEAD(&commits, entry, entry);
340 8bf5b3c9 2018-03-17 stsp
341 8bf5b3c9 2018-03-17 stsp while (!TAILQ_EMPTY(&commits)) {
342 8bf5b3c9 2018-03-17 stsp struct got_parent_id *pid;
343 5c860e29 2018-03-12 stsp
344 8bf5b3c9 2018-03-17 stsp entry = TAILQ_FIRST(&commits);
345 79109fed 2018-03-27 stsp err = print_commit(entry->commit, entry->id, repo, show_patch);
346 8bf5b3c9 2018-03-17 stsp if (err)
347 8bf5b3c9 2018-03-17 stsp break;
348 5c860e29 2018-03-12 stsp
349 8bf5b3c9 2018-03-17 stsp SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
350 8bf5b3c9 2018-03-17 stsp struct got_object *obj;
351 8bf5b3c9 2018-03-17 stsp struct got_commit_object *pcommit;
352 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *pentry;
353 8bf5b3c9 2018-03-17 stsp
354 8bf5b3c9 2018-03-17 stsp err = got_object_open(&obj, repo, pid->id);
355 8bf5b3c9 2018-03-17 stsp if (err)
356 8bf5b3c9 2018-03-17 stsp break;
357 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
358 8bf5b3c9 2018-03-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
359 8bf5b3c9 2018-03-17 stsp break;
360 8bf5b3c9 2018-03-17 stsp }
361 8bf5b3c9 2018-03-17 stsp
362 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&pcommit, repo, obj);
363 8bf5b3c9 2018-03-17 stsp got_object_close(obj);
364 8bf5b3c9 2018-03-17 stsp if (err)
365 8bf5b3c9 2018-03-17 stsp break;
366 8bf5b3c9 2018-03-17 stsp
367 8bf5b3c9 2018-03-17 stsp pentry = calloc(1, sizeof(*pentry));
368 8bf5b3c9 2018-03-17 stsp if (pentry == NULL) {
369 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
370 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
371 8bf5b3c9 2018-03-17 stsp break;
372 8bf5b3c9 2018-03-17 stsp }
373 8bf5b3c9 2018-03-17 stsp pentry->id = got_object_id_dup(pid->id);
374 8bf5b3c9 2018-03-17 stsp if (pentry->id == NULL) {
375 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
376 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
377 8bf5b3c9 2018-03-17 stsp break;
378 8bf5b3c9 2018-03-17 stsp }
379 8bf5b3c9 2018-03-17 stsp pentry->commit = pcommit;
380 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_TAIL(&commits, pentry, entry);
381 8bf5b3c9 2018-03-17 stsp }
382 8bf5b3c9 2018-03-17 stsp
383 8bf5b3c9 2018-03-17 stsp TAILQ_REMOVE(&commits, entry, entry);
384 8bf5b3c9 2018-03-17 stsp got_object_commit_close(entry->commit);
385 8bf5b3c9 2018-03-17 stsp free(entry->id);
386 8bf5b3c9 2018-03-17 stsp free(entry);
387 8bf5b3c9 2018-03-17 stsp }
388 8bf5b3c9 2018-03-17 stsp
389 f42b1b34 2018-03-12 stsp return err;
390 f42b1b34 2018-03-12 stsp }
391 5c860e29 2018-03-12 stsp
392 6f3d1eb0 2018-03-12 stsp __dead void
393 6f3d1eb0 2018-03-12 stsp usage_log(void)
394 6f3d1eb0 2018-03-12 stsp {
395 92e5c17e 2018-03-27 stsp fprintf(stderr, "usage: %s log [-p] [repository-path]\n",
396 92e5c17e 2018-03-27 stsp getprogname());
397 6f3d1eb0 2018-03-12 stsp exit(1);
398 6f3d1eb0 2018-03-12 stsp }
399 6f3d1eb0 2018-03-12 stsp
400 d7d4f210 2018-03-12 stsp const struct got_error *
401 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
402 f42b1b34 2018-03-12 stsp {
403 f42b1b34 2018-03-12 stsp const struct got_error *error;
404 f42b1b34 2018-03-12 stsp struct got_repository *repo;
405 f42b1b34 2018-03-12 stsp struct got_reference *head_ref;
406 f42b1b34 2018-03-12 stsp struct got_object_id *id;
407 f42b1b34 2018-03-12 stsp struct got_object *obj;
408 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
409 79109fed 2018-03-27 stsp int ch;
410 79109fed 2018-03-27 stsp int show_patch = 0;
411 5c860e29 2018-03-12 stsp
412 6715a751 2018-03-16 stsp #ifndef PROFILE
413 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
414 f42b1b34 2018-03-12 stsp err(1, "pledge");
415 6715a751 2018-03-16 stsp #endif
416 79109fed 2018-03-27 stsp
417 79109fed 2018-03-27 stsp while ((ch = getopt(argc, argv, "p")) != -1) {
418 79109fed 2018-03-27 stsp switch (ch) {
419 79109fed 2018-03-27 stsp case 'p':
420 79109fed 2018-03-27 stsp show_patch = 1;
421 79109fed 2018-03-27 stsp break;
422 79109fed 2018-03-27 stsp default:
423 79109fed 2018-03-27 stsp usage();
424 79109fed 2018-03-27 stsp /* NOTREACHED */
425 79109fed 2018-03-27 stsp }
426 79109fed 2018-03-27 stsp }
427 79109fed 2018-03-27 stsp
428 79109fed 2018-03-27 stsp argc -= optind;
429 79109fed 2018-03-27 stsp argv += optind;
430 79109fed 2018-03-27 stsp
431 79109fed 2018-03-27 stsp if (argc == 0) {
432 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
433 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
434 6f3d1eb0 2018-03-12 stsp err(1, "getcwd");
435 79109fed 2018-03-27 stsp } else if (argc == 1)
436 1ca4538e 2018-03-27 stsp repo_path = argv[0];
437 6f3d1eb0 2018-03-12 stsp else
438 6f3d1eb0 2018-03-12 stsp usage_log();
439 f42b1b34 2018-03-12 stsp
440 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
441 d7d4f210 2018-03-12 stsp if (error != NULL)
442 d7d4f210 2018-03-12 stsp return error;
443 d7d4f210 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
444 d7d4f210 2018-03-12 stsp if (error != NULL)
445 d7d4f210 2018-03-12 stsp return error;
446 f42b1b34 2018-03-12 stsp error = got_ref_resolve(&id, repo, head_ref);
447 d7d4f210 2018-03-12 stsp if (error != NULL)
448 d7d4f210 2018-03-12 stsp return error;
449 f42b1b34 2018-03-12 stsp
450 f42b1b34 2018-03-12 stsp error = got_object_open(&obj, repo, id);
451 d7d4f210 2018-03-12 stsp if (error != NULL)
452 d7d4f210 2018-03-12 stsp return error;
453 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
454 79109fed 2018-03-27 stsp error = print_commits(obj, id, repo, show_patch);
455 8bf5b3c9 2018-03-17 stsp else
456 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
457 f42b1b34 2018-03-12 stsp got_object_close(obj);
458 f42b1b34 2018-03-12 stsp free(id);
459 f42b1b34 2018-03-12 stsp got_ref_close(head_ref);
460 f42b1b34 2018-03-12 stsp got_repo_close(repo);
461 8bf5b3c9 2018-03-17 stsp return error;
462 5c860e29 2018-03-12 stsp }
463 5c860e29 2018-03-12 stsp
464 f42b1b34 2018-03-12 stsp #ifdef notyet
465 d7d4f210 2018-03-12 stsp const struct got_error *
466 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
467 5c860e29 2018-03-12 stsp {
468 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
469 5c860e29 2018-03-12 stsp git_status_list *status;
470 5c860e29 2018-03-12 stsp git_status_options statusopts;
471 5c860e29 2018-03-12 stsp size_t i;
472 5c860e29 2018-03-12 stsp
473 5c860e29 2018-03-12 stsp git_libgit2_init();
474 5c860e29 2018-03-12 stsp
475 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
476 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
477 5c860e29 2018-03-12 stsp
478 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
479 5c860e29 2018-03-12 stsp errx(1, "bar repository");
480 5c860e29 2018-03-12 stsp
481 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
482 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
483 5c860e29 2018-03-12 stsp
484 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
485 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
486 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
487 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
488 5c860e29 2018-03-12 stsp
489 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
490 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
491 5c860e29 2018-03-12 stsp
492 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
493 5c860e29 2018-03-12 stsp const git_status_entry *se;
494 5c860e29 2018-03-12 stsp
495 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
496 5c860e29 2018-03-12 stsp switch (se->status) {
497 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
498 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
499 5c860e29 2018-03-12 stsp break;
500 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
501 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
502 5c860e29 2018-03-12 stsp break;
503 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
504 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
505 5c860e29 2018-03-12 stsp break;
506 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
507 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
508 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
509 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
510 5c860e29 2018-03-12 stsp break;
511 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
512 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
513 5c860e29 2018-03-12 stsp break;
514 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
515 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
516 5c860e29 2018-03-12 stsp break;
517 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
518 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
519 5c860e29 2018-03-12 stsp break;
520 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
521 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
522 5c860e29 2018-03-12 stsp break;
523 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
524 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
525 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
526 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
527 5c860e29 2018-03-12 stsp break;
528 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
529 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
530 5c860e29 2018-03-12 stsp break;
531 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
532 5c860e29 2018-03-12 stsp default:
533 5c860e29 2018-03-12 stsp break;
534 5c860e29 2018-03-12 stsp }
535 5c860e29 2018-03-12 stsp }
536 5c860e29 2018-03-12 stsp
537 5c860e29 2018-03-12 stsp git_status_list_free(status);
538 5c860e29 2018-03-12 stsp git_repository_free(repo);
539 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
540 5c860e29 2018-03-12 stsp
541 5c860e29 2018-03-12 stsp return 0;
542 5c860e29 2018-03-12 stsp }
543 f42b1b34 2018-03-12 stsp #endif