/// <summary> /// Probe for a git repository. /// <para>The lookup start from <paramref name = "startingPath" /> and walk upward parent directories if nothing has been found.</para> /// </summary> /// <param name = "startingPath">The base path where the lookup starts.</param> /// <returns>The path to the git repository.</returns> public static string Discover(string startingPath) { var buffer = new byte[4096]; int result = NativeMethods.git_repository_discover(buffer, buffer.Length, PosixPathHelper.ToPosix(startingPath), false, null); if ((GitErrorCode)result == GitErrorCode.GIT_ENOTAREPO) { return(null); } Ensure.Success(result); int nullTerminator; for (nullTerminator = 0; nullTerminator < buffer.Length; nullTerminator++) { if (buffer[nullTerminator] == 0) { break; } } if (nullTerminator == 0) { return(null); } return(PosixPathHelper.ToNative(Encoding.UTF8.GetString(buffer, 0, nullTerminator))); }
/// <summary> /// Init a repo at the specified <paramref name = "path" />. /// </summary> /// <param name = "path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param> /// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param> /// <returns>Path the git repository.</returns> public static string Init(string path, bool isBare = false) { Ensure.ArgumentNotNullOrEmptyString(path, "path"); RepositorySafeHandle repo; int res = NativeMethods.git_repository_init(out repo, PosixPathHelper.ToPosix(path), isBare); Ensure.Success(res); string normalizedPath = NativeMethods.git_repository_path(repo, GitRepositoryPathId.GIT_REPO_PATH).MarshallAsString(); repo.Dispose(); string nativePath = PosixPathHelper.ToNative(normalizedPath); // TODO: To be removed once it's being dealt with by libgit2 // libgit2 doesn't currently create the git config file, so create a minimal one if we can't find it // See https://github.com/libgit2/libgit2sharp/issues/56 for details string configFile = Path.Combine(nativePath, "config"); if (!File.Exists(configFile)) { File.WriteAllText(configFile, "[core]\n\trepositoryformatversion = 0\n"); } return(nativePath); }
private void AddToIndex(string relativePath) { relativePath = PosixPathHelper.ToPosix(relativePath); int res = NativeMethods.git_index_add(handle, relativePath); Ensure.Success(res); }
private void RemoveFromIndex(string relativePath) { relativePath = PosixPathHelper.ToPosix(relativePath); int res = NativeMethods.git_index_find(handle, relativePath); Ensure.Success(res, true); res = NativeMethods.git_index_remove(handle, res); Ensure.Success(res); }
internal RepositoryInformation(Repository repo, bool isBare) { this.repo = repo; IsBare = isBare; string posixPath = NativeMethods.git_repository_path(repo.Handle, GitRepositoryPathId.GIT_REPO_PATH).MarshallAsString(); string posixWorkingDirectoryPath = NativeMethods.git_repository_path(repo.Handle, GitRepositoryPathId.GIT_REPO_PATH_WORKDIR).MarshallAsString(); Path = PosixPathHelper.ToNative(posixPath); WorkingDirectory = PosixPathHelper.ToNative(posixWorkingDirectoryPath); }
internal RepositoryInformation(Repository repo, bool isBare) { this.repo = repo; IsBare = isBare; string posixPath = NativeMethods.git_repository_path(repo.Handle).MarshallAsString(); string posixWorkingDirectoryPath = NativeMethods.git_repository_workdir(repo.Handle).MarshallAsString(); Path = PosixPathHelper.ToNative(posixPath); WorkingDirectory = PosixPathHelper.ToNative(posixWorkingDirectoryPath); }
internal static IndexEntry CreateFromPtr(Repository repo, IntPtr ptr) { var entry = (GitIndexEntry)Marshal.PtrToStructure(ptr, typeof(GitIndexEntry)); return(new IndexEntry { Path = PosixPathHelper.ToNative(entry.Path), Id = new ObjectId(entry.oid), state = () => repo.Index.RetrieveStatus(entry.Path) }); }
/// <summary> /// Probe for a git repository. /// <para>The lookup start from <paramref name = "startingPath" /> and walk upward parent directories if nothing has been found.</para> /// </summary> /// <param name = "startingPath">The base path where the lookup starts.</param> /// <returns>The path to the git repository.</returns> public static string Discover(string startingPath) { var buffer = new byte[NativeMethods.GIT_PATH_MAX]; int result = NativeMethods.git_repository_discover(buffer, buffer.Length, PosixPathHelper.ToPosix(startingPath), false, null); if ((GitErrorCode)result == GitErrorCode.GIT_ENOTAREPO) { return(null); } Ensure.Success(result); return(PosixPathHelper.ToNative(Utf8Marshaler.Utf8FromBuffer(buffer))); }
/// <summary> /// Initialize a repository at the specified <paramref name = "path" />. /// </summary> /// <param name = "path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param> /// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param> /// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref="Dispose"/> on this instance.</returns> public static Repository Init(string path, bool isBare = false) { Ensure.ArgumentNotNullOrEmptyString(path, "path"); RepositorySafeHandle repo; int res = NativeMethods.git_repository_init(out repo, PosixPathHelper.ToPosix(path), isBare); Ensure.Success(res); string normalizedPath = NativeMethods.git_repository_path(repo).MarshallAsString(); repo.SafeDispose(); string nativePath = PosixPathHelper.ToNative(normalizedPath); return(new Repository(nativePath)); }
/// <summary> /// Retrieves the state of a file in the working directory, comparing it against the staging area and the latest commmit. /// </summary> /// <param name = "filePath">The relative path within the working directory to the file.</param> /// <returns>A <see cref = "FileStatus" /> representing the state of the <paramref name = "filePath" /> parameter.</returns> public FileStatus RetrieveStatus(string filePath) { Ensure.ArgumentNotNullOrEmptyString(filePath, "filePath"); string relativePath = BuildRelativePathFrom(repo, filePath); FileStatus status; int res = NativeMethods.git_status_file(out status, repo.Handle, PosixPathHelper.ToPosix(relativePath)); if (res == (int)GitErrorCode.GIT_ENOTFOUND) { return(FileStatus.Nonexistent); } Ensure.Success(res); return(status); }
/// <summary> /// Gets the <see cref = "IndexEntry" /> with the specified relative path. /// </summary> public IndexEntry this[string path] { get { path = PosixPathHelper.ToPosix(path); Ensure.ArgumentNotNullOrEmptyString(path, "path"); int res = NativeMethods.git_index_find(handle, path); if (res == (int)GitErrorCode.GIT_ENOTFOUND) { return(null); } Ensure.Success(res, true); return(this[(uint)res]); } }
private int StateChanged(string filePath, uint state, IntPtr payload) { filePath = PosixPathHelper.ToNative(filePath); var gitStatus = (FileStatus)state; statusEntries.Add(new StatusEntry(filePath, gitStatus)); foreach (KeyValuePair <FileStatus, Action <RepositoryStatus, string> > kvp in dispatcher) { if (!gitStatus.Has(kvp.Key)) { continue; } kvp.Value(this, filePath); } return(0); }
/// <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)); }
/// <summary> /// Gets the <see cref = "TreeEntry" /> pointed at by the <paramref name = "relativePath" /> in this <see cref = "Tree" /> instance. /// </summary> /// <param name = "relativePath">The relative path to the <see cref = "TreeEntry" /> from this instance.</param> /// <returns><c>null</c> if nothing has been found, the <see cref = "TreeEntry" /> otherwise.</returns> public TreeEntry this[string relativePath] { get { return(RetrieveFromPath(PosixPathHelper.ToPosix(relativePath))); } }