Skip to content

Commit 08402b0

Browse files
moygitster
authored andcommitted
merge-recursive: distinguish "removed" and "overwritten" messages
To limit the number of possible error messages, the error messages for the case would_lose_untracked_file and would_lose_orphaned in unpack_trees_options.msgs were handled with a single string, parameterized by an action string ("overwritten" or "removed"). Instead, we consider them as two different cases, with unparameterized string. This will make it easier to make separate lists sorted by error types later. Only the bind_overlap case still takes two %s parameters, but that's unavoidable. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23cbf11 commit 08402b0

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

merge-recursive.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,13 +1197,16 @@ void set_porcelain_error_msgs(const char **msgs, const char *cmd)
11971197
"Updating '%s' would lose untracked files in it. Aborting.";
11981198

11991199
if (advice_commit_before_merge)
1200-
msg = "Untracked working tree file '%%s' would be %%s by %s. Aborting"
1200+
msg = "Untracked working tree file '%%s' would be %s by %s. Aborting"
12011201
"Please move or remove them before you can %s.";
12021202
else
1203-
msg = "Untracked working tree file '%%s' would be %%s by %s. Aborting";
1204-
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 3);
1205-
sprintf(tmp, msg, cmd, cmd2);
1206-
msgs[ERROR_WOULD_LOSE_UNTRACKED] = tmp;
1203+
msg = "Untracked working tree file '%%s' would be %s by %s. Aborting";
1204+
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("removed") + strlen(cmd2) - 4);
1205+
sprintf(tmp, msg, "removed", cmd, cmd2);
1206+
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = tmp;
1207+
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("overwritten") + strlen(cmd2) - 4);
1208+
sprintf(tmp, msg, "overwritten", cmd, cmd2);
1209+
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = tmp;
12071210
}
12081211

12091212
int merge_trees(struct merge_options *o,

unpack-trees.c

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
2626
/* ERROR_NOT_UPTODATE_DIR */
2727
"Updating '%s' would lose untracked files in it",
2828

29-
/* ERROR_WOULD_LOSE_UNTRACKED */
30-
"Untracked working tree file '%s' would be %s by merge.",
29+
/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
30+
"Untracked working tree file '%s' would be overwritten by merge.",
31+
32+
/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
33+
"Untracked working tree file '%s' would be removed by merge.",
3134

3235
/* ERROR_BIND_OVERLAP */
3336
"Entry '%s' overlaps with '%s'. Cannot bind.",
3437

3538
/* ERROR_SPARSE_NOT_UPTODATE_FILE */
3639
"Entry '%s' not uptodate. Cannot update sparse checkout.",
3740

38-
/* ERROR_WOULD_LOSE_ORPHANED */
39-
"Working tree file '%s' would be %s by sparse checkout update.",
41+
/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */
42+
"Working tree file '%s' would be overwritten by sparse checkout update.",
43+
44+
/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */
45+
"Working tree file '%s' would be removed by sparse checkout update.",
4046
};
4147

4248
#define ERRORMSG(o,type) \
@@ -131,7 +137,7 @@ static int check_updates(struct unpack_trees_options *o)
131137
}
132138

133139
static int verify_uptodate_sparse(struct cache_entry *ce, struct unpack_trees_options *o);
134-
static int verify_absent_sparse(struct cache_entry *ce, const char *action, struct unpack_trees_options *o);
140+
static int verify_absent_sparse(struct cache_entry *ce, enum unpack_trees_error_types, struct unpack_trees_options *o);
135141

136142
static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_trees_options *o)
137143
{
@@ -174,7 +180,7 @@ static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_opt
174180
ce->ce_flags |= CE_WT_REMOVE;
175181
}
176182
if (was_skip_worktree && !ce_skip_worktree(ce)) {
177-
if (verify_absent_sparse(ce, "overwritten", o))
183+
if (verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
178184
return -1;
179185
ce->ce_flags |= CE_UPDATE;
180186
}
@@ -859,7 +865,7 @@ static int same(struct cache_entry *a, struct cache_entry *b)
859865
*/
860866
static int verify_uptodate_1(struct cache_entry *ce,
861867
struct unpack_trees_options *o,
862-
const char *error_msg)
868+
enum unpack_trees_error_types error_type)
863869
{
864870
struct stat st;
865871

@@ -884,21 +890,21 @@ static int verify_uptodate_1(struct cache_entry *ce,
884890
if (errno == ENOENT)
885891
return 0;
886892
return o->gently ? -1 :
887-
error(error_msg, ce->name);
893+
error(ERRORMSG(o, error_type), ce->name);
888894
}
889895

890896
static int verify_uptodate(struct cache_entry *ce,
891897
struct unpack_trees_options *o)
892898
{
893899
if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
894900
return 0;
895-
return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_NOT_UPTODATE_FILE));
901+
return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
896902
}
897903

898904
static int verify_uptodate_sparse(struct cache_entry *ce,
899905
struct unpack_trees_options *o)
900906
{
901-
return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_SPARSE_NOT_UPTODATE_FILE));
907+
return verify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);
902908
}
903909

