version_control_editor_plugin.cpp 66 KB

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