コード例 #1
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command against
        /// the Mercurial repository.
        /// </summary>
        /// <param name="repositoryPath">
        /// The root path of the repository to execute the command in.
        /// </param>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="repositoryPath"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// HG did not complete within the allotted time.
        /// </exception>
        public static void Execute(string repositoryPath, IMercurialCommand command)
        {
            if (StringEx.IsNullOrWhiteSpace(repositoryPath))
            {
                throw new ArgumentNullException("repositoryPath");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            ClientExecutable.LazyInitialize();
            var specialArguments = (IEnumerable <string>) new[]
            {
                "--noninteractive", "--encoding", "utf-8",
            };
            var environmentVariables = new[]
            {
                new KeyValuePair <string, string>("LANGUAGE", "EN"), new KeyValuePair <string, string>("HGENCODING", "utf-8"),
            };

            try
            {
                CommandProcessor.Execute(repositoryPath, ClientExecutable.ClientPath, command, environmentVariables, specialArguments);
            }
            finally
            {
                MercurialVersionBase.Current.WaitForLocksToDissipate(repositoryPath);
            }
        }
コード例 #2
0
        /// <summary>
        /// Determines if the client should be switched out after the specific command has
        /// executed. If it is switched out, the old client should be disposed of, and a new client
        /// should be returned. If it is not switched out, the current client should be simply returned.
        /// This method is only called upon successful execution of the command.
        /// </summary>
        /// <param name="command">
        /// The command that was successfully executed.
        /// </param>
        /// <param name="currentClient">
        /// The current <see cref="IClient"/> implementation.
        /// </param>
        /// <returns>
        /// The <see cref="IClient"/> to use from now on.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="currentClient"/> is <c>null</c>.</para>
        /// </exception>
        public IClient SwitchAfterCommand(IMercurialCommand command, IClient currentClient)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            if (currentClient == null)
            {
                throw new ArgumentNullException("currentClient");
            }

            if (!(command is InitCommand))
            {
                return(currentClient);
            }
            if (!(currentClient is NonPersistentClient))
            {
                return(currentClient);
            }

            if (PersistentClient.IsSupported(currentClient.RepositoryPath))
            {
                string repositoryPath = currentClient.RepositoryPath;
                var    disposable     = currentClient as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }

                return(new PersistentClient(repositoryPath));
            }

            return(currentClient);
        }
コード例 #3
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command without
        /// a repository.
        /// </summary>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        public static void Execute(IMercurialCommand command)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            ClientExecutable.LazyInitialize();
            Execute(Path.GetTempPath(), command);
        }
        /// <summary>
        /// Determines if the client should be switched out before the specific command is
        /// executed. If it is switched out, the old client should be disposed of, and a new client
        /// should be returned. If it is not switched out, the current client should be simply returned.
        /// </summary>
        /// <param name="command">
        /// The command that is about to be executed.
        /// </param>
        /// <param name="currentClient">
        /// The current <see cref="IClient"/> implementation.
        /// </param>
        /// <returns>
        /// The <see cref="IClient"/> to use to execute the command, and from now on.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="currentClient"/> is <c>null</c>.</para>
        /// </exception>
        public IClient SwitchBeforeCommand(IMercurialCommand command, IClient currentClient)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            if (currentClient == null)
                throw new ArgumentNullException("currentClient");

            return currentClient;
        }
コード例 #5
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command without
        /// a repository.
        /// </summary>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        public static void Execute(IMercurialCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            ClientExecutable.LazyInitialize();
            Execute(Path.GetTempPath(), command);
        }
