Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libgen.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_refs.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
35 #ifndef nitems
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 #endif
39 struct cmd {
40 const char *cmd_name;
41 const struct got_error *(*cmd_main)(int, char *[]);
42 void (*cmd_usage)(void);
43 const char *cmd_descr;
44 };
46 __dead void usage(void);
47 __dead void usage_checkout(void);
48 __dead void usage_log(void);
50 const struct got_error* cmd_checkout(int, char *[]);
51 const struct got_error* cmd_log(int, char *[]);
52 const struct got_error* cmd_status(int, char *[]);
54 struct cmd got_commands[] = {
55 { "checkout", cmd_checkout, usage_checkout,
56 "check out a new work tree from a repository" },
57 { "log", cmd_log, usage_log,
58 "show repository history" },
59 #ifdef notyet
60 { "status", cmd_status, usage_status,
61 "show modification status of files" },
62 #endif
63 };
65 int
66 main(int argc, char *argv[])
67 {
68 struct cmd *cmd;
69 unsigned int i;
70 int ch;
71 int hflag = 0;
73 setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "h")) != -1) {
76 switch (ch) {
77 case 'h':
78 hflag = 1;
79 break;
80 default:
81 usage();
82 /* NOTREACHED */
83 }
84 }
86 argc -= optind;
87 argv += optind;
89 if (argc <= 0)
90 usage();
92 for (i = 0; i < nitems(got_commands); i++) {
93 const struct got_error *error;
95 cmd = &got_commands[i];
97 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
98 continue;
100 if (hflag)
101 got_commands[i].cmd_usage();
103 error = got_commands[i].cmd_main(argc, argv);
104 if (error) {
105 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
106 return 1;
109 return 0;
112 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
113 return 1;
116 __dead void
117 usage(void)
119 int i;
121 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
122 "Available commands:\n", getprogname());
123 for (i = 0; i < nitems(got_commands); i++) {
124 struct cmd *cmd = &got_commands[i];
125 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
127 exit(1);
130 __dead void
131 usage_checkout(void)
133 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
134 "[worktree-path]\n", getprogname());
135 exit(1);
138 static void
139 checkout_progress(void *arg, const char *path)
141 char *worktree_path = arg;
143 while (path[0] == '/')
144 path++;
146 printf("A %s/%s\n", worktree_path, path);
149 const struct got_error *
150 cmd_checkout(int argc, char *argv[])
152 const struct got_error *error = NULL;
153 struct got_repository *repo = NULL;
154 struct got_reference *head_ref = NULL;
155 struct got_worktree *worktree = NULL;
156 char *repo_path = NULL;
157 char *worktree_path = NULL;
158 const char *path_prefix = "";
159 int ch;
161 optind = 0;
162 while ((ch = getopt(argc, argv, "p:")) != -1) {
163 switch (ch) {
164 case 'p':
165 path_prefix = optarg;
166 break;
167 default:
168 usage();
169 /* NOTREACHED */
173 argc -= optind;
174 argv += optind;
176 #ifndef PROFILE
177 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
178 err(1, "pledge");
179 #endif
180 if (argc == 1) {
181 char *cwd, *base, *dotgit;
182 repo_path = argv[0];
183 cwd = getcwd(NULL, 0);
184 if (cwd == NULL)
185 err(1, "getcwd");
186 base = basename(repo_path);
187 if (base == NULL)
188 err(1, "basename");
189 dotgit = strstr(base, ".git");
190 if (dotgit)
191 *dotgit = '\0';
192 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
193 error = got_error_from_errno();
194 free(cwd);
195 return error;
197 free(cwd);
198 } else if (argc == 2) {
199 repo_path = argv[0];
200 worktree_path = strdup(argv[1]);
201 if (worktree_path == NULL)
202 return got_error_from_errno();
203 } else
204 usage_checkout();
206 error = got_repo_open(&repo, repo_path);
207 if (error != NULL)
208 goto done;
209 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
210 if (error != NULL)
211 goto done;
213 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
214 if (error != NULL)
215 goto done;
217 error = got_worktree_open(&worktree, worktree_path);
218 if (error != NULL)
219 goto done;
221 error = got_worktree_checkout_files(worktree, head_ref, repo,
222 checkout_progress, worktree_path);
223 if (error != NULL)
224 goto done;
226 printf("checked out %s\n", worktree_path);
228 done:
229 free(worktree_path);
230 return error;
233 static const struct got_error *
234 print_commit(struct got_commit_object *commit, struct got_object_id *id,
235 struct got_repository *repo)
237 const struct got_error *err = NULL;
238 char *buf;
240 err = got_object_id_str(&buf, id);
241 if (err)
242 return err;
244 printf("-----------------------------------------------\n");
245 printf("commit: %s\n", buf);
246 printf("Author: %s\n", commit->author);
247 printf("\n%s\n", commit->logmsg);
249 free(buf);
250 return NULL;
253 struct commit_queue_entry {
254 TAILQ_ENTRY(commit_queue_entry) entry;
255 struct got_object_id *id;
256 struct got_commit_object *commit;
257 };
259 static const struct got_error *
260 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
261 struct got_repository *repo)
263 const struct got_error *err;
264 struct got_commit_object *root_commit;
265 TAILQ_HEAD(, commit_queue_entry) commits;
266 struct commit_queue_entry *entry;
268 TAILQ_INIT(&commits);
270 err = got_object_commit_open(&root_commit, repo, root_obj);
271 if (err)
272 return err;
274 entry = calloc(1, sizeof(*entry));
275 if (entry == NULL)
276 return got_error_from_errno();
277 entry->id = got_object_id_dup(root_id);
278 if (entry->id == NULL) {
279 err = got_error_from_errno();
280 free(entry);
281 return err;
283 entry->commit = root_commit;
284 TAILQ_INSERT_HEAD(&commits, entry, entry);
286 while (!TAILQ_EMPTY(&commits)) {
287 struct got_parent_id *pid;
289 entry = TAILQ_FIRST(&commits);
290 err = print_commit(entry->commit, entry->id, repo);
291 if (err)
292 break;
294 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
295 struct got_object *obj;
296 struct got_commit_object *pcommit;
297 struct commit_queue_entry *pentry;
299 err = got_object_open(&obj, repo, pid->id);
300 if (err)
301 break;
302 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
304 break;
307 err = got_object_commit_open(&pcommit, repo, obj);
308 got_object_close(obj);
309 if (err)
310 break;
312 pentry = calloc(1, sizeof(*pentry));
313 if (pentry == NULL) {
314 err = got_error_from_errno();
315 got_object_commit_close(pcommit);
316 break;
318 pentry->id = got_object_id_dup(pid->id);
319 if (pentry->id == NULL) {
320 err = got_error_from_errno();
321 got_object_commit_close(pcommit);
322 break;
324 pentry->commit = pcommit;
325 TAILQ_INSERT_TAIL(&commits, pentry, entry);
328 TAILQ_REMOVE(&commits, entry, entry);
329 got_object_commit_close(entry->commit);
330 free(entry->id);
331 free(entry);
334 return err;
337 __dead void
338 usage_log(void)
340 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
341 exit(1);
344 const struct got_error *
345 cmd_log(int argc, char *argv[])
347 const struct got_error *error;
348 struct got_repository *repo;
349 struct got_reference *head_ref;
350 struct got_object_id *id;
351 struct got_object *obj;
352 char *repo_path = NULL;
354 #ifndef PROFILE
355 if (pledge("stdio rpath wpath cpath", NULL) == -1)
356 err(1, "pledge");
357 #endif
358 if (argc == 1) {
359 repo_path = getcwd(NULL, 0);
360 if (repo_path == NULL)
361 err(1, "getcwd");
362 } else if (argc == 2)
363 repo_path = argv[1];
364 else
365 usage_log();
367 error = got_repo_open(&repo, repo_path);
368 if (error != NULL)
369 return error;
370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 if (error != NULL)
372 return error;
373 error = got_ref_resolve(&id, repo, head_ref);
374 if (error != NULL)
375 return error;
377 error = got_object_open(&obj, repo, id);
378 if (error != NULL)
379 return error;
380 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
381 error = print_commits(obj, id, repo);
382 else
383 error = got_error(GOT_ERR_OBJ_TYPE);
384 got_object_close(obj);
385 free(id);
386 got_ref_close(head_ref);
387 got_repo_close(repo);
388 return error;
391 #ifdef notyet
392 const struct got_error *
393 cmd_status(int argc __unused, char *argv[] __unused)
395 git_repository *repo = NULL;
396 git_status_list *status;
397 git_status_options statusopts;
398 size_t i;
400 git_libgit2_init();
402 if (git_repository_open_ext(&repo, ".", 0, NULL))
403 errx(1, "git_repository_open: %s", giterr_last()->message);
405 if (git_repository_is_bare(repo))
406 errx(1, "bar repository");
408 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
409 errx(1, "git_status_init_options: %s", giterr_last()->message);
411 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
412 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
413 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
414 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
416 if (git_status_list_new(&status, repo, &statusopts))
417 errx(1, "git_status_list_new: %s", giterr_last()->message);
419 for (i = 0; i < git_status_list_entrycount(status); i++) {
420 const git_status_entry *se;
422 se = git_status_byindex(status, i);
423 switch (se->status) {
424 case GIT_STATUS_WT_NEW:
425 printf("? %s\n", se->index_to_workdir->new_file.path);
426 break;
427 case GIT_STATUS_WT_MODIFIED:
428 printf("M %s\n", se->index_to_workdir->new_file.path);
429 break;
430 case GIT_STATUS_WT_DELETED:
431 printf("R %s\n", se->index_to_workdir->new_file.path);
432 break;
433 case GIT_STATUS_WT_RENAMED:
434 printf("m %s -> %s\n",
435 se->index_to_workdir->old_file.path,
436 se->index_to_workdir->new_file.path);
437 break;
438 case GIT_STATUS_WT_TYPECHANGE:
439 printf("t %s\n", se->index_to_workdir->new_file.path);
440 break;
441 case GIT_STATUS_INDEX_NEW:
442 printf("A %s\n", se->head_to_index->new_file.path);
443 break;
444 case GIT_STATUS_INDEX_MODIFIED:
445 printf("M %s\n", se->head_to_index->old_file.path);
446 break;
447 case GIT_STATUS_INDEX_DELETED:
448 printf("R %s\n", se->head_to_index->old_file.path);
449 break;
450 case GIT_STATUS_INDEX_RENAMED:
451 printf("m %s -> %s\n",
452 se->head_to_index->old_file.path,
453 se->head_to_index->new_file.path);
454 break;
455 case GIT_STATUS_INDEX_TYPECHANGE:
456 printf("t %s\n", se->head_to_index->old_file.path);
457 break;
458 case GIT_STATUS_CURRENT:
459 default:
460 break;
464 git_status_list_free(status);
465 git_repository_free(repo);
466 git_libgit2_shutdown();
468 return 0;
470 #endif