add a basic 'got update' command; does not merge files yet
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
diff --git a/got/got.1 b/got/got.1
index c206a51..b85c7d4 100644
--- a/got/got.1
+++ b/got/got.1
@@ -89,6 +89,23 @@ Only files beneath the specified
.Ar path-prefix
will be checked out.
.El
+.It Cm update [ Fl c Ar commit ] [ work-tree-path ]
+Update an existing work tree to another commit.
+By default, the latest commit on the current branch is assumed.
+If the
+.Ar work tree path
+is omitted, use the current working directory.
+.Pp
+The options for
+.Cm got update
+are as follows:
+.Bl -tag -width Ds
+.It Fl c Ar commit
+Update the work tree to the specified
+.Ar commit .
+The expected argument is the name of a branch or a SHA1 hash which corresponds
+to a commit object.
+.El
.\".It Cm status
.\"Show current status of files.
.It Cm log [ Fl c Ar commit ] [ Fl C Ar number ] [ Fl f ] [ Fl l Ar N ] [ Fl p ] [ Fl r Ar repository-path ] [ path ]
diff --git a/got/got.c b/got/got.c
index 6c95c16..ed8b115 100644
--- a/got/got.c
+++ b/got/got.c
@@ -69,12 +69,14 @@ struct cmd {
__dead static void usage(void);
__dead static void usage_checkout(void);
+__dead static void usage_update(void);
__dead static void usage_log(void);
__dead static void usage_diff(void);
__dead static void usage_blame(void);
__dead static void usage_tree(void);
static const struct got_error* cmd_checkout(int, char *[]);
+static const struct got_error* cmd_update(int, char *[]);
static const struct got_error* cmd_log(int, char *[]);
static const struct got_error* cmd_diff(int, char *[]);
static const struct got_error* cmd_blame(int, char *[]);
@@ -86,6 +88,8 @@ static const struct got_error* cmd_status(int, char *[]);
static struct cmd got_commands[] = {
{ "checkout", cmd_checkout, usage_checkout,
"check out a new work tree from a repository" },
+ { "update", cmd_update, usage_update,
+ "update a work tree to a different commit" },
{ "log", cmd_log, usage_log,
"show repository history" },
{ "diff", cmd_diff, usage_diff,
@@ -305,6 +309,114 @@ done:
return error;
}
+__dead static void
+usage_update(void)
+{
+ fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
+ getprogname());
+ exit(1);
+}
+
+static void
+update_progress(void *arg, unsigned char status, const char *path)
+{
+ if (status == GOT_STATUS_EXISTS)
+ return;
+
+ while (path[0] == '/')
+ path++;
+ printf("%c %s\n", status, path);
+}
+
+static const struct got_error *
+cmd_update(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_repository *repo = NULL;
+ struct got_worktree *worktree = NULL;
+ char *worktree_path = NULL;
+ struct got_object_id *commit_id = NULL;
+ const char *commit_id_str = NULL;
+ int ch;
+
+ while ((ch = getopt(argc, argv, "c:")) != -1) {
+ switch (ch) {
+ case 'c':
+ commit_id_str = optarg;
+ break;
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+#ifndef PROFILE
+ if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
+ == -1)
+ err(1, "pledge");
+#endif
+ if (argc == 0) {
+ worktree_path = getcwd(NULL, 0);
+ if (worktree_path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+ } else if (argc == 1) {
+ worktree_path = realpath(argv[0], NULL);
+ if (worktree_path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+ } else
+ usage_update();
+
+ error = got_worktree_open(&worktree, worktree_path);
+ if (error != NULL)
+ goto done;
+
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ if (commit_id_str == NULL) {
+ struct got_reference *head_ref;
+ error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
+ if (error != NULL)
+ goto done;
+ error = got_ref_resolve(&commit_id, repo, head_ref);
+ if (error != NULL)
+ goto done;
+ } else {
+ error = got_object_resolve_id_str(&commit_id, repo,
+ commit_id_str);
+ if (error != NULL)
+ goto done;
+ }
+
+ if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
+ commit_id) != 0) {
+ error = got_worktree_set_base_commit_id(worktree, repo,
+ commit_id);
+ if (error)
+ goto done;
+ }
+
+ error = got_worktree_checkout_files(worktree, repo,
+ update_progress, NULL, checkout_cancel, NULL);
+ if (error != NULL)
+ goto done;
+
+ printf("Now shut up and hack\n");
+
+done:
+ free(worktree_path);
+ free(commit_id);
+ return error;
+}
+
static const struct got_error *
print_patch(struct got_commit_object *commit, struct got_object_id *id,
int diff_context, struct got_repository *repo)
diff --git a/include/got_opentemp.h b/include/got_opentemp.h
index 1820266..86beac8 100644
--- a/include/got_opentemp.h
+++ b/include/got_opentemp.h
@@ -27,3 +27,6 @@ FILE *got_opentemp(void);
/* Open a new temporary file for writing.
* The file is visible in the filesystem. */
const struct got_error *got_opentemp_named(char **, FILE **, const char *);
+
+/* Like got_opentemp_named() but returns a file descriptor instead of a FILE. */
+const struct got_error *got_opentemp_named_fd(char **, int *, const char *);
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 49f1b19..43411d6 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -19,6 +19,7 @@ struct got_worktree;
/* status codes */
#define GOT_STATUS_ADD 'A'
#define GOT_STATUS_EXISTS 'E'
+#define GOT_STATUS_UPDATE 'U'
/*
* Attempt to initialize a new work tree on disk.
@@ -62,6 +63,17 @@ const struct got_error *got_worktree_match_path_prefix(int *,
*/
char *got_worktree_get_head_ref_name(struct got_worktree *);
+/*
+ * Get the current base commit ID of a worktree.
+ */
+const struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
+
+/*
+ * Set the base commit Id of a worktree.
+ */
+const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
+ struct got_repository *, struct got_object_id *);
+
/* A callback function which is invoked when a path is checked out. */
typedef void (*got_worktree_checkout_cb)(void *, unsigned char, const char *);
@@ -80,3 +92,4 @@ typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
const struct got_error *got_worktree_checkout_files(struct got_worktree *,
struct got_repository *, got_worktree_checkout_cb progress, void *,
got_worktree_cancel_cb, void *);
+
diff --git a/lib/opentemp.c b/lib/opentemp.c
index 082513f..b9667ec 100644
--- a/lib/opentemp.c
+++ b/lib/opentemp.c
@@ -86,3 +86,28 @@ got_opentemp_named(char **path, FILE **outfile, const char *basepath)
return err;
}
+
+const struct got_error *
+got_opentemp_named_fd(char **path, int *outfd, const char *basepath)
+{
+ const struct got_error *err = NULL;
+ int fd;
+
+ *outfd = -1;
+
+ if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
+ *path = NULL;
+ return got_error_from_errno();
+ }
+
+ fd = mkstemp(*path);
+ if (fd == -1) {
+ err = got_error_from_errno();
+ free(*path);
+ *path = NULL;
+ return err;
+ }
+
+ *outfd = fd;
+ return err;
+}
diff --git a/lib/worktree.c b/lib/worktree.c
index 174d53d..67ce5a4 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -83,6 +83,43 @@ done:
}
static const struct got_error *
+update_meta_file(const char *path_got, const char *name, const char *content)
+{
+ const struct got_error *err = NULL;
+ FILE *tmpfile = NULL;
+ char *tmppath = NULL;
+ char *path = NULL;
+
+ if (asprintf(&path, "%s/%s", path_got, name) == -1) {
+ err = got_error_from_errno();
+ path = NULL;
+ goto done;
+ }
+
+ err = got_opentemp_named(&tmppath, &tmpfile, path);
+ if (err)
+ goto done;
+
+ if (content) {
+ int len = fprintf(tmpfile, "%s\n", content);
+ if (len != strlen(content) + 1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ }
+
+ if (rename(tmppath, path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+done:
+ free(tmppath);
+ fclose(tmpfile);
+ return err;
+}
+
+static const struct got_error *
read_meta_file(char **content, const char *path_got, const char *name)
{
const struct got_error *err = NULL;
@@ -404,6 +441,59 @@ got_worktree_get_head_ref_name(struct got_worktree *worktree)
return got_ref_to_str(worktree->head_ref);
}
+const struct got_object_id *
+got_worktree_get_base_commit_id(struct got_worktree *worktree)
+{
+ return worktree->base_commit_id;
+}
+
+const struct got_error *
+got_worktree_set_base_commit_id(struct got_worktree *worktree,
+ struct got_repository *repo, struct got_object_id *commit_id)
+{
+ const struct got_error *err;
+ struct got_object *obj = NULL;
+ char *id_str = NULL;
+ char *path_got = NULL;
+
+ if (asprintf(&path_got, "%s/%s", worktree->root_path,
+ GOT_WORKTREE_GOT_DIR) == -1) {
+ err = got_error_from_errno();
+ path_got = NULL;
+ goto done;
+ }
+
+ err = got_object_open(&obj, repo, commit_id);
+ if (err)
+ return err;
+
+ if (obj->type != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+
+ /* Record our base commit. */
+ err = got_object_id_str(&id_str, commit_id);
+ if (err)
+ goto done;
+ err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
+ if (err)
+ goto done;
+
+ free(worktree->base_commit_id);
+ worktree->base_commit_id = got_object_id_dup(commit_id);
+ if (worktree->base_commit_id == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+done:
+ if (obj)
+ got_object_close(obj);
+ free(id_str);
+ free(path_got);
+ return err;
+}
+
static const struct got_error *
lock_worktree(struct got_worktree *worktree, int operation)
{
@@ -432,8 +522,10 @@ blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
{
const struct got_error *err = NULL;
char *ondisk_path;
- int fd;
+ int fd = -1;
size_t len, hdrlen;
+ int update = 0;
+ char *tmppath = NULL;
if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
apply_path_prefix(worktree, path)) == -1)
@@ -447,20 +539,24 @@ blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
struct stat sb;
if (lstat(ondisk_path, &sb) == -1) {
err = got_error_from_errno();
+ goto done;
} else if (!S_ISREG(sb.st_mode)) {
/* TODO file is obstructed; do something */
err = got_error(GOT_ERR_FILE_OBSTRUCTED);
+ goto done;
} else {
- /* TODO: Merge the file! */
- (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
- progress_path);
- return NULL;
+ err = got_opentemp_named_fd(&tmppath, &fd,
+ ondisk_path);
+ if (err)
+ goto done;
+ update = 1;
}
- }
- return err;
+ } else
+ return err;
}
- (*progress_cb)(progress_arg, GOT_STATUS_ADD, progress_path);
+ (*progress_cb)(progress_arg,
+ update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
hdrlen = got_object_blob_get_hdrlen(blob);
do {
@@ -484,6 +580,13 @@ blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
fsync(fd);
+ if (update) {
+ if (rename(tmppath, ondisk_path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ }
+
if (entry)
err = got_fileindex_entry_update(entry, ondisk_path,
blob->id.sha1, worktree->base_commit_id->sha1);
@@ -498,8 +601,10 @@ blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
if (err)
goto done;
done:
- close(fd);
+ if (fd != -1)
+ close(fd);
free(ondisk_path);
+ free(tmppath);
return err;
}
@@ -588,8 +693,14 @@ tree_checkout_entry(struct got_worktree *worktree,
apply_path_prefix(worktree, path));
if (entry &&
memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
+ SHA1_DIGEST_LENGTH) == 0) {
+ (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
+ progress_path);
+ break;
+ }
+ if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
SHA1_DIGEST_LENGTH) == 0)
- break; /* file already checked out */
+ break;
err = got_object_blob_open(&blob, repo, obj, 8192);
if (err)
goto done;