コード例 #6
0
        /// <summary>
        /// Determines if the client should be switched out before the specific command is
        /// executed. If it is switched out, the old client should be disposed of, and a new client
        /// should be returned. If it is not switched out, the current client should be simply returned.
        /// </summary>
        /// <param name="command">
        /// The command that is about to be executed.
        /// </param>
        /// <param name="currentClient">
        /// The current <see cref="IClient"/> implementation.
        /// </param>
        /// <returns>
        /// The <see cref="IClient"/> to use to execute the command, and from now on.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="currentClient"/> is <c>null</c>.</para>
        /// </exception>
        public IClient SwitchBeforeCommand(IMercurialCommand command, IClient currentClient)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            if (currentClient == null)
            {
                throw new ArgumentNullException("currentClient");
            }

            return(currentClient);
        }
        /// <summary>
        /// Determines if the client should be switched out after the specific command has
        /// executed. If it is switched out, the old client should be disposed of, and a new client
        /// should be returned. If it is not switched out, the current client should be simply returned.
        /// This method is only called upon successful execution of the command.
        /// </summary>
        /// <param name="command">
        /// The command that was successfully executed.
        /// </param>
        /// <param name="currentClient">
        /// The current <see cref="IClient"/> implementation.
        /// </param>
        /// <returns>
        /// The <see cref="IClient"/> to use from now on.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="currentClient"/> is <c>null</c>.</para>
        /// </exception>
        public IClient SwitchAfterCommand(IMercurialCommand command, IClient currentClient)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            if (currentClient == null)
                throw new ArgumentNullException("currentClient");

            if (!(command is InitCommand))
                return currentClient;
            if (!(currentClient is NonPersistentClient))
                return currentClient;

            if (PersistentClient.IsSupported(currentClient.RepositoryPath))
            {
                string repositoryPath = currentClient.RepositoryPath;
                var disposable = currentClient as IDisposable;
                if (disposable != null)
                    disposable.Dispose();

                return new PersistentClient(repositoryPath);
            }

            return currentClient;
        }
コード例 #8
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command against
        /// the Mercurial repository.
        /// </summary>
        /// <param name="repositoryPath">
        /// The root path of the repository to execute the command in.
        /// </param>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="repositoryPath"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// HG did not complete within the allotted time.
        /// </exception>
        public static void Execute(string repositoryPath, IMercurialCommand command)
        {
            if (StringEx.IsNullOrWhiteSpace(repositoryPath))
                throw new ArgumentNullException("repositoryPath");
            if (command == null)
                throw new ArgumentNullException("command");

            ClientExecutable.LazyInitialize();
            var specialArguments = (IEnumerable<string>)new[]
            {
                "--noninteractive", "--encoding", "utf-8",
            };
            var environmentVariables = new[]
            {
                new KeyValuePair<string, string>("LANGUAGE", "EN"), new KeyValuePair<string, string>("HGENCODING", "utf-8"),
            };

            try
            {
                CommandProcessor.Execute(repositoryPath, ClientExecutable.ClientPath, command, environmentVariables, specialArguments);
            }
            finally
            {
                MercurialVersionBase.Current.WaitForLocksToDissipate(repositoryPath);    
            }
        }
コード例 #9
0
 /// <summary>
 /// Executes the given <see cref="IMercurialCommand"/> command against
 /// the Mercurial repository.
 /// </summary>
 /// <param name="command">
 /// The <see cref="IMercurialCommand"/> command to execute.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <para><paramref name="command"/> is <c>null</c>.</para>
 /// </exception>
 /// <exception cref="MercurialException">
 /// HG did not complete within the allotted time.
 /// </exception>
 void IClient.Execute(IMercurialCommand command)
 {
     Execute(_RepositoryPath, command);
 }
