示例#1
0
        /// <summary>
        /// Resolve a rev to a shortened commit sha1
        /// </summary>
        ///
        /// <param name="minimumLength">
        /// Minimum length of the shortened sha1, or <c>0</c> for Git to choose automatically
        /// </param>
        ///
        public GitShortSha1 GetShortCommitId(GitRev rev, int minimumLength = 0)
        {
            Guard.NotNull(rev, nameof(rev));

            if (minimumLength < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minimumLength));
            }
            if (0 < minimumLength && minimumLength < 4)
            {
                throw new ArgumentOutOfRangeException(nameof(minimumLength));
            }

            var length = minimumLength == 0 ? "auto" : minimumLength.ToString();

            var r = ProcessExtensions.ExecuteCaptured(false, false, null, "git", "-C", Path,
                                                      "rev-parse", $"--short={length}", rev);

            if (r.ExitCode != 0)
            {
                throw new GitException("Resolve rev to short commit sha1 failed", r);
            }

            return(new GitShortSha1(r.StandardOutput.Trim()));
        }
示例#2
0
        /// <summary>
        /// List commit IDs from one commit to another
        /// </summary>
        ///
        /// <remarks>
        /// <paramref name="from"/> is not included in the output, but <paramref name="to"/> is.
        /// </remarks>
        ///
        /// <param name="from">
        /// The commit to list from, or <c>null</c> to list from the beginning of history
        /// </param>
        ///
        /// <param name="to">
        /// The commit to list to
        /// </param>
        ///
        /// <returns>
        /// The list of IDs of commits between <paramref name="from"/> and <paramref name="to"/> in (more or less)
        /// chronological order
        /// </returns>
        ///
        public IEnumerable <GitSha1> ListCommits(GitRev from, GitRev to)
        {
            Guard.NotNull(to, nameof(to));

            var rev = new GitRev(from != null ? $"{from}..{to}" : to);

            return
                (RevList(-1, rev)
                 .Select(commit => commit.Sha1)
                 .Reverse());
        }
示例#3
0
        /// <summary>
        /// Create a branch
        /// </summary>
        ///
        public void CreateBranch(GitRefNameComponent name, GitRev target)
        {
            Guard.NotNull(name, nameof(name));
            Guard.NotNull(target, nameof(target));

            var r = ProcessExtensions.ExecuteCaptured(false, false, null, "git", "-C", Path, "branch", name, target);

            if (r.ExitCode != 0)
            {
                throw new GitException("Create branch failed", r);
            }
        }
示例#4
0
        /// <summary>
        /// Resolve a rev to a commit sha1
        /// </summary>
        ///
        public GitSha1 GetCommitId(GitRev rev)
        {
            Guard.NotNull(rev, nameof(rev));

            var r = ProcessExtensions.ExecuteCaptured(false, false, null, "git", "-C", Path,
                                                      "rev-parse", "-q", "--verify", $"{rev}^{{commit}}");

            if (r.ExitCode != 0)
            {
                throw new GitException("Resolve rev to commit sha1 failed", r);
            }

            return(new GitSha1(r.StandardOutput.Trim()));
        }
示例#5
0
        /// <summary>
        /// Try resolving a rev to a commit sha1
        /// </summary>
        ///
        public bool TryGetCommitId(GitRev rev, out GitSha1 sha1)
        {
            Guard.NotNull(rev, nameof(rev));

            try
            {
                sha1 = GetCommitId(rev);
                return(true);
            }
            catch (GitException)
            {
                sha1 = default;
                return(false);
            }
        }
示例#6
0
        /// <summary>
        /// Check out a particular commit
        /// </summary>
        ///
        /// <exception cref="InvalidOperationException">
        /// The repository has uncommitted changes
        /// </exception>
        ///
        /// <exception cref="GitException">
        /// The checkout operation failed
        /// </exception>
        ///
        public void Checkout(GitRev rev)
        {
            Guard.NotNull(rev, nameof(rev));

            if (HasUncommittedChanges())
            {
                throw new InvalidOperationException("Repository contains uncommitted changes");
            }

            var r = ProcessExtensions.ExecuteCaptured(false, false, null, "git", "-C", Path, "checkout", rev);

            if (r.ExitCode != 0)
            {
                throw new GitException("Checkout failed", r);
            }
        }
示例#7
0
        /// <summary>
        /// Is one commit the ancestor of another?
        /// </summary>
        ///
        public bool IsAncestor(GitRev ancestor, GitRev descendent)
        {
            Guard.NotNull(ancestor, nameof(ancestor));
            Guard.NotNull(descendent, nameof(descendent));

            var r = ProcessExtensions.ExecuteCaptured(false, false, null,
                                                      "git", "-C", Path, "merge-base", "--is-ancestor", ancestor, descendent);

            switch (r.ExitCode)
            {
            case 0:
                return(true);

            case 1:
                return(false);

            default:
                throw new GitException("merge-base --is-ancestor failed", r);
            }
        }
示例#8
0
 /// <summary>
 /// Get a commit's message
 /// </summary>
 ///
 public string GetCommitMessage(GitRev rev) =>
 RevList(1, rev).Single().Message;
示例#9
0
 /// <summary>
 /// Get a commit's commit date
 /// </summary>
 ///
 public DateTimeOffset GetCommitterDate(GitRev rev) =>
 RevList(1, rev).Single().CommitDate;
示例#10
0
 /// <summary>
 /// Calculate the distance in commits from one commit to another
 /// </summary>
 ///
 /// <param name="from">
 /// The commit to measure from, or <c>null</c> to measure from the beginning of history
 /// </param>
 ///
 /// <param name="to">
 /// The commit to measure to
 /// </param>
 ///
 public int Distance(GitRev from, GitRev to)
 {
     return(ListCommits(from, to).Count());
 }
示例#11
0
 /// <summary>
 /// Calculate the distance in commits from the beginning of revision history
 /// </summary>
 ///
 /// <param name="to">
 /// The commit to measure to
 /// </param>
 ///
 public int Distance(GitRev to)
 {
     return(Distance(null, to));
 }
示例#12
0
 /// <summary>
 /// Determine whether a rev resolves to a commit in the repository
 /// </summary>
 ///
 public bool Exists(GitRev rev)
 {
     return(TryGetCommitId(rev, out var _));
 }
示例#13
0
 public IEnumerable <GitCommitInfo> RevList(int maxCount, GitRev rev)
 {
     return(RevList(maxCount, new[] { rev }));
 }
示例#14
0
 public IEnumerable <GitCommitInfo> RevList(GitRev rev)
 {
     return(RevList(-1, rev));
 }