From: Stefan Sperling Subject: make'got checkout' display what was checked out To: gameoftrees@openbsd.org Date: Tue, 14 Sep 2021 12:30:37 +0200 Make 'got checkout' display the checked out reference and commit ID for informative purposes. This makes it more obvious which branch was chosen by default. And following traces of shell scripts which run 'got checkout' should also become easier with this. New output looks like this (paths omitted, thanks to the new -q option added by Tracey): $ got co -q /git/got.git/ test-wt Checked out refs/heads/main: 26960ff7b4461002e1b3b1c5c0ffd0e492bc71ba Now shut up and hack $ ok? As a next step, I think making 'got update' consistent with the above would be good, by having it display the current branch name in addition to the commit hash. diff 74f5be75024abbc5165ebae56899d1705af31d14 cd6928e6f7942963e5ddcf6520da8efdacde7fe0 blob - e0fa2127a56f4fd45394cbea92d0f7cbe3ce866f blob + 5b9314a3697912717163cbf631321921615245a0 --- got/got.c +++ got/got.c @@ -2823,13 +2823,14 @@ cmd_checkout(int argc, char *argv[]) { const struct got_error *error = NULL; struct got_repository *repo = NULL; - struct got_reference *head_ref = NULL; + struct got_reference *head_ref = NULL, *ref = NULL; struct got_worktree *worktree = NULL; char *repo_path = NULL; char *worktree_path = NULL; const char *path_prefix = ""; - const char *branch_name = GOT_REF_HEAD; + const char *branch_name = GOT_REF_HEAD, *refname = NULL; char *commit_id_str = NULL; + struct got_object_id *commit_id = NULL; char *cwd = NULL; int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0; struct got_pathlist_head paths; @@ -2966,7 +2967,6 @@ cmd_checkout(int argc, char *argv[]) } if (commit_id_str) { - struct got_object_id *commit_id; struct got_reflist_head refs; TAILQ_INIT(&refs); error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, @@ -2998,9 +2998,23 @@ cmd_checkout(int argc, char *argv[]) } error = got_worktree_set_base_commit_id(worktree, repo, commit_id); - free(commit_id); if (error) goto done; + /* Expand potentially abbreviated commit ID string. */ + free(commit_id_str); + error = got_object_id_str(&commit_id_str, commit_id); + if (error) + goto done; + } else { + commit_id = got_object_id_dup( + got_worktree_get_base_commit_id(worktree)); + if (commit_id == NULL) { + error = got_error_from_errno("got_object_id_dup"); + goto done; + } + error = got_object_id_str(&commit_id_str, commit_id); + if (error) + goto done; } error = got_pathlist_append(&paths, "", NULL); @@ -3014,12 +3028,25 @@ cmd_checkout(int argc, char *argv[]) if (error != NULL) goto done; + if (got_ref_is_symbolic(head_ref)) { + error = got_ref_resolve_symbolic(&ref, repo, head_ref); + if (error) + goto done; + refname = got_ref_get_name(ref); + } else + refname = got_ref_get_name(head_ref); + printf("Checked out %s: %s\n", refname, commit_id_str); printf("Now shut up and hack\n"); if (cpa.had_base_commit_ref_error) show_worktree_base_ref_warning(); done: + if (head_ref) + got_ref_close(head_ref); + if (ref) + got_ref_close(ref); got_pathlist_free(&paths); free(commit_id_str); + free(commit_id); free(repo_path); free(worktree_path); free(cwd); blob - b6c5784556b7f6d287d651006dc0ccee50de3901 blob + f9066950a272a65b3095aa0b65af01fccd329c41 --- regress/cmdline/checkout.sh +++ regress/cmdline/checkout.sh @@ -18,11 +18,14 @@ test_checkout_basic() { local testroot=`test_init checkout_basic` + local commit_id=`git_show_head $testroot/repo` echo "A $testroot/wt/alpha" > $testroot/stdout.expected echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout $testroot/repo $testroot/wt > $testroot/stdout @@ -57,11 +60,14 @@ test_checkout_basic() { test_checkout_dir_exists() { local testroot=`test_init checkout_dir_exists` + local commit_id=`git_show_head $testroot/repo` echo "A $testroot/wt/alpha" > $testroot/stdout.expected echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected mkdir $testroot/wt @@ -98,11 +104,14 @@ test_checkout_dir_exists() { test_checkout_dir_not_empty() { local testroot=`test_init checkout_dir_not_empty` + local commit_id=`git_show_head $testroot/repo` echo "A $testroot/wt/alpha" > $testroot/stdout.expected echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected mkdir $testroot/wt @@ -144,8 +153,11 @@ test_checkout_sets_xbit() { chmod +x $testroot/repo/xfile (cd $testroot/repo && git add .) git_commit $testroot/repo -m "adding executable file" + local commit_id=`git_show_head $testroot/repo` echo "A $testroot/wt/xfile" > $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout $testroot/repo $testroot/wt > $testroot/stdout @@ -218,6 +230,7 @@ test_checkout_commit_from_wrong_branch() { test_checkout_tag() { local testroot=`test_init checkout_tag` + local commit_id=`git_show_head $testroot/repo` local tag="1.0.0" (cd $testroot/repo && git tag -a -m "test" $tag) @@ -226,6 +239,8 @@ test_checkout_tag() { echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout -c $tag $testroot/repo $testroot/wt > $testroot/stdout @@ -265,12 +280,15 @@ test_checkout_ignores_submodules() { (cd $testroot/repo && git submodule -q add ../repo2) (cd $testroot/repo && git commit -q -m 'adding submodule') + local commit_id=`git_show_head $testroot/repo` echo "A $testroot/wt/.gitmodules" > $testroot/stdout.expected echo "A $testroot/wt/alpha" >> $testroot/stdout.expected echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout $testroot/repo $testroot/wt > $testroot/stdout @@ -305,6 +323,7 @@ test_checkout_ignores_submodules() { test_checkout_read_only() { local testroot=`test_init checkout_read_only` + local commit_id=`git_show_head $testroot/repo` # Make the repostiory read-only chmod -R a-w $testroot/repo @@ -313,6 +332,8 @@ test_checkout_read_only() { echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout $testroot/repo $testroot/wt \ @@ -366,6 +387,7 @@ test_checkout_read_only() { test_checkout_into_nonempty_dir() { local testroot=`test_init checkout_into_nonempty_dir` + local commit_id=`git_show_head $testroot/repo` mkdir -p $testroot/wt make_test_tree $testroot/wt @@ -402,6 +424,8 @@ test_checkout_into_nonempty_dir() { echo "? $testroot/wt/beta" >> $testroot/stdout.expected echo "? $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "? $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout -E $testroot/repo $testroot/wt > $testroot/stdout @@ -423,6 +447,8 @@ test_checkout_into_nonempty_dir() { echo "E $testroot/wt/beta" >> $testroot/stdout.expected echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout -E $testroot/repo $testroot/wt > $testroot/stdout @@ -461,6 +487,8 @@ test_checkout_into_nonempty_dir() { echo "E $testroot/wt/beta" >> $testroot/stdout.expected echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout -E $testroot/repo $testroot/wt > $testroot/stdout @@ -516,6 +544,7 @@ test_checkout_symlink() { (cd $testroot/repo && ln -s .got/foo dotgotfoo.link) (cd $testroot/repo && git add .) git_commit $testroot/repo -m "add symlinks" + local commit_id=`git_show_head $testroot/repo` got checkout $testroot/repo $testroot/wt > $testroot/stdout ret="$?" @@ -536,6 +565,8 @@ test_checkout_symlink() { echo "A $testroot/wt/nonexistent.link" >> $testroot/stdout.expected echo "A $testroot/wt/passwd.link" >> $testroot/stdout.expected echo "A $testroot/wt/passwd2.link" >> $testroot/stdout.expected + echo "Checked out refs/heads/master: $commit_id" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected cmp -s $testroot/stdout.expected $testroot/stdout blob - 487d5e7d9c6ed6c9c2532b3a7fc2dd808e7f3284 blob + f90827299e298d8d6dcedf838627c59d16f80333 --- regress/cmdline/import.sh +++ regress/cmdline/import.sh @@ -104,6 +104,8 @@ test_import_basic() { echo "A $testroot/wt/beta" >> $testroot/stdout.expected echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected + echo "Checked out refs/heads/main: $head_commit" \ + >> $testroot/stdout.expected echo "Now shut up and hack" >> $testroot/stdout.expected got checkout $testroot/repo $testroot/wt > $testroot/stdout