Debian Codegraph @

git/gitentry.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : gitentry.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 //////////////////////////////////////////////////////////////////////////////
27
28 #include "gitentry.h"
29
30 #include "GitLocator.h"
31
32 #include <wx/ffile.h>
33 #include <wx/fileconf.h>
34 #include <wx/msgdlg.h>
35 #include <wx/sstream.h>
36
37 const wxEventType wxEVT_GIT_CONFIG_CHANGED = ::wxNewEventType();
38
39 #define GIT_EXE "git"
40 #define GITK_EXE "gitk"
41
42 GitEntry::GitEntry()
43 : clConfigItem("git-settings")
44 , m_colourTrackedFile(*wxGREEN)
45 , m_colourDiffFile(*wxRED)
46 #ifdef __WXMSW__
47 , m_flags(Git_Show_Terminal)
48 #else
49 , m_flags(0)
50 #endif
51 , m_gitDiffDlgSashPos(0)
52 , m_gitConsoleSashPos(0)
53 , m_gitCommitDlgHSashPos(0)
54 , m_gitCommitDlgVSashPos(0)
55 , m_gitBlameShowLogControls(true)
56 , m_gitBlameShowParentCommit(true)
57 , m_gitBlameDlgMainSashPos(0)
58 , m_gitBlameDlgHSashPos(0)
59 , m_gitBlameDlgVSashPos(0)
60 {
61 GitLocator locator;
62 wxFileName gitpath;
63 if(locator.GetExecutable(gitpath)) {
64 m_pathGIT = gitpath.GetFullPath();
65 }
66 locator.MSWGetGitShellCommand(m_gitShellCommand);
67 }
68
69 GitEntry::~GitEntry() {}
70
71 void GitEntry::FromJSON(const JSONItem& json)
72 {
73 GitLocator locator;
74 m_entries = json.namedObject("m_entries").toStringMap();
75 wxString track, diff;
76 track = json.namedObject("m_colourTrackedFile").toString();
77 diff = json.namedObject("m_colourDiffFile").toString();
78 m_pathGIT = json.namedObject("m_pathGIT").toString(m_pathGIT);
79
80 // If the current path does not exists, search again
81 bool updateGitShell = false;
82 if(!wxFileName::FileExists(m_pathGIT)) {
83 GitLocator locator;
84 wxFileName gitpath;
85 if(locator.GetExecutable(gitpath)) {
86 updateGitShell = true;
87 m_pathGIT = gitpath.GetFullPath();
88 }
89 }
90 m_pathGITK = json.namedObject("m_pathGITK").toString(m_pathGITK);
91 m_flags = json.namedObject("m_flags").toSize_t(m_flags);
92 m_gitDiffDlgSashPos = json.namedObject("m_gitDiffDlgSashPos").toInt(m_gitDiffDlgSashPos);
93 m_gitDiffChooseDlgRadioSel1 = json.namedObject("m_gitDiffChooseDlgRadioSel1").toInt(m_gitDiffChooseDlgRadioSel1);
94 m_gitDiffChooseDlgRadioSel2 = json.namedObject("m_gitDiffChooseDlgRadioSel2").toInt(m_gitDiffChooseDlgRadioSel2);
95 m_gitDiffChooseDlgCBoxValues1 = json.namedObject("m_gitDiffChooseDlgCBoxValues1").toArrayString();
96 m_gitDiffChooseDlgCBoxValues2 = json.namedObject("m_gitDiffChooseDlgCBoxValues2").toArrayString();
97 m_gitConsoleSashPos = json.namedObject("m_gitConsoleSashPos").toInt(m_gitConsoleSashPos);
98 m_gitCommitDlgHSashPos = json.namedObject("m_gitCommitDlgHSashPos").toInt(m_gitCommitDlgHSashPos);
99 m_gitCommitDlgVSashPos = json.namedObject("m_gitCommitDlgVSashPos").toInt(m_gitCommitDlgVSashPos);
100 m_gitBlameShowLogControls = json.namedObject("gitBlameShowLogControls").toBool(m_gitBlameShowLogControls);
101 m_gitBlameShowParentCommit = json.namedObject("m_gitBlameShowParentCommit").toBool(m_gitBlameShowParentCommit);
102 m_gitBlameDlgMainSashPos = json.namedObject("m_gitBlameDlgMainSashPos").toInt(m_gitBlameDlgMainSashPos);
103 m_gitBlameDlgHSashPos = json.namedObject("m_gitBlameDlgHSashPos").toInt(m_gitBlameDlgHSashPos);
104 m_gitBlameDlgVSashPos = json.namedObject("m_gitBlameDlgVSashPos").toInt(m_gitBlameDlgVSashPos);
105
106 wxString defaultGitShell;
107 locator.MSWGetGitShellCommand(defaultGitShell);
108 if(updateGitShell) {
109 m_gitShellCommand = defaultGitShell;
110 } else {
111 m_gitShellCommand = json.namedObject("m_gitShellCommand").toString(defaultGitShell);
112 }
113
114 // override the colour only if it is a valid colour
115 if(!track.IsEmpty()) {
116 m_colourTrackedFile = track;
117 }
118 if(!diff.IsEmpty()) {
119 m_colourDiffFile = diff;
120 }
121
122 m_recentCommits = json.namedObject("m_recentCommits").toArrayString();
123
124 // read the git commands
125 JSONItem arrCommands = json.namedObject("Commands");
126 for(int i = 0; i < arrCommands.arraySize(); ++i) {
127 GitCommandsEntries entry;
128 entry.FromJSON(arrCommands.arrayItem(i));
129 m_commandsMap.insert(std::make_pair(entry.GetCommandname(), entry));
130 }
131
132 // Load the workspace info: each known workspace name & its known projects' repo paths
133 JSONItem arrWorkspaces = json.namedObject("Workspaces");
134 for(int i = 0; i < arrWorkspaces.arraySize(); ++i) {
135 GitWorkspace workspace;
136 workspace.FromJSON(arrWorkspaces.arrayItem(i));
137 m_workspacesMap.insert(std::make_pair(workspace.GetWorkspaceName(), workspace));
138 }
139 }
140
141 JSONItem GitEntry::ToJSON() const
142 {
143 JSONItem json = JSONItem::createObject(GetName());
144 json.addProperty("m_entries", m_entries);
145 if(m_colourTrackedFile.IsOk()) {
146 json.addProperty("m_colourTrackedFile", m_colourTrackedFile.GetAsString(wxC2S_HTML_SYNTAX));
147 }
148
149 if(m_colourDiffFile.IsOk()) {
150 json.addProperty("m_colourDiffFile", m_colourDiffFile.GetAsString(wxC2S_HTML_SYNTAX));
151 }
152
153 json.addProperty("m_pathGIT", m_pathGIT);
154 json.addProperty("m_pathGITK", m_pathGITK);
155 json.addProperty("m_flags", m_flags);
156 json.addProperty("m_gitDiffDlgSashPos", m_gitDiffDlgSashPos);
157 json.addProperty("m_gitDiffChooseDlgRadioSel1", m_gitDiffChooseDlgRadioSel1);
158 json.addProperty("m_gitDiffChooseDlgRadioSel2", m_gitDiffChooseDlgRadioSel2);
159 json.addProperty("m_gitDiffChooseDlgCBoxValues1", m_gitDiffChooseDlgCBoxValues1);
160 json.addProperty("m_gitDiffChooseDlgCBoxValues2", m_gitDiffChooseDlgCBoxValues2);
161 json.addProperty("m_gitConsoleSashPos", m_gitConsoleSashPos);
162 json.addProperty("m_gitCommitDlgHSashPos", m_gitCommitDlgHSashPos);
163 json.addProperty("m_gitCommitDlgVSashPos", m_gitCommitDlgVSashPos);
164 json.addProperty("m_recentCommits", m_recentCommits);
165 json.addProperty("m_gitShellCommand", m_gitShellCommand);
166 json.addProperty("gitBlameShowLogControls", m_gitBlameShowLogControls);
167 json.addProperty("m_gitBlameShowParentCommit", m_gitBlameShowParentCommit);
168 json.addProperty("m_gitBlameDlgMainSashPos", m_gitBlameDlgMainSashPos);
169 json.addProperty("m_gitBlameDlgHSashPos", m_gitBlameDlgHSashPos);
170 json.addProperty("m_gitBlameDlgVSashPos", m_gitBlameDlgVSashPos);
171
172 // Add the git commands array
173 JSONItem arrCommands = JSONItem::createArray("Commands");
174 json.append(arrCommands);
175 GitCommandsEntriesMap_t::const_iterator iter = m_commandsMap.begin();
176 for(; iter != m_commandsMap.end(); ++iter) {
177 iter->second.ToJSON(arrCommands);
178 }
179 // and the workspace info
180 JSONItem arrWorkspaces = JSONItem::createArray("Workspaces");
181 json.append(arrWorkspaces);
182 GitWorkspaceMap_t::const_iterator it = m_workspacesMap.begin();
183 for(; it != m_workspacesMap.end(); ++it) {
184 it->second.ToJSON(arrWorkspaces);
185 }
186 return json;
187 }
188
189 wxString GitEntry::GetGITExecutablePath() const
190 {
191 if(m_pathGIT.IsEmpty()) {
192 return GIT_EXE;
193
194 } else {
195 return m_pathGIT;
196 }
197 }
198
199 wxString GitEntry::GetGITKExecutablePath() const
200 {
201 if(m_pathGITK.IsEmpty()) {
202 return GITK_EXE;
203
204 } else {
205 return m_pathGITK;
206 }
207 }
208
209 GitEntry::GitProperties GitEntry::ReadGitProperties(const wxString& localRepoPath)
210 {
211 GitEntry::GitProperties props;
212 // Read the global name/email
213 // ~/.gitconfig | %USERPROFILE%\.gitconfig
214 {
215 wxFileName globalConfig(::wxGetHomeDir(), ".gitconfig");
216 if(globalConfig.Exists()) {
217 wxFFile fp(globalConfig.GetFullPath(), "rb");
218 if(fp.IsOpened()) {
219 wxString content;
220 fp.ReadAll(&content, wxConvUTF8);
221 wxStringInputStream sis(content);
222
223 wxFileConfig conf(sis);
224 conf.Read("user/email", &props.global_email);
225 conf.Read("user/name", &props.global_username);
226
227 fp.Close();
228 }
229 }
230 }
231
232 // Read the repo config file
233 if(!localRepoPath.IsEmpty()) {
234 wxFileName localConfig(localRepoPath, "config");
235 localConfig.AppendDir(".git");
236 wxFFile fp(localConfig.GetFullPath(), "rb");
237 if(fp.IsOpened()) {
238 wxString content;
239 fp.ReadAll(&content, wxConvUTF8);
240 wxStringInputStream sis(content);
241
242 wxFileConfig conf(sis);
243 conf.Read("user/email", &props.local_email);
244 conf.Read("user/name", &props.local_username);
245
246 fp.Close();
247 }
248 }
249 return props;
250 }
251
252 GitCommandsEntries& GitEntry::GetGitCommandsEntries(const wxString& entryName)
253 {
254 if(!m_commandsMap.count(entryName)) {
255 GitCommandsEntries entries(entryName);
256 m_commandsMap.insert(std::make_pair(entryName, entries));
257 }
258
259 GitCommandsEntriesMap_t::iterator iter = m_commandsMap.find(entryName);
260 wxASSERT(iter != m_commandsMap.end());
261
262 return iter->second;
263 }
264
265 void GitEntry::AddGitCommandsEntry(GitCommandsEntries& entries, const wxString& entryName)
266 {
267 if(!m_commandsMap.count(entryName)) {
268 m_commandsMap.insert(std::make_pair(entryName, entries));
269 }
270 // Possible TODO: Append any novel items to the existing vector
271 }
272
273 void GitEntry::WriteGitProperties(const wxString& localRepoPath, const GitEntry::GitProperties& props)
274 {
275 // Read the global name/email
276 // ~/.gitconfig | %USERPROFILE%\.gitconfig
277 {
278 wxFileName globalConfig(::wxGetHomeDir(), ".gitconfig");
279 if(globalConfig.Exists()) {
280 wxFFile fp(globalConfig.GetFullPath(), "rb");
281 if(fp.IsOpened()) {
282 wxString content;
283 fp.ReadAll(&content, wxConvUTF8);
284 fp.Close();
285 wxStringInputStream sis(content);
286 wxFileConfig conf(sis);
287 conf.Write("user/email", props.global_email);
288 conf.Write("user/name", props.global_username);
289
290 // Write the content
291 content.Clear();
292 wxStringOutputStream sos(&content);
293 if(conf.Save(sos, wxConvUTF8)) {
294 wxFFile fpo(globalConfig.GetFullPath(), "w+b");
295 if(fpo.IsOpened()) {
296 fpo.Write(content, wxConvUTF8);
297 fpo.Close();
298 }
299 } else {
300 ::wxMessageBox("Could not save GIT global configuration. Configuration is unmodified", "git",
301 wxICON_WARNING | wxOK | wxCENTER);
302 }
303 }
304 }
305 }
306
307 // Read the repo config file
308 if(!localRepoPath.IsEmpty()) {
309 wxFileName localConfig(localRepoPath, "config");
310 localConfig.AppendDir(".git");
311 wxFFile fp(localConfig.GetFullPath(), "rb");
312 if(fp.IsOpened()) {
313 wxString content;
314 fp.ReadAll(&content, wxConvUTF8);
315 fp.Close();
316 wxStringInputStream sis(content);
317 wxFileConfig conf(sis);
318 conf.Write("user/email", props.local_email);
319 conf.Write("user/name", props.local_username);
320
321 content.Clear();
322 wxStringOutputStream sos(&content);
323 if(conf.Save(sos, wxConvUTF8)) {
324 wxFFile fpo(localConfig.GetFullPath(), "w+b");
325 if(fpo.IsOpened()) {
326 fpo.Write(content, wxConvUTF8);
327 fpo.Close();
328 }
329 } else {
330 ::wxMessageBox("Could not save GIT local configuration. Configuration is unmodified", "git",
331 wxICON_WARNING | wxOK | wxCENTER);
332 }
333 }
334 }
335 }
336
337 GitEntry& GitEntry::Load()
338 {
339 clConfig conf("git.conf");
340 conf.ReadItem(this);
341 return *this;
342 }
343
344 void GitEntry::Save()
345 {
346 clConfig conf("git.conf");
347 conf.WriteItem(this);
348 }
349
350 void GitEntry::AddRecentCommit(const wxString& commitMessage)
351 {
352 wxString msg = commitMessage;
353 msg.Trim().Trim(false);
354 if(msg.IsEmpty())
355 return;
356
357 if(m_recentCommits.Index(msg) == wxNOT_FOUND) {
358 m_recentCommits.Insert(msg, 0);
359 }
360
361 if(m_recentCommits.size() > 20) {
362 m_recentCommits.RemoveAt(m_recentCommits.size() - 1); // Remove the last commit
363 }
364 }
365
366 void GitEntry::DeleteEntry(const wxString& workspace)
367 {
368 if(m_entries.count(workspace)) {
369 m_entries.erase(workspace);
370 }
371 }
372
373 wxString GitEntry::GetProjectUserEnteredRepoPath(const wxString& nameHash)
374 {
375 wxString repoPath;
376 wxString projectName;
377 wxString workspaceName = nameHash.BeforeFirst('-', &projectName);
378
379 if(workspaceName.empty() || projectName.empty())
380 return "";
381
382 GitWorkspaceMap_t::iterator iter;
383
384 if(!m_workspacesMap.count(workspaceName)) {
385 // A new workspace so add it
386 GitWorkspace workspace(workspaceName);
387 m_workspacesMap.insert(std::make_pair(workspaceName, workspace));
388 }
389 iter = m_workspacesMap.find(workspaceName);
390 wxCHECK_MSG(iter != m_workspacesMap.end(), repoPath, "Failed to add a workspace to the entry");
391
392 GitWorkspace workspace = iter->second;
393 repoPath = workspace.GetProjectUserEnteredRepoPath(projectName);
394
395 return repoPath;
396 }
397
398 void GitEntry::SetProjectUserEnteredRepoPath(const wxString& repoPath, const wxString& nameHash)
399 {
400 wxString projectName;
401 wxString workspaceName = nameHash.BeforeFirst('-', &projectName);
402 if(workspaceName.empty() || projectName.empty()) {
403 return;
404 }
405
406 GitWorkspaceMap_t::iterator iter;
407
408 if(!m_workspacesMap.count(workspaceName)) {
409 // A new workspace so add it
410 GitWorkspace workspace(workspaceName);
411 m_workspacesMap.insert(std::make_pair(workspaceName, workspace));
412 }
413 iter = m_workspacesMap.find(workspaceName);
414 wxCHECK_RET(iter != m_workspacesMap.end(), "Failed to add a workspace to the entry");
415
416 GitWorkspace& workspace = iter->second;
417 workspace.SetProjectUserEnteredRepoPath(projectName, repoPath);
418 }
419
420 void GitCommandsEntries::FromJSON(const JSONItem& json)
421 {
422 m_commands.clear();
423 m_commandName = json.namedObject("m_commandName").toString();
424 m_lastUsed = json.namedObject("m_lastUsed").toInt();
425
426 JSONItem arrCommandChoices = json.namedObject("m_commands");
427 for(int i = 0; i < arrCommandChoices.arraySize(); ++i) {
428 GitLabelCommand item;
429 item.label = arrCommandChoices.arrayItem(i).namedObject("label").toString();
430 item.command = arrCommandChoices.arrayItem(i).namedObject("command").toString();
431 m_commands.push_back(item);
432 }
433 }
434
435 void GitCommandsEntries::ToJSON(JSONItem& arr) const
436 {
437 JSONItem obj = JSONItem::createObject();
438 obj.addProperty("m_commandName", m_commandName);
439 obj.addProperty("m_lastUsed", m_lastUsed);
440
441 JSONItem commandsArr = JSONItem::createArray("m_commands");
442 obj.append(commandsArr);
443
444 vGitLabelCommands_t::const_iterator iter = m_commands.begin();
445 for(; iter != m_commands.end(); ++iter) {
446 JSONItem e = JSONItem::createObject();
447 e.addProperty("label", iter->label);
448 e.addProperty("command", iter->command);
449 commandsArr.arrayAppend(e);
450 }
451 arr.arrayAppend(obj);
452 }
453
454 const wxString GitWorkspace::GetProjectUserEnteredRepoPath(const wxString& projectName)
455 {
456 return m_userEnteredRepoPath[projectName];
457 }
458
459 void GitWorkspace::SetProjectUserEnteredRepoPath(const wxString& projectName, const wxString& userEnteredRepoPath)
460 {
461 m_userEnteredRepoPath[projectName] = userEnteredRepoPath;
462 }
463
464 void GitWorkspace::FromJSON(const JSONItem& json)
465 {
466 m_projectData.clear();
467 SetWorkspaceName(json.namedObject("m_workspaceName").toString());
468 m_projectData = json.namedObject("m_projectData").toStringMap();
469 m_userEnteredRepoPath = json.namedObject("m_userEnteredRepoPath").toStringMap();
470 }
471
472 void GitWorkspace::ToJSON(JSONItem& arr) const
473 {
474 if(!GetWorkspaceName().empty()) {
475 JSONItem json = JSONItem::createObject(GetWorkspaceName());
476 json.addProperty("m_workspaceName", GetWorkspaceName());
477 json.addProperty("m_projectData", m_projectData);
478 json.addProperty("m_userEnteredRepoPath", m_userEnteredRepoPath);
479 arr.arrayAppend(json);
480 }
481 }