Beispiel #1
0
        public Repository(string path)
        {
            Contract.Requires<ArgumentNullException>(path != null, "path");

            RepositoryOptions repositoryOptions = new RepositoryOptions();
            this._git = new LibGit2Sharp.Repository(path, repositoryOptions);
            var navigator = new RepositoryNavigator(this._git);
            this.Root = new Container(navigator);
        }
Beispiel #2
0
        /// <summary>
        ///   Initializes a new instance of the <see cref = "Repository" /> class, providing ooptional behavioral overrides through <paramref name="options"/> parameter.
        ///   <para>For a standard repository, <paramref name = "path" /> should either point to the ".git" folder or to the working directory. For a bare repository, <paramref name = "path" /> should directly point to the repository folder.</para>
        /// </summary>
        /// <param name = "path">
        ///   The path to the git repository to open, can be either the path to the git directory (for non-bare repositories this
        ///   would be the ".git" folder inside the working directory) or the path to the working directory.
        /// </param>
        /// <param name="options">
        ///   Overrides to the way a repository is opened.
        /// </param>
        public Repository(string path, RepositoryOptions options = null)
        {
            Ensure.ArgumentNotNullOrEmptyString(path, "path");

            Ensure.Success(NativeMethods.git_repository_open(out handle, path));
            RegisterForCleanup(handle);

            bool isBare = NativeMethods.RepositoryStateChecker(handle, NativeMethods.git_repository_is_bare);

            Func<Index> indexBuilder = () => new Index(this);

            if (options != null)
            {
                bool isWorkDirNull = string.IsNullOrEmpty(options.WorkingDirectoryPath);
                bool isIndexNull = string.IsNullOrEmpty(options.IndexPath);

                if (isWorkDirNull && isIndexNull)
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "At least one member of the {0} instance has to be provided.", typeof(RepositoryOptions).Name));
                }

                if (isBare && (isWorkDirNull ^ isIndexNull))
                {
                    throw new ArgumentException("When overriding the opening of a bare repository, both RepositoryOptions.WorkingDirectoryPath an RepositoryOptions.IndexPath have to be provided.");
                }

                isBare = false;

                if (!isIndexNull)
                {
                    indexBuilder = () => new Index(this, options.IndexPath);
                }

                if (!isWorkDirNull)
                {
                    Ensure.Success(NativeMethods.git_repository_set_workdir(handle, options.WorkingDirectoryPath));
                }
            }

            if (!isBare)
            {
                index = indexBuilder();
            }

            commits = new CommitCollection(this);
            refs = new ReferenceCollection(this);
            branches = new BranchCollection(this);
            tags = new TagCollection(this);
            info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
            config = new Lazy<Configuration>(() => new Configuration(this));
            remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this));
            odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
            diff = new Diff(this);
        }
Beispiel #3
0
        public void InitNewRepo()
        {
            string repoPath = InitNewRepository();
            string configPath = CreateConfigurationWithDummyUser(Constants.Identity);
            var options = new RepositoryOptions {GlobalConfigurationLocation = configPath};

            using (var repo = new Repository(repoPath, options)) {
                string dir = repo.Info.Path;
                Assert.IsTrue(Path.IsPathRooted(dir));
                Assert.IsTrue(Directory.Exists(dir));
            }
        }
Beispiel #4
0
        private void EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(RepositoryOptions options)
        {
            if (options == null)
            {
                return;
            }

            if (options.GlobalConfigurationLocation == null &&
                options.XdgConfigurationLocation == null &&
                options.SystemConfigurationLocation == null)
            {
                return;
            }

            // Dirty hack to force the eager load of the configuration
            // without Resharper pestering about useless code

            if (!Config.HasConfig(ConfigurationLevel.Local))
            {
                throw new InvalidOperationException("Unexpected state.");
            }
        }
Beispiel #5
0
        /// <summary>
        /// Clone with specified options.
        /// </summary>
        /// <param name="sourceUrl">URI for the remote repository</param>
        /// <param name="workdirPath">Local path to clone into</param>
        /// <param name="bare">True will result in a bare clone, false a full clone.</param>
        /// <param name="checkout">If true, the origin's HEAD will be checked out. This only applies
        /// to non-bare repositories.</param>
        /// <param name="onTransferProgress">Handler for network transfer and indexing progress information</param>
        /// <param name="onCheckoutProgress">Handler for checkout progress information</param>
        /// <param name="options">Overrides to the way a repository is opened.</param>
        /// <param name="credentials">Credentials to use for user/pass authentication</param>
        /// <returns></returns>
        public static Repository Clone(string sourceUrl, string workdirPath,
                                       bool bare     = false,
                                       bool checkout = true,
                                       TransferProgressHandler onTransferProgress = null,
                                       CheckoutProgressHandler onCheckoutProgress = null,
                                       RepositoryOptions options = null,
                                       Credentials credentials   = null)
        {
            var cloneOpts = new GitCloneOptions
            {
                Bare = bare ? 1 : 0,
                TransferProgressCallback = TransferCallbacks.GenerateCallback(onTransferProgress),
                CheckoutOpts             =
                {
                    version     = 1,
                    progress_cb =
                        CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress),
                    checkout_strategy = checkout
                                                    ? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
                                                    : CheckoutStrategy.GIT_CHECKOUT_NONE
                },
            };

            if (credentials != null)
            {
                cloneOpts.CredAcquireCallback =
                    (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
                    NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
            }

            using (Proxy.git_clone(sourceUrl, workdirPath, cloneOpts)) {}

            // To be safe, make sure the credential callback is kept until
            // alive until at least this point.
            GC.KeepAlive(cloneOpts.CredAcquireCallback);

            return(new Repository(workdirPath, options));
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Repository"/> class, providing ooptional behavioral overrides through <paramref name="options"/> parameter.
        /// <para>For a standard repository, <paramref name="path"/> should either point to the ".git" folder or to the working directory. For a bare repository, <paramref name="path"/> should directly point to the repository folder.</para>
        /// </summary>
        /// <param name="path">
        /// The path to the git repository to open, can be either the path to the git directory (for non-bare repositories this
        /// would be the ".git" folder inside the working directory) or the path to the working directory.
        /// </param>
        /// <param name="options">
        /// Overrides to the way a repository is opened.
        /// </param>
        public Repository(string path, RepositoryOptions options = null)
        {
            Ensure.ArgumentNotNullOrEmptyString(path, "path");

            try
            {
                handle = Proxy.git_repository_open(path);
                RegisterForCleanup(handle);

                isBare = Proxy.git_repository_is_bare(handle);

                Func <Index> indexBuilder = () => new Index(this);

                string configurationGlobalFilePath = null;
                string configurationXDGFilePath    = null;
                string configurationSystemFilePath = null;

                if (options != null)
                {
                    bool isWorkDirNull = string.IsNullOrEmpty(options.WorkingDirectoryPath);
                    bool isIndexNull   = string.IsNullOrEmpty(options.IndexPath);

                    if (isBare && (isWorkDirNull ^ isIndexNull))
                    {
                        throw new ArgumentException(
                                  "When overriding the opening of a bare repository, both RepositoryOptions.WorkingDirectoryPath an RepositoryOptions.IndexPath have to be provided.");
                    }

                    isBare = false;

                    if (!isIndexNull)
                    {
                        indexBuilder = () => new Index(this, options.IndexPath);
                    }

                    if (!isWorkDirNull)
                    {
                        Proxy.git_repository_set_workdir(handle, options.WorkingDirectoryPath);
                    }

                    configurationGlobalFilePath = options.GlobalConfigurationLocation;
                    configurationXDGFilePath    = options.XdgConfigurationLocation;
                    configurationSystemFilePath = options.SystemConfigurationLocation;
                }

                if (!isBare)
                {
                    index = indexBuilder();
                }

                commits  = new CommitLog(this);
                refs     = new ReferenceCollection(this);
                branches = new BranchCollection(this);
                tags     = new TagCollection(this);
                stashes  = new StashCollection(this);
                info     = new Lazy <RepositoryInformation>(() => new RepositoryInformation(this, isBare));
                config   =
                    new Lazy <Configuration>(
                        () =>
                        RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationXDGFilePath,
                                                             configurationSystemFilePath)));
                odb        = new Lazy <ObjectDatabase>(() => new ObjectDatabase(this));
                diff       = new Diff(this);
                notes      = new NoteCollection(this);
                ignore     = new Ignore(this);
                network    = new Lazy <Network>(() => new Network(this));
                pathCase   = new Lazy <PathCase>(() => new PathCase(this));
                submodules = new SubmoduleCollection(this);

                EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(options);
            }
            catch
            {
                CleanupDisposableDependencies();
                throw;
            }
        }
Beispiel #7
0
        public static Repository Init(string path, bool isBare, RepositoryOptions options)
        {
            string gitDirPath = Init(path, isBare);

            return(new Repository(gitDirPath, options));
        }