Example #1
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);

            string configurationGlobalFilePath = 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)
                {
                    Ensure.Success(NativeMethods.git_repository_set_workdir(handle, options.WorkingDirectoryPath));
                }

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

            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>(() => RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationSystemFilePath)));
            remotes  = new Lazy <RemoteCollection>(() => new RemoteCollection(this));
            odb      = new Lazy <ObjectDatabase>(() => new ObjectDatabase(this));
            diff     = new Diff(this);
            notes    = new NoteCollection(this);
        }
Example #2
0
        /// <summary>
        /// Deletes the note on the specified object, and for the given namespace.
        /// <para>Both the Author and Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
        /// </summary>
        /// <param name="collection">The <see cref="NoteCollection"/></param>
        /// <param name="targetId">The target <see cref="ObjectId"/>, for which the note will be created.</param>
        /// <param name="namespace">The namespace on which the note will be removed. It can be either a canonical namespace or an abbreviated namespace ('refs/notes/myNamespace' or just 'myNamespace').</param>
        public static void Remove(this NoteCollection collection, ObjectId targetId, string @namespace)
        {
            Signature author = collection.repo.Config.BuildSignature(DateTimeOffset.Now, true);

            collection.Remove(targetId, author, author, @namespace);
        }
Example #3
0
        /// <summary>
        /// Creates or updates a <see cref="Note"/> on the specified object, and for the given namespace.
        /// <para>Both the Author and Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
        /// </summary>
        /// <param name="collection">The <see cref="NoteCollection"/></param>
        /// <param name="targetId">The target <see cref="ObjectId"/>, for which the note will be created.</param>
        /// <param name="message">The note message.</param>
        /// <param name="namespace">The namespace on which the note will be created. It can be either a canonical namespace or an abbreviated namespace ('refs/notes/myNamespace' or just 'myNamespace').</param>
        /// <returns>The note which was just saved.</returns>
        public static Note Add(this NoteCollection collection, ObjectId targetId, string message, string @namespace)
        {
            Signature author = collection.repo.Config.BuildSignature(DateTimeOffset.Now, true);

            return(collection.Add(targetId, message, author, author, @namespace));
        }
Example #4
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;
            }
        }