【问题标题】:How to get specific commit from GitLab project如何从 GitLab 项目中获取特定的提交
【发布时间】:2021-02-21 04:08:28
【问题描述】:

我正在 Visual Studio 中使用 Windows Forms C# 开发(非常新)一个应用程序,我需要从 GitLab 项目中获得特定的提交。

我看过很多关于 GitHub 的例子。我试过这种方式(不知道是否正确):

使用 7 位 SHA1 在特定提交处下载存储库:

 var Token = "xxxx";              
 var url = "http://{my_domain}/{user}/{project_name}/repository/archive/{shor_id}.zip";
 var path = @"C:\GitLab\My_Projects";

      try 
      {
         using (var client = new System.Net.Http.HttpClient())
         {
            var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", Token);
            credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
            var contents = client.GetByteArrayAsync(url).Result;
            System.IO.File.WriteAllBytes(path, contents);
         }
      }
      catch (System.UnauthorizedAccessException)
      {
            backgroundWorker1.CancelAsync();
            Console.WriteLine("Problem downloading");
      }

但我收到此错误:

'System.UnauthorizedAccessException' in mscorlib.dll. Access denied to 'C:\GitLab\My_Projects'.

正如我之前所说,我是新手,可能说了一些愚蠢的话,提前抱歉。 如果有人知道这个主题,我很乐意解释或帮助我了解这个概念。

【问题讨论】:

  • Access denied to 'C:\GitLab\My_Projects' 显然是 local 权限问题,与 GitHub 或 GitLab 无关。
  • 迂腐:你没有下载提交;您正在下载存储库的存档 at 特定提交。此外,尝试“克隆”提交也没有任何意义。您克隆了一个包含大量提交的项目。
  • 是的,我知道我正在获取某个提交的存储库,如果我克隆,我将获得所有包含所有提交的项目。
  • 对不起,我想你不明白,因为你编的这个网址是无效的,甚至概念也不合逻辑:$"http://{my_domain}/{user}/{project_name}/{short_id_commit}.git
  • 您知道如何解决权限问题吗?我正在使用令牌,但我不明白为什么会出现访问问题。

标签: c# .net gitlab commit windows-forms-designer


【解决方案1】:

最后,我得到了解决方案。我使用 GitLab API 采取了另一个方向。 如果有人想查看我到达的结果,这里有代码:

var url_sha = $"http://{my_gitlab_domain}/api/v4/projects/{id_project}/repository/archive.zip? 
private_token={my_token}&sha={short_id_commit}";
var client = new HttpClient();
var response = await client.GetAsync(url_sha);
var local_path = @"C:\GitLab\My_Projects";

using (var stream = await response.Content.ReadAsStreamAsync())
{
     var fileInfo = new FileInfo(local_path + ".zip");
     using (var fileStream = fileInfo.OpenWrite())
     {
           await stream.CopyToAsync(fileStream);
     }
}

【讨论】:

    猜你喜欢
    • 2011-12-11
    • 2018-06-25
    • 2016-09-17
    • 2011-09-01
    • 1970-01-01
    • 2014-11-08
    • 2011-11-22
    • 2019-06-23
    • 2023-02-02
    相关资源
    最近更新 更多