8000 Added support for remote repositories (with and without authentication) by GeertvanHorrik · Pull Request #101 · GitTools/GitVersion · GitHub
[go: up one dir, main page]

Skip to content

Added support for remote repositories (with and without authentication) #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 20, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Parameters are now being passed around to ensure that the username / …
…password are first read from the environment variables, but can be overridden by command line arguments
  • Loading branch information
GeertvanHorrik committed Mar 18, 2014
commit f3bde9cdd22fae39173df24a1ecb69fff4e8efe1
10 changes: 9 additions & 1 deletion GitVersion/Arguments.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
namespace GitVersion
{
class Arguments
using System;

public class Arguments
{
public Arguments()
{
Username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
Password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
}

public string TargetPath;

public string TargetUrl;
Expand Down
23 changes: 14 additions & 9 deletions GitVersion/BuildServers/BuildServerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@

public static class BuildServerList
{
public static List<IBuildServer> BuildServers = new List<IBuildServer>
{
new ContinuaCi(),
new TeamCity()
};
static List<IBuildServer> BuildServers;

public static Func<IEnumerable<IBuildServer>> Selector = () => DefaultSelector();
public static Func<Arguments, IEnumerable<IBuildServer>> Selector = arguments => DefaultSelector(arguments);

public static void ResetSelector()
{
Selector = DefaultSelector;
}

public static IEnumerable<IBuildServer> GetApplicableBuildServers()
public static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
{
return Selector();
return Selector(arguments);
}

static IEnumerable<IBuildServer> DefaultSelector()
static IEnumerable<IBuildServer> DefaultSelector(Arguments arguments)
{
if (BuildServers == null)
{
BuildServers = new List<IBuildServer>
{
new ContinuaCi(arguments),
new TeamCity(arguments)
};
}

foreach (var buildServer in BuildServers)
{
if (buildServer.CanApplyToCurrentContext())
Expand Down
9 changes: 8 additions & 1 deletion GitVersion/BuildServers/ContinuaCi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

public class ContinuaCi : IBuildServer
{
readonly Arguments _arguments;

public ContinuaCi(Arguments arguments)
{
_arguments = arguments;
}

public bool CanApplyToCurrentContext()
{
using (var registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\VSoft Technologies\\Continua CI Agent"))
Expand All @@ -19,7 +26,7 @@ public void PerformPreProcessingSteps(string gitDirectory)
throw new ErrorException("Failed to find .git directory on agent");
}

GitHelper.NormalizeGitDirectory(gitDirectory);
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
}

public string[] GenerateSetParameterMessage(string name, string value)
Expand Down
17 changes: 8 additions & 9 deletions GitVersion/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace GitVersion
{
using System;
using System.Linq;
using LibGit2Sharp;

public static class GitHelper
{
public static void NormalizeGitDirectory(string gitDirectory)
public static void NormalizeGitDirectory(string gitDirectory, Arguments arguments)
{
using (var repo = new Repository(gitDirectory))
{
Expand All @@ -15,7 +14,7 @@ public static void NormalizeGitDirectory(string gitDirectory)
Logger.WriteInfo(string.Format("Fetching from remote '{0}' using the following refspecs: {1}.",
remote.Name, string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))));

var fetchOptions = BuildFetchOptions();
var fetchOptions = BuildFetchOptions(arguments.Username, arguments.Password);
repo.Network.Fetch(remote, fetchOptions);

CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
Expand All @@ -32,17 +31,17 @@ public static void NormalizeGitDirectory(string gitDirectory)
}
}

static FetchOptions BuildFetchOptions()
static FetchOptions BuildFetchOptions(string username, string password)
{
// TODO: Respect username/password of arguments?
var username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
var password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");

var fetchOptions = new FetchOptions();

if (!string.IsNullOrEmpty(username))
{
fetchOptions.Credentials = new Credentials { Username = username, Password = password };
fetchOptions.Credentials = new Credentials
{
Username = username,
Password = password
};
}

return fetchOptions;
Expand Down
10 changes: 9 additions & 1 deletion GitVersion/BuildServers/TeamCity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

public class TeamCity : IBuildServer
{
readonly Arguments _arguments;

public TeamCity(Arguments arguments)
{
_arguments = arguments;
}


public bool CanApplyToCurrentContext()
{
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
Expand All @@ -16,7 +24,7 @@ public void PerformPreProcessingSteps(string gitDirectory)
throw new ErrorException("Failed to find .git directory on agent. Please make sure agent checkout mode is enabled for you VCS roots - http://confluence.jetbrains.com/display/TCD8/VCS+Checkout+Mode");
}

GitHelper.NormalizeGitDirectory(gitDirectory);
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
}

public string[] GenerateSetParameterMessage(string name, string value)
Expand Down
46 changes: 17 additions & 29 deletions GitVersion/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,13 @@

public class GitPreparer
{
public GitPreparer(string targetPath, string url, string branchName, string username, string password)
readonly Arguments _arguments;

public GitPreparer(Arguments arguments)
{
TargetPath = targetPath;
Url = url;
BranchName = branchName;
Username = username;
Password = password;
_arguments = arguments;
}

public string TargetPath { get; private set; }

public string Url { get; private set; }

public string BranchName { get; private set; }

public string Username { get; private set; }

public string Password { get; private set; }

public bool IsDynamicGitRepository
{
get { return !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath); }
Expand All @@ -33,9 +21,9 @@ public bool IsDynamicGitRepository

public string Prepare()
{
var gitPath = TargetPath;
var gitPath = _arguments.TargetPath;

if (!string.IsNullOrWhiteSpace(Url))
if (!string.IsNullOrWhiteSpace(_arguments.TargetUrl))
{
gitPath = GetGitInfoFromUrl();
}
Expand All @@ -45,7 +33,7 @@ public string Prepare()

private string GetGitInfoFromUrl()
{
var gitDirectory = Path.Combine(TargetPath, "_dynamicrepository", ".git");
var gitDirectory = Path.Combine(_arguments.TargetPath, "_dynamicrepository", ".git");
if (Directory.Exists(gitDirectory))
{
Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory));
Expand All @@ -54,32 +42,32 @@ private string GetGitInfoFromUrl()
}

Credentials credentials = null;
if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password))
if (!string.IsNullOrWhiteSpace(_arguments.Username) && !string.IsNullOrWhiteSpace(_arguments.Password))
{
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", Username));
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", _arguments.Username));

credentials = new Credentials()
{
Username = Username,
Password = Password
Username = _arguments.Username,
Password = _arguments.Password
};
}

Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", Url));
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", _arguments.TargetUrl));

Repository.Clone(Url, gitDirectory, checkout: false, credentials: credentials);
Repository.Clone(_arguments.TargetUrl, gitDirectory, checkout: false, credentials: credentials);

if (!string.IsNullOrWhiteSpace(BranchName))
if (!string.IsNullOrWhiteSpace(_arguments.TargetBranch))
{
// Normalize (download branches) before using the branch
GitHelper.NormalizeGitDirectory(gitDirectory);
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);

using (var repository = new Repository(gitDirectory))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just check the branch out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have an easier way, yes. But I couldn't find it (clone doesn't support default branch to check out). I don't want to download all the files though (since this might be a lot of data).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the repository is already fully downloaded, I think that something like this should work (Warning: untested code)

var branchName = "..."
var b = repo.FindBranch(branchName);
repo.Checkout(b, CheckoutModifiers.Force);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code works, but is much slower. Just let me know what you prefer, I don't mind what is being used in the end.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code works, but is much slower

Hmmm. You're right. We don't actually need to check the files out.

How about

var branchName = "..."
var b = repo.FindBranch(branchName);
Debug.Assert(b != null);
repo.Refs.UpdateTarget("HEAD", b.CanonicalName);
Logger.WriteInfo(string.Format("Moving HEAD to branch '{0}'", b.CanonicalName));

That should work 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed (with other options commented out). This works great and is slightly simpler than what we had before.

{
var targetBranchName = string.Format("refs/heads/{0}", BranchName);
var targetBranchName = string.Format("refs/heads/{0}", _arguments.TargetBranch);
if (!string.Equals(repository.Head.CanonicalName, targetBranchName))
{
Logger.WriteInfo(string.Format("Switching to branch '{0}'", BranchName));
Logger.WriteInfo(string.Format("Switching to branch '{0}'", _arguments.TargetBranch));

repository.Refs.UpdateTarget("HEAD", targetBranchName);
}
Expand Down
9 changes: 4 additions & 5 deletions GitVersion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ static void Main()

ConfigureLogging(arguments);

var gitPreparer = new GitPreparer(arguments.TargetPath, arguments.TargetUrl,
arguments.TargetBranch, arguments.Username, arguments.Password);
var gitPreparer = new GitPreparer(arguments);
var gitDirectory = gitPreparer.Prepare();
if (string.IsNullOrEmpty(gitDirectory))
{
Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath);
Environment.Exit(1);
}

var applicableBuildServers = GetApplicableBuildServers().ToList();
var applicableBuildServers = GetApplicableBuildServers(arguments).ToList();

foreach (var buildServer in applicableBuildServers)
{
Expand Down Expand Up @@ -91,9 +90,9 @@ static void Main()
Environment.Exit(exitCode.Value);
}

static IEnumerable<IBuildServer> GetApplicableBuildServers()
static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
{
return BuildServerList.BuildServers.Where(buildServer => buildServer.CanApplyToCurrentContext());
return BuildServerList.GetApplicableBuildServers(arguments);
}

static void ConfigureLogging(Arguments arguments)
Expand Down
3 changes: 2 additions & 1 deletion GitVersionTask/VersionAndBranchFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static bool TryGetVersion(string directory, out VersionAndBranchAndDate v
return false;
}

foreach (var buildServer in BuildServerList.GetApplicableBuildServers())
var arguments = new Arguments();
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(arguments))
{
Logger.WriteInfo(string.Format("Executing PerformPreProcessingSteps for '{0}'.", buildServer.GetType().Name));
buildServer.PerformPreProcessingSteps(gitDirectory);
Expand Down
3 changes: 2 additions & 1 deletion GitVersionTask/WriteVersionInfoToBuildLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void InnerExecute()
return;
}

WriteIntegrationParameters(versionAndBranch, BuildServerList.GetApplicableBuildServers());
var arguments = new Arguments();
WriteIntegrationParameters(versionAndBranch, BuildServerList.GetApplicableBuildServers(arguments));
}

public void WriteIntegrationParameters(VersionAndBranch versionAndBranch, IEnumerable<IBuildServer> applicableBuildServers)
Expand Down
3 changes: 2 additions & 1 deletion Tests/BuildServers/ContinuaCiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class ContinuaCiTests
[Test]
public void GenerateBuildVersion()
{
var versionBuilder = new ContinuaCi();
var arguments = new Arguments();
var versionBuilder = new ContinuaCi(arguments);
var continuaCiVersion = versionBuilder.GenerateSetVersionMessage("0.0.0-Beta4.7");
Assert.AreEqual("@@continua[setBuildVersion value='0.0.0-Beta4.7']", continuaCiVersion);
}
Expand Down
6 changes: 4 additions & 2 deletions Tests/BuildServers/TeamCityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ public class TeamCityTests
[Test]
public void Develop_branch()
{
var versionBuilder = new TeamCity();
var arguments = new Arguments();
var versionBuilder = new TeamCity(arguments);
var tcVersion = versionBuilder.GenerateSetVersionMessage("0.0.0-Unstable4");
Assert.AreEqual("##teamcity[buildNumber '0.0.0-Unstable4']", tcVersion);
}

[Test]
public void EscapeValues()
{
var versionBuilder = new TeamCity();
var arguments = new Arguments();
var versionBuilder = new TeamCity(arguments);
var tcVersion = versionBuilder.GenerateSetParameterMessage("Foo", "0.8.0-unstable568 Branch:'develop' Sha:'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb'");
Assert.AreEqual("##teamcity[setParameter name='GitFlowVersion.Foo' value='0.8.0-unstable568 Branch:|'develop|' Sha:|'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb|'']", tcVersion[0]);
Assert.AreEqual("##teamcity[setParameter name='GitVersion.Foo' value='0.8.0-unstable568 Branch:|'develop|' Sha:|'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb|'']", tcVersion[1]);
Expand Down
12 changes: 8 additions & 4 deletions Tests/GitHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public void CanDetermineTheVersionFromAFetchedMaster()
{
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/master");

GitHelper.NormalizeGitDirectory(gitDirectory);
var arguments = new Arguments();
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);

using (var repository = new Repository(gitDirectory))
{
Expand All @@ -29,7 +30,8 @@ public void CanDetermineTheVersionFromAPullRequest()

var gitDirectory = FakeTeamCityFetchAndCheckout(repoPath, "refs/pull/1735/merge");

GitHelper.NormalizeGitDirectory(gitDirectory);
var arguments = new Arguments();
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);

using (var repository = new Repository(gitDirectory))
{
Expand All @@ -44,7 +46,8 @@ public void CanDetermineTheVersionFromAFetchedDevelop()
{
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/develop");

GitHelper.NormalizeGitDirectory(gitDirectory);
var arguments = new Arguments();
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);

using (var repository = new Repository(gitDirectory))
{
Expand All @@ -57,7 +60,8 @@ public void CanDetermineTheVersionFromAFetchedFeature()
{
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/feature/one");

GitHelper.NormalizeGitDirectory(gitDirectory);
var arguments = new Arguments();
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);

using (var repository = new Repository(gitDirectory))
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/UpdateAssemblyInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void StandardExecutionMode_CannotDetermineTheVersionFromADetachedHead()
public void SetUp()
{
//avoid buildserver detection to make the tests pass on the buildserver
BuildServerList.Selector = () => new List<IBuildServer>();
BuildServerList.Selector = arguments => new List<IBuildServer>();
}


Expand Down
0