3

我正在尝试通过 GitHub API 更新文件。

我已经完成了所有设置,最后实际文件已更新,这是一件好事。

但是,假设我的仓库中有这两个文件

  • FileToBeUpdated.txt
  • 自述文件.md

然后我运行我的程序

FileToBeUpdated.txt已按原样更新,但README.md已删除。

这是更新文件的 5 个步骤的代码:

private static void Main()
{
    string shaForLatestCommit = GetSHAForLatestCommit();

    string shaBaseTree = GetShaBaseTree(shaForLatestCommit);

    string shaNewTree = CreateTree(shaBaseTree);

    string shaNewCommit = CreateCommit(shaForLatestCommit, shaNewTree);

    SetHeadPointer(shaNewCommit);
}

private static void SetHeadPointer(string shaNewCommit)
{
    WebClient webClient = GetMeAFreshWebClient();

    string contents = "{" +
                      "\"sha\":" + "\"" + shaNewCommit + "\", " +
                      // "\"force\":" + "\"true\"" +
                      "}";

    // TODO validate ?
    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "refs/heads/master", "PATCH", contents);
}

private static string CreateCommit(string latestCommit, string shaNewTree)
{
    WebClient webClient = GetMeAFreshWebClient();

    string contents = "{" +
                      "\"parents\" :[ \"" + latestCommit + "\" ], " +
                      "\"tree\":" + "\"" + shaNewTree + "\", " +
                      "\"message\":" + "\"test\", " +
                      "\"author\": { \"name\": \""+ Constants.Username +"\", \"email\": \""+ Constants.Email+"\",\"date\": \"" + DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture) + "\"}" +
                      "}";


    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "commits", contents);

    var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);

    return foo.sha;
}

private static string CreateTree(string shaBaseTree)
{
    WebClient webClient = GetMeAFreshWebClient();



    string contents = "{" +
                      "\"tree\" :" +
                      "[ {" +
                      "\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," +
                      "\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
                      "\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
                      "} ]" +
                      "}";


    string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "trees", contents);

    var foo = JsonConvert.DeserializeObject<TreeRootObject>(downloadString);

    return foo.sha;
}

private static string GetShaBaseTree(string latestCommit)
{
    WebClient webClient = GetMeAFreshWebClient();

    string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "commits/" + latestCommit);

    var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);

    return foo.tree.sha;
}

private static string GetSHAForLatestCommit()
{
    WebClient webClient = GetMeAFreshWebClient();

    string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "refs/heads/master");

    var foo = JsonConvert.DeserializeObject<HeadRootObject>(downloadString);

    return foo.@object.sha;
}

private static WebClient GetMeAFreshWebClient()
{
    var webClient = new WebClient();

    webClient.Headers.Add(string.Format("Authorization: token {0}", Constants.OAuthToken));

    return webClient;
}

我错过了哪一部分?我是否使用了错误的树开始?

4

1 回答 1

3

似乎在函数 CreateTree 中我应该将 base_tree 设置在根级别,而不是在我创建的每棵树的级别。

所以在函数 CreateTree 中的内容变成了这样:

string contents = "{" +
                      "\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," + // IT'S THIS LINE!
                      "\"tree\" :" +
                          "[ {" +
                              "\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
                              "\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
                      "} ]" +
                  "}";

更新的 GitHub 文档:https ://github.com/Snakiej/developer.github.com/commit/2c4f93003703f68c4c8a436df8cf18e615e293c7

于 2013-01-30T16:03:35.373 回答