Example #1
0
        public bool TryCreateBranch()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var branchName = View.BranchName.Value.Trim();

            if (!GitControllerUtility.ValidateNewBranchName(branchName, StashedState.Repository, View.BranchName, View.ErrorNotifier))
            {
                return(false);
            }

            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    StashedState.ToBranch(branchName);
                }
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    string.Format(Resources.ErrFailedToCreateBranch, branchName),
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }

            return(true);
        }
Example #2
0
        public bool TryCreateBranch()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var branchName = View.BranchName.Value.Trim();
            var refspec    = View.StartingRevision.Value.Trim();
            var checkout   = View.Checkout.Value;
            var orphan     = checkout && View.Orphan.Value && GitFeatures.CheckoutOrphan.IsAvailableFor(Repository);
            var reflog     = View.CreateReflog.Value;
            var existent   = Repository.Refs.Heads.TryGetItem(branchName);

            if (!GitControllerUtility.ValidateBranchName(branchName, View.BranchName, View.ErrorNotifier))
            {
                return(false);
            }
            if (!GitControllerUtility.ValidateRefspec(refspec, View.StartingRevision, View.ErrorNotifier))
            {
                return(false);
            }
            if (existent != null)
            {
                return(TryResetExistingBranch(branchName, refspec, checkout, existent));
            }
            else
            {
                return(TryCreateNewBranch(branchName, refspec, checkout, orphan, reflog));
            }
        }
Example #3
0
        public bool TryInit()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var repositoryPath = View.RepositoryPath.Value.Trim();

            if (!GitControllerUtility.ValidateAbsolutePath(repositoryPath, View.RepositoryPath, View.ErrorNotifier))
            {
                return(false);
            }
            string template = null;

            if (View.UseCustomTemplate.Value)
            {
                template = View.Template.Value.Trim();
                if (!GitControllerUtility.ValidateAbsolutePath(template, View.Template, View.ErrorNotifier))
                {
                    return(false);
                }
            }
            bool bare = View.Bare.Value;

            try
            {
                if (!Directory.Exists(repositoryPath))
                {
                    Directory.CreateDirectory(repositoryPath);
                }
            }
            catch (Exception exc) when(!exc.IsCritical())
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    Resources.ErrFailedToCreateDirectory,
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    Repository.Init(GitRepositoryProvider.GitAccessor, repositoryPath, template, bare);
                }
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    Resources.ErrFailedToInit,
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Example #4
0
        public bool TryRename()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var repository = Branch.Repository;
            var oldName    = Branch.Name;
            var newName    = View.NewName.Value.Trim();

            if (oldName == newName)
            {
                return(true);
            }
            if (!GitControllerUtility.ValidateNewBranchName(newName, repository, View.NewName, View.ErrorNotifier))
            {
                return(false);
            }

            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    Branch.Name = newName;
                }
            }
            catch (BranchAlreadyExistsException)
            {
                View.ErrorNotifier.NotifyError(View.NewName,
                                               new UserInputError(
                                                   Resources.ErrInvalidBranchName,
                                                   Resources.ErrBranchAlreadyExists));
                return(false);
            }
            catch (InvalidBranchNameException exc)
            {
                View.ErrorNotifier.NotifyError(View.NewName,
                                               new UserInputError(
                                                   Resources.ErrInvalidBranchName,
                                                   exc.Message));
                return(false);
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    string.Format(Resources.ErrFailedToRenameBranch, oldName),
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Example #5
0
        public bool TryAddNote()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var revision = View.Revision.Value;
            var message  = View.Message.Value;

            if (!GitControllerUtility.ValidateRefspec(revision, View.Revision, View.ErrorNotifier))
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(message))
            {
                View.ErrorNotifier.NotifyError(View.Message,
                                               new UserInputError(
                                                   Resources.ErrInvalidMessage,
                                                   Resources.ErrMessageCannotBeEmpty));
                return(false);
            }
            revision = revision.Trim();
            message  = message.Trim();
            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    var ptr = Repository.GetRevisionPointer(revision);
                    ptr.AddNote(message);
                }
            }
            catch (UnknownRevisionException)
            {
                View.ErrorNotifier.NotifyError(View.Revision,
                                               new UserInputError(
                                                   Resources.ErrInvalidRevisionExpression,
                                                   Resources.ErrRevisionIsUnknown));
                return(false);
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    Resources.ErrFailedToAddNote,
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Example #6
0
        public bool TryAddSubmodule()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var path = View.Path.Value.Trim();

            if (!GitControllerUtility.ValidateRelativePath(path, View.Path, View.ErrorNotifier))
            {
                return(false);
            }
            var url = View.Url.Value.Trim();

            if (!GitControllerUtility.ValidateUrl(url, View.Url, View.ErrorNotifier))
            {
                return(false);
            }
            string branch = null;

            if (View.UseCustomBranch.Value)
            {
                branch = View.BranchName.Value.Trim();
                if (!GitControllerUtility.ValidateBranchName(branch, View.BranchName, View.ErrorNotifier))
                {
                    return(false);
                }
            }
            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    _repository.Submodules.Create(path, url, branch);
                }
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    string.Format(Resources.ErrFailedToAddSubmodule, path),
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Example #7
0
        public bool TryClone()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var url = View.Url.Value;

            if (!GitControllerUtility.ValidateUrl(url, View.Url, View.ErrorNotifier))
            {
                return(false);
            }
            var path = View.RepositoryPath.Value.Trim();

            if (!GitControllerUtility.ValidateAbsolutePath(path, View.RepositoryPath, View.ErrorNotifier))
            {
                return(false);
            }
            var remoteName = View.RemoteName.Value;

            if (!GitControllerUtility.ValidateRemoteName(remoteName, View.RemoteName, View.ErrorNotifier))
            {
                return(false);
            }
            url = url.Trim();
            bool   shallow  = View.ShallowClone.Value;
            int    depth    = shallow ? View.Depth.Value : -1;
            string template = View.UseTemplate.Value ? View.TemplatePath.Value.Trim() : null;

            if (!string.IsNullOrWhiteSpace(template) && !GitControllerUtility.ValidateAbsolutePath(template, View.TemplatePath, View.ErrorNotifier))
            {
                return(false);
            }

            bool bare       = View.Bare.Value;
            bool mirror     = bare && View.Mirror.Value;
            bool noCheckout = View.NoCheckout.Value;
            bool recursive  = View.Recursive.Value;

            var status = GuiCommands.Clone(View as IWin32Window,
                                           GitRepositoryProvider.GitAccessor,
                                           url, path, template, remoteName,
                                           shallow, depth, bare, mirror, recursive, noCheckout);

            return(status == GuiCommandStatus.Completed);
        }
