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 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 REPO_PATH [WORKTREE_PATH]\n",
134 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;
159 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
160 err(1, "pledge");
162 if (argc == 2) {
163 char *cwd, *base, *dotgit;
164 repo_path = argv[1];
165 cwd = getcwd(NULL, 0);
166 if (cwd == NULL)
167 err(1, "getcwd");
168 base = basename(repo_path);
169 if (base == NULL)
170 err(1, "basename");
171 dotgit = strstr(base, ".git");
172 if (dotgit)
173 *dotgit = '\0';
174 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
175 free(cwd);
176 return got_error(GOT_ERR_NO_MEM);
178 free(cwd);
179 } else if (argc == 3) {
180 repo_path = argv[1];
181 worktree_path = strdup(argv[2]);
182 if (worktree_path == NULL)
183 return got_error(GOT_ERR_NO_MEM);
184 } else
185 usage_checkout();
187 error = got_repo_open(&repo, repo_path);
188 if (error != NULL)
189 goto done;
190 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
191 if (error != NULL)
192 goto done;
194 error = got_worktree_init(worktree_path, head_ref, "/", repo);
195 if (error != NULL)
196 goto done;
198 error = got_worktree_open(&worktree, worktree_path);
199 if (error != NULL)
200 goto done;
202 error = got_worktree_checkout_files(worktree, head_ref, repo,
203 checkout_progress, worktree_path);
204 if (error != NULL)
205 goto done;
207 printf("checked out %s\n", worktree_path);
209 done:
210 free(worktree_path);
211 return error;
214 static const struct got_error *
215 print_commit_object(struct got_object *, struct got_object_id *,
216 struct got_repository *);
218 static const struct got_error *
219 print_parent_commits(struct got_commit_object *commit,
220 struct got_repository *repo)
222 struct got_parent_id *pid;
223 const struct got_error *err = NULL;
224 struct got_object *obj;
226 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
227 err = got_object_open(&obj, repo, pid->id);
228 if (err != NULL)
229 return err;
230 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
232 err = print_commit_object(obj, pid->id, repo);
233 got_object_close(obj);
236 return err;
239 static const struct got_error *
240 print_commit_object(struct got_object *obj, struct got_object_id *id,
241 struct got_repository *repo)
243 struct got_commit_object *commit;
244 char *buf;
245 const struct got_error *err;
247 err = got_object_commit_open(&commit, repo, obj);
248 if (err)
249 return err;
251 err = got_object_id_str(&buf, id);
252 if (err)
253 return err;
255 printf("-----------------------------------------------\n");
256 printf("commit: %s\n", buf);
257 printf("Author: %s\n", commit->author);
258 printf("\n%s\n", commit->logmsg);
260 free(buf);
262 err = print_parent_commits(commit, repo);
263 got_object_commit_close(commit);
264 return err;
267 __dead void
268 usage_log(void)
270 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
271 exit(1);
274 const struct got_error *
275 cmd_log(int argc, char *argv[])
277 const struct got_error *error;
278 struct got_repository *repo;
279 struct got_reference *head_ref;
280 struct got_object_id *id;
281 struct got_object *obj;
282 char *repo_path = NULL;
284 if (pledge("stdio rpath wpath cpath", NULL) == -1)
285 err(1, "pledge");
287 if (argc == 1) {
288 repo_path = getcwd(NULL, 0);
289 if (repo_path == NULL)
290 err(1, "getcwd");
291 } else if (argc == 2)
292 repo_path = argv[1];
293 else
294 usage_log();
296 error = got_repo_open(&repo, repo_path);
297 if (error != NULL)
298 return error;
299 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
300 if (error != NULL)
301 return error;
302 error = got_ref_resolve(&id, repo, head_ref);
303 if (error != NULL)
304 return error;
306 error = got_object_open(&obj, repo, id);
307 if (error != NULL)
308 return error;
309 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
310 error = print_commit_object(obj, id, repo);
311 if (error)
312 return error;
313 } else
314 return got_error(GOT_ERR_OBJ_TYPE);
315 got_object_close(obj);
316 free(id);
317 got_ref_close(head_ref);
318 got_repo_close(repo);
319 return NULL;
322 #ifdef notyet
323 const struct got_error *
324 cmd_status(int argc __unused, char *argv[] __unused)
326 git_repository *repo = NULL;
327 git_status_list *status;
328 git_status_options statusopts;
329 size_t i;
331 git_libgit2_init();
333 if (git_repository_open_ext(&repo, ".", 0, NULL))
334 errx(1, "git_repository_open: %s", giterr_last()->message);
336 if (git_repository_is_bare(repo))
337 errx(1, "bar repository");
339 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
340 errx(1, "git_status_init_options: %s", giterr_last()->message);
342 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
343 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
344 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
345 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
347 if (git_status_list_new(&status, repo, &statusopts))
348 errx(1, "git_status_list_new: %s", giterr_last()->message);
350 for (i = 0; i < git_status_list_entrycount(status); i++) {
351 const git_status_entry *se;
353 se = git_status_byindex(status, i);
354 switch (se->status) {
355 case GIT_STATUS_WT_NEW:
356 printf("? %s\n", se->index_to_workdir->new_file.path);
357 break;
358 case GIT_STATUS_WT_MODIFIED:
359 printf("M %s\n", se->index_to_workdir->new_file.path);
360 break;
361 case GIT_STATUS_WT_DELETED:
362 printf("R %s\n", se->index_to_workdir->new_file.path);
363 break;
364 case GIT_STATUS_WT_RENAMED:
365 printf("m %s -> %s\n",
366 se->index_to_workdir->old_file.path,
367 se->index_to_workdir->new_file.path);
368 break;
369 case GIT_STATUS_WT_TYPECHANGE:
370 printf("t %s\n", se->index_to_workdir->new_file.path);
371 break;
372 case GIT_STATUS_INDEX_NEW:
373 printf("A %s\n", se->head_to_index->new_file.path);
374 break;
375 case GIT_STATUS_INDEX_MODIFIED:
376 printf("M %s\n", se->head_to_index->old_file.path);
377 break;
378 case GIT_STATUS_INDEX_DELETED:
379 printf("R %s\n", se->head_to_index->old_file.path);
380 break;
381 case GIT_STATUS_INDEX_RENAMED:
382 printf("m %s -> %s\n",
383 se->head_to_index->old_file.path,
384 se->head_to_index->new_file.path);
385 break;
386 case GIT_STATUS_INDEX_TYPECHANGE:
387 printf("t %s\n", se->head_to_index->old_file.path);
388 break;
389 case GIT_STATUS_CURRENT:
390 default:
391 break;
395 git_status_list_free(status);
396 git_repository_free(repo);
397 git_libgit2_shutdown();
399 return 0;
401 #endif