This class handles checking out one or two trees merging with the index (actually a tree too). Three-way merges are no performed. See FailOnConflict.
Esempio n. 1
0
        public void testCheckingOutWithConflicts()
        {
            var index = new GitIndex(db);
            index.add(trash, writeTrashFile("bar", "bar"));
            index.add(trash, writeTrashFile("foo/bar/baz/qux", "foo/bar"));
            recursiveDelete(new FileInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));
            writeTrashFile("bar/baz/qux/foo", "another nasty one");
            writeTrashFile("foo", "troublesome little bugger");
            
            var workDirCheckout1 = new WorkDirCheckout(db, trash, index, index);

            AssertHelper.Throws<CheckoutConflictException>(workDirCheckout1.checkout);


            var workDirCheckout2 = new WorkDirCheckout(db, trash, index, index) { FailOnConflict = false };
            workDirCheckout2.checkout();

            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "bar")).IsFile());
            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "foo/bar/baz/qux")).IsFile());

            var index2 = new GitIndex(db);
            recursiveDelete(new FileInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));
            index2.add(trash, writeTrashFile("bar/baz/qux/foo", "bar"));
            writeTrashFile("bar/baz/qux/bar", "evil? I thought it said WEEVIL!");
            index2.add(trash, writeTrashFile("foo", "lalala"));

            workDirCheckout2 = new WorkDirCheckout(db, trash, index2, index) { FailOnConflict = false };
            workDirCheckout2.checkout();

            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "bar")).IsFile());
            Assert.IsTrue(new FileInfo(Path.Combine(trash.FullName, "foo/bar/baz/qux")).IsFile());
            Assert.IsNotNull(index2.GetEntry("bar"));
            Assert.IsNotNull(index2.GetEntry("foo/bar/baz/qux"));
            Assert.IsNull(index2.GetEntry("bar/baz/qux/foo"));
            Assert.IsNull(index2.GetEntry("foo"));
        }
Esempio n. 2
0
        private void Checkout(string working_directory)         // [henon] made this private to not confuse with Checkout( paths ). It seems to be not a common use case anyway and could be better exposed via the CheckoutCommand
        {
            if (InternalCommit == null)
            {
                throw new InvalidOperationException("Unable to checkout this commit. It was not initialized properly (i.e. the hash is not pointing to a commit object).");
            }
            if (working_directory == null)
            {
                throw new ArgumentException("Path to checkout directory must not be null");
            }
            if (new DirectoryInfo(working_directory).Exists == false)
            {
                throw new IOException("Cannot checkout into non-existent directory: " + working_directory);
            }
            var db    = _repo._internal_repo;
            var index = _repo.Index.GitIndex;

            index.RereadIfNecessary();
            CoreTree tree = InternalCommit.TreeEntry;
            var      co   = new GitSharp.Core.WorkDirCheckout(db, new DirectoryInfo(working_directory), index, tree);

            co.checkout();
        }
Esempio n. 3
0
		private void Checkout(string working_directory) // [henon] made this private to not confuse with Checkout( paths ). It seems to be not a common use case anyway and could be better exposed via the CheckoutCommand
		{
			if (InternalCommit == null)
				throw new InvalidOperationException("Unable to checkout this commit. It was not initialized properly (i.e. the hash is not pointing to a commit object).");
			if (working_directory == null)
				throw new ArgumentException("Path to checkout directory must not be null");
			if (new DirectoryInfo(working_directory).Exists == false)
				throw new IOException("Cannot checkout into non-existent directory: " + working_directory);
			var db = _repo._internal_repo;
			var index = _repo.Index.GitIndex;
			index.RereadIfNecessary();
			CoreTree tree = InternalCommit.TreeEntry;
			var co = new GitSharp.Core.WorkDirCheckout(db, new DirectoryInfo(working_directory), index, tree);
			co.checkout();
		}
Esempio n. 4
0
        private void doCheckout(GitSharp.Core.Ref branch)
        {
            if (branch == null)
                throw new ArgumentNullException("branch", "Cannot checkout; no HEAD advertised by remote");
            var repo = Repository._internal_repo;

            if (!Constants.HEAD.Equals(branch.getName()))
            {
                RefUpdate u1 = repo.UpdateRef(Constants.HEAD);
                u1.disableRefLog();
                u1.link(branch.getName());
            }

            GitSharp.Core.Commit commit = repo.MapCommit(branch.ObjectId);
            RefUpdate u = repo.UpdateRef(Constants.HEAD);
            u.NewObjectId = commit.CommitId;
            u.forceUpdate();
            GitIndex index = new GitIndex(repo);
            GitSharp.Core.Tree tree = commit.TreeEntry;
            WorkDirCheckout co = new WorkDirCheckout(repo, repo.WorkingDirectory, index, tree);
            co.checkout();
            index.write();
        }
