version_control_editor_plugin.cpp 68 KB

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