version_control_editor_plugin.cpp 69 KB

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