Esempio n. 5
0
        public void testFindingConflicts()
        {
            var index = new GitIndex(db);
            index.add(trash, writeTrashFile("bar", "bar"));
            index.add(trash, writeTrashFile("foo/bar/baz/qux", "foo/bar"));
            recursiveDelete(new FileInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));
            writeTrashFile("bar/baz/qux/foo", "another nasty one");
            writeTrashFile("foo", "troublesome little bugger");

            var workDirCheckout = new WorkDirCheckout(db, trash, index, index);
            workDirCheckout.PrescanOneTree();
            List<string> conflictingEntries = workDirCheckout.Conflicts;
            Assert.AreEqual("bar/baz/qux/foo", conflictingEntries[0]);
            Assert.AreEqual("foo", conflictingEntries[1]);

            var index2 = new GitIndex(db);
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "bar")));
            recursiveDelete(new DirectoryInfo(Path.Combine(trash.FullName, "foo")));

            index2.add(trash, writeTrashFile("bar/baz/qux/foo", "bar"));
            index2.add(trash, writeTrashFile("foo", "lalala"));

            workDirCheckout = new WorkDirCheckout(db, trash, index2, index);
            workDirCheckout.PrescanOneTree();

            conflictingEntries = workDirCheckout.Conflicts;
            List<string> removedEntries = workDirCheckout.Removed;
            Assert.IsTrue(conflictingEntries.Count == 0);
            Assert.IsTrue(removedEntries.Contains("bar/baz/qux/foo"));
            Assert.IsTrue(removedEntries.Contains("foo"));
        }
Esempio n. 6
0
 private WorkDirCheckout Go()
 {
     _theReadTree = new WorkDirCheckout(db, trash, _theHead, _theIndex, _theMerge);
     _theReadTree.PrescanTwoTrees();
     return _theReadTree;
 }
Esempio n. 7
0
 private void Checkout()
 {
     _theReadTree = new WorkDirCheckout(db, trash, _theHead, _theIndex, _theMerge);
     _theReadTree.checkout();
 }
Esempio n. 8
0
        public void testRules4thru13_IndexEntryNotInHead()
        {
            // rule 4 and 5
            var indexEntries = new Dictionary<string, string> {{"foo", "foo"}};
            SetupCase(null, null, indexEntries);
            _theReadTree = Go();
            assertAllEmpty();

            // rule 6 and 7
            indexEntries = new Dictionary<string, string> { { "foo", "foo" } };
            SetupCase(null, indexEntries, indexEntries);
            _theReadTree = Go();
            assertAllEmpty();

            // rule 8 and 9
            var mergeEntries = new Dictionary<string, string> { { "foo", "merge" } };
            SetupCase(null, mergeEntries, indexEntries);
            Go();
            Assert.IsTrue(_theReadTree.Updated.isEmpty());
            Assert.IsTrue(_theReadTree.Removed.isEmpty());
            Assert.IsTrue(_theReadTree.Conflicts.Contains("foo"));

            // rule 10
            var headEntries = new Dictionary<string, string> { { "foo", "foo" } };
            SetupCase(headEntries, null, indexEntries);
            Go();
            Assert.IsTrue(_theReadTree.Removed.Contains("foo"));
            Assert.IsTrue(_theReadTree.Updated.isEmpty());
            Assert.IsTrue(_theReadTree.Conflicts.isEmpty());

            // rule 11
            SetupCase(headEntries, null, indexEntries);
            new FileInfo(Path.Combine(trash.FullName, "foo")).Delete();
            writeTrashFile("foo", "bar");
            _theIndex.Members[0].forceRecheck();
            Go();
            Assert.IsTrue(_theReadTree.Removed.isEmpty());
            Assert.IsTrue(_theReadTree.Updated.isEmpty());
            Assert.IsTrue(_theReadTree.Conflicts.Contains("foo"));

            // rule 12 and 13
            headEntries["foo"] = "head";
            SetupCase(headEntries, null, indexEntries);
            Go();
            Assert.IsTrue(_theReadTree.Removed.isEmpty());
            Assert.IsTrue(_theReadTree.Updated.isEmpty());
            Assert.IsTrue(_theReadTree.Conflicts.Contains("foo"));

            // rule 14 and 15
            SetupCase(headEntries, headEntries, indexEntries);
            Go();
            assertAllEmpty();

            // rule 16 and 17
            SetupCase(headEntries, mergeEntries, indexEntries);
            Go();
            Assert.IsTrue(_theReadTree.Conflicts.Contains("foo"));

            // rule 18 and 19
            SetupCase(headEntries, indexEntries, indexEntries);
            Go();
            assertAllEmpty();

            // rule 20
            SetupCase(indexEntries, mergeEntries, indexEntries);
            Go();
            Assert.IsTrue(_theReadTree.Updated.ContainsKey("foo"));

            // rule 21
            SetupCase(indexEntries, mergeEntries, indexEntries);
            new FileInfo(Path.Combine(trash.FullName, "foo")).Delete();
            writeTrashFile("foo", "bar");
            _theIndex.Members[0].forceRecheck();
            Go();
            Assert.IsTrue(_theReadTree.Conflicts.Contains("foo"));
        }
