libgit2 / libgit2/libgit2sharp

Question: Using `RewriteHistory` multiple times on the same commit

Đang mở
#2,023 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
C#
Star
3.5k
Fork
925
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi Folks, This isn't a bug report but a question I have regarding calling the RewriteHistory method on the same commit.

public class Tests
{
    Repository Repository;
    DirectoryInfo RepositoryDirectoryInfo;

    private static void CopyDirectoryContents(string sourcePath, string targetPath)
    {
       // copies repository, containg .git and repository content to a temp path. not important for this.
    }

    private static Repository LoadRepository(string repositoryAbsolutePath)
    {
        return new Repository(repositoryAbsolutePath);
    }

    [SetUp]
    public void Setup()
    {
        // Create a temporary directory
        string temporaryGitRepositoryAbsPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        RepositoryDirectoryInfo = Directory.CreateDirectory(temporaryGitRepositoryAbsPath);

        // Create a copy of the git Repository fixture as testing will modify the fixture itself
        string fixtureGitRepositoryRelPath = "Fixtures\\SampleGitRepository";
        CopyDirectoryContents(fixtureGitRepositoryRelPath, RepositoryDirectoryInfo.FullName);

        Repository = LoadRepository(RepositoryDirectoryInfo.FullName);
    }

    [Test]
    public void Test1()
    {
        // Get the commits and grab the first one
        List<Commit> firstCommits = Repository.Commits.ToList();
        Commit firstChangeToFirstCommit = firstCommits.First();

        // Rewrite commit. Change commit message.
        GitRewriter.RewriteCommit(
            repository: Repository,
            commit: firstChangeToFirstCommit,
            author: firstChangeToFirstCommit.Author,
            committer: firstChangeToFirstCommit.Committer,
            commitMessage: "first change to commit message"
        );

        // Not sure if required (I dont think it is)
        Repository.Reset(ResetMode.Hard, Repository.Head.Tip);

        // Reload Repository thats changed.
        // a.k.a return new Repository("the abs path of the repository");
        Repository = LoadRepository(RepositoryDirectoryInfo.FullName);

        // Get the commits again, and grab the first one like before
        List<Commit> secondCommits = Repository.Commits.ToList();
        Commit secondChangeToFirstCommit = secondCommits.First();

        // Rewrite a second time
        GitRewriter.RewriteCommit(
            repository: Repository,
            commit: secondChangeToFirstCommit,
            author: secondChangeToFirstCommit.Author,
            committer: secondChangeToFirstCommit.Committer,
            commitMessage: "second change to commit message"
        );
    }
}
public class GitRewriter
{
  public static void RewriteCommits(Repository repository, Commit[] commits, Signature author, Signature committer, string commitMessage)
  {
      RewriteHistoryOptions options = new()
      {
          OnError = (error) => ... ,
          OnSucceeding = () => ... ,
          CommitHeaderRewriter = commit =>
          {
              return CommitRewriteInfo.From(
                  commit: commit,
                  author: author,
                  committer: committer,
                  message: commitMessage
              );
          }
      };
      repository.Refs.RewriteHistory(options, commits);
  }
  public static void RewriteCommit(Repository repository, Commit commit, Signature author, Signature committer, string commitMessage)
  {
      Commit[] singleCommit = new Commit[] { commit };
      RewriteCommits(
          repository: repository,
          commits: singleCommit,
          author: author,
          committer: committer,
          commitMessage: commitMessage
      );
  }
}

If you look at my test case Test1, I'm trying to use RewriteHistory on the same commit, twice.

The first rewrite works fine.

On the second attempt, I get the exception: Can't back up reference 'refs/heads/master' - 'refs/original/heads/master' already exists

Message: 
System.InvalidOperationException : Can't back up reference 'refs/heads/master' - 'refs/original/heads/master' already exists

Stack Trace: 
HistoryRewriter.RewriteReference[TRef,TTarget](TRef oldRef, Func`2 selectTarget, Func`2 rewriteTarget, ReferenceUpdater`2 updateTarget)
HistoryRewriter.RewriteReference(Reference reference)
HistoryRewriter.Execute()
ReferenceCollection.RewriteHistory(RewriteHistoryOptions options, IEnumerable`1 commitsToRewrite)
ReferenceCollection.RewriteHistory(RewriteHistoryOptions options, Commit[] commitsToRewrite)
GitRewriter.RewriteCommit(Repository repository, Commit[] commits, Signature author, Signature committer, String commitMessage) line 29
GitRewriter.RewriteCommit(Repository repository, Commit commit, Signature author, Signature committer, String commitMessage) line 34
Tests.Test1() line 168
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

I'm aware that when a commit is changed, a new SHA is allocated to that changed commit, and when after I've changed the commit the first time, the SHA does indeed change, but when I attempt to change it the second time, using the changed SHA, I recieve the error as described above.

I've checked the examples of LibGit2 and I could not find anything specific regarding this use-case of changing the same commit twice.

I've tried 'reloading' the repository by created a new Repository but no dice.

Any help, tips or pointers to documentation would be greatly appreciated. Thank you,

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với ReferenceCollection.RewriteHistory và HistoryRewriter.RewriteReference, theo chuỗi Test1 đến GitRewriter.RewriteCommit. Kiểm tra cách refs/heads/master và refs/original/heads/master được sao lưu giữa các lần viết lại. Hoàn thành nghĩa là xác định liệu việc viết lại lặp lại có được hỗ trợ hay không, đồng thời ghi lại hành vi bắt buộc hoặc cách xử lý đối với tham chiếu sao lưu hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, git
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.