version_control_editor_plugin.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /*************************************************************************/
  2. /* version_control_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "version_control_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/time.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/filesystem_dock.h"
  39. #include "editor/plugins/script_editor_plugin.h"
  40. #include "scene/gui/separator.h"
  41. #define CHECK_PLUGIN_INITIALIZED() \
  42. ERR_FAIL_COND_MSG(!EditorVCSInterface::get_singleton(), "No VCS plugin is initialized. Select a Version Control Plugin from Project menu.");
  43. VersionControlEditorPlugin *VersionControlEditorPlugin::singleton = nullptr;
  44. void VersionControlEditorPlugin::_bind_methods() {
  45. ClassDB::bind_method(D_METHOD("_initialize_vcs"), &VersionControlEditorPlugin::_initialize_vcs);
  46. ClassDB::bind_method(D_METHOD("_set_credentials"), &VersionControlEditorPlugin::_set_credentials);
  47. ClassDB::bind_method(D_METHOD("_update_set_up_warning"), &VersionControlEditorPlugin::_update_set_up_warning);
  48. ClassDB::bind_method(D_METHOD("_commit"), &VersionControlEditorPlugin::_commit);
  49. ClassDB::bind_method(D_METHOD("_refresh_stage_area"), &VersionControlEditorPlugin::_refresh_stage_area);
  50. ClassDB::bind_method(D_METHOD("_move_all"), &VersionControlEditorPlugin::_move_all);
  51. ClassDB::bind_method(D_METHOD("_load_diff"), &VersionControlEditorPlugin::_load_diff);
  52. ClassDB::bind_method(D_METHOD("_display_diff"), &VersionControlEditorPlugin::_display_diff);
  53. ClassDB::bind_method(D_METHOD("_item_activated"), &VersionControlEditorPlugin::_item_activated);
  54. ClassDB::bind_method(D_METHOD("_update_branch_create_button"), &VersionControlEditorPlugin::_update_branch_create_button);
  55. ClassDB::bind_method(D_METHOD("_update_remote_create_button"), &VersionControlEditorPlugin::_update_remote_create_button);
  56. ClassDB::bind_method(D_METHOD("_update_commit_button"), &VersionControlEditorPlugin::_update_commit_button);
  57. ClassDB::bind_method(D_METHOD("_refresh_branch_list"), &VersionControlEditorPlugin::_refresh_branch_list);
  58. ClassDB::bind_method(D_METHOD("_set_commit_list_size"), &VersionControlEditorPlugin::_set_commit_list_size);
  59. ClassDB::bind_method(D_METHOD("_refresh_commit_list"), &VersionControlEditorPlugin::_refresh_commit_list);
  60. ClassDB::bind_method(D_METHOD("_refresh_remote_list"), &VersionControlEditorPlugin::_refresh_remote_list);
  61. ClassDB::bind_method(D_METHOD("_ssh_public_key_selected"), &VersionControlEditorPlugin::_ssh_public_key_selected);
  62. ClassDB::bind_method(D_METHOD("_ssh_private_key_selected"), &VersionControlEditorPlugin::_ssh_private_key_selected);
  63. ClassDB::bind_method(D_METHOD("_commit_message_gui_input"), &VersionControlEditorPlugin::_commit_message_gui_input);
  64. ClassDB::bind_method(D_METHOD("_cell_button_pressed"), &VersionControlEditorPlugin::_cell_button_pressed);
  65. ClassDB::bind_method(D_METHOD("_discard_all"), &VersionControlEditorPlugin::_discard_all);
  66. ClassDB::bind_method(D_METHOD("_create_branch"), &VersionControlEditorPlugin::_create_branch);
  67. ClassDB::bind_method(D_METHOD("_create_remote"), &VersionControlEditorPlugin::_create_remote);
  68. ClassDB::bind_method(D_METHOD("_remove_branch"), &VersionControlEditorPlugin::_remove_branch);
  69. ClassDB::bind_method(D_METHOD("_remove_remote"), &VersionControlEditorPlugin::_remove_remote);
  70. ClassDB::bind_method(D_METHOD("_branch_item_selected"), &VersionControlEditorPlugin::_branch_item_selected);
  71. ClassDB::bind_method(D_METHOD("_remote_selected"), &VersionControlEditorPlugin::_remote_selected);
  72. ClassDB::bind_method(D_METHOD("_fetch"), &VersionControlEditorPlugin::_fetch);
  73. ClassDB::bind_method(D_METHOD("_pull"), &VersionControlEditorPlugin::_pull);
  74. ClassDB::bind_method(D_METHOD("_push"), &VersionControlEditorPlugin::_push);
  75. ClassDB::bind_method(D_METHOD("_extra_option_selected"), &VersionControlEditorPlugin::_extra_option_selected);
  76. ClassDB::bind_method(D_METHOD("_update_extra_options"), &VersionControlEditorPlugin::_update_extra_options);
  77. ClassDB::bind_method(D_METHOD("_popup_branch_remove_confirm"), &VersionControlEditorPlugin::_popup_branch_remove_confirm);
  78. ClassDB::bind_method(D_METHOD("_popup_remote_remove_confirm"), &VersionControlEditorPlugin::_popup_remote_remove_confirm);
  79. ClassDB::bind_method(D_METHOD("_popup_file_dialog"), &VersionControlEditorPlugin::_popup_file_dialog);
  80. ClassDB::bind_method(D_METHOD("popup_vcs_set_up_dialog"), &VersionControlEditorPlugin::popup_vcs_set_up_dialog);
  81. }
  82. void VersionControlEditorPlugin::_create_vcs_metadata_files() {
  83. String dir = "res://";
  84. EditorVCSInterface::create_vcs_metadata_files(EditorVCSInterface::VCSMetadata(metadata_selection->get_selected()), dir);
  85. }
  86. void VersionControlEditorPlugin::_notification(int p_what) {
  87. if (p_what == NOTIFICATION_READY) {
  88. String installed_plugin = GLOBAL_DEF("editor/version_control/plugin_name", "");
  89. String project_path = GLOBAL_DEF("editor/version_control/project_path", OS::get_singleton()->get_resource_dir());
  90. project_path_input->set_text(project_path);
  91. bool has_autoload_enable = GLOBAL_DEF("editor/version_control/autoload_on_startup", false);
  92. if (installed_plugin != "" && has_autoload_enable) {
  93. if (_load_plugin(installed_plugin, project_path)) {
  94. _set_credentials();
  95. }
  96. }
  97. }
  98. }
  99. void VersionControlEditorPlugin::_populate_available_vcs_names() {
  100. set_up_choice->clear();
  101. for (int i = 0; i < available_plugins.size(); i++) {
  102. set_up_choice->add_item(available_plugins[i]);
  103. }
  104. }
  105. VersionControlEditorPlugin *VersionControlEditorPlugin::get_singleton() {
  106. return singleton ? singleton : memnew(VersionControlEditorPlugin);
  107. }
  108. void VersionControlEditorPlugin::popup_vcs_metadata_dialog() {
  109. metadata_dialog->popup_centered();
  110. }
  111. void VersionControlEditorPlugin::popup_vcs_set_up_dialog(const Control *p_gui_base) {
  112. fetch_available_vcs_plugin_names();
  113. if (!available_plugins.is_empty()) {
  114. Size2 popup_size = Size2(400, 100);
  115. Size2 window_size = p_gui_base->get_viewport_rect().size;
  116. popup_size.x = MIN(window_size.x * 0.5, popup_size.x);
  117. popup_size.y = MIN(window_size.y * 0.5, popup_size.y);
  118. _populate_available_vcs_names();
  119. set_up_dialog->popup_centered_clamped(popup_size * EDSCALE);
  120. } else {
  121. // TODO: Give info to user on how to fix this error.
  122. EditorNode::get_singleton()->show_warning(TTR("No VCS plugins are available in the project. Install a VCS plugin to use VCS integration features."), TTR("Error"));
  123. }
  124. }
  125. void VersionControlEditorPlugin::_initialize_vcs() {
  126. ERR_FAIL_COND_MSG(EditorVCSInterface::get_singleton(), EditorVCSInterface::get_singleton()->get_vcs_name() + " is already active.");
  127. const int id = set_up_choice->get_selected_id();
  128. String selected_plugin = set_up_choice->get_item_text(id);
  129. if (_load_plugin(selected_plugin, project_path_input->get_text())) {
  130. ProjectSettings::get_singleton()->set("editor/version_control/autoload_on_startup", true);
  131. ProjectSettings::get_singleton()->set("editor/version_control/plugin_name", selected_plugin);
  132. ProjectSettings::get_singleton()->set("editor/version_control/project_path", project_path_input->get_text());
  133. ProjectSettings::get_singleton()->save();
  134. }
  135. }
  136. void VersionControlEditorPlugin::_set_vcs_ui_state(bool p_enabled) {
  137. select_project_path_button->set_disabled(p_enabled);
  138. set_up_dialog->get_ok_button()->set_disabled(!p_enabled);
  139. project_path_input->set_editable(!p_enabled);
  140. set_up_choice->set_disabled(p_enabled);
  141. toggle_vcs_choice->set_pressed_no_signal(p_enabled);
  142. }
  143. void VersionControlEditorPlugin::_set_credentials() {
  144. CHECK_PLUGIN_INITIALIZED();
  145. String username = set_up_username->get_text();
  146. String password = set_up_password->get_text();
  147. String ssh_public_key = set_up_ssh_public_key_path->get_text();
  148. String ssh_private_key = set_up_ssh_private_key_path->get_text();
  149. String ssh_passphrase = set_up_ssh_passphrase->get_text();
  150. EditorVCSInterface::get_singleton()->set_credentials(
  151. username,
  152. password,
  153. ssh_public_key,
  154. ssh_private_key,
  155. ssh_passphrase);
  156. EditorSettings::get_singleton()->set_setting("version_control/username", username);
  157. EditorSettings::get_singleton()->set_setting("version_control/ssh_public_key_path", ssh_public_key);
  158. EditorSettings::get_singleton()->set_setting("version_control/ssh_private_key_path", ssh_private_key);
  159. }
  160. bool VersionControlEditorPlugin::_load_plugin(String p_name, String p_project_path) {
  161. Object *extension_instance = ClassDB::instantiate(p_name);
  162. ERR_FAIL_NULL_V_MSG(extension_instance, false, "Received a nullptr VCS extension instance during construction.");
  163. EditorVCSInterface *vcs_plugin = Object::cast_to<EditorVCSInterface>(extension_instance);
  164. ERR_FAIL_NULL_V_MSG(vcs_plugin, false, vformat("Could not cast VCS extension instance to %s.", EditorVCSInterface::get_class_static()));
  165. String res_dir = project_path_input->get_text();
  166. ERR_FAIL_COND_V_MSG(!vcs_plugin->initialize(res_dir), false, "Could not initialize " + p_name);
  167. EditorVCSInterface::set_singleton(vcs_plugin);
  168. register_editor();
  169. EditorFileSystem::get_singleton()->connect(SNAME("filesystem_changed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  170. _refresh_stage_area();
  171. _refresh_commit_list();
  172. _refresh_branch_list();
  173. _refresh_remote_list();
  174. return true;
  175. }
  176. void VersionControlEditorPlugin::_update_set_up_warning(String p_new_text) {
  177. bool empty_settings = set_up_username->get_text().strip_edges().is_empty() &&
  178. set_up_password->get_text().is_empty() &&
  179. set_up_ssh_public_key_path->get_text().strip_edges().is_empty() &&
  180. set_up_ssh_private_key_path->get_text().strip_edges().is_empty() &&
  181. set_up_ssh_passphrase->get_text().is_empty();
  182. if (empty_settings) {
  183. set_up_warning_text->add_theme_color_override(SNAME("font_color"), EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  184. set_up_warning_text->set_text(TTR("Remote settings are empty. VCS features that use the network may not work."));
  185. } else {
  186. set_up_warning_text->set_text("");
  187. }
  188. }
  189. void VersionControlEditorPlugin::_refresh_branch_list() {
  190. CHECK_PLUGIN_INITIALIZED();
  191. List<String> branch_list = EditorVCSInterface::get_singleton()->get_branch_list();
  192. branch_select->clear();
  193. branch_select->set_disabled(branch_list.is_empty());
  194. String current_branch = EditorVCSInterface::get_singleton()->get_current_branch_name();
  195. for (int i = 0; i < branch_list.size(); i++) {
  196. branch_select->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("VcsBranches"), SNAME("EditorIcons")), branch_list[i], i);
  197. if (branch_list[i] == current_branch) {
  198. branch_select->select(i);
  199. }
  200. }
  201. }
  202. String VersionControlEditorPlugin::_get_date_string_from(int64_t p_unix_timestamp, int64_t p_offset_minutes) const {
  203. return vformat(
  204. "%s %s",
  205. Time::get_singleton()->get_datetime_string_from_unix_time(p_unix_timestamp + p_offset_minutes * 60, true),
  206. Time::get_singleton()->get_offset_string_from_offset_minutes(p_offset_minutes));
  207. }
  208. void VersionControlEditorPlugin::_set_commit_list_size(int p_index) {
  209. _refresh_commit_list();
  210. }
  211. void VersionControlEditorPlugin::_refresh_commit_list() {
  212. CHECK_PLUGIN_INITIALIZED();
  213. commit_list->get_root()->clear_children();
  214. List<EditorVCSInterface::Commit> commit_info_list = EditorVCSInterface::get_singleton()->get_previous_commits(commit_list_size_button->get_selected_metadata());
  215. for (List<EditorVCSInterface::Commit>::Element *e = commit_info_list.front(); e; e = e->next()) {
  216. EditorVCSInterface::Commit commit = e->get();
  217. TreeItem *item = commit_list->create_item();
  218. // Only display the first line of a commit message
  219. int line_ending = commit.msg.find_char('\n');
  220. String commit_display_msg = commit.msg.substr(0, line_ending);
  221. String commit_date_string = _get_date_string_from(commit.unix_timestamp, commit.offset_minutes);
  222. Dictionary meta_data;
  223. meta_data[SNAME("commit_id")] = commit.id;
  224. meta_data[SNAME("commit_title")] = commit_display_msg;
  225. meta_data[SNAME("commit_subtitle")] = commit.msg.substr(line_ending).strip_edges();
  226. meta_data[SNAME("commit_unix_timestamp")] = commit.unix_timestamp;
  227. meta_data[SNAME("commit_author")] = commit.author;
  228. meta_data[SNAME("commit_date_string")] = commit_date_string;
  229. item->set_text(0, commit_display_msg);
  230. item->set_text(1, commit.author.strip_edges());
  231. item->set_metadata(0, meta_data);
  232. }
  233. }
  234. void VersionControlEditorPlugin::_refresh_remote_list() {
  235. CHECK_PLUGIN_INITIALIZED();
  236. List<String> remotes = EditorVCSInterface::get_singleton()->get_remotes();
  237. String current_remote = remote_select->get_selected_metadata();
  238. remote_select->clear();
  239. remote_select->set_disabled(remotes.is_empty());
  240. for (int i = 0; i < remotes.size(); i++) {
  241. remote_select->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("ArrowUp"), SNAME("EditorIcons")), remotes[i], i);
  242. remote_select->set_item_metadata(i, remotes[i]);
  243. if (remotes[i] == current_remote) {
  244. remote_select->select(i);
  245. }
  246. }
  247. }
  248. void VersionControlEditorPlugin::_commit() {
  249. CHECK_PLUGIN_INITIALIZED();
  250. String msg = commit_message->get_text().strip_edges();
  251. ERR_FAIL_COND_MSG(msg.is_empty(), "No commit message was provided.");
  252. EditorVCSInterface::get_singleton()->commit(msg);
  253. version_control_dock_button->set_pressed(false);
  254. commit_message->release_focus();
  255. commit_button->release_focus();
  256. commit_message->set_text("");
  257. _refresh_stage_area();
  258. _refresh_commit_list();
  259. _refresh_branch_list();
  260. _clear_diff();
  261. }
  262. void VersionControlEditorPlugin::_branch_item_selected(int p_index) {
  263. CHECK_PLUGIN_INITIALIZED();
  264. String branch_name = branch_select->get_item_text(p_index);
  265. EditorVCSInterface::get_singleton()->checkout_branch(branch_name);
  266. EditorFileSystem::get_singleton()->scan_changes();
  267. ScriptEditor::get_singleton()->reload_scripts();
  268. _refresh_branch_list();
  269. _refresh_commit_list();
  270. _refresh_stage_area();
  271. _clear_diff();
  272. _update_opened_tabs();
  273. }
  274. void VersionControlEditorPlugin::_remote_selected(int p_index) {
  275. _refresh_remote_list();
  276. }
  277. void VersionControlEditorPlugin::_ssh_public_key_selected(String p_path) {
  278. set_up_ssh_public_key_path->set_text(p_path);
  279. }
  280. void VersionControlEditorPlugin::_ssh_private_key_selected(String p_path) {
  281. set_up_ssh_private_key_path->set_text(p_path);
  282. }
  283. void VersionControlEditorPlugin::_popup_file_dialog(Variant p_file_dialog_variant) {
  284. FileDialog *file_dialog = Object::cast_to<FileDialog>(p_file_dialog_variant);
  285. ERR_FAIL_NULL(file_dialog);
  286. file_dialog->popup_centered_ratio();
  287. }
  288. void VersionControlEditorPlugin::_create_branch() {
  289. CHECK_PLUGIN_INITIALIZED();
  290. String new_branch_name = branch_create_name_input->get_text().strip_edges();
  291. EditorVCSInterface::get_singleton()->create_branch(new_branch_name);
  292. EditorVCSInterface::get_singleton()->checkout_branch(new_branch_name);
  293. branch_create_name_input->clear();
  294. _refresh_branch_list();
  295. }
  296. void VersionControlEditorPlugin::_create_remote() {
  297. CHECK_PLUGIN_INITIALIZED();
  298. String new_remote_name = remote_create_name_input->get_text().strip_edges();
  299. String new_remote_url = remote_create_url_input->get_text().strip_edges();
  300. EditorVCSInterface::get_singleton()->create_remote(new_remote_name, new_remote_url);
  301. remote_create_name_input->clear();
  302. remote_create_url_input->clear();
  303. _refresh_remote_list();
  304. }
  305. void VersionControlEditorPlugin::_update_branch_create_button(String p_new_text) {
  306. branch_create_ok->set_disabled(p_new_text.strip_edges().is_empty());
  307. }
  308. void VersionControlEditorPlugin::_update_remote_create_button(String p_new_text) {
  309. remote_create_ok->set_disabled(p_new_text.strip_edges().is_empty());
  310. }
  311. int VersionControlEditorPlugin::_get_item_count(Tree *p_tree) {
  312. if (!p_tree->get_root()) {
  313. return 0;
  314. }
  315. return p_tree->get_root()->get_children().size();
  316. }
  317. void VersionControlEditorPlugin::_refresh_stage_area() {
  318. CHECK_PLUGIN_INITIALIZED();
  319. staged_files->get_root()->clear_children();
  320. unstaged_files->get_root()->clear_children();
  321. List<EditorVCSInterface::StatusFile> status_files = EditorVCSInterface::get_singleton()->get_modified_files_data();
  322. for (List<EditorVCSInterface::StatusFile>::Element *E = status_files.front(); E; E = E->next()) {
  323. EditorVCSInterface::StatusFile sf = E->get();
  324. if (sf.area == EditorVCSInterface::TREE_AREA_STAGED) {
  325. _add_new_item(staged_files, sf.file_path, sf.change_type);
  326. } else if (sf.area == EditorVCSInterface::TREE_AREA_UNSTAGED) {
  327. _add_new_item(unstaged_files, sf.file_path, sf.change_type);
  328. }
  329. }
  330. staged_files->queue_redraw();
  331. unstaged_files->queue_redraw();
  332. int total_changes = status_files.size();
  333. String commit_tab_title = TTR("Commit") + (total_changes > 0 ? " (" + itos(total_changes) + ")" : "");
  334. version_commit_dock->set_name(commit_tab_title);
  335. }
  336. void VersionControlEditorPlugin::_discard_file(String p_file_path, EditorVCSInterface::ChangeType p_change) {
  337. CHECK_PLUGIN_INITIALIZED();
  338. if (p_change == EditorVCSInterface::CHANGE_TYPE_NEW) {
  339. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  340. dir->remove(p_file_path);
  341. } else {
  342. CHECK_PLUGIN_INITIALIZED();
  343. EditorVCSInterface::get_singleton()->discard_file(p_file_path);
  344. }
  345. // FIXIT: The project.godot file shows weird behavior
  346. EditorFileSystem::get_singleton()->update_file(p_file_path);
  347. }
  348. void VersionControlEditorPlugin::_discard_all() {
  349. TreeItem *file_entry = unstaged_files->get_root()->get_first_child();
  350. while (file_entry) {
  351. String file_path = file_entry->get_meta(SNAME("file_path"));
  352. EditorVCSInterface::ChangeType change = (EditorVCSInterface::ChangeType)(int)file_entry->get_meta(SNAME("change_type"));
  353. _discard_file(file_path, change);
  354. file_entry = file_entry->get_next();
  355. }
  356. _refresh_stage_area();
  357. }
  358. void VersionControlEditorPlugin::_add_new_item(Tree *p_tree, String p_file_path, EditorVCSInterface::ChangeType p_change) {
  359. String change_text = p_file_path + " (" + change_type_to_strings[p_change] + ")";
  360. TreeItem *new_item = p_tree->create_item();
  361. new_item->set_text(0, change_text);
  362. new_item->set_icon(0, change_type_to_icon[p_change]);
  363. new_item->set_meta(SNAME("file_path"), p_file_path);
  364. new_item->set_meta(SNAME("change_type"), p_change);
  365. new_item->set_custom_color(0, change_type_to_color[p_change]);
  366. new_item->add_button(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("File"), SNAME("EditorIcons")), BUTTON_TYPE_OPEN, false, TTR("Open in editor"));
  367. if (p_tree == unstaged_files) {
  368. new_item->add_button(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")), BUTTON_TYPE_DISCARD, false, TTR("Discard changes"));
  369. }
  370. }
  371. void VersionControlEditorPlugin::_fetch() {
  372. CHECK_PLUGIN_INITIALIZED();
  373. EditorVCSInterface::get_singleton()->fetch(remote_select->get_selected_metadata());
  374. _refresh_branch_list();
  375. }
  376. void VersionControlEditorPlugin::_pull() {
  377. CHECK_PLUGIN_INITIALIZED();
  378. EditorVCSInterface::get_singleton()->pull(remote_select->get_selected_metadata());
  379. _refresh_stage_area();
  380. _refresh_branch_list();
  381. _refresh_commit_list();
  382. _clear_diff();
  383. _update_opened_tabs();
  384. }
  385. void VersionControlEditorPlugin::_push() {
  386. CHECK_PLUGIN_INITIALIZED();
  387. EditorVCSInterface::get_singleton()->push(remote_select->get_selected_metadata(), false);
  388. }
  389. void VersionControlEditorPlugin::_force_push() {
  390. CHECK_PLUGIN_INITIALIZED();
  391. EditorVCSInterface::get_singleton()->push(remote_select->get_selected_metadata(), true);
  392. }
  393. void VersionControlEditorPlugin::_update_opened_tabs() {
  394. Vector<EditorData::EditedScene> open_scenes = EditorNode::get_singleton()->get_editor_data().get_edited_scenes();
  395. for (int i = 0; i < open_scenes.size(); i++) {
  396. if (open_scenes[i].root == NULL) {
  397. continue;
  398. }
  399. EditorNode::get_singleton()->reload_scene(open_scenes[i].path);
  400. }
  401. }
  402. void VersionControlEditorPlugin::_move_all(Object *p_tree) {
  403. Tree *tree = Object::cast_to<Tree>(p_tree);
  404. TreeItem *file_entry = tree->get_root()->get_first_child();
  405. while (file_entry) {
  406. _move_item(tree, file_entry);
  407. file_entry = file_entry->get_next();
  408. }
  409. _refresh_stage_area();
  410. }
  411. void VersionControlEditorPlugin::_load_diff(Object *p_tree) {
  412. CHECK_PLUGIN_INITIALIZED();
  413. version_control_dock_button->set_pressed(true);
  414. Tree *tree = Object::cast_to<Tree>(p_tree);
  415. if (tree == staged_files) {
  416. show_commit_diff_header = false;
  417. String file_path = tree->get_selected()->get_meta(SNAME("file_path"));
  418. diff_title->set_text(TTR("Staged Changes"));
  419. diff_content = EditorVCSInterface::get_singleton()->get_diff(file_path, EditorVCSInterface::TREE_AREA_STAGED);
  420. } else if (tree == unstaged_files) {
  421. show_commit_diff_header = false;
  422. String file_path = tree->get_selected()->get_meta(SNAME("file_path"));
  423. diff_title->set_text(TTR("Unstaged Changes"));
  424. diff_content = EditorVCSInterface::get_singleton()->get_diff(file_path, EditorVCSInterface::TREE_AREA_UNSTAGED);
  425. } else if (tree == commit_list) {
  426. show_commit_diff_header = true;
  427. Dictionary meta_data = tree->get_selected()->get_metadata(0);
  428. String commit_id = meta_data[SNAME("commit_id")];
  429. String commit_title = meta_data[SNAME("commit_title")];
  430. diff_title->set_text(commit_title);
  431. diff_content = EditorVCSInterface::get_singleton()->get_diff(commit_id, EditorVCSInterface::TREE_AREA_COMMIT);
  432. }
  433. _display_diff(0);
  434. }
  435. void VersionControlEditorPlugin::_clear_diff() {
  436. diff->clear();
  437. diff_content.clear();
  438. diff_title->set_text("");
  439. }
  440. void VersionControlEditorPlugin::_item_activated(Object *p_tree) {
  441. Tree *tree = Object::cast_to<Tree>(p_tree);
  442. _move_item(tree, tree->get_selected());
  443. _refresh_stage_area();
  444. }
  445. void VersionControlEditorPlugin::_move_item(Tree *p_tree, TreeItem *p_item) {
  446. CHECK_PLUGIN_INITIALIZED();
  447. if (p_tree == staged_files) {
  448. EditorVCSInterface::get_singleton()->unstage_file(p_item->get_meta(SNAME("file_path")));
  449. } else {
  450. EditorVCSInterface::get_singleton()->stage_file(p_item->get_meta(SNAME("file_path")));
  451. }
  452. }
  453. void VersionControlEditorPlugin::_cell_button_pressed(Object *p_item, int p_column, int p_id, int p_mouse_button_index) {
  454. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  455. String file_path = item->get_meta(SNAME("file_path"));
  456. EditorVCSInterface::ChangeType change = (EditorVCSInterface::ChangeType)(int)item->get_meta(SNAME("change_type"));
  457. if (p_id == BUTTON_TYPE_OPEN && change != EditorVCSInterface::CHANGE_TYPE_DELETED) {
  458. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  459. if (!dir->file_exists(file_path)) {
  460. return;
  461. }
  462. file_path = "res://" + file_path;
  463. if (ResourceLoader::get_resource_type(file_path) == "PackedScene") {
  464. EditorNode::get_singleton()->open_request(file_path);
  465. } else if (file_path.ends_with(".gd")) {
  466. EditorNode::get_singleton()->load_resource(file_path);
  467. ScriptEditor::get_singleton()->reload_scripts();
  468. } else {
  469. FileSystemDock::get_singleton()->navigate_to_path(file_path);
  470. }
  471. } else if (p_id == BUTTON_TYPE_DISCARD) {
  472. _discard_file(file_path, change);
  473. _refresh_stage_area();
  474. }
  475. }
  476. void VersionControlEditorPlugin::_display_diff(int p_idx) {
  477. DiffViewType diff_view = (DiffViewType)diff_view_type_select->get_selected();
  478. diff->clear();
  479. if (show_commit_diff_header) {
  480. Dictionary meta_data = commit_list->get_selected()->get_metadata(0);
  481. String commit_id = meta_data[SNAME("commit_id")];
  482. String commit_subtitle = meta_data[SNAME("commit_subtitle")];
  483. String commit_date = meta_data[SNAME("commit_date")];
  484. String commit_author = meta_data[SNAME("commit_author")];
  485. String commit_date_string = meta_data[SNAME("commit_date_string")];
  486. diff->push_font(EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts")));
  487. diff->push_color(EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("accent_color"), SNAME("Editor")));
  488. diff->add_text(TTR("Commit:") + " " + commit_id);
  489. diff->add_newline();
  490. diff->add_text(TTR("Author:") + " " + commit_author);
  491. diff->add_newline();
  492. diff->add_text(TTR("Date:") + " " + commit_date_string);
  493. diff->add_newline();
  494. if (!commit_subtitle.is_empty()) {
  495. diff->add_text(TTR("Subtitle:") + " " + commit_subtitle);
  496. diff->add_newline();
  497. }
  498. diff->add_newline();
  499. diff->pop();
  500. diff->pop();
  501. }
  502. for (int i = 0; i < diff_content.size(); i++) {
  503. EditorVCSInterface::DiffFile diff_file = diff_content[i];
  504. diff->push_font(EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts")));
  505. diff->push_color(EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("accent_color"), SNAME("Editor")));
  506. diff->add_text(TTR("File:") + " " + diff_file.new_file);
  507. diff->pop();
  508. diff->pop();
  509. diff->add_newline();
  510. diff->push_font(EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("status_source"), SNAME("EditorFonts")));
  511. for (int j = 0; j < diff_file.diff_hunks.size(); j++) {
  512. EditorVCSInterface::DiffHunk hunk = diff_file.diff_hunks[j];
  513. String old_start = String::num_int64(hunk.old_start);
  514. String new_start = String::num_int64(hunk.new_start);
  515. String old_lines = String::num_int64(hunk.old_lines);
  516. String new_lines = String::num_int64(hunk.new_lines);
  517. diff->append_text("[center]@@ " + old_start + "," + old_lines + " " + new_start + "," + new_lines + " @@[/center]");
  518. diff->add_newline();
  519. switch (diff_view) {
  520. case DIFF_VIEW_TYPE_SPLIT:
  521. _display_diff_split_view(hunk.diff_lines);
  522. break;
  523. case DIFF_VIEW_TYPE_UNIFIED:
  524. _display_diff_unified_view(hunk.diff_lines);
  525. break;
  526. }
  527. diff->add_newline();
  528. diff->add_newline();
  529. }
  530. diff->pop();
  531. diff->add_newline();
  532. }
  533. }
  534. void VersionControlEditorPlugin::_display_diff_split_view(List<EditorVCSInterface::DiffLine> &p_diff_content) {
  535. List<EditorVCSInterface::DiffLine> parsed_diff;
  536. for (int i = 0; i < p_diff_content.size(); i++) {
  537. EditorVCSInterface::DiffLine diff_line = p_diff_content[i];
  538. String line = diff_line.content.strip_edges(false, true);
  539. if (diff_line.new_line_no >= 0 && diff_line.old_line_no >= 0) {
  540. diff_line.new_text = line;
  541. diff_line.old_text = line;
  542. parsed_diff.push_back(diff_line);
  543. } else if (diff_line.new_line_no == -1) {
  544. diff_line.new_text = "";
  545. diff_line.old_text = line;
  546. parsed_diff.push_back(diff_line);
  547. } else if (diff_line.old_line_no == -1) {
  548. int j = parsed_diff.size() - 1;
  549. while (j >= 0 && parsed_diff[j].new_line_no == -1) {
  550. j--;
  551. }
  552. if (j == parsed_diff.size() - 1) {
  553. // no lines are modified
  554. diff_line.new_text = line;
  555. diff_line.old_text = "";
  556. parsed_diff.push_back(diff_line);
  557. } else {
  558. // lines are modified
  559. EditorVCSInterface::DiffLine modified_line = parsed_diff[j + 1];
  560. modified_line.new_text = line;
  561. modified_line.new_line_no = diff_line.new_line_no;
  562. parsed_diff[j + 1] = modified_line;
  563. }
  564. }
  565. }
  566. diff->push_table(6);
  567. /*
  568. [cell]Old Line No[/cell]
  569. [cell]prefix[/cell]
  570. [cell]Old Code[/cell]
  571. [cell]New Line No[/cell]
  572. [cell]prefix[/cell]
  573. [cell]New Line[/cell]
  574. */
  575. diff->set_table_column_expand(2, true);
  576. diff->set_table_column_expand(5, true);
  577. for (int i = 0; i < parsed_diff.size(); i++) {
  578. EditorVCSInterface::DiffLine diff_line = parsed_diff[i];
  579. bool has_change = diff_line.status != " ";
  580. static const Color red = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor"));
  581. static const Color green = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor"));
  582. static const Color white = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Label")) * Color(1, 1, 1, 0.6);
  583. if (diff_line.old_line_no >= 0) {
  584. diff->push_cell();
  585. diff->push_indent(1);
  586. diff->push_color(has_change ? red : white);
  587. diff->add_text(String::num_int64(diff_line.old_line_no));
  588. diff->pop();
  589. diff->pop();
  590. diff->pop();
  591. diff->push_cell();
  592. diff->push_color(has_change ? red : white);
  593. diff->add_text(has_change ? "-|" : " |");
  594. diff->pop();
  595. diff->pop();
  596. diff->push_cell();
  597. diff->push_color(has_change ? red : white);
  598. diff->add_text(diff_line.old_text);
  599. diff->pop();
  600. diff->pop();
  601. } else {
  602. diff->push_cell();
  603. diff->pop();
  604. diff->push_cell();
  605. diff->pop();
  606. diff->push_cell();
  607. diff->pop();
  608. }
  609. if (diff_line.new_line_no >= 0) {
  610. diff->push_cell();
  611. diff->push_indent(1);
  612. diff->push_color(has_change ? green : white);
  613. diff->add_text(String::num_int64(diff_line.new_line_no));
  614. diff->pop();
  615. diff->pop();
  616. diff->pop();
  617. diff->push_cell();
  618. diff->push_color(has_change ? green : white);
  619. diff->add_text(has_change ? "+|" : " |");
  620. diff->pop();
  621. diff->pop();
  622. diff->push_cell();
  623. diff->push_color(has_change ? green : white);
  624. diff->add_text(diff_line.new_text);
  625. diff->pop();
  626. diff->pop();
  627. } else {
  628. diff->push_cell();
  629. diff->pop();
  630. diff->push_cell();
  631. diff->pop();
  632. diff->push_cell();
  633. diff->pop();
  634. }
  635. }
  636. diff->pop();
  637. }
  638. void VersionControlEditorPlugin::_display_diff_unified_view(List<EditorVCSInterface::DiffLine> &p_diff_content) {
  639. diff->push_table(4);
  640. diff->set_table_column_expand(3, true);
  641. /*
  642. [cell]Old Line No[/cell]
  643. [cell]New Line No[/cell]
  644. [cell]status[/cell]
  645. [cell]code[/cell]
  646. */
  647. for (int i = 0; i < p_diff_content.size(); i++) {
  648. EditorVCSInterface::DiffLine diff_line = p_diff_content[i];
  649. String line = diff_line.content.strip_edges(false, true);
  650. Color color;
  651. if (diff_line.status == "+") {
  652. color = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor"));
  653. } else if (diff_line.status == "-") {
  654. color = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor"));
  655. } else {
  656. color = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Label"));
  657. color *= Color(1, 1, 1, 0.6);
  658. }
  659. diff->push_cell();
  660. diff->push_color(color);
  661. diff->push_indent(1);
  662. diff->add_text(diff_line.old_line_no >= 0 ? String::num_int64(diff_line.old_line_no) : "");
  663. diff->pop();
  664. diff->pop();
  665. diff->pop();
  666. diff->push_cell();
  667. diff->push_color(color);
  668. diff->push_indent(1);
  669. diff->add_text(diff_line.new_line_no >= 0 ? String::num_int64(diff_line.new_line_no) : "");
  670. diff->pop();
  671. diff->pop();
  672. diff->pop();
  673. diff->push_cell();
  674. diff->push_color(color);
  675. diff->add_text(diff_line.status != "" ? diff_line.status + "|" : " |");
  676. diff->pop();
  677. diff->pop();
  678. diff->push_cell();
  679. diff->push_color(color);
  680. diff->add_text(line);
  681. diff->pop();
  682. diff->pop();
  683. }
  684. diff->pop();
  685. }
  686. void VersionControlEditorPlugin::_update_commit_button() {
  687. commit_button->set_disabled(commit_message->get_text().strip_edges().is_empty());
  688. }
  689. void VersionControlEditorPlugin::_remove_branch() {
  690. CHECK_PLUGIN_INITIALIZED();
  691. EditorVCSInterface::get_singleton()->remove_branch(branch_to_remove);
  692. branch_to_remove.clear();
  693. _refresh_branch_list();
  694. }
  695. void VersionControlEditorPlugin::_remove_remote() {
  696. CHECK_PLUGIN_INITIALIZED();
  697. EditorVCSInterface::get_singleton()->remove_remote(remote_to_remove);
  698. remote_to_remove.clear();
  699. _refresh_remote_list();
  700. }
  701. void VersionControlEditorPlugin::_extra_option_selected(int p_index) {
  702. CHECK_PLUGIN_INITIALIZED();
  703. switch ((ExtraOption)p_index) {
  704. case EXTRA_OPTION_FORCE_PUSH:
  705. _force_push();
  706. break;
  707. case EXTRA_OPTION_CREATE_BRANCH:
  708. branch_create_confirm->popup_centered();
  709. break;
  710. case EXTRA_OPTION_CREATE_REMOTE:
  711. remote_create_confirm->popup_centered();
  712. break;
  713. }
  714. }
  715. void VersionControlEditorPlugin::_popup_branch_remove_confirm(int p_index) {
  716. branch_to_remove = extra_options_remove_branch_list->get_item_text(p_index);
  717. branch_remove_confirm->set_text(vformat(TTR("Do you want to remove the %s branch?"), branch_to_remove));
  718. branch_remove_confirm->popup_centered();
  719. }
  720. void VersionControlEditorPlugin::_popup_remote_remove_confirm(int p_index) {
  721. remote_to_remove = extra_options_remove_remote_list->get_item_text(p_index);
  722. remote_remove_confirm->set_text(vformat(TTR("Do you want to remove the %s remote?"), branch_to_remove));
  723. remote_remove_confirm->popup_centered();
  724. }
  725. void VersionControlEditorPlugin::_update_extra_options() {
  726. extra_options_remove_branch_list->clear();
  727. for (int i = 0; i < branch_select->get_item_count(); i++) {
  728. extra_options_remove_branch_list->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("VcsBranches"), SNAME("EditorIcons")), branch_select->get_item_text(branch_select->get_item_id(i)));
  729. }
  730. extra_options_remove_branch_list->update_canvas_items();
  731. extra_options_remove_remote_list->clear();
  732. for (int i = 0; i < remote_select->get_item_count(); i++) {
  733. extra_options_remove_remote_list->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("ArrowUp"), SNAME("EditorIcons")), remote_select->get_item_text(remote_select->get_item_id(i)));
  734. }
  735. extra_options_remove_remote_list->update_canvas_items();
  736. }
  737. bool VersionControlEditorPlugin::_is_staging_area_empty() {
  738. return staged_files->get_root()->get_child_count() == 0;
  739. }
  740. void VersionControlEditorPlugin::_commit_message_gui_input(const Ref<InputEvent> &p_event) {
  741. if (!commit_message->has_focus()) {
  742. return;
  743. }
  744. if (commit_message->get_text().strip_edges().is_empty()) {
  745. // Do not allow empty commit messages.
  746. return;
  747. }
  748. const Ref<InputEventKey> k = p_event;
  749. if (k.is_valid() && k->is_pressed()) {
  750. if (ED_IS_SHORTCUT("version_control/commit", p_event)) {
  751. if (_is_staging_area_empty()) {
  752. // Stage all files only when no files were previously staged.
  753. _move_all(unstaged_files);
  754. }
  755. _commit();
  756. commit_message->accept_event();
  757. }
  758. }
  759. }
  760. void VersionControlEditorPlugin::_toggle_vcs_integration(bool p_toggled) {
  761. if (p_toggled) {
  762. _initialize_vcs();
  763. } else {
  764. shut_down();
  765. }
  766. }
  767. void VersionControlEditorPlugin::_project_path_selected(String p_project_path) {
  768. project_path_input->set_text(p_project_path);
  769. }
  770. void VersionControlEditorPlugin::fetch_available_vcs_plugin_names() {
  771. available_plugins.clear();
  772. ClassDB::get_direct_inheriters_from_class(EditorVCSInterface::get_class_static(), &available_plugins);
  773. }
  774. void VersionControlEditorPlugin::register_editor() {
  775. EditorNode::get_singleton()->add_control_to_dock(EditorNode::DOCK_SLOT_RIGHT_UL, version_commit_dock);
  776. version_control_dock_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Version Control"), version_control_dock);
  777. _set_vcs_ui_state(true);
  778. }
  779. void VersionControlEditorPlugin::shut_down() {
  780. if (!EditorVCSInterface::get_singleton()) {
  781. return;
  782. }
  783. if (EditorFileSystem::get_singleton()->is_connected(SNAME("filesystem_changed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area))) {
  784. EditorFileSystem::get_singleton()->disconnect(SNAME("filesystem_changed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  785. }
  786. EditorVCSInterface::get_singleton()->shut_down();
  787. memdelete(EditorVCSInterface::get_singleton());
  788. EditorVCSInterface::set_singleton(nullptr);
  789. EditorNode::get_singleton()->remove_control_from_dock(version_commit_dock);
  790. EditorNode::get_singleton()->remove_bottom_panel_item(version_control_dock);
  791. _set_vcs_ui_state(false);
  792. }
  793. VersionControlEditorPlugin::VersionControlEditorPlugin() {
  794. singleton = this;
  795. version_control_actions = memnew(PopupMenu);
  796. metadata_dialog = memnew(ConfirmationDialog);
  797. metadata_dialog->set_title(TTR("Create Version Control Metadata"));
  798. metadata_dialog->set_min_size(Size2(200, 40));
  799. metadata_dialog->get_ok_button()->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_create_vcs_metadata_files));
  800. add_child(metadata_dialog);
  801. VBoxContainer *metadata_vb = memnew(VBoxContainer);
  802. metadata_dialog->add_child(metadata_vb);
  803. HBoxContainer *metadata_hb = memnew(HBoxContainer);
  804. metadata_hb->set_custom_minimum_size(Size2(200, 20));
  805. metadata_vb->add_child(metadata_hb);
  806. Label *l = memnew(Label);
  807. l->set_text(TTR("Create VCS metadata files for:"));
  808. metadata_hb->add_child(l);
  809. metadata_selection = memnew(OptionButton);
  810. metadata_selection->set_custom_minimum_size(Size2(100, 20));
  811. metadata_selection->add_item("None", (int)EditorVCSInterface::VCSMetadata::NONE);
  812. metadata_selection->add_item("Git", (int)EditorVCSInterface::VCSMetadata::GIT);
  813. metadata_selection->select((int)EditorVCSInterface::VCSMetadata::GIT);
  814. metadata_hb->add_child(metadata_selection);
  815. l = memnew(Label);
  816. l->set_text(TTR("Existing VCS metadata files will be overwritten."));
  817. metadata_vb->add_child(l);
  818. set_up_dialog = memnew(AcceptDialog);
  819. set_up_dialog->set_title(TTR("Local Settings"));
  820. set_up_dialog->set_min_size(Size2(600, 100));
  821. set_up_dialog->add_cancel_button("Cancel");
  822. set_up_dialog->set_hide_on_ok(true);
  823. add_child(set_up_dialog);
  824. Button *set_up_apply_button = set_up_dialog->get_ok_button();
  825. set_up_apply_button->set_text(TTR("Apply"));
  826. set_up_apply_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_set_credentials));
  827. set_up_vbc = memnew(VBoxContainer);
  828. set_up_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  829. set_up_dialog->add_child(set_up_vbc);
  830. HBoxContainer *set_up_hbc = memnew(HBoxContainer);
  831. set_up_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  832. set_up_vbc->add_child(set_up_hbc);
  833. Label *set_up_vcs_label = memnew(Label);
  834. set_up_vcs_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  835. set_up_vcs_label->set_text(TTR("VCS Provider"));
  836. set_up_hbc->add_child(set_up_vcs_label);
  837. set_up_choice = memnew(OptionButton);
  838. set_up_choice->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  839. set_up_hbc->add_child(set_up_choice);
  840. HBoxContainer *project_path_hbc = memnew(HBoxContainer);
  841. project_path_hbc->set_h_size_flags(Control::SIZE_FILL);
  842. set_up_vbc->add_child(project_path_hbc);
  843. Label *project_path_label = memnew(Label);
  844. project_path_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  845. project_path_label->set_text(TTR("VCS Project Path"));
  846. project_path_hbc->add_child(project_path_label);
  847. project_path_input = memnew(LineEdit);
  848. project_path_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  849. project_path_input->set_text(OS::get_singleton()->get_resource_dir());
  850. project_path_hbc->add_child(project_path_input);
  851. FileDialog *select_project_path_file_dialog = memnew(FileDialog);
  852. select_project_path_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  853. select_project_path_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_DIR);
  854. select_project_path_file_dialog->set_show_hidden_files(true);
  855. select_project_path_file_dialog->set_current_dir(OS::get_singleton()->get_resource_dir());
  856. select_project_path_file_dialog->connect(SNAME("dir_selected"), callable_mp(this, &VersionControlEditorPlugin::_project_path_selected));
  857. project_path_hbc->add_child(select_project_path_file_dialog);
  858. select_project_path_button = memnew(Button);
  859. select_project_path_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Folder", "EditorIcons"));
  860. select_project_path_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_file_dialog).bind(select_project_path_file_dialog));
  861. select_project_path_button->set_tooltip_text(TTR("Select VCS project path"));
  862. project_path_hbc->add_child(select_project_path_button);
  863. HBoxContainer *toggle_vcs_hbc = memnew(HBoxContainer);
  864. toggle_vcs_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  865. set_up_vbc->add_child(toggle_vcs_hbc);
  866. Label *toggle_vcs_label = memnew(Label);
  867. toggle_vcs_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  868. toggle_vcs_label->set_text(TTR("Connect to VCS"));
  869. toggle_vcs_hbc->add_child(toggle_vcs_label);
  870. toggle_vcs_choice = memnew(CheckButton);
  871. toggle_vcs_choice->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  872. toggle_vcs_choice->set_pressed_no_signal(false);
  873. toggle_vcs_choice->connect(SNAME("toggled"), callable_mp(this, &VersionControlEditorPlugin::_toggle_vcs_integration));
  874. toggle_vcs_hbc->add_child(toggle_vcs_choice);
  875. set_up_vbc->add_child(memnew(HSeparator));
  876. set_up_settings_vbc = memnew(VBoxContainer);
  877. set_up_settings_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  878. set_up_vbc->add_child(set_up_settings_vbc);
  879. Label *remote_login = memnew(Label);
  880. remote_login->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  881. remote_login->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  882. remote_login->set_text(TTR("Remote Login"));
  883. set_up_settings_vbc->add_child(remote_login);
  884. HBoxContainer *set_up_username_input = memnew(HBoxContainer);
  885. set_up_username_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  886. set_up_settings_vbc->add_child(set_up_username_input);
  887. Label *set_up_username_label = memnew(Label);
  888. set_up_username_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  889. set_up_username_label->set_text(TTR("Username"));
  890. set_up_username_input->add_child(set_up_username_label);
  891. set_up_username = memnew(LineEdit);
  892. set_up_username->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  893. set_up_username->set_text(EDITOR_DEF("version_control/username", ""));
  894. set_up_username->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  895. set_up_username_input->add_child(set_up_username);
  896. HBoxContainer *set_up_password_input = memnew(HBoxContainer);
  897. set_up_password_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  898. set_up_settings_vbc->add_child(set_up_password_input);
  899. Label *set_up_password_label = memnew(Label);
  900. set_up_password_label->set_text(TTR("Password"));
  901. set_up_password_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  902. set_up_password_input->add_child(set_up_password_label);
  903. set_up_password = memnew(LineEdit);
  904. set_up_password->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  905. set_up_password->set_secret(true);
  906. set_up_password->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  907. set_up_password_input->add_child(set_up_password);
  908. const String home_dir = OS::get_singleton()->has_environment("HOME") ? OS::get_singleton()->get_environment("HOME") : OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DOCUMENTS);
  909. HBoxContainer *set_up_ssh_public_key_input = memnew(HBoxContainer);
  910. set_up_ssh_public_key_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  911. set_up_settings_vbc->add_child(set_up_ssh_public_key_input);
  912. Label *set_up_ssh_public_key_label = memnew(Label);
  913. set_up_ssh_public_key_label->set_text(TTR("SSH Public Key Path"));
  914. set_up_ssh_public_key_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  915. set_up_ssh_public_key_input->add_child(set_up_ssh_public_key_label);
  916. HBoxContainer *set_up_ssh_public_key_input_hbc = memnew(HBoxContainer);
  917. set_up_ssh_public_key_input_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  918. set_up_ssh_public_key_input->add_child(set_up_ssh_public_key_input_hbc);
  919. set_up_ssh_public_key_path = memnew(LineEdit);
  920. set_up_ssh_public_key_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  921. set_up_ssh_public_key_path->set_text(EDITOR_DEF("version_control/ssh_public_key_path", ""));
  922. set_up_ssh_public_key_path->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  923. set_up_ssh_public_key_input_hbc->add_child(set_up_ssh_public_key_path);
  924. set_up_ssh_public_key_file_dialog = memnew(FileDialog);
  925. set_up_ssh_public_key_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  926. set_up_ssh_public_key_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
  927. set_up_ssh_public_key_file_dialog->set_show_hidden_files(true);
  928. set_up_ssh_public_key_file_dialog->set_current_dir(home_dir);
  929. set_up_ssh_public_key_file_dialog->connect(SNAME("file_selected"), callable_mp(this, &VersionControlEditorPlugin::_ssh_public_key_selected));
  930. set_up_ssh_public_key_input_hbc->add_child(set_up_ssh_public_key_file_dialog);
  931. Button *select_public_path_button = memnew(Button);
  932. select_public_path_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Folder", "EditorIcons"));
  933. select_public_path_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_file_dialog).bind(set_up_ssh_public_key_file_dialog));
  934. select_public_path_button->set_tooltip_text(TTR("Select SSH public key path"));
  935. set_up_ssh_public_key_input_hbc->add_child(select_public_path_button);
  936. HBoxContainer *set_up_ssh_private_key_input = memnew(HBoxContainer);
  937. set_up_ssh_private_key_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  938. set_up_settings_vbc->add_child(set_up_ssh_private_key_input);
  939. Label *set_up_ssh_private_key_label = memnew(Label);
  940. set_up_ssh_private_key_label->set_text(TTR("SSH Private Key Path"));
  941. set_up_ssh_private_key_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  942. set_up_ssh_private_key_input->add_child(set_up_ssh_private_key_label);
  943. HBoxContainer *set_up_ssh_private_key_input_hbc = memnew(HBoxContainer);
  944. set_up_ssh_private_key_input_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  945. set_up_ssh_private_key_input->add_child(set_up_ssh_private_key_input_hbc);
  946. set_up_ssh_private_key_path = memnew(LineEdit);
  947. set_up_ssh_private_key_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  948. set_up_ssh_private_key_path->set_text(EDITOR_DEF("version_control/ssh_private_key_path", ""));
  949. set_up_ssh_private_key_path->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  950. set_up_ssh_private_key_input_hbc->add_child(set_up_ssh_private_key_path);
  951. set_up_ssh_private_key_file_dialog = memnew(FileDialog);
  952. set_up_ssh_private_key_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  953. set_up_ssh_private_key_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
  954. set_up_ssh_private_key_file_dialog->set_show_hidden_files(true);
  955. set_up_ssh_private_key_file_dialog->set_current_dir(home_dir);
  956. set_up_ssh_private_key_file_dialog->connect("file_selected", callable_mp(this, &VersionControlEditorPlugin::_ssh_private_key_selected));
  957. set_up_ssh_private_key_input_hbc->add_child(set_up_ssh_private_key_file_dialog);
  958. Button *select_private_path_button = memnew(Button);
  959. select_private_path_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Folder", "EditorIcons"));
  960. select_private_path_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_file_dialog).bind(set_up_ssh_private_key_file_dialog));
  961. select_private_path_button->set_tooltip_text(TTR("Select SSH private key path"));
  962. set_up_ssh_private_key_input_hbc->add_child(select_private_path_button);
  963. HBoxContainer *set_up_ssh_passphrase_input = memnew(HBoxContainer);
  964. set_up_ssh_passphrase_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  965. set_up_settings_vbc->add_child(set_up_ssh_passphrase_input);
  966. Label *set_up_ssh_passphrase_label = memnew(Label);
  967. set_up_ssh_passphrase_label->set_text(TTR("SSH Passphrase"));
  968. set_up_ssh_passphrase_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  969. set_up_ssh_passphrase_input->add_child(set_up_ssh_passphrase_label);
  970. set_up_ssh_passphrase = memnew(LineEdit);
  971. set_up_ssh_passphrase->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  972. set_up_ssh_passphrase->set_secret(true);
  973. set_up_ssh_passphrase->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  974. set_up_ssh_passphrase_input->add_child(set_up_ssh_passphrase);
  975. set_up_warning_text = memnew(Label);
  976. set_up_warning_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  977. set_up_warning_text->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  978. set_up_settings_vbc->add_child(set_up_warning_text);
  979. version_commit_dock = memnew(VBoxContainer);
  980. version_commit_dock->set_visible(false);
  981. version_commit_dock->set_name(TTR("Commit"));
  982. VBoxContainer *unstage_area = memnew(VBoxContainer);
  983. unstage_area->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  984. unstage_area->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  985. version_commit_dock->add_child(unstage_area);
  986. HBoxContainer *unstage_title = memnew(HBoxContainer);
  987. unstage_area->add_child(unstage_title);
  988. Label *unstage_label = memnew(Label);
  989. unstage_label->set_text(TTR("Unstaged Changes"));
  990. unstage_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  991. unstage_title->add_child(unstage_label);
  992. refresh_button = memnew(Button);
  993. refresh_button->set_tooltip_text(TTR("Detect new changes"));
  994. refresh_button->set_flat(true);
  995. refresh_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  996. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  997. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_commit_list));
  998. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_branch_list));
  999. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_remote_list));
  1000. unstage_title->add_child(refresh_button);
  1001. discard_all_button = memnew(Button);
  1002. discard_all_button->set_tooltip_text(TTR("Discard all changes"));
  1003. discard_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1004. discard_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_discard_all));
  1005. discard_all_button->set_flat(true);
  1006. unstage_title->add_child(discard_all_button);
  1007. stage_all_button = memnew(Button);
  1008. stage_all_button->set_flat(true);
  1009. stage_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1010. stage_all_button->set_tooltip_text(TTR("Stage all changes"));
  1011. unstage_title->add_child(stage_all_button);
  1012. unstaged_files = memnew(Tree);
  1013. unstaged_files->set_h_size_flags(Tree::SIZE_EXPAND_FILL);
  1014. unstaged_files->set_v_size_flags(Tree::SIZE_EXPAND_FILL);
  1015. unstaged_files->set_select_mode(Tree::SELECT_ROW);
  1016. unstaged_files->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(unstaged_files));
  1017. unstaged_files->connect(SNAME("item_activated"), callable_mp(this, &VersionControlEditorPlugin::_item_activated).bind(unstaged_files));
  1018. unstaged_files->connect(SNAME("button_clicked"), callable_mp(this, &VersionControlEditorPlugin::_cell_button_pressed));
  1019. unstaged_files->create_item();
  1020. unstaged_files->set_hide_root(true);
  1021. unstage_area->add_child(unstaged_files);
  1022. VBoxContainer *stage_area = memnew(VBoxContainer);
  1023. stage_area->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1024. stage_area->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1025. version_commit_dock->add_child(stage_area);
  1026. HBoxContainer *stage_title = memnew(HBoxContainer);
  1027. stage_area->add_child(stage_title);
  1028. Label *stage_label = memnew(Label);
  1029. stage_label->set_text(TTR("Staged Changes"));
  1030. stage_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1031. stage_title->add_child(stage_label);
  1032. unstage_all_button = memnew(Button);
  1033. unstage_all_button->set_flat(true);
  1034. unstage_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1035. unstage_all_button->set_tooltip_text(TTR("Unstage all changes"));
  1036. stage_title->add_child(unstage_all_button);
  1037. staged_files = memnew(Tree);
  1038. staged_files->set_h_size_flags(Tree::SIZE_EXPAND_FILL);
  1039. staged_files->set_v_size_flags(Tree::SIZE_EXPAND_FILL);
  1040. staged_files->set_select_mode(Tree::SELECT_ROW);
  1041. staged_files->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(staged_files));
  1042. staged_files->connect(SNAME("button_clicked"), callable_mp(this, &VersionControlEditorPlugin::_cell_button_pressed));
  1043. staged_files->connect(SNAME("item_activated"), callable_mp(this, &VersionControlEditorPlugin::_item_activated).bind(staged_files));
  1044. staged_files->create_item();
  1045. staged_files->set_hide_root(true);
  1046. stage_area->add_child(staged_files);
  1047. // Editor crashes if bind is null
  1048. unstage_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_move_all).bind(staged_files));
  1049. stage_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_move_all).bind(unstaged_files));
  1050. version_commit_dock->add_child(memnew(HSeparator));
  1051. VBoxContainer *commit_area = memnew(VBoxContainer);
  1052. version_commit_dock->add_child(commit_area);
  1053. Label *commit_label = memnew(Label);
  1054. commit_label->set_text(TTR("Commit Message"));
  1055. commit_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1056. commit_area->add_child(commit_label);
  1057. commit_message = memnew(TextEdit);
  1058. commit_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1059. commit_message->set_h_grow_direction(Control::GrowDirection::GROW_DIRECTION_BEGIN);
  1060. commit_message->set_v_grow_direction(Control::GrowDirection::GROW_DIRECTION_END);
  1061. commit_message->set_custom_minimum_size(Size2(200, 100));
  1062. commit_message->set_line_wrapping_mode(TextEdit::LINE_WRAPPING_BOUNDARY);
  1063. commit_message->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_commit_button));
  1064. commit_message->connect(SNAME("gui_input"), callable_mp(this, &VersionControlEditorPlugin::_commit_message_gui_input));
  1065. commit_area->add_child(commit_message);
  1066. ED_SHORTCUT("version_control/commit", TTR("Commit"), KeyModifierMask::CMD_OR_CTRL | Key::ENTER);
  1067. commit_button = memnew(Button);
  1068. commit_button->set_text(TTR("Commit Changes"));
  1069. commit_button->set_disabled(true);
  1070. commit_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_commit));
  1071. commit_area->add_child(commit_button);
  1072. version_commit_dock->add_child(memnew(HSeparator));
  1073. HBoxContainer *commit_list_hbc = memnew(HBoxContainer);
  1074. version_commit_dock->add_child(commit_list_hbc);
  1075. Label *commit_list_label = memnew(Label);
  1076. commit_list_label->set_text(TTR("Commit List"));
  1077. commit_list_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1078. commit_list_hbc->add_child(commit_list_label);
  1079. commit_list_size_button = memnew(OptionButton);
  1080. commit_list_size_button->set_tooltip_text(TTR("Commit list size"));
  1081. commit_list_size_button->add_item("10");
  1082. commit_list_size_button->set_item_metadata(0, 10);
  1083. commit_list_size_button->add_item("20");
  1084. commit_list_size_button->set_item_metadata(1, 20);
  1085. commit_list_size_button->add_item("30");
  1086. commit_list_size_button->set_item_metadata(2, 30);
  1087. commit_list_size_button->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_set_commit_list_size));
  1088. commit_list_hbc->add_child(commit_list_size_button);
  1089. commit_list = memnew(Tree);
  1090. commit_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1091. commit_list->set_v_grow_direction(Control::GrowDirection::GROW_DIRECTION_END);
  1092. commit_list->set_custom_minimum_size(Size2(200, 160));
  1093. commit_list->create_item();
  1094. commit_list->set_hide_root(true);
  1095. commit_list->set_select_mode(Tree::SELECT_ROW);
  1096. commit_list->set_columns(2); // Commit msg, author
  1097. commit_list->set_column_custom_minimum_width(0, 40);
  1098. commit_list->set_column_custom_minimum_width(1, 20);
  1099. commit_list->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(commit_list));
  1100. version_commit_dock->add_child(commit_list);
  1101. version_commit_dock->add_child(memnew(HSeparator));
  1102. HBoxContainer *menu_bar = memnew(HBoxContainer);
  1103. menu_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1104. menu_bar->set_v_size_flags(Control::SIZE_FILL);
  1105. version_commit_dock->add_child(menu_bar);
  1106. branch_select = memnew(OptionButton);
  1107. branch_select->set_tooltip_text(TTR("Branches"));
  1108. branch_select->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1109. branch_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_branch_item_selected));
  1110. branch_select->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_branch_list));
  1111. menu_bar->add_child(branch_select);
  1112. branch_create_confirm = memnew(AcceptDialog);
  1113. branch_create_confirm->set_title(TTR("Create New Branch"));
  1114. branch_create_confirm->set_min_size(Size2(400, 100));
  1115. branch_create_confirm->set_hide_on_ok(true);
  1116. version_commit_dock->add_child(branch_create_confirm);
  1117. branch_create_ok = branch_create_confirm->get_ok_button();
  1118. branch_create_ok->set_text(TTR("Create"));
  1119. branch_create_ok->set_disabled(true);
  1120. branch_create_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_create_branch));
  1121. branch_remove_confirm = memnew(AcceptDialog);
  1122. branch_remove_confirm->set_title(TTR("Remove Branch"));
  1123. branch_remove_confirm->add_cancel_button();
  1124. version_commit_dock->add_child(branch_remove_confirm);
  1125. Button *branch_remove_ok = branch_remove_confirm->get_ok_button();
  1126. branch_remove_ok->set_text(TTR("Remove"));
  1127. branch_remove_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_remove_branch));
  1128. VBoxContainer *branch_create_vbc = memnew(VBoxContainer);
  1129. branch_create_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1130. branch_create_confirm->add_child(branch_create_vbc);
  1131. HBoxContainer *branch_create_hbc = memnew(HBoxContainer);
  1132. branch_create_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1133. branch_create_vbc->add_child(branch_create_hbc);
  1134. Label *branch_create_name_label = memnew(Label);
  1135. branch_create_name_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1136. branch_create_name_label->set_text(TTR("Branch Name"));
  1137. branch_create_hbc->add_child(branch_create_name_label);
  1138. branch_create_name_input = memnew(LineEdit);
  1139. branch_create_name_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1140. branch_create_name_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_branch_create_button));
  1141. branch_create_hbc->add_child(branch_create_name_input);
  1142. remote_select = memnew(OptionButton);
  1143. remote_select->set_tooltip_text(TTR("Remotes"));
  1144. remote_select->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1145. remote_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_remote_selected));
  1146. remote_select->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_remote_list));
  1147. menu_bar->add_child(remote_select);
  1148. remote_create_confirm = memnew(AcceptDialog);
  1149. remote_create_confirm->set_title(TTR("Create New Remote"));
  1150. remote_create_confirm->set_min_size(Size2(400, 100));
  1151. remote_create_confirm->set_hide_on_ok(true);
  1152. version_commit_dock->add_child(remote_create_confirm);
  1153. remote_create_ok = remote_create_confirm->get_ok_button();
  1154. remote_create_ok->set_text(TTR("Create"));
  1155. remote_create_ok->set_disabled(true);
  1156. remote_create_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_create_remote));
  1157. remote_remove_confirm = memnew(AcceptDialog);
  1158. remote_remove_confirm->set_title(TTR("Remove Remote"));
  1159. remote_remove_confirm->add_cancel_button();
  1160. version_commit_dock->add_child(remote_remove_confirm);
  1161. Button *remote_remove_ok = remote_remove_confirm->get_ok_button();
  1162. remote_remove_ok->set_text(TTR("Remove"));
  1163. remote_remove_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_remove_remote));
  1164. VBoxContainer *remote_create_vbc = memnew(VBoxContainer);
  1165. remote_create_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1166. remote_create_confirm->add_child(remote_create_vbc);
  1167. HBoxContainer *remote_create_name_hbc = memnew(HBoxContainer);
  1168. remote_create_name_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1169. remote_create_vbc->add_child(remote_create_name_hbc);
  1170. Label *remote_create_name_label = memnew(Label);
  1171. remote_create_name_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1172. remote_create_name_label->set_text(TTR("Remote Name"));
  1173. remote_create_name_hbc->add_child(remote_create_name_label);
  1174. remote_create_name_input = memnew(LineEdit);
  1175. remote_create_name_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1176. remote_create_name_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_remote_create_button));
  1177. remote_create_name_hbc->add_child(remote_create_name_input);
  1178. HBoxContainer *remote_create_hbc = memnew(HBoxContainer);
  1179. remote_create_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1180. remote_create_vbc->add_child(remote_create_hbc);
  1181. Label *remote_create_url_label = memnew(Label);
  1182. remote_create_url_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1183. remote_create_url_label->set_text(TTR("Remote URL"));
  1184. remote_create_hbc->add_child(remote_create_url_label);
  1185. remote_create_url_input = memnew(LineEdit);
  1186. remote_create_url_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1187. remote_create_url_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_remote_create_button));
  1188. remote_create_hbc->add_child(remote_create_url_input);
  1189. fetch_button = memnew(Button);
  1190. fetch_button->set_flat(true);
  1191. fetch_button->set_tooltip_text(TTR("Fetch"));
  1192. fetch_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  1193. fetch_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_fetch));
  1194. menu_bar->add_child(fetch_button);
  1195. pull_button = memnew(Button);
  1196. pull_button->set_flat(true);
  1197. pull_button->set_tooltip_text(TTR("Pull"));
  1198. pull_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1199. pull_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_pull));
  1200. menu_bar->add_child(pull_button);
  1201. push_button = memnew(Button);
  1202. push_button->set_flat(true);
  1203. push_button->set_tooltip_text(TTR("Push"));
  1204. push_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1205. push_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_push));
  1206. menu_bar->add_child(push_button);
  1207. extra_options = memnew(MenuButton);
  1208. extra_options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  1209. extra_options->get_popup()->connect(SNAME("about_to_popup"), callable_mp(this, &VersionControlEditorPlugin::_update_extra_options));
  1210. extra_options->get_popup()->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_extra_option_selected));
  1211. menu_bar->add_child(extra_options);
  1212. extra_options->get_popup()->add_item(TTR("Force Push"), EXTRA_OPTION_FORCE_PUSH);
  1213. extra_options->get_popup()->add_separator();
  1214. extra_options->get_popup()->add_item(TTR("Create New Branch"), EXTRA_OPTION_CREATE_BRANCH);
  1215. extra_options_remove_branch_list = memnew(PopupMenu);
  1216. extra_options_remove_branch_list->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_branch_remove_confirm));
  1217. extra_options_remove_branch_list->set_name("Remove Branch");
  1218. extra_options->get_popup()->add_child(extra_options_remove_branch_list);
  1219. extra_options->get_popup()->add_submenu_item(TTR("Remove Branch"), "Remove Branch");
  1220. extra_options->get_popup()->add_separator();
  1221. extra_options->get_popup()->add_item(TTR("Create New Remote"), EXTRA_OPTION_CREATE_REMOTE);
  1222. extra_options_remove_remote_list = memnew(PopupMenu);
  1223. extra_options_remove_remote_list->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_remote_remove_confirm));
  1224. extra_options_remove_remote_list->set_name("Remove Remote");
  1225. extra_options->get_popup()->add_child(extra_options_remove_remote_list);
  1226. extra_options->get_popup()->add_submenu_item(TTR("Remove Remote"), "Remove Remote");
  1227. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_NEW] = TTR("New");
  1228. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = TTR("Modified");
  1229. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_RENAMED] = TTR("Renamed");
  1230. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_DELETED] = TTR("Deleted");
  1231. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = TTR("Typechange");
  1232. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = TTR("Unmerged");
  1233. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_NEW] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor"));
  1234. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1235. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_RENAMED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1236. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_DELETED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor"));
  1237. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Editor"));
  1238. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1239. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_NEW] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons"));
  1240. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1241. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_RENAMED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1242. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1243. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_DELETED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  1244. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1245. version_control_dock = memnew(VBoxContainer);
  1246. version_control_dock->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1247. version_control_dock->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  1248. version_control_dock->hide();
  1249. HBoxContainer *diff_heading = memnew(HBoxContainer);
  1250. diff_heading->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1251. diff_heading->set_tooltip_text(TTR("View file diffs before committing them to the latest version"));
  1252. version_control_dock->add_child(diff_heading);
  1253. diff_title = memnew(Label);
  1254. diff_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1255. diff_heading->add_child(diff_title);
  1256. Label *view = memnew(Label);
  1257. view->set_text(TTR("View:"));
  1258. diff_heading->add_child(view);
  1259. diff_view_type_select = memnew(OptionButton);
  1260. diff_view_type_select->add_item(TTR("Split"), DIFF_VIEW_TYPE_SPLIT);
  1261. diff_view_type_select->add_item(TTR("Unified"), DIFF_VIEW_TYPE_UNIFIED);
  1262. diff_view_type_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_display_diff));
  1263. diff_heading->add_child(diff_view_type_select);
  1264. diff = memnew(RichTextLabel);
  1265. diff->set_h_size_flags(TextEdit::SIZE_EXPAND_FILL);
  1266. diff->set_v_size_flags(TextEdit::SIZE_EXPAND_FILL);
  1267. diff->set_use_bbcode(true);
  1268. diff->set_selection_enabled(true);
  1269. version_control_dock->add_child(diff);
  1270. _update_set_up_warning("");
  1271. }
  1272. VersionControlEditorPlugin::~VersionControlEditorPlugin() {
  1273. shut_down();
  1274. memdelete(version_commit_dock);
  1275. memdelete(version_control_dock);
  1276. memdelete(version_control_actions);
  1277. }