Esempio n. 9
0
 public void testRules1thru3_NoIndexEntry()
 {
     var index = new GitIndex(db);
     var head = new Core.Tree(db);
     FileTreeEntry entry = head.AddFile("foo");
     ObjectId expected = ObjectId.FromString("ba78e065e2c261d4f7b8f42107588051e87e18e9");
     entry.Id = expected;
     var merge = new Core.Tree(db);
     var checkout = new WorkDirCheckout(db, trash, head, index, merge);
     checkout.PrescanTwoTrees();
     Assert.IsTrue(checkout.Removed.Contains("foo"));
     checkout = new WorkDirCheckout(db, trash, merge, index, head);
     checkout.PrescanTwoTrees();
     Assert.AreEqual(expected, checkout.Updated["foo"]);
     ObjectId id2 = ObjectId.FromString("ba78e065e2c261d4f7b8f42107588051e87e18ee");
     merge.AddFile("foo").Id = id2;
     checkout = new WorkDirCheckout(db, trash, head, index, merge);
     checkout.PrescanTwoTrees();
     Assert.AreEqual(id2, checkout.Updated["foo"]);
 }
Esempio n. 10
0
        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"));
        }
Esempio n. 11
0
        private void doCheckout(GitSharp.Core.Ref branch)
        {
            if (branch == null)
                throw new ArgumentNullException("branch", "Cannot checkout; no HEAD advertised by remote");

            if (!Constants.HEAD.Equals(branch.Name))
                GitRepository.WriteSymref(Constants.HEAD, branch.Name);
            GitSharp.Core.Commit commit = GitRepository.MapCommit(branch.ObjectId);
            RefUpdate u = GitRepository.UpdateRef(Constants.HEAD);
            u.NewObjectId = commit.CommitId;
            u.ForceUpdate();
            GitIndex index = new GitIndex(GitRepository);
            GitSharp.Core.Tree tree = commit.TreeEntry;
            WorkDirCheckout co = new WorkDirCheckout(GitRepository, GitRepository.WorkingDirectory, index, tree);
            co.checkout();
            index.write();
        }
		protected override void Run ()
		{
			var cloneDialog = new CloneRepositoryDialog ();
			cloneDialog.Run ();
			cloneDialog.Destroy ();
			
			var repositoryPath = cloneDialog.RepositoryPath;
			URIish source = new URIish (repositoryPath);
			var originName = cloneDialog.OriginName;
			var destination = cloneDialog.WorkingDirectory;
			var workingDirectory = Path.Combine (destination, Constants.DOT_GIT);
			
			if (string.IsNullOrEmpty (originName))
				originName = Constants.DEFAULT_REMOTE_NAME;
			
			var rep = new GitSharp.Core.Repository (new DirectoryInfo (workingDirectory));
			rep.Create ();
			rep.Config.setBoolean ("core", null, "bare", false);
			rep.Config.save ();
			
			var rc = new RemoteConfig (rep.Config, originName);
			rc.AddURI (source);
			rc.AddFetchRefSpec (new RefSpec ().SetForce (true).SetSourceDestination (
					Constants.R_HEADS + "*", 
					Constants.R_REMOTES + originName + "/*"));
			rc.Update (rep.Config);
			rep.Config.save ();
			
			Transport tn = Transport.open (rep, originName);
			FetchResult fetchResult = null;
			try 
			{
				fetchResult = tn.fetch (new NullProgressMonitor (), null);
			} 
			catch 
			{
				tn.Dispose ();
			}
			
			GitSharp.Core.Ref branch = null;
			if (fetchResult != null) 
			{
				var headId = fetchResult.GetAdvertisedRef (Constants.HEAD);
				var availableRefs = new List<GitSharp.Core.Ref> ();
				
				foreach (GitSharp.Core.Ref r in fetchResult.AdvertisedRefs) 
				{
					var n = r.Name;
					if (!n.StartsWith (Constants.R_HEADS))
						continue;
					
					availableRefs.Add (r);
					if (headId == null || branch != null)
						continue;
					
					if (r.ObjectId.Equals (headId.ObjectId))
						branch = r;
				}
				
				availableRefs.Sort (RefComparator.INSTANCE);
				
				if (headId != null && branch == null)
					branch = headId;
			}
			
			if (branch != null) 
			{
				if (!Constants.HEAD.Equals (branch.Name)) 
				{
					//rep. (Constants.HEAD, branch.Name);
					GitSharp.Core.Commit commit = rep.MapCommit (branch.ObjectId);
					RefUpdate update = rep.UpdateRef (Constants.HEAD);
					update.NewObjectId = commit.CommitId;
					update.forceUpdate ();
					
					var index = new GitIndex (rep);
					var tree = commit.TreeEntry;
					WorkDirCheckout co = new WorkDirCheckout (rep, rep.WorkingDirectory, index, tree);
					co.checkout ();
					index.write ();
				}
			} 
			else 
			{
				MessageService.ShowError ("Cannot clone: no HEAD advertised by remote.");
			}
			
			MessageService.ShowMessage(string.Format("Finished cloning {0} to {1}", 
					repositoryPath, 
					destination));
		}