public override void Setup()
        {
            // Generate directories and a svn util.
            RemotePath = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar);
            LocalPath  = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar);
            Directory.CreateDirectory(RemotePath.FullPath + "repo.git");
            RemoteUrl = "file:///" + RemotePath.FullPath + "repo.git";

            // Initialize the bare repo.
            var ci = new InitCommand();

            ci.SetDirectory(new Sharpen.FilePath(RemotePath.FullPath + "repo.git"));
            ci.SetBare(true);
            ci.Call();
            var    bare   = new FileRepository(new Sharpen.FilePath(RemotePath.FullPath + "repo.git"));
            string branch = Constants.R_HEADS + "master";

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

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

            // Check out the repository.
            Checkout(LocalPath, RemoteUrl);
            Repo   = GetRepo(LocalPath, RemoteUrl);
            DotDir = ".git";
        }
Exemplo n.º 2
0
        public override void Setup()
        {
            // Generate directories and a svn util.
            rootUrl      = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar);
            rootCheckout = new FilePath(FileService.CreateTempDirectory() + Path.DirectorySeparatorChar);
            Directory.CreateDirectory(rootUrl.FullPath + "repo.git");
            repoLocation = "file:///" + rootUrl.FullPath + "repo.git";

            // Initialize the bare repo.
            InitCommand ci = new InitCommand();

            ci.SetDirectory(new Sharpen.FilePath(rootUrl.FullPath + "repo.git"));
            ci.SetBare(true);
            ci.Call();
            FileRepository bare   = new FileRepository(new Sharpen.FilePath(rootUrl.FullPath + "repo.git"));
            string         branch = Constants.R_HEADS + "master";

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

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

            // Check out the repository.
            Checkout(rootCheckout, repoLocation);
            repo    = GetRepo(rootCheckout, repoLocation);
            DOT_DIR = ".git";
        }
        private bool LinkHEAD(RefUpdate target)
        {
            try
            {
                RefUpdate u = ((RefDirectoryUpdate)refdb.NewUpdate(Constants.HEAD, false));
                u.DisableRefLog();
                switch (u.Link(target.GetName()))
                {
                case RefUpdate.Result.NEW:
                case RefUpdate.Result.FORCED:
                case RefUpdate.Result.NO_CHANGE:
                {
                    return(true);
                }

                default:
                {
                    return(false);

                    break;
                }
                }
            }
            catch (IOException)
            {
                return(false);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void Checkout(Repository repo, FetchResult result)
        {
            if (branch.StartsWith(Constants.R_HEADS))
            {
                RefUpdate head = repo.UpdateRef(Constants.HEAD);
                head.DisableRefLog();
                head.Link(branch);
            }
            Ref head_1 = result.GetAdvertisedRef(branch);

            if (head_1 == null || head_1.GetObjectId() == null)
            {
                return;
            }
            // throw exception?
            RevCommit commit   = ParseCommit(repo, head_1);
            bool      detached = !head_1.GetName().StartsWith(Constants.R_HEADS);
            RefUpdate u        = repo.UpdateRef(Constants.HEAD, detached);

            u.SetNewObjectId(commit.Id);
            u.ForceUpdate();
            if (!bare)
            {
                DirCache         dc = repo.LockDirCache();
                DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.Tree);
                co.Checkout();
            }
        }
Exemplo n.º 6
0
        /// <exception cref="System.IO.IOException"></exception>
        private RefUpdate CreateRefUpdate(Ref stashRef)
        {
            RefUpdate update = repo.UpdateRef(Constants.R_STASH);

            update.DisableRefLog();
            update.SetExpectedOldObjectId(stashRef.GetObjectId());
            update.SetForceUpdate(true);
            return(update);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a new Git repository initializing the necessary files and
        /// directories.
        /// </summary>
        /// <remarks>
        /// Create a new Git repository initializing the necessary files and
        /// directories.
        /// </remarks>
        /// <param name="bare">if true, a bare repository is created.</param>
        /// <exception cref="System.IO.IOException">in case of IO problem</exception>
        public override void Create(bool bare)
        {
            FileBasedConfig cfg = ((FileBasedConfig)GetConfig());

            if (cfg.GetFile().Exists())
            {
                throw new InvalidOperationException(MessageFormat.Format(JGitText.Get().repositoryAlreadyExists
                                                                         , Directory));
            }
            FileUtils.Mkdirs(Directory, true);
            refs.Create();
            objectDatabase.Create();
            FileUtils.Mkdir(new FilePath(Directory, "branches"));
            FileUtils.Mkdir(new FilePath(Directory, "hooks"));
            RefUpdate head = UpdateRef(Constants.HEAD);

            head.DisableRefLog();
            head.Link(Constants.R_HEADS + Constants.MASTER);
            bool fileMode;

            if (FileSystem.SupportsExecute())
            {
                FilePath tmp = FilePath.CreateTempFile("try", "execute", Directory);
                FileSystem.SetExecute(tmp, true);
                bool on = FileSystem.CanExecute(tmp);
                FileSystem.SetExecute(tmp, false);
                bool off = FileSystem.CanExecute(tmp);
                FileUtils.Delete(tmp);
                fileMode = on && !off;
            }
            else
            {
                fileMode = false;
            }
            cfg.SetInt(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION
                       , 0);
            cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE
                           , fileMode);
            if (bare)
            {
                cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_BARE
                               , true);
            }
            cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES
                           , !bare);
            if (SystemReader.GetInstance().IsMacOS())
            {
                // Java has no other way
                cfg.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE
                               , true);
            }
            cfg.Save();
        }
        public virtual void EmptyStashReflog()
        {
            Git git = Git.Wrap(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit commit = git.Commit().SetMessage("create file").Call();
            RefUpdate update = db.UpdateRef(Constants.R_STASH);

            update.SetNewObjectId(commit);
            update.DisableRefLog();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update.Update());
            StashListCommand        command = Git.Wrap(db).StashList();
            ICollection <RevCommit> stashed = command.Call();

            NUnit.Framework.Assert.IsNotNull(stashed);
            NUnit.Framework.Assert.IsTrue(stashed.IsEmpty());
        }
Exemplo n.º 9
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        private void Checkout(Repository clonedRepo, FetchResult result)
        {
            Ref head = result.GetAdvertisedRef(branch);

            if (branch.Equals(Constants.HEAD))
            {
                Ref foundBranch = FindBranchToCheckout(result);
                if (foundBranch != null)
                {
                    head = foundBranch;
                }
            }
            if (head == null || head.GetObjectId() == null)
            {
                return;
            }
            // throw exception?
            if (head.GetName().StartsWith(Constants.R_HEADS))
            {
                RefUpdate newHead = clonedRepo.UpdateRef(Constants.HEAD);
                newHead.DisableRefLog();
                newHead.Link(head.GetName());
                AddMergeConfig(clonedRepo, head);
            }
            RevCommit commit   = ParseCommit(clonedRepo, head);
            bool      detached = !head.GetName().StartsWith(Constants.R_HEADS);
            RefUpdate u        = clonedRepo.UpdateRef(Constants.HEAD, detached);

            u.SetNewObjectId(commit.Id);
            u.ForceUpdate();
            if (!bare)
            {
                DirCache         dc = clonedRepo.LockDirCache();
                DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.Tree);
                co.Checkout();
                if (cloneSubmodules)
                {
                    CloneSubmodules(clonedRepo);
                }
            }
        }