904910
static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
@@ -914,13 +920,15 @@ static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_optio
914920
* Currently, git does not checkout subprojects during a superproject
915921
* checkout, so it is not going to overwrite anything.
916922
*/
917-
static int verify_clean_submodule(struct cache_entry *ce, const char *action,
923+
static int verify_clean_submodule(struct cache_entry *ce,
924+
enum unpack_trees_error_types error_type,
918925
struct unpack_trees_options *o)
919926
{
920927
return 0;
921928
}
922929

923-
static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
930+
static int verify_clean_subdirectory(struct cache_entry *ce,
931+
enum unpack_trees_error_types error_type,
924932
struct unpack_trees_options *o)
925933
{
926934
/*
@@ -941,7 +949,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
941949
*/
942950
if (!hashcmp(sha1, ce->sha1))
943951
return 0;
944-
return verify_clean_submodule(ce, action, o);
952+
return verify_clean_submodule(ce, error_type, o);
945953
}
946954

947955
/*
@@ -1010,9 +1018,9 @@ static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst,
10101018
* We do not want to remove or overwrite a working tree file that
10111019
* is not tracked, unless it is ignored.
10121020
*/
1013-
static int verify_absent_1(struct cache_entry *ce, const char *action,
1014-
struct unpack_trees_options *o,
1015-
const char *error_msg)
1021+
static int verify_absent_1(struct cache_entry *ce,
1022+
enum unpack_trees_error_types error_type,
1023+
struct unpack_trees_options *o)
10161024
{
10171025
struct stat st;
10181026

@@ -1050,7 +1058,7 @@ static int verify_absent_1(struct cache_entry *ce, const char *action,
10501058
* files that are in "foo/" we would lose
10511059
* them.
10521060
*/
1053-
if (verify_clean_subdirectory(ce, action, o) < 0)
1061+
if (verify_clean_subdirectory(ce, error_type, o) < 0)
10541062
return -1;
10551063
return 0;
10561064
}
@@ -1067,22 +1075,28 @@ static int verify_absent_1(struct cache_entry *ce, const char *action,
10671075
}
10681076

10691077
return o->gently ? -1 :
1070-
error(ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED), ce->name, action);
1078+
error(ERRORMSG(o, error_type), ce->name);
10711079
}
10721080
return 0;
10731081
}
1074-
static int verify_absent(struct cache_entry *ce, const char *action,
1082+
static int verify_absent(struct cache_entry *ce,
1083+
enum unpack_trees_error_types error_type,
10751084
struct unpack_trees_options *o)
10761085
{
10771086
if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
10781087
return 0;
1079-
return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED));
1088+
return verify_absent_1(ce, error_type, o);
10801089
}
10811090

1082-
static int verify_absent_sparse(struct cache_entry *ce, const char *action,
1091+
static int verify_absent_sparse(struct cache_entry *ce,
1092+
enum unpack_trees_error_types error_type,
10831093
struct unpack_trees_options *o)
10841094
{
1085-
return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_ORPHANED));
1095+
enum unpack_trees_error_types orphaned_error = error_type;
1096+
if (orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)
1097+
orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;
1098+
1099+
return verify_absent_1(ce, orphaned_error, o);
10861100
}
10871101

10881102
static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
@@ -1091,7 +1105,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
10911105
int update = CE_UPDATE;
10921106

10931107
if (!old) {
1094-
if (verify_absent(merge, "overwritten", o))
1108+
if (verify_absent(merge, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
10951109
return -1;
10961110
invalidate_ce_path(merge, o);
10971111
} else if (!(old->ce_flags & CE_CONFLICTED)) {
@@ -1129,7 +1143,7 @@ static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
11291143
{
11301144
/* Did it exist in the index? */
11311145
if (!old) {
1132-
if (verify_absent(ce, "removed", o))
1146+
if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
11331147
return -1;
11341148
return 0;
11351149
}
@@ -1278,7 +1292,7 @@ int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
12781292
if (index)
12791293
return deleted_entry(index, index, o);
12801294
if (ce && !head_deleted) {
1281-
if (verify_absent(ce, "removed", o))
1295+
if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
12821296
return -1;
12831297
}
12841298
return 0;

unpack-trees.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ enum unpack_trees_error_types {
1313
ERROR_WOULD_OVERWRITE = 0,
1414
ERROR_NOT_UPTODATE_FILE,
1515
ERROR_NOT_UPTODATE_DIR,
16-
ERROR_WOULD_LOSE_UNTRACKED,
16+
ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN,
17+
ERROR_WOULD_LOSE_UNTRACKED_REMOVED,
1718
ERROR_BIND_OVERLAP,
1819
ERROR_SPARSE_NOT_UPTODATE_FILE,
19-
ERROR_WOULD_LOSE_ORPHANED,
20+
ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN,
21+
ERROR_WOULD_LOSE_ORPHANED_REMOVED,
2022
NB_UNPACK_TREES_ERROR_TYPES
2123
};
2224

0 commit comments

Comments
 (0)