Debian Codegraph @

git/gitDiffDlg.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : gitDiffDlg.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 "editor_config.h"
27 #include "gitDiffDlg.h"
28 #include "gitentry.h"
29 #include "windowattrmanager.h"
30
31 #include "GitDiffOutputParser.h"
32 #include "asyncprocess.h"
33 #include "cl_config.h"
34 #include "git.h"
35 #include "gitCommitEditor.h"
36 #include "gitdiffchoosecommitishdlg.h"
37 #include "globals.h"
38 #include "processreaderthread.h"
39 #include <wx/tokenzr.h>
40
41 BEGIN_EVENT_TABLE(GitDiffDlg, wxDialog)
42
43 END_EVENT_TABLE()
44
45 GitDiffDlg::GitDiffDlg(wxWindow* parent, const wxString& workingDir, GitPlugin* plugin)
46 : GitDiffDlgBase(parent)
47 , m_workingDir(workingDir)
48 , m_plugin(plugin)
49 {
50 clConfig conf("git.conf");
51 GitEntry data;
52 conf.ReadItem(&data);
53
54 SetName("GitDiffDlg");
55 WindowAttrManager::Load(this);
56 m_splitter->SetSashPosition(data.GetGitDiffDlgSashPos());
57
58 Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &GitDiffDlg::OnProcessOutput, this);
59 Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &GitDiffDlg::OnProcessTerminated, this);
60
61 CreateDiff();
62 ::clSetDialogBestSizeAndPosition(this);
63 }
64
65 /*******************************************************************************/
66 GitDiffDlg::~GitDiffDlg()
67 {
68
69 clConfig conf("git.conf");
70 GitEntry data;
71 conf.ReadItem(&data);
72 data.SetGitDiffDlgSashPos(m_splitter->GetSashPosition());
73 conf.WriteItem(&data);
74 }
75
76 /*******************************************************************************/
77
78 void GitDiffDlg::CreateDiff()
79 {
80 m_commandOutput.Empty(); // There might be stale contents from a previous run
81
82 wxString command = PrepareCommand();
83 m_plugin->DisplayMessage("GitDiff: " + command);
84 m_process = m_plugin->AsyncRunGit(this, command, IProcessCreateDefault | IProcessWrapInShell,
85 m_plugin->GetRepositoryPath());
86 }
87
88 wxString GitDiffDlg::PrepareCommand() const
89 {
90 wxString commitsString = m_commits;
91 if(commitsString.empty()) {
92 // Standard diff of changes against HEAD, but which sort?
93 switch(m_radioBoxStaged->GetSelection()) {
94 case 0:
95 commitsString = "";
96 break; // Unstaged only
97 case 1:
98 commitsString = "--cached ";
99 break; // Staged only
100 default:
101 commitsString = "HEAD "; // Both
102 }
103 }
104
105 wxString command(" --no-pager diff ");
106 if(m_checkIgnoreSpace->GetValue()) {
107 command << "--ignore-all-space "; // -w
108 }
109
110 return command + commitsString;
111 }
112
113 void GitDiffDlg::SetDiff(const wxString& diff)
114 {
115 wxString commandOutput = diff;
116 m_fileListBox->Clear();
117 m_diffMap.clear();
118 commandOutput.Replace(wxT("\r"), wxT(""));
119 GitDiffOutputParser diff_parser;
120 diff_parser.GetDiffMap(m_commandOutput, m_diffMap);
121
122 for(wxStringMap_t::iterator it = m_diffMap.begin(); it != m_diffMap.end(); ++it) {
123 m_fileListBox->Append((*it).first);
124 }
125
126 m_editor->SetReadOnly(false);
127 m_editor->SetText(wxT(""));
128
129 if(m_diffMap.size() != 0) {
130 wxStringMap_t::iterator it = m_diffMap.begin();
131 m_editor->SetText((*it).second);
132 m_fileListBox->Select(0);
133 m_editor->SetReadOnly(true);
134 }
135 }
136
137 /*******************************************************************************/
138 void GitDiffDlg::OnChangeFile(wxCommandEvent& e)
139 {
140 int sel = m_fileListBox->GetSelection();
141 wxString file = m_fileListBox->GetString(sel);
142 m_editor->SetReadOnly(false);
143 m_editor->SetText(m_diffMap[file]);
144 m_editor->SetReadOnly(true);
145 }
146
147 void GitDiffDlg::OnChoseCommits(wxCommandEvent& event)
148 {
149 GitDiffChooseCommitishDlg dlg(this, m_plugin);
150 if(dlg.ShowModal() == wxID_OK) {
151 wxString commit1 = dlg.GetFirstCommit();
152 wxString joiner = dlg.GetJoiner(); // May be ' ' or '...'
153 wxString commit2 = dlg.GetSecondCommit();
154 m_commits = commit1 + joiner + commit2;
155
156 CreateDiff();
157 }
158 }
159
160 void GitDiffDlg::OnProcessOutput(clProcessEvent& event) { m_commandOutput.Append(event.GetOutput()); }
161
162 void GitDiffDlg::OnProcessTerminated(clProcessEvent& event)
163 {
164 wxUnusedVar(event);
165 wxDELETE(m_process);
166
167 SetDiff(m_commandOutput);
168 }
169
170 void GitDiffDlg::OnOptionsChanged(wxCommandEvent& event) { CreateDiff(); }