-
Notifications
You must be signed in to change notification settings - Fork 655
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
Changes from 5 commits
173518b
81fb3ec
314cf17
d56fde9
20c935d
981c4f6
f3bde9c
2ff757f
2ed9693
623f617
292270b
2052f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
namespace GitVersion | ||
{ | ||
using System.IO; | ||
using LibGit2Sharp; | ||
|
||
public class GitPreparer | ||
{ | ||
public GitPreparer(string targetPath, string url, string branchName, string username, string password) | ||
{ | ||
TargetPath = targetPath; | ||
Url = url; | ||
BranchName = branchName; | ||
Username = username; | ||
Password = password; | ||
} | ||
|
||
public string TargetPath { get; private set; } | ||
|
||
public string Url { get; private set; } | ||
|
||
public string BranchName { get; private set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this property leveraged by the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, you are right. Let me check... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, good catch! |
||
|
||
public string Username { get; private set; } | ||
|
||
public string Password { get; private set; } | ||
|
||
public bool IsDynamicGitRepository | ||
{ | ||
get { return !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath); } | ||
} | ||
|
||
public string DynamicGitRepositoryPath { get; private set; } | ||
|
||
public string Prepare() | ||
{ | ||
var gitPath = TargetPath; | ||
|
||
if (!string.IsNullOrWhiteSpace(Url)) | ||
{ | ||
gitPath = GetGitInfoFromUrl(); | ||
} | ||
|
||
return GitDirFinder.TreeWalkForGitDir(gitPath); | ||
} | ||
|
||
private string GetGitInfoFromUrl() | ||
{ | ||
var gitDirectory = Path.Combine(TargetPath, "_dynamicrepository", ".git"); | ||
if (Directory.Exists(gitDirectory)) | ||
{ | ||
Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory)); | ||
|
||
DeleteHelper.DeleteGitRepository(gitDirectory); | ||
} | ||
|
||
Credentials credentials = null; | ||
if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password)) | ||
{ | ||
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", Username)); | ||
|
||
credentials = new Credentials() | ||
{ | ||
Username = Username, | ||
Password = Password | ||
}; | ||
} | ||
|
||
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", Url)); | ||
|
||
Repository.Clone(Url, gitDirectory, checkout: false, credentials: credentials); | ||
|
||
if (!string.IsNullOrWhiteSpace(BranchName)) | ||
{ | ||
// Normalize (download branches) before using the branch | ||
GitHelper.NormalizeGitDirectory(gitDirectory); | ||
|
||
using (var repository = new Repository(gitDirectory)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just check the branch out? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
if (!string.Equals(repository.Head.CanonicalName, targetBranchName)) | ||
{ | ||
Logger.WriteInfo(string.Format("Switching to branch '{0}'", BranchName)); | ||
|
||
var branch = repository.FindBranch(BranchName); | ||
if ((branch != null) && !branch.IsCurrentRepositoryHead) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line (and the one above) can be safely dropped There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
{ | ||
// Option 1: checkout (slow) | ||
//repository.Checkout(branch, CheckoutModifiers.Force, null, null); | ||
|
||
// Option 2: add head refs | ||
//var finalName = string.Format("refs/heads/{0}", BranchName); | ||
//repository.Refs.Add("HEAD", finalName, true); | ||
|
||
// Option 3: replace head | ||
repository.Refs.UpdateTarget("HEAD", branch.CanonicalName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...and |
||
} | ||
} | ||
} | ||
} | ||
|
||
DynamicGitRepositoryPath = gitDirectory; | ||
|
||
return gitDirectory; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,12 @@ public static void Write() | |
|
||
GitVersion [path] [/l logFilePath] | ||
|
||
path The directory containing .git. If not defined current directory is used. | ||
/l Path to logfile. | ||
path The directory containing .git. If not defined current directory is used. | ||
/url Url to remote git repository. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: tabs vs spaces identations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
/b Name of the branch to use on the remote repository, must be used in combination with /url. | ||
/u Username in case authentication is required. | ||
/p Password in case authentication is required. | ||
/l Path to logfile. | ||
"; | ||
Console.Write(message); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace GitVersion | ||
{ | ||
using System.IO; | ||
|
||
public static class DeleteHelper | ||
{ | ||
public static void DeleteGitRepository(string directory) | ||
{ | ||
if (string.IsNullOrEmpty(directory)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var fileName in Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories)) | ||
{ | ||
var fileInfo = new FileInfo(fileName) | ||
{ | ||
IsReadOnly = false | ||
}; | ||
|
||
fileInfo.Delete(); | ||
} | ||
|
||
Directory.Delete(directory, true); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
@andreasohlund What's your take about this? How should we behave when both arguments and environment variables are valued? Which one should we consider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say commandline first, then fallback to env?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Commandline should override general settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GeertvanHorrik Makes sense. Could you please update the PR to cope with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on it. I will pass the arguments around everywhere then. Normally I would use an IoC container to inject the arguments automatically, but since this app is so simple, I will just pass it into the ctors manually.