首页 > 解决方案 > 带有 C# 和位桶的自动 git 克隆脚本

问题描述

我正在尝试编写一个可以从 bitbucket auto-magic-ally 克隆许多 git 存储库的 C# 程序。

我目前在使用此代码的 ssh 连接部分时遇到问题,并不断收到以下错误:

Cloning into 'reponame'...
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我正在使用System.Diagnostics.ProcessC# 来尝试实现这一目标。在 Windows 的 gitbash 安装的众多 shell 中,我不确定使用哪个 shell。下面是给我带来麻烦的代码块。

/// <summary>
/// Takes tokens and execute git cloning process for each token automatically..
/// </summary>
/// <param name="tokens"></param>
private static void GitCloneProcessExecutor(string[] tokens)
{
    #region Creating a process for git cloning
    foreach (string repoNameToken in tokens)
    {
        string workingDirectory = @"C:\path\to\working\directory";
        string repoName = FormatTokens(repoNameToken);    //Reformats tokens from a csv and gets rid of the commas
        var gitCloneCommand = $"git clone git@bitbucket.org:username/{repoName}.git";

        var gitCloneProcess = new Process { EnableRaisingEvents = true };
        var gitCloneProcessStartInfo = new ProcessStartInfo
        {
            WorkingDirectory = workingDirectory,
            FileName = @"C:\Program Files\Git\bin\bash.exe",        
            Verb = "runas",
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Normal,
        };
        gitCloneProcess.StartInfo = gitCloneProcessStartInfo;
        gitCloneProcess.Start();
        using (var so = gitCloneProcess.StandardOutput)
        {
            using (var si = gitCloneProcess.StandardInput)
            {
                string newDirectory = "GitRepositories";
                si.WriteLine($"cd {BashFormatDirectory(workingDirectory)}");    //Reformats working driectory from cmd readable format to bash friendly format.
                si.WriteLine($"mkdir {newDirectory}");
                si.WriteLine($"cd {newDirectory}");
                si.WriteLine($"{gitCloneCommand} -v --progress");
            }
        }

    }
    #endregion
}

标签: c#gitsshgit-bashgit-clone

解决方案


推荐阅读