コード例 #10
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command against
        /// the Mercurial repository.
        /// </summary>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// HG did not complete within the allotted time.
        /// </exception>
        public void Execute(IMercurialCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (_Process == null)
            {
                StartPersistentMercurialClient();
            }

            command.Validate();
            command.Before();

            IEnumerable <string> arguments = new[]
            {
                command.Command,
                "--noninteractive",
                "--encoding",
                ClientExecutable.TextEncodingName,
            };

            arguments = arguments.Concat(command.Arguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));
            arguments = arguments.Concat(command.AdditionalArguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));

            var commandParts = arguments.ToArray();

            string commandEncoded = string.Join("\0", commandParts.Select(p => p.Trim('"')).ToArray());
            int    length         = commandEncoded.Length;
            var    commandBuffer  = new StringBuilder();

            commandBuffer.Append("runcommand\n");
            commandBuffer.Append((char)((length >> 24) & 0xff));
            commandBuffer.Append((char)((length >> 16) & 0xff));
            commandBuffer.Append((char)((length >> 8) & 0xff));
            commandBuffer.Append((char)(length & 0xff));
            commandBuffer.Append(commandEncoded);

            string commandArguments = null;

            if (command.Observer != null)
            {
                commandArguments = string.Join(" ", commandParts.Skip(1).ToArray());
                command.Observer.Executing(command.Command, commandArguments);
            }

            byte[] buffer = ClientExecutable.TextEncoding.GetBytes(commandBuffer.ToString());
            foreach (byte b in buffer)
            {
                _Process.StandardInput.BaseStream.WriteByte(b);
                _Process.StandardInput.BaseStream.Flush();
            }

            string standardOutput;
            string standardError;
            int    exitCode;

            if (CommandServerOutputDecoder.GetOutput(_Process.StandardOutput, out standardOutput, out standardError, out exitCode))
            {
                if (command.Observer != null)
                {
                    using (var lineReader = new StringReader(standardOutput))
                    {
                        string line;
                        while ((line = lineReader.ReadLine()) != null)
                        {
                            command.Observer.Output(line);
                        }
                    }
                    using (var lineReader = new StringReader(standardError))
                    {
                        string line;
                        while ((line = lineReader.ReadLine()) != null)
                        {
                            command.Observer.ErrorOutput(line);
                        }
                    }
                    command.Observer.Executed(command.Command, commandArguments, exitCode, standardOutput, standardError);
                }
                command.After(exitCode, standardOutput, standardError);
                return;
            }

            StopPersistentMercurialClient();
            throw new MercurialExecutionException("Unable to decode output from executing command, spinning down persistent client");
        }
コード例 #11
0
 /// <summary>
 /// Executes the given <see cref="IMercurialCommand"/> command against
 /// the Mercurial repository.
 /// </summary>
 /// <param name="command">
 /// The <see cref="IMercurialCommand"/> command to execute.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <para><paramref name="command"/> is <c>null</c>.</para>
 /// </exception>
 /// <exception cref="MercurialException">
 /// HG did not complete within the allotted time.
 /// </exception>
 void IClient.Execute(IMercurialCommand command)
 {
     Execute(_RepositoryPath, command);
 }
コード例 #12
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command against
        /// the Mercurial repository.
        /// </summary>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// HG did not complete within the allotted time.
        /// </exception>
        public void Execute(IMercurialCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (_Process == null)
            {
                StartPersistentMercurialClient();
            }

            command.Validate();
            command.Before();

            IEnumerable <string> arguments = new[]
            {
                command.Command,
                "--noninteractive",
            };

            arguments = arguments.Concat(command.Arguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));
            arguments = arguments.Concat(command.AdditionalArguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));

            var commandParts = arguments.ToArray();

            string commandEncoded = string.Join("\0", commandParts.Select(p => p.Trim('"')).ToArray());
            int    length         = commandEncoded.Length;
            var    commandBuffer  = new StringBuilder();

            commandBuffer.Append("runcommand\n");
            commandBuffer.Append((char)((length >> 24) & 0xff));
            commandBuffer.Append((char)((length >> 16) & 0xff));
            commandBuffer.Append((char)((length >> 8) & 0xff));
            commandBuffer.Append((char)(length & 0xff));
            commandBuffer.Append(commandEncoded);

            string commandArguments = null;

            if (command.Observer != null)
            {
                commandArguments = string.Join(" ", commandParts.Skip(1).ToArray());
                command.Observer.Executing(command.Command, commandArguments);
            }

            MemoryStream output  = new MemoryStream();
            MemoryStream error   = new MemoryStream();
            var          outputs = new Dictionary <CommandChannel, Stream>()
            {
                { CommandChannel.Output, output },
                { CommandChannel.Error, error },
            };

            var _codec = ClientExecutable.GetMainEncoding();

            int resultCode = RunCommand(commandParts, outputs, null);
            var result     = new CommandResult(_codec.GetString(output.GetBuffer(), 0, (int)output.Length),
                                               _codec.GetString(error.GetBuffer(), 0, (int)error.Length),
                                               resultCode);

            if (resultCode == 0 || !string.IsNullOrEmpty(result.Output))
            {
                if (command.Observer != null)
                {
                    command.Observer.Output(result.Output);
                    command.Observer.ErrorOutput(result.Error);
                    command.Observer.Executed(command.Command, commandArguments, resultCode, result.Output, result.Error);
                }
                command.After(resultCode, result.Output, result.Error);
                return;
            }

            StopPersistentMercurialClient();
            throw new MercurialExecutionException(
                      string.IsNullOrEmpty(result.Error) ?
                      "Unable to decode output from executing command, spinning down persistent client"
                : result.Error);
        }
