Skip to content

Commit 98bd461

Browse files
committed
GH-1874, fix the nofetch behaviour, it was not correctly initialized
1 parent f3278ec commit 98bd461

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

src/GitVersionCore/Configuration/NamedConfigFileLocator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public class NamedConfigFileLocator : ConfigFileLocator
1010
{
1111
public NamedConfigFileLocator(IFileSystem fileSystem, ILog log, IOptions<Arguments> options) : base(fileSystem, log)
1212
{
13-
var arguments = options.Value;
14-
var filePath = arguments.ConfigFile;
13+
var filePath = options.Value.ConfigFile;
1514
if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath), "Empty file path provided!");
1615
FilePath = filePath;
1716
}

src/GitVersionCore/GitPreparer.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class GitPreparer : IGitPreparer
1313
private readonly ILog log;
1414
private readonly IEnvironment environment;
1515
private readonly string dynamicRepositoryLocation;
16-
private readonly bool noFetch;
1716
private readonly Arguments arguments;
1817

1918
private const string DefaultRemoteName = "origin";
@@ -29,7 +28,6 @@ public GitPreparer(ILog log, IEnvironment environment, IOptions<Arguments> optio
2928
WorkingDirectory = arguments.TargetPath.TrimEnd('/', '\\');
3029

3130
dynamicRepositoryLocation = arguments.DynamicRepositoryLocation;
32-
noFetch = arguments.NoFetch;
3331
}
3432

3533
public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
@@ -39,23 +37,24 @@ public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shoul
3937
Username = arguments.Authentication?.Username,
4038
Password = arguments.Authentication?.Password
4139
};
42-
if (string.IsNullOrWhiteSpace(TargetUrl))
40+
if (!string.IsNullOrWhiteSpace(TargetUrl))
4341
{
44-
if (!normalizeGitDirectory) return;
45-
using (log.IndentLog($"Normalizing git directory for branch '{currentBranch}'"))
42+
var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation);
43+
44+
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch);
45+
}
46+
else
47+
{
48+
if (normalizeGitDirectory)
4649
{
4750
if (shouldCleanUpRemotes)
4851
{
4952
CleanupDuplicateOrigin();
5053
}
51-
GitRepositoryHelper.NormalizeGitDirectory(log, environment, GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository());
54+
55+
NormalizeGitDirectory(authentication, currentBranch, GetDotGitDirectory(), IsDynamicGitRepository());
5256
}
53-
return;
5457
}
55-
56-
var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation);
57-
58-
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch);
5958
}
6059

6160
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
@@ -108,7 +107,7 @@ private void CleanupDuplicateOrigin()
108107
{
109108
var remoteToKeep = DefaultRemoteName;
110109

111-
var repo = new Repository(GetDotGitDirectory());
110+
using var repo = new Repository(GetDotGitDirectory());
112111

113112
// check that we have a remote that matches defaultRemoteName if not take the first remote
114113
if (!repo.Network.Remotes.Any(remote => remote.Name.Equals(DefaultRemoteName, StringComparison.InvariantCultureIgnoreCase)))
@@ -176,29 +175,28 @@ private string CreateDynamicRepository(string targetPath, AuthenticationInfo aut
176175
using (log.IndentLog($"Creating dynamic repository at '{targetPath}'"))
177176
{
178177
var gitDirectory = Path.Combine(targetPath, ".git");
179-
if (Directory.Exists(targetPath))
178+
if (!Directory.Exists(targetPath))
180179
{
181-
log.Info("Git repository already exists");
182-
using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
183-
{
184-
GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, noFetch, targetBranch, true);
185-
}
186-
187-
return gitDirectory;
180+
CloneRepository(repositoryUrl, gitDirectory, auth);
188181
}
189-
190-
CloneRepository(repositoryUrl, gitDirectory, auth);
191-
192-
using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
182+
else
193183
{
194-
// Normalize (download branches) before using the branch
195-
GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, noFetch, targetBranch, true);
184+
log.Info("Git repository already exists");
196185
}
197-
186+
NormalizeGitDirectory(auth, targetBranch, gitDirectory, true);
198187
return gitDirectory;
199188
}
200189
}
201190

191+
private void NormalizeGitDirectory(AuthenticationInfo auth, string targetBranch, string gitDirectory, bool isDynamicRepository)
192+
{
193+
using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
194+
{
195+
// Normalize (download branches) before using the branch
196+
GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, arguments.NoFetch, targetBranch, isDynamicRepository);
197+
}
198+
}
199+
202200
private void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo auth)
203201
{
204202
Credentials credentials = null;

src/GitVersionCore/Helpers/GitRepositoryHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace GitVersion.Helpers
1010
public static class GitRepositoryHelper
1111
{
1212
/// <summary>
13-
/// Normalisation of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
14-
/// It is not recommended to run normalisation against a local repository
13+
/// Normalization of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
14+
/// It is not recommended to run normalization against a local repository
1515
/// </summary>
1616
public static void NormalizeGitDirectory(ILog log, IEnvironment environment, string gitDirectory, AuthenticationInfo authentication,
1717
bool noFetch, string currentBranch, bool isDynamicRepository)
@@ -265,7 +265,7 @@ private static IEnumerable<DirectReference> GetRemoteTipsForAnonymousUser(Reposi
265265
private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(ILog log, Repository repo, string remoteName)
266266
{
267267
var prefix = $"refs/remotes/{remoteName}/";
268-
var remoteHeadCanonicalName = $"{prefix}{"HEAD"}";
268+
var remoteHeadCanonicalName = $"{prefix}HEAD";
269269

270270
foreach (var remoteTrackingReference in repo.Refs.FromGlob(prefix + "*").Where(r => r.CanonicalName != remoteHeadCanonicalName))
271271
{
@@ -299,7 +299,7 @@ private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(ILog log,
299299
}
300300
}
301301

302-
public static Remote EnsureOnlyOneRemoteIsDefined(ILog log, IRepository repo)
302+
private static Remote EnsureOnlyOneRemoteIsDefined(ILog log, IRepository repo)
303303
{
304304
var remotes = repo.Network.Remotes;
305305
var howMany = remotes.Count();

0 commit comments

Comments
 (0)