1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2011, 2013-2016 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "ProjectProperties.h"
21 #include "CommonAppUtils.h"
23 #include "UnicodeUtils.h"
28 bool operator() (const CString
& lhs
, const CString
& rhs
) const
30 return StrCmpLogicalW(lhs
, rhs
) < 0;
34 ProjectProperties::ProjectProperties(void)
35 : regExNeedUpdate (true)
37 , bWarnNoSignedOffBy(FALSE
)
39 , bWarnIfNoIssue(FALSE
)
42 , bFileListInEnglish(TRUE
)
48 int ProjectProperties::ReadProps()
50 CAutoConfig
gitconfig(true);
52 if (GitAdminDir::GetAdminDirPath(g_Git
.m_CurrentDir
, adminDirPath
))
53 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(adminDirPath
+ L
"config"), GIT_CONFIG_LEVEL_APP
, FALSE
); // this needs to have the highest priority in order to override .tgitconfig settings
55 if (!GitAdminDir::IsBareRepo(g_Git
.m_CurrentDir
))
56 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(g_Git
.CombinePath(L
".tgitconfig")), GIT_CONFIG_LEVEL_LOCAL
, FALSE
); // this needs to have the second highest priority
59 CString tmpFile
= CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
60 CTGitPath
path(L
".tgitconfig");
61 if (g_Git
.GetOneFile(L
"HEAD", path
, tmpFile
) == 0)
62 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(tmpFile
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
); // this needs to have the second highest priority
65 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(g_Git
.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
);
66 git_config_add_file_ondisk(gitconfig
,CGit::GetGitPathStringA(g_Git
.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG
, FALSE
);
67 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(g_Git
.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
);
68 if (!g_Git
.ms_bCygwinGit
&& !g_Git
.ms_bMsys2Git
)
69 git_config_add_file_ondisk(gitconfig
, CGit::GetGitPathStringA(g_Git
.GetGitProgramDataConfig()), GIT_CONFIG_LEVEL_PROGRAMDATA
, FALSE
);
74 gitconfig
.GetString(BUGTRAQPROPNAME_LABEL
, sLabel
);
75 gitconfig
.GetString(BUGTRAQPROPNAME_MESSAGE
, sMessage
);
76 nBugIdPos
= sMessage
.Find(L
"%BUGID%");
77 gitconfig
.GetString(BUGTRAQPROPNAME_URL
, sUrl
);
79 gitconfig
.GetBOOL(BUGTRAQPROPNAME_WARNIFNOISSUE
, bWarnIfNoIssue
);
80 gitconfig
.GetBOOL(BUGTRAQPROPNAME_NUMBER
, bNumber
);
81 gitconfig
.GetBOOL(BUGTRAQPROPNAME_APPEND
, bAppend
);
83 gitconfig
.GetString(BUGTRAQPROPNAME_PROVIDERUUID
, sProviderUuid
);
84 gitconfig
.GetString(BUGTRAQPROPNAME_PROVIDERUUID64
, sProviderUuid64
);
85 gitconfig
.GetString(BUGTRAQPROPNAME_PROVIDERPARAMS
, sProviderParams
);
87 gitconfig
.GetBOOL(PROJECTPROPNAME_WARNNOSIGNEDOFFBY
, bWarnNoSignedOffBy
);
88 gitconfig
.GetString(PROJECTPROPNAME_ICON
, sIcon
);
90 gitconfig
.GetString(BUGTRAQPROPNAME_LOGREGEX
, sPropVal
);
93 if (sCheckRe
.Find('\n')>=0)
95 sBugIDRe
= sCheckRe
.Mid(sCheckRe
.Find('\n')).Trim();
96 sCheckRe
= sCheckRe
.Left(sCheckRe
.Find('\n')).Trim();
98 if (!sCheckRe
.IsEmpty())
99 sCheckRe
= sCheckRe
.Trim();
101 if (gitconfig
.GetString(PROJECTPROPNAME_LOGWIDTHLINE
, sPropVal
) == 0)
106 nLogWidthMarker
= _wtoi(val
);
109 if (gitconfig
.GetString(PROJECTPROPNAME_PROJECTLANGUAGE
, sPropVal
) == 0)
114 lProjectLanguage
= -1;
118 lProjectLanguage
= wcstol(val
, &strEnd
, 0);
122 if (gitconfig
.GetString(PROJECTPROPNAME_LOGMINSIZE
, sPropVal
) == 0)
127 nMinLogSize
= _wtoi(val
);
133 CString
ProjectProperties::GetBugIDFromLog(CString
& msg
)
137 if (!sMessage
.IsEmpty())
145 sFirstPart
= sMessage
.Left(nBugIdPos
);
146 sLastPart
= sMessage
.Mid(nBugIdPos
+ 7);
148 if (msg
.ReverseFind('\n')>=0)
151 sBugLine
= msg
.Mid(msg
.ReverseFind('\n')+1);
154 sBugLine
= msg
.Left(msg
.Find('\n'));
162 // find out if the message consists only of numbers
163 bool bOnlyNumbers
= true;
164 for (int i
=0; i
<msg
.GetLength(); ++i
)
166 if (!_istdigit(msg
[i
]))
168 bOnlyNumbers
= false;
178 if (sBugLine
.IsEmpty() && (msg
.ReverseFind('\n') < 0))
179 sBugLine
= msg
.Mid(msg
.ReverseFind('\n')+1);
180 if (sBugLine
.Left(sFirstPart
.GetLength()).Compare(sFirstPart
)!=0)
182 if (sBugLine
.Right(sLastPart
.GetLength()).Compare(sLastPart
)!=0)
184 if (sBugLine
.IsEmpty())
186 if (msg
.Find('\n')>=0)
187 sBugLine
= msg
.Left(msg
.Find('\n'));
188 if (sBugLine
.Left(sFirstPart
.GetLength()).Compare(sFirstPart
)!=0)
190 if (sBugLine
.Right(sLastPart
.GetLength()).Compare(sLastPart
)!=0)
194 if (sBugLine
.IsEmpty())
196 sBugID
= sBugLine
.Mid(sFirstPart
.GetLength(), sBugLine
.GetLength() - sFirstPart
.GetLength() - sLastPart
.GetLength());
199 msg
= msg
.Mid(sBugLine
.GetLength());
204 msg
= msg
.Left(msg
.GetLength()-sBugLine
.GetLength());
211 void ProjectProperties::AutoUpdateRegex()
217 regCheck
= std::tr1::wregex(sCheckRe
);
218 regBugID
= std::tr1::wregex(sBugIDRe
);
220 catch (std::exception
&)
224 regExNeedUpdate
= false;
228 std::vector
<CHARRANGE
> ProjectProperties::FindBugIDPositions(const CString
& msg
)
232 std::vector
<CHARRANGE
> result
;
234 // first use the checkre string to find bug ID's in the message
235 if (!sCheckRe
.IsEmpty())
237 if (!sBugIDRe
.IsEmpty())
239 // match with two regex strings (without grouping!)
243 const std::tr1::wsregex_iterator end
;
244 std::wstring s
= msg
;
245 for (std::tr1::wsregex_iterator
it(s
.cbegin(), s
.cend(), regCheck
); it
!= end
; ++it
)
247 // (*it)[0] is the matched string
248 std::wstring matchedString
= (*it
)[0];
249 ptrdiff_t matchpos
= it
->position(0);
250 for (std::tr1::wsregex_iterator
it2(matchedString
.cbegin(), matchedString
.cend(), regBugID
); it2
!= end
; ++it2
)
252 ATLTRACE(L
"matched id : %s\n", (*it2
)[0].str().c_str());
253 ptrdiff_t matchposID
= it2
->position(0);
254 CHARRANGE range
= {(LONG
)(matchpos
+matchposID
), (LONG
)(matchpos
+matchposID
+(*it2
)[0].str().size())};
255 result
.push_back(range
);
259 catch (std::exception
&) {}
266 const std::tr1::wsregex_iterator end
;
267 std::wstring s
= msg
;
268 for (std::tr1::wsregex_iterator
it(s
.cbegin(), s
.cend(), regCheck
); it
!= end
; ++it
)
270 const std::tr1::wsmatch match
= *it
;
271 // we define group 1 as the whole issue text and
272 // group 2 as the bug ID
273 if (match
.size() >= 2)
275 ATLTRACE(L
"matched id : %s\n", std::wstring(match
[1]).c_str());
276 CHARRANGE range
= {(LONG
)(match
[1].first
- s
.cbegin()), (LONG
)(match
[1].second
- s
.cbegin())};
277 result
.push_back(range
);
281 catch (std::exception
&) {}
284 else if (result
.empty() && (!sMessage
.IsEmpty()))
293 sFirstPart
= sMessage
.Left(nBugIdPos
);
294 sLastPart
= sMessage
.Mid(nBugIdPos
+ 7);
296 sMsg
.TrimRight('\n');
297 if (sMsg
.ReverseFind('\n')>=0)
300 sBugLine
= sMsg
.Mid(sMsg
.ReverseFind('\n')+1);
303 sBugLine
= sMsg
.Left(sMsg
.Find('\n'));
309 if (sBugLine
.Left(sFirstPart
.GetLength()).Compare(sFirstPart
)!=0)
311 if (sBugLine
.Right(sLastPart
.GetLength()).Compare(sLastPart
)!=0)
313 if (sBugLine
.IsEmpty())
315 if (sMsg
.Find('\n')>=0)
316 sBugLine
= sMsg
.Left(sMsg
.Find('\n'));
317 if (sBugLine
.Left(sFirstPart
.GetLength()).Compare(sFirstPart
)!=0)
319 if (sBugLine
.Right(sLastPart
.GetLength()).Compare(sLastPart
)!=0)
323 if (sBugLine
.IsEmpty())
326 CString sBugIDPart
= sBugLine
.Mid(sFirstPart
.GetLength(), sBugLine
.GetLength() - sFirstPart
.GetLength() - sLastPart
.GetLength());
327 if (sBugIDPart
.IsEmpty())
330 //the bug id part can contain several bug id's, separated by commas
332 offset1
= sMsg
.GetLength() - sBugLine
.GetLength() + sFirstPart
.GetLength();
334 offset1
= sFirstPart
.GetLength();
335 sBugIDPart
.Trim(L
',');
336 while (sBugIDPart
.Find(',')>=0)
338 offset2
= offset1
+ sBugIDPart
.Find(',');
339 CHARRANGE range
= {(LONG
)offset1
, (LONG
)offset2
};
340 result
.push_back(range
);
341 sBugIDPart
= sBugIDPart
.Mid(sBugIDPart
.Find(',')+1);
342 offset1
= offset2
+ 1;
344 offset2
= offset1
+ sBugIDPart
.GetLength();
345 CHARRANGE range
= {(LONG
)offset1
, (LONG
)offset2
};
346 result
.push_back(range
);
352 BOOL
ProjectProperties::FindBugID(const CString
& msg
, CWnd
* pWnd
)
354 std::vector
<CHARRANGE
> positions
= FindBugIDPositions(msg
);
355 CCommonAppUtils::SetCharFormat(pWnd
, CFM_LINK
, CFE_LINK
, positions
);
357 return positions
.empty() ? FALSE
: TRUE
;
360 std::set
<CString
> ProjectProperties::FindBugIDs (const CString
& msg
)
362 std::vector
<CHARRANGE
> positions
= FindBugIDPositions(msg
);
363 std::set
<CString
> bugIDs
;
365 for (const auto& pos
: positions
)
366 bugIDs
.insert(msg
.Mid(pos
.cpMin
, pos
.cpMax
- pos
.cpMin
));
371 CString
ProjectProperties::FindBugID(const CString
& msg
)
374 if (!sCheckRe
.IsEmpty() || (nBugIdPos
>= 0))
376 std::vector
<CHARRANGE
> positions
= FindBugIDPositions(msg
);
377 std::set
<CString
, num_compare
> bugIDs
;
378 for (const auto& pos
: positions
)
379 bugIDs
.insert(msg
.Mid(pos
.cpMin
, pos
.cpMax
- pos
.cpMin
));
381 for (const auto& id
: bugIDs
)
392 bool ProjectProperties::MightContainABugID()
394 return !sCheckRe
.IsEmpty() || (nBugIdPos
>= 0);
397 CString
ProjectProperties::GetBugIDUrl(const CString
& sBugID
)
402 if (!sMessage
.IsEmpty() || !sCheckRe
.IsEmpty())
405 ret
.Replace(L
"%BUGID%", sBugID
);
410 BOOL
ProjectProperties::CheckBugID(const CString
& sID
)
414 // check if the revision actually _is_ a number
415 // or a list of numbers separated by colons
417 int len
= sID
.GetLength();
418 for (int i
=0; i
<len
; ++i
)
421 if ((c
< '0')&&(c
!= ',')&&(c
!= ' '))
430 BOOL
ProjectProperties::HasBugID(const CString
& sMsg
)
432 if (!sCheckRe
.IsEmpty())
437 return std::tr1::regex_search((LPCTSTR
)sMsg
, regCheck
);
439 catch (std::exception
&) {}