Skip to content

Commit c141058

Browse files
KarthikNayakgitster
authored andcommitted
refs/files: handle F/D conflicts in case-insensitive FS
Similar to the previous commit, 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, 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 a lower case 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 <gitster@pobox.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e48514f commit c141058

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

refs/files-backend.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,23 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
869869
* If the ref did not exist and we are creating it, we have to
870870
* make sure there is no existing packed ref that conflicts
871871
* with refname. This check is deferred so that we can batch it.
872+
*
873+
* For case-insensitive filesystems, we should also check for F/D
874+
* conflicts between 'foo' and 'Foo/bar'. So let's lowercase
875+
* the refname.
872876
*/
873-
item = string_list_append(refnames_to_check, refname);
877+
if (ignore_case) {
878+
struct strbuf lower = STRBUF_INIT;
879+
880+
strbuf_addstr(&lower, refname);
881+
strbuf_tolower(&lower);
882+
883+
item = string_list_append(refnames_to_check, lower.buf);
884+
strbuf_release(&lower);
885+
} else {
886+
item = string_list_append(refnames_to_check, refname);
887+
}
888+
874889
item->util = xmalloc(sizeof(update_idx));
875890
memcpy(item->util, &update_idx, sizeof(update_idx));
876891
}
@@ -2796,7 +2811,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
27962811
"ref_transaction_prepare");
27972812
size_t i;
27982813
int ret = 0;
2799-
struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
2814+
struct string_list refnames_to_check = STRING_LIST_INIT_DUP;
28002815
char *head_ref = NULL;
28012816
int head_type;
28022817
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

@@ -1546,6 +1552,20 @@ test_expect_success CASE_INSENSITIVE_FS,REFFILES 'existing references in a case
15461552
)
15471553
'
15481554

1555+
test_expect_success CASE_INSENSITIVE_FS,REFFILES 'F/D conflict on case insensitive filesystem' '
1556+
test_when_finished rm -rf case_insensitive &&
1557+
(
1558+
git init --bare case_insensitive &&
1559+
cd case_insensitive &&
1560+
git remote add origin -- ../case_sensitive_fd &&
1561+
test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1562+
test_grep "failed: refname conflict" err &&
1563+
git rev-parse refs/heads/main >expect &&
1564+
git rev-parse refs/heads/foo/bar >actual &&
1565+
test_cmp expect actual
1566+
)
1567+
'
1568+
15491569
. "$TEST_DIRECTORY"/lib-httpd.sh
15501570
start_httpd
15511571

0 commit comments

Comments
 (0)