使用 LibGit2Sharp 创建文件夹结构?
Create a folder structure using LibGit2Sharp?
思路是这样的。我们正在运行一个带有一堆存储库的内部 "GitLab"。其中一些存储库由用户体验人员使用,他们希望与用于向客户展示他们的工作的网站同步。他们要求我制作一个界面,用户可以在其中输入他们存储库的 url,然后我会将其同步到网站。
我已经创建了一个计划任务,它将读取网站上输入的存储库,然后使用 "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;
}
}
}
与其执行 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);
}
}
思路是这样的。我们正在运行一个带有一堆存储库的内部 "GitLab"。其中一些存储库由用户体验人员使用,他们希望与用于向客户展示他们的工作的网站同步。他们要求我制作一个界面,用户可以在其中输入他们存储库的 url,然后我会将其同步到网站。
我已经创建了一个计划任务,它将读取网站上输入的存储库,然后使用 "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;
}
}
}
与其执行 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);
}
}