Skip to content

Commit 770f389

Browse files
KarthikNayakgitster
authored andcommitted
refs/files: handle F/D conflicts in case-insensitive FS
When using the files-backend on case-insensitive filesystems, there is possibility of hitting F/D conflicts when creating references within a single transaction, such as: - 'refs/heads/foo' - 'refs/heads/Foo/bar' Ideally such conflicts are caught in `refs_verify_refnames_available()` which is responsible for checking F/D conflicts within a given transaction. This utility function is shared across the reference backends. As such, it doesn't consider the issues of using a case-insensitive file system, which only affects the files-backend. While one solution would be to make the function aware of such issues, this feels like leaking implementation details of file-backend specific issues into the utility function. So opt for the more simpler option, of lowercasing all references sent to this function when on a case-insensitive filesystem and operating on the files-backend. To do this, simply use a `struct strbuf` to convert the refname to lowercase and append it to the list of refnames to be checked. Since we use a `struct strbuf` and the memory is cleared right after, make sure that the string list duplicates all provided string. Without this change, the user would simply be left with a repository with '.lock' files which were created in the 'prepare' phase of the transaction, as the 'commit' phase would simply abort and not do the necessary cleanup. Reported-by: Junio C Hamano <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b62a67 commit 770f389

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

refs/files-backend.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,23 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
906906
* If the ref did not exist and we are creating it, we have to
907907
* make sure there is no existing packed ref that conflicts
908908
* with refname. This check is deferred so that we can batch it.
909+
*
910+
* For case-insensitive filesystems, we should also check for F/D
911+
* conflicts between 'foo' and 'Foo/bar'. So let's lowercase
912+
* the refname.
909913
*/
910-
item = string_list_append(refnames_to_check, refname);
914+
if (ignore_case) {
915+
struct strbuf lower = STRBUF_INIT;
916+
917+
strbuf_addstr(&lower, refname);
918+
strbuf_tolower(&lower);
919+
920+
item = string_list_append_nodup(refnames_to_check,
921+
strbuf_detach(&lower, NULL));
922+
} else {
923+
item = string_list_append(refnames_to_check, refname);
924+
}
925+
911926
item->util = xmalloc(sizeof(update_idx));
912927
memcpy(item->util, &update_idx, sizeof(update_idx));
913928
}
@@ -2832,7 +2847,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
28322847
"ref_transaction_prepare");
28332848
size_t i;
28342849
int ret = 0;
2835-
struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
2850+
struct string_list refnames_to_check = STRING_LIST_INIT_DUP;
28362851
char *head_ref = NULL;
28372852
int head_type;
28382853
struct files_transaction_backend_data *backend_data;

t/t5510-fetch.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ test_expect_success "clone and setup child repos" '
5353
cd case_sensitive &&
5454
git branch branch1 &&
5555
git branch bRanch1
56+
) &&
57+
git clone --ref-format=reftable . case_sensitive_fd &&
58+
(
59+
cd case_sensitive_fd &&
60+
git branch foo/bar &&
61+
git branch Foo
5662
)
5763
'
5864

@@ -1572,6 +1578,20 @@ test_expect_success REFFILES 'existing reference lock in repo' '
15721578
)
15731579
'
15741580

1581+
test_expect_success CASE_INSENSITIVE_FS,REFFILES 'F/D conflict on case insensitive filesystem' '
1582+
test_when_finished rm -rf case_insensitive &&
1583+
(
1584+
git init --bare case_insensitive &&
1585+
cd case_insensitive &&
1586+
git remote add origin -- ../case_sensitive_fd &&
1587+
test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1588+
test_grep "failed: refname conflict" err &&
1589+
git rev-parse refs/heads/main >expect &&
1590+
git rev-parse refs/heads/foo/bar >actual &&
1591+
test_cmp expect actual
1592+
)
1593+
'
1594+
15751595
. "$TEST_DIRECTORY"/lib-httpd.sh
15761596
start_httpd
15771597

0 commit comments

Comments
 (0)