使用LibGit2Sharp创建文件夹结构?

使用LibGit2Sharp创建文件夹结构?,git,github,libgit2sharp,Git,Github,Libgit2sharp,想法是这样的。我们正在运行一个内部“GitLab”,其中包含一系列存储库。其中一些存储库供用户体验人员使用,他们希望与用于向客户展示其工作的网站同步。他们要求我制作一个界面,用户可以在其中输入他们存储库的url,我会将其同步到网站 我创建了一个计划任务,该任务将读取网站上输入的存储库,然后使用“LibGit2Sharp”在本地获取文件。然后,这些文件将通过FTP传输到网站 设计完美吗?没有,但我还在学习,这只是我在另一个客户登陆之前的一个内部项目 实际上,我希望获得存储库,并将每个分支放入其自己

想法是这样的。我们正在运行一个内部“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);
    }
}

“branchFolderName”声明中的“now”变量应该是什么。很抱歉我忘了描述它了。这是一个唯一的数字(记号),用于防止覆盖以前版本的文件夹。