internal static async Task <GitActionResult <string> > Commit(GitRepository repository, string message, bool signoff = false)
        {
            var result = new GitActionResult <string>();

            SolutionExtensions.WriteMessageToOutputPane("Commiting");

            if (String.IsNullOrWhiteSpace(message))
            {
                result.Succeeded    = false;
                result.ErrorMessage = ErrorMessages.CommitMissingComment;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment);
                return(result);
            }

            var stagedCount = repository?.ChangedFiles.Count(f => f.IsStaged) ?? 0;

            if (stagedCount <= 0)
            {
                result.Succeeded    = false;
                result.ErrorMessage = ErrorMessages.CommitNoFilesStaged;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitNoFilesStaged);
                return(result);
            }

            result = repository?.Commit(message, false, signoff);
            if (result.Succeeded)
            {
                SolutionExtensions.WriteMessageToOutputPane("Commit successfully. Commit Hash: " + result.Item);
            }
            return(result);
        }
        internal static async Task<GitActionResult<string>> Commit(GitRepository repository, string message, bool signoff = false)
        {
            var result = new GitActionResult<string>();

            SolutionExtensions.WriteMessageToOutputPane("Commiting");

            if (String.IsNullOrWhiteSpace(message))
            {
                result.Succeeded = false;
                result.ErrorMessage = ErrorMessages.CommitMissingComment;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment);
                return result;
            }

            var stagedCount = repository?.ChangedFiles.Count(f => f.IsStaged) ?? 0;

            if (stagedCount <= 0)
            {
                result.Succeeded = false;
                result.ErrorMessage = ErrorMessages.CommitNoFilesStaged;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitNoFilesStaged);
                return result;
            }

            result = repository?.Commit(message, false, signoff);
            if (result.Succeeded)
            {
                SolutionExtensions.WriteMessageToOutputPane("Commit successfully. Commit Hash: " + result.Item);
            }
            return result;

        }
        internal static async Task <GitActionResult <string> > AmendCommit(GitRepository repository, string message, bool signoff = false)
        {
            var result = new GitActionResult <string>();

            SolutionExtensions.WriteMessageToOutputPane("Amending Commiti");

            if (String.IsNullOrWhiteSpace(message))
            {
                result.Succeeded    = false;
                result.ErrorMessage = ErrorMessages.CommitMissingComment;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment);
                return(result);
            }

            result = repository?.Commit(message, true, signoff);
            if (result.Succeeded)
            {
                SolutionExtensions.WriteMessageToOutputPane("Amend last commit successfully. Commit Hash: " + result.Item);
            }
            return(result);
        }
        internal static async Task<GitActionResult<string>> AmendCommit(GitRepository repository, string message, bool signoff = false)
        {
            var result = new GitActionResult<string>();

            SolutionExtensions.WriteMessageToOutputPane("Amending Commiti");

            if (String.IsNullOrWhiteSpace(message))
            {
                result.Succeeded = false;
                result.ErrorMessage = ErrorMessages.CommitMissingComment;
                SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment);
                return result;
            }

            result = repository?.Commit(message, true, signoff);
            if (result.Succeeded)
            {
                SolutionExtensions.WriteMessageToOutputPane("Amend last commit successfully. Commit Hash: " + result.Item);
            }
            return result;

        }
        internal static async Task<GitActionResult<GitBranchInfo>> SwitchCommand(SwitchBranchInfo result)
        {
            if (!result.CreateBranch && !result.Switch)
            {
                return new GitActionResult<GitBranchInfo>() { ErrorMessage = "No Branch Operation", Succeeded = false };
            }

            GitActionResult<GitBranchInfo> branchResult = new GitActionResult<GitBranchInfo>();
            bool inError = false;
            var branch = result.BranchInfo;
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
            SolutionExtensions.WriteMessageToOutputPane("Branch Operation Started");
            if (result.CreateBranch)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                SolutionExtensions.WriteMessageToOutputPane("Creating Branch");
                await TaskScheduler.Default;
                branchResult = result.Repository.CreateBranch(result.BranchName);
                if (branchResult.Succeeded)
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    SolutionExtensions.WriteMessageToOutputPane("Branch: ' " + branchResult.Item.Name + "' Created");
                    branch = branchResult.Item;
                }
                else
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    inError = true;
                    SolutionExtensions.WriteMessageToOutputPane(branchResult.ErrorMessage);
                }
            }
            if (result.Switch && !inError)
            {
                if (branch != null)
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    SolutionExtensions.WriteMessageToOutputPane("Switching Branch");
                    await TaskScheduler.Default;
                    if (branch.IsRemote)
                    {
                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                        SolutionExtensions.WriteMessageToOutputPane("Creating Local Branch");
                        var createResult = result.Repository.CreateBranch(branch.Name.Remove(0,branch.RemoteName.Length +1),branch.Sha);
                        if (!createResult.Succeeded)
                        {
                            return createResult;
                        }
                        result.Repository.SetRemoteBranch(createResult.Item,branch.RemoteName);
                        branchResult = result.Repository.Checkout(createResult.Item);
                    }
                    else
                    {
                        branchResult = result.Repository.Checkout(branch);
                    }
                    
                    if (!branchResult.Succeeded)
                    {
                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                        inError = true;
                        SolutionExtensions.WriteMessageToOutputPane(branchResult.ErrorMessage);
                    }
                }
            }

            if (!inError)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                SolutionExtensions.WriteMessageToOutputPane("Branch Operation Complete");
            }
            return branchResult;
        }
        internal static async Task <GitActionResult <GitBranchInfo> > SwitchCommand(SwitchBranchInfo result)
        {
            if (!result.CreateBranch && !result.Switch)
            {
                return(new GitActionResult <GitBranchInfo>()
                {
                    ErrorMessage = "No Branch Operation", Succeeded = false
                });
            }

            GitActionResult <GitBranchInfo> branchResult = new GitActionResult <GitBranchInfo>();
            bool inError = false;
            var  branch  = result.BranchInfo;
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            SolutionExtensions.WriteMessageToOutputPane("Branch Operation Started");
            if (result.CreateBranch)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                SolutionExtensions.WriteMessageToOutputPane("Creating Branch");
                await TaskScheduler.Default;
                branchResult = result.Repository.CreateBranch(result.BranchName);
                if (branchResult.Succeeded)
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    SolutionExtensions.WriteMessageToOutputPane("Branch: ' " + branchResult.Item.Name + "' Created");
                    branch = branchResult.Item;
                }
                else
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    inError = true;
                    SolutionExtensions.WriteMessageToOutputPane(branchResult.ErrorMessage);
                }
            }
            if (result.Switch && !inError)
            {
                if (branch != null)
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    SolutionExtensions.WriteMessageToOutputPane("Switching Branch");
                    await TaskScheduler.Default;
                    if (branch.IsRemote)
                    {
                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                        SolutionExtensions.WriteMessageToOutputPane("Creating Local Branch");
                        var createResult = result.Repository.CreateBranch(branch.Name.Remove(0, branch.RemoteName.Length + 1), branch.Sha);
                        if (!createResult.Succeeded)
                        {
                            return(createResult);
                        }
                        result.Repository.SetRemoteBranch(createResult.Item, branch.RemoteName);
                        branchResult = result.Repository.Checkout(createResult.Item);
                    }
                    else
                    {
                        branchResult = result.Repository.Checkout(branch);
                    }

                    if (!branchResult.Succeeded)
                    {
                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                        inError = true;
                        SolutionExtensions.WriteMessageToOutputPane(branchResult.ErrorMessage);
                    }
                }
            }

            if (!inError)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                SolutionExtensions.WriteMessageToOutputPane("Branch Operation Complete");
            }
            return(branchResult);
        }
 public GitActionResult<GitBranchInfo> CreateBranch(string branchName, string commitish = "HEAD")
 {
     var result = new GitActionResult<GitBranchInfo>();
     try
     {
         using (var repository = GetRepository())
         {
             var branch = repository.CreateBranch(branchName, commitish);
             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;
 }
        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;
            }
        }