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