private bool PerformTreeFiltering(SimpleCommit commit, out Tree newTree) { newTree = null; if (hasTreeFiltering) { // clear the cache of entries to keep and the tasks to run entriesToKeep.Clear(); // Process white list if (keepPathPatterns.Count == 0) { KeepAllEntries(commit.Tree); } else { KeepEntries(commit, commit.Tree); } ProcessPendingTasks(); // Process black list if (removePathPatterns.Count > 0) { RemoveEntries(commit); ProcessPendingTasks(); } // If the commit was discarded by a tree-filtering, we need to skip it also here if (commit.Discard || entriesToKeep.Count == 0) { commit.Discard = true; // Store that this commit was discarded (used for re-parenting commits) commitsDiscarded.Add(commit.Sha); return(true); } // Rebuild a new tree based on the list of entries to keep var treeDef = new TreeDefinition(); foreach (var entryIt in entriesToKeep) { var entry = entryIt.Key; var entryValue = entryIt.Value; if (entryValue.Blob != null) { treeDef.Add(entry.Path, entryValue.Blob, entryValue.Mode); } else { treeDef.Add(entry.Path, entry); } } newTree = repo.ObjectDatabase.CreateTree(treeDef); } else { // If we don't have any tree filtering, just use the original tree newTree = commit.Tree; } return(false); }
public void Vote(Post post, Identity voter, VoteType vote) { using (var repo = new Repository(_directory.FullName)) { var postCommit = repo.Branches[post.Id].Tip; // Retrieve existing tree var commitRoot = postCommit.Tree; var votesDir = (Tree)commitRoot[VOTES_DIR].Target; var repliesDir = (Tree)commitRoot[REPLIES_DIR].Target; // Copy existing content to new votes treedef var newVotesDir = new TreeDefinition(); foreach (TreeEntry obj in votesDir) { newVotesDir.Add(obj.Name, obj); } // Add new vote to new votes treedef Vote(repo, newVotesDir, vote); // Assemble new root treedef var newPostRoot = new TreeDefinition(); newPostRoot.Add(VOTES_DIR, repo.ObjectDatabase.CreateTree(newVotesDir)); newPostRoot.Add(REPLIES_DIR, repliesDir); // Commit new root treedef to post branch var message = string.Format("{0} by {1}", vote, voter.Name); var sig = new Signature(voter.Name, voter.Identifier, DateTimeOffset.UtcNow); CommitToBranch(repo, post.Id, message, sig, repo.ObjectDatabase.CreateTree(newPostRoot)); } }
private void Vote(Repository repo, TreeDefinition votesDir, VoteType vote) { switch (vote) { case LibBastion.VoteType.Upvote: votesDir.Add(UPVOTE, Upvote(repo), Mode.NonExecutableFile); break; case LibBastion.VoteType.Downvote: votesDir.Add(DOWNVOTE, Downvote(repo), Mode.NonExecutableFile); break; } }
public void CanNotReplaceAnExistingTreeWithATreeBeingAssembled() { using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Equal(TreeEntryTargetType.Tree, td["1"].TargetType); td.Add("new/one", repo.Lookup <Blob>("a823312"), Mode.NonExecutableFile) .Add("new/two", repo.Lookup <Blob>("a71586c"), Mode.NonExecutableFile) .Add("new/tree", repo.Lookup <Tree>("7f76480")); Assert.Throws <InvalidOperationException>(() => td.Add("1", td["new"])); } }
private static void CopyTree(TreeDefinition definition, string name, Tree tree, Repository repo, TreeModify?modifyForMe) { Tree newObject = CopyTreeToAnotherRepositoryWithModifying(tree, repo, modifyForMe); definition.Add(name, newObject); }
public void CanAddAnExistingSubmodule() { const string submodulePath = "sm_unchanged"; using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath)) { var submodule = repo.Submodules[submodulePath]; Assert.NotNull(submodule); TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.NotNull(td[submodulePath]); td.Remove(submodulePath); Assert.Null(td[submodulePath]); td.Add(submodule); TreeEntryDefinition fetched = td[submodulePath]; Assert.NotNull(fetched); Assert.Equal(submodule.HeadCommitId, fetched.TargetId); Assert.Equal(TreeEntryTargetType.GitLink, fetched.TargetType); Assert.Equal(Mode.GitLink, fetched.Mode); } }
public void CanReplaceAnExistingBlobWithATree(string targetPath) { const string treeSha = "7f76480d939dc401415927ea7ef25c676b8ddb8f"; string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.NotNull(td[targetPath]); Assert.Equal(TreeEntryTargetType.Blob, td[targetPath].TargetType); var objectId = new ObjectId(treeSha); var tree = repo.Lookup <Tree>(objectId); td.Add(targetPath, tree); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(TreeEntryTargetType.Tree, td[targetPath].TargetType); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(Mode.Directory, fetched.Mode); } }
public void CanAddAnExistingTree() { const string treeSha = "7f76480d939dc401415927ea7ef25c676b8ddb8f"; const string targetPath = "1/2"; string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); var objectId = new ObjectId(treeSha); var tree = repo.Lookup <Tree>(objectId); td.Add(targetPath, tree); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(Mode.Directory, fetched.Mode); Assert.NotNull(td["1/2/branch_file.txt"]); } }
/// <summary> /// Write a config back to the global git repository. /// </summary> /// <param name="config"></param> public void WriteConfig(Config config) { using (var repository = new Repository(LocalGitRepositoryLocation)) { var remote = repository.Network.Remotes[ConfigurationRemote]; repository.Network.Fetch(remote); // Create a blob from the content stream Blob blob; var serializedObject = new JavaScriptSerializer().Serialize(config); var contentBytes = Encoding.UTF8.GetBytes(serializedObject); using (var memoryStream = new MemoryStream(contentBytes)) { blob = repository.ObjectDatabase.CreateBlob(memoryStream); } // Put the blob in a tree var treeDefinition = new TreeDefinition(); treeDefinition.Add(GitConfigurationFile, blob, Mode.NonExecutableFile); var tree = repository.ObjectDatabase.CreateTree(treeDefinition); // Committer and author var committer = new Signature(string.Format("{0}@{1}", Environment.UserName, Environment.MachineName), TeamEmailAddress, DateTime.Now); var author = committer; //Create the commit var refOriginMaster = repository.Refs[ConfigurationRemoteRef]; var parentCommit = refOriginMaster != null ? new[] { repository.Lookup <Commit>(refOriginMaster.TargetIdentifier) } : new Commit[0]; var commit = repository.ObjectDatabase.CreateCommit(author, committer, "Updating config.json", tree, parentCommit, false); // Update the HEAD reference to point to the latest commit repository.Refs.UpdateTarget(repository.Refs.Head, commit.Id); repository.Network.Push(remote, repository.Refs.Head.CanonicalName, ConfigurationRef); } }
public void CanReplaceAnExistingTreeWithABlob() { const string blobSha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6"; const string targetPath = "1"; using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Equal(TreeEntryTargetType.Tree, td[targetPath].TargetType); var objectId = new ObjectId(blobSha); var blob = repo.Lookup <Blob>(objectId); Assert.NotNull(td["1/branch_file.txt"]); td.Add(targetPath, blob, Mode.NonExecutableFile); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(TreeEntryTargetType.Blob, td[targetPath].TargetType); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(Mode.NonExecutableFile, fetched.Mode); Assert.Null(td["1/branch_file.txt"]); } }
public void CanReplaceAnExistingGitLinkWithATree() { const string treeSha = "607d96653d4d0a4f733107f7890c2e67b55b620d"; const string targetPath = "sm_unchanged"; using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.NotNull(td[targetPath]); Assert.Equal(TreeEntryTargetType.GitLink, td[targetPath].TargetType); Assert.Equal(Mode.GitLink, td[targetPath].Mode); var objectId = new ObjectId(treeSha); var tree = repo.Lookup <Tree>(objectId); td.Add(targetPath, tree); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(TreeEntryTargetType.Tree, fetched.TargetType); Assert.Equal(Mode.Directory, fetched.Mode); } }
public void CanReplaceAnExistingGitLinkWithABlob() { const string blobSha = "42cfb95cd01bf9225b659b5ee3edcc78e8eeb478"; const string targetPath = "sm_unchanged"; using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.NotNull(td[targetPath]); Assert.Equal(TreeEntryTargetType.GitLink, td[targetPath].TargetType); Assert.Equal(Mode.GitLink, td[targetPath].Mode); var objectId = new ObjectId(blobSha); var blob = repo.Lookup <Blob>(objectId); td.Add(targetPath, blob, Mode.NonExecutableFile); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(TreeEntryTargetType.Blob, fetched.TargetType); Assert.Equal(Mode.NonExecutableFile, fetched.Mode); } }
public void CanCreateATreeContainingABlobFromAFileInTheWorkingDirectory() { string path = SandboxStandardTestRepo(); using (var repo = new Repository(path)) { Assert.Equal(FileStatus.Nonexistent, repo.RetrieveStatus("hello.txt")); File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, "hello.txt"), "I'm a new file\n"); TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree) .Add("1/new file", "hello.txt", Mode.NonExecutableFile); TreeEntryDefinition ted = td["1/new file"]; Assert.NotNull(ted); Assert.Equal(ObjectId.Zero, ted.TargetId); td.Add("1/2/another new file", ted); Tree tree = repo.ObjectDatabase.CreateTree(td); TreeEntry te = tree["1/new file"]; Assert.NotNull(te.Target); Assert.Equal("dc53d4c6b8684c21b0b57db29da4a2afea011565", te.Target.Sha); Assert.Equal("dc53d4c6b8684c21b0b57db29da4a2afea011565", td["1/new file"].TargetId.Sha); te = tree["1/2/another new file"]; Assert.NotNull(te.Target); Assert.Equal("dc53d4c6b8684c21b0b57db29da4a2afea011565", te.Target.Sha); Assert.Equal("dc53d4c6b8684c21b0b57db29da4a2afea011565", td["1/2/another new file"].TargetId.Sha); } }
public void AddFile(string path, Blob file) { lock (_lock) { _treeDefinition.Add(path, file, Mode.NonExecutableFile); _added.Add(path); } }
public void NewPost(Post post) { using (var repo = new Repository(_directory.FullName)) { var json = JsonConvert.SerializeObject(post); var sig = new Signature(post.Author.Name, post.Author.Identifier, post.Timestamp); // Create post structure var votesDir = repo.ObjectDatabase.CreateTree(new TreeDefinition()); var repliesDir = repo.ObjectDatabase.CreateTree(new TreeDefinition()); var postRoot = new TreeDefinition(); postRoot.Add(VOTES_DIR, votesDir); postRoot.Add(REPLIES_DIR, repliesDir); var commit = CommitToBranch(repo, CONTENT_BRANCH, json, sig, repo.ObjectDatabase.CreateTree(postRoot)); // Create a named branch for all future content on this post repo.CreateBranch(commit.Sha, commit); } }
public void Save(string description) { var state = JsonConvert.SerializeObject(_state, Formatting.Indented); var blob = _git.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(state)), "SvnToGitState.json"); var treeDef = new TreeDefinition(); treeDef.Add("SvnToGitState.json", blob, Mode.NonExecutableFile); var tree = _git.ObjectDatabase.CreateTree(treeDef); var signature = new Signature("SvnToGit", "SvnToGit", DateTimeOffset.Now); var branch = _git.Branches.SingleOrDefault(a => a.FriendlyName == "SvnToGitState"); var parents = branch == null ? new Commit[0] : new[] { branch.Tip }; var commit = _git.ObjectDatabase.CreateCommit(signature, signature, description, tree, parents, true); _git.Branches.Add("SvnToGitState", commit, true); }
public void CanAddAnExistingTreeEntryDefinition(string sourcePath, string targetPath) { using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Null(td[targetPath]); TreeEntryDefinition ted = td[sourcePath]; td.Add(targetPath, ted); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(ted, fetched); } }
/// <summary> /// Adds a commit to the object database. The tree will have a single text file with the given specific content /// at the beginning of the file and the given common content at the end of the file. /// </summary> /// <param name="repo">The repository.</param> /// <param name="message">The commit message.</param> /// <param name="path">The file's path.</param> /// <param name="specificContent">The content specific to that file.</param> /// <param name="commonContent">The content shared with other files.</param> /// <param name="parents">The commit's parents.</param> /// <returns>The commit added to the object database.</returns> private Commit AddCommitToOdb(Repository repo, string message, string path, string specificContent, string commonContent, params Commit[] parents) { var content = string.IsNullOrEmpty(commonContent) ? specificContent : specificContent + Environment.NewLine + commonContent + Environment.NewLine; var td = new TreeDefinition(); td.Add(path, OdbHelper.CreateBlob(repo, content), Mode.NonExecutableFile); var t = repo.ObjectDatabase.CreateTree(td); var commitSignature = GetNextSignature(); return(repo.ObjectDatabase.CreateCommit(commitSignature, commitSignature, message, t, parents, true)); }
public void TreeNamesCanContainCharsForbiddenOnSomeOS(string targetName) { string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { var pointedItem = repo.Head.Tip.Tree; var td = new TreeDefinition(); td.Add(targetName, pointedItem); var newTree = repo.ObjectDatabase.CreateTree(td); Assert.Equal(newTree[targetName].Target.Sha, pointedItem.Sha); Assert.Equal(newTree[targetName].Name, targetName); } }
private static void CopyBlob(TreeDefinition definition, string name, Blob blob, Mode mode, Repository repo) { Blob newObject; if (repo.ObjectDatabase.Contains(blob.Id)) { newObject = repo.Lookup <Blob>(blob.Id); } else { using var stream = blob.GetContentStream(); newObject = repo.ObjectDatabase.CreateBlob(stream); } definition.Add(name, newObject, mode); }
public void CannotAddTreeById() { const string treeSha = "7f76480d939dc401415927ea7ef25c676b8ddb8f"; const string targetPath = "1/2"; string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Null(td[targetPath]); var objectId = new ObjectId(treeSha); Assert.Throws <ArgumentException>(() => td.Add(targetPath, objectId, Mode.Directory)); } }
public void CanAddAnExistingBlobEntryWithAnExistingTree() { using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); TreeEntryDefinition original = td["README"]; td.Add("1/2/README", original); TreeEntryDefinition fetched = td["1/2/README"]; Assert.NotNull(fetched); Assert.Equal(original.TargetId, fetched.TargetId); Assert.Equal(original.Mode, fetched.Mode); Assert.NotNull(td["1/branch_file.txt"]); } }
public void ModifyingTheContentOfATreeSetsItsOidToNull() { const string blobSha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6"; const string targetPath = "1/another_branch_file.txt"; using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); var objectId = new ObjectId(blobSha); var blob = repo.Lookup <Blob>(objectId); Assert.NotEqual(ObjectId.Zero, td["1"].TargetId); td.Add(targetPath, blob, Mode.NonExecutableFile); Assert.Equal(ObjectId.Zero, td["1"].TargetId); } }
public void CanAddAnExistingBlob(string blobSha, string targetPath) { using (var repo = new Repository(BareTestRepoPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Null(td[targetPath]); var objectId = new ObjectId(blobSha); var blob = repo.Lookup <Blob>(objectId); td.Add(targetPath, blob, Mode.NonExecutableFile); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(Mode.NonExecutableFile, fetched.Mode); } }
public void CanAddAnExistingGitLinkTreeEntryDefinition() { const string sourcePath = "sm_unchanged"; const string targetPath = "sm_from_td"; using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Null(td[targetPath]); TreeEntryDefinition ted = td[sourcePath]; td.Add(targetPath, ted); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(ted, fetched); } }
private static TreeDefinition CreateTreeFromDirectoryCore(string path, ObjectDatabase objectDatabase, TreeDefinition treeDefinition, Func <string, string> convertToGitPath) { foreach (string filePath in Directory.GetFiles(path)) { var gitPath = convertToGitPath(filePath); using (var fs = File.OpenRead(filePath)) { var blob = objectDatabase.CreateBlob(fs, gitPath); var mode = GetFileModeForPath(filePath); treeDefinition = treeDefinition.Add(gitPath, blob, mode); } } foreach (string subPath in Directory.GetDirectories(path)) { treeDefinition = CreateTreeFromDirectoryCore(subPath, objectDatabase, treeDefinition, convertToGitPath); } return(treeDefinition); }
public void CanReplaceAnExistingTreeWithAnotherPersitedTree() { string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Equal(TreeEntryTargetType.Tree, td["1"].TargetType); TreeDefinition newTd = new TreeDefinition() .Add("new/one", repo.Lookup <Blob>("a823312"), Mode.NonExecutableFile) .Add("new/two", repo.Lookup <Blob>("a71586c"), Mode.NonExecutableFile) .Add("new/tree", repo.Lookup <Tree>("7f76480")); repo.ObjectDatabase.CreateTree(newTd); td.Add("1", newTd["new"]); Assert.Equal(TreeEntryTargetType.Tree, td["1/tree"].TargetType); } }
public void CanReplaceAnExistingTreeWithAnotherPersitedTree() { TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); using (var repo = new Repository(scd.RepositoryPath)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Equal(GitObjectType.Tree, td["1"].Type); TreeDefinition newTd = new TreeDefinition() .Add("new/one", repo.Lookup <Blob>("a823312"), Mode.NonExecutableFile) .Add("new/two", repo.Lookup <Blob>("a71586c"), Mode.NonExecutableFile) .Add("new/tree", repo.Lookup <Tree>("7f76480")); repo.ObjectDatabase.CreateTree(newTd); td.Add("1", newTd["new"]); Assert.Equal(GitObjectType.Tree, td["1/tree"].Type); } }
public void CanAddBlobById(string blobSha, string targetPath) { string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); Assert.Null(td[targetPath]); var objectId = new ObjectId(blobSha); td.Add(targetPath, objectId, Mode.NonExecutableFile); TreeEntryDefinition fetched = td[targetPath]; Assert.NotNull(fetched); Assert.Equal(objectId, fetched.TargetId); Assert.Equal(Mode.NonExecutableFile, fetched.Mode); } }
public void CanCreateACommit() { TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); using (var repo = new Repository(scd.RepositoryPath)) { Branch head = repo.Head; TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree); td.Add("1/2/readme", td["README"]); Tree tree = repo.ObjectDatabase.CreateTree(td); Commit commit = repo.ObjectDatabase.CreateCommit("message", DummySignature, DummySignature, tree, new[] { repo.Head.Tip }); Branch newHead = repo.Head; Assert.Equal(head, newHead); Assert.Equal(commit, repo.Lookup <Commit>(commit.Sha)); } }
/// <summary> /// Adds a commit to the object database. The tree will have a single text file with the given specific content /// at the beginning of the file and the given common content at the end of the file. /// </summary> /// <param name="repo">The repository.</param> /// <param name="message">The commit message.</param> /// <param name="path">The file's path.</param> /// <param name="specificContent">The content specific to that file.</param> /// <param name="commonContent">The content shared with other files.</param> /// <param name="parents">The commit's parents.</param> /// <returns>The commit added to the object database.</returns> private Commit AddCommitToOdb(Repository repo, string message, string path, string specificContent, string commonContent, params Commit[] parents) { var content = string.IsNullOrEmpty(commonContent) ? specificContent : specificContent + Environment.NewLine + commonContent + Environment.NewLine; var td = new TreeDefinition(); td.Add(path, OdbHelper.CreateBlob(repo, content), Mode.NonExecutableFile); var t = repo.ObjectDatabase.CreateTree(td); var commitSignature = GetNextSignature(); return repo.ObjectDatabase.CreateCommit(commitSignature, commitSignature, message, t, parents, true); }