Example #1
0
        private string DownloadFromScp(GerritSettings settings)
        {
            // This is a very quick and dirty "implementation" of the scp
            // protocol. By sending the 0's as input, we trigger scp to
            // send the file.

            string content = GerritUtil.RunGerritCommand(
                _mainForm,
                _gitUiCommands.GitModule,
                "scp -f hooks/commit-msg",
                settings.DefaultRemote,
                new byte[] { 0, 0, 0, 0, 0, 0, 0 }
                );

            // The first line of the output contains the file we're receiving
            // in a format like "C0755 4248 commit-msg".

            if (String.IsNullOrEmpty(content))
            {
                return(null);
            }

            int index = content.IndexOf('\n');

            if (index == -1)
            {
                return(null);
            }

            string header = content.Substring(0, index);

            if (!header.EndsWith(" commit-msg"))
            {
                return(null);
            }

            // This looks like a valid scp response; return the rest of the
            // response.

            content = content.Substring(index + 1);

            // The file should be terminated by a nul.

            index = content.LastIndexOf((char)0);

            Debug.Assert(index == content.Length - 1);

            if (index != -1)
            {
                content = content.Substring(0, index);
            }

            return(content);
        }
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
                return;

            Settings = GerritSettings.Load(Module);

            if (Settings == null)
            {
                Dispose();
                return;
            }

            base.OnLoad(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
                return;

            Settings = GerritSettings.Load(Module);

            if (Settings == null)
            {
                MessageBox.Show(this, "There was a problem loading the .gitreview file. Please review your settings.");
                Dispose();
                return;
            }

            base.OnLoad(e);
        }
Example #4
0
        private async Task InstallCommitMsgHookAsync()
        {
            await _mainForm.SwitchToMainThreadAsync();

            var settings = GerritSettings.Load(_mainForm, _gitUiCommands.GitModule);

            if (settings == null)
            {
                return;
            }

            string path = Path.Combine(
                _gitUiCommands.GitModule.ResolveGitInternalPath("hooks"),
                "commit-msg");

            string content;

            try
            {
                content = await DownloadFromScpAsync(settings);
            }
            catch
            {
                content = null;
            }

            await _mainForm.SwitchToMainThreadAsync();

            if (content == null)
            {
                MessageBox.Show(
                    _mainForm,
                    _installCommitMsgHookFailed.Text,
                    _installCommitMsgHookShortText.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            else
            {
                File.WriteAllText(path, content);

                // Update the cache.

                HaveValidCommitMsgHook(_gitUiCommands.GitModule, true);
            }
        }
Example #5
0
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }

            Settings = GerritSettings.Load(Module);

            if (Settings == null)
            {
                Dispose();
                return;
            }

            base.OnLoad(e);
        }
Example #6
0
        private void InstallCommitMsgHook()
        {
            var settings = GerritSettings.Load(_mainForm, _gitUiCommands.GitModule);

            if (settings == null)
            {
                return;
            }

            string path = Path.Combine(
                _gitUiCommands.GitModule.GetGitDirectory(),
                "hooks",
                "commit-msg"
                );

            string content;

            try
            {
                content = DownloadFromScp(settings);
            }
            catch
            {
                content = null;
            }

            if (content == null)
            {
                MessageBox.Show(
                    _mainForm,
                    _installCommitMsgHookFailed.Text,
                    _installCommitMsgHookShortText.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
            else
            {
                File.WriteAllText(path, content);

                // Update the cache.

                HaveValidCommitMsgHook(_gitUiCommands.GitModule.GetGitDirectory(), true);
            }
        }
Example #7
0
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }

            Settings = GerritSettings.Load();

            if (Settings == null)
            {
                MessageBox.Show(this, "There was a problem loading the .gitreview file. Please review your settings.");
                Dispose();
                return;
            }

            base.OnLoad(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }

            Settings = GerritSettings.Load(Module);

            if (Settings == null)
            {
                Dispose();
                return;
            }

            SetRestClientUrl();
            Version = GetGerritVersion();

            base.OnLoad(e);
        }
Example #9
0
        protected override void OnLoad(EventArgs e)
        {
            if (DesignMode)
                return;

            Settings = GerritSettings.Load();

            if (Settings == null)
            {
                MessageBox.Show(this, "There was a problem loading the .gitreview file. Please review your settings.");
                Dispose();
                return;
            }

            if (!GitCommandHelpers.Plink())
            {
                MessageBox.Show(this, "Ssh must be configured with PuTTY to be able to communicate to communicate with Gerrit.");
                Dispose();
                return;
            }

            base.OnLoad(e);
        }
