branch: consider refs under 'update-refs'

The branch_checked_out() helper helps commands like 'git branch' and 'git fetch' from overwriting refs that are currently checked out in other worktrees. A future update to 'git rebase' will introduce a new '--update-refs' option which will update the local refs that point to commits that are being rebased. To avoid collisions as the rebase completes, we want to make the future data store for these refs to be considered by branch_checked_out(). The data store is a plaintext file inside the 'rebase-merge' directory for that worktree. The file lists refnames followed by two OIDs, each on separate lines. The OIDs will be used to store the original values of the refs and the to-be-written values as the rebase progresses, but can be ignored at the moment. Create a new sequencer_get_update_refs_state() method that parses this file and populates a struct string_list with the ref-OID pairs. We can then use this list to add to the current_checked_out_branches strmap used by branch_checked_out(). To properly navigate to the rebase directory for a given worktree, extract the static strbuf_worktree_gitdir() method to a public API method. We can test that this works without having Git write this file by artificially creating one in our test script, at least until 'git rebase --update-refs' is implemented and we can use it directly. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 19, 2022 at 18:33 UTC aa7f2fd150e7817de6c781dc5911d6c85f334324
4 files changed +119
branch.c
+13
@@ -388,6 +388,7 @@ static void prepare_checked_out_branches(void)
388 char *old;
389 struct wt_status_state state = { 0 };
390 struct worktree *wt = worktrees[i++];
391 + struct string_list update_refs = STRING_LIST_INIT_DUP;
392
393 if (wt->is_bare)
394 continue;
@@ -423,6 +424,18 @@ static void prepare_checked_out_branches(void)
424 strbuf_release(&ref);
425 }
426 wt_status_state_free_buffers(&state);
427 +
428 + if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
429 + &update_refs)) {
430 + struct string_list_item *item;
431 + for_each_string_list_item(item, &update_refs) {
432 + old = strmap_put(&current_checked_out_branches,
433 + item->string,
434 + xstrdup(wt->path));
435 + free(old);
436 + }
437 + string_list_clear(&update_refs, 1);
438 + }
439 }
440
441 free_worktrees(worktrees);
sequencer.c
+74
@@ -147,6 +147,20 @@ static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto")
147 */
148 static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
149
150 +/*
151 + * The update-refs file stores a list of refs that will be updated at the end
152 + * of the rebase sequence. The 'update-ref <ref>' commands in the todo file
153 + * update the OIDs for the refs in this file, but the refs are not updated
154 + * until the end of the rebase sequence.
155 + *
156 + * rebase_path_update_refs() returns the path to this file for a given
157 + * worktree directory. For the current worktree, pass the_repository->gitdir.
158 + */
159 +static char *rebase_path_update_refs(const char *wt_git_dir)
160 +{
161 + return xstrfmt("%s/rebase-merge/update-refs", wt_git_dir);
162 +}
163 +
164 /*
165 * The following files are written by git-rebase just after parsing the
166 * command-line.
@@ -169,6 +183,15 @@ static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-res
183 static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits")
184 static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits")
185
186 +/**
187 + * A 'struct update_refs_record' represents a value in the update-refs
188 + * list. We use a string_list to map refs to these (before, after) pairs.
189 + */
190 +struct update_ref_record {
191 + struct object_id before;
192 + struct object_id after;
193 +};
194 +
195 static int git_sequencer_config(const char *k, const char *v, void *cb)
196 {
197 struct replay_opts *opts = cb;
@@ -5936,3 +5959,54 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
5959
5960 return 0;
5961 }
5962 +
5963 +int sequencer_get_update_refs_state(const char *wt_dir,
5964 + struct string_list *refs)
5965 +{
5966 + int result = 0;
5967 + FILE *fp = NULL;
5968 + struct strbuf ref = STRBUF_INIT;
5969 + struct strbuf hash = STRBUF_INIT;
5970 + struct update_ref_record *rec = NULL;
5971 +
5972 + char *path = rebase_path_update_refs(wt_dir);
5973 +
5974 + fp = fopen(path, "r");
5975 + if (!fp)
5976 + goto cleanup;
5977 +
5978 + while (strbuf_getline(&ref, fp) != EOF) {
5979 + struct string_list_item *item;
5980 +
5981 + CALLOC_ARRAY(rec, 1);
5982 +
5983 + if (strbuf_getline(&hash, fp) == EOF ||
5984 + get_oid_hex(hash.buf, &rec->before)) {
5985 + warning(_("update-refs file at '%s' is invalid"),
5986 + path);
5987 + result = -1;
5988 + goto cleanup;
5989 + }
5990 +
5991 + if (strbuf_getline(&hash, fp) == EOF ||
5992 + get_oid_hex(hash.buf, &rec->after)) {
5993 + warning(_("update-refs file at '%s' is invalid"),
5994 + path);
5995 + result = -1;
5996 + goto cleanup;
5997 + }
5998 +
5999 + item = string_list_insert(refs, ref.buf);
6000 + item->util = rec;
6001 + rec = NULL;
6002 + }
6003 +
6004 +cleanup:
6005 + if (fp)
6006 + fclose(fp);
6007 + free(path);
6008 + free(rec);
6009 + strbuf_release(&ref);
6010 + strbuf_release(&hash);
6011 + return result;
6012 +}
sequencer.h
+9
@@ -233,4 +233,13 @@ void sequencer_post_commit_cleanup(struct repository *r, int verbose);
233 int sequencer_get_last_command(struct repository* r,
234 enum replay_action *action);
235 int sequencer_determine_whence(struct repository *r, enum commit_whence *whence);
236 +
237 +/**
238 + * Append the set of ref-OID pairs that are currently stored for the 'git
239 + * rebase --update-refs' feature if such a rebase is currently happening.
240 + *
241 + * Localized to a worktree's git dir.
242 + */
243 +int sequencer_get_update_refs_state(const char *wt_dir, struct string_list *refs);
244 +
245 #endif /* SEQUENCER_H */
t/t2407-worktree-heads.sh
+23
@@ -81,6 +81,29 @@ test_expect_success !SANITIZE_LEAK 'refuse to overwrite: worktree in rebase (mer
81 grep "cannot force update the branch '\''wt-2'\'' checked out at.*wt-2" err
82 '
83
84 +test_expect_success 'refuse to overwrite: worktree in rebase with --update-refs' '
85 + test_when_finished rm -rf .git/worktrees/wt-3/rebase-merge &&
86 +
87 + mkdir -p .git/worktrees/wt-3/rebase-merge &&
88 + touch .git/worktrees/wt-3/rebase-merge/interactive &&
89 +
90 + cat >.git/worktrees/wt-3/rebase-merge/update-refs <<-EOF &&
91 + refs/heads/fake-3
92 + $(git rev-parse HEAD~1)
93 + $(git rev-parse HEAD)
94 + refs/heads/fake-4
95 + $(git rev-parse HEAD)
96 + $(git rev-parse HEAD)
97 + EOF
98 +
99 + for i in 3 4
100 + do
101 + test_must_fail git branch -f fake-$i HEAD 2>err &&
102 + grep "cannot force update the branch '\''fake-$i'\'' checked out at.*wt-3" err ||
103 + return 1
104 + done
105 +'
106 +
107 test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: checked out' '
108 test_must_fail git fetch server +refs/heads/wt-3:refs/heads/wt-3 2>err &&
109 grep "refusing to fetch into branch '\''refs/heads/wt-3'\''" err &&