Debian Codegraph @

plugins/vcs/GitModule.cpp

raw
1 #include "GitModule.h"
2
3 #include "i18n.h"
4 #include "igame.h"
5 #include "ui/imainframe.h"
6 #include "imap.h"
7 #include "ipreferencesystem.h"
8 #include "ui/istatusbarmanager.h"
9 #include "icommandsystem.h"
10 #include <git2.h>
11 #include "Repository.h"
12 #include "Commit.h"
13 #include "GitException.h"
14 #include "Remote.h"
15 #include "ui/VcsStatus.h"
16 #include "VersionControlLib.h"
17 #include "command/ExecutionFailure.h"
18
19 namespace vcs
20 {
21
22 std::string GitModule::getUriPrefix()
23 {
24 return UriPrefix;
25 }
26
27 ArchiveTextFilePtr GitModule::openTextFile(const std::string& vcsUri)
28 {
29 try
30 {
31 assert(getVcsPrefix(vcsUri) == UriPrefix);
32
33 if (!_repository)
34 {
35 return ArchiveTextFilePtr();
36 }
37
38 auto tree = _repository->getTreeByRevision(vcs::getVcsRevision(vcsUri));
39
40 return tree->openTextFile(vcs::getVcsFilePath(vcsUri), *_repository);
41 }
42 catch (const git::GitException& ex)
43 {
44 rWarning() << "git::openTextFile failed: " << ex.what() << std::endl;
45 return ArchiveTextFilePtr();
46 }
47 }
48
49 const std::string& GitModule::getName() const
50 {
51 static std::string _name("GitIntegration");
52 return _name;
53 }
54
55 const StringSet& GitModule::getDependencies() const
56 {
57 static StringSet _dependencies{ MODULE_MAINFRAME, MODULE_STATUSBARMANAGER,
58 MODULE_PREFERENCESYSTEM, MODULE_MAP, MODULE_VERSION_CONTROL_MANAGER };
59 return _dependencies;
60 }
61
62 void GitModule::initialiseModule(const IApplicationContext& ctx)
63 {
64 // Register commands
65 registerCommands();
66 createPreferencePage();
67
68 // Initialise libgit2 to have the memory handlers etc. set up
69 git_libgit2_init();
70
71 auto modPath = GlobalGameManager().getModPath();
72 _repository = std::make_unique<git::Repository>(modPath);
73
74 if (_repository->isOk())
75 {
76 rMessage() << "Opened repository at " << modPath << std::endl;
77 rMessage() << "Repository is currently on branch " << _repository->getCurrentBranchName() << std::endl;
78 }
79 else
80 {
81 _repository.reset();
82 }
83
84 GlobalMainFrame().signal_MainFrameConstructed().connect([&]()
85 {
86 _statusBarWidget = std::make_unique<ui::VcsStatus>(GlobalStatusBarManager().getStatusBar());
87 GlobalStatusBarManager().addElement(ui::VcsStatus::Name, _statusBarWidget->getWidget(),
88 ::ui::statusbar::StandardPosition::MapEditStopwatch + 10);
89
90 _statusBarWidget->setRepository(_repository);
91 });
92
93 GlobalVersionControlManager().registerModule(shared_from_this());
94 }
95
96 void GitModule::shutdownModule()
97 {
98 rMessage() << getName() << "::shutdownModule called." << std::endl;
99
100 GlobalVersionControlManager().unregisterModule(shared_from_this());
101
102 _statusBarWidget.reset();
103 _repository.reset();
104
105 git_libgit2_shutdown();
106 }
107
108 void GitModule::registerCommands()
109 {
110 GlobalCommandSystem().addCommand("GitFetch", std::bind(&GitModule::fetch, this, std::placeholders::_1));
111 }
112
113 void GitModule::fetch(const cmd::ArgumentList& args)
114 {
115 if (!_repository)
116 {
117 rWarning() << "Project is not under version control" << std::endl;
118 return;
119 }
120
121 _statusBarWidget->startFetchTask();
122 }
123
124 void GitModule::createPreferencePage()
125 {
126 auto& page = GlobalPreferenceSystem().getPage(_("Settings/Version Control"));
127
128 page.appendCheckBox(_("Enable Auto-Fetch"), RKEY_AUTO_FETCH_ENABLED);
129 page.appendSpinner(_("Fetch Interval (Minutes)"), RKEY_AUTO_FETCH_INTERVAL, 0.25, 900, 2);
130 }
131
132 }
133
134 /**
135 * greebo: This is the module entry point which the main binary will look for.
136 * The symbol RegisterModule is called with the singleton ModuleRegistry as argument.
137 */
138 extern "C" void DARKRADIANT_DLLEXPORT RegisterModule(IModuleRegistry& registry)
139 {
140 module::performDefaultInitialisation(registry);
141
142 registry.registerModule(std::make_shared<vcs::GitModule>());
143 }