【问题标题】:Doing a Move and an edit in one Commit with Git TFS API in C#?在 C# 中使用 Git TFS API 在一次提交中进行移动和编辑?
【发布时间】:2019-01-04 20:02:59
【问题描述】:

我正在尝试编辑我们的一个文件并将其移动到我们存储库中的另一个位置,到目前为止,我已经进行了以下设置:

        GitRepository repo = new GitRepository();
        repo.Id = new Guid(RepositoryId);
        repo.Url = "Working.url.goes.here.com";
        repo.DefaultBranch = "heads/master";

        GitRef defaultBranch = GitClient.GetRefsAsync(repo.Id, filter: repo.DefaultBranch).Result.First();

        string result = testpath.Replace(ACTUALFILEPATH, "");

        GitCommitRef newCommit;
        List<GitChange> changes;

        GitRefUpdate newBranch = new GitRefUpdate()
        {
            Name = currBranch,
            OldObjectId = defaultBranch.ObjectId,
        };

        changes = new List<GitChange>(3);

            //the changes for the test being edited
            changes.Add(new GitChange()
            {
                ChangeType = VersionControlChangeType.Add,
                Item = new GitItem() { Path = result },
                NewContent = new ItemContent()
                {
                    Content = TestContent,
                    ContentType = ItemContentType.RawText
                },
            });

            //the changes for the csproj file
            changes.Add(new GitChange()
            {
                ChangeType = VersionControlChangeType.Edit,
                Item = new GitItem() { Path = projPath },
                NewContent = new ItemContent()
                {
                    Content = projContent,
                    ContentType = ItemContentType.RawText
                },
            });

            //The deletion of the old file
            changes.Add(new GitChange()
            {
                ChangeType = VersionControlChangeType.Delete,
                Item = new GitItem() { Path = origpath },
            });

            newCommit = new GitCommitRef()
            {
                Comment = commitmessage,
                Changes = changes.ToArray()
            };

            GitPush push = GitClient.CreatePushAsync(new GitPush()
        {
            RefUpdates = new GitRefUpdate[] { newBranch },
            Commits = new GitCommitRef[] { newCommit },
            Repository = repo
        }, repo.Id).Result;

        return result;

每次我尝试运行它时,删除会导致错误,提示您不能在一次提交中修改同一个文件两次,或者如果我将删除移动到它说该文件不存在于分支目录中之前。

我需要什么:能够将测试从一个目录移动到另一个目录,编辑其内容,还可以编辑另一个文件的内容,并将所有内容推送到服务器。

其他注意事项:Powershell 和 Cmd 的使用非常有限。

任何想法或建议或知道什么是错的?非常感谢您的帮助!

【问题讨论】:

  • 没有显示相当多的内容,包括我认为的关键点:基于这些错误,听起来您正在错误地初始化 origpath。
  • 您说的很对,先生,几分钟前才发现这一点,打算在这里发布。不过感谢您的帮助!

标签: c# git tfs


【解决方案1】:

好吧,经过几个小时的测试,我终于弄明白了..

我为 origpath 使用了错误的参数,并且传递的路径与我添加的新文件相同。

TLDR:在编写函数时仔细检查你的参数

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 2011-01-08
    • 2021-05-20
    • 2011-08-22
    • 2016-04-27
    • 2017-12-22
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多