public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(repositoriesDirPath),
            };

            // Set the HOME environment so that Git can find the config file.
            info.EnvironmentVariables.Add("HOME", gitHomePath);

            using (var process = Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                process.StandardInput.Write('\0');

                process.StandardOutput.BaseStream.CopyTo(outStream);
                process.WaitForExit();
            }
        }
        public void ExecuteServiceByName(string correlationId, string repositoryName, string serviceName, ExecutionOptions options, System.IO.Stream inStream, System.IO.Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new System.Diagnostics.ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(this.repositoriesDirPath),
            };

            using (var process = System.Diagnostics.Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                process.StandardInput.Write('\0');

                var buffer = new byte[16 * 1024];
                int read;
                while ((read = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, read);
                    outStream.Flush();
                }

                process.WaitForExit();
            }
        }
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);
            var userid = HttpContext.Current.User.Username();

            info.EnvironmentVariables.Add("AUTH_USER", userid);
            info.EnvironmentVariables.Add("REMOTE_USER", userid);

            using (var process = Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                if (options.endStreamWithClose)
                {
                    process.StandardInput.Close();
                }
                else
                {
                    process.StandardInput.Write('\0');
                }

                process.StandardOutput.BaseStream.CopyTo(outStream);
                process.WaitForExit();
            }
        }
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";
            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                WorkingDirectory = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);
            var userid = HttpContext.Current.User.Id();
            info.EnvironmentVariables.Add("AUTH_USER", userid);
            info.EnvironmentVariables.Add("REMOTE_USER", userid);

            using (var process = Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                if (options.endStreamWithClose) {
                    process.StandardInput.Close();
                } else {
                    process.StandardInput.Write('\0');
                }

                process.StandardOutput.BaseStream.CopyTo(outStream);
                process.WaitForExit();
            }
        }
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);

            var username    = HttpContext.Current.User.Username();
            var teamsstr    = "";
            var rolesstr    = "";
            var displayname = "";

            if (!string.IsNullOrEmpty(username))
            {
                ITeamRepository tr     = DependencyResolver.Current.GetService <ITeamRepository>();
                var             userId = HttpContext.Current.User.Id();
                var             teams  = tr.GetTeams(userId);
                teamsstr = UserExtensions.StringlistToEscapedStringForEnvVar(teams.Select(x => x.Name));

                IRoleProvider rp = DependencyResolver.Current.GetService <IRoleProvider>();
                rolesstr = UserExtensions.StringlistToEscapedStringForEnvVar(rp.GetRolesForUser(userId));

                IMembershipService ms = DependencyResolver.Current.GetService <IMembershipService>();
                displayname = ms.GetUserModel(userId).DisplayName;
            }
            // If anonymous option is set then these will always be empty
            info.EnvironmentVariables.Add("AUTH_USER", username);
            info.EnvironmentVariables.Add("REMOTE_USER", username);
            info.EnvironmentVariables.Add("AUTH_USER_TEAMS", teamsstr);
            info.EnvironmentVariables.Add("AUTH_USER_ROLES", rolesstr);
            info.EnvironmentVariables.Add("AUTH_USER_DISPLAYNAME", displayname);


            using (var process = Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                if (options.endStreamWithClose)
                {
                    process.StandardInput.Close();
                }
                else
                {
                    process.StandardInput.Write('\0');
                }

                process.StandardOutput.BaseStream.CopyTo(outStream);
                process.WaitForExit();
            }
        }
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            var args = serviceName + " --stateless-rpc";
            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                WorkingDirectory = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);

            var username = HttpContext.Current.User.Username();
            var teamsstr = "";
            var rolesstr = "";
            var displayname = "";
            if(!string.IsNullOrEmpty(username)){
                ITeamRepository tr = DependencyResolver.Current.GetService<ITeamRepository>();
                var userId = HttpContext.Current.User.Id();
                var teams = tr.GetTeams(userId);
                teamsstr = UserExtensions.StringlistToEscapedStringForEnvVar(teams.Select(x => x.Name));

                IRoleProvider rp = DependencyResolver.Current.GetService<IRoleProvider>();
                rolesstr = UserExtensions.StringlistToEscapedStringForEnvVar(rp.GetRolesForUser(userId));

                IMembershipService ms = DependencyResolver.Current.GetService<IMembershipService>();
                displayname = ms.GetUserModel(userId).DisplayName;

            }
            // If anonymous option is set then these will always be empty
            info.EnvironmentVariables.Add("AUTH_USER", username);
            info.EnvironmentVariables.Add("REMOTE_USER", username);
            info.EnvironmentVariables.Add("AUTH_USER_TEAMS", teamsstr);
            info.EnvironmentVariables.Add("AUTH_USER_ROLES", rolesstr);
            info.EnvironmentVariables.Add("AUTH_USER_DISPLAYNAME", displayname);


            using (var process = Process.Start(info))
            {
                inStream.CopyTo(process.StandardInput.BaseStream);
                if (options.endStreamWithClose) {
                    process.StandardInput.Close();
                } else {
                    process.StandardInput.Write('\0');
                }

                process.StandardOutput.BaseStream.CopyTo(outStream);
                process.WaitForExit();
            }
        }
