public GitActionResult <GitBranchInfo> CreateBranch(string branchName)
        {
            var result = new GitActionResult <GitBranchInfo>();

            try
            {
                using (var repository = GetRepository())
                {
                    var branch = repository.CreateBranch(branchName, "HEAD");
                    if (branch != null)
                    {
                        result.Item = new GitBranchInfo
                        {
                            CanonicalName = branch.CanonicalName,
                            RemoteName    = branch.Remote?.Name,
                            Name          = branch.FriendlyName,
                            IsRemote      = branch.IsRemote
                        };
                        result.Succeeded = true;
                    }
                    else
                    {
                        result.Succeeded = false;
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Succeeded    = false;
            }
            return(result);
        }
Пример #2
0
        public GitActionResult <string> Commit(string message, bool amend = false, bool signoff = false)
        {
            var result = new GitActionResult <string>();

            using (var repository = GetRepository())
            {
                if (string.IsNullOrEmpty(message))
                {
                    result.Succeeded    = false;
                    result.ErrorMessage = "Commit message must not be null or empty!";
                    //throw new ArgumentException("Commit message must not be null or empty!", "message");
                }
                else
                {
                    Signature author    = repository.Config.BuildSignature(DateTimeOffset.Now);
                    Signature committer = author;

                    CommitOptions opts = new CommitOptions();
                    opts.AmendPreviousCommit = amend;
                    var commit = repository.Commit(message, author, committer, opts);
                    result.Succeeded = true;
                    result.Item      = commit.Sha;
                }
                return(result);
            }
        }
        public GitActionResult <GitBranchInfo> Checkout(GitBranchInfo info, bool force = false)
        {
            using (var repository = GetRepository())
            {
                var result = new GitActionResult <GitBranchInfo>();

                CheckoutOptions options = new CheckoutOptions();
                var             branch  = repository.Branches.FirstOrDefault(
                    x => string.Equals(x.CanonicalName, info.CanonicalName, StringComparison.OrdinalIgnoreCase));

                if (force)
                {
                    options.CheckoutModifiers = CheckoutModifiers.Force;
                }
                try
                {
                    var checkoutBranch = repository.Checkout(branch, options);
                    if (checkoutBranch != null)
                    {
                        result.Item = new GitBranchInfo
                        {
                            CanonicalName = checkoutBranch.CanonicalName,
                            RemoteName    = checkoutBranch.Remote?.Name,
                            Name          = checkoutBranch.FriendlyName,
                            IsRemote      = checkoutBranch.IsRemote
                        };
                        result.Succeeded = true;
                        return(result);
                    }
                    result.Succeeded = false;
                }
                catch (CheckoutConflictException conflict)
                {
                    result.Succeeded    = false;
                    result.ErrorMessage = conflict.Message;
                }

                return(result);
            }
        }