repo.Network.Remotes.Remove does not remove the remote
We have this code below where we remove remote (origin) before adding a new one. From time to time, Add would fail with remote already exists. Is there an issue where remote is not removed when calling repo.Network.Remotes.Remove? Is it a race condition? We doubt it is since rerun the same code still yield remote already exists. Anything we do wrong or can improve? BTW, if we run git remote remove origin command, then it is removed - so repo is not corrupted.
repo.Network.Remotes.Remove(_remoteAlias);
// Configure the remote
remote = repo.Network.Remotes.Add(_remoteAlias, remoteUrl, refSpec);
I have the same issue. It does remove url and fetch fields from remote section but does not remove the section header.
[remote "origin"]
url = ssh://[email protected]/test/repo
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "32137c08-b0d0-45e4-88b9-a3b15fc72d95"]
[remote "aef18d06-5524-4fd3-9f2e-4d5849048a48"]
[remote "00d58c81-3bf2-4276-8695-7167a10b6caf"]
[remote "10044228-77c9-4355-bc3a-11e444c5a8f8"]
[remote "e87ecf5a-37c7-4619-9214-9bfcd9f9b9a3"]
Thanks for reporting this and @arutkowski00 for the helpful clarification. We'll take a look.
The section header should be irrelevant for the existence of the remote. A remote does not exist because the header exists. Not removing the header is just a cosmetic bug because we do not remove entire sections but individual configuration entries.
What is the state of the config file when this error occurs? Is there concurrent access that might stop us from modifying the file (assuming you're on Windows).
@carlosmn We always synchronize call to git operation (so no concurrent access). As for file may not be writable, we are sure it was since calling git remote remove origin successfully remove it. Unfortunately, we did not capture the state of the config file - will do once it repro-ed again.
I can still reproduce this with 0.31.0.
Repro steps
Step 1: Create a new repo with git init
Step 2: Add a remote: git add remote abc https://abc
Step 3: Run the following code:
using LibGit2Sharp;
var repo = new Repository(".");
LogRemotes("Initial");
repo.Network.Remotes.Add("test", "https://bogus");
LogRemotes("After adding");
repo.Network.Remotes.Remove("test");
LogRemotes("After removing");
void LogRemotes(string description)
{
Console.WriteLine(description);
foreach (var remote in repo.Network.Remotes)
{
Console.WriteLine($"{remote.Name}: {remote.Url}");
}
Console.WriteLine();
}
Step 4: cat .git/config
Repro output
Step 3 - this looks okay
Initial
abc: https://abc
After adding
abc: https://abc
test: https://bogus
After removing
abc: https://abc
Step 4 - this is broken:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "abc"]
url = https://abc
fetch = +refs/heads/*:refs/remotes/abc/*
[remote "test"]
The empty [remote "test"] at the end is causing problems elsewhere in my system.