Exemple #7
0
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            if (!_permittedServiceNames.Contains(serviceName))
            {
                throw new ArgumentException("Invalid service name", nameof(serviceName));
            }

            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);

            var username    = HttpContext.Current.User.Username();
            var teamsstr    = "";
            var rolesstr    = "";
            var displayname = "";

            if (!string.IsNullOrEmpty(username))
            {
                ITeamRepository tr     = DependencyResolver.Current.GetService <ITeamRepository>();
                var             userId = HttpContext.Current.User.Id();
                var             teams  = tr.GetTeams(userId);
                teamsstr = UserExtensions.StringlistToEscapedStringForEnvVar(teams.Select(x => x.Name));

                IRoleProvider rp = DependencyResolver.Current.GetService <IRoleProvider>();
                rolesstr = UserExtensions.StringlistToEscapedStringForEnvVar(rp.GetRolesForUser(userId));

                IMembershipService ms = DependencyResolver.Current.GetService <IMembershipService>();
                displayname = ms.GetUserModel(userId).DisplayName;
            }
            // If anonymous option is set then these will always be empty
            info.EnvironmentVariables.Add("AUTH_USER", username);
            info.EnvironmentVariables.Add("REMOTE_USER", username);
            info.EnvironmentVariables.Add("AUTH_USER_TEAMS", teamsstr);
            info.EnvironmentVariables.Add("AUTH_USER_ROLES", rolesstr);
            info.EnvironmentVariables.Add("AUTH_USER_DISPLAYNAME", displayname);


            using (var process = Process.Start(info))
            {
                //Do asynchronous copy i.e. spin up a separate task so we can simultaneously read/write &
                //avoid deadlock due to filled buffers within git process
                Task stdInTask = inStream.CopyToAsync(process.StandardInput.BaseStream);

                //Don't bother waiting on completion of stdOutTask while process is running.
                //task will be sitting idle waiting for new bytes on stdOut of git
                Task stdOutTask = process.StandardOutput.BaseStream.CopyToAsync(outStream);

                //wait for process death and ensure all data is sent and received
                bool completionJobDone = false;
                while (true)
                {
                    //check if all output has been sent to git exe via stdin
                    if ((stdInTask.IsCompleted) && (completionJobDone == false))
                    {
                        //all output has been sent to git process, send final signal.
                        if (options.endStreamWithClose)
                        {
                            process.StandardInput.Close();
                        }
                        else
                        {
                            process.StandardInput.Write('\0');
                        }
                        completionJobDone = true;
                    }

                    //check if git has terminated
                    if (process.HasExited)
                    {
                        break;
                    }

                    //lets not hog all the CPU, sleep for a little while (20ms)
                    Thread.Sleep(20);
                }
                stdOutTask.Wait();
            }
        }
        public void ExecuteServiceByName(
            string correlationId,
            string repositoryName,
            string serviceName,
            ExecutionOptions options,
            Stream inStream,
            Stream outStream)
        {
            if (!_permittedServiceNames.Contains(serviceName))
            {
                throw new ArgumentException("Invalid service name", nameof(serviceName));
            }

            var args = serviceName + " --stateless-rpc";

            args += options.ToCommandLineArgs();
            args += " \"" + repoLocator.GetRepositoryDirectoryPath(repositoryName).FullName + "\"";

            var info = new ProcessStartInfo(gitPath, args)
            {
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                WorkingDirectory       = Path.GetDirectoryName(repositoriesDirPath),
            };

            SetHomePath(info);

            var username    = HttpContext.Current.User.Username();
            var teamsstr    = "";
            var rolesstr    = "";
            var displayname = "";

            if (!string.IsNullOrEmpty(username))
            {
                ITeamRepository tr     = DependencyResolver.Current.GetService <ITeamRepository>();
                var             userId = HttpContext.Current.User.Id();
                var             teams  = tr.GetTeams(userId);
                teamsstr = UserExtensions.StringlistToEscapedStringForEnvVar(teams.Select(x => x.Name));

                IRoleProvider rp = DependencyResolver.Current.GetService <IRoleProvider>();
                rolesstr = UserExtensions.StringlistToEscapedStringForEnvVar(rp.GetRolesForUser(userId));

                IMembershipService ms = DependencyResolver.Current.GetService <IMembershipService>();
                displayname = ms.GetUserModel(userId).DisplayName;
            }
            // If anonymous option is set then these will always be empty
            info.EnvironmentVariables.Add("AUTH_USER", username);
            info.EnvironmentVariables.Add("REMOTE_USER", username);
            info.EnvironmentVariables.Add("AUTH_USER_TEAMS", teamsstr);
            info.EnvironmentVariables.Add("AUTH_USER_ROLES", rolesstr);
            info.EnvironmentVariables.Add("AUTH_USER_DISPLAYNAME", displayname);


            using (var process = Process.Start(info))
            {
                var readStreamHelper = new StreamHelper {
                    FromStream = process.StandardOutput.BaseStream,
                    ToStream   = outStream
                };

                //Seperate thread for reading the output to prevent a buffer overflow at the  4k console buffer limit
                var thrdRead = new Thread((obj) => {
                    try
                    {
                        var strmHelper = (StreamHelper)obj;
                        strmHelper.FromStream.CopyTo(strmHelper.ToStream);
                    }
                    //Extra to prevent error if client disconnected
                    catch (System.Web.HttpException)
                    {
                    }
                });

                thrdRead.Start(readStreamHelper);

                inStream.CopyTo(process.StandardInput.BaseStream);
                if (options.endStreamWithClose)
                {
                    process.StandardInput.Close();
                }
                else
                {
                    process.StandardInput.Write('\0');
                }

                process.WaitForExit();

                thrdRead.Abort();
            }
        }