public async Task CreateSvnTree() { using var repo = GitRepository.Init(TestContext.PerTestDirectory()); var items = new RepoItem[] { new RepoItem { Name = "iota", Content = "This is dthe file 'iota'.\n" }, new RepoItem { Name = "A" }, new RepoItem { Name = "A/mu", Content = "This is the file 'mu'.\n" }, new RepoItem { Name = "A/B" }, new RepoItem { Name = "A/B/lambda", Content = "This is the file 'lambda'.\n" }, new RepoItem { Name = "A/B/E", }, new RepoItem { Name = "A/B/E/alpha", Content = "This is the file 'alpha'.\n" }, new RepoItem { Name = "A/B/E/beta", Content = "This is the file 'beta'.\n" }, new RepoItem { Name = "A/B/F" }, new RepoItem { Name = "A/C" }, new RepoItem { Name = "A/D" }, new RepoItem { Name = "A/D/gamma", Content = "This is the file 'gamma'.\n" }, new RepoItem { Name = "A/D/G" }, new RepoItem { Name = "A/D/G/pi", Content = "This is the file 'pi'.\n" }, new RepoItem { Name = "A/D/G/rho", Content = "This is the file 'rho'.\n" }, new RepoItem { Name = "A/D/G/tau", Content = "This is the file 'tau'.\n" }, new RepoItem { Name = "A/D/H" }, new RepoItem { Name = "A/D/H/chi", Content = "This is the file 'chi'.\n" }, new RepoItem { Name = "A/D/H/psi", Content = "This is the file 'psi'.\n" }, new RepoItem { Name = "A/D/H/omega", Content = "This is the file 'omega'.\n" } }; Dictionary <string, GitObject> ht = new(); foreach (var i in items.Where(x => x.Content != null)) { GitBlobWriter b = GitBlobWriter.CreateFrom(Bucket.Create.FromUTF8(i.Content !)); var r = await b.WriteAndFetchAsync(repo); ht[i.Name] = r; } foreach (var m in items.Where(x => x.Content == null).OrderByDescending(x => x.Name).Concat(new[] { new RepoItem { Name = "" } })) { GitTreeWriter t = GitTreeWriter.CreateEmpty(); foreach (var o in items.Where(x => x.Name.StartsWith(m.Name + "/") || (m.Name == "" && !x.Name.Contains('/'))).Select(x => new { Item = x, Name = x.Name.Substring(m.Name.Length).TrimStart('/') }).Where(x => !x.Name.Contains('/'))) { if (o.Item.Content is not null) { var b = (GitBlob)ht[o.Item.Name] !; t.Add(o.Name, b !); } else { var to = (GitTree)ht[o.Item.Name] !; t.Add(o.Name, to !); } } var r = await t.WriteAndFetchAsync(repo); ht[m.Name] = r; } GitCommitWriter cw = GitCommitWriter.Create(Array.Empty <GitCommit>()); cw.Tree = ((GitTree)ht[""]).AsWriter(); cw.Author = cw.Committer = new GitSignature("BH", "bh@BH", new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local))); var cs = await cw.WriteAndFetchAsync(repo); var fsckOutput = await repo.GetPlumbing().ConsistencyCheck(new GitConsistencyCheckArgs() { Full = true }); Assert.AreEqual($"dangling commit {cs.Id}", fsckOutput); Assert.AreEqual(items.Length + 1, repo.Objects.Count()); // F and C are the same empty tree Assert.IsFalse(items.Select(x => x.Name).Except(((IEnumerable <GitTreeItem>)repo.Commits[cs.Id] !.Tree.AllItems).Select(x => x.Path)).Any(), "All paths reached"); }
private static async Task CreateGreekTreeAsync(string v) { using var repo = GitRepository.Init(v, new GitRepositoryInitArgs { Bare = true }); GitCommitWriter cw = GitCommitWriter.Create(new GitCommitWriter[0]); var items = new RepoItem[] { new RepoItem { Name = "iota", Content = "This is dthe file 'iota'.\n" }, new RepoItem { Name = "A" }, new RepoItem { Name = "A/mu", Content = "This is the file 'mu'.\n" }, new RepoItem { Name = "A/B" }, new RepoItem { Name = "A/B/lambda", Content = "This is the file 'lambda'.\n" }, new RepoItem { Name = "A/B/E", }, new RepoItem { Name = "A/B/E/alpha", Content = "This is the file 'alpha'.\n" }, new RepoItem { Name = "A/B/E/beta", Content = "This is the file 'beta'.\n" }, new RepoItem { Name = "A/B/F" }, new RepoItem { Name = "A/C" }, new RepoItem { Name = "A/D" }, new RepoItem { Name = "A/D/gamma", Content = "This is the file 'gamma'.\n" }, new RepoItem { Name = "A/D/G" }, new RepoItem { Name = "A/D/G/pi", Content = "This is the file 'pi'.\n" }, new RepoItem { Name = "A/D/G/rho", Content = "This is the file 'rho'.\n" }, new RepoItem { Name = "A/D/G/tau", Content = "This is the file 'tau'.\n" }, new RepoItem { Name = "A/D/H" }, new RepoItem { Name = "A/D/H/chi", Content = "This is the file 'chi'.\n" }, new RepoItem { Name = "A/D/H/psi", Content = "This is the file 'psi'.\n" }, new RepoItem { Name = "A/D/H/omega", Content = "This is the file 'omega'.\n" } }; foreach (var item in items) { if (item.Content is not null) { cw.Tree.Add(item.Name, GitBlobWriter.CreateFrom(System.Text.Encoding.UTF8.GetBytes(item.Content !).AsBucket())); } else { cw.Tree.Add(item.Name, GitTreeWriter.CreateEmpty()); } } TimeSpan offset = new TimeSpan(1, 0, 0); cw.Author = cw.Committer = new GitSignature("BH", "bh@BH", new DateTimeOffset(2000, 1, 1, 0, 0, 0, offset)); cw.Message = "Initial Commit"; var firstCommit = await cw.WriteAndFetchAsync(repo); cw = GitCommitWriter.Create(firstCommit); cw.Author = cw.Committer = new GitSignature("BH", "bh@BH", new DateTimeOffset(2000, 1, 1, 1, 0, 0, offset)); cw.Message = "Second Commit"; var secondCommit = await cw.WriteAndFetchAsync(repo); var ct = GitTagObjectWriter.Create(secondCommit, "v0.1"); ct.Message = "Tag second Commit"; ct.Tagger = new GitSignature("BH", "bh@BH", new DateTimeOffset(2000, 1, 2, 0, 0, 0, offset)); var tag = await ct.WriteAndFetchAsync(repo); using (var rt = repo.References.CreateUpdateTransaction()) { rt.Reason = "Apply tag"; rt.Create($"refs/tags/{tag.Name}", tag.Id); await rt.CommitAsync(); } var baseId = secondCommit.Id; cw = GitCommitWriter.Create(firstCommit); cw.Author = cw.Committer = new GitSignature("BH", "bh@BH", new DateTimeOffset(2000, 1, 3, 0, 0, 0, offset)); cw.Tree.Add("A/C/delta", GitBlobWriter.CreateFrom(Encoding.UTF8.GetBytes("This is the file 'delta'.\n").AsBucket())); cw.Message = "Committed on branch"; var branchCommit = await cw.WriteAndFetchAsync(repo); cw = GitCommitWriter.Create(firstCommit, branchCommit); cw.Author = cw.Committer = new GitSignature("BH", "bh@BH", new DateTimeOffset(2000, 1, 4, 0, 0, 0, offset)); cw.Message = "Merged"; cw.Tree.Add("A/C/delta", branchCommit.Tree.AllItems["A/C/delta"].Entry.GitObject.AsLazy()); var headCommit = await cw.WriteAndFetchAsync(repo); string?refName = ((GitSymbolicReference)repo.Head).ReferenceName; Assert.AreEqual("refs/heads/master", refName); using (var rt = repo.References.CreateUpdateTransaction()) { rt.Reason = "Initial Checkout"; rt.UpdateHead(headCommit.Id); await rt.CommitAsync(); } }