Skip to content

Commit 3055d78

Browse files
Clemens Buchachergitster
authored andcommitted
use persistent memory for rejected paths
An aborted merge prints the list of rejected paths as part of the error message. Since commit f66caaf (do not overwrite files in leading path), some of those paths do not have static buffers, so we have to keep a copy. Use string_list's to accomplish this. This changes the order of the list to the order in which the paths are processed. Previously, it was reversed. Signed-off-by: Clemens Buchacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1735b1 commit 3055d78

File tree

4 files changed

+38
-42
lines changed

4 files changed

+38
-42
lines changed

t/t7607-merge-overwrite.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ test_expect_success 'setup' '
1515
git reset --hard c0 &&
1616
mkdir sub &&
1717
echo "sub/f" > sub/f &&
18-
git add sub/f &&
18+
mkdir sub2 &&
19+
echo "sub2/f" > sub2/f &&
20+
git add sub/f sub2/f &&
1921
git commit -m sub &&
2022
git tag sub &&
2123
echo "VERY IMPORTANT CHANGES" > important
@@ -100,13 +102,24 @@ test_expect_success 'will not overwrite untracked subtree' '
100102
test_cmp important sub/f/important
101103
'
102104

105+
cat >expect <<\EOF
106+
error: The following untracked working tree files would be overwritten by merge:
107+
sub
108+
sub2
109+
Please move or remove them before you can merge.
110+
EOF
111+
103112
test_expect_success 'will not overwrite untracked file in leading path' '
104113
git reset --hard c0 &&
105114
rm -rf sub &&
106115
cp important sub &&
107-
test_must_fail git merge sub &&
116+
cp important sub2 &&
117+
test_must_fail git merge sub 2>out &&
118+
test_cmp out expect &&
108119
test_path_is_missing .git/MERGE_HEAD &&
109-
test_cmp important sub
120+
test_cmp important sub &&
121+
test_cmp important sub2 &&
122+
rm -f sub sub2
110123
'
111124

112125
test_expect_failure SYMLINKS 'will not overwrite untracked symlink in leading path' '

t/t7609-merge-co-error-msgs.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ test_expect_success 'setup' '
2727

2828
cat >expect <<\EOF
2929
error: The following untracked working tree files would be overwritten by merge:
30-
two
31-
three
32-
four
3330
five
31+
four
32+
three
33+
two
3434
Please move or remove them before you can merge.
3535
EOF
3636

@@ -49,9 +49,9 @@ test_expect_success 'untracked files overwritten by merge (fast and non-fast for
4949

5050
cat >expect <<\EOF
5151
error: Your local changes to the following files would be overwritten by merge:
52-
two
53-
three
5452
four
53+
three
54+
two
5555
Please, commit your changes or stash them before you can merge.
5656
error: The following untracked working tree files would be overwritten by merge:
5757
five
@@ -68,8 +68,8 @@ test_expect_success 'untracked files or local changes ovewritten by merge' '
6868

6969
cat >expect <<\EOF
7070
error: Your local changes to the following files would be overwritten by checkout:
71-
rep/two
7271
rep/one
72+
rep/two
7373
Please, commit your changes or stash them before you can switch branches.
7474
EOF
7575

@@ -89,8 +89,8 @@ test_expect_success 'cannot switch branches because of local changes' '
8989

9090
cat >expect <<\EOF
9191
error: Your local changes to the following files would be overwritten by checkout:
92-
rep/two
9392
rep/one
93+
rep/two
9494
Please, commit your changes or stash them before you can switch branches.
9595
EOF
9696

@@ -102,8 +102,8 @@ test_expect_success 'not uptodate file porcelain checkout error' '
102102

103103
cat >expect <<\EOF
104104
error: Updating the following directories would lose untracked files in it:
105-
rep2
106105
rep
106+
rep2
107107
108108
EOF
109109

unpack-trees.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
5353
void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
5454
const char *cmd)
5555
{
56+
int i;
5657
const char **msgs = opts->msgs;
5758
const char *msg;
5859
char *tmp;
@@ -96,6 +97,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
9697
"The following Working tree files would be removed by sparse checkout update:\n%s";
9798

9899
opts->show_all_errors = 1;
100+
/* rejected paths may not have a static buffer */
101+
for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
102+
opts->unpack_rejects[i].strdup_strings = 1;
99103
}
100104

101105
static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
@@ -124,53 +128,35 @@ static int add_rejected_path(struct unpack_trees_options *o,
124128
enum unpack_trees_error_types e,
125129
const char *path)
126130
{
127-
struct rejected_paths_list *newentry;
128131
if (!o->show_all_errors)
129132
return error(ERRORMSG(o, e), path);
130133

131134
/*
132135
* Otherwise, insert in a list for future display by
133136
* display_error_msgs()
134137
*/
135-
newentry = xmalloc(sizeof(struct rejected_paths_list));
136-
newentry->path = (char *)path;
137-
newentry->next = o->unpack_rejects[e];
138-
o->unpack_rejects[e] = newentry;
138+
string_list_append(&o->unpack_rejects[e], path);
139139
return -1;
140140
}
141141

142-
/*
143-
* free all the structures allocated for the error <e>
144-
*/
145-
static void free_rejected_paths(struct unpack_trees_options *o,
146-
enum unpack_trees_error_types e)
147-
{
148-
while (o->unpack_rejects[e]) {
149-
struct rejected_paths_list *del = o->unpack_rejects[e];
150-
o->unpack_rejects[e] = o->unpack_rejects[e]->next;
151-
free(del);
152-
}
153-
free(o->unpack_rejects[e]);
154-
}
155-
156142
/*
157143
* display all the error messages stored in a nice way
158144
*/
159145
static void display_error_msgs(struct unpack_trees_options *o)
160146
{
161-
int e;
147+
int e, i;
162148
int something_displayed = 0;
163149
for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
164-
if (o->unpack_rejects[e]) {
165-
struct rejected_paths_list *rp;
150+
struct string_list *rejects = &o->unpack_rejects[e];
151+
if (rejects->nr > 0) {
166152
struct strbuf path = STRBUF_INIT;
167153
something_displayed = 1;
168-
for (rp = o->unpack_rejects[e]; rp; rp = rp->next)
169-
strbuf_addf(&path, "\t%s\n", rp->path);
154+
for (i = 0; i < rejects->nr; i++)
155+
strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
170156
error(ERRORMSG(o, e), path.buf);
171157
strbuf_release(&path);
172-
free_rejected_paths(o, e);
173158
}
159+
string_list_clear(rejects, 0);
174160
}
175161
if (something_displayed)
176162
printf("Aborting\n");

unpack-trees.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef UNPACK_TREES_H
22
#define UNPACK_TREES_H
33

4+
#include "string-list.h"
5+
46
#define MAX_UNPACK_TREES 8
57

68
struct unpack_trees_options;
@@ -29,11 +31,6 @@ enum unpack_trees_error_types {
2931
void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
3032
const char *cmd);
3133

32-
struct rejected_paths_list {
33-
char *path;
34-
struct rejected_paths_list *next;
35-
};
36-
3734
struct unpack_trees_options {
3835
unsigned int reset,
3936
merge,
@@ -59,7 +56,7 @@ struct unpack_trees_options {
5956
* Store error messages in an array, each case
6057
* corresponding to a error message type
6158
*/
62-
struct rejected_paths_list *unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
59+
struct string_list unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
6360

6461
int head_idx;
6562
int merge_size;

0 commit comments

Comments
 (0)