add branch support to '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
diff --git a/got/got.1 b/got/got.1
index b395b0e..fe65278 100644
--- a/got/got.1
+++ b/got/got.1
@@ -60,7 +60,7 @@ The commands for
.Nm
are as follows:
.Bl -tag -width checkout
-.It Cm checkout [ Fl c Ar commit ] [ Fl p Ar path-prefix ] repository-path [ work-tree-path ]
+.It Cm checkout [ Fl b Ar branch ] [ 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
@@ -75,11 +75,16 @@ The options for
.Cm got checkout
are as follows:
.Bl -tag -width Ds
+.It Fl b Ar branch
+Check out files from the specified
+.Ar branch .
+If this option is not specified, a branch resolved via the repository's HEAD
+reference will be used.
.It Fl c Ar commit
Check out files from the specified
.Ar commit .
-If this option is not specified, a commit resolved via the repository's HEAD
-reference will be used.
+If this option is not specified, the most recent commit on the selected
+branch will be 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 dcd21cd..6196ec8 100644
--- a/got/got.c
+++ b/got/got.c
@@ -271,8 +271,8 @@ apply_unveil(const char *repo_path, int repo_read_only,
__dead static void
usage_checkout(void)
{
- fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
- "[worktree-path]\n", getprogname());
+ fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
+ "[-p prefix] repository-path [worktree-path]\n", getprogname());
exit(1);
}
@@ -348,11 +348,15 @@ cmd_checkout(int argc, char *argv[])
char *repo_path = NULL;
char *worktree_path = NULL;
const char *path_prefix = "";
+ const char *branch_name = GOT_REF_HEAD;
char *commit_id_str = NULL;
int ch, same_path_prefix;
- while ((ch = getopt(argc, argv, "c:p:")) != -1) {
+ while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
switch (ch) {
+ case 'b':
+ branch_name = optarg;
+ break;
case 'c':
commit_id_str = strdup(optarg);
if (commit_id_str == NULL)
@@ -434,7 +438,7 @@ cmd_checkout(int argc, char *argv[])
if (error)
goto done;
- error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
+ error = got_ref_open(&head_ref, repo, branch_name, 0);
if (error != NULL)
goto done;
diff --git a/lib/worktree.c b/lib/worktree.c
index c6e5bb6..674f852 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -257,10 +257,18 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
goto done;
/* Write the HEAD reference. */
- refstr = got_ref_to_str(head_ref);
- if (refstr == NULL) {
- err = got_error_from_errno("got_ref_to_str");
- goto done;
+ if (got_ref_is_symbolic(head_ref)) {
+ refstr = got_ref_to_str(head_ref);
+ if (refstr == NULL) {
+ err = got_error_from_errno("got_ref_to_str");
+ goto done;
+ }
+ } else {
+ refstr = strdup(got_ref_get_name(head_ref));
+ if (refstr == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
}
err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
if (err)