builtin/merge.c - cleanup of code in for-cycle that tests strategies
PATCH 1/1 builtin/merge.c - cleanup of code in for-cycle that tests strategies
Previous code was a little convoluted to follow logic New code is shorter and logic is easier to follow
- Easier to see what happens when merge is successful
and how --no-commit affects result
- Simpler to see that for-cycle will stop when merge_was_ok is set
- Easier to spot what logic will run through best_strategy
- Easier to see that in case of ret being 2, cycle will continue
- Keep a single break case (when automerge succedes and a revision will be created)
- Put together closing actions when automerge succedes if a revision will be created
--- builtin/merge.c | 51 ++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-)
builtin/merge.c +19 -32
@@ -1586,7 +1586,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) save_state(&stash))oidclr (&stash); -for (i =0 ; i < use_strategies_nr; i++) { +for (i =0 ; !merge_was_ok && i < use_strategies_nr; i++) {int ret;if (i) {printf (_ ("Rewinding the tree to pristine...\n" ));
@@ -1604,40 +1604,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix) ret =try_merge_strategy (use_strategies[i]->name, common, remoteheads, head_commit); -if (!option_commit && !ret) { - merge_was_ok =1 ; -/* -* This is necessary here just to avoid writing -* the tree, but later we will *not* exit with -* status code 1 because merge_was_ok is set. -*/ - ret =1 ; - } - -if (ret) { -/* -* The backend exits with 1 when conflicts are -* left to be resolved, with 2 when it does not -* handle the given merge at all. -*/ -if (ret ==1 ) { -int cnt =evaluate_result (); - -if (best_cnt <=0 || cnt <= best_cnt) { - best_strategy = use_strategies[i]->name; - best_cnt = cnt; +/* +* The backend exits with 1 when conflicts are +* left to be resolved, with 2 when it does not +* handle the given merge at all. +*/ +if (ret <2 ) { +if (!ret) { +if (option_commit) { +/* Automerge succeeded. */ + automerge_was_ok =1 ; +break ; } + merge_was_ok =1 ; + } +int cnt =evaluate_result (); +if (best_cnt <=0 || cnt <= best_cnt) { + best_strategy = use_strategies[i]->name; + best_cnt = cnt; } -if (merge_was_ok) -break ; -else -continue ; } - -/* Automerge succeeded. */ -write_tree_trivial (&result_tree); - automerge_was_ok =1 ; -break ; }/*
@@ -1645,6 +1631,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)* auto resolved the merge cleanly. */ if (automerge_was_ok) { +write_tree_trivial (&result_tree); ret =finish_automerge (head_commit, head_subsumed, common, remoteheads, &result_tree, wt_strategy);
Signature · 1 line
2.20.1
builtin/merge.c int cmd_merge(int argc, const char **argv, const char *prefix) · line 1637
Quoted 8 lines · Edmundo Carmona Antoranz
builtin/merge.c +1
-0@@ -1645,6 +1631,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)* auto resolved the merge cleanly. */ if (automerge_was_ok) { +write_tree_trivial (&result_tree); ret =finish_automerge (head_commit, head_subsumed, common, remoteheads, &result_tree, wt_strategy);
I realized later that the call to write_tree_trivial could be included in finish_automerge. Will include this change on v2 of the patch (if there's a v2, depending on feedback).
Quoted 2 lines · Edmundo Carmona Antoranz
Previous code was a little convoluted to follow logic New code is shorter and logic is easier to follow
In the body of the proposed commit log message, please finish each sentence with a full-stop.
Quoted 5 lines
- Easier to see what happens when merge is successful
and how --no-commit affects result- Simpler to see that for-cycle will stop when
merge_was_okis set
- Easier to spot what logic will run throughbest_strategy
- Easier to see that in case of ret being 2, cycle will continue
These bullets are all subjective, and do not add any value to what you already said in the second sentence.
Quoted 4 lines
- Keep a single break case (when automerge succedes and a revision will be created)
- Put together closing actions when automerge succedes if a revision will be created
These are facts that readers can see for themselves and agree with.
Something like...
The cmd_merge() function has a loop that tries different merge strategies in turn, and stops when a strategy gets a clean merge, while keeping the "best" conflicted merge so far.
Make the loop easier to follow by moving the code around, ensuring that there is only one "break" in the loop where an automerge succeeds. Also group the actions that are performed after an automerge succeeds together to a single location, outside and after the loop.
perhaps?
Quoted 49 lines
Signed-off-by Edmundo Carmona Antoranz <[email protected]>--- builtin/merge.c | 51 ++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-)builtin/merge.c +6
-23@@ -1586,7 +1586,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) save_state(&stash))oidclr (&stash); -for (i =0 ; i < use_strategies_nr; i++) { +for (i =0 ; !merge_was_ok && i < use_strategies_nr; i++) {int ret;if (i) {printf (_ ("Rewinding the tree to pristine...\n" ));@@ -1604,40 +1604,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix) ret =try_merge_strategy (use_strategies[i]->name, common, remoteheads, head_commit); -if (!option_commit && !ret) { - merge_was_ok =1 ; -/* -* This is necessary here just to avoid writing -* the tree, but later we will *not* exit with -* status code 1 because merge_was_ok is set. -*/ - ret =1 ; - } - -if (ret) { -/* -* The backend exits with 1 when conflicts are -* left to be resolved, with 2 when it does not -* handle the given merge at all. -*/ -if (ret ==1 ) { -int cnt =evaluate_result (); - -if (best_cnt <=0 || cnt <= best_cnt) { - best_strategy = use_strategies[i]->name; - best_cnt = cnt; +/* +* The backend exits with 1 when conflicts are +* left to be resolved, with 2 when it does not +* handle the given merge at all. +*/
builtin/merge.c int cmd_merge(int argc, const char **argv, const char *prefix) · line 1621
Quoted 10 lines
builtin/merge.c +9
-0+if (ret <2 ) { +if (!ret) { +if (option_commit) { +/* Automerge succeeded. */ + automerge_was_ok =1 ; +break ; } + merge_was_ok =1 ; + } +int cnt =evaluate_result ();
This introduces -Wdeclaration-after-statement, doesn't it? Perhaps just declare the variable at the top of the for loop, next to where the local 'ret' is declared?
Other than this single glitch, I think the code with this patch does become easier to follow.
Quoted 25 lines
builtin/merge.c +4
-9+if (best_cnt <=0 || cnt <= best_cnt) { + best_strategy = use_strategies[i]->name; + best_cnt = cnt; } -if (merge_was_ok) -break ; -else -continue ; } - -/* Automerge succeeded. */ -write_tree_trivial (&result_tree); - automerge_was_ok =1 ; -break ; }/* @@ -1645,6 +1631,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)* auto resolved the merge cleanly. */ if (automerge_was_ok) { +write_tree_trivial (&result_tree); ret =finish_automerge (head_commit, head_subsumed, common, remoteheads, &result_tree, wt_strategy);
Quoted 2 lines · Junio C Hamano
In the body of the proposed commit log message, please finish each sentence with a full-stop.
Ok
Quoted 3 lines
These bullets are all subjective, and do not add any value to what you already said in the second sentence.
Ok
Quoted 10 lines
These are facts that readers can see for themselves and agree with.
Something like...
The
cmd_merge()function has a loop that tries different merge strategies in turn, and stops when a strategy gets a clean merge, while keeping the "best" conflicted merge so far.
Quoted 7 lines
Make the loop easier to follow by moving the code around, ensuring that there is only one "break" in the loop where an automerge succeeds. Also group the actions that are performed after an automerge succeeds together to a single location, outside and after the loop.
perhaps?
I like it.
Quoted 2 lines
Quoted 1 line
+ int cnt =
evaluate_result();This introduces -Wdeclaration-after-statement, doesn't it?
It does! Let me take care of it. (Didn't know about DEVELOPER environment variable up until... 5 minutes ago?)
Quoted 2 lines
Perhaps just declare the variable at the top of the for loop, next to where the local 'ret' is declared?
Yep, make sense
Quoted 3 lines
Other than this single glitch, I think the code with this patch does become easier to follow.
Great!
Let me come back with v2 during the day.
Thanks!