version_control_editor_plugin.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 behaviour
  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. HBoxContainer *set_up_ssh_public_key_input = memnew(HBoxContainer);
  908. set_up_ssh_public_key_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  909. set_up_settings_vbc->add_child(set_up_ssh_public_key_input);
  910. Label *set_up_ssh_public_key_label = memnew(Label);
  911. set_up_ssh_public_key_label->set_text(TTR("SSH Public Key Path"));
  912. set_up_ssh_public_key_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  913. set_up_ssh_public_key_input->add_child(set_up_ssh_public_key_label);
  914. HBoxContainer *set_up_ssh_public_key_input_hbc = memnew(HBoxContainer);
  915. set_up_ssh_public_key_input_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  916. set_up_ssh_public_key_input->add_child(set_up_ssh_public_key_input_hbc);
  917. set_up_ssh_public_key_path = memnew(LineEdit);
  918. set_up_ssh_public_key_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  919. set_up_ssh_public_key_path->set_text(EDITOR_DEF("version_control/ssh_public_key_path", ""));
  920. set_up_ssh_public_key_path->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  921. set_up_ssh_public_key_input_hbc->add_child(set_up_ssh_public_key_path);
  922. set_up_ssh_public_key_file_dialog = memnew(FileDialog);
  923. set_up_ssh_public_key_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  924. set_up_ssh_public_key_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
  925. set_up_ssh_public_key_file_dialog->set_show_hidden_files(true);
  926. // TODO: Make this start at the user's home folder
  927. Ref<DirAccess> d = DirAccess::open(OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DOCUMENTS));
  928. d->change_dir("../");
  929. set_up_ssh_public_key_file_dialog->set_current_dir(d->get_current_dir());
  930. set_up_ssh_public_key_file_dialog->connect(SNAME("file_selected"), callable_mp(this, &VersionControlEditorPlugin::_ssh_public_key_selected));
  931. set_up_ssh_public_key_input_hbc->add_child(set_up_ssh_public_key_file_dialog);
  932. Button *select_public_path_button = memnew(Button);
  933. select_public_path_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Folder", "EditorIcons"));
  934. select_public_path_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_file_dialog).bind(set_up_ssh_public_key_file_dialog));
  935. select_public_path_button->set_tooltip_text(TTR("Select SSH public key path"));
  936. set_up_ssh_public_key_input_hbc->add_child(select_public_path_button);
  937. HBoxContainer *set_up_ssh_private_key_input = memnew(HBoxContainer);
  938. set_up_ssh_private_key_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  939. set_up_settings_vbc->add_child(set_up_ssh_private_key_input);
  940. Label *set_up_ssh_private_key_label = memnew(Label);
  941. set_up_ssh_private_key_label->set_text(TTR("SSH Private Key Path"));
  942. set_up_ssh_private_key_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  943. set_up_ssh_private_key_input->add_child(set_up_ssh_private_key_label);
  944. HBoxContainer *set_up_ssh_private_key_input_hbc = memnew(HBoxContainer);
  945. set_up_ssh_private_key_input_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  946. set_up_ssh_private_key_input->add_child(set_up_ssh_private_key_input_hbc);
  947. set_up_ssh_private_key_path = memnew(LineEdit);
  948. set_up_ssh_private_key_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  949. set_up_ssh_private_key_path->set_text(EDITOR_DEF("version_control/ssh_private_key_path", ""));
  950. set_up_ssh_private_key_path->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  951. set_up_ssh_private_key_input_hbc->add_child(set_up_ssh_private_key_path);
  952. set_up_ssh_private_key_file_dialog = memnew(FileDialog);
  953. set_up_ssh_private_key_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  954. set_up_ssh_private_key_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
  955. set_up_ssh_private_key_file_dialog->set_show_hidden_files(true);
  956. // TODO: Make this start at the user's home folder
  957. set_up_ssh_private_key_file_dialog->set_current_dir(d->get_current_dir());
  958. set_up_ssh_private_key_file_dialog->connect("file_selected", callable_mp(this, &VersionControlEditorPlugin::_ssh_private_key_selected));
  959. set_up_ssh_private_key_input_hbc->add_child(set_up_ssh_private_key_file_dialog);
  960. Button *select_private_path_button = memnew(Button);
  961. select_private_path_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Folder", "EditorIcons"));
  962. select_private_path_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_file_dialog).bind(set_up_ssh_private_key_file_dialog));
  963. select_private_path_button->set_tooltip_text(TTR("Select SSH private key path"));
  964. set_up_ssh_private_key_input_hbc->add_child(select_private_path_button);
  965. HBoxContainer *set_up_ssh_passphrase_input = memnew(HBoxContainer);
  966. set_up_ssh_passphrase_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  967. set_up_settings_vbc->add_child(set_up_ssh_passphrase_input);
  968. Label *set_up_ssh_passphrase_label = memnew(Label);
  969. set_up_ssh_passphrase_label->set_text(TTR("SSH Passphrase"));
  970. set_up_ssh_passphrase_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  971. set_up_ssh_passphrase_input->add_child(set_up_ssh_passphrase_label);
  972. set_up_ssh_passphrase = memnew(LineEdit);
  973. set_up_ssh_passphrase->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  974. set_up_ssh_passphrase->set_secret(true);
  975. set_up_ssh_passphrase->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_set_up_warning));
  976. set_up_ssh_passphrase_input->add_child(set_up_ssh_passphrase);
  977. set_up_warning_text = memnew(Label);
  978. set_up_warning_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  979. set_up_warning_text->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  980. set_up_settings_vbc->add_child(set_up_warning_text);
  981. version_commit_dock = memnew(VBoxContainer);
  982. version_commit_dock->set_visible(false);
  983. version_commit_dock->set_name(TTR("Commit"));
  984. VBoxContainer *unstage_area = memnew(VBoxContainer);
  985. unstage_area->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  986. unstage_area->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  987. version_commit_dock->add_child(unstage_area);
  988. HBoxContainer *unstage_title = memnew(HBoxContainer);
  989. unstage_area->add_child(unstage_title);
  990. Label *unstage_label = memnew(Label);
  991. unstage_label->set_text(TTR("Unstaged Changes"));
  992. unstage_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  993. unstage_title->add_child(unstage_label);
  994. refresh_button = memnew(Button);
  995. refresh_button->set_tooltip_text(TTR("Detect new changes"));
  996. refresh_button->set_flat(true);
  997. refresh_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  998. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_stage_area));
  999. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_commit_list));
  1000. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_branch_list));
  1001. refresh_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_remote_list));
  1002. unstage_title->add_child(refresh_button);
  1003. discard_all_button = memnew(Button);
  1004. discard_all_button->set_tooltip_text(TTR("Discard all changes"));
  1005. discard_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1006. discard_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_discard_all));
  1007. discard_all_button->set_flat(true);
  1008. unstage_title->add_child(discard_all_button);
  1009. stage_all_button = memnew(Button);
  1010. stage_all_button->set_flat(true);
  1011. stage_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1012. stage_all_button->set_tooltip_text(TTR("Stage all changes"));
  1013. unstage_title->add_child(stage_all_button);
  1014. unstaged_files = memnew(Tree);
  1015. unstaged_files->set_h_size_flags(Tree::SIZE_EXPAND_FILL);
  1016. unstaged_files->set_v_size_flags(Tree::SIZE_EXPAND_FILL);
  1017. unstaged_files->set_select_mode(Tree::SELECT_ROW);
  1018. unstaged_files->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(unstaged_files));
  1019. unstaged_files->connect(SNAME("item_activated"), callable_mp(this, &VersionControlEditorPlugin::_item_activated).bind(unstaged_files));
  1020. unstaged_files->connect(SNAME("button_clicked"), callable_mp(this, &VersionControlEditorPlugin::_cell_button_pressed));
  1021. unstaged_files->create_item();
  1022. unstaged_files->set_hide_root(true);
  1023. unstage_area->add_child(unstaged_files);
  1024. VBoxContainer *stage_area = memnew(VBoxContainer);
  1025. stage_area->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1026. stage_area->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1027. version_commit_dock->add_child(stage_area);
  1028. HBoxContainer *stage_title = memnew(HBoxContainer);
  1029. stage_area->add_child(stage_title);
  1030. Label *stage_label = memnew(Label);
  1031. stage_label->set_text(TTR("Staged Changes"));
  1032. stage_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1033. stage_title->add_child(stage_label);
  1034. unstage_all_button = memnew(Button);
  1035. unstage_all_button->set_flat(true);
  1036. unstage_all_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1037. unstage_all_button->set_tooltip_text(TTR("Unstage all changes"));
  1038. stage_title->add_child(unstage_all_button);
  1039. staged_files = memnew(Tree);
  1040. staged_files->set_h_size_flags(Tree::SIZE_EXPAND_FILL);
  1041. staged_files->set_v_size_flags(Tree::SIZE_EXPAND_FILL);
  1042. staged_files->set_select_mode(Tree::SELECT_ROW);
  1043. staged_files->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(staged_files));
  1044. staged_files->connect(SNAME("button_clicked"), callable_mp(this, &VersionControlEditorPlugin::_cell_button_pressed));
  1045. staged_files->connect(SNAME("item_activated"), callable_mp(this, &VersionControlEditorPlugin::_item_activated).bind(staged_files));
  1046. staged_files->create_item();
  1047. staged_files->set_hide_root(true);
  1048. stage_area->add_child(staged_files);
  1049. // Editor crashes if bind is null
  1050. unstage_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_move_all).bind(staged_files));
  1051. stage_all_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_move_all).bind(unstaged_files));
  1052. version_commit_dock->add_child(memnew(HSeparator));
  1053. VBoxContainer *commit_area = memnew(VBoxContainer);
  1054. version_commit_dock->add_child(commit_area);
  1055. Label *commit_label = memnew(Label);
  1056. commit_label->set_text(TTR("Commit Message"));
  1057. commit_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1058. commit_area->add_child(commit_label);
  1059. commit_message = memnew(TextEdit);
  1060. commit_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1061. commit_message->set_h_grow_direction(Control::GrowDirection::GROW_DIRECTION_BEGIN);
  1062. commit_message->set_v_grow_direction(Control::GrowDirection::GROW_DIRECTION_END);
  1063. commit_message->set_custom_minimum_size(Size2(200, 100));
  1064. commit_message->set_line_wrapping_mode(TextEdit::LINE_WRAPPING_BOUNDARY);
  1065. commit_message->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_commit_button));
  1066. commit_message->connect(SNAME("gui_input"), callable_mp(this, &VersionControlEditorPlugin::_commit_message_gui_input));
  1067. commit_area->add_child(commit_message);
  1068. ED_SHORTCUT("version_control/commit", TTR("Commit"), KeyModifierMask::CMD_OR_CTRL | Key::ENTER);
  1069. commit_button = memnew(Button);
  1070. commit_button->set_text(TTR("Commit Changes"));
  1071. commit_button->set_disabled(true);
  1072. commit_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_commit));
  1073. commit_area->add_child(commit_button);
  1074. version_commit_dock->add_child(memnew(HSeparator));
  1075. HBoxContainer *commit_list_hbc = memnew(HBoxContainer);
  1076. version_commit_dock->add_child(commit_list_hbc);
  1077. Label *commit_list_label = memnew(Label);
  1078. commit_list_label->set_text(TTR("Commit List"));
  1079. commit_list_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1080. commit_list_hbc->add_child(commit_list_label);
  1081. commit_list_size_button = memnew(OptionButton);
  1082. commit_list_size_button->set_tooltip_text(TTR("Commit list size"));
  1083. commit_list_size_button->add_item("10");
  1084. commit_list_size_button->set_item_metadata(0, 10);
  1085. commit_list_size_button->add_item("20");
  1086. commit_list_size_button->set_item_metadata(1, 20);
  1087. commit_list_size_button->add_item("30");
  1088. commit_list_size_button->set_item_metadata(2, 30);
  1089. commit_list_size_button->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_set_commit_list_size));
  1090. commit_list_hbc->add_child(commit_list_size_button);
  1091. commit_list = memnew(Tree);
  1092. commit_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1093. commit_list->set_v_grow_direction(Control::GrowDirection::GROW_DIRECTION_END);
  1094. commit_list->set_custom_minimum_size(Size2(200, 160));
  1095. commit_list->create_item();
  1096. commit_list->set_hide_root(true);
  1097. commit_list->set_select_mode(Tree::SELECT_ROW);
  1098. commit_list->set_columns(2); // Commit msg, author
  1099. commit_list->set_column_custom_minimum_width(0, 40);
  1100. commit_list->set_column_custom_minimum_width(1, 20);
  1101. commit_list->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_load_diff).bind(commit_list));
  1102. version_commit_dock->add_child(commit_list);
  1103. version_commit_dock->add_child(memnew(HSeparator));
  1104. HBoxContainer *menu_bar = memnew(HBoxContainer);
  1105. menu_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1106. menu_bar->set_v_size_flags(Control::SIZE_FILL);
  1107. version_commit_dock->add_child(menu_bar);
  1108. branch_select = memnew(OptionButton);
  1109. branch_select->set_tooltip_text(TTR("Branches"));
  1110. branch_select->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1111. branch_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_branch_item_selected));
  1112. branch_select->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_branch_list));
  1113. menu_bar->add_child(branch_select);
  1114. branch_create_confirm = memnew(AcceptDialog);
  1115. branch_create_confirm->set_title(TTR("Create New Branch"));
  1116. branch_create_confirm->set_min_size(Size2(400, 100));
  1117. branch_create_confirm->set_hide_on_ok(true);
  1118. version_commit_dock->add_child(branch_create_confirm);
  1119. branch_create_ok = branch_create_confirm->get_ok_button();
  1120. branch_create_ok->set_text(TTR("Create"));
  1121. branch_create_ok->set_disabled(true);
  1122. branch_create_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_create_branch));
  1123. branch_remove_confirm = memnew(AcceptDialog);
  1124. branch_remove_confirm->set_title(TTR("Remove Branch"));
  1125. branch_remove_confirm->add_cancel_button();
  1126. version_commit_dock->add_child(branch_remove_confirm);
  1127. Button *branch_remove_ok = branch_remove_confirm->get_ok_button();
  1128. branch_remove_ok->set_text(TTR("Remove"));
  1129. branch_remove_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_remove_branch));
  1130. VBoxContainer *branch_create_vbc = memnew(VBoxContainer);
  1131. branch_create_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1132. branch_create_confirm->add_child(branch_create_vbc);
  1133. HBoxContainer *branch_create_hbc = memnew(HBoxContainer);
  1134. branch_create_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1135. branch_create_vbc->add_child(branch_create_hbc);
  1136. Label *branch_create_name_label = memnew(Label);
  1137. branch_create_name_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1138. branch_create_name_label->set_text(TTR("Branch Name"));
  1139. branch_create_hbc->add_child(branch_create_name_label);
  1140. branch_create_name_input = memnew(LineEdit);
  1141. branch_create_name_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1142. branch_create_name_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_branch_create_button));
  1143. branch_create_hbc->add_child(branch_create_name_input);
  1144. remote_select = memnew(OptionButton);
  1145. remote_select->set_tooltip_text(TTR("Remotes"));
  1146. remote_select->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1147. remote_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_remote_selected));
  1148. remote_select->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_refresh_remote_list));
  1149. menu_bar->add_child(remote_select);
  1150. remote_create_confirm = memnew(AcceptDialog);
  1151. remote_create_confirm->set_title(TTR("Create New Remote"));
  1152. remote_create_confirm->set_min_size(Size2(400, 100));
  1153. remote_create_confirm->set_hide_on_ok(true);
  1154. version_commit_dock->add_child(remote_create_confirm);
  1155. remote_create_ok = remote_create_confirm->get_ok_button();
  1156. remote_create_ok->set_text(TTR("Create"));
  1157. remote_create_ok->set_disabled(true);
  1158. remote_create_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_create_remote));
  1159. remote_remove_confirm = memnew(AcceptDialog);
  1160. remote_remove_confirm->set_title(TTR("Remove Remote"));
  1161. remote_remove_confirm->add_cancel_button();
  1162. version_commit_dock->add_child(remote_remove_confirm);
  1163. Button *remote_remove_ok = remote_remove_confirm->get_ok_button();
  1164. remote_remove_ok->set_text(TTR("Remove"));
  1165. remote_remove_ok->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_remove_remote));
  1166. VBoxContainer *remote_create_vbc = memnew(VBoxContainer);
  1167. remote_create_vbc->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1168. remote_create_confirm->add_child(remote_create_vbc);
  1169. HBoxContainer *remote_create_name_hbc = memnew(HBoxContainer);
  1170. remote_create_name_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1171. remote_create_vbc->add_child(remote_create_name_hbc);
  1172. Label *remote_create_name_label = memnew(Label);
  1173. remote_create_name_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1174. remote_create_name_label->set_text(TTR("Remote Name"));
  1175. remote_create_name_hbc->add_child(remote_create_name_label);
  1176. remote_create_name_input = memnew(LineEdit);
  1177. remote_create_name_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1178. remote_create_name_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_remote_create_button));
  1179. remote_create_name_hbc->add_child(remote_create_name_input);
  1180. HBoxContainer *remote_create_hbc = memnew(HBoxContainer);
  1181. remote_create_hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1182. remote_create_vbc->add_child(remote_create_hbc);
  1183. Label *remote_create_url_label = memnew(Label);
  1184. remote_create_url_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1185. remote_create_url_label->set_text(TTR("Remote URL"));
  1186. remote_create_hbc->add_child(remote_create_url_label);
  1187. remote_create_url_input = memnew(LineEdit);
  1188. remote_create_url_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1189. remote_create_url_input->connect(SNAME("text_changed"), callable_mp(this, &VersionControlEditorPlugin::_update_remote_create_button));
  1190. remote_create_hbc->add_child(remote_create_url_input);
  1191. fetch_button = memnew(Button);
  1192. fetch_button->set_flat(true);
  1193. fetch_button->set_tooltip_text(TTR("Fetch"));
  1194. fetch_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  1195. fetch_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_fetch));
  1196. menu_bar->add_child(fetch_button);
  1197. pull_button = memnew(Button);
  1198. pull_button->set_flat(true);
  1199. pull_button->set_tooltip_text(TTR("Pull"));
  1200. pull_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1201. pull_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_pull));
  1202. menu_bar->add_child(pull_button);
  1203. push_button = memnew(Button);
  1204. push_button->set_flat(true);
  1205. push_button->set_tooltip_text(TTR("Push"));
  1206. push_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1207. push_button->connect(SNAME("pressed"), callable_mp(this, &VersionControlEditorPlugin::_push));
  1208. menu_bar->add_child(push_button);
  1209. extra_options = memnew(MenuButton);
  1210. extra_options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  1211. extra_options->get_popup()->connect(SNAME("about_to_popup"), callable_mp(this, &VersionControlEditorPlugin::_update_extra_options));
  1212. extra_options->get_popup()->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_extra_option_selected));
  1213. menu_bar->add_child(extra_options);
  1214. extra_options->get_popup()->add_item(TTR("Force Push"), EXTRA_OPTION_FORCE_PUSH);
  1215. extra_options->get_popup()->add_separator();
  1216. extra_options->get_popup()->add_item(TTR("Create New Branch"), EXTRA_OPTION_CREATE_BRANCH);
  1217. extra_options_remove_branch_list = memnew(PopupMenu);
  1218. extra_options_remove_branch_list->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_branch_remove_confirm));
  1219. extra_options_remove_branch_list->set_name("Remove Branch");
  1220. extra_options->get_popup()->add_child(extra_options_remove_branch_list);
  1221. extra_options->get_popup()->add_submenu_item(TTR("Remove Branch"), "Remove Branch");
  1222. extra_options->get_popup()->add_separator();
  1223. extra_options->get_popup()->add_item(TTR("Create New Remote"), EXTRA_OPTION_CREATE_REMOTE);
  1224. extra_options_remove_remote_list = memnew(PopupMenu);
  1225. extra_options_remove_remote_list->connect(SNAME("id_pressed"), callable_mp(this, &VersionControlEditorPlugin::_popup_remote_remove_confirm));
  1226. extra_options_remove_remote_list->set_name("Remove Remote");
  1227. extra_options->get_popup()->add_child(extra_options_remove_remote_list);
  1228. extra_options->get_popup()->add_submenu_item(TTR("Remove Remote"), "Remove Remote");
  1229. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_NEW] = TTR("New");
  1230. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = TTR("Modified");
  1231. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_RENAMED] = TTR("Renamed");
  1232. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_DELETED] = TTR("Deleted");
  1233. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = TTR("Typechange");
  1234. change_type_to_strings[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = TTR("Unmerged");
  1235. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_NEW] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("success_color"), SNAME("Editor"));
  1236. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1237. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_RENAMED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1238. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_DELETED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), SNAME("Editor"));
  1239. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("font_color"), SNAME("Editor"));
  1240. change_type_to_color[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("warning_color"), SNAME("Editor"));
  1241. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_NEW] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons"));
  1242. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_MODIFIED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1243. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_RENAMED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1244. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_TYPECHANGE] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1245. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_DELETED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  1246. change_type_to_icon[EditorVCSInterface::CHANGE_TYPE_UNMERGED] = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons"));
  1247. version_control_dock = memnew(VBoxContainer);
  1248. version_control_dock->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1249. version_control_dock->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  1250. version_control_dock->hide();
  1251. HBoxContainer *diff_heading = memnew(HBoxContainer);
  1252. diff_heading->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1253. diff_heading->set_tooltip_text(TTR("View file diffs before committing them to the latest version"));
  1254. version_control_dock->add_child(diff_heading);
  1255. diff_title = memnew(Label);
  1256. diff_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1257. diff_heading->add_child(diff_title);
  1258. Label *view = memnew(Label);
  1259. view->set_text(TTR("View:"));
  1260. diff_heading->add_child(view);
  1261. diff_view_type_select = memnew(OptionButton);
  1262. diff_view_type_select->add_item(TTR("Split"), DIFF_VIEW_TYPE_SPLIT);
  1263. diff_view_type_select->add_item(TTR("Unified"), DIFF_VIEW_TYPE_UNIFIED);
  1264. diff_view_type_select->connect(SNAME("item_selected"), callable_mp(this, &VersionControlEditorPlugin::_display_diff));
  1265. diff_heading->add_child(diff_view_type_select);
  1266. diff = memnew(RichTextLabel);
  1267. diff->set_h_size_flags(TextEdit::SIZE_EXPAND_FILL);
  1268. diff->set_v_size_flags(TextEdit::SIZE_EXPAND_FILL);
  1269. diff->set_use_bbcode(true);
  1270. diff->set_selection_enabled(true);
  1271. version_control_dock->add_child(diff);
  1272. _update_set_up_warning("");
  1273. }
  1274. VersionControlEditorPlugin::~VersionControlEditorPlugin() {
  1275. shut_down();
  1276. memdelete(version_commit_dock);
  1277. memdelete(version_control_dock);
  1278. memdelete(version_control_actions);
  1279. }