Debian Codegraph @

git/gitdiffchoosecommitishdlg.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2017 Eran Ifrah
5 // file name : gitdiffchoosecommitishdlg.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 "asyncprocess.h"
27 #include "cl_config.h"
28 #include "git.h"
29 #include "gitdiffchoosecommitishdlg.h"
30 #include "gitentry.h"
31 #include "globals.h"
32 #include "processreaderthread.h"
33 #include "windowattrmanager.h"
34 #include <wx/tokenzr.h>
35
36 GitDiffChooseCommitishDlg::GitDiffChooseCommitishDlg(wxWindow* parent, GitPlugin* plugin)
37 : GitDiffChooseCommitishDlgBase(parent)
38 , m_plugin(plugin)
39 , m_selectedRadio1(3)
40 , m_selectedRadio2(3)
41 , m_activeChoice1(m_choiceCommit1)
42 , m_activeChoice2(m_choiceCommit2)
43 {
44 WindowAttrManager::Load(this);
45
46 // Set a max width for these, else selecting a commit with a long message will break the layout
47 m_choiceCommit1->SetMaxSize(wxSize(60, -1));
48 m_choiceCommit2->SetMaxSize(wxSize(60, -1));
49
50 clConfig conf("git.conf");
51 GitEntry data;
52 conf.ReadItem(&data);
53
54 m_selectedRadio1 = data.GetGitDiffChooseDlgRadioSel1();
55 m_selectedRadio2 = data.GetGitDiffChooseDlgRadioSel2();
56 wxRadioButton* radiosL[] = { m_radioBranch1, m_radioTag1, m_radioCommit1, m_radioUserEntered1 };
57 wxRadioButton* radiosR[] = { m_radioBranch2, m_radioTag2, m_radioCommit2, m_radioUserEntered2 };
58 wxItemContainerImmutable* choicesL[] = { m_choiceBranch1, m_choiceTag1, m_choiceCommit1, m_comboCommitish1 };
59 wxItemContainerImmutable* choicesR[] = { m_choiceBranch2, m_choiceTag2, m_choiceCommit2, m_comboCommitish2 };
60 if(m_selectedRadio1 > -1 && m_selectedRadio1 < 4) {
61 radiosL[m_selectedRadio1]->SetValue(1);
62 m_activeChoice1 = choicesL[m_selectedRadio1];
63 }
64 if(m_selectedRadio2 > -1 && m_selectedRadio2 < 4) {
65 radiosR[m_selectedRadio2]->SetValue(1);
66 m_activeChoice2 = choicesR[m_selectedRadio2];
67 }
68 m_comboCommitish1->Append(data.GetGitDiffChooseDlgCBoxValues1());
69 m_comboCommitish2->Append(data.GetGitDiffChooseDlgCBoxValues2());
70
71 m_plugin->AsyncRunGitWithCallback(
72 " --no-pager branch -a --no-color",
73 [this](const wxString& output) {
74 wxArrayString items = wxStringTokenize(output, "\n", wxTOKEN_STRTOK);
75 m_choiceBranch1->Set(items);
76 m_choiceBranch1->Set(items);
77 },
78 IProcessCreateDefault | IProcessWrapInShell, m_plugin->GetRepositoryPath());
79
80 m_plugin->AsyncRunGitWithCallback(
81 " --no-pager tag",
82 [this](const wxString& output) {
83 wxArrayString items = wxStringTokenize(output, "\n", wxTOKEN_STRTOK);
84 m_choiceTag1->Set(items);
85 m_choiceTag2->Set(items);
86 },
87 IProcessCreateDefault | IProcessWrapInShell, m_plugin->GetRepositoryPath());
88
89 // Restrict the commits to 1000: filling a wxChoice with many more froze CodeLite for several minutes
90 // and in any case, selecting one particular commit out of hundreds is not easy!
91 m_plugin->AsyncRunGitWithCallback(
92 " --no-pager log -1000 --format=\"%h %<(60,trunc)%s\"",
93 [this](const wxString& output) {
94 wxArrayString items = wxStringTokenize(output, "\n", wxTOKEN_STRTOK);
95 m_choiceCommit1->Set(items);
96 m_choiceCommit2->Set(items);
97 },
98 IProcessCreateDefault | IProcessWrapInShell, m_plugin->GetRepositoryPath());
99 }
100
101 GitDiffChooseCommitishDlg::~GitDiffChooseCommitishDlg()
102 {
103 wxArrayString comboCommitish1Strings = m_comboCommitish1->GetStrings();
104 if(m_selectedRadio1 == 3) {
105 wxString sel = m_comboCommitish1->GetValue();
106 if(!sel.empty()) {
107 if(comboCommitish1Strings.Index(sel) != wxNOT_FOUND) {
108 comboCommitish1Strings.Remove(sel);
109 }
110 comboCommitish1Strings.Insert(sel, 0);
111 }
112 }
113 wxArrayString comboCommitish2Strings = m_comboCommitish2->GetStrings();
114 if(m_selectedRadio1 == 3) {
115 wxString sel = m_comboCommitish2->GetValue();
116 if(!sel.empty()) {
117 if(comboCommitish2Strings.Index(sel) != wxNOT_FOUND) {
118 comboCommitish2Strings.Remove(sel);
119 }
120 comboCommitish2Strings.Insert(sel, 0);
121 }
122 }
123 clConfig conf("git.conf");
124 GitEntry data;
125 conf.ReadItem(&data);
126
127 data.SetGitDiffChooseDlgRadioSel1(m_selectedRadio1);
128 data.SetGitDiffChooseDlgRadioSel2(m_selectedRadio2);
129 data.SetGitDiffChooseDlgCBoxValues1(comboCommitish1Strings);
130 data.SetGitDiffChooseDlgCBoxValues2(comboCommitish2Strings);
131
132 conf.WriteItem(&data);
133 }
134
135 wxString GitDiffChooseCommitishDlg::GetAncestorSetting(wxSpinCtrl* spin) const
136 {
137 wxString setting;
138 if(spin) {
139 int value = spin->GetValue();
140 if(value > 0) {
141 setting = wxString::Format("~%i", value);
142 }
143 }
144 return setting;
145 }
146
147 void GitDiffChooseCommitishDlg::OnUpdateUIBranch1(wxUpdateUIEvent& event) { event.Enable(m_radioBranch1->GetValue()); }
148 void GitDiffChooseCommitishDlg::OnUpdateUIBranch2(wxUpdateUIEvent& event) { event.Enable(m_radioBranch2->GetValue()); }
149 void GitDiffChooseCommitishDlg::OnUpdateUICommit1(wxUpdateUIEvent& event) { event.Enable(m_radioCommit1->GetValue()); }
150 void GitDiffChooseCommitishDlg::OnUpdateUICommit2(wxUpdateUIEvent& event) { event.Enable(m_radioCommit2->GetValue()); }
151 void GitDiffChooseCommitishDlg::OnUpdateUICommitish1(wxUpdateUIEvent& event)
152 {
153 event.Enable(m_radioUserEntered1->GetValue());
154 }
155 void GitDiffChooseCommitishDlg::OnUpdateUICommitish2(wxUpdateUIEvent& event)
156 {
157 event.Enable(m_radioUserEntered2->GetValue());
158 }
159 void GitDiffChooseCommitishDlg::OnUpdateUITags1(wxUpdateUIEvent& event) { event.Enable(m_radioTag1->GetValue()); }
160 void GitDiffChooseCommitishDlg::OnUpdateUITags2(wxUpdateUIEvent& event) { event.Enable(m_radioTag2->GetValue()); }
161 void GitDiffChooseCommitishDlg::OnRadioBranch1Selected(wxCommandEvent& WXUNUSED(event))
162 {
163 m_activeChoice1 = m_choiceBranch1;
164 m_selectedRadio1 = 0;
165 }
166 void GitDiffChooseCommitishDlg::OnRadioBranch2Selected(wxCommandEvent& WXUNUSED(event))
167 {
168 m_activeChoice2 = m_choiceBranch2;
169 m_selectedRadio2 = 0;
170 }
171 void GitDiffChooseCommitishDlg::OnRadioTag1Selected(wxCommandEvent& WXUNUSED(event))
172 {
173 m_activeChoice1 = m_choiceTag1;
174 m_selectedRadio1 = 1;
175 }
176 void GitDiffChooseCommitishDlg::OnRadioTag2Selected(wxCommandEvent& WXUNUSED(event))
177 {
178 m_activeChoice2 = m_choiceTag2;
179 m_selectedRadio2 = 1;
180 }
181 void GitDiffChooseCommitishDlg::OnRadioCommit1Selected(wxCommandEvent& WXUNUSED(event))
182 {
183 m_activeChoice1 = m_choiceCommit1;
184 m_selectedRadio1 = 2;
185 }
186 void GitDiffChooseCommitishDlg::OnRadioCommit2Selected(wxCommandEvent& WXUNUSED(event))
187 {
188 m_activeChoice2 = m_choiceCommit2;
189 m_selectedRadio2 = 2;
190 }
191 void GitDiffChooseCommitishDlg::OnRadioUser1Selected(wxCommandEvent& WXUNUSED(event))
192 {
193 m_activeChoice1 = m_comboCommitish1;
194 m_selectedRadio1 = 3;
195 }
196 void GitDiffChooseCommitishDlg::OnRadioUser2Selected(wxCommandEvent& WXUNUSED(event))
197 {
198 m_activeChoice2 = m_comboCommitish2;
199 m_selectedRadio2 = 3;
200 }
201 void GitDiffChooseCommitishDlg::OnTextFirstUI(wxUpdateUIEvent& event)
202 {
203 wxString text =
204 (m_activeChoice1 == m_comboCommitish1) ? m_comboCommitish1->GetValue() : m_activeChoice1->GetStringSelection();
205 if(text.StartsWith("* ")) {
206 text = text.Mid(2); // Remove the 'active branch' marker
207 }
208 if(m_activeChoice1 == m_choiceCommit1) {
209 text = text.BeforeFirst(' '); // We want only the commit hash, not the message
210 }
211
212 static_cast<wxTextCtrl*>(event.GetEventObject())->ChangeValue(text.Trim().Trim(false));
213 }
214 void GitDiffChooseCommitishDlg::OnTextSecondUI(wxUpdateUIEvent& event)
215 {
216 wxString text =
217 (m_activeChoice2 == m_comboCommitish2) ? m_comboCommitish2->GetValue() : m_activeChoice2->GetStringSelection();
218 if(text.StartsWith("* ")) {
219 text = text.Mid(2); // Remove the 'active branch' marker
220 }
221 if(m_activeChoice2 == m_choiceCommit2) {
222 text = text.BeforeFirst(' '); // We want only the commit hash, not the message
223 }
224
225 static_cast<wxTextCtrl*>(event.GetEventObject())->ChangeValue(text.Trim().Trim(false));
226 }
227 void GitDiffChooseCommitishDlg::OnBranch1Changed(wxCommandEvent& event)
228 {
229 wxString newBranch = m_choiceBranch1->GetString(event.GetInt());
230 if(newBranch.StartsWith("* ")) {
231 newBranch = newBranch.Mid(2); // Remove the 'active branch' marker
232 }
233 m_plugin->AsyncRunGitWithCallback(
234 " --no-pager log -1000 --format=\"%h %<(60,trunc)%s\" " + newBranch,
235 [this](const wxString& output) {
236 wxArrayString items = wxStringTokenize(output, "\n", wxTOKEN_STRTOK);
237 m_choiceCommit1->Set(items);
238 },
239 IProcessCreateDefault | IProcessWrapInShell, m_plugin->GetRepositoryPath());
240 }
241 void GitDiffChooseCommitishDlg::OnBranch2Changed(wxCommandEvent& event)
242 {
243 wxString newBranch = m_choiceBranch2->GetString(event.GetInt());
244 if(newBranch.StartsWith("* ")) {
245 newBranch = newBranch.Mid(2); // Remove the 'active branch' marker
246 }
247
248 m_plugin->AsyncRunGitWithCallback(
249 " --no-pager log -1000 --format=\"%h %<(60,trunc)%s\" " + newBranch,
250 [this](const wxString& output) {
251 wxArrayString items = wxStringTokenize(output, "\n", wxTOKEN_STRTOK);
252 m_choiceCommit2->Set(items);
253 },
254 IProcessCreateDefault | IProcessWrapInShell, m_plugin->GetRepositoryPath());
255 }