我使用 Octokit.net 工具制作了自己的 GitHub 客户端,我的问题是,当存储库中的文件不再位于本地文件夹中时,如何删除它们?
这是我已经拥有的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reactive.Linq;
using System.IO;
using Octokit;
using Octokit.Internal;
using Octokit.Reactive;
class Program
{
static void Main(string[] args)
{
MainAsync(args[0], args[1], args[2], args[3]).Wait();
}
static async Task MainAsync(string rootPath, string owner, string repo, string branch)
{
var github = new GitHubClient(new ProductHeaderValue("repository"));
var user = await github.User.Get("username");
github.Credentials = new Credentials("token");
var contents = await github.Repository.Content.GetAllContents(owner, repo);
foreach(var githubFilePath in contents)
{
Console.WriteLine(githubFilePath);
}
Console.WriteLine(contents);
UpdateFull().Wait();
// Update a whole folder
async Task UpdateFull()
{
updateGithub(rootPath).Wait();
async Task updateGithub(string path)
{
var localContents = Directory.GetFiles(path);
var localContentFolders = Directory.GetDirectories(path);
var headMasterRef = "heads/master";
var masterReference = await github.Git.Reference.Get(owner, repo, headMasterRef);
var latestCommit = await github.Git.Commit.Get(owner, repo, masterReference.Object.Sha);
var nt = new NewTree { BaseTree = latestCommit.Tree.Sha };
// Add items based on blobs
AddDirectoryToTree(path, path + "\\", nt).Wait();
var rootTree = await github.Git.Tree.Create(owner, repo, nt);
// Create Commit
var newCommit = new NewCommit("Commit test with several files", rootTree.Sha, masterReference.Object.Sha);
var commit = await github.Git.Commit.Create(owner, repo, newCommit);
await github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha));
}
async Task AddDirectoryToTree(string DirectoryPath, string RootPath, NewTree Tree)
{
// all direct files in the folder
//
var localContents = Directory.GetFiles(DirectoryPath);
foreach (var filePath in localContents)
{
string sExtension = Path.GetExtension(filePath).ToUpper();
string sFilename = filePath.Replace(RootPath, "").Replace(@"\", "/");
Console.WriteLine("File(Local): " + sFilename);
switch (sExtension)
{
default:
// Create text blob
var textBlob = new NewBlob { Encoding = EncodingType.Utf8, Content = File.ReadAllText(filePath) };
var textBlobRef = await github.Git.Blob.Create(owner, repo, textBlob);
Tree.Tree.Add(new NewTreeItem { Path = sFilename, Mode = "100644", Type = TreeType.Blob, Sha = textBlobRef.Sha });
break;
case ".PNG":
case ".JPEG":
case ".JPG":
// For image, get image content and convert it to base64
var imgBase64 = Convert.ToBase64String(File.ReadAllBytes(filePath));
// Create image blob
var imgBlob = new NewBlob { Encoding = EncodingType.Base64, Content = (imgBase64) };
var imgBlobRef = await github.Git.Blob.Create(owner, repo, imgBlob);
Tree.Tree.Add(new NewTreeItem { Path = sFilename, Mode = "100644", Type = TreeType.Blob, Sha = imgBlobRef.Sha });
break;
}
}
// subfolder
//
var localContentFolders = Directory.GetDirectories(DirectoryPath);
foreach (string subDirPath in localContentFolders)
{
AddDirectoryToTree(subDirPath, RootPath, Tree).Wait();
}
}
}
Console.WriteLine("-----------------------------------");
Console.WriteLine("Successfully commited Files");
Console.ReadKey();
我是否可能需要为 Github 存储库添加一个新的 foreach 并随后创建一个 if 语句来查看存储库和本地文件夹?:o
如果你们能帮助我,将不胜感激!:D