From a40f93ce2c95d52facf32d90daab3239b5df192b Mon Sep 17 00:00:00 2001 From: 2m0nd Date: Tue, 23 Oct 2012 19:06:44 +0600 Subject: [PATCH] Fixed encoding at Repository page. --- GitTools/Git.cs | 267 +++++++++++++++++++------------------- GitTools/Repository.cs | 282 ++++++++++++++++++++--------------------- 2 files changed, 276 insertions(+), 273 deletions(-) diff --git a/GitTools/Git.cs b/GitTools/Git.cs index 54c2358..6e6f6a6 100644 --- a/GitTools/Git.cs +++ b/GitTools/Git.cs @@ -1,132 +1,135 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Diagnostics; -using System.Configuration; -using System.IO; - -namespace GitTools -{ - public abstract class Git - { - private const string TRACE_CATEGORY = "git"; - public const string GIT_EXTENSION = "git"; - - public static string Run(string args, string workingDirectory) - { - var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; - - Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); - - var pinfo = new ProcessStartInfo(gitExePath) - { - Arguments = args, - CreateNoWindow = true, - RedirectStandardError = true, - RedirectStandardOutput = true, - UseShellExecute = false, - WorkingDirectory = workingDirectory, - }; - - using (var process = Process.Start(pinfo)) - { - string output = process.StandardOutput.ReadToEnd(); - string error = process.StandardError.ReadToEnd(); - process.WaitForExit(); - - Trace.WriteLine(output, TRACE_CATEGORY); - - if (!string.IsNullOrEmpty(error)) - { - Trace.WriteLine("STDERR: " + error, TRACE_CATEGORY); - throw new Exception(error); - } - return output; - } - } - - - public static void RunCmd(string args, string workingDirectory) - { - - var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; - - Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); - - var pinfo = new ProcessStartInfo("cmd.exe") - { - Arguments = "/C \"\"" + gitExePath + "\"\" " + args, - CreateNoWindow = true, - RedirectStandardError = true, - UseShellExecute = false, - WorkingDirectory = workingDirectory, - }; - - using (var process = Process.Start(pinfo)) - { - string error = process.StandardError.ReadToEnd(); - process.WaitForExit(); - - if (!string.IsNullOrEmpty(error)) - throw new Exception(error); - } - } - - //public static void Run(string args, string workingDirectory, Action action) - //{ - // var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; - - // Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); - - // var pinfo = new ProcessStartInfo(gitExePath) - // { - // Arguments = args, - // CreateNoWindow = true, - // RedirectStandardError = true, - // RedirectStandardOutput = true, - // UseShellExecute = false, - // WorkingDirectory = workingDirectory, - // }; - - // var process = new Process(); - // process.StartInfo = pinfo; - // process.EnableRaisingEvents = true; - - // process.OutputDataReceived += (_, e) => { action(e.Data); }; - // //process.ErrorDataReceived += (_, e) => { throw new Exception(e.Data); }; - - // process.Start(); - - // process.BeginErrorReadLine(); - // process.BeginOutputReadLine(); - - // process.WaitForExit(); - - //} - - public static void RunGitCmd(string args) - { - var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; - - Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, ""), TRACE_CATEGORY); - - var pinfo = new ProcessStartInfo("cmd.exe") - { - Arguments = "/C " + Path.GetFileName(gitExePath) + " " + args, - CreateNoWindow = true, - RedirectStandardError = true, - UseShellExecute = false, - WorkingDirectory = Path.GetDirectoryName(gitExePath), - }; - - using (var process = Process.Start(pinfo)) - { - string error = process.StandardError.ReadToEnd(); - process.WaitForExit(); - - if (!string.IsNullOrEmpty(error)) throw new Exception(error); - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Diagnostics; +using System.Configuration; +using System.IO; + +namespace GitTools +{ + public abstract class Git + { + private const string TRACE_CATEGORY = "git"; + public const string GIT_EXTENSION = "git"; + + public static string Run(string args, string workingDirectory, Encoding enconding = null) + { + var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; + + Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); + + var pinfo = new ProcessStartInfo(gitExePath) + { + Arguments = args, + CreateNoWindow = true, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false, + WorkingDirectory = workingDirectory, + }; + + if (enconding != null) + pinfo.StandardOutputEncoding = enconding; + + using (var process = Process.Start(pinfo)) + { + string output = process.StandardOutput.ReadToEnd(); + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + Trace.WriteLine(output, TRACE_CATEGORY); + + if (!string.IsNullOrEmpty(error)) + { + Trace.WriteLine("STDERR: " + error, TRACE_CATEGORY); + throw new Exception(error); + } + return output; + } + } + + + public static void RunCmd(string args, string workingDirectory) + { + + var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; + + Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); + + var pinfo = new ProcessStartInfo("cmd.exe") + { + Arguments = "/C \"\"" + gitExePath + "\"\" " + args, + CreateNoWindow = true, + RedirectStandardError = true, + UseShellExecute = false, + WorkingDirectory = workingDirectory, + }; + + using (var process = Process.Start(pinfo)) + { + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + if (!string.IsNullOrEmpty(error)) + throw new Exception(error); + } + } + + //public static void Run(string args, string workingDirectory, Action action) + //{ + // var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; + + // Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory), TRACE_CATEGORY); + + // var pinfo = new ProcessStartInfo(gitExePath) + // { + // Arguments = args, + // CreateNoWindow = true, + // RedirectStandardError = true, + // RedirectStandardOutput = true, + // UseShellExecute = false, + // WorkingDirectory = workingDirectory, + // }; + + // var process = new Process(); + // process.StartInfo = pinfo; + // process.EnableRaisingEvents = true; + + // process.OutputDataReceived += (_, e) => { action(e.Data); }; + // //process.ErrorDataReceived += (_, e) => { throw new Exception(e.Data); }; + + // process.Start(); + + // process.BeginErrorReadLine(); + // process.BeginOutputReadLine(); + + // process.WaitForExit(); + + //} + + public static void RunGitCmd(string args) + { + var gitExePath = ConfigurationManager.AppSettings["GitExePath"]; + + Trace.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, ""), TRACE_CATEGORY); + + var pinfo = new ProcessStartInfo("cmd.exe") + { + Arguments = "/C " + Path.GetFileName(gitExePath) + " " + args, + CreateNoWindow = true, + RedirectStandardError = true, + UseShellExecute = false, + WorkingDirectory = Path.GetDirectoryName(gitExePath), + }; + + using (var process = Process.Start(pinfo)) + { + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + if (!string.IsNullOrEmpty(error)) throw new Exception(error); + } + } + } +} diff --git a/GitTools/Repository.cs b/GitTools/Repository.cs index f63d4a5..29e28d7 100644 --- a/GitTools/Repository.cs +++ b/GitTools/Repository.cs @@ -1,142 +1,142 @@ -using System; -using System.Collections.Generic; -using System.Data.Services.Common; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using System.Security.Cryptography; -using System.Text; -using System.Configuration; - -namespace GitTools -{ - [DataServiceKey("Id")] - public class Repository - { - public string Id { get; set; } - public string Name { get; set; } - public string RepoFolder { get; set; } - - public static Repository Open(string directory) - { - var repo = new Repository - { - Name = Path.GetFileNameWithoutExtension(directory), - RepoFolder = directory, - Id = GetId(directory) - }; - return repo; - } - - private static string GetId(string directory) - { - var baseFolder = ConfigurationManager.AppSettings["GitBaseFolder"]; - return directory.Substring(baseFolder.Length + 1).Replace("\\", ".").Replace(".git", ""); - } - - public static bool IsValid(string path) - { - if (path == null) - return false; - if (!Directory.Exists(path)) - return false; - if (!File.Exists(Path.Combine(path, "HEAD"))) - return false; - if (!File.Exists(Path.Combine(path, "config"))) - return false; - if (!Directory.Exists(Path.Combine(path, "objects"))) - return false; - if (!Directory.Exists(Path.Combine(path, "objects/info"))) - return false; - if (!Directory.Exists(Path.Combine(path, "objects/pack"))) - return false; - if (!Directory.Exists(Path.Combine(path, "refs"))) - return false; - if (!Directory.Exists(Path.Combine(path, "refs/heads"))) - return false; - if (!Directory.Exists(Path.Combine(path, "refs/tags"))) - return false; - return true; - } - - //public IEnumerable Branches - //{ - // get - // { - // var branches = from b in Git.Run("branch", this.RepoFolder).Split('\n') - // where !string.IsNullOrWhiteSpace(b) - // select new Branch { Name = b.Substring(2) }; - // return branches; - // } - //} - - //public string CurrentBranch - //{ - // get - // { - // var branches = from b in Git.Run("branch", this.RepoFolder).Split('\n') - // where b.StartsWith("*") - // select b.Substring(2); - // return branches.FirstOrDefault(); - // } - //} - - public IEnumerable Commits - { - get - { - var output = ""; - try - { - output = Git.Run("log -n 100 --date-order HEAD --pretty=format:%H`%P`%cr`%cn`%ce`%ci`%T`%s --all --boundary", this.RepoFolder); - } - catch - { - - } - if (!string.IsNullOrEmpty(output)) - { - var logs = output.Split('\n'); - foreach (string log in logs) - { - string[] ss = log.Split('`'); - - if (ss[0].Contains("'")) ss[0] = ss[0].Replace("'", ""); - - yield return new Commit - { - Id = ss[0], - ParentIds = ss[1], - CommitDateRelative = ss[2], - CommitterName = ss[3], - CommitterEmail = ss[4], - CommitDate = DateTime.Parse(ss[5]), - Tree = new Tree - { - Id = ss[6], - RepoFolder = this.RepoFolder, - Name = "", - }, - Message = ss[7] + (ss.Length <= 8 ? "" : "`" + string.Join("`", ss, 8, ss.Length - 8)) - }; - } - } - } - } - - public IEnumerable Refs - { - get - { - var refs = from t in Git.Run("show-ref", this.RepoFolder).Split('\n') - where !string.IsNullOrWhiteSpace(t) - select new Ref - { - Id = t.Substring(0, 40), - RefName = t.Substring(46) - }; - return refs; - } - } - } +using System; +using System.Collections.Generic; +using System.Data.Services.Common; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using System.Security.Cryptography; +using System.Text; +using System.Configuration; + +namespace GitTools +{ + [DataServiceKey("Id")] + public class Repository + { + public string Id { get; set; } + public string Name { get; set; } + public string RepoFolder { get; set; } + + public static Repository Open(string directory) + { + var repo = new Repository + { + Name = Path.GetFileNameWithoutExtension(directory), + RepoFolder = directory, + Id = GetId(directory) + }; + return repo; + } + + private static string GetId(string directory) + { + var baseFolder = ConfigurationManager.AppSettings["GitBaseFolder"]; + return directory.Substring(baseFolder.Length + 1).Replace("\\", ".").Replace(".git", ""); + } + + public static bool IsValid(string path) + { + if (path == null) + return false; + if (!Directory.Exists(path)) + return false; + if (!File.Exists(Path.Combine(path, "HEAD"))) + return false; + if (!File.Exists(Path.Combine(path, "config"))) + return false; + if (!Directory.Exists(Path.Combine(path, "objects"))) + return false; + if (!Directory.Exists(Path.Combine(path, "objects/info"))) + return false; + if (!Directory.Exists(Path.Combine(path, "objects/pack"))) + return false; + if (!Directory.Exists(Path.Combine(path, "refs"))) + return false; + if (!Directory.Exists(Path.Combine(path, "refs/heads"))) + return false; + if (!Directory.Exists(Path.Combine(path, "refs/tags"))) + return false; + return true; + } + + //public IEnumerable Branches + //{ + // get + // { + // var branches = from b in Git.Run("branch", this.RepoFolder).Split('\n') + // where !string.IsNullOrWhiteSpace(b) + // select new Branch { Name = b.Substring(2) }; + // return branches; + // } + //} + + //public string CurrentBranch + //{ + // get + // { + // var branches = from b in Git.Run("branch", this.RepoFolder).Split('\n') + // where b.StartsWith("*") + // select b.Substring(2); + // return branches.FirstOrDefault(); + // } + //} + + public IEnumerable Commits + { + get + { + var output = ""; + try + { + output = Git.Run("log -n 100 --date-order HEAD --pretty=format:%H`%P`%cr`%cn`%ce`%ci`%T`%s --all --boundary", this.RepoFolder, Encoding.UTF8); + } + catch + { + + } + if (!string.IsNullOrEmpty(output)) + { + var logs = output.Split('\n'); + foreach (string log in logs) + { + string[] ss = log.Split('`'); + + if (ss[0].Contains("'")) ss[0] = ss[0].Replace("'", ""); + + yield return new Commit + { + Id = ss[0], + ParentIds = ss[1], + CommitDateRelative = ss[2], + CommitterName = ss[3], + CommitterEmail = ss[4], + CommitDate = DateTime.Parse(ss[5]), + Tree = new Tree + { + Id = ss[6], + RepoFolder = this.RepoFolder, + Name = "", + }, + Message = ss[7] + (ss.Length <= 8 ? "" : "`" + string.Join("`", ss, 8, ss.Length - 8)) + }; + } + } + } + } + + public IEnumerable Refs + { + get + { + var refs = from t in Git.Run("show-ref", this.RepoFolder).Split('\n') + where !string.IsNullOrWhiteSpace(t) + select new Ref + { + Id = t.Substring(0, 40), + RefName = t.Substring(46) + }; + return refs; + } + } + } } \ No newline at end of file