Example #10
0
        public static GerritSettings Load([CanBeNull] IWin32Window owner, [NotNull] IGitModule aModule)
        {
            if (aModule == null)
            {
                throw new ArgumentNullException(nameof(aModule));
            }

            string path = aModule.WorkingDir + ".gitreview";

            var result = new GerritSettings(aModule);

            try
            {
                if (!File.Exists(path))
                {
                    throw new GerritSettingsException(result._settingsErrorFileNotFound.Text);
                }

                bool inHeader = false;

                foreach (string line in File.ReadLines(path))
                {
                    string trimmed = line.Trim();

                    // Skip empty lines and comments.

                    if (trimmed.Length == 0 || trimmed[0] == '#')
                    {
                        continue;
                    }

                    // Look for the section header.

                    if (trimmed[0] == '[')
                    {
                        inHeader = trimmed.Trim(new[] { '[', ']' }).Equals("gerrit", StringComparison.OrdinalIgnoreCase);
                    }
                    else if (inHeader)
                    {
                        // Split on the = and trim the parts.

                        string[] parts = trimmed.Split(new[] { '=' }, 2).Select(p => p.Trim()).ToArray();

                        // Ignore invalid lines.

                        if (parts.Length != 2 || parts[1].Length == 0)
                        {
                            continue;
                        }

                        // Get the parts of the config file.

                        switch (parts[0].ToLowerInvariant())
                        {
                        case "host": result.Host = parts[1]; break;

                        case "project": result.Project = parts[1]; break;

                        case "defaultbranch": result.DefaultBranch = parts[1]; break;

                        case "defaultremote": result.DefaultRemote = parts[1]; break;

                        case "defaultrebase": result.DefaultRebase = !parts[1].Equals("0"); break;

                        case "port":
                            if (!int.TryParse(parts[1], out var value))
                            {
                                throw new GerritSettingsException(result._settingsErrorPortNotNumeric.Text);
                            }

                            result.Port = value;
                            break;
                        }
                    }
                }

                result.Validate();
            }
            catch (GerritSettingsException ex)
            {
                MessageBox.Show(owner, ex.Message, result._settingsError.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return(null);
            }

            return(result);
        }
Example #11
0
        public static GerritSettings Load([NotNull] IGitModule aModule)
        {
            if (aModule == null)
                throw new ArgumentNullException("aModule");

            string path = aModule.GitWorkingDir + ".gitreview";

            if (!File.Exists(path))
                throw new GerritSettingsException(_settingsErrorFileNotFound.Text);

            bool inHeader = false;
            var result = new GerritSettings(aModule);

            foreach (string line in File.ReadLines(path))
            {
                string trimmed = line.Trim();

                // Skip empty lines and comments.

                if (trimmed.Length == 0 || trimmed[0] == '#')
                    continue;

                // Look for the section header.

                if (trimmed[0] == '[')
                {
                    inHeader = trimmed.Trim(new[] { '[', ']' }).Equals("gerrit", StringComparison.OrdinalIgnoreCase);
                }
                else if (inHeader)
                {
                    // Split on the = and trim the parts.

                    string[] parts = trimmed.Split(new[] { '=' }, 2).Select(p => p.Trim()).ToArray();

                    // Ignore invalid lines.

                    if (parts.Length != 2 || parts[1].Length == 0)
                        continue;

                    // Get the parts of the config file.

                    switch (parts[0].ToLowerInvariant())
                    {
                        case "host": result.Host = parts[1]; break;
                        case "project": result.Project = parts[1]; break;
                        case "defaultbranch": result.DefaultBranch = parts[1]; break;
                        case "defaultremote": result.DefaultRemote = parts[1]; break;
                        case "defaultrebase": result.DefaultRebase = !parts[1].Equals("0"); break;

                        case "port":
                            int value;
                            if (!int.TryParse(parts[1], out value))
                                throw new GerritSettingsException(_settingsErrorPortNotNumeric.Text);
                            result.Port = value;
                            break;
                    }
                }
            }

            result.Validate();

            return result;
        }
Example #12
0
        private async Task InstallCommitMsgHookAsync()
        {
            await _mainForm.SwitchToMainThreadAsync();

            var settings = GerritSettings.Load(_mainForm, _gitUiCommands.GitModule);

            if (settings == null)
            {
                return;
            }

            var hooksFolderPath = _gitUiCommands.GitModule.ResolveGitInternalPath(HooksFolderName);

            if (!Directory.Exists(hooksFolderPath))
            {
                try
                {
                    Directory.CreateDirectory(hooksFolderPath);
                }
                catch
                {
                    MessageBox.Show(
                        _mainForm,
                        _installCommitMsgHookFolderCreationFailed.Text,
                        _installCommitMsgHookShortText.Text,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    return;
                }
            }

            var commitMessageHookPath = Path.Combine(hooksFolderPath, CommitMessageHookFileName);

            string content;

            try
            {
                content = await DownloadFromScpAsync(settings);
            }
            catch
            {
                content = null;
            }

            await _mainForm.SwitchToMainThreadAsync();

            if (content == null)
            {
                MessageBox.Show(
                    _mainForm,
                    _installCommitMsgHookDownloadFileFailed.Text,
                    _installCommitMsgHookShortText.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            else
            {
                File.WriteAllText(commitMessageHookPath, content);

                // Update the cache.

                HaveValidCommitMsgHook(_gitUiCommands.GitModule, true);
            }
        }
Example #13
0
            public static GerritSettings Load()
            {
                string path = GitCommands.GitModule.CurrentWorkingDir + ".gitreview";

                if (!File.Exists(path))
                {
                    return(null);
                }

                bool inHeader = false;
                var  result   = new GerritSettings();

                foreach (string line in File.ReadAllLines(path))
                {
                    string trimmed = line.Trim();

                    // Skip empty lines and comments.

                    if (trimmed.Length == 0 || trimmed[0] == '#')
                    {
                        continue;
                    }

                    // Look for the section header.

                    if (trimmed[0] == '[')
                    {
                        inHeader = trimmed.Trim(new[] { '[', ']' }).Equals("gerrit", StringComparison.OrdinalIgnoreCase);
                    }
                    else if (inHeader)
                    {
                        // Split on the = and trim the parts.

                        string[] parts = trimmed.Split(new[] { '=' }, 2).Select(p => p.Trim()).ToArray();

                        // Ignore invalid lines.

                        if (parts.Length != 2 || parts[1].Length == 0)
                        {
                            continue;
                        }

                        // Get the parts of the config file.

                        switch (parts[0].ToLowerInvariant())
                        {
                        case "host": result.Host = parts[1]; break;

                        case "project": result.Project = parts[1]; break;

                        case "defaultbranch": result.DefaultBranch = parts[1]; break;

                        case "defaultremote": result.DefaultRemote = parts[1]; break;

                        case "defaultrebase": result.DefaultRebase = !parts[1].Equals("0"); break;

                        case "port":
                            int value;
                            if (!int.TryParse(parts[1], out value))
                            {
                                return(null);
                            }
                            result.Port = value;
                            break;
                        }
                    }
                }

                if (!result.IsValid())
                {
                    return(null);
                }

                return(result);
            }
Example #14
0
            public static GerritSettings Load()
            {
                string path = GitCommands.Settings.WorkingDir + ".gitreview";

                if (!File.Exists(path))
                    return null;

                bool inHeader = false;
                var result = new GerritSettings();

                foreach (string line in File.ReadAllLines(path))
                {
                    string trimmed = line.Trim();

                    // Skip empty lines and comments.

                    if (trimmed.Length == 0 || trimmed[0] == '#')
                        continue;

                    // Look for the section header.

                    if (trimmed[0] == '[')
                    {
                        inHeader = trimmed.Trim(new[] { '[', ']' }).Equals("gerrit", StringComparison.OrdinalIgnoreCase);
                    }
                    else if (inHeader)
                    {
                        // Split on the = and trim the parts.

                        string[] parts = trimmed.Split(new[] { '=' }, 2).Select(p => p.Trim()).ToArray();

                        // Ignore invalid lines.

                        if (parts.Length != 2 || parts[1].Length == 0)
                            continue;

                        // Get the parts of the config file.

                        switch (parts[0].ToLowerInvariant())
                        {
                            case "host": result.Host = parts[1]; break;
                            case "project": result.Project = parts[1]; break;
                            case "defaultbranch": result.DefaultBranch = parts[1]; break;
                            case "defaultremote": result.DefaultRemote = parts[1]; break;
                            case "defaultrebase": result.DefaultRebase = !parts[1].Equals("0"); break;

                            case "port":
                                int value;
                                if (!int.TryParse(parts[1], out value))
                                    return null;
                                result.Port = value;
                                break;
                        }
                    }
                }

                if (!result.IsValid())
                    return null;

                return result;
            }