Пример #1
0
        public static LocalGitRepository Init(string targetLocalPath, string url)
        {
            InitCommand ci = new InitCommand();

            ci.SetDirectory(targetLocalPath);
            ci.Call();
            LocalGitRepository repo = new LocalGitRepository(Path.Combine(targetLocalPath, Constants.DOT_GIT));

            string branch = Constants.R_HEADS + "master";

            RefUpdate head = repo.UpdateRef(Constants.HEAD);

            head.DisableRefLog();
            head.Link(branch);

            if (url != null)
            {
                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);
                remoteConfig.Update(repo.GetConfig());
            }

            // we're setting up for a clone with a checkout
            repo.GetConfig().SetBoolean("core", null, "bare", false);

            repo.GetConfig().Save();
            return(repo);
        }
Пример #2
0
        /// <exception cref="Sharpen.URISyntaxException"></exception>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        /// <exception cref="NGit.Api.Errors.InvalidRemoteException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private FetchResult Fetch(Repository repo, URIish u)
        {
            // create the remote config and save it
            RemoteConfig config = new RemoteConfig(repo.GetConfig(), remote);

            config.AddURI(u);
            string  dst     = bare ? Constants.R_HEADS : Constants.R_REMOTES + config.Name;
            RefSpec refSpec = new RefSpec();

            refSpec = refSpec.SetForceUpdate(true);
            refSpec = refSpec.SetSourceDestination(Constants.R_HEADS + "*", dst + "/*");
            //$NON-NLS-1$ //$NON-NLS-2$
            config.AddFetchRefSpec(refSpec);
            config.Update(repo.GetConfig());
            repo.GetConfig().Save();
            // run the fetch command
            FetchCommand command = new FetchCommand(repo);

            command.SetRemote(remote);
            command.SetProgressMonitor(monitor);
            command.SetTagOpt(TagOpt.FETCH_TAGS);
            command.SetTimeout(timeout);
            if (credentialsProvider != null)
            {
                command.SetCredentialsProvider(credentialsProvider);
            }
            IList <RefSpec> specs = CalculateRefSpecs(dst);

            command.SetRefSpecs(specs);
            return(command.Call());
        }
Пример #3
0
        public async Task Clone()
        {
            if (Repository != null)
            {
                throw new InvalidOperationException();
            }
            var url = "https://github.com/" + Settings.Owner + "/" + Settings.Repository + ".git";
            var git = Git.Init().SetDirectory(Location).Call();

            Repository = git.GetRepository();

            var config       = Repository.GetConfig();
            var remoteConfig = new RemoteConfig(config, "origin");

            remoteConfig.AddURI(new URIish(url));
            remoteConfig.AddFetchRefSpec(new RefSpec(
                                             "+refs/heads/" + Settings.Branch +
                                             ":refs/remotes/origin/" + Settings.Branch));
            remoteConfig.Update(config);
            config.Save();

            await Fetch();

            await Task.Run(() => {
                git.BranchCreate().SetName(Settings.Branch).SetStartPoint("origin/" + Settings.Branch)
                .SetUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).Call();
                git.Checkout().SetName(Settings.Branch).Call();
            });
        }
        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();
            AssertFileContentsEqual(targetFile, "Hello world");
        }
Пример #5
0
        /// <exception cref="Sharpen.URISyntaxException"></exception>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        /// <exception cref="NGit.Api.Errors.InvalidRemoteException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private FetchResult Fetch(Repository repo, URIish u)
        {
            // create the remote config and save it
            RemoteConfig config = new RemoteConfig(repo.GetConfig(), remote);

            config.AddURI(u);
            string  dst     = Constants.R_REMOTES + config.Name;
            RefSpec refSpec = new RefSpec();

            refSpec = refSpec.SetForceUpdate(true);
            refSpec = refSpec.SetSourceDestination(Constants.R_HEADS + "*", dst + "/*");
            //$NON-NLS-1$ //$NON-NLS-2$
            config.AddFetchRefSpec(refSpec);
            config.Update(repo.GetConfig());
            repo.GetConfig().SetString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants
                                       .CONFIG_KEY_REMOTE, remote);
            repo.GetConfig().SetString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants
                                       .CONFIG_KEY_MERGE, branch);
            repo.GetConfig().Save();
            // run the fetch command
            FetchCommand command = new FetchCommand(repo);

            command.SetRemote(remote);
            command.SetProgressMonitor(monitor);
            command.SetTagOpt(TagOpt.FETCH_TAGS);
            if (credentialsProvider != null)
            {
                command.SetCredentialsProvider(credentialsProvider);
            }
            return(command.Call());
        }
Пример #6
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"
                                                                               ));
        }
Пример #7
0
        public void testCreateOrigin()
        {
            RemoteConfig rc = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
            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");
        }
Пример #8
0
        private void saveRemote(URIish uri)
        {
            var          repo = Repository._internal_repo;
            RemoteConfig rc   = new RemoteConfig(repo.Config, OriginName);

            rc.AddURI(uri);
            rc.AddFetchRefSpec(new RefSpec().SetForce(true).SetSourceDestination(Constants.R_HEADS + "*",
                                                                                 Constants.R_REMOTES + OriginName + "/*"));
            rc.Update(repo.Config);
            repo.Config.save();
        }
Пример #9
0
        public void testSaveAllTags()
        {
            RemoteConfig rc = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
            rc.AddURI(new URIish("/some/dir"));
            rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
            rc.SetTagOpt(TagOpt.FETCH_TAGS);
            rc.Update(config);

            checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
                    + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
                    + "\ttagopt = --tags\n");
        }
Пример #10
0
        public 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");
        }
Пример #11
0
        public void testSaveNoTags()
        {
            RemoteConfig rc = new RemoteConfig(config, "origin");

            rc.AddURI(new URIish("/some/dir"));
            rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.Name + "/*"));
            rc.SetTagOpt(TagOpt.NO_TAGS);
            rc.Update(config);

            checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
                        + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
                        + "\ttagopt = --no-tags\n");
        }
Пример #12
0
        public void testFindRemoteRefUpdatesTrackingRef()
        {
            remoteConfig.AddFetchRefSpec(new RefSpec("refs/heads/*:refs/remotes/test/*"));
            transport = GitSharp.Core.Transport.Transport.Open(db, remoteConfig);
            ICollection <RemoteRefUpdate> result =
                transport.findRemoteRefUpdatesFor(new List <RefSpec> {
                new RefSpec("+refs/heads/a:refs/heads/a")
            });

            Assert.AreEqual(1, result.Count);
            TrackingRefUpdate tru = result.ToArray()[0].TrackingRefUpdate;

            Assert.AreEqual("refs/remotes/test/a", tru.LocalName);
            Assert.AreEqual("refs/heads/a", tru.RemoteName);
            Assert.AreEqual(db.Resolve("refs/heads/a"), tru.NewObjectId);
            Assert.AreEqual(null, tru.OldObjectId);
        }
Пример #13
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));
        }
Пример #14
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);
        }
Пример #15
0
 /// <summary>
 /// Add a new fetch RefSpec to this remote.
 /// </summary>
 /// <param name="s">the new specification to add.</param>
 /// <returns>true if the specification was added; false if it already exists.</returns>
 public bool AddFetchRefSpec(RefSpec s)
 {
     return(_config.AddFetchRefSpec(s));
 }