summaryrefslogtreecommitdiff
path: root/sequencer.c
diff options
context:
space:
mode:
authorDerrick Stolee <derrickstolee@github.com>2022-07-19 18:33:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-07-19 19:49:03 (GMT)
commitaa7f2fd150e7817de6c781dc5911d6c85f334324 (patch)
tree0de1b907391368987b31a20a9efd7c372eb8a711 /sequencer.c
parent18ea59582778bd0b80fc643bc4e2981275d2300d (diff)
downloadgit-aa7f2fd150e7817de6c781dc5911d6c85f334324.zip
git-aa7f2fd150e7817de6c781dc5911d6c85f334324.tar.gz
git-aa7f2fd150e7817de6c781dc5911d6c85f334324.tar.bz2
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>
Diffstat (limited to 'sequencer.c')
-rw-r--r--sequencer.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/sequencer.c b/sequencer.c
index 61a8e00..c4f3b48 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -148,6 +148,20 @@ static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto")
static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
/*
+ * The update-refs file stores a list of refs that will be updated at the end
+ * of the rebase sequence. The 'update-ref <ref>' commands in the todo file
+ * update the OIDs for the refs in this file, but the refs are not updated
+ * until the end of the rebase sequence.
+ *
+ * rebase_path_update_refs() returns the path to this file for a given
+ * worktree directory. For the current worktree, pass the_repository->gitdir.
+ */
+static char *rebase_path_update_refs(const char *wt_git_dir)
+{
+ return xstrfmt("%s/rebase-merge/update-refs", wt_git_dir);
+}
+
+/*
* The following files are written by git-rebase just after parsing the
* command-line.
*/
@@ -169,6 +183,15 @@ static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-res
static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits")
static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits")
+/**
+ * A 'struct update_refs_record' represents a value in the update-refs
+ * list. We use a string_list to map refs to these (before, after) pairs.
+ */
+struct update_ref_record {
+ struct object_id before;
+ struct object_id after;
+};
+
static int git_sequencer_config(const char *k, const char *v, void *cb)
{
struct replay_opts *opts = cb;
@@ -5936,3 +5959,54 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
return 0;
}
+
+int sequencer_get_update_refs_state(const char *wt_dir,
+ struct string_list *refs)
+{
+ int result = 0;
+ FILE *fp = NULL;
+ struct strbuf ref = STRBUF_INIT;
+ struct strbuf hash = STRBUF_INIT;
+ struct update_ref_record *rec = NULL;
+
+ char *path = rebase_path_update_refs(wt_dir);
+
+ fp = fopen(path, "r");
+ if (!fp)
+ goto cleanup;
+
+ while (strbuf_getline(&ref, fp) != EOF) {
+ struct string_list_item *item;
+
+ CALLOC_ARRAY(rec, 1);
+
+ if (strbuf_getline(&hash, fp) == EOF ||
+ get_oid_hex(hash.buf, &rec->before)) {
+ warning(_("update-refs file at '%s' is invalid"),
+ path);
+ result = -1;
+ goto cleanup;
+ }
+
+ if (strbuf_getline(&hash, fp) == EOF ||
+ get_oid_hex(hash.buf, &rec->after)) {
+ warning(_("update-refs file at '%s' is invalid"),
+ path);
+ result = -1;
+ goto cleanup;
+ }
+
+ item = string_list_insert(refs, ref.buf);
+ item->util = rec;
+ rec = NULL;
+ }
+
+cleanup:
+ if (fp)
+ fclose(fp);
+ free(path);
+ free(rec);
+ strbuf_release(&ref);
+ strbuf_release(&hash);
+ return result;
+}