Merge branch 'libgit2-and-better-errorhandling'
[TortoiseGit.git] / src / TortoiseProc / RefLogDlg.cpp
blobdf9b38a69096ac545a6a24b5f1c08a37587d4826
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - 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 // RefLogDlg.cpp : implementation file
22 #include "stdafx.h"
23 #include "resource.h"
24 #include "RefLogDlg.h"
25 #include "git.h"
26 #include "AppUtils.h"
27 #include "MessageBox.h"
29 // CRefLogDlg dialog
31 IMPLEMENT_DYNAMIC(CRefLogDlg, CResizableStandAloneDialog)
33 UINT CRefLogDlg::m_FindDialogMessage = ::RegisterWindowMessage(FINDMSGSTRING);
35 CRefLogDlg::CRefLogDlg(CWnd* pParent /*=NULL*/)
36 : CResizableStandAloneDialog(CRefLogDlg::IDD, pParent)
37 , m_pFindDialog(NULL)
42 CRefLogDlg::~CRefLogDlg()
46 void CRefLogDlg::DoDataExchange(CDataExchange* pDX)
48 CDialog::DoDataExchange(pDX);
49 DDX_Control(pDX, IDC_COMBOBOXEX_REF, m_ChooseRef);
50 DDX_Control(pDX, IDC_REFLOG_LIST, m_RefList);
54 BEGIN_MESSAGE_MAP(CRefLogDlg, CResizableStandAloneDialog)
55 ON_BN_CLICKED(IDOK, &CRefLogDlg::OnBnClickedOk)
56 ON_BN_CLICKED(IDC_REFLOG_BUTTONCLEARSTASH, &CRefLogDlg::OnBnClickedClearStash)
57 ON_CBN_SELCHANGE(IDC_COMBOBOXEX_REF, &CRefLogDlg::OnCbnSelchangeRef)
58 ON_MESSAGE(MSG_REFLOG_CHANGED,OnRefLogChanged)
59 ON_REGISTERED_MESSAGE(m_FindDialogMessage, OnFindDialogMessage)
60 END_MESSAGE_MAP()
62 LRESULT CRefLogDlg::OnRefLogChanged(WPARAM wParam, LPARAM lParam)
64 UNREFERENCED_PARAMETER(wParam);
65 UNREFERENCED_PARAMETER(lParam);
66 m_RefList.m_RefMap.clear();
67 OnCbnSelchangeRef();
68 return 0;
71 BOOL CRefLogDlg::OnInitDialog()
73 CResizableStandAloneDialog::OnInitDialog();
74 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
76 AddAnchor(IDOK,BOTTOM_RIGHT);
77 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
78 AddAnchor(IDC_REFLOG_BUTTONCLEARSTASH, BOTTOM_LEFT);
79 AddAnchor(IDC_REFLOG_LIST,TOP_LEFT,BOTTOM_RIGHT);
80 AddAnchor(IDHELP, BOTTOM_RIGHT);
81 AddAnchor(IDC_COMBOBOXEX_REF, TOP_LEFT, TOP_RIGHT);
83 AddOthersToAnchor();
84 this->EnableSaveRestore(_T("RefLogDlg"));
86 CString sWindowTitle;
87 GetWindowText(sWindowTitle);
88 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
90 m_ChooseRef.SetMaxHistoryItems(0x7FFFFFFF);
92 m_RefList.m_hasWC = !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir);
94 this->m_RefList.InsertRefLogColumn();
96 Refresh();
98 return TRUE;
100 // CRefLogDlg message handlers
102 void CRefLogDlg::OnBnClickedOk()
104 if (m_RefList.GetSelectedCount() == 1)
106 // get the selected row
107 POSITION pos = m_RefList.GetFirstSelectedItemPosition();
108 int selIndex = m_RefList.GetNextSelectedItem(pos);
109 if (selIndex < m_RefList.m_arShownList.GetCount())
111 // all ok, pick up the revision
112 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_RefList.m_arShownList.GetAt(selIndex));
113 // extract the hash
114 m_SelectedHash = pLogEntry->m_CommitHash;
118 OnOK();
120 void CRefLogDlg::OnBnClickedClearStash()
122 if (CMessageBox::Show(this->GetSafeHwnd(), IDS_PROC_DELETEALLSTASH, IDS_APPNAME, 2, IDI_QUESTION, IDS_DELETEBUTTON, IDS_ABORTBUTTON) == 1)
124 CString cmdOut;
125 if (g_Git.Run(_T("git.exe stash clear"), &cmdOut, CP_UTF8))
127 MessageBox(cmdOut, _T("TortoiseGit"), MB_ICONERROR);
128 return;
131 m_RefList.m_RefMap.clear();
133 OnCbnSelchangeRef();
137 void CRefLogDlg::OnCbnSelchangeRef()
139 CString ref=m_ChooseRef.GetString();
140 if(m_RefList.m_RefMap.find(ref) == m_RefList.m_RefMap.end())
142 m_RefList.m_RefMap[ref].m_pLogCache = &m_RefList.m_LogCache;
143 m_RefList.m_RefMap[ref].ParserFromRefLog(ref);
145 m_RefList.ClearText();
147 //this->m_logEntries.ParserFromLog();
148 m_RefList.SetRedraw(false);
150 CLogDataVector *plog;
151 plog = &m_RefList.m_RefMap[ref];
153 m_RefList.SetItemCountEx((int)plog->size());
155 this->m_RefList.m_arShownList.RemoveAll();
157 for(unsigned int i=0;i<m_RefList.m_RefMap[ref].size();i++)
159 plog->GetGitRevAt(i).m_IsFull=TRUE;
160 this->m_RefList.m_arShownList.Add(&(plog->GetGitRevAt(i)));
164 m_RefList.SetRedraw(true);
166 m_RefList.Invalidate();
168 if (ref == _T("refs/stash"))
170 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->ShowWindow(SW_SHOW);
171 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->EnableWindow((m_RefList.m_arShownList.GetSize() > 0));
173 else
174 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->ShowWindow(SW_HIDE);
177 BOOL CRefLogDlg::PreTranslateMessage(MSG* pMsg)
179 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F5)
180 Refresh();
181 else if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_F3 || (pMsg->wParam == 'F' && (GetAsyncKeyState(VK_CONTROL) & 0x8000))))
182 OnFind();
184 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
187 void CRefLogDlg::Refresh()
189 STRING_VECTOR list;
190 list.push_back(_T("HEAD"));
191 if (g_Git.GetRefList(list))
192 MessageBox(g_Git.GetGitLastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR);
194 m_ChooseRef.AddString(list);
196 if (m_CurrentBranch.IsEmpty())
198 m_CurrentBranch.Format(_T("refs/heads/%s"), g_Git.GetCurrentBranch());
199 m_ChooseRef.SetCurSel(0); /* Choose HEAD */
201 else
203 bool found = false;
204 for (int i = 0; i < list.size(); i++)
206 if(list[i] == m_CurrentBranch)
208 m_ChooseRef.SetCurSel(i);
209 found = true;
210 break;
213 if (!found)
214 m_ChooseRef.SetCurSel(0);
217 m_RefList.m_RefMap.clear();
219 OnCbnSelchangeRef();
222 void CRefLogDlg::OnFind()
224 m_nSearchLine = 0;
225 m_pFindDialog = new CFindReplaceDialog();
226 m_pFindDialog->Create(TRUE, _T(""), NULL, FR_DOWN | FR_HIDEWHOLEWORD | FR_HIDEUPDOWN, this);
229 LRESULT CRefLogDlg::OnFindDialogMessage(WPARAM /*wParam*/, LPARAM /*lParam*/)
231 ASSERT(m_pFindDialog != NULL);
233 if (m_RefList.m_arShownList.IsEmpty())
234 return 0;
236 // If the FR_DIALOGTERM flag is set,
237 // invalidate the handle identifying the dialog box.
238 if (m_pFindDialog->IsTerminating())
240 m_pFindDialog = NULL;
241 return 0;
244 // If the FR_FINDNEXT flag is set,
245 // call the application-defined search routine
246 // to search for the requested string.
247 if (m_pFindDialog->FindNext())
249 //read data from dialog
250 CString findString = m_pFindDialog->GetFindString();
252 bool bFound = false;
253 bool bCaseSensitive = !!(m_pFindDialog->m_nFlags & FR_MATCHCASE);
255 if (!bCaseSensitive)
256 findString.MakeLower();
258 int i = m_nSearchLine;
259 if (i < 0 || i >= m_RefList.m_arShownList.GetCount())
260 i = 0;
264 GitRev * data = (GitRev*)m_RefList.m_arShownList.SafeGetAt(i);
266 CString str;
267 str += data->m_Ref;
268 str += _T("\n");
269 str += data->m_RefAction;
270 str += _T("\n");
271 str += data->m_CommitHash.ToString();
272 str += _T("\n");
273 str += data->GetSubject();
274 str += _T("\n");
275 str += data->GetBody();
276 str += _T("\n");
278 if (!bCaseSensitive)
279 str.MakeLower();
281 if (str.Find(findString) >= 0)
282 bFound = true;
284 i++;
285 if(!bFound && i >= m_RefList.m_arShownList.GetCount())
286 i=0;
287 } while (i != m_nSearchLine && (!bFound));
289 if (bFound)
291 m_RefList.SetHotItem(i - 1);
292 m_RefList.EnsureVisible(i - 1, FALSE);
293 m_nSearchLine = i;
295 else
296 MessageBox(_T("\"") + findString + _T("\" ") + CString(MAKEINTRESOURCE(IDS_NOTFOUND)), _T("TortoiseGit"), MB_ICONINFORMATION);
299 return 0;