如何使用Lib2Gitsharp推送新创建的标签

时间:2015-07-07 22:50:25

标签: git libgit2sharp

有人可以帮我用Lib2GitSharp推送新创建的标签吗?下面的代码正确地创建了标签,但当我按下标签时,它给了我"请求失败并带有statis代码:401"

    public static bool createTag(string tag,string localRepoPath)
    {
        var repo = new Repository(localRepoPath);
        if (repo == null)
        {
            Console.WriteLine(DateTime.Now + "No repository exists in " + localRepoPath);
            return false;
        }
        Tag t = repo.ApplyTag(tag);
        if (t == null)
        {
            Console.WriteLine(DateTime.Now + "Could not create tag :" + tag);
            return false;
        }
        else
            Console.WriteLine(DateTime.Now + "Tag has been created successfully :" + tag);
        return true;
    }

    //push the tags
    public static bool pushTags(string tag, string localRepoPath)
    {
        try
        {
            using (Repository repo = new Repository(localRepoPath))
            {
                Remote remote = repo.Network.Remotes["origin"];
                repo.Network.Push(remote, "refs/tags/test1", "refs/tags/test1");

            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(DateTime.Now + "----#Errors in push tag " + tag + " " + ex.Message);
            return false;
        }

        return true;
    }

1 个答案:

答案 0 :(得分:2)

此代码有效!

    public static bool createTag(string tag,string localRepoPath)
    {
        var repo = new Repository(localRepoPath);
        if (repo == null)
        {
            Console.WriteLine(DateTime.Now + "No repository exists in " + localRepoPath);
            return false;
        }
        Tag t = repo.ApplyTag(tag);
        if (t == null)
        {
            Console.WriteLine(DateTime.Now + "Could not create tag :" + tag);
            return false;
        }
        else
            Console.WriteLine(DateTime.Now + "Tag has been created successfully :" + tag);
        return true;
    }

    //push the tags
    public static bool pushTags(string tag, string localRepoPath)
    {
        try
        {
            Credentials creds = new UsernamePasswordCredentials()
            {
                Username = USERNAME,
                Password = PASSWORD
            };
            CredentialsHandler ccd = (url, usernameFromUrl, types) => creds;
            PushOptions options = new PushOptions { CredentialsProvider = ccd };
            string rfspec = "refs/tags/" + tag;
            using (Repository repo = new Repository(localRepoPath))
            {
                Remote remote = repo.Network.Remotes["origin"];
                repo.Network.Push(remote, rfspec, rfspec, options);

            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(DateTime.Now + "----#Errors in Push tag " + tag + " " + ex.Message);
            return false;
        }

        return true;
    }