Ejemplo n.º 1
0
        /// <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)));
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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)
            });
        }
Ejemplo n.º 6
0
        /// <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)));
        }
Ejemplo n.º 7
0
        /// <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));
        }
Ejemplo n.º 8
0
        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);
        }