implement -c option for 'got checkout'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
diff --git a/got/got.1 b/got/got.1
index 5e8952c..38b8a36 100644
--- a/got/got.1
+++ b/got/got.1
@@ -68,7 +68,7 @@ The commands for
.Nm
are as follows:
.Bl -tag -width checkout
-.It Cm checkout [ Fl p Ar path-prefix ] repository-path [ work-tree-path ]
+.It Cm checkout [ Fl c Ar commit ] [ Fl p Ar path-prefix ] repository-path [ work-tree-path ]
Copy files from a repository into a new work tree.
If the
.Ar work tree path
@@ -83,6 +83,10 @@ The options for
.Cm got checkout
are as follows:
.Bl -tag -width Ds
+.It Fl c Ar commit
+Check out files from the specified
+.Ar commit .
+By default, the HEAD commit is used.
.It Fl p Ar path-prefix
Restrict the work tree to a subset of the repository's tree hierarchy.
Only files beneath the specified
diff --git a/got/got.c b/got/got.c
index 9344bf9..cabd57b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -226,6 +226,65 @@ checkout_cancel(void *arg)
}
static const struct got_error *
+check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
+ struct got_repository *repo)
+{
+ const struct got_error *err;
+ struct got_reference *head_ref = NULL;
+ struct got_object_id *head_commit_id = NULL;
+ struct got_commit_graph *graph = NULL;
+
+ head_ref = got_worktree_get_head_ref(worktree);
+ if (head_ref == NULL)
+ return got_error_from_errno();
+
+ /* TODO: Check the reflog. The head ref may have been rebased. */
+ err = got_ref_resolve(&head_commit_id, repo, head_ref);
+ if (err)
+ goto done;
+
+ err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
+ if (err)
+ goto done;
+
+ err = got_commit_graph_iter_start(graph, head_commit_id, repo);
+ if (err)
+ goto done;
+ while (1) {
+ struct got_object_id *id;
+
+ if (sigint_received || sigpipe_received)
+ break;
+
+ err = got_commit_graph_iter_next(&id, graph);
+ if (err) {
+ if (err->code == GOT_ERR_ITER_COMPLETED) {
+ err = got_error(GOT_ERR_ANCESTRY);
+ break;
+ }
+ if (err->code != GOT_ERR_ITER_NEED_MORE)
+ break;
+ err = got_commit_graph_fetch_commits(graph, 1, repo);
+ if (err)
+ break;
+ else
+ continue;
+ }
+ if (id == NULL)
+ break;
+ if (got_object_id_cmp(id, commit_id) == 0)
+ break;
+ }
+done:
+ if (head_ref)
+ got_ref_close(head_ref);
+ if (graph)
+ got_commit_graph_close(graph);
+ return err;
+}
+
+
+static const struct got_error *
cmd_checkout(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -235,10 +294,16 @@ cmd_checkout(int argc, char *argv[])
char *repo_path = NULL;
char *worktree_path = NULL;
const char *path_prefix = "";
+ char *commit_id_str = NULL;
int ch, same_path_prefix;
- while ((ch = getopt(argc, argv, "p:")) != -1) {
+ while ((ch = getopt(argc, argv, "c:p:")) != -1) {
switch (ch) {
+ case 'c':
+ commit_id_str = strdup(optarg);
+ if (commit_id_str == NULL)
+ return got_error_from_errno();
+ break;
case 'p':
path_prefix = optarg;
break;
@@ -304,6 +369,7 @@ cmd_checkout(int argc, char *argv[])
error = got_repo_open(&repo, repo_path);
if (error != NULL)
goto done;
+
error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
if (error != NULL)
goto done;
@@ -325,6 +391,24 @@ cmd_checkout(int argc, char *argv[])
goto done;
}
+ if (commit_id_str) {
+ struct got_object_id *commit_id;
+ error = got_object_resolve_id_str(&commit_id, repo,
+ commit_id_str);
+ if (error != NULL)
+ goto done;
+ error = check_ancestry(worktree, commit_id, repo);
+ if (error != NULL) {
+ free(commit_id);
+ goto done;
+ }
+ error = got_worktree_set_base_commit_id(worktree, repo,
+ commit_id);
+ free(commit_id);
+ if (error)
+ goto done;
+ }
+
error = got_worktree_checkout_files(worktree, repo,
checkout_progress, worktree_path, checkout_cancel, NULL);
if (error != NULL)
@@ -333,6 +417,7 @@ cmd_checkout(int argc, char *argv[])
printf("Now shut up and hack\n");
done:
+ free(commit_id_str);
free(repo_path);
free(worktree_path);
return error;
@@ -358,64 +443,6 @@ update_progress(void *arg, unsigned char status, const char *path)
}
static const struct got_error *
-check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
- struct got_repository *repo)
-{
- const struct got_error *err;
- struct got_reference *head_ref = NULL;
- struct got_object_id *head_commit_id = NULL;
- struct got_commit_graph *graph = NULL;
-
- head_ref = got_worktree_get_head_ref(worktree);
- if (head_ref == NULL)
- return got_error_from_errno();
-
- /* TODO: Check the reflog. The head ref may have been rebased. */
- err = got_ref_resolve(&head_commit_id, repo, head_ref);
- if (err)
- goto done;
-
- err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
- if (err)
- goto done;
-
- err = got_commit_graph_iter_start(graph, head_commit_id, repo);
- if (err)
- goto done;
- while (1) {
- struct got_object_id *id;
-
- if (sigint_received || sigpipe_received)
- break;
-
- err = got_commit_graph_iter_next(&id, graph);
- if (err) {
- if (err->code == GOT_ERR_ITER_COMPLETED) {
- err = got_error(GOT_ERR_ANCESTRY);
- break;
- }
- if (err->code != GOT_ERR_ITER_NEED_MORE)
- break;
- err = got_commit_graph_fetch_commits(graph, 1, repo);
- if (err)
- break;
- else
- continue;
- }
- if (id == NULL)
- break;
- if (got_object_id_cmp(id, commit_id) == 0)
- break;
- }
-done:
- if (head_ref)
- got_ref_close(head_ref);
- if (graph)
- got_commit_graph_close(graph);
- return err;
-}
-
-static const struct got_error *
cmd_update(int argc, char *argv[])
{
const struct got_error *error = NULL;