Debian Codegraph @

git/git.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : git.h
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 //
29 // copyright : (C) 2011 by René Kraus (aka upCASE)
30 //
31 //////////////////////////////////////////////////////////////////////////////
32
33 #ifndef __git__
34 #define __git__
35
36 #include <wx/progdlg.h>
37
38 #include "asyncprocess.h"
39 #include "clCodeLiteRemoteProcess.hpp"
40 #include "clTabTogglerHelper.h"
41 #include "cl_command_event.h"
42 #include "gitentry.h"
43 #include "gitui.h"
44 #include "ieditor.h"
45 #include "overlaytool.h"
46 #include "plugin.h"
47 #include "processreaderthread.h"
48 #include "project.h" // wxStringSet_t
49 #include <map>
50 #include <queue>
51 #include <set>
52 #include <vector>
53 #include <wx/progdlg.h>
54
55 class clTreeCtrl;
56 class clCommandProcessor;
57 class GitBlameDlg;
58
59 class gitAction
60 {
61 public:
62 int action;
63 wxString arguments;
64 wxString workingDirectory;
65
66 public:
67 gitAction()
68 : action(0)
69 , arguments("")
70 , workingDirectory("")
71 {
72 }
73 gitAction(int act, const wxString& args)
74 : action(act)
75 , arguments(args)
76 {
77 }
78 ~gitAction() {}
79 };
80
81 class GitConsole;
82 class GitCommitListDlg;
83
84 struct GitCmd {
85 wxString baseCommand;
86 size_t processFlags;
87
88 GitCmd(const wxString& cmd, size_t console)
89 : baseCommand(cmd)
90 , processFlags(console)
91 {
92 }
93 GitCmd()
94 : processFlags(IProcessCreateDefault)
95 {
96 }
97 typedef std::vector<GitCmd> Vec_t;
98 };
99
100 class GitPlugin : public IPlugin
101 {
102 friend class GitConsole;
103 friend class GitCommitListDlg;
104 friend class GitCommitDlg;
105
106 typedef std::map<int, int> IntMap_t;
107 enum {
108 gitNone = 0,
109 gitUpdateRemotes,
110 gitListAll,
111 gitListModified,
112 gitListRemotes,
113 gitAddFile,
114 gitDeleteFile,
115 gitDiffFile,
116 gitDiffRepoCommit,
117 gitDiffRepoShow,
118 gitResetFile,
119 gitResetRepo,
120 gitPull,
121 gitPush,
122 gitCommit,
123 gitBranchCreate,
124 gitBranchCurrent,
125 gitBranchList,
126 gitBranchListRemote,
127 gitBranchSwitch,
128 gitBranchSwitchRemote,
129 gitCommitList,
130 gitBlame,
131 gitBlameSummary,
132 gitRevlist,
133 gitRebase,
134 gitGarbageCollection,
135 gitClone,
136 gitStatus,
137 gitUndoAdd,
138 gitRmFiles,
139 gitApplyPatch,
140 gitRevertCommit,
141 gitStash,
142 gitStashPop,
143 gitConfig,
144 };
145
146 wxArrayString m_localBranchList;
147 wxArrayString m_remoteBranchList;
148 wxStringSet_t m_trackedFiles;
149 wxStringSet_t m_modifiedFiles;
150 bool m_addedFiles;
151 wxArrayString m_remotes;
152 wxColour m_colourTrackedFile;
153 wxColour m_colourDiffFile;
154 wxString m_pathGITExecutable;
155 wxString m_pathGITKExecutable;
156 wxString m_repositoryDirectory;
157 wxString m_userEnteredRepositoryDirectory;
158 wxString m_currentBranch;
159 std::list<gitAction> m_gitActionQueue;
160 wxTimer m_progressTimer;
161 wxString m_progressMessage;
162 wxString m_commandOutput;
163 bool m_bActionRequiresTreUpdate;
164 IProcess* m_process;
165 wxEvtHandler* m_eventHandler;
166 clToolBar* m_pluginToolbar;
167 wxMenu* m_pluginMenu;
168 IntMap_t m_treeImageMapping;
169 int m_baseImageCount;
170 GitConsole* m_console;
171 wxString m_workspace_file;
172 GitCommitListDlg* m_commitListDlg;
173 wxArrayString m_filesSelected;
174 wxString m_selectedFolder;
175 clCommandProcessor* m_commandProcessor;
176 clTabTogglerHelper::Ptr_t m_tabToggler;
177 GitBlameDlg* m_gitBlameDlg;
178 std::unordered_map<wxString, std::vector<wxString>>
179 m_blameMap; // contains file: comment per line (extracted from the 'git blame' info)
180 size_t m_configFlags = 0;
181 wxString m_lastBlameMessage;
182 bool m_isRemoteWorkspace = false;
183 wxString m_remoteWorkspaceAccount;
184 clCodeLiteRemoteProcess m_remoteProcess;
185 wxString m_codeliteRemoteScriptPath;
186
187 private:
188 void StartCodeLiteRemote();
189 void ClearCodeLiteRemoteInfo();
190 void DoCreateTreeImages();
191 void DoShowDiffViewer(const wxString& headFile, const wxString& fileName);
192 void DoExecuteCommands(const GitCmd::Vec_t& commands, const wxString& workingDir);
193 bool DoExecuteCommandSync(const wxString& command, wxString* commandOutput,
194 const wxString& workingDir = wxEmptyString);
195
196 void DoSetTreeItemImage(clTreeCtrl* ctrl, const wxTreeItemId& item, OverlayTool::BmpType bmpType) const;
197 void InitDefaults();
198 void AddDefaultActions();
199 void LoadDefaultGitCommands(GitEntry& data, bool overwrite = false);
200 void ProcessGitActionQueue();
201 void ColourFileTree(clTreeCtrl* tree, const wxStringSet_t& files, OverlayTool::BmpType bmpType) const;
202 void CreateFilesTreeIDsMap(std::map<wxString, wxTreeItemId>& IDs, bool ifmodified = false) const;
203 void DoShowCommitDialog(const wxString& diff, wxString& commitArgs);
204 void DoRefreshView(bool ensureVisible);
205
206 /// Workspace management
207 bool IsWorkspaceOpened() const;
208 wxString GetCommitMessageFile() const;
209 wxString FindRepositoryRoot(const wxString& starting_dir) const;
210 void FinishGitListAction(const gitAction& ga);
211 void ListBranchAction(const gitAction& ga);
212 void GetCurrentBranchAction(const gitAction& ga);
213 void UpdateFileTree();
214
215 void ShowProgress(const wxString& message, bool pulse = true);
216 void HideProgress();
217 void DoCleanup();
218 void DoAddFiles(const wxArrayString& files);
219 void DoResetFiles(const wxArrayString& files);
220 void DoGetFileViewSelectedFiles(wxArrayString& files, bool relativeToRepo);
221 void DoShowDiffsForFiles(const wxArrayString& files, bool useFileAsBase = false);
222 void DoSetRepoPath(const wxString& repo_path = wxEmptyString);
223 void DoRecoverFromGitCommandError(bool clear_queue = true);
224 void DoLoadBlameInfo(bool clearCache);
225 void DoUpdateBlameInfo(const wxString& info, const wxString& fullpath);
226 void DoAnyFileModified();
227 DECLARE_EVENT_TABLE()
228
229 // Event handlers
230 void OnProgressTimer(wxTimerEvent& Event);
231 void OnProcessTerminated(clProcessEvent& event);
232 void OnProcessOutput(clProcessEvent& event);
233 void OnFileMenu(clContextMenuEvent& event);
234 void OnFolderMenu(clContextMenuEvent& event);
235
236 void OnFileCreated(clFileSystemEvent& event);
237 void OnReplaceInFiles(clFileSystemEvent& event);
238 void OnEditorChanged(wxCommandEvent& event);
239 void OnFileSaved(clCommandEvent& e);
240 void OnFileModifiedExternally(clFileSystemEvent& e);
241 void OnFilesAddedToProject(clCommandEvent& e);
242 void OnFilesRemovedFromProject(clCommandEvent& e);
243 void OnWorkspaceLoaded(clWorkspaceEvent& e);
244 void OnWorkspaceClosed(clWorkspaceEvent& e);
245 void OnWorkspaceConfigurationChanged(wxCommandEvent& e);
246 void OnMainFrameTitle(clCommandEvent& e);
247 void OnSetGitRepoPath(wxCommandEvent& e);
248 void OnSettings(wxCommandEvent& e);
249 void OnFileDiffSelected(wxCommandEvent& e);
250 void OnFileResetSelected(wxCommandEvent& e);
251 void OnFileAddSelected(wxCommandEvent& e);
252 void OnFileDeleteSelected(wxCommandEvent& e);
253 void OnSwitchLocalBranch(wxCommandEvent& e);
254 void OnSwitchRemoteBranch(wxCommandEvent& e);
255 void OnCreateBranch(wxCommandEvent& e);
256 void OnCommit(wxCommandEvent& e);
257 void OnCommitList(wxCommandEvent& e);
258 void OnShowDiffs(wxCommandEvent& e);
259 void OnApplyPatch(wxCommandEvent& e);
260 void OnPush(wxCommandEvent& e);
261 void OnRebase(wxCommandEvent& e);
262 void OnPull(wxCommandEvent& e);
263 void OnResetRepository(wxCommandEvent& e);
264 void OnStartGitk(wxCommandEvent& e);
265 void OnStartGitkUI(wxUpdateUIEvent& e);
266 void OnListModified(wxCommandEvent& e);
267 void OnGitBlame(wxCommandEvent& e);
268 void OnRefresh(wxCommandEvent& e);
269 void OnGarbageColletion(wxCommandEvent& e);
270 void OnOpenMSYSGit(wxCommandEvent& e);
271 void OnActiveProjectChanged(clProjectSettingsEvent& event);
272 void OnFileCommitListSelected(wxCommandEvent& e);
273 void OnFileGitBlame(wxCommandEvent& event);
274 void OnAppActivated(wxCommandEvent& event);
275 void OnUpdateNavBar(clCodeCompletionEvent& event);
276 void OnEditorClosed(wxCommandEvent& event);
277 void OnEnableGitRepoExists(wxUpdateUIEvent& e);
278 void OnClone(wxCommandEvent& e);
279 void OnSftpFileSaved(clCommandEvent& event);
280
281 // Event handlers from folder context menu
282 void OnFolderPullRebase(wxCommandEvent& event);
283 void OnFolderCommit(wxCommandEvent& event);
284 void OnFolderPush(wxCommandEvent& event);
285 void OnFolderStash(wxCommandEvent& event);
286 void OnFolderStashPop(wxCommandEvent& event);
287 void OnFolderGitBash(wxCommandEvent& event);
288
289 // Respond to local events
290 void OnGitActionDone(clSourceControlEvent& event);
291 bool HandleErrorsOnRemoteRepo(const wxString& output) const;
292
293 // Remote callbacks
294 void OnFindPath(clCommandEvent& event);
295
296 public:
297 GitPlugin(IManager* manager);
298 virtual ~GitPlugin();
299
300 const wxString& GetRepositoryPath() const { return m_repositoryDirectory; }
301 void WorkspaceClosed();
302
303 bool IsRemoteWorkspace() const { return m_isRemoteWorkspace; }
304
305 /**
306 * @brief create git process and return the process handle
307 */
308 IProcess* AsyncRunGit(wxEvtHandler* handler, const wxString& git_args, size_t create_flags,
309 const wxString& working_directory, bool logMessage = false);
310 /**
311 * @brief create a git process and direct the output to a callback
312 */
313 void AsyncRunGitWithCallback(const wxString& git_args, std::function<void(const wxString&)> callback,
314 size_t create_flags, const wxString& working_directory, bool logMessage = false);
315 /**
316 * @brief is git enabled for the current workspace?
317 */
318 bool IsGitEnabled() const;
319
320 /**
321 * @brief open a file. The path is relative to the workspace location
322 */
323 IEditor* OpenFile(const wxString& relativePathFile);
324
325 /**
326 * @brief fetch the next 100 commits (skip 'skip' first commits)
327 * and show them in the commit list dialog
328 * @param skip number of first commits to skip
329 */
330 void FetchNextCommits(int skip, const wxString& args);
331
332 GitConsole* GetConsole() { return m_console; }
333 IProcess* GetProcess() { return m_process; }
334 clCommandProcessor* GetFolderProcess() { return m_commandProcessor; }
335
336 IManager* GetManager() { return m_mgr; }
337
338 void ShowDiff(const wxArrayString& files) { DoShowDiffsForFiles(files); }
339
340 void ApplyPatch(const wxString& filename, const wxString& extraFlags);
341 void AddFiles(const wxArrayString& files) { DoAddFiles(files); }
342
343 void RevertCommit(const wxString& commitId);
344
345 void ResetFiles(const wxArrayString& files) { DoResetFiles(files); }
346
347 void UndoAddFiles(const wxArrayString& files);
348
349 void RefreshFileListView();
350 /**
351 * @brief display a message in the console
352 * used by outside classes e.g. GitDiffDlg
353 * @param message to display
354 */
355 void DisplayMessage(const wxString& message) const;
356
357 void DoGitBlame(const wxString& args); // Called by OnGitBlame or the git blame dialog
358 wxString GetEditorRelativeFilepath() const; // Called by OnGitBlame or the git blame dialog
359 void OnGitBlameRevList(const wxString& arg, const wxString& filepath,
360 const wxString& commit = ""); // Called by the git blame dialog
361
362 /**
363 * @brief simple git command executioin completed. Display its output etc
364 */
365 void OnCommandOutput(clCommandEvent& event);
366 void OnCommandEnded(clCommandEvent& event);
367
368 //--------------------------------------------
369 // Abstract methods
370 //--------------------------------------------
371 virtual void CreateToolBar(clToolBarGeneric* toolbar);
372 virtual void CreatePluginMenu(wxMenu* pluginsMenu);
373 virtual void HookPopupMenu(wxMenu* menu, MenuType type);
374 virtual void UnPlug();
375 };
376
377 #endif // git