コード例 #13
0
        public void Execute_NullCommand_ThrowsArgumentNullException()
        {
            IMercurialCommand command = null;

            Assert.Throws <ArgumentNullException>(() => NonPersistentClient.Execute(command));
        }
コード例 #14
0
        /// <summary>
        /// Executes the given <see cref="IMercurialCommand"/> command against
        /// the Mercurial repository.
        /// </summary>
        /// <param name="command">
        /// The <see cref="IMercurialCommand"/> command to execute.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// HG did not complete within the allotted time.
        /// </exception>
        public void Execute(IMercurialCommand command)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            if (_Process == null)
                StartPersistentMercurialClient();

            command.Validate();
            command.Before();

            IEnumerable<string> arguments = new[]
            {
                command.Command,
                 "--noninteractive",
                 "--encoding",
                 "cp1252",
            };
            arguments = arguments.Concat(command.Arguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));
            arguments = arguments.Concat(command.AdditionalArguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));

            var commandParts = arguments.ToArray();

            string commandEncoded = string.Join("\0", commandParts.Select(p => p.Trim('"')).ToArray());
            int length = commandEncoded.Length;
            var commandBuffer = new StringBuilder();
            commandBuffer.Append("runcommand\n");
            commandBuffer.Append((char)((length >> 24) & 0xff));
            commandBuffer.Append((char)((length >> 16) & 0xff));
            commandBuffer.Append((char)((length >> 8) & 0xff));
            commandBuffer.Append((char)(length & 0xff));
            commandBuffer.Append(commandEncoded);

            string commandArguments = null;
            if (command.Observer != null)
            {
                commandArguments = string.Join(" ", commandParts.Skip(1).ToArray());
                command.Observer.Executing(command.Command, commandArguments);
            }

            byte[] buffer = Encoding.GetEncoding("iso-8859-1").GetBytes(commandBuffer.ToString());
            foreach (byte b in buffer)
            {
                _Process.StandardInput.BaseStream.WriteByte(b);
                _Process.StandardInput.BaseStream.Flush();
            }

            string standardOutput;
            string standardError;
            int exitCode;

            if (CommandServerOutputDecoder.GetOutput(_Process.StandardOutput, out standardOutput, out standardError, out exitCode))
            {
                if (command.Observer != null)
                {
                    using (var lineReader = new StringReader(standardOutput))
                    {
                        string line;
                        while ((line = lineReader.ReadLine()) != null)
                            command.Observer.Output(line);
                    }
                    using (var lineReader = new StringReader(standardError))
                    {
                        string line;
                        while ((line = lineReader.ReadLine()) != null)
                            command.Observer.ErrorOutput(line);
                    }
                    command.Observer.Executed(command.Command, commandArguments, exitCode, standardOutput, standardError);
                }
                command.After(exitCode, standardOutput, standardError);
                return;
            }

            StopPersistentMercurialClient();
            throw new MercurialExecutionException("Unable to decode output from executing command, spinning down persistent client");
        }