具有C#和位存储桶的自动git克隆脚本

时间:2019-07-10 17:39:08

标签: c# git ssh git-bash git-clone

我正在尝试编写一个C#程序,该程序可以自动从bitbucket克隆许多git repos。

我目前在这段代码的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.

我正在C#中使用System.Diagnostics.Process来尝试实现这一目标。我不确定在gitbash安装的Windows中使用哪个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
}

0 个答案:

没有答案