Debian Codegraph @

git/gitCommitListDlg.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : gitCommitListDlg.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "gitCommitListDlg.h"
27
28 #include "GitDiffOutputParser.h"
29 #include "asyncprocess.h"
30 #include "cl_config.h"
31 #include "editor_config.h"
32 #include "git.h"
33 #include "gitCommitEditor.h"
34 #include "gitentry.h"
35 #include "globals.h"
36 #include "lexer_configuration.h"
37 #include "processreaderthread.h"
38 #include "windowattrmanager.h"
39
40 #include <wx/tokenzr.h>
41
42 static int ID_COPY_COMMIT_HASH = wxNewId();
43 static int ID_REVERT_COMMIT = wxNewId();
44
45 GitCommitListDlg::GitCommitListDlg(wxWindow* parent, const wxString& workingDir, GitPlugin* git)
46 : GitCommitListDlgBase(parent)
47 , m_git(git)
48 , m_workingDir(workingDir)
49 , m_skip(0)
50 {
51 Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &GitCommitListDlg::OnProcessOutput, this);
52 Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &GitCommitListDlg::OnProcessTerminated, this);
53 // Bind(wxEVT_CHAR_HOOK, &GitCommitListDlg::OnCharHook, this);
54
55 LexerConf::Ptr_t lex = EditorConfigST::Get()->GetLexer("diff");
56 if(lex) {
57 lex->Apply(m_stcDiff, true);
58 }
59
60 m_dvListCtrlCommitList->Connect(ID_COPY_COMMIT_HASH, wxEVT_COMMAND_MENU_SELECTED,
61 wxCommandEventHandler(GitCommitListDlg::OnCopyCommitHashToClipboard), NULL, this);
62 m_dvListCtrlCommitList->Connect(ID_REVERT_COMMIT, wxEVT_COMMAND_MENU_SELECTED,
63 wxCommandEventHandler(GitCommitListDlg::OnRevertCommit), NULL, this);
64
65 ::clSetTLWindowBestSizeAndPosition(this);
66 }
67
68 /*******************************************************************************/
69 GitCommitListDlg::~GitCommitListDlg() { m_git->m_commitListDlg = NULL; }
70
71 /*******************************************************************************/
72 void GitCommitListDlg::SetCommitList(const wxString& commits)
73 {
74 m_commitList = commits;
75 m_history.insert(std::make_pair(m_skip, m_commitList));
76 // Load all commits, un-filtered
77 DoLoadCommits("");
78 }
79
80 /*******************************************************************************/
81 void GitCommitListDlg::OnChangeFile(wxCommandEvent& e)
82 {
83 int sel = m_fileListBox->GetSelection();
84 wxString file = m_fileListBox->GetString(sel);
85 m_stcDiff->SetReadOnly(false);
86 m_stcDiff->SetText(m_diffMap[file]);
87 m_stcDiff->SetReadOnly(true);
88 }
89
90 /*******************************************************************************/
91 void GitCommitListDlg::OnProcessTerminated(clProcessEvent& event)
92 {
93 wxUnusedVar(event);
94 wxDELETE(m_process);
95
96 ClearAll(false);
97
98 m_commandOutput.Replace(wxT("\r"), wxT(""));
99 m_stcCommitMessage->SetEditable(true);
100 m_stcDiff->SetEditable(true);
101
102 wxArrayString commitMessage;
103 GitDiffOutputParser diff_parser;
104 diff_parser.GetDiffMap(m_commandOutput, m_diffMap, &commitMessage);
105
106 for(wxStringMap_t::iterator it = m_diffMap.begin(); it != m_diffMap.end(); ++it) {
107 m_fileListBox->Append((*it).first);
108 }
109 if(m_diffMap.size() != 0) {
110 wxStringMap_t::iterator it = m_diffMap.begin();
111 m_stcDiff->SetText((*it).second);
112 m_fileListBox->Select(0);
113 }
114
115 for(size_t i = 0; i < commitMessage.GetCount(); ++i) {
116 m_stcCommitMessage->AppendText(commitMessage.Item(i));
117 }
118
119 m_stcDiff->SetEditable(false);
120 m_commandOutput.Clear();
121 m_stcCommitMessage->SetEditable(false);
122 }
123 /*******************************************************************************/
124 void GitCommitListDlg::OnProcessOutput(clProcessEvent& event) { m_commandOutput.Append(event.GetOutput()); }
125
126 void GitCommitListDlg::OnSelectionChanged(wxDataViewEvent& event)
127 {
128 wxVariant v;
129 if(!event.GetItem().IsOk()) {
130 return;
131 }
132
133 wxString commitID = m_dvListCtrlCommitList->GetItemText(event.GetItem());
134 wxString command_args;
135 command_args << "--no-pager show --first-parent " << commitID;
136 m_process = m_git->AsyncRunGit(this, command_args, IProcessCreateDefault | IProcessWrapInShell, m_workingDir);
137 }
138
139 void GitCommitListDlg::OnContextMenu(wxDataViewEvent& event)
140 {
141 wxMenu menu;
142 menu.Append(ID_COPY_COMMIT_HASH, _("Copy commit hash to clipboard"));
143 menu.Append(ID_REVERT_COMMIT, _("Revert this commit"));
144 m_dvListCtrlCommitList->PopupMenu(&menu);
145 }
146
147 void GitCommitListDlg::OnCopyCommitHashToClipboard(wxCommandEvent& e)
148 {
149 wxDataViewItem sel = m_dvListCtrlCommitList->GetSelection();
150 CHECK_ITEM_RET(sel);
151
152 wxString commitID = m_dvListCtrlCommitList->GetItemText(sel);
153 ::CopyToClipboard(commitID);
154 }
155
156 void GitCommitListDlg::OnRevertCommit(wxCommandEvent& e)
157 {
158 wxDataViewItem sel = m_dvListCtrlCommitList->GetSelection();
159 CHECK_ITEM_RET(sel);
160
161 wxString commitID = m_dvListCtrlCommitList->GetItemText(sel);
162
163 if(::wxMessageBox(_("Are you sure you want to revert commit #") + commitID, "CodeLite",
164 wxYES_NO | wxCANCEL | wxICON_QUESTION, this) != wxYES) {
165 return;
166 }
167 m_git->CallAfter(&GitPlugin::RevertCommit, commitID);
168 }
169
170 void GitCommitListDlg::OnClose(wxCloseEvent& event)
171 {
172 event.Skip();
173 Destroy();
174 }
175
176 void GitCommitListDlg::OnOK(wxCommandEvent& event) { Destroy(); }
177
178 void GitCommitListDlg::DoLoadCommits(const wxString& filter)
179 {
180 ClearAll();
181
182 // hash @ subject @ author-name @ date
183 wxArrayString gitList = wxStringTokenize(m_commitList, wxT("\n"), wxTOKEN_STRTOK);
184 wxArrayString filters = wxStringTokenize(filter, " ");
185 wxVector<wxVariant> cols;
186 for(unsigned i = 0; i < gitList.GetCount(); ++i) {
187 wxArrayString gitCommit = ::wxStringTokenize(gitList[i], "@");
188 if(gitCommit.GetCount() >= 4) {
189 cols.clear();
190 cols.reserve(4);
191
192 cols.push_back(gitCommit.Item(0));
193 cols.push_back(gitCommit.Item(1));
194 cols.push_back(gitCommit.Item(2));
195 cols.push_back(gitCommit.Item(3));
196 m_dvListCtrlCommitList->AppendItem(cols);
197 }
198 }
199 }
200
201 void GitCommitListDlg::ClearAll(bool includingCommitlist /*=true*/)
202 {
203 m_stcCommitMessage->SetEditable(true);
204 m_stcDiff->SetEditable(true);
205 m_stcCommitMessage->ClearAll();
206 m_fileListBox->Clear();
207 if(includingCommitlist) {
208 m_dvListCtrlCommitList->DeleteAllItems();
209 }
210 m_diffMap.clear();
211 m_stcDiff->ClearAll();
212 m_stcCommitMessage->SetEditable(false);
213 m_stcDiff->SetEditable(false);
214 }
215
216 void GitCommitListDlg::OnSearchCommitList(wxCommandEvent& event)
217 {
218 wxString filter = GetFilterString();
219 if(filter == m_Filter) {
220 return; // No change
221 }
222
223 m_Filter = filter;
224
225 if(m_Filter.empty()) {
226 wxCommandEvent e; // A search-filter has been removed, so reload
227 m_git->OnCommitList(e);
228 return;
229 }
230
231 ClearAll();
232
233 m_skip = 0;
234 m_history.clear();
235 m_commitList.clear();
236
237 m_git->FetchNextCommits(m_skip, m_Filter);
238 }
239
240 wxString GitCommitListDlg::GetFilterString() const
241 {
242 wxString args;
243 wxString filter = m_searchCtrlFilter->GetValue();
244
245 if(filter.empty() && m_comboExtraArgs->GetValue().empty()) {
246 return args;
247 }
248
249 wxArrayString searchStrings = ::wxStringTokenize(filter, " ", wxTOKEN_STRTOK);
250 for(size_t i = 0; i < searchStrings.size(); ++i) {
251 // Pass each search string using its own --grep field
252 args << " --grep=" << searchStrings.Item(i);
253 }
254
255 if(!searchStrings.IsEmpty()) {
256 // Limit the commits output to ones that match all given --grep
257 args << " --all-match";
258 }
259
260 if(m_checkBoxIgnoreCase->IsChecked()) {
261 args << " -i";
262 }
263
264 if(!m_comboExtraArgs->GetValue().empty()) {
265 args << ' ' << m_comboExtraArgs->GetValue();
266 }
267
268 return args;
269 }
270
271 void GitCommitListDlg::OnNext(wxCommandEvent& event)
272 {
273 wxUnusedVar(event);
274 m_skip += 100;
275 // Check the cache first
276 if(m_history.count(m_skip)) {
277 SetCommitList(m_history.find(m_skip)->second);
278 } else {
279 m_git->FetchNextCommits(m_skip, m_Filter);
280 }
281 }
282
283 void GitCommitListDlg::OnPrevious(wxCommandEvent& event)
284 {
285 wxUnusedVar(event);
286 int skip = m_skip - 100;
287 if(m_history.count(skip)) {
288 m_skip -= 100;
289 SetCommitList(m_history.find(m_skip)->second);
290 }
291 }
292
293 void GitCommitListDlg::OnExtraArgsTextEnter(wxCommandEvent& event)
294 {
295 // Add any text to the combobox, uniqued
296 wxString extraArgs = m_comboExtraArgs->GetValue();
297 if(!extraArgs.empty()) {
298 int pos = m_comboExtraArgs->FindString(extraArgs);
299 if(pos != 0 && pos != wxNOT_FOUND) { // 0 == already the first item in the list
300 m_comboExtraArgs->Delete(pos);
301 }
302 m_comboExtraArgs->Insert(extraArgs, 0);
303 }
304
305 OnSearchCommitList(event);
306 }
307
308 void GitCommitListDlg::OnPreviousUI(wxUpdateUIEvent& event) { event.Enable(m_skip >= 100); }
309
310 void GitCommitListDlg::OnNextUpdateUI(wxUpdateUIEvent& event)
311 {
312 event.Enable(m_dvListCtrlCommitList->GetItemCount() >= 100);
313 }
314
315 void GitCommitListDlg::OnBtnClose(wxCommandEvent& event) { Destroy(); }
316
317 void GitCommitListDlg::OnCharHook(wxKeyEvent& event) // Needed to catch ESC
318 {
319 event.Skip();
320 if(event.GetKeyCode() == WXK_ESCAPE) {
321 Destroy();
322 }
323 }