This repository has been archived by the owner. It is now read-only.
/
CommitInformation.cs
169 lines (140 loc) · 5.13 KB
/
CommitInformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Collections;
namespace SvnPostCommitHook
{
public class CommitInformation
{
// Create a logger for use in this class
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// input parameters
private string _repository;
private string _revision;
// Info gathered from svnlook utility
private SvnLookOutputParser _lookInfo = null;
public CommitInformation(string repos, string rev)
{
_repository = repos;
_revision = rev;
}
public string Repository
{
get { return _repository; }
}
public string Revision
{
get { return _revision; }
}
public SvnLookOutputParser LookInfo
{
get
{
if (null == _lookInfo)
log.Error("Accessing _lookinfo but it is null");
return _lookInfo;
}
}
public void Read()
{
// not really elegant, but sufficient as no recovery from an error condition is needed
try
{
string strRevAndRepos = Revision + " " + Repository;
string strSvnLookPath = TypedConfiguration.Instance.SvnLookPath;
strSvnLookPath = "\"" + strSvnLookPath + "\"";
if (log.IsInfoEnabled) log.Info(strSvnLookPath + " info -r " + strRevAndRepos);
StringCollection infoColl = SilentCmdLineApplication.Execute(strSvnLookPath + " info -r " + strRevAndRepos);
if (log.IsInfoEnabled) log.Info(strSvnLookPath + " changed -r " + strRevAndRepos);
StringCollection changeColl = SilentCmdLineApplication.Execute(strSvnLookPath + " changed -r " + strRevAndRepos);
StringCollection diffColl = null;
if (TypedConfiguration.Instance.AppendDiffToMail)
{
if (log.IsInfoEnabled) log.Info(strSvnLookPath + " diff --no-diff-deleted -r " + strRevAndRepos);
diffColl = SilentCmdLineApplication.Execute(strSvnLookPath + " diff --no-diff-deleted -r " + strRevAndRepos);
}
_lookInfo = SvnLookOutputParser.Parse(infoColl, changeColl, diffColl);
}
catch (Exception e)
{
log.Error("Read/Parse failed fatally", e);
}
}
// SharpDevelop specific mail subject inferral
private string InferSubjectFormatStringFromSvnPath(string svnPath)
{
string standardSubjectEnd = " rev {0}, {1}";
StringDictionary mapSvnUrlToName = new StringDictionary();
mapSvnUrlToName.Add("trunk/", "Mirador");
mapSvnUrlToName.Add("branches/3.0/", "Montferrer");
mapSvnUrlToName.Add("branches/2.1/", "Serralongue");
mapSvnUrlToName.Add("branches/2.0/", "Corsavy");
foreach (DictionaryEntry de in mapSvnUrlToName)
{
if (svnPath.StartsWith((string)de.Key))
{
return "n:" + de.Value + standardSubjectEnd;
}
}
if (svnPath.StartsWith("branches/"))
{
string[] pathParts = svnPath.Split(new char[] { '/' });
if (pathParts.Length > 1)
{
return "a:" + pathParts[1] + standardSubjectEnd;
}
}
// Error case
return "[Unidentified branch] rev {0}, {1}";
}
private string InferSubjectFormatString()
{
string mailingSubjectFormatString = "Foo rev {0}, {1}";
if (LookInfo.Added.Count > 0)
{
mailingSubjectFormatString = InferSubjectFormatStringFromSvnPath(LookInfo.Added[0]);
}
else if (LookInfo.Modified.Count > 0)
{
mailingSubjectFormatString = InferSubjectFormatStringFromSvnPath(LookInfo.Modified[0]);
}
else if (LookInfo.Deleted.Count > 0)
{
mailingSubjectFormatString = InferSubjectFormatStringFromSvnPath(LookInfo.Deleted[0]);
}
return mailingSubjectFormatString;
}
public void PostToMailingList()
{
Mailer mailer = TypedConfiguration.Instance.Mailer;
mailer.To = TypedConfiguration.Instance.MailTo;
mailer.From = string.Format(TypedConfiguration.Instance.MailFrom, LookInfo.Author);
mailer.SmtpHost = TypedConfiguration.Instance.MailServer;
mailer.SmtpServerPort = TypedConfiguration.Instance.SmtpServerPort;
mailer.SmtpAuthType = TypedConfiguration.Instance.SmtpAuthType;
mailer.SmtpAuthUsername = TypedConfiguration.Instance.SmtpAuthUsername;
mailer.SmtpAuthPassword = TypedConfiguration.Instance.SmtpAuthPassword;
// hack for the SharpDevelop commit hook
string mailingSubjectFormatString = InferSubjectFormatString();
mailer.Subject = String.Format(mailingSubjectFormatString, Revision, LookInfo.Author);
MessageFormatter formatter = new TextMessageFormatter();
formatter.Format(mailer, this);
if (!TypedConfiguration.Instance.MailTextOnly)
{
formatter = new HtmlMessageFormatter();
formatter.Format(mailer, this);
}
try
{
mailer.Send();
}
catch (Exception e)
{
log.Error("Mailing commit message failed fatally", e);
}
}
}
}