BrowseRefs: Context menu enhancements
[TortoiseGit.git] / src / TortoiseProc / CommitDlg.cpp
blobc78eb55a61f21eb8aad8259c74dd2789934913ef
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - 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.
19 #include "stdafx.h"
20 #include "TortoiseProc.h"
21 #include "CommitDlg.h"
22 #include "DirFileEnum.h"
23 //#include "GitConfig.h"
24 //#include "GitProperties.h"
25 #include "MessageBox.h"
26 #include "AppUtils.h"
27 #include "PathUtils.h"
28 #include "Git.h"
29 #include "Registry.h"
30 #include "GitStatus.h"
31 #include "HistoryDlg.h"
32 #include "Hooks.h"
33 #include "CommonResource.h"
34 #include "UnicodeUtils.h"
35 #include "ProgressDlg.h"
36 #include "ShellUpdater.h"
38 #ifdef _DEBUG
39 #define new DEBUG_NEW
40 #undef THIS_FILE
41 static char THIS_FILE[] = __FILE__;
42 #endif
44 UINT CCommitDlg::WM_AUTOLISTREADY = RegisterWindowMessage(_T("TORTOISEGIT_AUTOLISTREADY_MSG"));
46 IMPLEMENT_DYNAMIC(CCommitDlg, CResizableStandAloneDialog)
47 CCommitDlg::CCommitDlg(CWnd* pParent /*=NULL*/)
48 : CResizableStandAloneDialog(CCommitDlg::IDD, pParent)
49 , m_bRecursive(FALSE)
50 , m_bShowUnversioned(FALSE)
51 , m_bBlock(FALSE)
52 , m_bThreadRunning(FALSE)
53 , m_bRunThread(FALSE)
54 , m_pThread(NULL)
55 , m_bWholeProject(FALSE)
56 , m_bKeepChangeList(TRUE)
57 , m_itemsCount(0)
58 , m_bSelectFilesForCommit(TRUE)
60 this->m_bCommitAmend=FALSE;
63 CCommitDlg::~CCommitDlg()
65 if(m_pThread != NULL)
67 delete m_pThread;
71 void CCommitDlg::DoDataExchange(CDataExchange* pDX)
73 CResizableStandAloneDialog::DoDataExchange(pDX);
74 DDX_Control(pDX, IDC_FILELIST, m_ListCtrl);
75 DDX_Control(pDX, IDC_LOGMESSAGE, m_cLogMessage);
76 DDX_Check(pDX, IDC_SHOWUNVERSIONED, m_bShowUnversioned);
77 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
78 DDX_Text(pDX, IDC_BUGID, m_sBugID);
79 DDX_Check(pDX, IDC_WHOLE_PROJECT, m_bWholeProject);
80 DDX_Control(pDX, IDC_SPLITTER, m_wndSplitter);
81 DDX_Check(pDX, IDC_KEEPLISTS, m_bKeepChangeList);
82 DDX_Check(pDX,IDC_COMMIT_AMEND,m_bCommitAmend);
85 BEGIN_MESSAGE_MAP(CCommitDlg, CResizableStandAloneDialog)
86 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
87 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
88 ON_BN_CLICKED(IDC_SHOWUNVERSIONED, OnBnClickedShowunversioned)
89 // ON_BN_CLICKED(IDC_HISTORY, OnBnClickedHistory)
90 ON_BN_CLICKED(IDC_BUGTRAQBUTTON, OnBnClickedBugtraqbutton)
91 ON_EN_CHANGE(IDC_LOGMESSAGE, OnEnChangeLogmessage)
92 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ITEMCOUNTCHANGED, OnGitStatusListCtrlItemCountChanged)
93 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnGitStatusListCtrlNeedsRefresh)
94 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)
95 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_CHECKCHANGED, &CCommitDlg::OnGitStatusListCtrlCheckChanged)
96 ON_REGISTERED_MESSAGE(WM_AUTOLISTREADY, OnAutoListReady)
97 ON_WM_TIMER()
98 ON_WM_SIZE()
99 ON_STN_CLICKED(IDC_EXTERNALWARNING, &CCommitDlg::OnStnClickedExternalwarning)
100 ON_BN_CLICKED(IDC_SIGNOFF, &CCommitDlg::OnBnClickedSignOff)
101 ON_STN_CLICKED(IDC_COMMITLABEL, &CCommitDlg::OnStnClickedCommitlabel)
102 ON_BN_CLICKED(IDC_COMMIT_AMEND, &CCommitDlg::OnBnClickedCommitAmend)
103 ON_BN_CLICKED(IDC_WHOLE_PROJECT, &CCommitDlg::OnBnClickedWholeProject)
104 END_MESSAGE_MAP()
106 BOOL CCommitDlg::OnInitDialog()
108 CResizableStandAloneDialog::OnInitDialog();
110 m_regAddBeforeCommit = CRegDWORD(_T("Software\\TortoiseGit\\AddBeforeCommit"), TRUE);
111 m_bShowUnversioned = m_regAddBeforeCommit;
113 m_History.SetMaxHistoryItems((LONG)CRegDWORD(_T("Software\\TortoiseGit\\MaxHistoryItems"), 25));
115 m_regKeepChangelists = CRegDWORD(_T("Software\\TortoiseGit\\KeepChangeLists"), FALSE);
116 m_bKeepChangeList = m_regKeepChangelists;
118 // GitConfig config;
119 // m_bWholeProject = config.KeepLocks();
121 UpdateData(FALSE);
123 m_ListCtrl.Init(SVNSLC_COLEXT | SVNSLC_COLSTATUS | SVNSLC_COLADD |SVNSLC_COLDEL, _T("CommitDlg"));
124 m_ListCtrl.SetSelectButton(&m_SelectAll);
125 m_ListCtrl.SetStatLabel(GetDlgItem(IDC_STATISTICS));
126 m_ListCtrl.SetCancelBool(&m_bCancelled);
127 m_ListCtrl.SetEmptyString(IDS_COMMITDLG_NOTHINGTOCOMMIT);
128 m_ListCtrl.EnableFileDrop();
129 m_ListCtrl.SetBackgroundImage(IDI_COMMIT_BKG);
131 //this->DialogEnableWindow(IDC_COMMIT_AMEND,FALSE);
132 // m_ProjectProperties.ReadPropsPathList(m_pathList);
133 m_cLogMessage.Init(m_ProjectProperties);
134 m_cLogMessage.SetFont((CString)CRegString(_T("Software\\TortoiseGit\\LogFontName"), _T("Courier New")), (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\LogFontSize"), 8));
135 m_cLogMessage.RegisterContextMenuHandler(this);
137 OnEnChangeLogmessage();
139 m_tooltips.Create(this);
140 m_tooltips.AddTool(IDC_EXTERNALWARNING, IDS_COMMITDLG_EXTERNALS);
141 m_tooltips.AddTool(IDC_COMMIT_AMEND,IDS_COMMIT_AMEND_TT);
142 // m_tooltips.AddTool(IDC_HISTORY, IDS_COMMITDLG_HISTORY_TT);
144 m_SelectAll.SetCheck(BST_INDETERMINATE);
146 #if 0
147 CBugTraqAssociations bugtraq_associations;
148 bugtraq_associations.Load();
150 if (bugtraq_associations.FindProvider(m_pathList, &m_bugtraq_association))
152 GetDlgItem(IDC_BUGID)->ShowWindow(SW_HIDE);
153 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_HIDE);
155 CComPtr<IBugTraqProvider> pProvider;
156 HRESULT hr = pProvider.CoCreateInstance(m_bugtraq_association.GetProviderClass());
157 if (SUCCEEDED(hr))
159 m_BugTraqProvider = pProvider;
160 BSTR temp = NULL;
161 if (SUCCEEDED(hr = pProvider->GetLinkText(GetSafeHwnd(), m_bugtraq_association.GetParameters().AllocSysString(), &temp)))
163 SetDlgItemText(IDC_BUGTRAQBUTTON, temp);
164 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(TRUE);
165 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_SHOW);
168 SysFreeString(temp);
171 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();
173 else if (!m_ProjectProperties.sMessage.IsEmpty())
175 GetDlgItem(IDC_BUGID)->ShowWindow(SW_SHOW);
176 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_SHOW);
177 if (!m_ProjectProperties.sLabel.IsEmpty())
178 SetDlgItemText(IDC_BUGIDLABEL, m_ProjectProperties.sLabel);
179 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_HIDE);
180 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(FALSE);
181 GetDlgItem(IDC_BUGID)->SetFocus();
182 CString sBugID = m_ProjectProperties.GetBugIDFromLog(m_sLogMessage);
183 if (!sBugID.IsEmpty())
185 SetDlgItemText(IDC_BUGID, sBugID);
188 else
189 #endif
191 GetDlgItem(IDC_BUGID)->ShowWindow(SW_HIDE);
192 GetDlgItem(IDC_BUGIDLABEL)->ShowWindow(SW_HIDE);
193 GetDlgItem(IDC_BUGTRAQBUTTON)->ShowWindow(SW_HIDE);
194 GetDlgItem(IDC_BUGTRAQBUTTON)->EnableWindow(FALSE);
195 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();
198 if (!m_sLogMessage.IsEmpty())
199 m_cLogMessage.SetText(m_sLogMessage);
201 GetWindowText(m_sWindowTitle);
203 AdjustControlSize(IDC_SHOWUNVERSIONED);
204 AdjustControlSize(IDC_SELECTALL);
205 AdjustControlSize(IDC_WHOLE_PROJECT);
207 GetClientRect(m_DlgOrigRect);
208 m_cLogMessage.GetClientRect(m_LogMsgOrigRect);
210 AddAnchor(IDC_COMMITLABEL, TOP_LEFT, TOP_RIGHT);
211 AddAnchor(IDC_BUGIDLABEL, TOP_RIGHT);
212 AddAnchor(IDC_BUGID, TOP_RIGHT);
213 AddAnchor(IDC_BUGTRAQBUTTON, TOP_RIGHT);
214 AddAnchor(IDC_COMMIT_TO, TOP_LEFT, TOP_RIGHT);
215 AddAnchor(IDC_MESSAGEGROUP, TOP_LEFT, TOP_RIGHT);
216 // AddAnchor(IDC_HISTORY, TOP_LEFT);
217 AddAnchor(IDC_LOGMESSAGE, TOP_LEFT, TOP_RIGHT);
218 AddAnchor(IDC_SIGNOFF, TOP_RIGHT);
219 AddAnchor(IDC_LISTGROUP, TOP_LEFT, BOTTOM_RIGHT);
220 AddAnchor(IDC_SPLITTER, TOP_LEFT, TOP_RIGHT);
221 AddAnchor(IDC_FILELIST, TOP_LEFT, BOTTOM_RIGHT);
222 AddAnchor(IDC_SHOWUNVERSIONED, BOTTOM_LEFT);
223 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
224 AddAnchor(IDC_EXTERNALWARNING, BOTTOM_RIGHT);
225 AddAnchor(IDC_STATISTICS, BOTTOM_LEFT, BOTTOM_RIGHT);
226 AddAnchor(IDC_WHOLE_PROJECT, BOTTOM_LEFT);
227 AddAnchor(IDC_KEEPLISTS, BOTTOM_LEFT);
228 AddAnchor(IDOK, BOTTOM_RIGHT);
229 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
230 AddAnchor(IDHELP, BOTTOM_RIGHT);
231 AddAnchor(IDC_COMMIT_AMEND,TOP_LEFT);
233 if (hWndExplorer)
234 CenterWindow(CWnd::FromHandle(hWndExplorer));
235 EnableSaveRestore(_T("CommitDlg"));
236 DWORD yPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\CommitDlgSizer"));
237 RECT rcDlg, rcLogMsg, rcFileList;
238 GetClientRect(&rcDlg);
239 m_cLogMessage.GetWindowRect(&rcLogMsg);
240 ScreenToClient(&rcLogMsg);
241 m_ListCtrl.GetWindowRect(&rcFileList);
242 ScreenToClient(&rcFileList);
243 if (yPos)
245 RECT rectSplitter;
246 m_wndSplitter.GetWindowRect(&rectSplitter);
247 ScreenToClient(&rectSplitter);
248 int delta = yPos - rectSplitter.top;
249 if ((rcLogMsg.bottom + delta > rcLogMsg.top)&&(rcLogMsg.bottom + delta < rcFileList.bottom - 30))
251 m_wndSplitter.SetWindowPos(NULL, 0, yPos, 0, 0, SWP_NOSIZE);
252 DoSize(delta);
256 // add all directories to the watcher
257 for (int i=0; i<m_pathList.GetCount(); ++i)
259 if (m_pathList[i].IsDirectory())
260 m_pathwatcher.AddPath(m_pathList[i]);
263 m_updatedPathList = m_pathList;
265 //first start a thread to obtain the file list with the status without
266 //blocking the dialog
267 InterlockedExchange(&m_bBlock, TRUE);
268 m_pThread = AfxBeginThread(StatusThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
269 if (m_pThread==NULL)
271 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
272 InterlockedExchange(&m_bBlock, FALSE);
274 else
276 m_pThread->m_bAutoDelete = FALSE;
277 m_pThread->ResumeThread();
279 CRegDWORD err = CRegDWORD(_T("Software\\TortoiseGit\\ErrorOccurred"), FALSE);
280 CRegDWORD historyhint = CRegDWORD(_T("Software\\TortoiseGit\\HistoryHintShown"), FALSE);
281 if ((((DWORD)err)!=FALSE)&&((((DWORD)historyhint)==FALSE)))
283 historyhint = TRUE;
284 // ShowBalloon(IDC_HISTORY, IDS_COMMITDLG_HISTORYHINT_TT, IDI_INFORMATION);
286 err = FALSE;
288 //this->UpdateData(TRUE);
289 //this->m_bCommitAmend=FALSE;
290 //this->UpdateData(FALSE);
292 return FALSE; // return TRUE unless you set the focus to a control
293 // EXCEPTION: OCX Property Pages should return FALSE
296 void CCommitDlg::OnOK()
298 if (m_bBlock)
299 return;
300 if (m_bThreadRunning)
302 m_bCancelled = true;
303 InterlockedExchange(&m_bRunThread, FALSE);
304 WaitForSingleObject(m_pThread->m_hThread, 1000);
305 if (m_bThreadRunning)
307 // we gave the thread a chance to quit. Since the thread didn't
308 // listen to us we have to kill it.
309 TerminateThread(m_pThread->m_hThread, (DWORD)-1);
310 InterlockedExchange(&m_bThreadRunning, FALSE);
313 this->UpdateData();
315 CString id;
316 GetDlgItemText(IDC_BUGID, id);
317 if (!m_ProjectProperties.CheckBugID(id))
319 ShowBalloon(IDC_BUGID, IDS_COMMITDLG_ONLYNUMBERS, IDI_EXCLAMATION);
320 return;
322 m_sLogMessage = m_cLogMessage.GetText();
323 if ( m_sLogMessage.IsEmpty() )
325 // no message entered, go round again
326 CMessageBox::Show(this->m_hWnd, IDS_COMMITDLG_NOMESSAGE, IDS_APPNAME, MB_OK | MB_ICONERROR);
327 return;
329 if ((m_ProjectProperties.bWarnIfNoIssue) && (id.IsEmpty() && !m_ProjectProperties.HasBugID(m_sLogMessage)))
331 if (CMessageBox::Show(this->m_hWnd, IDS_COMMITDLG_NOISSUEWARNING, IDS_APPNAME, MB_YESNO | MB_ICONWARNING)!=IDYES)
332 return;
335 #if 0
336 CRegDWORD regUnversionedRecurse (_T("Software\\TortoiseGit\\UnversionedRecurse"), TRUE);
337 if (!(DWORD)regUnversionedRecurse)
339 // Find unversioned directories which are marked for commit. The user might expect them
340 // to be added recursively since he cannot the the files. Let's ask the user if he knows
341 // what he is doing.
342 int nListItems = m_ListCtrl.GetItemCount();
343 for (int j=0; j<nListItems; j++)
345 const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(j);
346 if (entry->IsChecked() && (entry->status == Git_wc_status_unversioned) && entry->IsFolder() )
348 if (CMessageBox::Show(this->m_hWnd, IDS_COMMITDLG_UNVERSIONEDFOLDERWARNING, IDS_APPNAME, MB_YESNO | MB_ICONWARNING)!=IDYES)
349 return;
353 #endif
354 m_pathwatcher.Stop();
355 InterlockedExchange(&m_bBlock, TRUE);
356 CDWordArray arDeleted;
357 //first add all the unversioned files the user selected
358 //and check if all versioned files are selected
359 int nchecked = 0;
360 m_bRecursive = true;
361 int nListItems = m_ListCtrl.GetItemCount();
363 CTGitPathList itemsToAdd;
364 CTGitPathList itemsToRemove;
365 //std::set<CString> checkedLists;
366 //std::set<CString> uncheckedLists;
368 //CString checkedfiles;
369 //CString uncheckedfiles;
371 CString cmd;
372 CString out;
374 for (int j=0; j<nListItems; j++)
376 //const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(j);
377 CTGitPath *entry = (CTGitPath*)m_ListCtrl.GetItemData(j);
378 if (entry->m_Checked)
380 #if 0
381 if (entry->status == Git_wc_status_unversioned)
383 itemsToAdd.AddPath(entry->GetPath());
385 if (entry->status == Git_wc_status_conflicted)
387 bHasConflicted = true;
389 if (entry->status == Git_wc_status_missing)
391 itemsToRemove.AddPath(entry->GetPath());
393 if (entry->status == Git_wc_status_deleted)
395 arDeleted.Add(j);
397 if (entry->IsInExternal())
399 bCheckedInExternal = true;
401 #endif
402 if( entry->m_Action & CTGitPath::LOGACTIONS_UNVER)
403 cmd.Format(_T("git.exe add -f -- \"%s\""),entry->GetGitPathString());
404 else if ( entry->m_Action & CTGitPath::LOGACTIONS_DELETED)
405 cmd.Format(_T("git.exe update-index --remove -- \"%s\""),entry->GetGitPathString());
406 else
407 cmd.Format(_T("git.exe update-index -- \"%s\""),entry->GetGitPathString());
409 g_Git.Run(cmd,&out,CP_ACP);
410 nchecked++;
412 //checkedLists.insert(entry->GetGitPathString());
413 // checkedfiles += _T("\"")+entry->GetGitPathString()+_T("\" ");
415 else
417 //uncheckedLists.insert(entry->GetGitPathString());
418 if(entry->m_Action & CTGitPath::LOGACTIONS_ADDED)
419 { //To init git repository, there are not HEAD, so we can use git reset command
420 cmd.Format(_T("git.exe rm --cache -- \"%s\""),entry->GetGitPathString());
421 g_Git.Run(cmd,&out,CP_ACP);
423 else if(!( entry->m_Action & CTGitPath::LOGACTIONS_UNVER ) )
425 cmd.Format(_T("git.exe reset -- \"%s\""),entry->GetGitPathString());
426 g_Git.Run(cmd,&out,CP_ACP);
429 // uncheckedfiles += _T("\"")+entry->GetGitPathString()+_T("\" ");
430 #if 0
431 if ((entry->status != Git_wc_status_unversioned) &&
432 (entry->status != Git_wc_status_ignored))
434 nUnchecked++;
435 uncheckedLists.insert(entry->GetChangeList());
436 if (m_bRecursive)
438 // This algorithm is for the sake of simplicity of the complexity O(N?
439 for (int k=0; k<nListItems; k++)
441 const CGitStatusListCtrl::FileEntry * entryK = m_ListCtrl.GetListEntry(k);
442 if (entryK->IsChecked() && entryK->GetPath().IsAncestorOf(entry->GetPath()) )
444 // Fall back to a non-recursive commit to prevent items being
445 // committed which aren't checked although its parent is checked
446 // (property change, directory deletion, ... )
447 m_bRecursive = false;
448 break;
453 #endif
456 CShellUpdater::Instance().AddPathForUpdate(*entry);
459 //if(uncheckedfiles.GetLength()>0)
461 // cmd.Format(_T("git.exe reset -- %s"),uncheckedfiles);
462 // g_Git.Run(cmd,&out);
465 BOOL bCloseCommitDlg=true;
466 //if(checkedfiles.GetLength()>0)
467 if(nchecked||m_bCommitAmend)
469 // cmd.Format(_T("git.exe update-index -- %s"),checkedfiles);
470 // g_Git.Run(cmd,&out);
472 CString tempfile=::GetTempFile();
473 CFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
474 CStringA log=CUnicodeUtils::GetUTF8( m_sLogMessage);
475 file.Write(log,log.GetLength());
476 //file.WriteString(m_sLogMessage);
477 file.Close();
479 out =_T("");
480 CString amend;
481 if(this->m_bCommitAmend)
483 amend=_T("--amend");
485 cmd.Format(_T("git.exe commit %s -F \"%s\""),amend, tempfile);
487 CProgressDlg progress;
488 progress.m_GitCmd=cmd;
489 progress.m_bShowCommand = FALSE; // don't show the commit command
490 progress.m_PreText = out; // show any output already generated in log window
491 progress.DoModal();
493 if(progress.m_GitStatus)
495 bCloseCommitDlg = false;
496 this->Refresh();
499 CFile::Remove(tempfile);
500 }else
501 CMessageBox::Show(this->m_hWnd, _T("Nothing Commit"), _T("Commit Finish"), MB_OK | MB_ICONINFORMATION);
502 #if 0
503 if (m_pathwatcher.GetNumberOfChangedPaths() && m_bRecursive)
505 // There are paths which got changed (touched at least).
506 // We have to find out if this affects the selection in the commit dialog
507 // If it could affect the selection, revert back to a non-recursive commit
508 CTGitPathList changedList = m_pathwatcher.GetChangedPaths();
509 changedList.RemoveDuplicates();
510 for (int i=0; i<changedList.GetCount(); ++i)
512 if (changedList[i].IsAdminDir())
514 // something inside an admin dir was changed.
515 // if it's the entries file, then we have to fully refresh because
516 // files may have been added/removed from version control
517 if ((changedList[i].GetWinPathString().Right(7).CompareNoCase(_T("entries")) == 0) &&
518 (changedList[i].GetWinPathString().Find(_T("\\tmp\\"))<0))
520 m_bRecursive = false;
521 break;
524 else if (!m_ListCtrl.IsPathShown(changedList[i]))
526 // a path which is not shown in the list has changed
527 CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(changedList[i]);
528 if (entry)
530 // check if the changed path would get committed by a recursive commit
531 if ((!entry->IsFromDifferentRepository()) &&
532 (!entry->IsInExternal()) &&
533 (!entry->IsNested()) &&
534 (!entry->IsChecked()))
536 m_bRecursive = false;
537 break;
545 // Now, do all the adds - make sure that the list is sorted so that parents
546 // are added before their children
547 itemsToAdd.SortByPathname();
548 Git Git;
549 if (!Git.Add(itemsToAdd, &m_ProjectProperties, Git_depth_empty, FALSE, FALSE, TRUE))
551 CMessageBox::Show(m_hWnd, Git.GetLastErrorMessage(), _T("TortoiseGit"), MB_ICONERROR);
552 InterlockedExchange(&m_bBlock, FALSE);
553 Refresh();
554 return;
557 // Remove any missing items
558 // Not sure that this sort is really necessary - indeed, it might be better to do a reverse sort at this point
559 itemsToRemove.SortByPathname();
560 Git.Remove(itemsToRemove, TRUE);
562 //the next step: find all deleted files and check if they're
563 //inside a deleted folder. If that's the case, then remove those
564 //files from the list since they'll get deleted by the parent
565 //folder automatically.
566 m_ListCtrl.Block(TRUE, FALSE);
567 INT_PTR nDeleted = arDeleted.GetCount();
568 for (INT_PTR i=0; i<arDeleted.GetCount(); i++)
570 if (m_ListCtrl.GetCheck(arDeleted.GetAt(i)))
572 const CTGitPath& path = m_ListCtrl.GetListEntry(arDeleted.GetAt(i))->GetPath();
573 if (path.IsDirectory())
575 //now find all children of this directory
576 for (int j=0; j<arDeleted.GetCount(); j++)
578 if (i!=j)
580 CGitStatusListCtrl::FileEntry* childEntry = m_ListCtrl.GetListEntry(arDeleted.GetAt(j));
581 if (childEntry->IsChecked())
583 if (path.IsAncestorOf(childEntry->GetPath()))
585 m_ListCtrl.SetEntryCheck(childEntry, arDeleted.GetAt(j), false);
586 nDeleted--;
594 m_ListCtrl.Block(FALSE, FALSE);
596 if ((nUnchecked != 0)||(bCheckedInExternal)||(bHasConflicted)||(!m_bRecursive))
598 //save only the files the user has checked into the temporary file
599 m_ListCtrl.WriteCheckedNamesToPathList(m_pathList);
601 m_ListCtrl.WriteCheckedNamesToPathList(m_selectedPathList);
602 // the item count is used in the progress dialog to show the overall commit
603 // progress.
604 // deleted items only send one notification event, all others send two
605 m_itemsCount = ((m_selectedPathList.GetCount() - nDeleted - itemsToRemove.GetCount()) * 2) + nDeleted + itemsToRemove.GetCount();
607 if ((m_bRecursive)&&(checkedLists.size() == 1))
609 // all checked items belong to the same changelist
610 // find out if there are any unchecked items which belong to that changelist
611 if (uncheckedLists.find(*checkedLists.begin()) == uncheckedLists.end())
612 m_sChangeList = *checkedLists.begin();
614 #endif
615 UpdateData();
616 m_regAddBeforeCommit = m_bShowUnversioned;
617 if (!GetDlgItem(IDC_WHOLE_PROJECT)->IsWindowEnabled())
618 m_bWholeProject = FALSE;
619 m_regKeepChangelists = m_bKeepChangeList;
620 if (!GetDlgItem(IDC_KEEPLISTS)->IsWindowEnabled())
621 m_bKeepChangeList = FALSE;
622 InterlockedExchange(&m_bBlock, FALSE);
623 m_sBugID.Trim();
624 if (!m_sBugID.IsEmpty())
626 m_sBugID.Replace(_T(", "), _T(","));
627 m_sBugID.Replace(_T(" ,"), _T(","));
628 CString sBugID = m_ProjectProperties.sMessage;
629 sBugID.Replace(_T("%BUGID%"), m_sBugID);
630 if (m_ProjectProperties.bAppend)
631 m_sLogMessage += _T("\n") + sBugID + _T("\n");
632 else
633 m_sLogMessage = sBugID + _T("\n") + m_sLogMessage;
635 m_History.AddEntry(m_sLogMessage);
636 m_History.Save();
638 SaveSplitterPos();
640 if( bCloseCommitDlg )
641 CResizableStandAloneDialog::OnOK();
643 CShellUpdater::Instance().Flush();
646 void CCommitDlg::SaveSplitterPos()
648 if (!IsIconic())
650 CRegDWORD regPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\CommitDlgSizer"));
651 RECT rectSplitter;
652 m_wndSplitter.GetWindowRect(&rectSplitter);
653 ScreenToClient(&rectSplitter);
654 regPos = rectSplitter.top;
658 UINT CCommitDlg::StatusThreadEntry(LPVOID pVoid)
660 return ((CCommitDlg*)pVoid)->StatusThread();
663 UINT CCommitDlg::StatusThread()
665 //get the status of all selected file/folders recursively
666 //and show the ones which have to be committed to the user
667 //in a list control.
668 InterlockedExchange(&m_bBlock, TRUE);
669 InterlockedExchange(&m_bThreadRunning, TRUE);// so the main thread knows that this thread is still running
670 InterlockedExchange(&m_bRunThread, TRUE); // if this is set to FALSE, the thread should stop
671 m_bCancelled = false;
673 DialogEnableWindow(IDOK, false);
674 DialogEnableWindow(IDC_SHOWUNVERSIONED, false);
675 DialogEnableWindow(IDC_WHOLE_PROJECT, false);
676 DialogEnableWindow(IDC_SELECTALL, false);
677 GetDlgItem(IDC_EXTERNALWARNING)->ShowWindow(SW_HIDE);
678 DialogEnableWindow(IDC_EXTERNALWARNING, false);
679 // read the list of recent log entries before querying the WC for status
680 // -> the user may select one and modify / update it while we are crawling the WC
682 if (m_History.GetCount()==0)
684 CString reg;
685 reg.Format(_T("Software\\TortoiseGit\\History\\commit%s"), (LPCTSTR)m_ListCtrl.m_sUUID);
686 reg.Replace(_T(':'),_T('_'));
687 m_History.Load(reg, _T("logmsgs"));
690 // Initialise the list control with the status of the files/folders below us
691 m_ListCtrl.Clear();
692 BOOL success;
693 if(m_bWholeProject)
694 success=m_ListCtrl.GetStatus(NULL);
695 else
696 success=m_ListCtrl.GetStatus(&m_pathList);
698 //m_ListCtrl.UpdateFileList(git_revnum_t(GIT_REV_ZERO));
699 if(this->m_bShowUnversioned)
700 m_ListCtrl.UpdateFileList(CGitStatusListCtrl::FILELIST_UNVER,true,&m_pathList);
702 m_ListCtrl.CheckIfChangelistsArePresent(false);
704 DWORD dwShow = SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWLOCKS | SVNSLC_SHOWINCHANGELIST;
705 dwShow |= DWORD(m_regAddBeforeCommit) ? SVNSLC_SHOWUNVERSIONED : 0;
706 if (success)
708 if (m_checkedPathList.GetCount())
709 m_ListCtrl.Show(dwShow, m_checkedPathList);
710 else
712 DWORD dwCheck = m_bSelectFilesForCommit ? dwShow : 0;
713 dwCheck &=~(CTGitPath::LOGACTIONS_UNVER); //don't check unversion file default.
714 m_ListCtrl.Show(dwShow, dwCheck);
715 m_bSelectFilesForCommit = true;
718 if (m_ListCtrl.HasExternalsFromDifferentRepos())
720 GetDlgItem(IDC_EXTERNALWARNING)->ShowWindow(SW_SHOW);
721 DialogEnableWindow(IDC_EXTERNALWARNING, TRUE);
724 SetDlgItemText(IDC_COMMIT_TO, g_Git.GetCurrentBranch());
725 m_tooltips.AddTool(GetDlgItem(IDC_STATISTICS), m_ListCtrl.GetStatisticsString());
727 CString logmsg;
728 GetDlgItemText(IDC_LOGMESSAGE, logmsg);
729 DialogEnableWindow(IDOK, logmsg.GetLength() >= m_ProjectProperties.nMinLogSize);
730 if (!success)
732 if (!m_ListCtrl.GetLastErrorMessage().IsEmpty())
733 m_ListCtrl.SetEmptyString(m_ListCtrl.GetLastErrorMessage());
734 m_ListCtrl.Show(dwShow);
736 if ((m_ListCtrl.GetItemCount()==0)&&(m_ListCtrl.HasUnversionedItems()))
738 if (CMessageBox::Show(m_hWnd, IDS_COMMITDLG_NOTHINGTOCOMMITUNVERSIONED, IDS_APPNAME, MB_ICONINFORMATION | MB_YESNO)==IDYES)
740 m_bShowUnversioned = TRUE;
741 GetDlgItem(IDC_SHOWUNVERSIONED)->SendMessage(BM_SETCHECK, BST_CHECKED);
742 DWORD dwShow = (DWORD)(SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWUNVERSIONED | SVNSLC_SHOWLOCKS);
743 m_ListCtrl.UpdateFileList(CGitStatusListCtrl::FILELIST_UNVER);
744 m_ListCtrl.Show(dwShow,dwShow&(~CTGitPath::LOGACTIONS_UNVER));
748 CTGitPath commonDir = m_ListCtrl.GetCommonDirectory(false);
750 if(this->m_bWholeProject)
751 SetWindowText(m_sWindowTitle + _T(" - ") + CString(_T("Whole Project")));
752 else
753 SetWindowText(m_sWindowTitle + _T(" - ") + commonDir.GetWinPathString());
755 m_autolist.clear();
756 // we don't have to block the commit dialog while we fetch the
757 // auto completion list.
758 m_pathwatcher.ClearChangedPaths();
759 InterlockedExchange(&m_bBlock, FALSE);
760 if ((DWORD)CRegDWORD(_T("Software\\TortoiseGit\\Autocompletion"), TRUE)==TRUE)
762 m_ListCtrl.Block(TRUE, TRUE);
763 GetAutocompletionList();
764 m_ListCtrl.Block(FALSE, FALSE);
766 if (m_bRunThread)
768 DialogEnableWindow(IDC_SHOWUNVERSIONED, true);
769 DialogEnableWindow(IDC_WHOLE_PROJECT, true);
770 DialogEnableWindow(IDC_SELECTALL, true);
771 if (m_ListCtrl.HasChangeLists())
772 DialogEnableWindow(IDC_KEEPLISTS, true);
773 if (m_ListCtrl.HasLocks())
774 DialogEnableWindow(IDC_WHOLE_PROJECT, true);
775 // we have the list, now signal the main thread about it
776 SendMessage(WM_AUTOLISTREADY); // only send the message if the thread wasn't told to quit!
779 InterlockedExchange(&m_bRunThread, FALSE);
780 InterlockedExchange(&m_bThreadRunning, FALSE);
781 // force the cursor to normal
782 RefreshCursor();
784 return 0;
787 void CCommitDlg::OnCancel()
789 m_bCancelled = true;
790 if (m_bBlock)
791 return;
792 m_pathwatcher.Stop();
793 if (m_bThreadRunning)
795 InterlockedExchange(&m_bRunThread, FALSE);
796 WaitForSingleObject(m_pThread->m_hThread, 1000);
797 if (m_bThreadRunning)
799 // we gave the thread a chance to quit. Since the thread didn't
800 // listen to us we have to kill it.
801 TerminateThread(m_pThread->m_hThread, (DWORD)-1);
802 InterlockedExchange(&m_bThreadRunning, FALSE);
805 UpdateData();
806 m_sBugID.Trim();
807 m_sLogMessage = m_cLogMessage.GetText();
808 if (!m_sBugID.IsEmpty())
810 m_sBugID.Replace(_T(", "), _T(","));
811 m_sBugID.Replace(_T(" ,"), _T(","));
812 CString sBugID = m_ProjectProperties.sMessage;
813 sBugID.Replace(_T("%BUGID%"), m_sBugID);
814 if (m_ProjectProperties.bAppend)
815 m_sLogMessage += _T("\n") + sBugID + _T("\n");
816 else
817 m_sLogMessage = sBugID + _T("\n") + m_sLogMessage;
819 if (m_ProjectProperties.sLogTemplate.Compare(m_sLogMessage) != 0)
820 m_History.AddEntry(m_sLogMessage);
821 m_History.Save();
822 SaveSplitterPos();
823 CResizableStandAloneDialog::OnCancel();
826 void CCommitDlg::OnBnClickedSelectall()
828 m_tooltips.Pop(); // hide the tooltips
829 UINT state = (m_SelectAll.GetState() & 0x0003);
830 if (state == BST_INDETERMINATE)
832 // It is not at all useful to manually place the checkbox into the indeterminate state...
833 // We will force this on to the unchecked state
834 state = BST_UNCHECKED;
835 m_SelectAll.SetCheck(state);
837 m_ListCtrl.SelectAll(state == BST_CHECKED);
840 BOOL CCommitDlg::PreTranslateMessage(MSG* pMsg)
842 if (!m_bBlock)
843 m_tooltips.RelayEvent(pMsg);
844 if (pMsg->message == WM_KEYDOWN)
846 switch (pMsg->wParam)
848 case VK_F5:
850 if (m_bBlock)
851 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
852 Refresh();
854 break;
855 case VK_RETURN:
857 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
859 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
861 PostMessage(WM_COMMAND, IDOK);
863 return TRUE;
865 if ( GetFocus()==GetDlgItem(IDC_BUGID) )
867 // Pressing RETURN in the bug id control
868 // moves the focus to the message
869 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();
870 return TRUE;
873 break;
877 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
880 void CCommitDlg::Refresh()
882 if (m_bThreadRunning)
883 return;
885 InterlockedExchange(&m_bBlock, TRUE);
886 m_pThread = AfxBeginThread(StatusThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
887 if (m_pThread==NULL)
889 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
890 InterlockedExchange(&m_bBlock, FALSE);
892 else
894 m_pThread->m_bAutoDelete = FALSE;
895 m_pThread->ResumeThread();
899 void CCommitDlg::OnBnClickedHelp()
901 OnHelp();
904 void CCommitDlg::OnBnClickedShowunversioned()
906 m_tooltips.Pop(); // hide the tooltips
907 UpdateData();
908 m_regAddBeforeCommit = m_bShowUnversioned;
909 if (!m_bBlock)
911 DWORD dwShow = m_ListCtrl.GetShowFlags();
912 if (DWORD(m_regAddBeforeCommit))
913 dwShow |= SVNSLC_SHOWUNVERSIONED;
914 else
915 dwShow &= ~SVNSLC_SHOWUNVERSIONED;
916 if(dwShow & SVNSLC_SHOWUNVERSIONED)
918 if(m_bWholeProject)
919 m_ListCtrl.GetStatus(NULL,false,false,true);
920 else
921 m_ListCtrl.GetStatus(&this->m_pathList,false,false,true);
923 m_ListCtrl.Show(dwShow);
927 void CCommitDlg::OnStnClickedExternalwarning()
929 m_tooltips.Popup();
932 void CCommitDlg::OnEnChangeLogmessage()
934 UpdateOKButton();
937 LRESULT CCommitDlg::OnGitStatusListCtrlItemCountChanged(WPARAM, LPARAM)
939 #if 0
940 if ((m_ListCtrl.GetItemCount() == 0)&&(m_ListCtrl.HasUnversionedItems())&&(!m_bShowUnversioned))
942 if (CMessageBox::Show(*this, IDS_COMMITDLG_NOTHINGTOCOMMITUNVERSIONED, IDS_APPNAME, MB_ICONINFORMATION | MB_YESNO)==IDYES)
944 m_bShowUnversioned = TRUE;
945 DWORD dwShow = GitSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GitSLC_SHOWUNVERSIONED | GitSLC_SHOWLOCKS;
946 m_ListCtrl.Show(dwShow);
947 UpdateData(FALSE);
950 #endif
951 return 0;
954 LRESULT CCommitDlg::OnGitStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
956 Refresh();
957 return 0;
960 LRESULT CCommitDlg::OnFileDropped(WPARAM, LPARAM /*lParam*/)
962 #if 0
963 BringWindowToTop();
964 SetForegroundWindow();
965 SetActiveWindow();
966 // if multiple files/folders are dropped
967 // this handler is called for every single item
968 // separately.
969 // To avoid creating multiple refresh threads and
970 // causing crashes, we only add the items to the
971 // list control and start a timer.
972 // When the timer expires, we start the refresh thread,
973 // but only if it isn't already running - otherwise we
974 // restart the timer.
975 CTGitPath path;
976 path.SetFromWin((LPCTSTR)lParam);
978 // just add all the items we get here.
979 // if the item is versioned, the add will fail but nothing
980 // more will happen.
981 Git Git;
982 Git.Add(CTGitPathList(path), &m_ProjectProperties, Git_depth_empty, false, true, true);
984 if (!m_ListCtrl.HasPath(path))
986 if (m_pathList.AreAllPathsFiles())
988 m_pathList.AddPath(path);
989 m_pathList.RemoveDuplicates();
990 m_updatedPathList.AddPath(path);
991 m_updatedPathList.RemoveDuplicates();
993 else
995 // if the path list contains folders, we have to check whether
996 // our just (maybe) added path is a child of one of those. If it is
997 // a child of a folder already in the list, we must not add it. Otherwise
998 // that path could show up twice in the list.
999 bool bHasParentInList = false;
1000 for (int i=0; i<m_pathList.GetCount(); ++i)
1002 if (m_pathList[i].IsAncestorOf(path))
1004 bHasParentInList = true;
1005 break;
1008 if (!bHasParentInList)
1010 m_pathList.AddPath(path);
1011 m_pathList.RemoveDuplicates();
1012 m_updatedPathList.AddPath(path);
1013 m_updatedPathList.RemoveDuplicates();
1018 // Always start the timer, since the status of an existing item might have changed
1019 SetTimer(REFRESHTIMER, 200, NULL);
1020 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
1021 #endif
1022 return 0;
1025 LRESULT CCommitDlg::OnAutoListReady(WPARAM, LPARAM)
1027 m_cLogMessage.SetAutoCompletionList(m_autolist, '*');
1028 return 0;
1031 //////////////////////////////////////////////////////////////////////////
1032 // functions which run in the status thread
1033 //////////////////////////////////////////////////////////////////////////
1035 void CCommitDlg::ParseRegexFile(const CString& sFile, std::map<CString, CString>& mapRegex)
1037 CString strLine;
1040 CStdioFile file(sFile, CFile::typeText | CFile::modeRead | CFile::shareDenyWrite);
1041 while (m_bRunThread && file.ReadString(strLine))
1043 int eqpos = strLine.Find('=');
1044 CString rgx;
1045 rgx = strLine.Mid(eqpos+1).Trim();
1047 int pos = -1;
1048 while (((pos = strLine.Find(','))>=0)&&(pos < eqpos))
1050 mapRegex[strLine.Left(pos)] = rgx;
1051 strLine = strLine.Mid(pos+1).Trim();
1053 mapRegex[strLine.Left(strLine.Find('=')).Trim()] = rgx;
1055 file.Close();
1057 catch (CFileException* pE)
1059 TRACE("CFileException loading auto list regex file\n");
1060 pE->Delete();
1061 return;
1064 void CCommitDlg::GetAutocompletionList()
1066 // the auto completion list is made of strings from each selected files.
1067 // the strings used are extracted from the files with regexes found
1068 // in the file "autolist.txt".
1069 // the format of that file is:
1070 // file extensions separated with commas '=' regular expression to use
1071 // example:
1072 // .h, .hpp = (?<=class[\s])\b\w+\b|(\b\w+(?=[\s ]?\(\);))
1073 // .cpp = (?<=[^\s]::)\b\w+\b
1075 std::map<CString, CString> mapRegex;
1076 CString sRegexFile = CPathUtils::GetAppDirectory();
1077 CRegDWORD regtimeout = CRegDWORD(_T("Software\\TortoiseGit\\AutocompleteParseTimeout"), 5);
1078 DWORD timeoutvalue = regtimeout*1000;
1079 sRegexFile += _T("autolist.txt");
1080 if (!m_bRunThread)
1081 return;
1082 ParseRegexFile(sRegexFile, mapRegex);
1083 SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, sRegexFile.GetBuffer(MAX_PATH+1));
1084 sRegexFile.ReleaseBuffer();
1085 sRegexFile += _T("\\TortoiseGit\\autolist.txt");
1086 if (PathFileExists(sRegexFile))
1088 ParseRegexFile(sRegexFile, mapRegex);
1090 DWORD starttime = GetTickCount();
1092 // now we have two arrays of strings, where the first array contains all
1093 // file extensions we can use and the second the corresponding regex strings
1094 // to apply to those files.
1096 // the next step is to go over all files shown in the commit dialog
1097 // and scan them for strings we can use
1098 int nListItems = m_ListCtrl.GetItemCount();
1100 for (int i=0; i<nListItems && m_bRunThread; ++i)
1102 // stop parsing after timeout
1103 if ((!m_bRunThread) || (GetTickCount() - starttime > timeoutvalue))
1104 return;
1106 CTGitPath *path = (CTGitPath*)m_ListCtrl.GetItemData(i);
1108 if(path == NULL)
1109 continue;
1111 CString sPartPath =path->GetGitPathString();
1112 m_autolist.insert(sPartPath);
1114 // const CGitStatusListCtrl::FileEntry * entry = m_ListCtrl.GetListEntry(i);
1115 // if (!entry)
1116 // continue;
1118 // add the path parts to the auto completion list too
1119 // CString sPartPath = entry->GetRelativeGitPath();
1120 // m_autolist.insert(sPartPath);
1123 int pos = 0;
1124 int lastPos = 0;
1125 while ((pos = sPartPath.Find('/', pos)) >= 0)
1127 pos++;
1128 lastPos = pos;
1129 m_autolist.insert(sPartPath.Mid(pos));
1132 // Last inserted entry is a file name.
1133 // Some users prefer to also list file name without extension.
1134 if (CRegDWORD(_T("Software\\TortoiseGit\\AutocompleteRemovesExtensions"), FALSE))
1136 int dotPos = sPartPath.ReverseFind('.');
1137 if ((dotPos >= 0) && (dotPos > lastPos))
1138 m_autolist.insert(sPartPath.Mid(lastPos, dotPos - lastPos));
1140 #if 0
1141 if ((entry->status <= Git_wc_status_normal)||(entry->status == Git_wc_status_ignored))
1142 continue;
1144 CString sExt = entry->GetPath().GetFileExtension();
1145 sExt.MakeLower();
1146 // find the regex string which corresponds to the file extension
1147 CString rdata = mapRegex[sExt];
1148 if (rdata.IsEmpty())
1149 continue;
1151 ScanFile(entry->GetPath().GetWinPathString(), rdata);
1152 if ((entry->textstatus != Git_wc_status_unversioned) &&
1153 (entry->textstatus != Git_wc_status_none) &&
1154 (entry->textstatus != Git_wc_status_ignored) &&
1155 (entry->textstatus != Git_wc_status_added) &&
1156 (entry->textstatus != Git_wc_status_normal))
1158 CTGitPath basePath = Git::GetPristinePath(entry->GetPath());
1159 if (!basePath.IsEmpty())
1160 ScanFile(basePath.GetWinPathString(), rdata);
1162 #endif
1164 ATLTRACE(_T("Auto completion list loaded in %d msec\n"), GetTickCount() - starttime);
1167 void CCommitDlg::ScanFile(const CString& sFilePath, const CString& sRegex)
1169 wstring sFileContent;
1170 HANDLE hFile = CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
1171 if (hFile != INVALID_HANDLE_VALUE)
1173 DWORD size = GetFileSize(hFile, NULL);
1174 if (size > 1000000L)
1176 // no files bigger than 1 Meg
1177 CloseHandle(hFile);
1178 return;
1180 // allocate memory to hold file contents
1181 char * buffer = new char[size];
1182 DWORD readbytes;
1183 ReadFile(hFile, buffer, size, &readbytes, NULL);
1184 CloseHandle(hFile);
1185 int opts = 0;
1186 IsTextUnicode(buffer, readbytes, &opts);
1187 if (opts & IS_TEXT_UNICODE_NULL_BYTES)
1189 delete [] buffer;
1190 return;
1192 if (opts & IS_TEXT_UNICODE_UNICODE_MASK)
1194 sFileContent = wstring((wchar_t*)buffer, readbytes/sizeof(WCHAR));
1196 if ((opts & IS_TEXT_UNICODE_NOT_UNICODE_MASK)||(opts == 0))
1198 int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, NULL, 0);
1199 wchar_t * pWideBuf = new wchar_t[ret];
1200 int ret2 = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)buffer, readbytes, pWideBuf, ret);
1201 if (ret2 == ret)
1202 sFileContent = wstring(pWideBuf, ret);
1203 delete [] pWideBuf;
1205 delete [] buffer;
1207 if (sFileContent.empty()|| !m_bRunThread)
1209 return;
1214 const tr1::wregex regCheck(sRegex, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
1215 const tr1::wsregex_iterator end;
1216 wstring s = sFileContent;
1217 for (tr1::wsregex_iterator it(s.begin(), s.end(), regCheck); it != end; ++it)
1219 const tr1::wsmatch match = *it;
1220 for (size_t i=1; i<match.size(); ++i)
1222 if (match[i].second-match[i].first)
1224 ATLTRACE(_T("matched keyword : %s\n"), wstring(match[i]).c_str());
1225 m_autolist.insert(wstring(match[i]).c_str());
1230 catch (exception) {}
1233 // CSciEditContextMenuInterface
1234 void CCommitDlg::InsertMenuItems(CMenu& mPopup, int& nCmd)
1236 CString sMenuItemText(MAKEINTRESOURCE(IDS_COMMITDLG_POPUP_PASTEFILELIST));
1237 m_nPopupPasteListCmd = nCmd++;
1238 mPopup.AppendMenu(MF_STRING | MF_ENABLED, m_nPopupPasteListCmd, sMenuItemText);
1240 //CString sMenuItemText(MAKEINTRESOURCE(IDS_COMMITDLG_POPUP_PASTEFILELIST));
1241 if(m_History.GetCount() > 0)
1243 sMenuItemText.LoadString(IDS_COMMITDLG_POPUP_PASTELASTMESSAGE);
1244 m_nPopupPasteLastMessage = nCmd++;
1245 mPopup.AppendMenu(MF_STRING | MF_ENABLED, m_nPopupPasteLastMessage, sMenuItemText);
1250 bool CCommitDlg::HandleMenuItemClick(int cmd, CSciEdit * pSciEdit)
1253 if (m_bBlock)
1254 return false;
1255 if (cmd == m_nPopupPasteListCmd)
1257 CString logmsg;
1258 TCHAR buf[MAX_STATUS_STRING_LENGTH];
1259 int nListItems = m_ListCtrl.GetItemCount();
1260 for (int i=0; i<nListItems; ++i)
1262 CTGitPath * entry = (CTGitPath*)m_ListCtrl.GetItemData(i);
1263 if (entry&&entry->m_Checked)
1265 CString line;
1266 CString status = entry->GetActionName();
1267 if(entry->m_Action & CTGitPath::LOGACTIONS_UNVER)
1268 status = _T("Add");
1270 //git_wc_status_kind status = entry->status;
1271 WORD langID = (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), GetUserDefaultLangID());
1272 if (m_ProjectProperties.bFileListInEnglish)
1273 langID = 1033;
1275 line.Format(_T("%-10s %s\r\n"),status , (LPCTSTR)m_ListCtrl.GetItemText(i,0));
1276 logmsg += line;
1279 pSciEdit->InsertText(logmsg);
1280 return true;
1283 if(cmd == m_nPopupPasteLastMessage)
1285 if(m_History.GetCount() ==0 )
1286 return false;
1288 CString logmsg;
1289 logmsg +=m_History.GetEntry(0);
1290 pSciEdit->InsertText(logmsg);
1291 return true;
1293 return false;
1296 void CCommitDlg::OnTimer(UINT_PTR nIDEvent)
1298 switch (nIDEvent)
1300 case ENDDIALOGTIMER:
1301 KillTimer(ENDDIALOGTIMER);
1302 EndDialog(0);
1303 break;
1304 case REFRESHTIMER:
1305 if (m_bThreadRunning)
1307 SetTimer(REFRESHTIMER, 200, NULL);
1308 ATLTRACE("Wait some more before refreshing\n");
1310 else
1312 KillTimer(REFRESHTIMER);
1313 ATLTRACE("Refreshing after items dropped\n");
1314 Refresh();
1316 break;
1318 __super::OnTimer(nIDEvent);
1321 void CCommitDlg::OnBnClickedHistory()
1323 m_tooltips.Pop(); // hide the tooltips
1324 if (m_pathList.GetCount() == 0)
1325 return;
1326 #if 0
1327 CHistoryDlg historyDlg;
1328 historyDlg.SetHistory(m_History);
1329 if (historyDlg.DoModal() != IDOK)
1330 return;
1332 CString sMsg = historyDlg.GetSelectedText();
1333 if (sMsg != m_cLogMessage.GetText().Left(sMsg.GetLength()))
1335 CString sBugID = m_ProjectProperties.GetBugIDFromLog(sMsg);
1336 if (!sBugID.IsEmpty())
1338 SetDlgItemText(IDC_BUGID, sBugID);
1340 if (m_ProjectProperties.sLogTemplate.Compare(m_cLogMessage.GetText())!=0)
1341 m_cLogMessage.InsertText(sMsg, !m_cLogMessage.GetText().IsEmpty());
1342 else
1343 m_cLogMessage.SetText(sMsg);
1346 UpdateOKButton();
1347 GetDlgItem(IDC_LOGMESSAGE)->SetFocus();
1348 #endif
1351 void CCommitDlg::OnBnClickedBugtraqbutton()
1353 #if 0
1354 m_tooltips.Pop(); // hide the tooltips
1355 CString sMsg = m_cLogMessage.GetText();
1357 if (m_BugTraqProvider == NULL)
1358 return;
1360 BSTR parameters = m_bugtraq_association.GetParameters().AllocSysString();
1361 BSTR commonRoot = SysAllocString(m_pathList.GetCommonRoot().GetDirectory().GetWinPath());
1362 SAFEARRAY *pathList = SafeArrayCreateVector(VT_BSTR, 0, m_pathList.GetCount());
1364 for (LONG index = 0; index < m_pathList.GetCount(); ++index)
1365 SafeArrayPutElement(pathList, &index, m_pathList[index].GetGitPathString().AllocSysString());
1367 BSTR originalMessage = sMsg.AllocSysString();
1368 BSTR temp = NULL;
1370 // first try the IBugTraqProvider2 interface
1371 CComPtr<IBugTraqProvider2> pProvider2 = NULL;
1372 HRESULT hr = m_BugTraqProvider.QueryInterface(&pProvider2);
1373 if (SUCCEEDED(hr))
1375 CString common = m_ListCtrl.GetCommonURL(false).GetGitPathString();
1376 BSTR repositoryRoot = common.AllocSysString();
1377 if (FAILED(hr = pProvider2->GetCommitMessage2(GetSafeHwnd(), parameters, repositoryRoot, commonRoot, pathList, originalMessage, &temp)))
1379 CString sErr;
1380 sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());
1381 CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);
1383 else
1384 m_cLogMessage.SetText(temp);
1386 else
1388 // if IBugTraqProvider2 failed, try IBugTraqProvider
1389 CComPtr<IBugTraqProvider> pProvider = NULL;
1390 HRESULT hr = m_BugTraqProvider.QueryInterface(&pProvider);
1391 if (FAILED(hr))
1393 CString sErr;
1394 sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, (LPCTSTR)m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());
1395 CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);
1396 return;
1399 if (FAILED(hr = pProvider->GetCommitMessage(GetSafeHwnd(), parameters, commonRoot, pathList, originalMessage, &temp)))
1401 CString sErr;
1402 sErr.Format(IDS_ERR_FAILEDISSUETRACKERCOM, m_bugtraq_association.GetProviderName(), _com_error(hr).ErrorMessage());
1403 CMessageBox::Show(m_hWnd, sErr, _T("TortoiseGit"), MB_ICONERROR);
1405 else
1406 m_cLogMessage.SetText(temp);
1409 m_cLogMessage.SetFocus();
1411 SysFreeString(temp);
1412 #endif
1415 LRESULT CCommitDlg::OnGitStatusListCtrlCheckChanged(WPARAM, LPARAM)
1417 UpdateOKButton();
1418 return 0;
1421 void CCommitDlg::UpdateOKButton()
1423 #if 0
1424 BOOL bValidLogSize = FALSE;
1426 if (m_cLogMessage.GetText().GetLength() >= m_ProjectProperties.nMinLogSize)
1427 bValidLogSize = !m_bBlock;
1429 LONG nSelectedItems = m_ListCtrl.GetSelected();
1430 DialogEnableWindow(IDOK, bValidLogSize && nSelectedItems>0);
1431 #endif
1435 LRESULT CCommitDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
1437 switch (message) {
1438 case WM_NOTIFY:
1439 if (wParam == IDC_SPLITTER)
1441 SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam;
1442 DoSize(pHdr->delta);
1444 break;
1447 return __super::DefWindowProc(message, wParam, lParam);
1450 void CCommitDlg::SetSplitterRange()
1452 if ((m_ListCtrl)&&(m_cLogMessage))
1454 CRect rcTop;
1455 m_cLogMessage.GetWindowRect(rcTop);
1456 ScreenToClient(rcTop);
1457 CRect rcMiddle;
1458 m_ListCtrl.GetWindowRect(rcMiddle);
1459 ScreenToClient(rcMiddle);
1460 if (rcMiddle.Height() && rcMiddle.Width())
1461 m_wndSplitter.SetRange(rcTop.top+60, rcMiddle.bottom-80);
1465 void CCommitDlg::DoSize(int delta)
1467 RemoveAnchor(IDC_MESSAGEGROUP);
1468 RemoveAnchor(IDC_LOGMESSAGE);
1469 RemoveAnchor(IDC_SPLITTER);
1470 RemoveAnchor(IDC_SIGNOFF);
1471 RemoveAnchor(IDC_COMMIT_AMEND);
1472 RemoveAnchor(IDC_LISTGROUP);
1473 RemoveAnchor(IDC_FILELIST);
1474 CSplitterControl::ChangeHeight(&m_cLogMessage, delta, CW_TOPALIGN);
1475 CSplitterControl::ChangeHeight(GetDlgItem(IDC_MESSAGEGROUP), delta, CW_TOPALIGN);
1476 CSplitterControl::ChangeHeight(&m_ListCtrl, -delta, CW_BOTTOMALIGN);
1477 CSplitterControl::ChangeHeight(GetDlgItem(IDC_LISTGROUP), -delta, CW_BOTTOMALIGN);
1478 CSplitterControl::ChangePos(GetDlgItem(IDC_SIGNOFF),0,delta);
1479 CSplitterControl::ChangePos(GetDlgItem(IDC_COMMIT_AMEND),0,delta);
1480 AddAnchor(IDC_MESSAGEGROUP, TOP_LEFT, TOP_RIGHT);
1481 AddAnchor(IDC_LOGMESSAGE, TOP_LEFT, TOP_RIGHT);
1482 AddAnchor(IDC_SPLITTER, TOP_LEFT, TOP_RIGHT);
1483 AddAnchor(IDC_LISTGROUP, TOP_LEFT, BOTTOM_RIGHT);
1484 AddAnchor(IDC_FILELIST, TOP_LEFT, BOTTOM_RIGHT);
1485 AddAnchor(IDC_SIGNOFF,TOP_RIGHT);
1486 AddAnchor(IDC_COMMIT_AMEND,TOP_LEFT);
1487 ArrangeLayout();
1488 // adjust the minimum size of the dialog to prevent the resizing from
1489 // moving the list control too far down.
1490 CRect rcLogMsg;
1491 m_cLogMessage.GetClientRect(rcLogMsg);
1492 SetMinTrackSize(CSize(m_DlgOrigRect.Width(), m_DlgOrigRect.Height()-m_LogMsgOrigRect.Height()+rcLogMsg.Height()));
1494 SetSplitterRange();
1495 m_cLogMessage.Invalidate();
1496 GetDlgItem(IDC_LOGMESSAGE)->Invalidate();
1499 void CCommitDlg::OnSize(UINT nType, int cx, int cy)
1501 // first, let the resizing take place
1502 __super::OnSize(nType, cx, cy);
1504 //set range
1505 SetSplitterRange();
1509 void CCommitDlg::OnBnClickedSignOff()
1511 // TODO: Add your control notification handler code here
1512 CString str;
1513 CString username;
1514 CString email;
1515 username=g_Git.GetUserName();
1516 email=g_Git.GetUserEmail();
1517 username.Remove(_T('\n'));
1518 email.Remove(_T('\n'));
1519 str.Format(_T("Signed-off-by: %s <%s>\n"),username,email);
1521 m_cLogMessage.SetText(m_cLogMessage.GetText()+_T("\r\n\r\n")+str);
1524 void CCommitDlg::OnStnClickedCommitlabel()
1526 // TODO: Add your control notification handler code here
1529 void CCommitDlg::OnBnClickedCommitAmend()
1531 // TODO: Add your control notification handler code here
1532 this->UpdateData();
1533 if(this->m_bCommitAmend && this->m_AmendStr.IsEmpty())
1535 GitRev rev;
1536 BYTE_VECTOR vector;
1537 CString head(_T("HEAD"));
1538 g_Git.GetLog(vector,head,NULL,1);
1539 rev.ParserFromLog(vector);
1540 m_AmendStr=rev.m_Subject+_T("\n\n")+rev.m_Body;
1543 if(this->m_bCommitAmend)
1545 this->m_NoAmendStr=this->m_cLogMessage.GetText();
1546 m_cLogMessage.SetText(m_AmendStr);
1548 }else
1550 this->m_AmendStr=this->m_cLogMessage.GetText();
1551 m_cLogMessage.SetText(m_NoAmendStr);
1557 void CCommitDlg::OnBnClickedWholeProject()
1559 // TODO: Add your control notification handler code here
1560 m_tooltips.Pop(); // hide the tooltips
1561 UpdateData();
1562 m_ListCtrl.Clear();
1563 if (!m_bBlock)
1565 if(m_bWholeProject)
1566 m_ListCtrl.GetStatus(NULL,true,false,true);
1567 else
1568 m_ListCtrl.GetStatus(&this->m_pathList,true,false,true);
1570 m_ListCtrl.Show(m_ListCtrl.GetShowFlags());
1573 CTGitPath commonDir = m_ListCtrl.GetCommonDirectory(false);
1575 if(this->m_bWholeProject)
1576 SetWindowText(m_sWindowTitle + _T(" - ") + CString(_T("Whole Project")));
1577 else
1578 SetWindowText(m_sWindowTitle + _T(" - ") + commonDir.GetWinPathString());