Beispiel #1
0
        /// <summary>
        /// Returns the default remote for push operation.
        /// </summary>
        /// <returns>The <see cref="GitRef.Name"/> if found, otheriwse <see langword="null"/>.</returns>
        // TODO: moved verbatim from FormPush.cs, perhaps needs refactoring
        public string GetDefaultPushRemote(GitRemote remote, string branch)
        {
            if (remote == null)
            {
                throw new ArgumentNullException(nameof(remote));
            }

            var module = GetModule();

            bool IsSettingForBranch(string setting, string branchName)
            {
                var head = new GitRef(module, string.Empty, setting);

                return(head.IsHead && head.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase));
            }

            var remoteHead = remote.Push
                             .Select(s => s.Split(':'))
                             .Where(t => t.Length == 2)
                             .Where(t => IsSettingForBranch(t[0], branch))
                             .Select(t => new GitRef(module, string.Empty, t[1]))
                             .FirstOrDefault(h => h.IsHead);

            return(remoteHead?.Name);
        }
Beispiel #2
0
        /// <summary>
        ///   Saves the remote details by creating a new or updating an existing remote entry in .git/config file.
        /// </summary>
        /// <param name="remote">An existing remote instance or <see langword="null"/> if creating a new entry.</param>
        /// <param name="remoteName">
        ///   <para>The remote name.</para>
        ///   <para>If updating an existing remote and the name changed, it will result in remote name change and prompt for "remote update".</para>
        /// </param>
        /// <param name="remoteUrl">
        ///   <para>The remote URL.</para>
        ///   <para>If updating an existing remote and the URL changed, it will result in remote URL change and prompt for "remote update".</para>
        /// </param>
        /// <param name="remotePushUrl">An optional alternative remote push URL.</param>
        /// <param name="remotePuttySshKey">An optional Putty SSH key.</param>
        /// <returns>Result of the operation.</returns>
        public GitRemoteSaveResult SaveRemote(GitRemote remote, string remoteName, string remoteUrl, string remotePushUrl, string remotePuttySshKey)
        {
            if (string.IsNullOrWhiteSpace(remoteName))
            {
                throw new ArgumentNullException(nameof(remoteName));
            }

            remoteName = remoteName.Trim();

            // if create a new remote or updated the url - we may need to perform "update remote"
            bool updateRemoteRequired = false;

            // if operation return anything back, relay that to the user
            var output = string.Empty;

            var  module         = GetModule();
            bool creatingNew    = remote == null;
            bool remoteDisabled = false;

            if (creatingNew)
            {
                output = module.AddRemote(remoteName, remoteUrl);
                updateRemoteRequired = true;
            }
            else
            {
                if (remote.Disabled)
                {
                    // disabled branches can't updated as it poses to many problems, i.e.
                    // - verify that the branch name is valid, and
                    // - it does not duplicate an active branch name etc.
                    return(new GitRemoteSaveResult(null, false));
                }

                remoteDisabled = remote.Disabled;
                if (!string.Equals(remote.Name, remoteName, StringComparison.OrdinalIgnoreCase))
                {
                    // the name of the remote changed - perform rename
                    output = module.RenameRemote(remote.Name, remoteName);
                }

                if (!string.Equals(remote.Url, remoteUrl, StringComparison.OrdinalIgnoreCase))
                {
                    // the remote url changed - we may need to update remote
                    updateRemoteRequired = true;
                }
            }

            UpdateSettings(module, remoteName, remoteDisabled, SettingKeyString.RemoteUrl, remoteUrl);
            UpdateSettings(module, remoteName, remoteDisabled, SettingKeyString.RemotePushUrl, remotePushUrl);
            UpdateSettings(module, remoteName, remoteDisabled, SettingKeyString.RemotePuttySshKey, remotePuttySshKey);

            return(new GitRemoteSaveResult(output, updateRemoteRequired));
        }
        /// <summary>
        /// Removes the specified remote from .git/config file.
        /// </summary>
        /// <param name="remote">Remote to remove.</param>
        /// <returns>Output of <see cref="IGitModule.RemoveRemote"/> operation, if the remote is active; otherwise <see cref="string.Empty"/>.</returns>
        public string RemoveRemote(GitRemote remote)
        {
            if (remote == null)
            {
                throw new ArgumentNullException(nameof(remote));
            }

            if (!remote.Disabled)
            {
                return(_module.RemoveRemote(remote.Name));
            }

            var sectionName = $"{DisabledSectionPrefix}{SectionRemote}.{remote.Name}";

            _module.LocalConfigFile.RemoveConfigSection(sectionName, true);
            return(string.Empty);
        }
        /// <summary>
        /// Returns the default remote for push operation.
        /// </summary>
        /// <param name="remote"></param>
        /// <param name="branch"></param>
        /// <returns>The <see cref="GitRef.Name"/> if found, otheriwse <see langword="null"/>.</returns>
        // TODO: moved verbatim from FormPush.cs, perhaps needs refactoring
        public string GetDefaultPushRemote(GitRemote remote, string branch)
        {
            if (remote == null)
            {
                throw new ArgumentNullException("remote");
            }

            Func <string, string, bool> isSettingForBranch = (setting, branchName) =>
            {
                var head = new GitRef(_module, string.Empty, setting);
                return(head.IsHead && head.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase));
            };

            var remoteHead = remote.Push
                             .Select(s => s.Split(':'))
                             .Where(t => t.Length == 2)
                             .Where(t => isSettingForBranch(t[0], branch))
                             .Select(t => new GitRef(_module, string.Empty, t[1]))
                             .FirstOrDefault(h => h.IsHead);

            return(remoteHead == null ? null : remoteHead.Name);
        }