/// <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); }
/// <summary> /// Initializes a new instance of the <see cref = "Repository" /> class. /// <para>For a standard repository, <paramref name = "path" /> should point to the ".git" folder. 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.</param> public Repository(string path) { Ensure.ArgumentNotNullOrEmptyString(path, "path"); int res = NativeMethods.git_repository_open(out handle, PosixPathHelper.ToPosix(path)); Ensure.Success(res); isBare = NativeMethods.git_repository_is_bare(handle); if (!isBare) { index = new Index(this); } 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)); }