Example #8
0
        public bool TryAddRemote()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var name = View.RemoteName.Value;

            if (!GitControllerUtility.ValidateNewRemoteName(name, Repository, View.RemoteName, View.ErrorNotifier))
            {
                return(false);
            }
            var url = View.Url.Value;

            if (!GitControllerUtility.ValidateUrl(url, View.Url, View.ErrorNotifier))
            {
                return(false);
            }
            name = name.Trim();
            url  = url.Trim();
            var fetch        = View.Fetch.Value;
            var mirror       = View.Mirror.Value;
            var tagFetchMode = View.TagFetchMode.Value;

            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    Repository.Remotes.AddRemote(name, url, fetch, mirror, tagFetchMode);
                }
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    Resources.ErrFailedToAddRemote,
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Example #9
0
        public bool TryCreateTag()
        {
            Verify.State.IsTrue(View != null, "Controller is not attached to a view.");

            var tagName = View.TagName.Value.Trim();
            var refspec = View.Revision.Value.Trim();

            if (!GitControllerUtility.ValidateNewTagName(tagName, Repository, View.TagName, View.ErrorNotifier))
            {
                return(false);
            }
            if (!GitControllerUtility.ValidateRefspec(refspec, View.Revision, View.ErrorNotifier))
            {
                return(false);
            }

            string message   = null;
            bool   signed    = View.Signed.Value;
            bool   annotated = signed || View.Annotated.Value;

            if (annotated)
            {
                message = View.Message.Value;
                if (string.IsNullOrWhiteSpace(message))
                {
                    View.ErrorNotifier.NotifyError(View.Message,
                                                   new UserInputError(
                                                       Resources.ErrNoMessageSpecified,
                                                       Resources.ErrMessageCannotBeEmpty));
                    return(false);
                }
                message = message.Trim();
            }
            string keyId = null;

            if (signed)
            {
                if (View.UseKeyId.Value)
                {
                    keyId = View.KeyId.Value;
                    if (string.IsNullOrWhiteSpace(keyId))
                    {
                        View.ErrorNotifier.NotifyError(View.KeyId,
                                                       new UserInputError(
                                                           Resources.ErrNoKeyIdSpecified,
                                                           Resources.ErrKeyIdCannotBeEmpty));
                        return(false);
                    }
                    keyId = keyId.Trim();
                }
            }
            try
            {
                using (View.ChangeCursor(MouseCursor.WaitCursor))
                {
                    var ptr = Repository.GetRevisionPointer(refspec);
                    if (annotated)
                    {
                        if (signed)
                        {
                            if (keyId == null)
                            {
                                Repository.Refs.Tags.Create(tagName, ptr, message, true);
                            }
                            else
                            {
                                Repository.Refs.Tags.Create(tagName, ptr, message, keyId);
                            }
                        }
                        else
                        {
                            Repository.Refs.Tags.Create(tagName, ptr, message, false);
                        }
                    }
                    else
                    {
                        Repository.Refs.Tags.Create(tagName, ptr);
                    }
                }
            }
            catch (TagAlreadyExistsException)
            {
                View.ErrorNotifier.NotifyError(View.TagName,
                                               new UserInputError(
                                                   Resources.ErrInvalidTagName,
                                                   Resources.ErrTagAlreadyExists));
                return(false);
            }
            catch (UnknownRevisionException)
            {
                View.ErrorNotifier.NotifyError(View.Revision,
                                               new UserInputError(
                                                   Resources.ErrInvalidRevisionExpression,
                                                   Resources.ErrRevisionIsUnknown));
                return(false);
            }
            catch (InvalidTagNameException exc)
            {
                View.ErrorNotifier.NotifyError(View.TagName,
                                               new UserInputError(
                                                   Resources.ErrInvalidTagName,
                                                   exc.Message));
                return(false);
            }
            catch (GitException exc)
            {
                GitterApplication.MessageBoxService.Show(
                    View as IWin32Window,
                    exc.Message,
                    string.Format(Resources.ErrFailedToCreateTag, tagName),
                    MessageBoxButton.Close,
                    MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }