这个想法是这样的。我们正在运行内部" GitLab"有一堆存储库。其中一些存储库被UX用户使用,他们希望与用于向客户展示其工作的网站同步。他们让我建立一个界面,用户可以进入他们的存储库的网址,我会将其同步到网站。
我创建了一个计划任务,该任务将读取网站上输入的存储库,然后使用" LibGit2Sharp"在本地获取文件。然后这些文件将使用FTP传输到网站。
设计完美吗?不,但我还在学习,这只是一个内部项目,然后才能登陆另一个客户。
有效的我想获取存储库,并将每个分支放入自己的文件夹中。我已成功获取存储库并提取最新信息。而且我也做了一个结帐以切换分支,但是拉动没有工作,它仍然没有完成我的文件结构。有没有一种简单的方法可以做到这一点,或者我是否必须为每个分支创建一个新的存储库并在那里进行结账?
TL; DR是否有一种使用LibGit2Sharp将存储库及其分支作为文件夹的简单方法?
这是我现在使用默认变量值的代码。
public class GitActions
{
public string url = "";
public string RepoName = "test";
public string path = @"C:\temp\rooted\test2";
public string user = Properties.Settings.Default.User;
public string pass = Properties.Settings.Default.Password;
public Signature sig = new Signature("test", "test", new DateTimeOffset(2011, 06, 16, 10, 58, 27, TimeSpan.FromHours(2)));
public bool CloneRepo()
{
try
{
string clonedRepoPath = Repository.Clone(url, path, new CloneOptions()
{
CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
{
Username = user,
Password = pass,
}
});
Console.WriteLine("Clone Success");
return true;
}
catch (Exception ex)
{
Console.WriteLine("Clone Failure");
return false;
}
}
public bool PullRepo()
{
try
{
using (var repo = new Repository(path))
{
repo.Network.Pull(sig, new PullOptions()
{
FetchOptions = new FetchOptions()
{
CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
{
Username = user,
Password = pass,
}
},
MergeOptions = new MergeOptions()
});
Console.WriteLine("Pull Success");
}
return true;
}
catch (Exception ex)
{
Console.WriteLine("Pull Failure");
return false;
}
}
public bool SwitchBranch(string name)
{
try
{
using (var repo = new Repository(path))
{
repo.Checkout(repo.Branches[name], sig);
}
Console.WriteLine("Switch successful");
return true;
}
catch (Exception ex)
{
Console.WriteLine("Branch Switch Failed");
return false;
}
}
}
答案 0 :(得分:1)
考虑到您的方案,我建议您只执行Pull()
,而不是执行Fetch()
,而只会检索更新的内容。
给定存储库,以下内容应该为每个分支创建一个新文件夹
var path = ... // Path where the repo has been cloned/fetched
var branchesRootPath = ... // Target root directory where the new branches folder should be created
var now = DateTimeOffset.UtcNow.Ticks; // Unique number to always create new folders
using (var repo = new Repository(path))
{
foreach (var b in repo.Branches)
{
// A branch name can contains a slash.
var branchFolderName = string.Format("{0}-{1}", now, b.Name.Replace("/", "-"));
var branchFolder = Directory.CreateDirectory(
Path.Combine(branchesRootPath, branchFolderName));
// Force will ensure a clean checkout and update the working directory
// with the content of the branch
repo.Checkout(b, new CheckoutOptions
{ CheckoutModifiers = CheckoutModifiers.Force });
// This body of this function is not described here, but should
// recursively copy the working directory into the branchFolder directory
//
// Note: Everything should be copied *except* the ".git" directory
CopyFilesRecursively(repo.Info.WorkingDirectory, branchFolder.FullName);
}
}