Пример #1
0
        public virtual void TestCreateOrigin()
        {
            RemoteConfig rc = new RemoteConfig(config, "origin");

            rc.AddURI(new URIish("/some/dir"));
            rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
            rc.Update(config);
            CheckConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n" + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
                        );
        }
Пример #2
0
        public virtual void TestSaveAllTags()
        {
            RemoteConfig rc = new RemoteConfig(config, "origin");

            rc.AddURI(new URIish("/some/dir"));
            rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
            rc.TagOpt = TagOpt.FETCH_TAGS;
            rc.Update(config);
            CheckConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n" + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
                        + "\ttagopt = --tags\n");
        }
Пример #3
0
        public virtual void TestFindRemoteRefUpdatesTrackingRef()
        {
            remoteConfig.AddFetchRefSpec(new RefSpec("refs/heads/*:refs/remotes/test/*"));
            transport = NGit.Transport.Transport.Open(db, remoteConfig);
            ICollection <RemoteRefUpdate> result = transport.FindRemoteRefUpdatesFor(Sharpen.Collections
                                                                                     .NCopies(1, new RefSpec("+refs/heads/a:refs/heads/a")));

            NUnit.Framework.Assert.AreEqual(1, result.Count);
            TrackingRefUpdate tru = result.Iterator().Next().GetTrackingRefUpdate();

            NUnit.Framework.Assert.AreEqual("refs/remotes/test/a", tru.GetLocalName());
            NUnit.Framework.Assert.AreEqual("refs/heads/a", tru.GetRemoteName());
            NUnit.Framework.Assert.AreEqual(db.Resolve("refs/heads/a"), tru.GetNewObjectId());
            NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, tru.GetOldObjectId());
        }
Пример #4
0
		public static FileRepository Init (string targetLocalPath, string url, IProgressMonitor monitor)
		{
			InitCommand ci = new InitCommand ();
			ci.SetDirectory (targetLocalPath);
			var git = ci.Call ();
			FileRepository repo = (FileRepository) git.GetRepository ();
			
			string branch = Constants.R_HEADS + "master";
			
			RefUpdate head = repo.UpdateRef (Constants.HEAD);
			head.DisableRefLog ();
			head.Link (branch);
			
			RemoteConfig remoteConfig = new RemoteConfig (repo.GetConfig (), "origin");
			remoteConfig.AddURI (new URIish (url));
			
			string dst = Constants.R_REMOTES + remoteConfig.Name;
			RefSpec wcrs = new RefSpec();
			wcrs = wcrs.SetForceUpdate (true);
			wcrs = wcrs.SetSourceDestination (Constants.R_HEADS	+ "*", dst + "/*");
			
			remoteConfig.AddFetchRefSpec (wcrs);
	
			// we're setting up for a clone with a checkout
			repo.GetConfig().SetBoolean ("core", null, "bare", false);
	
			remoteConfig.Update (repo.GetConfig());
	
			repo.GetConfig().Save();
			return repo;
		}
Пример #5
0
 public override void SetUp()
 {
     base.SetUp();
     dbTarget = CreateWorkRepository();
     source = new Git(db);
     target = new Git(dbTarget);
     // put some file in the source repo
     sourceFile = new FilePath(db.WorkTree, "SomeFile.txt");
     WriteToFile(sourceFile, "Hello world");
     // and commit it
     source.Add().AddFilepattern("SomeFile.txt").Call();
     source.Commit().SetMessage("Initial commit for source").Call();
     // configure the target repo to connect to the source via "origin"
     StoredConfig targetConfig = ((FileBasedConfig)dbTarget.GetConfig());
     targetConfig.SetString("branch", "master", "remote", "origin");
     targetConfig.SetString("branch", "master", "merge", "refs/heads/master");
     RemoteConfig config = new RemoteConfig(targetConfig, "origin");
     config.AddURI(new URIish(source.GetRepository().WorkTree.GetPath()));
     config.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
     config.Update(targetConfig);
     targetConfig.Save();
     targetFile = new FilePath(dbTarget.WorkTree, "SomeFile.txt");
     // make sure we have the same content
     target.Pull().Call();
     target.Checkout().SetStartPoint("refs/remotes/origin/master").SetName("master").Call
         ();
     targetConfig.SetString("branch", "master", "merge", "refs/heads/master");
     targetConfig.SetBoolean("branch", "master", "rebase", true);
     targetConfig.Save();
     AssertFileContentsEqual(targetFile, "Hello world");
 }
Пример #6
0
		public virtual void TestSaveTimeout()
		{
			RemoteConfig rc = new RemoteConfig(config, "origin");
			rc.AddURI(new URIish("/some/dir"));
			rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
			rc.Timeout = 60;
			rc.Update(config);
			CheckConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n" + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
				 + "\ttimeout = 60\n");
		}
Пример #7
0
		public virtual void TestSaveAllTags()
		{
			RemoteConfig rc = new RemoteConfig(config, "origin");
			rc.AddURI(new URIish("/some/dir"));
			rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
			rc.TagOpt = TagOpt.FETCH_TAGS;
			rc.Update(config);
			CheckConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n" + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
				 + "\ttagopt = --tags\n");
		}
Пример #8
0
		public virtual void TestTrackingUpdate()
		{
			Repository db2 = CreateBareRepository();
			string remote = "origin";
			string branch = "refs/heads/master";
			string trackingBranch = "refs/remotes/" + remote + "/master";
			Git git = new Git(db);
			RevCommit commit1 = git.Commit().SetMessage("Initial commit").Call();
			RefUpdate branchRefUpdate = db.UpdateRef(branch);
			branchRefUpdate.SetNewObjectId(commit1.Id);
			branchRefUpdate.Update();
			RefUpdate trackingBranchRefUpdate = db.UpdateRef(trackingBranch);
			trackingBranchRefUpdate.SetNewObjectId(commit1.Id);
			trackingBranchRefUpdate.Update();
			StoredConfig config = ((FileBasedConfig)db.GetConfig());
			RemoteConfig remoteConfig = new RemoteConfig(config, remote);
			URIish uri = new URIish(db2.Directory.ToURI().ToURL());
			remoteConfig.AddURI(uri);
			remoteConfig.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + remote +
				 "/*"));
			remoteConfig.Update(config);
			config.Save();
			RevCommit commit2 = git.Commit().SetMessage("Commit to push").Call();
			RefSpec spec = new RefSpec(branch + ":" + branch);
			Iterable<PushResult> resultIterable = git.Push().SetRemote(remote).SetRefSpecs(spec
				).Call();
			PushResult result = resultIterable.Iterator().Next();
			TrackingRefUpdate trackingRefUpdate = result.GetTrackingRefUpdate(trackingBranch);
			NUnit.Framework.Assert.IsNotNull(trackingRefUpdate);
			NUnit.Framework.Assert.AreEqual(trackingBranch, trackingRefUpdate.GetLocalName());
			NUnit.Framework.Assert.AreEqual(branch, trackingRefUpdate.GetRemoteName());
			NUnit.Framework.Assert.AreEqual(commit2.Id, trackingRefUpdate.GetNewObjectId());
			NUnit.Framework.Assert.AreEqual(commit2.Id, db.Resolve(trackingBranch));
			NUnit.Framework.Assert.AreEqual(commit2.Id, db2.Resolve(branch));
		}
Пример #9
0
		public virtual void TestPushWithoutPushRefSpec()
		{
			Git git = new Git(db);
			Git git2 = new Git(CreateBareRepository());
			StoredConfig config = git.GetRepository().GetConfig();
			RemoteConfig remoteConfig = new RemoteConfig(config, "test");
			URIish uri = new URIish(git2.GetRepository().Directory.ToURI().ToURL());
			remoteConfig.AddURI(uri);
			remoteConfig.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
			remoteConfig.Update(config);
			config.Save();
			WriteTrashFile("f", "content of f");
			git.Add().AddFilepattern("f").Call();
			RevCommit commit = git.Commit().SetMessage("adding f").Call();
			git.Checkout().SetName("not-pushed").SetCreateBranch(true).Call();
			git.Checkout().SetName("branchtopush").SetCreateBranch(true).Call();
			NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/branchtopush"
				));
			NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed"
				));
			NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master"
				));
			git.Push().SetRemote("test").Call();
			NUnit.Framework.Assert.AreEqual(commit.Id, git2.GetRepository().Resolve("refs/heads/branchtopush"
				));
			NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/not-pushed"
				));
			NUnit.Framework.Assert.AreEqual(null, git2.GetRepository().Resolve("refs/heads/master"
				));
		}
Пример #10
0
		public static FileRepository Clone (string targetLocalPath, string url, IProgressMonitor monitor)
		{
			// Initialize
			
			InitCommand ci = new InitCommand ();
			ci.SetDirectory (targetLocalPath);
			var git = ci.Call ();
			FileRepository repo = (FileRepository) git.GetRepository ();
			
			string branch = Constants.R_HEADS + "master";
			string remoteName = "origin";
			
			RefUpdate head = repo.UpdateRef (Constants.HEAD);
			head.DisableRefLog ();
			head.Link (branch);
			
			RemoteConfig remoteConfig = new RemoteConfig (repo.GetConfig (), remoteName);
			remoteConfig.AddURI (new URIish (url));
			
			string dst = Constants.R_REMOTES + remoteConfig.Name;
			RefSpec wcrs = new RefSpec();
			wcrs = wcrs.SetForceUpdate (true);
			wcrs = wcrs.SetSourceDestination (Constants.R_HEADS	+ "*", dst + "/*");
			
			remoteConfig.AddFetchRefSpec (wcrs);
	
			// we're setting up for a clone with a checkout
			repo.GetConfig().SetBoolean ("core", null, "bare", false);
	
			remoteConfig.Update (repo.GetConfig());
	
			repo.GetConfig().Save();
			
			// Fetch
			
			Transport tn = Transport.Open (repo, remoteName);
			FetchResult r;

			try {
				r = tn.Fetch(new GitMonitor (monitor), null);
			}
			finally {
				tn.Close ();
			}
			
			// Create the master branch
			
			// branch is like 'Constants.R_HEADS + branchName', we need only
			// the 'branchName' part
			String branchName = branch.Substring (Constants.R_HEADS.Length);
			git.BranchCreate ().SetName (branchName).SetUpstreamMode (CreateBranchCommand.SetupUpstreamMode.TRACK).SetStartPoint ("origin/master").Call ();
			
			// Checkout

			DirCache dc = repo.LockDirCache ();
			try {
				RevWalk rw = new RevWalk (repo);
				ObjectId remCommitId = repo.Resolve (remoteName + "/" + branchName);
				RevCommit remCommit = rw.ParseCommit (remCommitId);
				DirCacheCheckout co = new DirCacheCheckout (repo, null, dc, remCommit.Tree);
				co.Checkout ();
			} catch {
				dc.Unlock ();
				throw;
			}
			
			return repo;
		}
Пример #11
0
 /// <exception cref="System.Exception"></exception>
 private Git SetUpRepoWithRemote()
 {
     Repository remoteRepository = CreateWorkRepository();
     Git remoteGit = new Git(remoteRepository);
     // commit something
     WriteTrashFile("Test.txt", "Hello world");
     remoteGit.Add().AddFilepattern("Test.txt").Call();
     initialCommit = remoteGit.Commit().SetMessage("Initial commit").Call();
     WriteTrashFile("Test.txt", "Some change");
     remoteGit.Add().AddFilepattern("Test.txt").Call();
     secondCommit = remoteGit.Commit().SetMessage("Second commit").Call();
     // create a master branch
     RefUpdate rup = remoteRepository.UpdateRef("refs/heads/master");
     rup.SetNewObjectId(initialCommit.Id);
     rup.ForceUpdate();
     Repository localRepository = CreateWorkRepository();
     Git localGit = new Git(localRepository);
     StoredConfig config = localRepository.GetConfig();
     RemoteConfig rc = new RemoteConfig(config, "origin");
     rc.AddURI(new URIish(remoteRepository.Directory.GetPath()));
     rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
     rc.Update(config);
     config.Save();
     FetchResult res = localGit.Fetch().SetRemote("origin").Call();
     NUnit.Framework.Assert.IsFalse(res.GetTrackingRefUpdates().IsEmpty());
     rup = localRepository.UpdateRef("refs/heads/master");
     rup.SetNewObjectId(initialCommit.Id);
     rup.ForceUpdate();
     rup = localRepository.UpdateRef(Constants.HEAD);
     rup.Link("refs/heads/master");
     rup.SetNewObjectId(initialCommit.Id);
     rup.Update();
     return localGit;
 }