checkout -m: recreate conflict labels
cover checkout -m: recreate conflict labels
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
As "git checkout -m" is recreating the original conflict I wonder if we should remember the conflict style as well so that
git -c merge.conflictStyle=diff3 git merge topic git checkout -m <unmerged-path>
would recreate diff3 style conflicts, instead of using the default config. I cannot decide if that would be convenient or confusing and am interested to hear what others think.
base-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fconflict-labels%2Fv1
View-Changes-At: https://github.com/phillipwood/git/compare/3bc034112...fdaf3da99
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/conflict-labels/v1
Phillip Wood (2):
remove_branch_state: convert boolean argument to flags
merge: remember conflict labels
branch.c | 17 +++++++++---- branch.h | 4 ++- builtin/checkout.c | 30 ++++++++++++++++++---- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 ++++++++++++++++ 13 files changed, 153 insertions(+), 11 deletions(-)
Signature · 1 line
2.56.0.rc2.84.gaf8b4f0d381
PATCH 1/2 remove_branch_state: convert boolean argument to flags
From: Phillip Wood <[email protected]>
Convert the "verbose" boolean argument to a flag so that we can add more flags in a future commit.
--- branch.c | 4 ++-- branch.h | 3 ++- builtin/checkout.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-)
branch.c +2 -2
@@ -871,9 +871,9 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } -void remove_branch_state (struct repository *r,int verbose) +void remove_branch_state (struct repository *r,unsigned flags) { -sequencer_post_commit_cleanup (r, verbose); +sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r));remove_merge_branch_state (r); }
branch.h +2 -1
@@ -127,6 +127,7 @@ int validate_branchname(const char *name, struct strbuf *ref);*/ int validate_new_branchname (const char *name,struct strbuf *ref,int force); +#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
@@ -137,7 +138,7 @@ void remove_merge_branch_state(struct repository *r);* Remove information about the state of working on the current * branch. (E.g., MERGE_HEAD) */ -void remove_branch_state (struct repository *r,int verbose); +void remove_branch_state (struct repository *r,unsigned flags);/* * Configure local branch "local" as downstream to branch "remote"
builtin/checkout.c +5 -1
@@ -950,6 +950,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg; +unsigned flags =0 ; +if (opts->new_branch) {if (opts->new_orphan_branch) {enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;
@@ -1044,7 +1046,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts, old_branch_info->path); } } -remove_branch_state (the_repository, !opts->quiet); +if (!opts->quiet) + flags |=REMOVE_BRANCH_STATE_VERBOSE ; +remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet && !opts->force_detach &&
Signature · 1 line
2.56.0.rc2.84.gaf8b4f0d381
PATCH 2/2 merge: remember conflict labels
In reply to Phillip WoodFrom: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (MERGE_HEAD and CHERRY_PICK_HEAD respectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.
As "git checkout -m <branch>" calls remove_branch_state() which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note that merge_switch_to_result() we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct as result->priv is void*.
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 24 +++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 ++++++++++++++++ 13 files changed, 143 insertions(+), 6 deletions(-)
branch.c +9 -2
@@ -860,9 +860,11 @@ void create_branches_recursively(struct repository *r, const char *name,free (branch_point); } -void remove_merge_branch_state (struct repository *r) +static void do_remove_merge_branch_state (struct repository *r,unsigned flags) {unlink (git_path_merge_head (r)); +if (!(flags &REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS )) +unlink (git_path_merge_labels (r));unlink (git_path_merge_rr (r));unlink (git_path_merge_msg (r));unlink (git_path_merge_mode (r));
@@ -871,11 +873,16 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } +void remove_merge_branch_state (struct repository *r) +{ +do_remove_merge_branch_state (r,0 ); +} +void remove_branch_state (struct repository *r,unsigned flags) {sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r)); -remove_merge_branch_state (r); +do_remove_merge_branch_state (r, flags); }void die_if_checked_out (const char *branch,int ignore_current_worktree)
branch.h +1 -0
@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
builtin/checkout.c +20 -4
@@ -15,6 +15,7 @@#include "hex.h" #include "hook.h" #include "merge-ll.h" +#include "merge.h" #include "lockfile.h" #include "mem-pool.h" #include "object-file.h"
@@ -317,6 +318,7 @@ static int checkout_merged(int pos, const struct checkout *state,struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;mmfile_t ancestor, ours, theirs; +char *base_label, *ours_label, *theirs_label;enum ll_merge_result merge_status;int status;struct object_id oid;
@@ -347,10 +349,19 @@ static int checkout_merged(int pos, const struct checkout *state,repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); ll_opts.renormalize = renormalize; +if (read_merge_labels (the_repository, &base_label, &ours_label, + &theirs_label)) { + base_label =xstrdup ("base" ); + ours_label =xstrdup ("ours" ); + theirs_label =xstrdup ("theirs" ); + } ll_opts.conflict_style = conflict_style; - merge_status =ll_merge (&result_buf, path, &ancestor,"base" , - &ours,"ours" , &theirs,"theirs" , + merge_status =ll_merge (&result_buf, path, &ancestor, base_label, + &ours, ours_label, &theirs, theirs_label, state->istate, &ll_opts); +free (base_label); +free (ours_label); +free (theirs_label);free (ancestor.ptr);free (ours.ptr);free (theirs.ptr);
@@ -946,7 +957,8 @@ static void report_tracking(struct branch_info *new_branch_info)static void update_refs_for_switch (const struct checkout_opts *opts,struct branch_info *old_branch_info, -struct branch_info *new_branch_info) +struct branch_info *new_branch_info, +bool merge_conflicts) {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg;
@@ -1048,6 +1060,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }if (!opts->quiet) flags |=REMOVE_BRANCH_STATE_VERBOSE ; +if (merge_conflicts) + flags |=REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS ;remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet &&
@@ -1262,7 +1276,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );if (created_autostash) {discard_index (the_repository->index);
builtin/commit.c +1 -0
@@ -1977,6 +1977,7 @@ int cmd_commit(int argc,sequencer_post_commit_cleanup (the_repository,0 );unlink (git_path_merge_head (the_repository)); +unlink (git_path_merge_labels (the_repository));unlink (git_path_merge_msg (the_repository));unlink (git_path_merge_mode (the_repository));unlink (git_path_squash_msg (the_repository));
merge-ort.c +19 -0
@@ -35,6 +35,7 @@#include "hex.h" #include "entry.h" #include "merge-ll.h" +#include "merge.h" #include "match-trees.h" #include "mem-pool.h" #include "object-file.h"
@@ -418,6 +419,9 @@ struct merge_options_internal {/* field that holds submodule conflict information */ struct string_list conflicted_submodules; + +/* Copies of the labels used for conflict markers */ +char *labels[3 ]; };struct conflicted_submodule_item {
@@ -4969,6 +4973,13 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo); }if (display_update_msgs)merge_display_update_messages (opt,/* detailed */ 0 , result);
@@ -5234,6 +5245,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + } result->priv = opt->priv; result->_properly_initialized =RESULT_INITIALIZED ; opt->priv =NULL ;
merge.c +63 -0
72 lines not on this page · raw message
merge.h +4 -0
@@ -13,5 +13,9 @@ int checkout_fast_forward(struct repository *r,const struct object_id *from, const struct object_id *to, int overwrite_ignore); +int write_merge_labels (struct repository *r, +const char *base,const char *ours,const char *theirs); +int read_merge_labels (struct repository *r, +char **base,char **ours,char **theirs);#endif /* MERGE_H */
path.c +1 -0
@@ -1655,3 +1655,4 @@ REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") REPO_GIT_PATH_FUNC(shallow, "shallow") +REPO_GIT_PATH_FUNC (merge_labels,"MERGE_LABELS" )
path.h +1 -0
@@ -142,6 +142,7 @@ const char *git_path_merge_mode(struct repository *r);const char *git_path_merge_head (struct repository *r);const char *git_path_fetch_head (struct repository *r);const char *git_path_shallow (struct repository *r); +const char *git_path_merge_labels (struct repository *r);int ends_with_path_components (const char *path,const char *components);
repository.c +1 -0
@@ -367,6 +367,7 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)FREE_AND_NULL (cache->merge_head);FREE_AND_NULL (cache->fetch_head);FREE_AND_NULL (cache->shallow); +FREE_AND_NULL (cache->merge_labels); }void repo_clear (struct repository *repo)
repository.h +1 -0
@@ -36,6 +36,7 @@ struct repo_path_cache {char *merge_head;char *fetch_head;char *shallow; +char *merge_labels; };struct repository {
sequencer.c +1 -0
@@ -5145,6 +5145,7 @@ static int pick_commits(struct repository *r,unlink (rebase_path_stopped_sha ());unlink (rebase_path_amend ());unlink (rebase_path_patch ()); +unlink (git_path_merge_labels (r));while (todo_list->current < todo_list->nr) {struct todo_item *item = todo_list->items + todo_list->current;
t/t7201-co.sh +21 -0
@@ -183,6 +183,27 @@ test_expect_success 'format of merge conflict from checkout -m' ' d >>>>>>> local EOF + test_cmp expect two && + + test_path_is_file .git/MERGE_LABELS && + + git checkout --conflict=diff3 two && + cat >expect <<-\EOF && + <<<<<<< simple + a + c + e + ||||||| main + a + b + c + d + e + ======= + b + d + >>>>>>> local + EOF test_cmp expect two '
Signature · 1 line
2.56.0.rc2.84.gaf8b4f0d381
Re: [PATCH 2/2] merge: remember conflict labels
Quoted 21 lines · Phillip Wood
From: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (
MERGE_HEADandCHERRY_PICK_HEADrespectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.As "git checkout -m <branch>" calls
remove_branch_state()which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note thatmerge_switch_to_result()we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct asresult->privis void*.Signed-off-by Phillip Wood <[email protected]>
Quoted 15 lines
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 24 +++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 ++++++++++++++++ 13 files changed, 143 insertions(+), 6 deletions(-)
Where do we talk about MERGE_HEAD and CHERRY_PICK_HEAD in the current documentation set? Do we want to mention MERGE_LABELS alongside them?
Quoted 7 lines
merge.c +7
-0+ +int write_merge_labels (struct repository *r,const char *base, +const char *ours,const char *theirs) +{ +FILE *f =fopen_or_warn (git_path_merge_labels (r),"w" ); + +if (!f)
merge.c int checkout_fast_forward(struct repository *r, · line 131
Quoted 10 lines
merge.c +10
-0+return -1 ; + +fprintf (f,"%s\n%s\n%s\n" , base, ours, theirs); +if (fclose (f)) +return error_errno ("could not write '%s'" , +git_path_merge_labels (r)); + +return 0 ; +} +
We write three items, one per line, delimited by LF. As this goes through stdio, wouldn't Windows write CRLF-delimited lines? I guess if we read this back through stdio, that will cancel out and we get the LF-delimited lines back?
Wait. Do we want to read this file via stdio, one line at a time, using three calls to fgets()? No, we do not give a strict upper limit to the length of these labels. So if we read with strbuf_read_line() or something, we would be safe, I guess, but alas there is no such helper function X-<.
merge.c int checkout_fast_forward(struct repository *r, · line 143
Quoted 12 lines
merge.c +12
-0+static int parse_merge_label_line (const char **p,char **line) +{ +const char *eol =strchr (*p,'\n' ); + +if (!eol) +return -1 ; + + *line =xmemdupz (*p, eol - *p); + *p = eol +1 ; + +return 0 ; +}
OK, this reads one line at a time from the file contents already fully read by strbuf_read_file(), as seen below.
Which means that the CRLF fprintf() may have written in write_merge_labels() will come back to this function, and our 'ours' may become 'ours\015' after stripping only the LF at the end?
merge.c int checkout_fast_forward(struct repository *r, · line 154
Quoted 10 lines
merge.c +10
-0+int read_merge_labels (struct repository *r, +char **pbase,char ** pours,char ** ptheirs) +{ +struct strbuf buf =STRBUF_INIT ; +const char *p; +char *base =NULL , *ours =NULL , *theirs =NULL ; +int ret =-1 ; + +if (strbuf_read_file (&buf,git_path_merge_labels (r),0 ) <0 ) +return -1 ;
Can strbuf_read_file() fill '.buf' halfway and return a failure, or does it ensure that it frees '.buf' before returning failure? Just double-checking.
... goes and checks ...
strbuf_read_file() calls strbuf_read(), which calls read_in_full() to fill a sufficiently large buffer, and a failure from there results in strbuf_release() or strbuf_setlen() resetting back to the '.len' before strbuf_read() was called (i.e., 0 in this case), so we do not leak anything on the error path and this code is safe, I think.
merge.c int checkout_fast_forward(struct repository *r, · line 162
Quoted 8 lines
merge.c +8
-0+ + p = buf.buf; +if (parse_merge_label_line (&p, &base)) + goto out; +if (parse_merge_label_line (&p, &ours)) + goto out; +if (parse_merge_label_line (&p, &theirs)) + goto out;
OK, we read three things.
merge.c int checkout_fast_forward(struct repository *r, · line 173
Quoted 11 lines
merge.c +11
-0+ ret =0 ; + *pbase = base; + *pours = ours; + *ptheirs = theirs; +out: +if (ret) { +free (base); +free (ours); +free (theirs); + } +strbuf_release (&buf);
OK, so the contract is that we will not touch p{base,ours,theirs} if we return failure, and we will not leak anything when doing so.
Which is very sensible.
merge.c int checkout_fast_forward(struct repository *r, · line 176
Quoted 2 lines
merge.c +2
-0+return ret; +}
Looking good so far, modulo a small worry about writing via stdio and reading back while bypassing stdio. But perhaps CRLF is so annoying that the compat/mingw layer takes care of all of the above worries by passing the 'binary' bit down to the msvcrt/ucrt layer, in which case we should not have to worry about it. I dunno.
Thanks for working on these patches.
Re: [PATCH 2/2] merge: remember conflict labels
Hi Junio
Quoted 39 lines · Junio C Hamano
Quoted 21 lines · Phillip Wood
From: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (
MERGE_HEADandCHERRY_PICK_HEADrespectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.As "git checkout -m <branch>" calls
remove_branch_state()which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note thatmerge_switch_to_result()we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct asresult->privis void*.Signed-off-by Phillip Wood <[email protected]>Quoted 15 lines
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 24 +++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 ++++++++++++++++ 13 files changed, 143 insertions(+), 6 deletions(-)
Quoted 3 lines
Where do we talk about
MERGE_HEADandCHERRY_PICK_HEADin the current documentation set? Do we want to mentionMERGE_LABELSalongside them?
We talk about those in gitrevisions, the "refs" section of gitglossary and in the merge documentation. As this is not a ref I don't think it fits with MERGE_HEAD, it is more like MERGE_MSG, or MERGE_MODE. The merge man page mentions MERGE_MSG in passing but never explicitly says what it contains and MERGE_MODE is undocumented as far as I can see. We would perhaps benefit from documenting the common files like COMMIT_EDITMSG, MERGE_MSG, SQUASH_MSG, MERGE_HEAD, FETCH_HEAD and MERGE_LABELS somewhere in gitrepository briefly explaining what they contain and how they are used as a separate series.
Quoted 17 lines
merge.c int checkout_fast_forward(struct repository *r, · line 143
Quoted 12 lines
merge.c +12
-0+static int parse_merge_label_line (const char **p,char **line) +{ +const char *eol =strchr (*p,'\n' ); + +if (!eol) +return -1 ; + + *line =xmemdupz (*p, eol - *p); + *p = eol +1 ; + +return 0 ; +}OK, this reads one line at a time from the file contents already fully read by
strbuf_read_file(), as seen below.
Quoted 3 lines
Which means that the CRLF
fprintf()may have written inwrite_merge_labels()will come back to this function, and our 'ours' may become 'ours\015' after stripping only the LF at the end?
That's a good point, I've changed it to use strbuf_getline() instead.
Quoted 1 line
Thanks for working on these patches.
Thanks for reviewing them, I'll send a re-roll in a couple of days
Phillip
Re: [PATCH 2/2] merge: remember conflict labels
Quoted 5 lines · Phillip Wood
We
would perhaps benefit from documenting the common files likeCOMMIT_EDITMSG,MERGE_MSG,SQUASH_MSG,MERGE_HEAD,FETCH_HEADandMERGE_LABELSsomewhere in gitrepository briefly explaining what they contain and how they are used as a separate series.
Sounds good.
Thanks.
Am 30.09.26 um 11:48 schrieb Phillip Wood:
Quoted 6 lines
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Would an index extension not be a better place to store auxiliary information about merges?
-- Hannes
Quoted 10 lines · Johannes Sixt
Am 30.09.26 um 11:48 schrieb Phillip Wood:
Quoted 6 lines
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Would an index extension not be a better place to store auxiliary information about merges?
Wow. MERGE_HEAD, CHERRY_PICK_HEAD, and all others replaced with index extensions? That would unclutter $GIT_DIR/ quite a lot (for some reason, I find ORIG_HEAD is a bit of eyesore). It makes the information less accessible, so I am not sure how I feel about the proposal, but it is an interesting thought.
Thanks.
Am 30.09.26 um 22:40 schrieb Junio C Hamano:
Quoted 10 lines
Quoted 8 lines · Johannes Sixt
Am 30.09.26 um 11:48 schrieb Phillip Wood:
Quoted 6 lines
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Quoted 5 lines
Quoted 2 lines
Would an index extension not be a better place to store auxiliary information about merges?
Wow.
MERGE_HEAD,CHERRY_PICK_HEAD, and all others replaced with index extensions?
Absolutely not. IIUC, MERGE_LABELS should not be a pseudo ref, but a file carrying auxiliary information.
Quoted 4 lines
That would unclutter $GIT_DIR/ quite a lot (for
some reason, I findORIG_HEADis a bit of eyesore). It makes the information less accessible, so I am not sure how I feel about the proposal, but it is an interesting thought.
I don't know how accessible data in an index extension is. But if it's prohibitively difficult to access, then the idea is dead on arrival.
-- Hannes
Quoted 10 lines · Johannes Sixt
Am 30.09.26 um 11:48 schrieb Phillip Wood:
Quoted 6 lines
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Would an index extension not be a better place to store auxiliary information about merges?
I did briefly consider that, but it makes it much harder for other merge strategies such as git-merge-octopus (which I should probably update to write MERGE_LABELS) to store the labels. We already have MERGE_MODE, MERGE_RR and MERGE_MSG storing various bits of merge-related information so this series just follows existing practice.
Thanks
Phillip
v2 cover checkout -m: recreate conflict labels
In reply to Phillip WoodWhen "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
As "git checkout -m" is recreating the original conflict I wonder if we should remember the conflict style as well so that
git -c merge.conflictStyle=diff3 git merge topic git checkout -m <unmerged-path>
would recreate diff3 style conflicts, instead of using the default config. I cannot decide if that would be convenient or confusing and am interested to hear what others think.
Changes since V1:
- use strbuf_getline() rather than strbuf_read_file() to read labels so that the newline handling of the reading and writing sides match.
NB ".git/MERGE_LABELS" is still undocumented - I'm hoping to find time to add some documentation for all the MERGE_* files in a future series. Johannes suggested using an index extension to store the labels, but as we already have MERGE_MODE, MERGE_RR and MERGE_MSG I think it is easier just to add another file.
base-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fconflict-labels%2Fv2
View-Changes-At: https://github.com/phillipwood/git/compare/3bc034112...18bdf7df4
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/conflict-labels/v2
Phillip Wood (2):
remove_branch_state: convert boolean argument to flags
merge: remember conflict labels
branch.c | 17 ++++++++---- branch.h | 4 ++- builtin/checkout.c | 30 ++++++++++++++++++---- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 +++++++++++++++ 13 files changed, 154 insertions(+), 11 deletions(-)
Range-diff against v1:
1: 86ef0f848a = 1: 86ef0f848a remove_branch_state: convert boolean argument to flags
2: fdaf3da993 ! 2: 18bdf7df49 merge: remember conflict labels
@@ merge.c: int checkout_fast_forward(struct repository *r,
+ return 0;
+}
+
-+static int parse_merge_label_line(const char **p, char **line)
++static char *parse_merge_label_line(struct strbuf *buf, FILE *fp)
+{
-+ const char *eol = strchr(*p, '\n');
-+
-+ if (!eol)
-+ return -1;
-+
-+ *line = xmemdupz(*p, eol - *p);
-+ *p = eol + 1;
-+
-+ return 0;
++ if (strbuf_getline(buf, fp) == EOF)
++ return NULL;
++
++ return xmemdupz(buf->buf, buf->len);
+}
+
+int read_merge_labels(struct repository *r,
+ char **pbase, char** pours, char** ptheirs)
+{
+ struct strbuf buf = STRBUF_INIT;
-+ const char *p;
+ char *base = NULL, *ours = NULL, *theirs = NULL;
+ int ret = -1;
++ FILE *fp = fopen(git_path_merge_labels(r), "r");
+
-+ if (strbuf_read_file(&buf, git_path_merge_labels(r), 0) < 0)
++ if (!fp)
+ return -1;
+
-+ p = buf.buf;
-+ if (parse_merge_label_line(&p, &base))
-+ goto out;
-+ if (parse_merge_label_line(&p, &ours))
-+ goto out;
-+ if (parse_merge_label_line(&p, &theirs))
-+ goto out;
++ base = parse_merge_label_line(&buf, fp);
++ if (!base)
++ goto out;
++
++ ours = parse_merge_label_line(&buf, fp);
++ if (!ours)
++ goto out;
++
++ theirs = parse_merge_label_line(&buf, fp);
++ if (!theirs)
++ goto out;
++
+ ret = 0;
+ *pbase = base;
+ *pours = ours;
@@ merge.c: int checkout_fast_forward(struct repository *r,
+ free(ours);
+ free(theirs);
+ }
++ fclose(fp);
+ strbuf_release(&buf);
+
+ return ret;Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v2 1/2 remove_branch_state: convert boolean argument to flags
From: Phillip Wood <[email protected]>
Convert the "verbose" boolean argument to a flag so that we can add more flags in a future commit.
--- branch.c | 4 ++-- branch.h | 3 ++- builtin/checkout.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-)
branch.c +2 -2
@@ -871,9 +871,9 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } -void remove_branch_state (struct repository *r,int verbose) +void remove_branch_state (struct repository *r,unsigned flags) { -sequencer_post_commit_cleanup (r, verbose); +sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r));remove_merge_branch_state (r); }
branch.h +2 -1
@@ -127,6 +127,7 @@ int validate_branchname(const char *name, struct strbuf *ref);*/ int validate_new_branchname (const char *name,struct strbuf *ref,int force); +#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
@@ -137,7 +138,7 @@ void remove_merge_branch_state(struct repository *r);* Remove information about the state of working on the current * branch. (E.g., MERGE_HEAD) */ -void remove_branch_state (struct repository *r,int verbose); +void remove_branch_state (struct repository *r,unsigned flags);/* * Configure local branch "local" as downstream to branch "remote"
builtin/checkout.c +5 -1
@@ -950,6 +950,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg; +unsigned flags =0 ; +if (opts->new_branch) {if (opts->new_orphan_branch) {enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;
@@ -1044,7 +1046,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts, old_branch_info->path); } } -remove_branch_state (the_repository, !opts->quiet); +if (!opts->quiet) + flags |=REMOVE_BRANCH_STATE_VERBOSE ; +remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet && !opts->force_detach &&
Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v2 2/2 merge: remember conflict labels
In reply to Phillip WoodFrom: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (MERGE_HEAD and CHERRY_PICK_HEAD respectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.
As "git checkout -m <branch>" calls remove_branch_state() which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note that merge_switch_to_result() we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct as result->priv is void*.
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 24 ++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 19 ++++++++++++++ merge.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 4 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 21 +++++++++++++++ 13 files changed, 144 insertions(+), 6 deletions(-)
branch.c +9 -2
@@ -860,9 +860,11 @@ void create_branches_recursively(struct repository *r, const char *name,free (branch_point); } -void remove_merge_branch_state (struct repository *r) +static void do_remove_merge_branch_state (struct repository *r,unsigned flags) {unlink (git_path_merge_head (r)); +if (!(flags &REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS )) +unlink (git_path_merge_labels (r));unlink (git_path_merge_rr (r));unlink (git_path_merge_msg (r));unlink (git_path_merge_mode (r));
@@ -871,11 +873,16 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } +void remove_merge_branch_state (struct repository *r) +{ +do_remove_merge_branch_state (r,0 ); +} +void remove_branch_state (struct repository *r,unsigned flags) {sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r)); -remove_merge_branch_state (r); +do_remove_merge_branch_state (r, flags); }void die_if_checked_out (const char *branch,int ignore_current_worktree)
branch.h +1 -0
@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
builtin/checkout.c +20 -4
@@ -15,6 +15,7 @@#include "hex.h" #include "hook.h" #include "merge-ll.h" +#include "merge.h" #include "lockfile.h" #include "mem-pool.h" #include "object-file.h"
@@ -317,6 +318,7 @@ static int checkout_merged(int pos, const struct checkout *state,struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;mmfile_t ancestor, ours, theirs; +char *base_label, *ours_label, *theirs_label;enum ll_merge_result merge_status;int status;struct object_id oid;
@@ -347,10 +349,19 @@ static int checkout_merged(int pos, const struct checkout *state,repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); ll_opts.renormalize = renormalize; +if (read_merge_labels (the_repository, &base_label, &ours_label, + &theirs_label)) { + base_label =xstrdup ("base" ); + ours_label =xstrdup ("ours" ); + theirs_label =xstrdup ("theirs" ); + } ll_opts.conflict_style = conflict_style; - merge_status =ll_merge (&result_buf, path, &ancestor,"base" , - &ours,"ours" , &theirs,"theirs" , + merge_status =ll_merge (&result_buf, path, &ancestor, base_label, + &ours, ours_label, &theirs, theirs_label, state->istate, &ll_opts); +free (base_label); +free (ours_label); +free (theirs_label);free (ancestor.ptr);free (ours.ptr);free (theirs.ptr);
@@ -946,7 +957,8 @@ static void report_tracking(struct branch_info *new_branch_info)static void update_refs_for_switch (const struct checkout_opts *opts,struct branch_info *old_branch_info, -struct branch_info *new_branch_info) +struct branch_info *new_branch_info, +bool merge_conflicts) {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg;
@@ -1048,6 +1060,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }if (!opts->quiet) flags |=REMOVE_BRANCH_STATE_VERBOSE ; +if (merge_conflicts) + flags |=REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS ;remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet &&
@@ -1262,7 +1276,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );if (created_autostash) {discard_index (the_repository->index);
builtin/commit.c +1 -0
@@ -1977,6 +1977,7 @@ int cmd_commit(int argc,sequencer_post_commit_cleanup (the_repository,0 );unlink (git_path_merge_head (the_repository)); +unlink (git_path_merge_labels (the_repository));unlink (git_path_merge_msg (the_repository));unlink (git_path_merge_mode (the_repository));unlink (git_path_squash_msg (the_repository));
merge-ort.c +19 -0
@@ -35,6 +35,7 @@#include "hex.h" #include "entry.h" #include "merge-ll.h" +#include "merge.h" #include "match-trees.h" #include "mem-pool.h" #include "object-file.h"
@@ -418,6 +419,9 @@ struct merge_options_internal {/* field that holds submodule conflict information */ struct string_list conflicted_submodules; + +/* Copies of the labels used for conflict markers */ +char *labels[3 ]; };struct conflicted_submodule_item {
@@ -4969,6 +4973,13 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo); }if (display_update_msgs)merge_display_update_messages (opt,/* detailed */ 0 , result);
@@ -5234,6 +5245,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + } result->priv = opt->priv; result->_properly_initialized =RESULT_INITIALIZED ; opt->priv =NULL ;
merge.c +64 -0
73 lines not on this page · raw message
merge.h +4 -0
@@ -13,5 +13,9 @@ int checkout_fast_forward(struct repository *r,const struct object_id *from, const struct object_id *to, int overwrite_ignore); +int write_merge_labels (struct repository *r, +const char *base,const char *ours,const char *theirs); +int read_merge_labels (struct repository *r, +char **base,char **ours,char **theirs);#endif /* MERGE_H */
path.c +1 -0
@@ -1655,3 +1655,4 @@ REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") REPO_GIT_PATH_FUNC(shallow, "shallow") +REPO_GIT_PATH_FUNC (merge_labels,"MERGE_LABELS" )
path.h +1 -0
@@ -142,6 +142,7 @@ const char *git_path_merge_mode(struct repository *r);const char *git_path_merge_head (struct repository *r);const char *git_path_fetch_head (struct repository *r);const char *git_path_shallow (struct repository *r); +const char *git_path_merge_labels (struct repository *r);int ends_with_path_components (const char *path,const char *components);
repository.c +1 -0
@@ -367,6 +367,7 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)FREE_AND_NULL (cache->merge_head);FREE_AND_NULL (cache->fetch_head);FREE_AND_NULL (cache->shallow); +FREE_AND_NULL (cache->merge_labels); }void repo_clear (struct repository *repo)
repository.h +1 -0
@@ -36,6 +36,7 @@ struct repo_path_cache {char *merge_head;char *fetch_head;char *shallow; +char *merge_labels; };struct repository {
sequencer.c +1 -0
@@ -5145,6 +5145,7 @@ static int pick_commits(struct repository *r,unlink (rebase_path_stopped_sha ());unlink (rebase_path_amend ());unlink (rebase_path_patch ()); +unlink (git_path_merge_labels (r));while (todo_list->current < todo_list->nr) {struct todo_item *item = todo_list->items + todo_list->current;
t/t7201-co.sh +21 -0
@@ -183,6 +183,27 @@ test_expect_success 'format of merge conflict from checkout -m' ' d >>>>>>> local EOF + test_cmp expect two && + + test_path_is_file .git/MERGE_LABELS && + + git checkout --conflict=diff3 two && + cat >expect <<-\EOF && + <<<<<<< simple + a + c + e + ||||||| main + a + b + c + d + e + ======= + b + d + >>>>>>> local + EOF test_cmp expect two '
Signature · 1 line
2.56.0.134.g299a3c16181
Re: [PATCH v2 2/2] merge: remember conflict labels
branch.h int validate_branchname(const char *name, struct strbuf *ref); · line 131
Quoted 5 lines · Phillip Wood
branch.h +1
-0@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)
Not complaining and I have no improvement suggestions, but this phrasing made me imagine that we would be passing this flag bit in code paths where we want to write the extra file out.
But that does not match the reality. merge_switch_to_result() calls write_merge_labels() unconditionally. The bit controls if the file written survives the clean-up after the operation.
Quoted 11 lines
builtin/checkout.c +2
-1@@ -946,7 +957,8 @@ static void report_tracking(struct branch_info *new_branch_info)static void update_refs_for_switch (const struct checkout_opts *opts,struct branch_info *old_branch_info, -struct branch_info *new_branch_info) +struct branch_info *new_branch_info, +bool merge_conflicts) {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg;
builtin/checkout.c static void update_refs_for_switch(const struct checkout_opts *opts, · line 1064
Quoted 5 lines
builtin/checkout.c +2
-0@@ -1048,6 +1060,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }if (!opts->quiet) flags |=REMOVE_BRANCH_STATE_VERBOSE ; +if (merge_conflicts) + flags |=REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS ;
OK.
builtin/checkout.c static int switch_branches(const struct checkout_opts *opts, · line 1281
Quoted 11 lines
builtin/checkout.c +3
-1remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet &&@@ -1262,7 +1276,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );
OK, so here we assume STASH_APPLY_CONFLICT result means we called write_merge_labels() and left the file. If not, we did not call it and the file should not be there.
But then can't we just unconditionally leave the file, instead of not removing what we wouldn't have created?
builtin/commit.c int cmd_commit(int argc, · line 1983
Quoted 12 lines
builtin/commit.c +1
-0@@ -1977,6 +1977,7 @@ int cmd_commit(int argc,sequencer_post_commit_cleanup (the_repository,0 );unlink (git_path_merge_head (the_repository)); +unlink (git_path_merge_labels (the_repository));unlink (git_path_merge_msg (the_repository));unlink (git_path_merge_mode (the_repository));unlink (git_path_squash_msg (the_repository));
Here we clean it up unconditionally after we are about to successfully finish "git commit".
Quoted 18 lines
builtin/commit.c +7
-0@@ -4969,6 +4973,13 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo); }if (display_update_msgs)merge_display_update_messages (opt,/* detailed */ 0 , result);@@ -5234,6 +5245,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it.
builtin/commit.c static void move_opt_priv_to_result_priv(struct merge_options *opt, · line 5255
Quoted 10 lines
builtin/commit.c +8
-0*/ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + }
OK, merge_switch_to_result() is the only thing that consumes these, and it will never happen after we call merge_finalize() where we destroy the mempool, so this allocation should be safe.
builtin/commit.c static void move_opt_priv_to_result_priv(struct merge_options *opt,
Quoted 7 lines
builtin/commit.c +7
-0+static char *parse_merge_label_line (struct strbuf *buf,FILE *fp) +{ +if (strbuf_getline (buf, fp) ==EOF ) +return NULL ; + +return xmemdupz (buf->buf, buf->len); +}
Wouldn't strbuf_detach() be more intuitive?
builtin/commit.c
Quoted 2 lines
builtin/commit.c +2
-0+int read_merge_labels (struct repository *r, +char **pbase,char ** pours,char ** ptheirs)
Be consistent. Asterisk sticks to variables, not types.
Quoted 10 lines
builtin/commit.c +10
-0+{ +struct strbuf buf =STRBUF_INIT ; +char *base =NULL , *ours =NULL , *theirs =NULL ; +int ret =-1 ; +FILE *fp =fopen (git_path_merge_labels (r),"r" ); + +if (!fp) +return -1 ; + + base =parse_merge_label_line (&buf, fp);
builtin/commit.c
Quoted 10 lines
builtin/commit.c +10
-0+if (!base) + goto out; + + ours =parse_merge_label_line (&buf, fp); +if (!ours) + goto out; + + theirs =parse_merge_label_line (&buf, fp); +if (!theirs) + goto out;
The repetitions are a bit annoying, but it does not get much better:
int i;
char bot[3] = {0}; /* base, ours, theirs */
for (i = 0; i < ARRAY_SIZE(bot); i++)
if (!(bot[i] = parse_merge_label_line(&buf, fp)))
goto out;so I am OK with what was posted.
It may be helpful to future developers to leave a comment that we deliberately ignore cruft after these three lines in the file and why, instead of diagnosing it as an error.
Quoted 15 lines
builtin/commit.c +15
-0+ ret =0 ; + *pbase = base; + *pours = ours; + *ptheirs = theirs; +out: +if (ret) { +free (base); +free (ours); +free (theirs); + } +fclose (fp); +strbuf_release (&buf); + +return ret; +}
Re: [PATCH v2 2/2] merge: remember conflict labels
Quoted 11 lines · Junio C Hamano
branch.h int validate_branchname(const char *name, struct strbuf *ref); · line 131
Quoted 5 lines · Phillip Wood
branch.h +1
-0@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)Not complaining and I have no improvement suggestions, but this phrasing made me imagine that we would be passing this flag bit in code paths where we want to write the extra file out.
I can see why you'd think that, would appending "_FILE" make it clearer? (though the name is long enough already)
Quoted 17 lines
But that does not match the reality.
merge_switch_to_result()callswrite_merge_labels()unconditionally. The bit controls if the file written survives the clean-up after the operation.builtin/checkout.c static int switch_branches(const struct checkout_opts *opts, · line 1281
Quoted 8 lines
builtin/checkout.c +3
-1@@ -1262,7 +1276,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );OK, so here we assume
STASH_APPLY_CONFLICTresult means we calledwrite_merge_labels()and left the file. If not, we did not call it and the file should not be there.
Quoted 2 lines
But then can't we just unconditionally leave the file, instead of not removing what we wouldn't have created?
Hmm, If a previous command such as "git stash pop" or "git checkout -m" had conflicts and wrote the file, and then the user resolves the conflicts and runs "git checkout" without "-m" (or with "-m" without creating conflicts) don't we want to remove the file?
Quoted 10 lines
merge.c int checkout_fast_forward(struct repository *r, · line 138
Quoted 7 lines
merge.c +7
-0+static char *parse_merge_label_line (struct strbuf *buf,FILE *fp) +{ +if (strbuf_getline (buf, fp) ==EOF ) +return NULL ; + +return xmemdupz (buf->buf, buf->len); +}Wouldn't
strbuf_detach()be more intuitive?
Quoted 4 lines
merge.c int checkout_fast_forward(struct repository *r, · line 141
Quoted 2 lines
merge.c +2
-0+int read_merge_labels (struct repository *r, +char **pbase,char ** pours,char ** ptheirs)Be consistent. Asterisk sticks to variables, not types.
Oops, I'll fix those.
Quoted 23 lines
Quoted 10 lines
merge.c +10
-0+{ +struct strbuf buf =STRBUF_INIT ; +char *base =NULL , *ours =NULL , *theirs =NULL ; +int ret =-1 ; +FILE *fp =fopen (git_path_merge_labels (r),"r" ); + +if (!fp) +return -1 ; + + base =parse_merge_label_line (&buf, fp);merge.c int checkout_fast_forward(struct repository *r, · line 161
Quoted 10 lines
merge.c +10
-0+if (!base) + goto out; + + ours =parse_merge_label_line (&buf, fp); +if (!ours) + goto out; + + theirs =parse_merge_label_line (&buf, fp); +if (!theirs) + goto out;The repetitions are a bit annoying, but it does not get much better:
Quoted 8 lines
int i; char bot[3] = {0}; /* base, ours, theirs */ for (i = 0; i < ARRAY_SIZE(bot); i++) if (!(bot[i] = parse_merge_label_line(&buf, fp))) goto out;so I am OK with what was posted.
Yeah, they are a bit annoying, but as there are only three of them it isn't too bad.
Quoted 3 lines
It may be helpful to future developers to leave a comment that we deliberately ignore cruft after these three lines in the file and why, instead of diagnosing it as an error.
Will do
Thanks
Phillip
Quoted 15 lines
Quoted 15 lines
merge.c +15
-0+ ret =0 ; + *pbase = base; + *pours = ours; + *ptheirs = theirs; +out: +if (ret) { +free (base); +free (ours); +free (theirs); + } +fclose (fp); +strbuf_release (&buf); + +return ret; +}
Re: [PATCH v2 2/2] merge: remember conflict labels
In reply to Phillip WoodQuoted 3 lines · Phillip Wood
Note that
merge_switch_to_result()
we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct asresult->privis void*.
I missed this part.
merge-ort.c void merge_switch_to_result(struct merge_options *opt, · line 4982
Quoted 8 lines
merge-ort.c +7
-0trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo);
Would it be better to do it this way instead?
struct merge_options_internal *priv = result->priv; write_merge_labels(opt->repo, priv->labels[0], priv->labels[1], priv->labels[2]);
Also, with the way merge labels are prepared and passed around, I wonder if we should just tighten its function signature and take
write_merge_labels(struct repository *repo, const char *labels[3])
so that this calling site becomes[*]
struct merge_options_internal *priv = result->priv; write_merge_labels(opt->repo, priv->labels);
[Footnote]
* Here, I deviate from the usual naming convention to call an array of things in singular (so the second label would become label[2]), because from the point of view of the API consumer, "labels" as a unit is what they pass around, and call it in plural.
Re: [PATCH v2 2/2] merge: remember conflict labels
Quoted 19 lines · Junio C Hamano
Quoted 3 lines · Phillip Wood
Note that
merge_switch_to_result()
we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct asresult->privis void*.I missed this part.
merge-ort.c void merge_switch_to_result(struct merge_options *opt, · line 4982
Quoted 8 lines
trace2_region_leave("merge", "write_auto_merge", opt->repo);merge-ort.c +7
-0+ +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo);Would it be better to do it this way instead?
Quoted 3 lines
struct merge_options_internal *priv = result->priv; write_merge_labels(opt->repo, priv->labels[0], priv->labels[1], priv->labels[2]);
Yes, maybe we should have a preparatory commit that adds that "priv" variable and updates the existing code that does the same dance. Another option would be to make result->priv a pointer to an opaque struct like opts->priv - I don't really see any advantage in keeping it as a void*.
Quoted 9 lines
Also, with the way merge labels are prepared and passed around, I wonder if we should just tighten its function signature and take
write_merge_labels(struct repository *repo, const char *labels[3])so that this calling site becomes[*]
struct merge_options_internal *priv = result->priv; write_merge_labels(opt->repo, priv->labels);
That's a nice idea
Thanks
Phillip
Quoted 9 lines
[Footnote]
* Here, I deviate from the usual naming convention to call an array of things in singular (so the second label would become label[2]), because from the point of view of the API consumer, "labels" as a unit is what they pass around, and call it in plural.
Re: [PATCH v2 2/2] merge: remember conflict labels
In reply to Phillip Woodmerge-ort.c void merge_switch_to_result(struct merge_options *opt, · line 4982
Quoted 11 lines · Phillip Wood
merge-ort.c +7
-0@@ -4969,6 +4973,13 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo);
A "if (result->clean >= 0 && update_worktree_and_index)" condition guards this code path, so we unconditionall call write_merge_labels(), whether the result is clean or with conflict. Am I reading the code correctly?
merge-ort.c static void move_opt_priv_to_result_priv(struct merge_options *opt, · line 5255
Quoted 12 lines
merge-ort.c +8
-0@@ -5234,6 +5245,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + }
But we only populate the labels[] when conflicted. What would we write when we do not have conflicts?
Quoted 5 lines
merge.c +5
-0+int write_merge_labels (struct repository *r,const char *base, +const char *ours,const char *theirs) +{ +FILE *f =fopen_or_warn (git_path_merge_labels (r),"w" ); +
merge.c int checkout_fast_forward(struct repository *r, · line 130
Quoted 10 lines
merge.c +10
-0+if (!f) +return -1 ; + +fprintf (f,"%s\n%s\n%s\n" , base, ours, theirs); +if (fclose (f)) +return error_errno ("could not write '%s'" , +git_path_merge_labels (r)); + +return 0 ; +}
Would the answer be "(null)\n(null)\n(null)\n"?
Re: [PATCH v2 2/2] merge: remember conflict labels
Quoted 18 lines · Junio C Hamano
merge-ort.c void merge_switch_to_result(struct merge_options *opt, · line 4982
Quoted 11 lines · Phillip Wood
merge-ort.c +7
-0@@ -4969,6 +4973,13 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); + opt->priv = result->priv; +write_merge_labels (opt->repo, opt->priv->labels[0 ], opt->priv->labels[1 ], + opt->priv->labels[2 ]); + opt->priv =NULL ; +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo);A "if (
result->clean>= 0 &&update_worktree_and_index)" condition guards this code path, so we unconditionall callwrite_merge_labels(), whether the result is clean or with conflict. Am I reading the code correctly?
Yes, I think so. Ouch! how did I miss that?
Will fix, thanks
Phillip
Quoted 33 lines
merge-ort.c static void move_opt_priv_to_result_priv(struct merge_options *opt, · line 5255
Quoted 12 lines
merge-ort.c +8
-0@@ -5234,6 +5245,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + }But we only populate the labels[] when conflicted. What would we write when we do not have conflicts?
Quoted 5 lines
merge.c +5
-0+int write_merge_labels (struct repository *r,const char *base, +const char *ours,const char *theirs) +{ +FILE *f =fopen_or_warn (git_path_merge_labels (r),"w" ); +merge.c int checkout_fast_forward(struct repository *r, · line 130
Quoted 10 lines
merge.c +10
-0+if (!f) +return -1 ; + +fprintf (f,"%s\n%s\n%s\n" , base, ours, theirs); +if (fclose (f)) +return error_errno ("could not write '%s'" , +git_path_merge_labels (r)); + +return 0 ; +}Would the answer be "(null)\n(null)\n(null)\n"?
Am 05.10.26 um 15:24 schrieb Phillip Wood:
Quoted 9 lines
As "git checkout -m" is recreating the original conflict I wonder if we should remember the conflict style as well so that
git -c merge.conflictStyle=diff3 git merge topic git checkout -m <unmerged-path>
would recreate diff3 style conflicts, instead of using the default config. I cannot decide if that would be convenient or confusing and am interested to hear what others think.
I think it hurts more than it helps. For example, I usually get away with the regular conflict markers, but at times I might decide to see the diff3 version. Then I could change the conflict marker style with
git checkout --conflict=diff3 -m <unmerged-path>
It would be disappointing if this were not possible.
-- Hannes
Quoted 11 lines · Johannes Sixt
Am 05.10.26 um 15:24 schrieb Phillip Wood:
Quoted 9 lines
As "git checkout -m" is recreating the original conflict I wonder if we should remember the conflict style as well so that
git -c merge.conflictStyle=diff3 git merge topic git checkout -m <unmerged-path>
would recreate diff3 style conflicts, instead of using the default config. I cannot decide if that would be convenient or confusing and am interested to hear what others think.
Quoted 7 lines
I think it hurts more than it helps. For example, I usually get away with the regular conflict markers, but at times I might decide to see the diff3 version. Then I could change the conflict marker style with
git checkout --conflict=diff3 -m <unmerged-path>It would be disappointing if this were not possible.
I do the same thing. What I'm talking about above is "git checkout -m" using the same conflict style as the command that created the conflicts when the user does not specify a conflict style for the checkout.
git checkout --conflict=<style> ...
and
git -c merge.conflictStyle=<style> checkout -m ...
would continue to work as they do now.
Thanks
Phillip
Quoted 10 lines · Phillip Wood
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
As "git checkout -m" is recreating the original conflict I wonder if we should remember the conflict style as well so that
Quoted 6 lines
git -c merge.conflictStyle=diff3 git merge topic git checkout -m <unmerged-path>
would recreate diff3 style conflicts, instead of using the default config. I cannot decide if that would be convenient or confusing and am interested to hear what others think.
It has been quite a while since I invented and last looked at the code paths for "checkout -m", but we should use the usual mechanism to decide what conflict style to use, so the only scenario that it makes difference between recording and not recording is the case you showed, i.e., the original merge was made with one-shot custom conflict style that is different from usual.
As "git checkout -m" can be used twice, after the above sequence, you can
$ git -c merge.conflictStyle=diff3 checkout -m <path>
to recover without losing any work. If your regular style is "merge", then the following sequence might be more commonly useful:
$ git merge topic
$ git diff
... stare at the diff output, feeling lost trying to
... figure out what the correct resolution would be.
$ git -c merge.conflictStyle=diff3 checkout -m \*
$ git diff
... now with the common ancestor version, you understand
... what both sides wanted to do better.v3 cover checkout -m: recreate conflict labels
In reply to Phillip WoodWhen "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Thanks to Junio and Johannes for their comments.
Changes since V2:
- only write ".git/MERGE_LABELS" when there are conflicts and added a check to an existing "checkout -m <branch>" test
- change write_merge_labels() to take an array of labels
- use a local variable to store the internal merge state when writing labels
- use strbuf_detach() rather than xmemdupz() when reading labels
- add a comment to say we ignore trailing cruft when reading the labels file
Changes since V1:
- use strbuf_getline() rather than strbuf_read_file() to read labels so that the newline handling of the reading and writing sides match.
NB ".git/MERGE_LABELS" is still undocumented - I'm hoping to find time to add some documentation for all the MERGE_* files in a future series. Johannes suggested using an index extension to store the labels, but as we already have MERGE_MODE, MERGE_RR and MERGE_MSG I think it is easier just to add another file.
base-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fconflict-labels%2Fv3
View-Changes-At: https://github.com/phillipwood/git/compare/3bc034112...182edb2e8
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/conflict-labels/v3
Phillip Wood (2):
remove_branch_state: convert boolean argument to flags
merge: remember conflict labels
branch.c | 17 ++++++++---- branch.h | 4 ++- builtin/checkout.c | 30 +++++++++++++++++---- builtin/commit.c | 1 + merge-ort.c | 20 ++++++++++++++ merge.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 3 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 22 ++++++++++++++++ 13 files changed, 157 insertions(+), 11 deletions(-)
Range-diff against v2:
1: 86ef0f848a = 1: 86ef0f848a remove_branch_state: convert boolean argument to flags
2: 18bdf7df49 ! 2: 182edb2e88 merge: remember conflict labels
@@ merge-ort.c: struct merge_options_internal {
struct string_list conflicted_submodules;
+
+ /* Copies of the labels used for conflict markers */
-+ char *labels[3];
++ const char *labels[3];
};
struct conflicted_submodule_item {
@@ merge-ort.c: void merge_switch_to_result(struct merge_options *opt,
}
trace2_region_leave("merge", "write_auto_merge", opt->repo);
+
-+ trace2_region_enter("merge", "write_merge_labels", opt->repo);
-+ opt->priv = result->priv;
-+ write_merge_labels(opt->repo, opt->priv->labels[0], opt->priv->labels[1],
-+ opt->priv->labels[2]);
-+ opt->priv = NULL;
-+ trace2_region_leave("merge", "write_merge_labels", opt->repo);
++ if (!result->clean) {
++ struct merge_options_internal *priv = result->priv;
++
++ trace2_region_enter("merge", "write_merge_labels", opt->repo);
++ write_merge_labels(opt->repo, priv->labels);
++ trace2_region_leave("merge", "write_merge_labels", opt->repo);
++ }
}
if (display_update_msgs)
merge_display_update_messages(opt, /* detailed */ 0, result);
@@ merge.c: int checkout_fast_forward(struct repository *r,
return 0;
}
+
-+int write_merge_labels(struct repository *r, const char *base,
-+ const char *ours, const char *theirs)
++int write_merge_labels(struct repository *r, const char *labels[3])
+{
+ FILE *f = fopen_or_warn(git_path_merge_labels(r), "w");
+
+ if (!f)
+ return -1;
+
-+ fprintf(f, "%s\n%s\n%s\n", base, ours, theirs);
++ fprintf(f, "%s\n%s\n%s\n", labels[0], labels[1], labels[2]);
+ if (fclose(f))
+ return error_errno("could not write '%s'",
+ git_path_merge_labels(r));
+
+ return 0;
+}
+
-+static char *parse_merge_label_line(struct strbuf *buf, FILE *fp)
-+{
-+ if (strbuf_getline(buf, fp) == EOF)
-+ return NULL;
-+
-+ return xmemdupz(buf->buf, buf->len);
-+}
-+
-+int read_merge_labels(struct repository *r,
-+ char **pbase, char** pours, char** ptheirs)
++static char *parse_merge_label_line(FILE *fp)
+{
+ struct strbuf buf = STRBUF_INIT;
++
++ if (strbuf_getline(&buf, fp) == EOF) {
++ strbuf_release(&buf);
++ return NULL;
++ }
++
++ return strbuf_detach(&buf, NULL);
++}
++
++int read_merge_labels(struct repository *r,
++ char **pbase, char **pours, char **ptheirs)
++{
+ char *base = NULL, *ours = NULL, *theirs = NULL;
+ int ret = -1;
+ FILE *fp = fopen(git_path_merge_labels(r), "r");
+
+ if (!fp)
+ return -1;
+
-+ base = parse_merge_label_line(&buf, fp);
++ base = parse_merge_label_line(fp);
+ if (!base)
+ goto out;
+
-+ ours = parse_merge_label_line(&buf, fp);
++ ours = parse_merge_label_line(fp);
+ if (!ours)
+ goto out;
+
-+ theirs = parse_merge_label_line(&buf, fp);
++ theirs = parse_merge_label_line(fp);
+ if (!theirs)
+ goto out;
++ /* We ignore any trailing lines */
+
+ ret = 0;
+ *pbase = base;
@@ merge.c: int checkout_fast_forward(struct repository *r,
+ free(theirs);
+ }
+ fclose(fp);
-+ strbuf_release(&buf);
+
+ return ret;
+}
@@ merge.h: int checkout_fast_forward(struct repository *r,
const struct object_id *from,
const struct object_id *to,
int overwrite_ignore);
-+int write_merge_labels(struct repository *r,
-+ const char *base, const char *ours, const char *theirs);
++int write_merge_labels(struct repository *r, const char *labels[3]);
+int read_merge_labels(struct repository *r,
+ char **base, char **ours, char **theirs);
@@ sequencer.c: static int pick_commits(struct repository *r,
struct todo_item *item = todo_list->items + todo_list->current;
## t/t7201-co.sh ##
+@@ t/t7201-co.sh: test_expect_success 'checkout -m with dirty tree' '
+
+ fill 0 1 2 3 4 5 6 7 8 >one &&
+ git checkout -m side >messages &&
++ test_path_is_missing .git/MERGE_LABELS &&
+
+ test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
+
@@ t/t7201-co.sh: test_expect_success 'format of merge conflict from checkout -m' '
d
>>>>>>> localSignature · 1 line
2.56.0.134.g299a3c16181
PATCH v3 1/2 remove_branch_state: convert boolean argument to flags
From: Phillip Wood <[email protected]>
Convert the "verbose" boolean argument to a flag so that we can add more flags in a future commit.
--- branch.c | 4 ++-- branch.h | 3 ++- builtin/checkout.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-)
branch.c +2 -2
@@ -871,9 +871,9 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } -void remove_branch_state (struct repository *r,int verbose) +void remove_branch_state (struct repository *r,unsigned flags) { -sequencer_post_commit_cleanup (r, verbose); +sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r));remove_merge_branch_state (r); }
branch.h +2 -1
@@ -127,6 +127,7 @@ int validate_branchname(const char *name, struct strbuf *ref);*/ int validate_new_branchname (const char *name,struct strbuf *ref,int force); +#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
@@ -137,7 +138,7 @@ void remove_merge_branch_state(struct repository *r);* Remove information about the state of working on the current * branch. (E.g., MERGE_HEAD) */ -void remove_branch_state (struct repository *r,int verbose); +void remove_branch_state (struct repository *r,unsigned flags);/* * Configure local branch "local" as downstream to branch "remote"
builtin/checkout.c +5 -1
@@ -950,6 +950,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg; +unsigned flags =0 ; +if (opts->new_branch) {if (opts->new_orphan_branch) {enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;
@@ -1044,7 +1046,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts, old_branch_info->path); } } -remove_branch_state (the_repository, !opts->quiet); +if (!opts->quiet) + flags |=REMOVE_BRANCH_STATE_VERBOSE ; +remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet && !opts->force_detach &&
Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v3 2/2 merge: remember conflict labels
In reply to Phillip WoodFrom: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (MERGE_HEAD and CHERRY_PICK_HEAD respectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.
As "git checkout -m <branch>" calls remove_branch_state() which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note that merge_switch_to_result() we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct as result->priv is void*.
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 24 ++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 20 ++++++++++++++ merge.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 3 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 22 ++++++++++++++++ 13 files changed, 147 insertions(+), 6 deletions(-)
branch.c +9 -2
@@ -860,9 +860,11 @@ void create_branches_recursively(struct repository *r, const char *name,free (branch_point); } -void remove_merge_branch_state (struct repository *r) +static void do_remove_merge_branch_state (struct repository *r,unsigned flags) {unlink (git_path_merge_head (r)); +if (!(flags &REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS )) +unlink (git_path_merge_labels (r));unlink (git_path_merge_rr (r));unlink (git_path_merge_msg (r));unlink (git_path_merge_mode (r));
@@ -871,11 +873,16 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } +void remove_merge_branch_state (struct repository *r) +{ +do_remove_merge_branch_state (r,0 ); +} +void remove_branch_state (struct repository *r,unsigned flags) {sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r)); -remove_merge_branch_state (r); +do_remove_merge_branch_state (r, flags); }void die_if_checked_out (const char *branch,int ignore_current_worktree)
branch.h +1 -0
@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
builtin/checkout.c +20 -4
@@ -15,6 +15,7 @@#include "hex.h" #include "hook.h" #include "merge-ll.h" +#include "merge.h" #include "lockfile.h" #include "mem-pool.h" #include "object-file.h"
@@ -317,6 +318,7 @@ static int checkout_merged(int pos, const struct checkout *state,struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;mmfile_t ancestor, ours, theirs; +char *base_label, *ours_label, *theirs_label;enum ll_merge_result merge_status;int status;struct object_id oid;
@@ -347,10 +349,19 @@ static int checkout_merged(int pos, const struct checkout *state,repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); ll_opts.renormalize = renormalize; +if (read_merge_labels (the_repository, &base_label, &ours_label, + &theirs_label)) { + base_label =xstrdup ("base" ); + ours_label =xstrdup ("ours" ); + theirs_label =xstrdup ("theirs" ); + } ll_opts.conflict_style = conflict_style; - merge_status =ll_merge (&result_buf, path, &ancestor,"base" , - &ours,"ours" , &theirs,"theirs" , + merge_status =ll_merge (&result_buf, path, &ancestor, base_label, + &ours, ours_label, &theirs, theirs_label, state->istate, &ll_opts); +free (base_label); +free (ours_label); +free (theirs_label);free (ancestor.ptr);free (ours.ptr);free (theirs.ptr);
@@ -946,7 +957,8 @@ static void report_tracking(struct branch_info *new_branch_info)static void update_refs_for_switch (const struct checkout_opts *opts,struct branch_info *old_branch_info, -struct branch_info *new_branch_info) +struct branch_info *new_branch_info, +bool merge_conflicts) {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg;
@@ -1048,6 +1060,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }if (!opts->quiet) flags |=REMOVE_BRANCH_STATE_VERBOSE ; +if (merge_conflicts) + flags |=REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS ;remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet &&
@@ -1262,7 +1276,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );if (created_autostash) {discard_index (the_repository->index);
builtin/commit.c +1 -0
@@ -1977,6 +1977,7 @@ int cmd_commit(int argc,sequencer_post_commit_cleanup (the_repository,0 );unlink (git_path_merge_head (the_repository)); +unlink (git_path_merge_labels (the_repository));unlink (git_path_merge_msg (the_repository));unlink (git_path_merge_mode (the_repository));unlink (git_path_squash_msg (the_repository));
merge-ort.c +20 -0
@@ -35,6 +35,7 @@#include "hex.h" #include "entry.h" #include "merge-ll.h" +#include "merge.h" #include "match-trees.h" #include "mem-pool.h" #include "object-file.h"
@@ -418,6 +419,9 @@ struct merge_options_internal {/* field that holds submodule conflict information */ struct string_list conflicted_submodules; + +/* Copies of the labels used for conflict markers */ +const char *labels[3 ]; };struct conflicted_submodule_item {
@@ -4969,6 +4973,14 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +if (!result->clean) { +struct merge_options_internal *priv = result->priv; + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); +write_merge_labels (opt->repo, priv->labels); +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo); + } }if (display_update_msgs)merge_display_update_messages (opt,/* detailed */ 0 , result);
@@ -5234,6 +5246,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + } result->priv = opt->priv; result->_properly_initialized =RESULT_INITIALIZED ; opt->priv =NULL ;
merge.c +66 -0
75 lines not on this page · raw message
merge.h +3 -0
@@ -13,5 +13,8 @@ int checkout_fast_forward(struct repository *r,const struct object_id *from, const struct object_id *to, int overwrite_ignore); +int write_merge_labels (struct repository *r,const char *labels[3 ]); +int read_merge_labels (struct repository *r, +char **base,char **ours,char **theirs);#endif /* MERGE_H */
path.c +1 -0
@@ -1655,3 +1655,4 @@ REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") REPO_GIT_PATH_FUNC(shallow, "shallow") +REPO_GIT_PATH_FUNC (merge_labels,"MERGE_LABELS" )
path.h +1 -0
@@ -142,6 +142,7 @@ const char *git_path_merge_mode(struct repository *r);const char *git_path_merge_head (struct repository *r);const char *git_path_fetch_head (struct repository *r);const char *git_path_shallow (struct repository *r); +const char *git_path_merge_labels (struct repository *r);int ends_with_path_components (const char *path,const char *components);
repository.c +1 -0
@@ -367,6 +367,7 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)FREE_AND_NULL (cache->merge_head);FREE_AND_NULL (cache->fetch_head);FREE_AND_NULL (cache->shallow); +FREE_AND_NULL (cache->merge_labels); }void repo_clear (struct repository *repo)
repository.h +1 -0
@@ -36,6 +36,7 @@ struct repo_path_cache {char *merge_head;char *fetch_head;char *shallow; +char *merge_labels; };struct repository {
sequencer.c +1 -0
@@ -5145,6 +5145,7 @@ static int pick_commits(struct repository *r,unlink (rebase_path_stopped_sha ());unlink (rebase_path_amend ());unlink (rebase_path_patch ()); +unlink (git_path_merge_labels (r));while (todo_list->current < todo_list->nr) {struct todo_item *item = todo_list->items + todo_list->current;
t/t7201-co.sh +22 -0
@@ -99,6 +99,7 @@ test_expect_success 'checkout -m with dirty tree' ' fill 0 1 2 3 4 5 6 7 8 >one && git checkout -m side >messages && + test_path_is_missing .git/MERGE_LABELS && test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
@@ -183,6 +184,27 @@ test_expect_success 'format of merge conflict from checkout -m' ' d >>>>>>> local EOF + test_cmp expect two && + + test_path_is_file .git/MERGE_LABELS && + + git checkout --conflict=diff3 two && + cat >expect <<-\EOF && + <<<<<<< simple + a + c + e + ||||||| main + a + b + c + d + e + ======= + b + d + >>>>>>> local + EOF test_cmp expect two '
Signature · 1 line
2.56.0.134.g299a3c16181
Re: [PATCH v3 2/2] merge: remember conflict labels
Quoted 12 lines · Phillip Wood
(quoted diff) +6
-1@@ -347,10 +349,19 @@ static int checkout_merged(int pos, const struct checkout *state, repo_config_get_bool(the_repository, "merge.renormalize", &renormalize); ll_opts.renormalize = renormalize; + if (read_merge_labels(the_repository, &base_label, &ours_label, + &theirs_label)) { + base_label = xstrdup("base"); + ours_label = xstrdup("ours"); + theirs_label = xstrdup("theirs"); + } ll_opts.conflict_style = conflict_style; - merge_status = ll_merge(&result_buf, path, &ancestor, "base",
Review static int checkout_merged(int pos, const struct checkout *state, · line 367
Quoted 10 lines
(quoted diff) +5
-1- &ours, "ours", &theirs, "theirs", + merge_status = ll_merge(&result_buf, path, &ancestor, base_label, + &ours, ours_label, &theirs, theirs_label, state->istate, &ll_opts); + free(base_label); + free(ours_label); + free(theirs_label); free(ancestor.ptr); free(ours.ptr); free(theirs.ptr);
This function is called once for each conflicted path, which means we would read the "merge.renormalize" configuration variable and the MERGE_LABELS file, both of which will stay constant during a single conflicted "checkout -m". The issue is shared with the original, but looking up the same configuration variable repeatedly would be helped with in-core configset cache. Compared to that, the overhead added by this patch is to open the same unchanging file, read & parse, allocate and deallocate.
Perhaps we want to have another preliminary [PATCH 1.5/2] before this step to allow setting these "per invocation constants" once to be reused? Then step [PATCH 2/2] can read labels in the prepare phace just once, use it from the data structure in checkout_merged(), and free them in release phase when we are done.
builtin/checkout.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-)
builtin/checkout.c +31 -6
@@ -310,9 +310,27 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,return error (_ ("path '%s' does not have their version" ), ce->name); } +struct checkout_merged_data { +int conflict_style; +int renormalize; +/* we will add more later */ +}; + +static void checkout_merged_release (struct checkout_merged_data *data) +{ + ;/* nothing to free (yet) */ +} + +static void checkout_merged_prepare (struct checkout_merged_data *data) +{ +int renormalize =0 ; +repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); + data->renormalize = renormalize; +} +static int checkout_merged (int pos,const struct checkout *state,int *nr_checkouts,struct mem_pool *ce_mem_pool, -int conflict_style) +struct checkout_merged_data *data) {struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;
@@ -324,7 +342,6 @@ static int checkout_merged(int pos, const struct checkout *state,struct object_id threeway[3 ];unsigned mode =0 ;struct ll_merge_options ll_opts =LL_MERGE_OPTIONS_INIT ; -int renormalize =0 ;memset (threeway,0 ,sizeof (threeway));while (pos < the_repository->index->cache_nr) {
@@ -345,9 +362,8 @@ static int checkout_merged(int pos, const struct checkout *state,read_mmblob (&ours, the_repository->objects, &threeway[1 ]);read_mmblob (&theirs, the_repository->objects, &threeway[2 ]); -repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); - ll_opts.renormalize = renormalize; - ll_opts.conflict_style = conflict_style; + ll_opts.renormalize = data->renormalize; + ll_opts.conflict_style = data->conflict_style; merge_status =ll_merge (&result_buf, path, &ancestor,"base" , &ours,"ours" , &theirs,"theirs" , state->istate, &ll_opts);
@@ -446,6 +462,7 @@ static int checkout_worktree(const struct checkout_opts *opts,int pos;int pc_workers, pc_threshold;struct mem_pool ce_mem_pool; +struct checkout_merged_data checkout_merged_data = {0 }; state.force =1 ; state.refresh_cache =1 ;
@@ -462,6 +479,10 @@ static int checkout_worktree(const struct checkout_opts *opts,if (pc_workers >1 )init_parallel_checkout (); +if (opts->merge) { +checkout_merged_prepare (&checkout_merged_data); + checkout_merged_data.conflict_style = opts->conflict_style; + }for (pos =0 ; pos < the_repository->index->cache_nr; pos++) {struct cache_entry *ce = the_repository->index->cache[pos];if (ce->ce_flags &CE_MATCHED ) {
@@ -479,10 +500,14 @@ static int checkout_worktree(const struct checkout_opts *opts, errs |=checkout_merged (pos, &state, &nr_unmerged, &ce_mem_pool, - opts->conflict_style); + &checkout_merged_data); pos =skip_same_name (ce, pos) -1 ; } } + +if (opts->merge) +checkout_merged_release (&checkout_merged_data); +if (pc_workers >1 ) errs |=run_parallel_checkout (&state, pc_workers, pc_threshold,NULL ,NULL );
Re: [PATCH v3 2/2] merge: remember conflict labels
Hi Junio
Quoted 10 lines · Junio C Hamano
This function is called once for each conflicted path, which means we would read the "merge.renormalize" configuration variable and the
MERGE_LABELSfile, both of which will stay constant during a single conflicted "checkout -m". The issue is shared with the original, but looking up the same configuration variable repeatedly would be helped with in-core configset cache. Compared to that, the overhead added by this patch is to open the same unchanging file, read & parse, allocate and deallocate.
Quoted 5 lines
Perhaps we want to have another preliminary [PATCH 1.5/2] before this step to allow setting these "per invocation constants" once to be reused? Then step [PATCH 2/2] can read labels in the prepare phace just once, use it from the data structure in
checkout_merged(), and free them in release phase when we are done.
That's a good idea, I'll re-roll using the diff below (I assume you're happy for me to add your sign off?)
Thanks
Phillip
Quoted 93 lines
builtin/checkout.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-)builtin/checkout.c +31
-6@@ -310,9 +310,27 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,return error (_ ("path '%s' does not have their version" ), ce->name); } +struct checkout_merged_data { +int conflict_style; +int renormalize; +/* we will add more later */ +}; + +static void checkout_merged_release (struct checkout_merged_data *data) +{ + ;/* nothing to free (yet) */ +} + +static void checkout_merged_prepare (struct checkout_merged_data *data) +{ +int renormalize =0 ; +repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); + data->renormalize = renormalize; +} +static int checkout_merged (int pos,const struct checkout *state,int *nr_checkouts,struct mem_pool *ce_mem_pool, -int conflict_style) +struct checkout_merged_data *data) {struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;@@ -324,7 +342,6 @@ static int checkout_merged(int pos, const struct checkout *state,struct object_id threeway[3 ];unsigned mode =0 ;struct ll_merge_options ll_opts =LL_MERGE_OPTIONS_INIT ; -int renormalize =0 ;memset (threeway,0 ,sizeof (threeway));while (pos < the_repository->index->cache_nr) {@@ -345,9 +362,8 @@ static int checkout_merged(int pos, const struct checkout *state,read_mmblob (&ours, the_repository->objects, &threeway[1 ]);read_mmblob (&theirs, the_repository->objects, &threeway[2 ]); -repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); - ll_opts.renormalize = renormalize; - ll_opts.conflict_style = conflict_style; + ll_opts.renormalize = data->renormalize; + ll_opts.conflict_style = data->conflict_style; merge_status =ll_merge (&result_buf, path, &ancestor,"base" , &ours,"ours" , &theirs,"theirs" , state->istate, &ll_opts);@@ -446,6 +462,7 @@ static int checkout_worktree(const struct checkout_opts *opts,int pos;int pc_workers, pc_threshold;struct mem_pool ce_mem_pool; +struct checkout_merged_data checkout_merged_data = {0 }; state.force =1 ; state.refresh_cache =1 ;@@ -462,6 +479,10 @@ static int checkout_worktree(const struct checkout_opts *opts,if (pc_workers >1 )init_parallel_checkout (); +if (opts->merge) { +checkout_merged_prepare (&checkout_merged_data); + checkout_merged_data.conflict_style = opts->conflict_style; + }for (pos =0 ; pos < the_repository->index->cache_nr; pos++) {struct cache_entry *ce = the_repository->index->cache[pos];if (ce->ce_flags &CE_MATCHED ) {@@ -479,10 +500,14 @@ static int checkout_worktree(const struct checkout_opts *opts, errs |=checkout_merged (pos, &state, &nr_unmerged, &ce_mem_pool, - opts->conflict_style); + &checkout_merged_data); pos =skip_same_name (ce, pos) -1 ; } } + +if (opts->merge) +checkout_merged_release (&checkout_merged_data); +if (pc_workers >1 ) errs |=run_parallel_checkout (&state, pc_workers, pc_threshold,NULL ,NULL );
Re: [PATCH v3 2/2] merge: remember conflict labels
Quoted 13 lines · Phillip Wood
Hi Junio
Quoted 10 lines · Junio C Hamano
This function is called once for each conflicted path, which means we would read the "merge.renormalize" configuration variable and the
MERGE_LABELSfile, both of which will stay constant during a single conflicted "checkout -m". The issue is shared with the original, but looking up the same configuration variable repeatedly would be helped with in-core configset cache. Compared to that, the overhead added by this patch is to open the same unchanging file, read & parse, allocate and deallocate.
Quoted 8 lines
Quoted 5 lines
Perhaps we want to have another preliminary [PATCH 1.5/2] before this step to allow setting these "per invocation constants" once to be reused? Then step [PATCH 2/2] can read labels in the prepare phace just once, use it from the data structure in
checkout_merged(), and free them in release phase when we are done.That's a good idea, I'll re-roll using the diff below (I assume you're happy for me to add your sign off?)
You assumed correctly, but if you ask me, I would say that it probably does not count much as coauthorship than a mere helped-by.
Re: [PATCH v3 2/2] merge: remember conflict labels
Quoted 18 lines · Junio C Hamano
Quoted 17 lines · Phillip Wood
Quoted 15 lines · Junio C Hamano
This function is called once for each conflicted path, which means we would read the "merge.renormalize" configuration variable and the
MERGE_LABELSfile, both of which will stay constant during a single conflicted "checkout -m". The issue is shared with the original, but looking up the same configuration variable repeatedly would be helped with in-core configset cache. Compared to that, the overhead added by this patch is to open the same unchanging file, read & parse, allocate and deallocate.Perhaps we want to have another preliminary [PATCH 1.5/2] before this step to allow setting these "per invocation constants" once to be reused? Then step [PATCH 2/2] can read labels in the prepare phace just once, use it from the data structure in
checkout_merged(), and free them in release phase when we are done.
Quoted 5 lines
Quoted 2 lines
That's a good idea, I'll re-roll using the diff below (I assume you're happy for me to add your sign off?)
You assumed correctly, but if you ask me, I would say that it probably does not count much as coauthorship than a mere helped-by.
As the new patch is a very lightly edited version of your diff I've made you the author.
Thanks
Phillip
Quoted 10 lines · Phillip Wood
Changes since V2:
- only write ".git/MERGE_LABELS" when there are conflicts and added a check to an existing "checkout -m <branch>" test
- changewrite_merge_labels()to take an array of labels
- use a local variable to store the internal merge state when writing labels
- usestrbuf_detach()rather thanxmemdupz()when reading labels
- add a comment to say we ignore trailing cruft when reading the labels file
These patches looked nicely done.
Will replace. Thanks.
v4 cover checkout -m: recreate conflict labels
In reply to Phillip WoodWhen "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Changes since V3:
- added patch 2 to cache merge config settings and conflict labels
Changes since V2:
- only write ".git/MERGE_LABELS" when there are conflicts and added a check to an existing "checkout -m <branch>" test
- change write_merge_labels() to take an array of labels
- use a local variable to store the internal merge state when writing labels
- use strbuf_detach() rather than xmemdupz() when reading labels
- add a comment to say we ignore trailing cruft when reading the labels file
Changes since V1:
- use strbuf_getline() rather than strbuf_read_file() to read labels so that the newline handling of the reading and writing sides match.
NB ".git/MERGE_LABELS" is still undocumented - I'm hoping to find time to add some documentation for all the MERGE_* files in a future series. Johannes suggested using an index extension to store the labels, but as we already have MERGE_MODE, MERGE_RR and MERGE_MSG I think it is easier just to add another file.
base-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fconflict-labels%2Fv4
View-Changes-At: https://github.com/phillipwood/git/compare/3bc034112...eda10c234
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/conflict-labels/v4
Junio C Hamano (1):
checkout -m <path>: cache merge settings
Phillip Wood (2):
remove_branch_state: convert boolean argument to flags
merge: remember conflict labels
branch.c | 17 ++++++++---- branch.h | 4 ++- builtin/checkout.c | 66 ++++++++++++++++++++++++++++++++++++++-------- builtin/commit.c | 1 + merge-ort.c | 20 ++++++++++++++ merge.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 3 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 22 ++++++++++++++++ 13 files changed, 187 insertions(+), 17 deletions(-)
Range-diff against v3:
1: 86ef0f848a = 1: 86ef0f848a remove_branch_state: convert boolean argument to flags -: ---------- > 2: 36f3808d2f checkout -m <path>: cache merge settings 2: 182edb2e88 ! 3: eda10c2340 merge: remember conflict labels
@@ builtin/checkout.c
#include "lockfile.h"
#include "mem-pool.h"
#include "object-file.h"
-@@ builtin/checkout.c: static int checkout_merged(int pos, const struct checkout *state,
- struct cache_entry *ce = the_repository->index->cache[pos];
- const char *path = ce->name;
- mmfile_t ancestor, ours, theirs;
+@@ builtin/checkout.c: static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
+ struct checkout_merged_data {
+ int conflict_style;
+ int renormalize;
+ char *base_label, *ours_label, *theirs_label;
- enum ll_merge_result merge_status;
- int status;
- struct object_id oid;
-@@ builtin/checkout.c: static int checkout_merged(int pos, const struct checkout *state,
+ };
+
++static void checkout_merged_release(struct checkout_merged_data *data)
++{
++ free(data->base_label);
++ free(data->ours_label);
++ free(data->theirs_label);
++}
++
+ static void checkout_merged_prepare(struct checkout_merged_data *data)
+ {
+ int renormalize = 0;
repo_config_get_bool(the_repository, "merge.renormalize", &renormalize);
- ll_opts.renormalize = renormalize;
-+ if (read_merge_labels(the_repository, &base_label, &ours_label,
-+ &theirs_label)) {
-+ base_label = xstrdup("base");
-+ ours_label = xstrdup("ours");
-+ theirs_label = xstrdup("theirs");
+ data->renormalize = renormalize;
++ if (read_merge_labels(the_repository, &data->base_label, &data->ours_label,
++ &data->theirs_label)) {
++ data->base_label = xstrdup("base");
++ data->ours_label = xstrdup("ours");
++ data->theirs_label = xstrdup("theirs");
+ }
- ll_opts.conflict_style = conflict_style;
+ }
+
+ static int checkout_merged(int pos, const struct checkout *state,
+@@ builtin/checkout.c: static int checkout_merged(int pos, const struct checkout *state, +
+ ll_opts.renormalize = data->renormalize;
+ ll_opts.conflict_style = data->conflict_style;
- merge_status = ll_merge(&result_buf, path, &ancestor, "base",
- &ours, "ours", &theirs, "theirs",
-+ merge_status = ll_merge(&result_buf, path, &ancestor, base_label,
-+ &ours, ours_label, &theirs, theirs_label,
++ merge_status = ll_merge(&result_buf, path, &ancestor, data->base_label,
++ &ours, data->ours_label, &theirs, data->theirs_label,
state->istate, &ll_opts);
-+ free(base_label);
-+ free(ours_label);
-+ free(theirs_label);
free(ancestor.ptr);
free(ours.ptr);
- free(theirs.ptr);
+@@ builtin/checkout.c: static int checkout_worktree(const struct checkout_opts *opts,
+ pos = skip_same_name(ce, pos) - 1;
+ }
+ }
++
++ if (opts->merge)
++ checkout_merged_release(&checkout_merged_data);
++
+ if (pc_workers > 1)
+ errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
+ NULL, NULL);
@@ builtin/checkout.c: static void report_tracking(struct branch_info *new_branch_info)
static void update_refs_for_switch(const struct checkout_opts *opts,
Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v4 1/3 remove_branch_state: convert boolean argument to flags
From: Phillip Wood <[email protected]>
Convert the "verbose" boolean argument to a flag so that we can add more flags in a future commit.
--- branch.c | 4 ++-- branch.h | 3 ++- builtin/checkout.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-)
branch.c +2 -2
@@ -871,9 +871,9 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } -void remove_branch_state (struct repository *r,int verbose) +void remove_branch_state (struct repository *r,unsigned flags) { -sequencer_post_commit_cleanup (r, verbose); +sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r));remove_merge_branch_state (r); }
branch.h +2 -1
@@ -127,6 +127,7 @@ int validate_branchname(const char *name, struct strbuf *ref);*/ int validate_new_branchname (const char *name,struct strbuf *ref,int force); +#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
@@ -137,7 +138,7 @@ void remove_merge_branch_state(struct repository *r);* Remove information about the state of working on the current * branch. (E.g., MERGE_HEAD) */ -void remove_branch_state (struct repository *r,int verbose); +void remove_branch_state (struct repository *r,unsigned flags);/* * Configure local branch "local" as downstream to branch "remote"
builtin/checkout.c +5 -1
@@ -950,6 +950,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg; +unsigned flags =0 ; +if (opts->new_branch) {if (opts->new_orphan_branch) {enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;
@@ -1044,7 +1046,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts, old_branch_info->path); } } -remove_branch_state (the_repository, !opts->quiet); +if (!opts->quiet) + flags |=REMOVE_BRANCH_STATE_VERBOSE ; +remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet && !opts->force_detach &&
Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v4 2/3 checkout -m <path>: cache merge settings
In reply to Phillip WoodFrom: Junio C Hamano <[email protected]>
Lookup the conflict style and normalization config settings once and cache the results, rather than looking up the same data for every path that is being merged. While looking up the config settings each time is fairly inexpensive, the next commit will start reading the conflict labels to use from a file and we want to avoid doing that more than once.
--- builtin/checkout.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
builtin/checkout.c +22 -6
@@ -310,9 +310,22 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,return error (_ ("path '%s' does not have their version" ), ce->name); } +struct checkout_merged_data { +int conflict_style; +int renormalize; +}; + +static void checkout_merged_prepare (struct checkout_merged_data *data) +{ +int renormalize =0 ; + +repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); + data->renormalize = renormalize; +} +static int checkout_merged (int pos,const struct checkout *state,int *nr_checkouts,struct mem_pool *ce_mem_pool, -int conflict_style) +struct checkout_merged_data *data) {struct cache_entry *ce = the_repository->index->cache[pos];const char *path = ce->name;
@@ -324,7 +337,6 @@ static int checkout_merged(int pos, const struct checkout *state,struct object_id threeway[3 ];unsigned mode =0 ;struct ll_merge_options ll_opts =LL_MERGE_OPTIONS_INIT ; -int renormalize =0 ;memset (threeway,0 ,sizeof (threeway));while (pos < the_repository->index->cache_nr) {
@@ -345,9 +357,8 @@ static int checkout_merged(int pos, const struct checkout *state,read_mmblob (&ours, the_repository->objects, &threeway[1 ]);read_mmblob (&theirs, the_repository->objects, &threeway[2 ]); -repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); - ll_opts.renormalize = renormalize; - ll_opts.conflict_style = conflict_style; + ll_opts.renormalize = data->renormalize; + ll_opts.conflict_style = data->conflict_style; merge_status =ll_merge (&result_buf, path, &ancestor,"base" , &ours,"ours" , &theirs,"theirs" , state->istate, &ll_opts);
@@ -446,6 +457,7 @@ static int checkout_worktree(const struct checkout_opts *opts,int pos;int pc_workers, pc_threshold;struct mem_pool ce_mem_pool; +struct checkout_merged_data checkout_merged_data = {0 }; state.force =1 ; state.refresh_cache =1 ;
@@ -462,6 +474,10 @@ static int checkout_worktree(const struct checkout_opts *opts,if (pc_workers >1 )init_parallel_checkout (); +if (opts->merge) { +checkout_merged_prepare (&checkout_merged_data); + checkout_merged_data.conflict_style = opts->conflict_style; + }for (pos =0 ; pos < the_repository->index->cache_nr; pos++) {struct cache_entry *ce = the_repository->index->cache[pos];if (ce->ce_flags &CE_MATCHED ) {
@@ -479,7 +495,7 @@ static int checkout_worktree(const struct checkout_opts *opts, errs |=checkout_merged (pos, &state, &nr_unmerged, &ce_mem_pool, - opts->conflict_style); + &checkout_merged_data); pos =skip_same_name (ce, pos) -1 ; } }
Signature · 1 line
2.56.0.134.g299a3c16181
PATCH v4 3/3 merge: remember conflict labels
In reply to Phillip WoodFrom: Phillip Wood <[email protected]>
When recreating merge conflicts with "git checkout -m <path>" the original conflict labels are lost. For commands like "git merge" and "git cherry-pick" we could use the presence of the related root ref (MERGE_HEAD and CHERRY_PICK_HEAD respectively) to recreate the labels. However, if the conflicts are from "git stash pop" or "git checkout -m <branch>", then there is no ref to deduce the labels from. To ensure the labels are always available, the merge machinery is updated to write ".git/MERGE_LABELS" when it updates the worktree and there are conflicts. The labels are then read from that file by "git checkout -m <path>" when recreating the conflicts.
As "git checkout -m <branch>" calls remove_branch_state() which ordinarily removes the labels file, we need to pass a flag down to optionally prevent that so that the labels are available for any subsequent "git checkout -m <path>". Note that merge_switch_to_result() we assign "result->priv" to "opt->priv" and later clear "opt->priv" in order to get a pointer to the private struct as result->priv is void*.
--- branch.c | 11 ++++++-- branch.h | 1 + builtin/checkout.c | 32 +++++++++++++++++++--- builtin/commit.c | 1 + merge-ort.c | 20 ++++++++++++++ merge.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ merge.h | 3 +++ path.c | 1 + path.h | 1 + repository.c | 1 + repository.h | 1 + sequencer.c | 1 + t/t7201-co.sh | 22 ++++++++++++++++ 13 files changed, 155 insertions(+), 6 deletions(-)
branch.c +9 -2
@@ -860,9 +860,11 @@ void create_branches_recursively(struct repository *r, const char *name,free (branch_point); } -void remove_merge_branch_state (struct repository *r) +static void do_remove_merge_branch_state (struct repository *r,unsigned flags) {unlink (git_path_merge_head (r)); +if (!(flags &REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS )) +unlink (git_path_merge_labels (r));unlink (git_path_merge_rr (r));unlink (git_path_merge_msg (r));unlink (git_path_merge_mode (r));
@@ -871,11 +873,16 @@ void remove_merge_branch_state(struct repository *r)save_autostash_ref (r,"MERGE_AUTOSTASH" ); } +void remove_merge_branch_state (struct repository *r) +{ +do_remove_merge_branch_state (r,0 ); +} +void remove_branch_state (struct repository *r,unsigned flags) {sequencer_post_commit_cleanup (r, flags &REMOVE_BRANCH_STATE_VERBOSE );unlink (git_path_squash_msg (r)); -remove_merge_branch_state (r); +do_remove_merge_branch_state (r, flags); }void die_if_checked_out (const char *branch,int ignore_current_worktree)
branch.h +1 -0
@@ -128,6 +128,7 @@ int validate_branchname(const char *name, struct strbuf *ref);int validate_new_branchname (const char *name,struct strbuf *ref,int force);#define REMOVE_BRANCH_STATE_VERBOSE (1u << 0) +#define REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS (1u << 1)/* * Remove information about the merge state on the current * branch. (E.g., MERGE_HEAD)
builtin/checkout.c +28 -4
@@ -15,6 +15,7 @@#include "hex.h" #include "hook.h" #include "merge-ll.h" +#include "merge.h" #include "lockfile.h" #include "mem-pool.h" #include "object-file.h"
@@ -313,14 +314,28 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,struct checkout_merged_data {int conflict_style;int renormalize; +char *base_label, *ours_label, *theirs_label; }; +static void checkout_merged_release (struct checkout_merged_data *data) +{ +free (data->base_label); +free (data->ours_label); +free (data->theirs_label); +} +static void checkout_merged_prepare (struct checkout_merged_data *data) {int renormalize =0 ;repo_config_get_bool (the_repository,"merge.renormalize" , &renormalize); data->renormalize = renormalize; +if (read_merge_labels (the_repository, &data->base_label, &data->ours_label, + &data->theirs_label)) { + data->base_label =xstrdup ("base" ); + data->ours_label =xstrdup ("ours" ); + data->theirs_label =xstrdup ("theirs" ); + } } static int checkout_merged(int pos, const struct checkout *state,
@@ -359,8 +374,8 @@ static int checkout_merged(int pos, const struct checkout *state, ll_opts.renormalize = data->renormalize; ll_opts.conflict_style = data->conflict_style; - merge_status =ll_merge (&result_buf, path, &ancestor,"base" , - &ours,"ours" , &theirs,"theirs" , + merge_status =ll_merge (&result_buf, path, &ancestor, data->base_label, + &ours, data->ours_label, &theirs, data->theirs_label, state->istate, &ll_opts);free (ancestor.ptr);free (ours.ptr);
@@ -499,6 +514,10 @@ static int checkout_worktree(const struct checkout_opts *opts, pos =skip_same_name (ce, pos) -1 ; } } + +if (opts->merge) +checkout_merged_release (&checkout_merged_data); +if (pc_workers >1 ) errs |=run_parallel_checkout (&state, pc_workers, pc_threshold,NULL ,NULL );
@@ -962,7 +981,8 @@ static void report_tracking(struct branch_info *new_branch_info)static void update_refs_for_switch (const struct checkout_opts *opts,struct branch_info *old_branch_info, -struct branch_info *new_branch_info) +struct branch_info *new_branch_info, +bool merge_conflicts) {struct strbuf msg =STRBUF_INIT ;const char *old_desc, *reflog_msg;
@@ -1064,6 +1084,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }if (!opts->quiet) flags |=REMOVE_BRANCH_STATE_VERBOSE ; +if (merge_conflicts) + flags |=REMOVE_BRANCH_STATE_PRESERVE_CONFLICT_LABELS ;remove_branch_state (the_repository, flags);strbuf_release (&msg); if (!opts->quiet &&
@@ -1278,7 +1300,9 @@ static int switch_branches(const struct checkout_opts *opts,if (autostash_res ==STASH_APPLY_CONFLICT && !opts->quiet)fputc ('\n' , stderr); -update_refs_for_switch (opts, &old_branch_info, new_branch_info); + +update_refs_for_switch (opts, &old_branch_info, new_branch_info, + autostash_res ==STASH_APPLY_CONFLICT );if (created_autostash) {discard_index (the_repository->index);
builtin/commit.c +1 -0
@@ -1977,6 +1977,7 @@ int cmd_commit(int argc,sequencer_post_commit_cleanup (the_repository,0 );unlink (git_path_merge_head (the_repository)); +unlink (git_path_merge_labels (the_repository));unlink (git_path_merge_msg (the_repository));unlink (git_path_merge_mode (the_repository));unlink (git_path_squash_msg (the_repository));
merge-ort.c +20 -0
@@ -35,6 +35,7 @@#include "hex.h" #include "entry.h" #include "merge-ll.h" +#include "merge.h" #include "match-trees.h" #include "mem-pool.h" #include "object-file.h"
@@ -418,6 +419,9 @@ struct merge_options_internal {/* field that holds submodule conflict information */ struct string_list conflicted_submodules; + +/* Copies of the labels used for conflict markers */ +const char *labels[3 ]; };struct conflicted_submodule_item {
@@ -4969,6 +4973,14 @@ void merge_switch_to_result(struct merge_options *opt,return ; }trace2_region_leave ("merge" ,"write_auto_merge" , opt->repo); + +if (!result->clean) { +struct merge_options_internal *priv = result->priv; + +trace2_region_enter ("merge" ,"write_merge_labels" , opt->repo); +write_merge_labels (opt->repo, priv->labels); +trace2_region_leave ("merge" ,"write_merge_labels" , opt->repo); + } }if (display_update_msgs)merge_display_update_messages (opt,/* detailed */ 0 , result);
@@ -5234,6 +5246,14 @@ static void move_opt_priv_to_result_priv(struct merge_options *opt,* to move it. */ assert (opt->priv && !result->priv); +if (!result->clean) { + opt->priv->labels[0 ] = +mem_pool_strdup (&opt->priv->pool, opt->ancestor); + opt->priv->labels[1 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch1); + opt->priv->labels[2 ] = +mem_pool_strdup (&opt->priv->pool, opt->branch2); + } result->priv = opt->priv; result->_properly_initialized =RESULT_INITIALIZED ; opt->priv =NULL ;
merge.c +66 -0
75 lines not on this page · raw message
merge.h +3 -0
@@ -13,5 +13,8 @@ int checkout_fast_forward(struct repository *r,const struct object_id *from, const struct object_id *to, int overwrite_ignore); +int write_merge_labels (struct repository *r,const char *labels[3 ]); +int read_merge_labels (struct repository *r, +char **base,char **ours,char **theirs);#endif /* MERGE_H */
path.c +1 -0
@@ -1655,3 +1655,4 @@ REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") REPO_GIT_PATH_FUNC(shallow, "shallow") +REPO_GIT_PATH_FUNC (merge_labels,"MERGE_LABELS" )
path.h +1 -0
@@ -142,6 +142,7 @@ const char *git_path_merge_mode(struct repository *r);const char *git_path_merge_head (struct repository *r);const char *git_path_fetch_head (struct repository *r);const char *git_path_shallow (struct repository *r); +const char *git_path_merge_labels (struct repository *r);int ends_with_path_components (const char *path,const char *components);
repository.c +1 -0
@@ -367,6 +367,7 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)FREE_AND_NULL (cache->merge_head);FREE_AND_NULL (cache->fetch_head);FREE_AND_NULL (cache->shallow); +FREE_AND_NULL (cache->merge_labels); }void repo_clear (struct repository *repo)
repository.h +1 -0
@@ -36,6 +36,7 @@ struct repo_path_cache {char *merge_head;char *fetch_head;char *shallow; +char *merge_labels; };struct repository {
sequencer.c +1 -0
@@ -5145,6 +5145,7 @@ static int pick_commits(struct repository *r,unlink (rebase_path_stopped_sha ());unlink (rebase_path_amend ());unlink (rebase_path_patch ()); +unlink (git_path_merge_labels (r));while (todo_list->current < todo_list->nr) {struct todo_item *item = todo_list->items + todo_list->current;
t/t7201-co.sh +22 -0
@@ -99,6 +99,7 @@ test_expect_success 'checkout -m with dirty tree' ' fill 0 1 2 3 4 5 6 7 8 >one && git checkout -m side >messages && + test_path_is_missing .git/MERGE_LABELS && test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
@@ -183,6 +184,27 @@ test_expect_success 'format of merge conflict from checkout -m' ' d >>>>>>> local EOF + test_cmp expect two && + + test_path_is_file .git/MERGE_LABELS && + + git checkout --conflict=diff3 two && + cat >expect <<-\EOF && + <<<<<<< simple + a + c + e + ||||||| main + a + b + c + d + e + ======= + b + d + >>>>>>> local + EOF test_cmp expect two '
Signature · 1 line
2.56.0.134.g299a3c16181
Quoted 6 lines · Phillip Wood
When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.
Both range-diff and "git diff @{1}" (endstate comparison) looked sensible. I may take a deeper look later on individual patches, but so far this is looking real good.
Thanks, will replace.