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 const struct got_error *
139 cmd_checkout(int argc, char *argv[])
141 const struct got_error *error = NULL;
142 struct got_repository *repo = NULL;
143 struct got_reference *head_ref = NULL;
144 struct got_worktree *worktree = NULL;
145 char *repo_path = NULL;
146 char *worktree_path = NULL;
148 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
149 err(1, "pledge");
151 if (argc == 2) {
152 char *cwd, *base, *dotgit;
153 repo_path = argv[1];
154 cwd = getcwd(NULL, 0);
155 if (cwd == NULL)
156 err(1, "getcwd");
157 base = basename(repo_path);
158 if (base == NULL)
159 err(1, "basename");
160 dotgit = strstr(base, ".git");
161 if (dotgit)
162 *dotgit = '\0';
163 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
164 free(cwd);
165 return got_error(GOT_ERR_NO_MEM);
167 free(cwd);
168 } else if (argc == 3) {
169 repo_path = argv[1];
170 worktree_path = strdup(argv[2]);
171 if (worktree_path == NULL)
172 return got_error(GOT_ERR_NO_MEM);
173 } else
174 usage_checkout();
176 printf("%s %s %s %s\n", getprogname(), argv[0], repo_path, worktree_path);
178 error = got_repo_open(&repo, repo_path);
179 if (error != NULL)
180 goto done;
181 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
182 if (error != NULL)
183 goto done;
185 error = got_worktree_init(worktree_path, head_ref, "/", repo);
186 if (error != NULL)
187 goto done;
189 error = got_worktree_open(&worktree, worktree_path);
190 if (error != NULL)
191 goto done;
193 error = got_worktree_checkout_files(worktree, head_ref, repo);
194 if (error != NULL)
195 goto done;
197 printf("checked out %s\n", worktree_path);
199 done:
200 free(worktree_path);
201 return error;
204 static const struct got_error *
205 print_commit_object(struct got_object *, struct got_object_id *,
206 struct got_repository *);
208 static const struct got_error *
209 print_parent_commits(struct got_commit_object *commit,
210 struct got_repository *repo)
212 struct got_parent_id *pid;
213 const struct got_error *err = NULL;
214 struct got_object *obj;
216 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
217 err = got_object_open(&obj, repo, pid->id);
218 if (err != NULL)
219 return err;
220 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
221 return got_error(GOT_ERR_OBJ_TYPE);
222 err = print_commit_object(obj, pid->id, repo);
223 got_object_close(obj);
226 return err;
229 static const struct got_error *
230 print_commit_object(struct got_object *obj, struct got_object_id *id,
231 struct got_repository *repo)
233 struct got_commit_object *commit;
234 char *buf;
235 const struct got_error *err;
237 err = got_object_commit_open(&commit, repo, obj);
238 if (err)
239 return err;
241 err = got_object_id_str(&buf, id);
242 if (err)
243 return err;
245 printf("-----------------------------------------------\n");
246 printf("commit: %s\n", buf);
247 printf("Author: %s\n", commit->author);
248 printf("\n%s\n", commit->logmsg);
250 free(buf);
252 err = print_parent_commits(commit, repo);
253 got_object_commit_close(commit);
254 return err;
257 __dead void
258 usage_log(void)
260 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
261 exit(1);
264 const struct got_error *
265 cmd_log(int argc, char *argv[])
267 const struct got_error *error;
268 struct got_repository *repo;
269 struct got_reference *head_ref;
270 struct got_object_id *id;
271 struct got_object *obj;
272 char *repo_path = NULL;
274 if (pledge("stdio rpath wpath cpath", NULL) == -1)
275 err(1, "pledge");
277 if (argc == 1) {
278 repo_path = getcwd(NULL, 0);
279 if (repo_path == NULL)
280 err(1, "getcwd");
281 } else if (argc == 2)
282 repo_path = argv[1];
283 else
284 usage_log();
286 error = got_repo_open(&repo, repo_path);
287 if (error != NULL)
288 return error;
289 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
290 if (error != NULL)
291 return error;
292 error = got_ref_resolve(&id, repo, head_ref);
293 if (error != NULL)
294 return error;
296 error = got_object_open(&obj, repo, id);
297 if (error != NULL)
298 return error;
299 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
300 error = print_commit_object(obj, id, repo);
301 if (error)
302 return error;
303 } else
304 return got_error(GOT_ERR_OBJ_TYPE);
305 got_object_close(obj);
306 free(id);
307 got_ref_close(head_ref);
308 got_repo_close(repo);
309 return NULL;
312 #ifdef notyet
313 const struct got_error *
314 cmd_status(int argc __unused, char *argv[] __unused)
316 git_repository *repo = NULL;
317 git_status_list *status;
318 git_status_options statusopts;
319 size_t i;
321 git_libgit2_init();
323 if (git_repository_open_ext(&repo, ".", 0, NULL))
324 errx(1, "git_repository_open: %s", giterr_last()->message);
326 if (git_repository_is_bare(repo))
327 errx(1, "bar repository");
329 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
330 errx(1, "git_status_init_options: %s", giterr_last()->message);
332 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
333 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
334 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
335 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
337 if (git_status_list_new(&status, repo, &statusopts))
338 errx(1, "git_status_list_new: %s", giterr_last()->message);
340 for (i = 0; i < git_status_list_entrycount(status); i++) {
341 const git_status_entry *se;
343 se = git_status_byindex(status, i);
344 switch (se->status) {
345 case GIT_STATUS_WT_NEW:
346 printf("? %s\n", se->index_to_workdir->new_file.path);
347 break;
348 case GIT_STATUS_WT_MODIFIED:
349 printf("M %s\n", se->index_to_workdir->new_file.path);
350 break;
351 case GIT_STATUS_WT_DELETED:
352 printf("R %s\n", se->index_to_workdir->new_file.path);
353 break;
354 case GIT_STATUS_WT_RENAMED:
355 printf("m %s -> %s\n",
356 se->index_to_workdir->old_file.path,
357 se->index_to_workdir->new_file.path);
358 break;
359 case GIT_STATUS_WT_TYPECHANGE:
360 printf("t %s\n", se->index_to_workdir->new_file.path);
361 break;
362 case GIT_STATUS_INDEX_NEW:
363 printf("A %s\n", se->head_to_index->new_file.path);
364 break;
365 case GIT_STATUS_INDEX_MODIFIED:
366 printf("M %s\n", se->head_to_index->old_file.path);
367 break;
368 case GIT_STATUS_INDEX_DELETED:
369 printf("R %s\n", se->head_to_index->old_file.path);
370 break;
371 case GIT_STATUS_INDEX_RENAMED:
372 printf("m %s -> %s\n",
373 se->head_to_index->old_file.path,
374 se->head_to_index->new_file.path);
375 break;
376 case GIT_STATUS_INDEX_TYPECHANGE:
377 printf("t %s\n", se->head_to_index->old_file.path);
378 break;
379 case GIT_STATUS_CURRENT:
380 default:
381 break;
385 git_status_list_free(status);
386 git_repository_free(repo);
387 git_libgit2_shutdown();
389 return 0;
391 #endif