public void testDirectoryFileSimple() { _theIndex = new GitIndex(db); _theIndex.add(trash, writeTrashFile("DF", "DF")); Core.Tree head = db.MapTree(_theIndex.writeTree()); recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "DF"))); _theIndex = new GitIndex(db); _theIndex.add(trash, writeTrashFile("DF/DF", "DF/DF")); Core.Tree merge = db.MapTree(_theIndex.writeTree()); _theIndex = new GitIndex(db); recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "DF"))); _theIndex.add(trash, writeTrashFile("DF", "DF")); _theReadTree = new WorkDirCheckout(db, trash, head, _theIndex, merge); _theReadTree.PrescanTwoTrees(); Assert.IsTrue(_theReadTree.Removed.Contains("DF")); Assert.IsTrue(_theReadTree.Updated.ContainsKey("DF/DF")); recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "DF"))); _theIndex = new GitIndex(db); _theIndex.add(trash, writeTrashFile("DF/DF", "DF/DF")); _theReadTree = new WorkDirCheckout(db, trash, merge, _theIndex, head); _theReadTree.PrescanTwoTrees(); Assert.IsTrue(_theReadTree.Removed.Contains("DF/DF")); Assert.IsTrue(_theReadTree.Updated.ContainsKey("DF")); }
public void testDelete() { var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a@b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); index.writeTree(); index.remove(trash, new FileInfo(Path.Combine(trash.FullName, "a@b"))); index.write(); Assert.AreEqual("a.b", index.Members[0].Name); Assert.AreEqual("a/b", index.Members[1].Name); var indexr = new GitIndex(db); indexr.Read(); Assert.AreEqual("a.b", indexr.Members[0].Name); Assert.AreEqual("a/b", indexr.Members[1].Name); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public void testCheckout() { // Prepare tree, remote it and checkout var index = new GitIndex(db); FileInfo aslashb = writeTrashFile("a/b", "data:a/b"); FileInfo acolonb = writeTrashFile("a@b", "data:a:b"); FileInfo adotb = writeTrashFile("a.b", "data:a.b"); index.add(trash, aslashb); index.add(trash, acolonb); index.add(trash, adotb); index.write(); index.writeTree(); Delete(aslashb); Delete(acolonb); Delete(adotb); Delete(Directory.GetParent(aslashb.FullName)); var index2 = new GitIndex(db); Assert.AreEqual(0, index2.Members.Count); index2.ReadTree(db.MapTree(ObjectId.FromString("0036d433dc4f10ec47b61abc3ec5033c78d34f84"))); index2.checkout(trash); Assert.AreEqual("data:a/b", Content(aslashb)); Assert.AreEqual("data:a:b", Content(acolonb)); Assert.AreEqual("data:a.b", Content(adotb)); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
internal WorkDirCheckout(Repository repo, DirectoryInfo workDir, GitIndex oldIndex, GitIndex newIndex) : this() { _repo = repo; _root = workDir; _index = oldIndex; _merge = repo.MapTree(newIndex.writeTree()); }
public void testLeavingTree() { var index = new GitIndex(db); index.add(trash, writeTrashFile("foo/bar", "foo/bar")); index.add(trash, writeTrashFile("foobar", "foobar")); new IndexTreeWalker(index, db.MapTree(index.writeTree()), trash, TestTreeOnlyOneLevelTreeVisitor).Walk(); }
public Stash Create(string message) { var parent = _repo.CurrentBranch.CurrentCommit; Author author = new Author(_repo.Config["user.name"] ?? "unknown", _repo.Config["user.email"] ?? "unknown@(none)."); if (message == null) { // Use the commit summary as message message = parent.ShortHash + " " + parent.Message; int i = message.IndexOfAny(new char[] { '\r', '\n' }); if (i != -1) { message = message.Substring(0, i); } } // Create the index tree commit GitIndex index = _repo.Index.GitIndex; index.RereadIfNecessary(); var tree_id = index.writeTree(); Tree indexTree = new Tree(_repo, tree_id); string commitMsg = "index on " + _repo.CurrentBranch.Name + ": " + message; var indexCommit = Commit.Create(commitMsg + "\n", parent, indexTree, author); // Create the working dir commit tree_id = WriteWorkingDirectoryTree(parent.Tree.InternalTree, index); commitMsg = "WIP on " + _repo.CurrentBranch.Name + ": " + message; var wipCommit = Commit.Create(commitMsg + "\n", new Commit[] { parent, indexCommit }, new Tree(_repo, tree_id), author, author, DateTimeOffset.Now); string prevCommit = null; FileInfo sf = StashRefFile; if (sf.Exists) { prevCommit = File.ReadAllText(sf.FullName); } Stash s = new Stash(prevCommit, wipCommit.Hash, author, commitMsg); FileInfo stashLog = StashLogFile; File.AppendAllText(stashLog.FullName, s.FullLine + "\n"); File.WriteAllText(sf.FullName, s.CommitId + "\n"); // Wipe all local changes _repo.CurrentBranch.Reset(ResetBehavior.Hard); s.StashCollection = this; return(s); }
public void testReadTree2() { // Prepare a larger tree to test some odd cases in tree writing var index = new GitIndex(db); FileInfo f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a"); FileInfo f2 = writeTrashFile("a/c/c", "data:a/c/c"); FileInfo f3 = writeTrashFile("a/b", "data:a/b"); FileInfo f4 = writeTrashFile("a@b", "data:a:b"); FileInfo f5 = writeTrashFile("a/d", "data:a/d"); FileInfo f6 = writeTrashFile("a.b", "data:a.b"); index.add(trash, f1); index.add(trash, f2); index.add(trash, f3); index.add(trash, f4); index.add(trash, f5); index.add(trash, f6); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("5e0bf0aad57706894c15fdf0681c9bc7343274fc", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("5e0bf0aad57706894c15fdf0681c9bc7343274fc"))); IList <GitIndex.Entry> members = index2.Members; Assert.AreEqual(6, members.Count); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/a/a/a", members[1].Name); Assert.AreEqual("a/b", members[2].Name); Assert.AreEqual("a/c/c", members[3].Name); Assert.AreEqual("a/d", members[4].Name); Assert.AreEqual("a@b", members[5].Name); // reread and test var indexr = new GitIndex(db); indexr.Read(); IList <GitIndex.Entry> membersr = indexr.Members; Assert.AreEqual(6, membersr.Count); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/a/a/a", membersr[1].Name); Assert.AreEqual("a/b", membersr[2].Name); Assert.AreEqual("a/c/c", membersr[3].Name); Assert.AreEqual("a/d", membersr[4].Name); Assert.AreEqual("a@b", membersr[5].Name); }
public void testReadTree() { // Prepare tree var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a@b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("0036d433dc4f10ec47b61abc3ec5033c78d34f84", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("0036d433dc4f10ec47b61abc3ec5033c78d34f84"))); IList <GitIndex.Entry> members = index2.Members; Assert.AreEqual(3, members.Count); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/b", members[1].Name); Assert.AreEqual("a@b", members[2].Name); Assert.AreEqual(3, members.Count); var indexr = new GitIndex(db); indexr.Read(); IList <GitIndex.Entry> membersr = indexr.Members; Assert.AreEqual(3, membersr.Count); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/b", membersr[1].Name); Assert.AreEqual("a@b", membersr[2].Name); Assert.AreEqual(3, membersr.Count); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public Commit CommitChanges(string message, Author author) { if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Commit message must not be null or empty!", "message"); } if (string.IsNullOrEmpty(author.Name)) { throw new ArgumentException("Author name must not be null or empty!", "author"); } GitIndex.RereadIfNecessary(); var tree_id = GitIndex.writeTree(); // check if tree is different from current commit's tree var parent = _repo.CurrentBranch.CurrentCommit; if ((parent == null && GitIndex.Members.Count == 0) || (parent != null && parent.Tree._id == tree_id)) { throw new InvalidOperationException("There are no changes to commit"); } var commit = Commit.Create(message, parent, new Tree(_repo, tree_id), author); Ref.Update("HEAD", commit); return(commit); }
public void testWriteTree() { var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a@b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); ObjectId id = index.writeTree(); Assert.AreEqual("0036d433dc4f10ec47b61abc3ec5033c78d34f84", id.Name); writeTrashFile("a/b", "data:a/b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public void testReadTree2() { // Prepare a larger tree to test some odd cases in tree writing var index = new GitIndex(db); FileInfo f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a"); FileInfo f2 = writeTrashFile("a/c/c", "data:a/c/c"); FileInfo f3 = writeTrashFile("a/b", "data:a/b"); FileInfo f4 = writeTrashFile("a@b", "data:a:b"); FileInfo f5 = writeTrashFile("a/d", "data:a/d"); FileInfo f6 = writeTrashFile("a.b", "data:a.b"); index.add(trash, f1); index.add(trash, f2); index.add(trash, f3); index.add(trash, f4); index.add(trash, f5); index.add(trash, f6); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("5e0bf0aad57706894c15fdf0681c9bc7343274fc", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("5e0bf0aad57706894c15fdf0681c9bc7343274fc"))); IList<GitIndex.Entry> members = index2.Members; Assert.AreEqual(6, members.Count); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/a/a/a", members[1].Name); Assert.AreEqual("a/b", members[2].Name); Assert.AreEqual("a/c/c", members[3].Name); Assert.AreEqual("a/d", members[4].Name); Assert.AreEqual("a@b", members[5].Name); // reread and test var indexr = new GitIndex(db); indexr.Read(); IList<GitIndex.Entry> membersr = indexr.Members; Assert.AreEqual(6, membersr.Count); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/a/a/a", membersr[1].Name); Assert.AreEqual("a/b", membersr[2].Name); Assert.AreEqual("a/c/c", membersr[3].Name); Assert.AreEqual("a/d", membersr[4].Name); Assert.AreEqual("a@b", membersr[5].Name); }
public void testReadTree() { // Prepare tree var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a@b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a@b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("0036d433dc4f10ec47b61abc3ec5033c78d34f84", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("0036d433dc4f10ec47b61abc3ec5033c78d34f84"))); IList<GitIndex.Entry> members = index2.Members; Assert.AreEqual(3, members.Count); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/b", members[1].Name); Assert.AreEqual("a@b", members[2].Name); Assert.AreEqual(3, members.Count); var indexr = new GitIndex(db); indexr.Read(); IList<GitIndex.Entry> membersr = indexr.Members; Assert.AreEqual(3, membersr.Count); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/b", membersr[1].Name); Assert.AreEqual("a@b", membersr[2].Name); Assert.AreEqual(3, membersr.Count); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public void testCheckout() { // Prepare tree, remote it and checkout var index = new GitIndex(db); FileInfo aslashb = writeTrashFile("a/b", "data:a/b"); FileInfo acolonb = writeTrashFile("a:b", "data:a:b"); FileInfo adotb = writeTrashFile("a.b", "data:a.b"); index.add(trash, aslashb); index.add(trash, acolonb); index.add(trash, adotb); index.write(); index.writeTree(); Delete(aslashb); Delete(acolonb); Delete(adotb); Delete(Directory.GetParent(aslashb.FullName)); var index2 = new GitIndex(db); Assert.AreEqual(0, index2.Members.Length); index2.ReadTree(db.MapTree(ObjectId.FromString("c696abc3ab8e091c665f49d00eb8919690b3aec3"))); index2.checkout(trash); Assert.AreEqual("data:a/b", Content(aslashb)); Assert.AreEqual("data:a:b", Content(acolonb)); Assert.AreEqual("data:a.b", Content(adotb)); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public void testReadTree() { // Prepare tree var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a:b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("c696abc3ab8e091c665f49d00eb8919690b3aec3", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("c696abc3ab8e091c665f49d00eb8919690b3aec3"))); GitIndex.Entry[] members = index2.Members; Assert.AreEqual(3, members.Length); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/b", members[1].Name); Assert.AreEqual("a:b", members[2].Name); Assert.AreEqual(3, members.Length); var indexr = new GitIndex(db); indexr.Read(); GitIndex.Entry[] membersr = indexr.Members; Assert.AreEqual(3, membersr.Length); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/b", membersr[1].Name); Assert.AreEqual("a:b", membersr[2].Name); Assert.AreEqual(3, membersr.Length); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }
public void testReadTree2() { // Prepare a larger tree to test some odd cases in tree writing var index = new GitIndex(db); FileInfo f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a"); FileInfo f2 = writeTrashFile("a/c/c", "data:a/c/c"); FileInfo f3 = writeTrashFile("a/b", "data:a/b"); FileInfo f4 = writeTrashFile("a:b", "data:a:b"); FileInfo f5 = writeTrashFile("a/d", "data:a/d"); FileInfo f6 = writeTrashFile("a.b", "data:a.b"); index.add(trash, f1); index.add(trash, f2); index.add(trash, f3); index.add(trash, f4); index.add(trash, f5); index.add(trash, f6); index.write(); ObjectId id = index.writeTree(); Console.WriteLine("wrote id " + id); Assert.AreEqual("ba78e065e2c261d4f7b8f42107588051e87e18e9", id.Name); var index2 = new GitIndex(db); index2.ReadTree(db.MapTree(ObjectId.FromString("ba78e065e2c261d4f7b8f42107588051e87e18e9"))); GitIndex.Entry[] members = index2.Members; Assert.AreEqual(6, members.Length); Assert.AreEqual("a.b", members[0].Name); Assert.AreEqual("a/a/a/a", members[1].Name); Assert.AreEqual("a/b", members[2].Name); Assert.AreEqual("a/c/c", members[3].Name); Assert.AreEqual("a/d", members[4].Name); Assert.AreEqual("a:b", members[5].Name); // reread and test var indexr = new GitIndex(db); indexr.Read(); GitIndex.Entry[] membersr = indexr.Members; Assert.AreEqual(6, membersr.Length); Assert.AreEqual("a.b", membersr[0].Name); Assert.AreEqual("a/a/a/a", membersr[1].Name); Assert.AreEqual("a/b", membersr[2].Name); Assert.AreEqual("a/c/c", membersr[3].Name); Assert.AreEqual("a/d", membersr[4].Name); Assert.AreEqual("a:b", membersr[5].Name); }
internal void Apply(Stash stash) { Commit wip = _repo.Get <Commit> (stash.CommitId); Commit index = wip.Parents.Last(); Tree wipTree = wip.Tree; Tree headTree = _repo.CurrentBranch.CurrentCommit.Tree; GitIndex currentIndex = _repo.Index.GitIndex; Tree currentIndexTree = new Tree(_repo, _repo._internal_repo.MapTree(currentIndex.writeTree())); WorkDirCheckout co = new WorkDirCheckout(_repo._internal_repo, _repo._internal_repo.WorkingDirectory, headTree.InternalTree, currentIndex, wip.Tree.InternalTree); co.checkout(); currentIndex.write(); List <DirCacheEntry> toAdd = new List <DirCacheEntry> (); DirCache dc = DirCache.Lock(_repo._internal_repo); try { var cacheEditor = dc.editor(); // The WorkDirCheckout class doesn't check if there are conflicts in modified files, // so we have to do it here. foreach (var c in co.Updated) { var baseEntry = wip.Parents.First().Tree[c.Key] as Leaf; var oursEntry = wipTree [c.Key] as Leaf; var theirsEntry = headTree [c.Key] as Leaf; if (baseEntry != null && oursEntry != null && currentIndexTree [c.Key] == null) { // If a file was reported as updated but that file is not present in the stashed index, // it means that the file was scheduled to be deleted. cacheEditor.@add(new DirCacheEditor.DeletePath(c.Key)); File.Delete(_repo.FromGitPath(c.Key)); } else if (baseEntry != null && oursEntry != null && theirsEntry != null) { MergeResult res = MergeAlgorithm.merge(new RawText(baseEntry.RawData), new RawText(oursEntry.RawData), new RawText(theirsEntry.RawData)); MergeFormatter f = new MergeFormatter(); using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(_repo.FromGitPath(c.Key)))) { f.formatMerge(bw, res, "Base", "Stash", "Head", Constants.CHARSET.WebName); } if (res.containsConflicts()) { // Remove the entry from the index. It will be added later on. cacheEditor.@add(new DirCacheEditor.DeletePath(c.Key)); // Generate index entries for each merge stage // Those entries can't be added right now to the index because a DirCacheEditor // can't be used at the same time as a DirCacheBuilder. var e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_1); e.setObjectId(baseEntry.InternalEntry.Id); e.setFileMode(baseEntry.InternalEntry.Mode); toAdd.Add(e); e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_2); e.setObjectId(oursEntry.InternalEntry.Id); e.setFileMode(oursEntry.InternalEntry.Mode); toAdd.Add(e); e = new DirCacheEntry(c.Key, DirCacheEntry.STAGE_3); e.setObjectId(theirsEntry.InternalEntry.Id); e.setFileMode(theirsEntry.InternalEntry.Mode); toAdd.Add(e); } } } cacheEditor.finish(); if (toAdd.Count > 0) { // Add the index entries generated above var cacheBuilder = dc.builder(); for (int n = 0; n < dc.getEntryCount(); n++) { cacheBuilder.@add(dc.getEntry(n)); } foreach (var entry in toAdd) { cacheBuilder.@add(entry); } cacheBuilder.finish(); } dc.write(); dc.commit(); } catch { dc.unlock(); throw; } }
public void testWriteTree() { var index = new GitIndex(db); writeTrashFile("a/b", "data:a/b"); writeTrashFile("a:b", "data:a:b"); writeTrashFile("a.b", "data:a.b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a:b"))); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a.b"))); index.write(); ObjectId id = index.writeTree(); Assert.AreEqual("c696abc3ab8e091c665f49d00eb8919690b3aec3", id.Name); writeTrashFile("a/b", "data:a/b"); index.add(trash, new FileInfo(Path.Combine(trash.FullName, "a/b"))); if (CanRunGitStatus) { Assert.AreEqual(0, System(trash, "git status")); } }