OSDN Git Service

56ef4a854198c6c82758a48a570b7e9b25a3351a
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / CommitDlg.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "TortoiseProc.h"\r
21 #include "CommitDlg.h"\r
22 #include "DirFileEnum.h"\r
23 //#include "GitConfig.h"\r
24 //#include "GitProperties.h"\r
25 #include "MessageBox.h"\r
26 #include "AppUtils.h"\r
27 #include "PathUtils.h"\r
28 #include "Git.h"\r
29 #include "Registry.h"\r
30 #include "GitStatus.h"\r
31 #include "HistoryDlg.h"\r
32 #include "Hooks.h"\r
33 #include "CommonResource.h"\r
34 #include "UnicodeUtils.h"\r
35 \r
36 #ifdef _DEBUG\r
37 #define new DEBUG_NEW\r
38 #undef THIS_FILE\r
39 static char THIS_FILE[] = __FILE__;\r
40 #endif\r
41 \r
42 UINT CCommitDlg::WM_AUTOLISTREADY = RegisterWindowMessage(_T("TORTOISEGIT_AUTOLISTREADY_MSG"));\r
43 \r
44 IMPLEMENT_DYNAMIC(CCommitDlg, CResizableStandAloneDialog)\r
45 CCommitDlg::CCommitDlg(CWnd* pParent /*=NULL*/)\r
46         : CResizableStandAloneDialog(CCommitDlg::IDD, pParent)\r
47         , m_bRecursive(FALSE)\r
48         , m_bShowUnversioned(FALSE)\r
49         , m_bBlock(FALSE)\r
50         , m_bThreadRunning(FALSE)\r
51         , m_bRunThread(FALSE)\r
52         , m_pThread(NULL)\r
53         , m_bKeepLocks(FALSE)\r
54         , m_bKeepChangeList(TRUE)\r
55         , m_itemsCount(0)\r
56         , m_bSelectFilesForCommit(TRUE)\r
57 {\r
58 }\r
59 \r
60 CCommitDlg::~CCommitDlg()\r
61 {\r
62         if(m_pThread != NULL)\r
63         {\r
64                 delete m_pThread;\r
65         }\r
66 }\r
67 \r
68 void CCommitDlg::DoDataExchange(CDataExchange* pDX)\r
69 {\r
70         CResizableStandAloneDialog::DoDataExchange(pDX);\r
71         DDX_Control(pDX, IDC_FILELIST, m_ListCtrl);\r
72         DDX_Control(pDX, IDC_LOGMESSAGE, m_cLogMessage);\r
73         DDX_Check(pDX, IDC_SHOWUNVERSIONED, m_bShowUnversioned);\r
74         DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);\r
75         DDX_Text(pDX, IDC_BUGID, m_sBugID);\r
76         DDX_Check(pDX, IDC_KEEPLOCK, m_bKeepLocks);\r
77         DDX_Control(pDX, IDC_SPLITTER, m_wndSplitter);\r
78         DDX_Check(pDX, IDC_KEEPLISTS, m_bKeepChangeList);\r
79 }\r
80 \r
81 BEGIN_MESSAGE_MAP(CCommitDlg, CResizableStandAloneDialog)\r
82         ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)\r
83         ON_BN_CLICKED(IDHELP, OnBnClickedHelp)\r
84         ON_BN_CLICKED(IDC_SHOWUNVERSIONED, OnBnClickedShowunversioned)\r
85 //      ON_BN_CLICKED(IDC_HISTORY, OnBnClickedHistory)\r
86         ON_BN_CLICKED(IDC_BUGTRAQBUTTON, OnBnClickedBugtraqbutton)\r
87         ON_EN_CHANGE(IDC_LOGMESSAGE, OnEnChangeLogmessage)\r
88         ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ITEMCOUNTCHANGED, OnGitStatusListCtrlItemCountChanged)\r
89         ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnGitStatusListCtrlNeedsRefresh)\r
90         ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)\r
91         ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_CHECKCHANGED, &CCommitDlg::OnGitStatusListCtrlCheckChanged)\r
92         ON_REGISTERED_MESSAGE(WM_AUTOLISTREADY, OnAutoListReady) \r
93         ON_WM_TIMER()\r
94     ON_WM_SIZE()\r
95         ON_STN_CLICKED(IDC_EXTERNALWARNING, &CCommitDlg::OnStnClickedExternalwarning)\r
96         ON_BN_CLICKED(IDC_SIGNOFF, &CCommitDlg::OnBnClickedSignOff)\r
97         ON_STN_CLICKED(IDC_COMMITLABEL, &CCommitDlg::OnStnClickedCommitlabel)\r
98 END_MESSAGE_MAP()\r
99 \r
100 BOOL CCommitDlg::OnInitDialog()\r
101 {\r
102         CResizableStandAloneDialog::OnInitDialog();\r
103         \r
104         m_regAddBeforeCommit = CRegDWORD(_T("Software\\TortoiseGit\\AddBeforeCommit"), TRUE);\r
105         m_bShowUnversioned = m_regAddBeforeCommit;\r
106 \r
107         m_History.SetMaxHistoryItems((LONG)CRegDWORD(_T("Software\\TortoiseGit\\MaxHistoryItems"), 25));\r
108 \r
109         m_regKeepChangelists = CRegDWORD(_T("Software\\TortoiseGit\\KeepChangeLists"), FALSE);\r
110         m_bKeepChangeList = m_regKeepChangelists;\r
111 \r
112 //      GitConfig config;\r
113 //      m_bKeepLocks = config.KeepLocks();\r
114 \r
115         UpdateData(FALSE);\r
116         \r
117         m_ListCtrl.Init(SVNSLC_COLEXT | SVNSLC_COLSTATUS , _T("CommitDlg"));\r
118         m_ListCtrl.SetSelectButton(&m_SelectAll);\r
119         m_ListCtrl.SetStatLabel(GetDlgItem(IDC_STATISTICS));\r
120         m_ListCtrl.SetCancelBool(&m_bCancelled);\r
121         m_ListCtrl.SetEmptyString(IDS_COMMITDLG_NOTHINGTOCOMMIT);\r
122         m_ListCtrl.EnableFileDrop();\r
123         m_ListCtrl.SetBackgroundImage(IDI_COMMIT_BKG);\r
124         \r
125 //      m_ProjectProperties.ReadPropsPathList(m_pathList);\r
126         m_cLogMessage.Init(m_ProjectProperties);\r
127         m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));\r
128         m_cLogMessage.RegisterContextMenuHandler(this);\r
129 \r
130         OnEnChangeLogmessage();\r
131 \r
132         m_tooltips.Create(this);\r
133         m_tooltips.AddTool(IDC_EXTERNALWARNING, IDS_COMMITDLG_EXTERNALS);\r
134 //      m_tooltips.AddTool(IDC_HISTORY, IDS_COMMITDLG_HISTORY_TT);\r
135         \r
136         m_SelectAll.SetCheck(BST_INDETERMINATE);\r
137 \r
138 #if 0\r
139         CBugTraqAssociations bugtraq_associations;\r
140         bugtraq_associations.Load();\r
141 \r
142         if (bugtraq_associations.FindProvider(m_pathList, &m_bugtraq_association))\r
143         {\r
144                 GetDlgItem(IDC_BUGID)->ShowWindow(SW_HIDE);\r
145                 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_HIDE);\r
146 \r
147                 CComPtr<IBugTraqProvider> pProvider;\r
148                 HRESULT hr = pProvider.CoCreateInstance(m_bugtraq_association.GetProviderClass());\r
149                 if (SUCCEEDED(hr))\r
150                 {\r
151                         m_BugTraqProvider = pProvider;\r
152                         BSTR temp = NULL;\r
153                         if (SUCCEEDED(hr = pProvider->GetLinkText(GetSafeHwnd(), m_bugtraq_association.GetParameters().AllocSysString(), &temp)))\r
154                         {\r
155                                 SetDlgItemText(IDC_BUGTRAQBUTTON, temp);\r
156                                 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(TRUE);\r
157                                 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_SHOW);\r
158                         }\r
159 \r
160                         SysFreeString(temp);\r
161                 }\r
162 \r
163                 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();\r
164         }\r
165         else if (!m_ProjectProperties.sMessage.IsEmpty())\r
166         {\r
167                 GetDlgItem(IDC_BUGID)->ShowWindow(SW_SHOW);\r
168                 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_SHOW);\r
169                 if (!m_ProjectProperties.sLabel.IsEmpty())\r
170                         SetDlgItemText(IDC_BUGIDLABEL, m_ProjectProperties.sLabel);\r
171                 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_HIDE);\r
172                 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(FALSE);\r
173                 GetDlgItem(IDC_BUGID)->SetFocus();\r
174                 CString sBugID = m_ProjectProperties.GetBugIDFromLog(m_sLogMessage);\r
175                 if (!sBugID.IsEmpty())\r
176                 {\r
177                         SetDlgItemText(IDC_BUGID, sBugID);\r
178                 }\r
179         }\r
180         else\r
181 #endif\r
182         {\r
183                 GetDlgItem(IDC_BUGID)->ShowWindow(SW_HIDE);\r
184                 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_HIDE);\r
185                 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_HIDE);\r
186                 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(FALSE);\r
187                 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();\r
188         }\r
189 \r
190         if (!m_sLogMessage.IsEmpty())\r
191                 m_cLogMessage.SetText(m_sLogMessage);\r
192                 \r
193         GetWindowText(m_sWindowTitle);\r
194         \r
195         AdjustControlSize(IDC_SHOWUNVERSIONED);\r
196         AdjustControlSize(IDC_SELECTALL);\r
197         AdjustControlSize(IDC_KEEPLOCK);\r
198 \r
199         GetClientRect(m_DlgOrigRect);\r
200         m_cLogMessage.GetClientRect(m_LogMsgOrigRect);\r
201 \r
202         AddAnchor(IDC_COMMITLABEL, TOP_LEFT, TOP_RIGHT);\r
203         AddAnchor(IDC_BUGIDLABEL, TOP_RIGHT);\r
204         AddAnchor(IDC_BUGID, TOP_RIGHT);\r
205         AddAnchor(IDC_BUGTRAQBUTTON, TOP_RIGHT);\r
206         AddAnchor(IDC_COMMIT_TO, TOP_LEFT, TOP_RIGHT);\r
207         AddAnchor(IDC_MESSAGEGROUP, TOP_LEFT, TOP_RIGHT);\r
208 //      AddAnchor(IDC_HISTORY, TOP_LEFT);\r
209         AddAnchor(IDC_LOGMESSAGE, TOP_LEFT, TOP_RIGHT);\r
210         AddAnchor(IDC_SIGNOFF,   TOP_RIGHT);\r
211         AddAnchor(IDC_LISTGROUP, TOP_LEFT, BOTTOM_RIGHT);\r
212         AddAnchor(IDC_SPLITTER, TOP_LEFT, TOP_RIGHT);\r
213         AddAnchor(IDC_FILELIST, TOP_LEFT, BOTTOM_RIGHT);\r
214         AddAnchor(IDC_SHOWUNVERSIONED, BOTTOM_LEFT);\r
215         AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);\r
216         AddAnchor(IDC_EXTERNALWARNING, BOTTOM_RIGHT);\r
217         AddAnchor(IDC_STATISTICS, BOTTOM_LEFT, BOTTOM_RIGHT);\r
218         AddAnchor(IDC_KEEPLOCK, BOTTOM_LEFT);\r
219         AddAnchor(IDC_KEEPLISTS, BOTTOM_LEFT);\r
220         AddAnchor(IDOK, BOTTOM_RIGHT);\r
221         AddAnchor(IDCANCEL, BOTTOM_RIGHT);\r
222         AddAnchor(IDHELP, BOTTOM_RIGHT);\r
223         \r
224         if (hWndExplorer)\r
225                 CenterWindow(CWnd::FromHandle(hWndExplorer));\r
226         EnableSaveRestore(_T("CommitDlg"));\r
227         DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\CommitDlgSizer"));\r
228         RECT rcDlg, rcLogMsg, rcFileList;\r
229         GetClientRect(&rcDlg);\r
230         m_cLogMessage.GetWindowRect(&rcLogMsg);\r
231         ScreenToClient(&rcLogMsg);\r
232         m_ListCtrl.GetWindowRect(&rcFileList);\r
233         ScreenToClient(&rcFileList);\r
234         if (yPos)\r
235         {\r
236                 RECT rectSplitter;\r
237                 m_wndSplitter.GetWindowRect(&rectSplitter);\r
238                 ScreenToClient(&rectSplitter);\r
239                 int delta = yPos - rectSplitter.top;\r
240                 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))\r
241                 {\r
242                         m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);\r
243                         DoSize(delta);\r
244                 }\r
245         }\r
246 \r
247         // add all directories to the watcher\r
248         for (int i=0; i<m_pathList.GetCount(); ++i)\r
249         {\r
250                 if (m_pathList[i].IsDirectory())\r
251                         m_pathwatcher.AddPath(m_pathList[i]);\r
252         }\r
253 \r
254         m_updatedPathList = m_pathList;\r
255 \r
256         //first start a thread to obtain the file list with the status without\r
257         //blocking the dialog\r
258         InterlockedExchange(&m_bBlock, TRUE);\r
259         m_pThread = AfxBeginThread(StatusThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
260         if (m_pThread==NULL)\r
261         {\r
262                 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);\r
263                 InterlockedExchange(&m_bBlock, FALSE);\r
264         }\r
265         else\r
266         {\r
267                 m_pThread->m_bAutoDelete = FALSE;\r
268                 m_pThread->ResumeThread();\r
269         }\r
270         CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);\r
271         CRegDWORD historyhint = CRegDWORD(_T("Software\\TortoiseGit\\HistoryHintShown"), FALSE);\r
272         if ((((DWORD)err)!=FALSE)&&((((DWORD)historyhint)==FALSE)))\r
273         {\r
274                 historyhint = TRUE;\r
275 //              ShowBalloon(IDC_HISTORY, IDS_COMMITDLG_HISTORYHINT_TT, IDI_INFORMATION);\r
276         }\r
277         err = FALSE;\r
278 \r
279 \r
280         return FALSE;  // return TRUE unless you set the focus to a control\r
281         // EXCEPTION: OCX Property Pages should return FALSE\r
282 }\r
283 \r
284 void CCommitDlg::OnOK()\r
285 {\r
286         if (m_bBlock)\r
287                 return;\r
288         if (m_bThreadRunning)\r
289         {\r
290                 m_bCancelled = true;\r
291                 InterlockedExchange(&m_bRunThread, FALSE);\r
292                 WaitForSingleObject(m_pThread->m_hThread, 1000);\r
293                 if (m_bThreadRunning)\r
294                 {\r
295                         // we gave the thread a chance to quit. Since the thread didn't\r
296                         // listen to us we have to kill it.\r
297                         TerminateThread(m_pThread->m_hThread, (DWORD)-1);\r
298                         InterlockedExchange(&m_bThreadRunning, FALSE);\r
299                 }\r
300         }\r
301         CString id;\r
302         GetDlgItemText(IDC_BUGID, id);\r
303         if (!m_ProjectProperties.CheckBugID(id))\r
304         {\r
305                 ShowBalloon(IDC_BUGID, IDS_COMMITDLG_ONLYNUMBERS, IDI_EXCLAMATION);\r
306                 return;\r
307         }\r
308         m_sLogMessage = m_cLogMessage.GetText();\r
309         if ((m_ProjectProperties.bWarnIfNoIssue) && (id.IsEmpty() && !m_ProjectProperties.HasBugID(m_sLogMessage)))\r
310         {\r
311                 if (CMessageBox::Show(this->m_hWnd, IDS_COMMITDLG_NOISSUEWARNING, IDS_APPNAME, MB_YESNO | MB_ICONWARNING)!=IDYES)\r
312                         return;\r
313         }\r
314 \r
315 #if 0\r
316         CRegDWORD regUnversionedRecurse (_T("Software\\TortoiseGit\\UnversionedRecurse"), TRUE);\r
317         if (!(DWORD)regUnversionedRecurse)\r
318         {\r
319                 // Find unversioned directories which are marked for commit. The user might expect them\r
320                 // to be added recursively since he cannot the the files. Let's ask the user if he knows\r
321                 // what he is doing.\r
322                 int nListItems = m_ListCtrl.GetItemCount();\r
323                 for (int j=0; j<nListItems; j++)\r
324                 {\r
325                         const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(j);\r
326                         if (entry->IsChecked() && (entry->status == Git_wc_status_unversioned) && entry->IsFolder() )\r
327                         {\r
328                                 if (CMessageBox::Show(this->m_hWnd, IDS_COMMITDLG_UNVERSIONEDFOLDERWARNING, IDS_APPNAME, MB_YESNO | MB_ICONWARNING)!=IDYES)\r
329                                         return;\r
330                         }\r
331                 }\r
332         }\r
333 #endif\r
334         m_pathwatcher.Stop();\r
335         InterlockedExchange(&m_bBlock, TRUE);\r
336         CDWordArray arDeleted;\r
337         //first add all the unversioned files the user selected\r
338         //and check if all versioned files are selected\r
339         int nUnchecked = 0;\r
340         m_bRecursive = true;\r
341         int nListItems = m_ListCtrl.GetItemCount();\r
342 \r
343         CTGitPathList itemsToAdd;\r
344         CTGitPathList itemsToRemove;\r
345         bool bCheckedInExternal = false;\r
346         bool bHasConflicted = false;\r
347         //std::set<CString> checkedLists;\r
348         //std::set<CString> uncheckedLists;\r
349 \r
350         CString checkedfiles;\r
351         CString uncheckedfiles;\r
352 \r
353         for (int j=0; j<nListItems; j++)\r
354         {\r
355                 //const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(j);\r
356                 CTGitPath *entry = (CTGitPath*)m_ListCtrl.GetItemData(j);\r
357                 if (entry->m_Checked)\r
358                 {\r
359 #if 0\r
360                         if (entry->status == Git_wc_status_unversioned)\r
361                         {\r
362                                 itemsToAdd.AddPath(entry->GetPath());\r
363                         }\r
364                         if (entry->status == Git_wc_status_conflicted)\r
365                         {\r
366                                 bHasConflicted = true;\r
367                         }\r
368                         if (entry->status == Git_wc_status_missing)\r
369                         {\r
370                                 itemsToRemove.AddPath(entry->GetPath());\r
371                         }\r
372                         if (entry->status == Git_wc_status_deleted)\r
373                         {\r
374                                 arDeleted.Add(j);\r
375                         }\r
376                         if (entry->IsInExternal())\r
377                         {\r
378                                 bCheckedInExternal = true;\r
379                         }\r
380 #endif\r
381                         //checkedLists.insert(entry->GetGitPathString());\r
382                         checkedfiles += _T("\"")+entry->GetGitPathString()+_T("\" ");\r
383                 }\r
384                 else\r
385                 {\r
386                         //uncheckedLists.insert(entry->GetGitPathString());\r
387                         uncheckedfiles += _T("\"")+entry->GetGitPathString()+_T("\" ");\r
388 #if 0\r
389                         if ((entry->status != Git_wc_status_unversioned)        &&\r
390                                 (entry->status != Git_wc_status_ignored))\r
391                         {\r
392                                 nUnchecked++;\r
393                                 uncheckedLists.insert(entry->GetChangeList());\r
394                                 if (m_bRecursive)\r
395                                 {\r
396                                         // This algorithm is for the sake of simplicity of the complexity O(N?\r
397                                         for (int k=0; k<nListItems; k++)\r
398                                         {\r
399                                                 const CGitStatusListCtrl::FileEntry * entryK = m_ListCtrl.GetListEntry(k);\r
400                                                 if (entryK->IsChecked() && entryK->GetPath().IsAncestorOf(entry->GetPath())  )\r
401                                                 {\r
402                                                         // Fall back to a non-recursive commit to prevent items being\r
403                                                         // committed which aren't checked although its parent is checked\r
404                                                         // (property change, directory deletion, ... )\r
405                                                         m_bRecursive = false;\r
406                                                         break;\r
407                                                 }\r
408                                         }\r
409                                 }\r
410                         }\r
411 #endif\r
412                 }\r
413         }\r
414 \r
415         CString cmd;\r
416         CString out;\r
417         if(uncheckedfiles.GetLength()>0)\r
418         {\r
419                 cmd.Format(_T("git.exe reset -- %s"),uncheckedfiles);\r
420                 g_Git.Run(cmd,&out);\r
421         }\r
422 \r
423         if(checkedfiles.GetLength()>0)\r
424         {\r
425                 cmd.Format(_T("git.exe update-index -- %s"),checkedfiles);\r
426                 g_Git.Run(cmd,&out);\r
427 \r
428                 CString tempfile=::GetTempFile();\r
429                 CFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );\r
430                 CStringA log=CUnicodeUtils::GetUTF8( m_sLogMessage);\r
431                 file.Write(log,log.GetLength());\r
432                 //file.WriteString(m_sLogMessage);\r
433                 file.Close();\r
434         \r
435                 out =_T("");\r
436                 cmd.Format(_T("git.exe commit -F \"%s\""), tempfile);\r
437                 g_Git.Run(cmd,&out);\r
438         \r
439                 CFile::Remove(tempfile);\r
440 \r
441                 CMessageBox::Show(this->m_hWnd, out, _T("Commit Finish"), MB_OK | MB_ICONINFORMATION);\r
442         }else\r
443                 CMessageBox::Show(this->m_hWnd, _T("Nothing Commit"), _T("Commit Finish"), MB_OK | MB_ICONINFORMATION);\r
444 #if 0\r
445         if (m_pathwatcher.GetNumberOfChangedPaths() && m_bRecursive)\r
446         {\r
447                 // There are paths which got changed (touched at least).\r
448                 // We have to find out if this affects the selection in the commit dialog\r
449                 // If it could affect the selection, revert back to a non-recursive commit\r
450                 CTGitPathList changedList = m_pathwatcher.GetChangedPaths();\r
451                 changedList.RemoveDuplicates();\r
452                 for (int i=0; i<changedList.GetCount(); ++i)\r
453                 {\r
454                         if (changedList[i].IsAdminDir())\r
455                         {\r
456                                 // something inside an admin dir was changed.\r
457                                 // if it's the entries file, then we have to fully refresh because\r
458                                 // files may have been added/removed from version control\r
459                                 if ((changedList[i].GetWinPathString().Right(7).CompareNoCase(_T("entries")) == 0) &&\r
460                                         (changedList[i].GetWinPathString().Find(_T("\\tmp\\"))<0))\r
461                                 {\r
462                                         m_bRecursive = false;\r
463                                         break;\r
464                                 }\r
465                         }\r
466                         else if (!m_ListCtrl.IsPathShown(changedList[i]))\r
467                         {\r
468                                 // a path which is not shown in the list has changed\r
469                                 CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(changedList[i]);\r
470                                 if (entry)\r
471                                 {\r
472                                         // check if the changed path would get committed by a recursive commit\r
473                                         if ((!entry->IsFromDifferentRepository()) &&\r
474                                                 (!entry->IsInExternal()) &&\r
475                                                 (!entry->IsNested()) && \r
476                                                 (!entry->IsChecked()))\r
477                                         {\r
478                                                 m_bRecursive = false;\r
479                                                 break;\r
480                                         }\r
481                                 }\r
482                         }\r
483                 }\r
484         }\r
485 \r
486 \r
487         // Now, do all the adds - make sure that the list is sorted so that parents \r
488         // are added before their children\r
489         itemsToAdd.SortByPathname();\r
490         Git Git;\r
491         if (!Git.Add(itemsToAdd, &m_ProjectProperties, Git_depth_empty, FALSE, FALSE, TRUE))\r
492         {\r
493                 CMessageBox::Show(m_hWnd, Git.GetLastErrorMessage(), _T("TortoiseGit"), MB_ICONERROR);\r
494                 InterlockedExchange(&m_bBlock, FALSE);\r
495                 Refresh();\r
496                 return;\r
497         }\r
498 \r
499         // Remove any missing items\r
500         // Not sure that this sort is really necessary - indeed, it might be better to do a reverse sort at this point\r
501         itemsToRemove.SortByPathname();\r
502         Git.Remove(itemsToRemove, TRUE);\r
503 \r
504         //the next step: find all deleted files and check if they're \r
505         //inside a deleted folder. If that's the case, then remove those\r
506         //files from the list since they'll get deleted by the parent\r
507         //folder automatically.\r
508         m_ListCtrl.Block(TRUE, FALSE);\r
509         INT_PTR nDeleted = arDeleted.GetCount();\r
510         for (INT_PTR i=0; i<arDeleted.GetCount(); i++)\r
511         {\r
512                 if (m_ListCtrl.GetCheck(arDeleted.GetAt(i)))\r
513                 {\r
514                         const CTGitPath& path = m_ListCtrl.GetListEntry(arDeleted.GetAt(i))->GetPath();\r
515                         if (path.IsDirectory())\r
516                         {\r
517                                 //now find all children of this directory\r
518                                 for (int j=0; j<arDeleted.GetCount(); j++)\r
519                                 {\r
520                                         if (i!=j)\r
521                                         {\r
522                                                 CGitStatusListCtrl::FileEntry* childEntry = m_ListCtrl.GetListEntry(arDeleted.GetAt(j));\r
523                                                 if (childEntry->IsChecked())\r
524                                                 {\r
525                                                         if (path.IsAncestorOf(childEntry->GetPath()))\r
526                                                         {\r
527                                                                 m_ListCtrl.SetEntryCheck(childEntry, arDeleted.GetAt(j), false);\r
528                                                                 nDeleted--;\r
529                                                         }\r
530                                                 }\r
531                                         }\r
532                                 }\r
533                         }\r
534                 }\r
535         } \r
536         m_ListCtrl.Block(FALSE, FALSE);\r
537 \r
538         if ((nUnchecked != 0)||(bCheckedInExternal)||(bHasConflicted)||(!m_bRecursive))\r
539         {\r
540                 //save only the files the user has checked into the temporary file\r
541                 m_ListCtrl.WriteCheckedNamesToPathList(m_pathList);\r
542         }\r
543         m_ListCtrl.WriteCheckedNamesToPathList(m_selectedPathList);\r
544         // the item count is used in the progress dialog to show the overall commit\r
545         // progress.\r
546         // deleted items only send one notification event, all others send two\r
547         m_itemsCount = ((m_selectedPathList.GetCount() - nDeleted - itemsToRemove.GetCount()) * 2) + nDeleted + itemsToRemove.GetCount();\r
548 \r
549         if ((m_bRecursive)&&(checkedLists.size() == 1))\r
550         {\r
551                 // all checked items belong to the same changelist\r
552                 // find out if there are any unchecked items which belong to that changelist\r
553                 if (uncheckedLists.find(*checkedLists.begin()) == uncheckedLists.end())\r
554                         m_sChangeList = *checkedLists.begin();\r
555         }\r
556 #endif\r
557         UpdateData();\r
558         m_regAddBeforeCommit = m_bShowUnversioned;\r
559         if (!GetDlgItem(IDC_KEEPLOCK)->IsWindowEnabled())\r
560                 m_bKeepLocks = FALSE;\r
561         m_regKeepChangelists = m_bKeepChangeList;\r
562         if (!GetDlgItem(IDC_KEEPLISTS)->IsWindowEnabled())\r
563                 m_bKeepChangeList = FALSE;\r
564         InterlockedExchange(&m_bBlock, FALSE);\r
565         m_sBugID.Trim();\r
566         if (!m_sBugID.IsEmpty())\r
567         {\r
568                 m_sBugID.Replace(_T(", "), _T(","));\r
569                 m_sBugID.Replace(_T(" ,"), _T(","));\r
570                 CString sBugID = m_ProjectProperties.sMessage;\r
571                 sBugID.Replace(_T("%BUGID%"), m_sBugID);\r
572                 if (m_ProjectProperties.bAppend)\r
573                         m_sLogMessage += _T("\n") + sBugID + _T("\n");\r
574                 else\r
575                         m_sLogMessage = sBugID + _T("\n") + m_sLogMessage;\r
576         }\r
577         m_History.AddEntry(m_sLogMessage);\r
578         m_History.Save();\r
579 \r
580         SaveSplitterPos();\r
581 \r
582         CResizableStandAloneDialog::OnOK();\r
583 }\r
584 \r
585 void CCommitDlg::SaveSplitterPos()\r
586 {\r
587         if (!IsIconic())\r
588         {\r
589                 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\CommitDlgSizer"));\r
590                 RECT rectSplitter;\r
591                 m_wndSplitter.GetWindowRect(&rectSplitter);\r
592                 ScreenToClient(&rectSplitter);\r
593                 regPos = rectSplitter.top;\r
594         }\r
595 }\r
596 \r
597 UINT CCommitDlg::StatusThreadEntry(LPVOID pVoid)\r
598 {\r
599         return ((CCommitDlg*)pVoid)->StatusThread();\r
600 }\r
601 \r
602 UINT CCommitDlg::StatusThread()\r
603 {\r
604         //get the status of all selected file/folders recursively\r
605         //and show the ones which have to be committed to the user\r
606         //in a list control. \r
607         InterlockedExchange(&m_bBlock, TRUE);\r
608         InterlockedExchange(&m_bThreadRunning, TRUE);// so the main thread knows that this thread is still running\r
609         InterlockedExchange(&m_bRunThread, TRUE);       // if this is set to FALSE, the thread should stop\r
610         m_bCancelled = false;\r
611 \r
612         DialogEnableWindow(IDOK, false);\r
613         DialogEnableWindow(IDC_SHOWUNVERSIONED, false);\r
614         DialogEnableWindow(IDC_SELECTALL, false);\r
615         GetDlgItem(IDC_EXTERNALWARNING)->ShowWindow(SW_HIDE);\r
616         DialogEnableWindow(IDC_EXTERNALWARNING, false);\r
617     // read the list of recent log entries before querying the WC for status\r
618     // -> the user may select one and modify / update it while we are crawling the WC\r
619 #if 0\r
620         if (m_History.GetCount()==0)\r
621         {\r
622                 CString reg;\r
623                 if (m_ListCtrl.m_sUUID.IsEmpty() && m_pathList.GetCount()>0)\r
624                 {\r
625                         Git Git;\r
626                         reg.Format(_T("Software\\TortoiseGit\\History\\commit%s"), (LPCTSTR)Git.GetUUIDFromPath(m_pathList[0]));\r
627                 }\r
628                 else\r
629                         reg.Format(_T("Software\\TortoiseGit\\History\\commit%s"), (LPCTSTR)m_ListCtrl.m_sUUID);\r
630                 m_History.Load(reg, _T("logmsgs"));\r
631         }\r
632 #endif\r
633     // Initialise the list control with the status of the files/folders below us\r
634         BOOL success = m_ListCtrl.GetStatus(m_pathList);\r
635 \r
636         //m_ListCtrl.UpdateFileList(git_revnum_t(GIT_REV_ZERO));\r
637         if(this->m_bShowUnversioned)\r
638                 m_ListCtrl.UpdateFileList(CGitStatusListCtrl::FILELIST_UNVER);\r
639         \r
640         m_ListCtrl.CheckIfChangelistsArePresent(false);\r
641 \r
642         DWORD dwShow = SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWLOCKS | SVNSLC_SHOWINCHANGELIST;\r
643         dwShow |= DWORD(m_regAddBeforeCommit) ? SVNSLC_SHOWUNVERSIONED : 0;\r
644         if (success)\r
645         {\r
646                 if (m_checkedPathList.GetCount())\r
647                         m_ListCtrl.Show(dwShow, m_checkedPathList);\r
648                 else\r
649                 {\r
650                         DWORD dwCheck = m_bSelectFilesForCommit ?SVNSLC_SHOWDIRECTS|SVNSLC_SHOWMODIFIED|SVNSLC_SHOWADDED|SVNSLC_SHOWREMOVED\r
651                                 |SVNSLC_SHOWREPLACED|SVNSLC_SHOWMERGED|SVNSLC_SHOWLOCKS : 0;\r
652                         m_ListCtrl.Show(dwShow, dwCheck);\r
653                         m_bSelectFilesForCommit = true;\r
654                 }\r
655 \r
656                 if (m_ListCtrl.HasExternalsFromDifferentRepos())\r
657                 {\r
658                         GetDlgItem(IDC_EXTERNALWARNING)->ShowWindow(SW_SHOW);\r
659                         DialogEnableWindow(IDC_EXTERNALWARNING, TRUE);\r
660                 }\r
661                 \r
662                 SetDlgItemText(IDC_COMMIT_TO, g_Git.GetCurrentBranch());\r
663                 m_tooltips.AddTool(GetDlgItem(IDC_STATISTICS), m_ListCtrl.GetStatisticsString());\r
664         }\r
665         CString logmsg;\r
666         GetDlgItemText(IDC_LOGMESSAGE, logmsg);\r
667         DialogEnableWindow(IDOK, logmsg.GetLength() >= m_ProjectProperties.nMinLogSize);\r
668         if (!success)\r
669         {\r
670                 if (!m_ListCtrl.GetLastErrorMessage().IsEmpty())\r
671                         m_ListCtrl.SetEmptyString(m_ListCtrl.GetLastErrorMessage());\r
672                 m_ListCtrl.Show(dwShow);\r
673         }\r
674         if ((m_ListCtrl.GetItemCount()==0)&&(m_ListCtrl.HasUnversionedItems()))\r
675         {\r
676 //              if (CMessageBox::Show(m_hWnd, IDS_COMMITDLG_NOTHINGTOCOMMITUNVERSIONED, IDS_APPNAME, MB_ICONINFORMATION | MB_YESNO)==IDYES)\r
677 //              {\r
678 //                      m_bShowUnversioned = TRUE;\r
679 //                      GetDlgItem(IDC_SHOWUNVERSIONED)->SendMessage(BM_SETCHECK, BST_CHECKED);\r
680 //                      DWORD dwShow = SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWUNVERSIONED | SVNSLC_SHOWLOCKS;\r
681 //                      m_ListCtrl.Show(dwShow);\r
682 //              }\r
683         }\r
684 \r
685 //      CTGitPath commonDir = m_ListCtrl.GetCommonDirectory(false);\r
686 //      SetWindowText(m_sWindowTitle + _T(" - ") + commonDir.GetWinPathString());\r
687 \r
688         m_autolist.clear();\r
689         // we don't have to block the commit dialog while we fetch the\r
690         // auto completion list.\r
691         m_pathwatcher.ClearChangedPaths();\r
692         InterlockedExchange(&m_bBlock, FALSE);\r
693         if ((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\Autocompletion"), TRUE)==TRUE)\r
694         {\r
695                 m_ListCtrl.Block(TRUE, TRUE);\r
696                 GetAutocompletionList();\r
697                 m_ListCtrl.Block(FALSE, FALSE);\r
698         }\r
699         if (m_bRunThread)\r
700         {\r
701                 DialogEnableWindow(IDC_SHOWUNVERSIONED, true);\r
702                 DialogEnableWindow(IDC_SELECTALL, true);\r
703                 if (m_ListCtrl.HasChangeLists())\r
704                         DialogEnableWindow(IDC_KEEPLISTS, true);\r
705                 if (m_ListCtrl.HasLocks())\r
706                         DialogEnableWindow(IDC_KEEPLOCK, true);\r
707                 // we have the list, now signal the main thread about it\r
708                 SendMessage(WM_AUTOLISTREADY);  // only send the message if the thread wasn't told to quit!\r
709         }\r
710 \r
711         InterlockedExchange(&m_bRunThread, FALSE);\r
712         InterlockedExchange(&m_bThreadRunning, FALSE);\r
713         // force the cursor to normal\r
714         RefreshCursor();\r
715 \r
716         return 0;\r
717 }\r
718 \r
719 void CCommitDlg::OnCancel()\r
720 {\r
721         m_bCancelled = true;\r
722         if (m_bBlock)\r
723                 return;\r
724         m_pathwatcher.Stop();\r
725         if (m_bThreadRunning)\r
726         {\r
727                 InterlockedExchange(&m_bRunThread, FALSE);\r
728                 WaitForSingleObject(m_pThread->m_hThread, 1000);\r
729                 if (m_bThreadRunning)\r
730                 {\r
731                         // we gave the thread a chance to quit. Since the thread didn't\r
732                         // listen to us we have to kill it.\r
733                         TerminateThread(m_pThread->m_hThread, (DWORD)-1);\r
734                         InterlockedExchange(&m_bThreadRunning, FALSE);\r
735                 }\r
736         }\r
737         UpdateData();\r
738         m_sBugID.Trim();\r
739         m_sLogMessage = m_cLogMessage.GetText();\r
740         if (!m_sBugID.IsEmpty())\r
741         {\r
742                 m_sBugID.Replace(_T(", "), _T(","));\r
743                 m_sBugID.Replace(_T(" ,"), _T(","));\r
744                 CString sBugID = m_ProjectProperties.sMessage;\r
745                 sBugID.Replace(_T("%BUGID%"), m_sBugID);\r
746                 if (m_ProjectProperties.bAppend)\r
747                         m_sLogMessage += _T("\n") + sBugID + _T("\n");\r
748                 else\r
749                         m_sLogMessage = sBugID + _T("\n") + m_sLogMessage;\r
750         }\r
751         if (m_ProjectProperties.sLogTemplate.Compare(m_sLogMessage) != 0)\r
752                 m_History.AddEntry(m_sLogMessage);\r
753         m_History.Save();\r
754         SaveSplitterPos();\r
755         CResizableStandAloneDialog::OnCancel();\r
756 }\r
757 \r
758 void CCommitDlg::OnBnClickedSelectall()\r
759 {\r
760         m_tooltips.Pop();       // hide the tooltips\r
761         UINT state = (m_SelectAll.GetState() & 0x0003);\r
762         if (state == BST_INDETERMINATE)\r
763         {\r
764                 // It is not at all useful to manually place the checkbox into the indeterminate state...\r
765                 // We will force this on to the unchecked state\r
766                 state = BST_UNCHECKED;\r
767                 m_SelectAll.SetCheck(state);\r
768         }\r
769         m_ListCtrl.SelectAll(state == BST_CHECKED);\r
770 }\r
771 \r
772 BOOL CCommitDlg::PreTranslateMessage(MSG* pMsg)\r
773 {\r
774         if (!m_bBlock)\r
775                 m_tooltips.RelayEvent(pMsg);\r
776         if (pMsg->message == WM_KEYDOWN)\r
777         {\r
778                 switch (pMsg->wParam)\r
779                 {\r
780                 case VK_F5:\r
781                         {\r
782                                 if (m_bBlock)\r
783                                         return CResizableStandAloneDialog::PreTranslateMessage(pMsg);\r
784                                 Refresh();\r
785                         }\r
786                         break;\r
787                 case VK_RETURN:\r
788                         {\r
789                                 if (GetAsyncKeyState(VK_CONTROL)&0x8000)\r
790                                 {\r
791                                         if ( GetDlgItem(IDOK)->IsWindowEnabled() )\r
792                                         {\r
793                                                 PostMessage(WM_COMMAND, IDOK);\r
794                                         }\r
795                                         return TRUE;\r
796                                 }\r
797                                 if ( GetFocus()==GetDlgItem(IDC_BUGID) )\r
798                                 {\r
799                                         // Pressing RETURN in the bug id control\r
800                                         // moves the focus to the message\r
801                                         GetDlgItem(IDC_LOGMESSAGE)->SetFocus();\r
802                                         return TRUE;\r
803                                 }\r
804                         }\r
805                         break;\r
806                 }\r
807         }\r
808 \r
809         return CResizableStandAloneDialog::PreTranslateMessage(pMsg);\r
810 }\r
811 \r
812 void CCommitDlg::Refresh()\r
813 {\r
814         if (m_bThreadRunning)\r
815                 return;\r
816 \r
817         InterlockedExchange(&m_bBlock, TRUE);\r
818         m_pThread = AfxBeginThread(StatusThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);\r
819         if (m_pThread==NULL)\r
820         {\r
821                 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);\r
822                 InterlockedExchange(&m_bBlock, FALSE);\r
823         }\r
824         else\r
825         {\r
826                 m_pThread->m_bAutoDelete = FALSE;\r
827                 m_pThread->ResumeThread();\r
828         }\r
829 }\r
830 \r
831 void CCommitDlg::OnBnClickedHelp()\r
832 {\r
833         OnHelp();\r
834 }\r
835 \r
836 void CCommitDlg::OnBnClickedShowunversioned()\r
837 {\r
838         m_tooltips.Pop();       // hide the tooltips\r
839         UpdateData();\r
840         m_regAddBeforeCommit = m_bShowUnversioned;\r
841         if (!m_bBlock)\r
842         {\r
843                 DWORD dwShow = m_ListCtrl.GetShowFlags();\r
844                 if (DWORD(m_regAddBeforeCommit))\r
845                         dwShow |= SVNSLC_SHOWUNVERSIONED;\r
846                 else\r
847                         dwShow &= ~SVNSLC_SHOWUNVERSIONED;\r
848                 if(dwShow & SVNSLC_SHOWUNVERSIONED)\r
849                 {\r
850                         m_ListCtrl.GetStatus(this->m_pathList,false,false,true);\r
851                 }\r
852                 m_ListCtrl.Show(dwShow);\r
853         }\r
854 }\r
855 \r
856 void CCommitDlg::OnStnClickedExternalwarning()\r
857 {\r
858         m_tooltips.Popup();\r
859 }\r
860 \r
861 void CCommitDlg::OnEnChangeLogmessage()\r
862 {\r
863         UpdateOKButton();\r
864 }\r
865 \r
866 LRESULT CCommitDlg::OnGitStatusListCtrlItemCountChanged(WPARAM, LPARAM)\r
867 {\r
868 #if 0\r
869         if ((m_ListCtrl.GetItemCount() == 0)&&(m_ListCtrl.HasUnversionedItems())&&(!m_bShowUnversioned))\r
870         {\r
871                 if (CMessageBox::Show(*this, IDS_COMMITDLG_NOTHINGTOCOMMITUNVERSIONED, IDS_APPNAME, MB_ICONINFORMATION | MB_YESNO)==IDYES)\r
872                 {\r
873                         m_bShowUnversioned = TRUE;\r
874                         DWORD dwShow = GitSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GitSLC_SHOWUNVERSIONED | GitSLC_SHOWLOCKS;\r
875                         m_ListCtrl.Show(dwShow);\r
876                         UpdateData(FALSE);\r
877                 }\r
878         }\r
879 #endif\r
880         return 0;\r
881 }\r
882 \r
883 LRESULT CCommitDlg::OnGitStatusListCtrlNeedsRefresh(WPARAM, LPARAM)\r
884 {\r
885         Refresh();\r
886         return 0;\r
887 }\r
888 \r
889 LRESULT CCommitDlg::OnFileDropped(WPARAM, LPARAM lParam)\r
890 {\r
891 #if 0\r
892         BringWindowToTop();\r
893         SetForegroundWindow();\r
894         SetActiveWindow();\r
895         // if multiple files/folders are dropped\r
896         // this handler is called for every single item\r
897         // separately.\r
898         // To avoid creating multiple refresh threads and\r
899         // causing crashes, we only add the items to the\r
900         // list control and start a timer.\r
901         // When the timer expires, we start the refresh thread,\r
902         // but only if it isn't already running - otherwise we\r
903         // restart the timer.\r
904         CTGitPath path;\r
905         path.SetFromWin((LPCTSTR)lParam);\r
906 \r
907         // just add all the items we get here.\r
908         // if the item is versioned, the add will fail but nothing\r
909         // more will happen.\r
910         Git Git;\r
911         Git.Add(CTGitPathList(path), &m_ProjectProperties, Git_depth_empty, false, true, true);\r
912 \r
913         if (!m_ListCtrl.HasPath(path))\r
914         {\r
915                 if (m_pathList.AreAllPathsFiles())\r
916                 {\r
917                         m_pathList.AddPath(path);\r
918                         m_pathList.RemoveDuplicates();\r
919                         m_updatedPathList.AddPath(path);\r
920                         m_updatedPathList.RemoveDuplicates();\r
921                 }\r
922                 else\r
923                 {\r
924                         // if the path list contains folders, we have to check whether\r
925                         // our just (maybe) added path is a child of one of those. If it is\r
926                         // a child of a folder already in the list, we must not add it. Otherwise\r
927                         // that path could show up twice in the list.\r
928                         bool bHasParentInList = false;\r
929                         for (int i=0; i<m_pathList.GetCount(); ++i)\r
930                         {\r
931                                 if (m_pathList[i].IsAncestorOf(path))\r
932                                 {\r
933                                         bHasParentInList = true;\r
934                                         break;\r
935                                 }\r
936                         }\r
937                         if (!bHasParentInList)\r
938                         {\r
939                                 m_pathList.AddPath(path);\r
940                                 m_pathList.RemoveDuplicates();\r
941                                 m_updatedPathList.AddPath(path);\r
942                                 m_updatedPathList.RemoveDuplicates();\r
943                         }\r
944                 }\r
945         }\r
946         \r
947         // Always start the timer, since the status of an existing item might have changed\r
948         SetTimer(REFRESHTIMER, 200, NULL);\r
949         ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());\r
950 #endif\r
951         return 0;\r
952 }\r
953 \r
954 LRESULT CCommitDlg::OnAutoListReady(WPARAM, LPARAM)\r
955 {\r
956         m_cLogMessage.SetAutoCompletionList(m_autolist, '*');\r
957         return 0;\r
958 }\r
959 \r
960 //////////////////////////////////////////////////////////////////////////\r
961 // functions which run in the status thread\r
962 //////////////////////////////////////////////////////////////////////////\r
963 \r
964 void CCommitDlg::ParseRegexFile(const CString& sFile, std::map<CString, CString>& mapRegex)\r
965 {\r
966         CString strLine;\r
967         try\r
968         {\r
969                 CStdioFile file(sFile, CFile::typeText | CFile::modeRead | CFile::shareDenyWrite);\r
970                 while (m_bRunThread && file.ReadString(strLine))\r
971                 {\r
972                         int eqpos = strLine.Find('=');\r
973                         CString rgx;\r
974                         rgx = strLine.Mid(eqpos+1).Trim();\r
975 \r
976                         int pos = -1;\r
977                         while (((pos = strLine.Find(','))>=0)&&(pos < eqpos))\r
978                         {\r
979                                 mapRegex[strLine.Left(pos)] = rgx;\r
980                                 strLine = strLine.Mid(pos+1).Trim();\r
981                         }\r
982                         mapRegex[strLine.Left(strLine.Find('=')).Trim()] = rgx;\r
983                 }\r
984                 file.Close();\r
985         }\r
986         catch (CFileException* pE)\r
987         {\r
988                 TRACE("CFileException loading auto list regex file\n");\r
989                 pE->Delete();\r
990                 return;\r
991         }\r
992 }\r
993 void CCommitDlg::GetAutocompletionList()\r
994 {\r
995         // the auto completion list is made of strings from each selected files.\r
996         // the strings used are extracted from the files with regexes found\r
997         // in the file "autolist.txt".\r
998         // the format of that file is:\r
999         // file extensions separated with commas '=' regular expression to use\r
1000         // example:\r
1001         // .h, .hpp = (?<=class[\s])\b\w+\b|(\b\w+(?=[\s ]?\(\);))\r
1002         // .cpp = (?<=[^\s]::)\b\w+\b\r
1003         \r
1004         std::map<CString, CString> mapRegex;\r
1005         CString sRegexFile = CPathUtils::GetAppDirectory();\r
1006         CRegDWORD regtimeout = CRegDWORD(_T("Software\\TortoiseGit\\AutocompleteParseTimeout"), 5);\r
1007         DWORD timeoutvalue = regtimeout*1000;\r
1008         sRegexFile += _T("autolist.txt");\r
1009         if (!m_bRunThread)\r
1010                 return;\r
1011         ParseRegexFile(sRegexFile, mapRegex);\r
1012         SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, sRegexFile.GetBuffer(MAX_PATH+1));\r
1013         sRegexFile.ReleaseBuffer();\r
1014         sRegexFile += _T("\\TortoiseGit\\autolist.txt");\r
1015         if (PathFileExists(sRegexFile))\r
1016         {\r
1017                 ParseRegexFile(sRegexFile, mapRegex);\r
1018         }\r
1019         DWORD starttime = GetTickCount();\r
1020 \r
1021         // now we have two arrays of strings, where the first array contains all\r
1022         // file extensions we can use and the second the corresponding regex strings\r
1023         // to apply to those files.\r
1024 \r
1025         // the next step is to go over all files shown in the commit dialog\r
1026         // and scan them for strings we can use\r
1027         int nListItems = m_ListCtrl.GetItemCount();\r
1028 \r
1029         for (int i=0; i<nListItems && m_bRunThread; ++i)\r
1030         {\r
1031                 // stop parsing after timeout\r
1032                 if ((!m_bRunThread) || (GetTickCount() - starttime > timeoutvalue))\r
1033                         return;\r
1034 \r
1035                 CTGitPath *path = (CTGitPath*)m_ListCtrl.GetItemData(i);\r
1036 \r
1037                 CString sPartPath =path->GetGitPathString();\r
1038                 m_autolist.insert(sPartPath);\r
1039 \r
1040 //              const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(i);\r
1041 //              if (!entry)\r
1042 //                      continue;\r
1043                 \r
1044                 // add the path parts to the auto completion list too\r
1045 //              CString sPartPath = entry->GetRelativeGitPath();\r
1046 //              m_autolist.insert(sPartPath);\r
1047 \r
1048 \r
1049                 int pos = 0;\r
1050                 int lastPos = 0;\r
1051                 while ((pos = sPartPath.Find('/', pos)) >= 0)\r
1052                 {\r
1053                         pos++;\r
1054                         lastPos = pos;\r
1055                         m_autolist.insert(sPartPath.Mid(pos));\r
1056                 }\r
1057 \r
1058                 // Last inserted entry is a file name.\r
1059                 // Some users prefer to also list file name without extension.\r
1060                 if (CRegDWORD(_T("Software\\TortoiseGit\\AutocompleteRemovesExtensions"), FALSE))\r
1061                 {\r
1062                         int dotPos = sPartPath.ReverseFind('.');\r
1063                         if ((dotPos >= 0) && (dotPos > lastPos))\r
1064                                 m_autolist.insert(sPartPath.Mid(lastPos, dotPos - lastPos));\r
1065                 }\r
1066 #if 0\r
1067                 if ((entry->status <= Git_wc_status_normal)||(entry->status == Git_wc_status_ignored))\r
1068                         continue;\r
1069 \r
1070                 CString sExt = entry->GetPath().GetFileExtension();\r
1071                 sExt.MakeLower();\r
1072                 // find the regex string which corresponds to the file extension\r
1073                 CString rdata = mapRegex[sExt];\r
1074                 if (rdata.IsEmpty())\r
1075                         continue;\r
1076 \r
1077                 ScanFile(entry->GetPath().GetWinPathString(), rdata);\r
1078                 if ((entry->textstatus != Git_wc_status_unversioned) &&\r
1079                         (entry->textstatus != Git_wc_status_none) &&\r
1080                         (entry->textstatus != Git_wc_status_ignored) &&\r
1081                         (entry->textstatus != Git_wc_status_added) &&\r
1082                         (entry->textstatus != Git_wc_status_normal))\r
1083                 {\r
1084                         CTGitPath basePath = Git::GetPristinePath(entry->GetPath());\r
1085                         if (!basePath.IsEmpty())\r
1086                                 ScanFile(basePath.GetWinPathString(), rdata);\r
1087                 }\r
1088 #endif\r
1089         }\r
1090         ATLTRACE(_T("Auto completion list loaded in %d msec\n"), GetTickCount() - starttime);\r
1091 }\r
1092 \r
1093 void CCommitDlg::ScanFile(const CString& sFilePath, const CString& sRegex)\r
1094 {\r
1095         wstring sFileContent;\r
1096         HANDLE hFile = CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);\r
1097         if (hFile != INVALID_HANDLE_VALUE)\r
1098         {\r
1099                 DWORD size = GetFileSize(hFile, NULL);\r
1100                 if (size > 1000000L)\r
1101                 {\r
1102                         // no files bigger than 1 Meg\r
1103                         CloseHandle(hFile);\r
1104                         return;\r
1105                 }\r
1106                 // allocate memory to hold file contents\r
1107                 char * buffer = new char[size];\r
1108                 DWORD readbytes;\r
1109                 ReadFile(hFile, buffer, size, &readbytes, NULL);\r
1110                 CloseHandle(hFile);\r
1111                 int opts = 0;\r
1112                 IsTextUnicode(buffer, readbytes, &opts);\r
1113                 if (opts & IS_TEXT_UNICODE_NULL_BYTES)\r
1114                 {\r
1115                         delete [] buffer;\r
1116                         return;\r
1117                 }\r
1118                 if (opts & IS_TEXT_UNICODE_UNICODE_MASK)\r
1119                 {\r
1120                         sFileContent = wstring((wchar_t*)buffer, readbytes/sizeof(WCHAR));\r
1121                 }\r
1122                 if ((opts & IS_TEXT_UNICODE_NOT_UNICODE_MASK)||(opts == 0))\r
1123                 {\r
1124                         int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, NULL, 0);\r
1125                         wchar_t * pWideBuf = new wchar_t[ret];\r
1126                         int ret2 = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, pWideBuf, ret);\r
1127                         if (ret2 == ret)\r
1128                                 sFileContent = wstring(pWideBuf, ret);\r
1129                         delete [] pWideBuf;\r
1130                 }\r
1131                 delete [] buffer;\r
1132         }\r
1133         if (sFileContent.empty()|| !m_bRunThread)\r
1134         {\r
1135                 return;\r
1136         }\r
1137 \r
1138         try\r
1139         {\r
1140                 const tr1::wregex regCheck(sRegex, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);\r
1141                 const tr1::wsregex_iterator end;\r
1142                 wstring s = sFileContent;\r
1143                 for (tr1::wsregex_iterator it(s.begin(), s.end(), regCheck); it != end; ++it)\r
1144                 {\r
1145                         const tr1::wsmatch match = *it;\r
1146                         for (size_t i=1; i<match.size(); ++i)\r
1147                         {\r
1148                                 if (match[i].second-match[i].first)\r
1149                                 {\r
1150                                         ATLTRACE(_T("matched keyword : %s\n"), wstring(match[i]).c_str());\r
1151                                         m_autolist.insert(wstring(match[i]).c_str());\r
1152                                 }\r
1153                         }\r
1154                 }\r
1155         }\r
1156         catch (exception) {}\r
1157 }\r
1158 \r
1159 // CSciEditContextMenuInterface\r
1160 void CCommitDlg::InsertMenuItems(CMenu& mPopup, int& nCmd)\r
1161 {\r
1162         CString sMenuItemText(MAKEINTRESOURCE(IDS_COMMITDLG_POPUP_PASTEFILELIST));\r
1163         m_nPopupPasteListCmd = nCmd++;\r
1164         mPopup.AppendMenu(MF_STRING | MF_ENABLED, m_nPopupPasteListCmd, sMenuItemText);\r
1165 }\r
1166 \r
1167 bool CCommitDlg::HandleMenuItemClick(int cmd, CSciEdit * pSciEdit)\r
1168 {\r
1169 #if 0\r
1170         if (m_bBlock)\r
1171                 return false;\r
1172         if (cmd == m_nPopupPasteListCmd)\r
1173         {\r
1174                 CString logmsg;\r
1175                 TCHAR buf[MAX_STATUS_STRING_LENGTH];\r
1176                 int nListItems = m_ListCtrl.GetItemCount();\r
1177                 for (int i=0; i<nListItems; ++i)\r
1178                 {\r
1179                         CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(i);\r
1180                         if (entry->IsChecked())\r
1181                         {\r
1182                                 CString line;\r
1183                                 Git_wc_status_kind status = entry->status;\r
1184                                 if (status == Git_wc_status_unversioned)\r
1185                                         status = Git_wc_status_added;\r
1186                                 if (status == Git_wc_status_missing)\r
1187                                         status = Git_wc_status_deleted;\r
1188                                 WORD langID = (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), GetUserDefaultLangID());\r
1189                                 if (m_ProjectProperties.bFileListInEnglish)\r
1190                                         langID = 1033;\r
1191                                 GitStatus::GetStatusString(AfxGetResourceHandle(), status, buf, sizeof(buf)/sizeof(TCHAR), langID);\r
1192                                 line.Format(_T("%-10s %s\r\n"), buf, (LPCTSTR)m_ListCtrl.GetItemText(i,0));\r
1193                                 logmsg += line;\r
1194                         }\r
1195                 }\r
1196                 pSciEdit->InsertText(logmsg);\r
1197                 return true;\r
1198         }\r
1199 #endif\r
1200         return false;\r
1201 }\r
1202 \r
1203 void CCommitDlg::OnTimer(UINT_PTR nIDEvent)\r
1204 {\r
1205         switch (nIDEvent)\r
1206         {\r
1207         case ENDDIALOGTIMER:\r
1208                 KillTimer(ENDDIALOGTIMER);\r
1209                 EndDialog(0);\r
1210                 break;\r
1211         case REFRESHTIMER:\r
1212                 if (m_bThreadRunning)\r
1213                 {\r
1214                         SetTimer(REFRESHTIMER, 200, NULL);\r
1215                         ATLTRACE("Wait some more before refreshing\n");\r
1216                 }\r
1217                 else\r
1218                 {\r
1219                         KillTimer(REFRESHTIMER);\r
1220                         ATLTRACE("Refreshing after items dropped\n");\r
1221                         Refresh();\r
1222                 }\r
1223                 break;\r
1224         }\r
1225         __super::OnTimer(nIDEvent);\r
1226 }\r
1227 \r
1228 void CCommitDlg::OnBnClickedHistory()\r
1229 {\r
1230         m_tooltips.Pop();       // hide the tooltips\r
1231         if (m_pathList.GetCount() == 0)\r
1232                 return;\r
1233 #if 0\r
1234         CHistoryDlg historyDlg;\r
1235         historyDlg.SetHistory(m_History);\r
1236         if (historyDlg.DoModal() != IDOK)\r
1237                 return;\r
1238 \r
1239         CString sMsg = historyDlg.GetSelectedText();\r
1240         if (sMsg != m_cLogMessage.GetText().Left(sMsg.GetLength()))\r
1241         {\r
1242                 CString sBugID = m_ProjectProperties.GetBugIDFromLog(sMsg);\r
1243                 if (!sBugID.IsEmpty())\r
1244                 {\r
1245                         SetDlgItemText(IDC_BUGID, sBugID);\r
1246                 }\r
1247                 if (m_ProjectProperties.sLogTemplate.Compare(m_cLogMessage.GetText())!=0)\r
1248                         m_cLogMessage.InsertText(sMsg, !m_cLogMessage.GetText().IsEmpty());\r
1249                 else\r
1250                         m_cLogMessage.SetText(sMsg);\r
1251         }\r
1252 \r
1253         UpdateOKButton();\r
1254         GetDlgItem(IDC_LOGMESSAGE)->SetFocus();\r
1255 #endif\r
1256 }\r
1257 \r
1258 void CCommitDlg::OnBnClickedBugtraqbutton()\r
1259 {\r
1260 #if 0\r
1261         m_tooltips.Pop();       // hide the tooltips\r
1262         CString sMsg = m_cLogMessage.GetText();\r
1263 \r
1264         if (m_BugTraqProvider == NULL)\r
1265                 return;\r
1266 \r
1267         BSTR parameters = m_bugtraq_association.GetParameters().AllocSysString();\r
1268         BSTR commonRoot = SysAllocString(m_pathList.GetCommonRoot().GetDirectory().GetWinPath());\r
1269         SAFEARRAY *pathList = SafeArrayCreateVector(VT_BSTR, 0, m_pathList.GetCount());\r
1270 \r
1271         for (LONG index = 0; index < m_pathList.GetCount(); ++index)\r
1272                 SafeArrayPutElement(pathList, &index, m_pathList[index].GetGitPathString().AllocSysString());\r
1273 \r
1274         BSTR originalMessage = sMsg.AllocSysString();\r
1275         BSTR temp = NULL;\r
1276 \r
1277         // first try the IBugTraqProvider2 interface\r
1278         CComPtr<IBugTraqProvider2> pProvider2 = NULL;\r
1279         HRESULT hr = m_BugTraqProvider.QueryInterface(&pProvider2);\r
1280         if (SUCCEEDED(hr))\r
1281         {\r
1282                 CString common = m_ListCtrl.GetCommonURL(false).GetGitPathString();\r
1283                 BSTR repositoryRoot = common.AllocSysString();\r
1284                 if (FAILED(hr = pProvider2->GetCommitMessage2(GetSafeHwnd(), parameters, repositoryRoot, commonRoot, pathList, originalMessage, &temp)))\r
1285                 {\r
1286                         CString sErr;\r
1287                         sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());\r
1288                         CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);\r
1289                 }\r
1290                 else\r
1291                         m_cLogMessage.SetText(temp);\r
1292         }\r
1293         else\r
1294         {\r
1295                 // if IBugTraqProvider2 failed, try IBugTraqProvider\r
1296                 CComPtr<IBugTraqProvider> pProvider = NULL;\r
1297                 HRESULT hr = m_BugTraqProvider.QueryInterface(&pProvider);\r
1298                 if (FAILED(hr))\r
1299                 {\r
1300                         CString sErr;\r
1301                         sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, (LPCTSTR)m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());\r
1302                         CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);\r
1303                         return;\r
1304                 }\r
1305 \r
1306                 if (FAILED(hr = pProvider->GetCommitMessage(GetSafeHwnd(), parameters, commonRoot, pathList, originalMessage, &temp)))\r
1307                 {\r
1308                         CString sErr;\r
1309                         sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());\r
1310                         CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);\r
1311                 }\r
1312                 else\r
1313                         m_cLogMessage.SetText(temp);\r
1314         }\r
1315 \r
1316         m_cLogMessage.SetFocus();\r
1317 \r
1318         SysFreeString(temp);\r
1319 #endif\r
1320 }\r
1321 \r
1322 LRESULT CCommitDlg::OnGitStatusListCtrlCheckChanged(WPARAM, LPARAM)\r
1323 {\r
1324         UpdateOKButton();\r
1325         return 0;\r
1326 }\r
1327 \r
1328 void CCommitDlg::UpdateOKButton()\r
1329 {\r
1330 #if 0\r
1331         BOOL bValidLogSize = FALSE;\r
1332 \r
1333     if (m_cLogMessage.GetText().GetLength() >= m_ProjectProperties.nMinLogSize)\r
1334                 bValidLogSize = !m_bBlock;\r
1335 \r
1336         LONG nSelectedItems = m_ListCtrl.GetSelected();\r
1337         DialogEnableWindow(IDOK, bValidLogSize && nSelectedItems>0);\r
1338 #endif\r
1339 }\r
1340 \r
1341 \r
1342 LRESULT CCommitDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)\r
1343 {\r
1344         switch (message) {\r
1345         case WM_NOTIFY:\r
1346                 if (wParam == IDC_SPLITTER)\r
1347                 { \r
1348                         SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;\r
1349                         DoSize(pHdr->delta);\r
1350                 }\r
1351                 break;\r
1352         }\r
1353 \r
1354         return __super::DefWindowProc(message, wParam, lParam);\r
1355 }\r
1356 \r
1357 void CCommitDlg::SetSplitterRange()\r
1358 {\r
1359         if ((m_ListCtrl)&&(m_cLogMessage))\r
1360         {\r
1361                 CRect rcTop;\r
1362                 m_cLogMessage.GetWindowRect(rcTop);\r
1363                 ScreenToClient(rcTop);\r
1364                 CRect rcMiddle;\r
1365                 m_ListCtrl.GetWindowRect(rcMiddle);\r
1366                 ScreenToClient(rcMiddle);\r
1367                 if (rcMiddle.Height() && rcMiddle.Width())\r
1368                         m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);\r
1369         }\r
1370 }\r
1371 \r
1372 void CCommitDlg::DoSize(int delta)\r
1373 {\r
1374         RemoveAnchor(IDC_MESSAGEGROUP);\r
1375         RemoveAnchor(IDC_LOGMESSAGE);\r
1376         RemoveAnchor(IDC_SPLITTER);\r
1377         RemoveAnchor(IDC_SIGNOFF);\r
1378         RemoveAnchor(IDC_LISTGROUP);\r
1379         RemoveAnchor(IDC_FILELIST);\r
1380         CSplitterControl::ChangeHeight(&m_cLogMessage, delta, CW_TOPALIGN);\r
1381         CSplitterControl::ChangeHeight(GetDlgItem(IDC_MESSAGEGROUP), delta, CW_TOPALIGN);\r
1382         CSplitterControl::ChangeHeight(&m_ListCtrl, -delta, CW_BOTTOMALIGN);\r
1383         CSplitterControl::ChangeHeight(GetDlgItem(IDC_LISTGROUP), -delta, CW_BOTTOMALIGN);\r
1384         CSplitterControl::ChangePos(GetDlgItem(IDC_SIGNOFF),0,delta);\r
1385         AddAnchor(IDC_MESSAGEGROUP, TOP_LEFT, TOP_RIGHT);\r
1386         AddAnchor(IDC_LOGMESSAGE, TOP_LEFT, TOP_RIGHT);\r
1387         AddAnchor(IDC_SPLITTER, TOP_LEFT, TOP_RIGHT);\r
1388         AddAnchor(IDC_LISTGROUP, TOP_LEFT, BOTTOM_RIGHT);\r
1389         AddAnchor(IDC_FILELIST, TOP_LEFT, BOTTOM_RIGHT);\r
1390         AddAnchor(IDC_SIGNOFF,TOP_RIGHT);\r
1391         ArrangeLayout();\r
1392         // adjust the minimum size of the dialog to prevent the resizing from\r
1393         // moving the list control too far down.\r
1394         CRect rcLogMsg;\r
1395         m_cLogMessage.GetClientRect(rcLogMsg);\r
1396         SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_LogMsgOrigRect.Height()+rcLogMsg.Height()));\r
1397 \r
1398         SetSplitterRange();\r
1399         m_cLogMessage.Invalidate();\r
1400         GetDlgItem(IDC_LOGMESSAGE)->Invalidate();\r
1401 }\r
1402 \r
1403 void CCommitDlg::OnSize(UINT nType, int cx, int cy)\r
1404 {\r
1405     // first, let the resizing take place\r
1406     __super::OnSize(nType, cx, cy);\r
1407 \r
1408     //set range\r
1409     SetSplitterRange();\r
1410 }\r
1411 \r
1412 \r
1413 void CCommitDlg::OnBnClickedSignOff()\r
1414 {\r
1415         // TODO: Add your control notification handler code here\r
1416         CString str;\r
1417         CString username;\r
1418         CString email;\r
1419         username=g_Git.GetUserName();\r
1420         email=g_Git.GetUserEmail();\r
1421         username.Remove(_T('\n'));\r
1422         email.Remove(_T('\n'));\r
1423         str.Format(_T("Signed-off-by: %s <%s>\n"),username,email);\r
1424 \r
1425         m_cLogMessage.SetText(m_cLogMessage.GetText()+_T("\r\n\r\n")+str);\r
1426 }\r
1427 \r
1428 void CCommitDlg::OnStnClickedCommitlabel()\r
1429 {\r
1430         // TODO: Add your control notification handler code here\r
1431 }\r