Debian Codegraph @

git/gitCommitDlg.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : gitCommitDlg.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 "gitCommitDlg.h"
27
28 #include "ColoursAndFontsManager.h"
29 #include "GitDiffOutputParser.h"
30 #include "clSingleChoiceDialog.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 "windowattrmanager.h"
38
39 #include <wx/tokenzr.h>
40
41 GitCommitDlg::GitCommitDlg(wxWindow* parent, GitPlugin* plugin, const wxString& workingDir)
42 : GitCommitDlgBase(parent)
43 , m_plugin(plugin)
44 , m_workingDir(workingDir)
45 , m_toggleChecks(false)
46 {
47 m_dvListCtrlFiles->SetBitmaps(clGetManager()->GetStdIcons()->GetStandardMimeBitmapListPtr());
48 // read the configuration
49 clConfig conf("git.conf");
50 GitEntry data;
51 conf.ReadItem(&data);
52 m_splitterInner->CallAfter(&wxSplitterWindow::SetSashPosition, data.GetGitCommitDlgHSashPos(), true);
53 m_splitterMain->CallAfter(&wxSplitterWindow::SetSashPosition, data.GetGitCommitDlgVSashPos(), true);
54
55 LexerConf::Ptr_t diffLexer = ColoursAndFontsManager::Get().GetLexer("diff");
56 if(diffLexer) {
57 diffLexer->Apply(m_stcDiff);
58 }
59
60 auto images = m_toolbar->GetBitmapsCreateIfNeeded();
61 m_toolbar->AddTool(XRCID("ID_CHECKALL"), _("Toggle files"), images->Add("check-all"));
62 m_toolbar->AddTool(XRCID("ID_HISTORY"), _("Show commit history"), images->Add("history"));
63 m_toolbar->Realize();
64 m_toolbar->Bind(wxEVT_TOOL, &GitCommitDlg::OnToggleCheckAll, this, XRCID("ID_CHECKALL"));
65 m_toolbar->Bind(wxEVT_TOOL, &GitCommitDlg::OnCommitHistory, this, XRCID("ID_HISTORY"));
66 m_editEventsHandlerDiffStc.Reset(new clEditEventsHandler(m_stcDiff));
67 ::clSetDialogBestSizeAndPosition(this);
68 CentreOnParent();
69
70 // set the focus to the text control
71 m_stcCommitMessage->CallAfter(&wxStyledTextCtrl::SetFocus);
72 }
73
74 /*******************************************************************************/
75 GitCommitDlg::~GitCommitDlg()
76 {
77 // read the configuration
78 clConfig conf("git.conf");
79 GitEntry data;
80 conf.ReadItem(&data);
81
82 data.SetGitCommitDlgHSashPos(m_splitterInner->GetSashPosition());
83 data.SetGitCommitDlgVSashPos(m_splitterMain->GetSashPosition());
84 conf.WriteItem(&data);
85 }
86
87 /*******************************************************************************/
88 void GitCommitDlg::AppendDiff(const wxString& diff)
89 {
90 GitDiffOutputParser diff_parser;
91 diff_parser.GetDiffMap(diff, m_diffMap);
92 m_dvListCtrlFiles->DeleteAllItems();
93 wxVector<wxVariant> cols;
94 BitmapLoader* bitmaps = clGetManager()->GetStdIcons();
95 std::vector<wxString> names;
96 names.reserve(m_diffMap.size());
97 for(const wxStringMap_t::value_type& vt : m_diffMap) {
98 names.push_back(vt.first);
99 }
100
101 std::sort(names.begin(), names.end(), [](const wxString& a, const wxString& b) { return a.CmpNoCase(b) < 0; });
102
103 for(const wxString& filename : names) {
104 cols.clear();
105 cols.push_back(::MakeCheckboxVariant(filename, true, bitmaps->GetMimeImageId(filename)));
106 m_dvListCtrlFiles->AppendItem(cols);
107 }
108
109 if(!names.empty()) {
110 m_dvListCtrlFiles->Select(m_dvListCtrlFiles->RowToItem(0));
111 wxStringMap_t::iterator it = m_diffMap.begin();
112 m_stcDiff->SetText((*it).second);
113 m_stcDiff->SetReadOnly(true);
114 }
115 }
116
117 /*******************************************************************************/
118 wxArrayString GitCommitDlg::GetSelectedFiles()
119 {
120 wxArrayString ret;
121 for(size_t i = 0; i < m_dvListCtrlFiles->GetItemCount(); ++i) {
122 wxDataViewItem item = m_dvListCtrlFiles->RowToItem(i);
123 if(m_dvListCtrlFiles->IsItemChecked(item, 0)) {
124 ret.Add(m_dvListCtrlFiles->GetItemText(item, 0));
125 }
126 }
127 return ret;
128 }
129
130 /*******************************************************************************/
131 wxString GitCommitDlg::GetCommitMessage()
132 {
133 wxString msg = m_stcCommitMessage->GetText();
134 return msg;
135 }
136
137 /*******************************************************************************/
138 void GitCommitDlg::OnChangeFile(wxDataViewEvent& e)
139 {
140 wxString file = m_dvListCtrlFiles->GetItemText(e.GetItem(), 0);
141 m_stcDiff->SetReadOnly(false);
142 m_stcDiff->SetText(m_diffMap[file]);
143 ::clRecalculateSTCHScrollBar(m_stcDiff);
144 m_stcDiff->SetReadOnly(true);
145 }
146
147 void GitCommitDlg::OnCommitOK(wxCommandEvent& event)
148 {
149 if(m_stcCommitMessage->GetText().IsEmpty() && !IsAmending()) {
150 ::wxMessageBox(_("Git requires a commit message"), "codelite", wxICON_WARNING | wxOK | wxCENTER);
151 return;
152 }
153 EndModal(wxID_OK);
154 }
155
156 /*******************************************************************************/
157 void GitCommitDlg::OnToggleCheckAll(wxCommandEvent& event)
158 {
159 for(size_t i = 0; i < m_dvListCtrlFiles->GetItemCount(); ++i) {
160 m_dvListCtrlFiles->SetItemChecked(m_dvListCtrlFiles->RowToItem(i), m_toggleChecks, 0);
161 }
162 m_toggleChecks = !m_toggleChecks;
163 }
164
165 void GitCommitDlg::OnCommitHistory(wxCommandEvent& event)
166 {
167 clSingleChoiceDialog dlg(this, m_history);
168 dlg.SetLabel(_("Choose a commit"));
169 if(dlg.ShowModal() != wxID_OK)
170 return;
171
172 wxString commitHash = dlg.GetSelection().BeforeFirst(' ');
173 if(!commitHash.empty()) {
174 wxString selectedCommit;
175 m_plugin->DoExecuteCommandSync("log -1 --pretty=format:\"%B\" " + commitHash, &selectedCommit);
176 if(!selectedCommit.empty()) {
177 m_stcCommitMessage->SetText(selectedCommit);
178 }
179 }
180 }
181
182 void GitCommitDlg::OnCommitHistoryUI(wxUpdateUIEvent& event) { event.Enable(!m_history.IsEmpty()); }
183
184 void GitCommitDlg::OnAmendClicked(wxCommandEvent& event)
185 {
186 if(event.IsChecked()) {
187 if(!m_previousCommitMessage.empty()) {
188 m_stashedMessage = m_stcCommitMessage->GetText();
189 m_stcCommitMessage->SetText(m_previousCommitMessage);
190 }
191 } else {
192 if(!m_stashedMessage.empty()) {
193 m_stcCommitMessage->SetText(m_stashedMessage);
194 }
195 }
196 }