editor_vcs_interface.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**************************************************************************/
  2. /* editor_vcs_interface.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor_vcs_interface.h"
  31. #include "editor_node.h"
  32. #define UNIMPLEMENTED() ERR_PRINT(vformat("Unimplemented virtual function in EditorVCSInterface based plugin: %s", __func__))
  33. EditorVCSInterface *EditorVCSInterface::singleton = nullptr;
  34. void EditorVCSInterface::popup_error(String p_msg) {
  35. // TRANSLATORS: %s refers to the name of a version control system (e.g. "Git").
  36. EditorNode::get_singleton()->show_warning(p_msg.strip_edges(), vformat(TTR("%s Error"), get_vcs_name()));
  37. }
  38. bool EditorVCSInterface::initialize(String p_project_path) {
  39. bool result = false;
  40. if (!GDVIRTUAL_CALL(_initialize, p_project_path, result)) {
  41. UNIMPLEMENTED();
  42. return false;
  43. }
  44. return result;
  45. }
  46. void EditorVCSInterface::set_credentials(String p_username, String p_password, String p_ssh_public_key, String p_ssh_private_key, String p_ssh_passphrase) {
  47. if (!GDVIRTUAL_CALL(_set_credentials, p_username, p_password, p_ssh_public_key, p_ssh_private_key, p_ssh_passphrase)) {
  48. UNIMPLEMENTED();
  49. }
  50. }
  51. List<String> EditorVCSInterface::get_remotes() {
  52. TypedArray<String> result;
  53. if (!GDVIRTUAL_CALL(_get_remotes, result)) {
  54. UNIMPLEMENTED();
  55. return {};
  56. }
  57. List<String> remotes;
  58. for (int i = 0; i < result.size(); i++) {
  59. remotes.push_back(result[i]);
  60. }
  61. return remotes;
  62. }
  63. List<EditorVCSInterface::StatusFile> EditorVCSInterface::get_modified_files_data() {
  64. TypedArray<Dictionary> result;
  65. if (!GDVIRTUAL_CALL(_get_modified_files_data, result)) {
  66. UNIMPLEMENTED();
  67. return {};
  68. }
  69. List<EditorVCSInterface::StatusFile> status_files;
  70. for (int i = 0; i < result.size(); i++) {
  71. status_files.push_back(_convert_status_file(result[i]));
  72. }
  73. return status_files;
  74. }
  75. void EditorVCSInterface::stage_file(String p_file_path) {
  76. if (!GDVIRTUAL_CALL(_stage_file, p_file_path)) {
  77. UNIMPLEMENTED();
  78. }
  79. }
  80. void EditorVCSInterface::unstage_file(String p_file_path) {
  81. if (!GDVIRTUAL_CALL(_unstage_file, p_file_path)) {
  82. UNIMPLEMENTED();
  83. }
  84. }
  85. void EditorVCSInterface::discard_file(String p_file_path) {
  86. if (!GDVIRTUAL_CALL(_discard_file, p_file_path)) {
  87. UNIMPLEMENTED();
  88. }
  89. }
  90. void EditorVCSInterface::commit(String p_msg) {
  91. if (!GDVIRTUAL_CALL(_commit, p_msg)) {
  92. UNIMPLEMENTED();
  93. }
  94. }
  95. List<EditorVCSInterface::DiffFile> EditorVCSInterface::get_diff(String p_identifier, TreeArea p_area) {
  96. TypedArray<Dictionary> result;
  97. if (!GDVIRTUAL_CALL(_get_diff, p_identifier, int(p_area), result)) {
  98. UNIMPLEMENTED();
  99. return {};
  100. }
  101. List<DiffFile> diff_files;
  102. for (int i = 0; i < result.size(); i++) {
  103. diff_files.push_back(_convert_diff_file(result[i]));
  104. }
  105. return diff_files;
  106. }
  107. List<EditorVCSInterface::Commit> EditorVCSInterface::get_previous_commits(int p_max_commits) {
  108. TypedArray<Dictionary> result;
  109. if (!GDVIRTUAL_CALL(_get_previous_commits, p_max_commits, result)) {
  110. UNIMPLEMENTED();
  111. return {};
  112. }
  113. List<EditorVCSInterface::Commit> commits;
  114. for (int i = 0; i < result.size(); i++) {
  115. commits.push_back(_convert_commit(result[i]));
  116. }
  117. return commits;
  118. }
  119. List<String> EditorVCSInterface::get_branch_list() {
  120. TypedArray<String> result;
  121. if (!GDVIRTUAL_CALL(_get_branch_list, result)) {
  122. UNIMPLEMENTED();
  123. return {};
  124. }
  125. List<String> branch_list;
  126. for (int i = 0; i < result.size(); i++) {
  127. branch_list.push_back(result[i]);
  128. }
  129. return branch_list;
  130. }
  131. void EditorVCSInterface::create_branch(String p_branch_name) {
  132. if (!GDVIRTUAL_CALL(_create_branch, p_branch_name)) {
  133. UNIMPLEMENTED();
  134. }
  135. }
  136. void EditorVCSInterface::create_remote(String p_remote_name, String p_remote_url) {
  137. if (!GDVIRTUAL_CALL(_create_remote, p_remote_name, p_remote_url)) {
  138. UNIMPLEMENTED();
  139. }
  140. }
  141. void EditorVCSInterface::remove_branch(String p_branch_name) {
  142. if (!GDVIRTUAL_CALL(_remove_branch, p_branch_name)) {
  143. UNIMPLEMENTED();
  144. }
  145. }
  146. void EditorVCSInterface::remove_remote(String p_remote_name) {
  147. if (!GDVIRTUAL_CALL(_remove_remote, p_remote_name)) {
  148. UNIMPLEMENTED();
  149. }
  150. }
  151. String EditorVCSInterface::get_current_branch_name() {
  152. String result;
  153. if (!GDVIRTUAL_CALL(_get_current_branch_name, result)) {
  154. UNIMPLEMENTED();
  155. return "";
  156. }
  157. return result;
  158. }
  159. bool EditorVCSInterface::checkout_branch(String p_branch_name) {
  160. bool result = false;
  161. if (!GDVIRTUAL_CALL(_checkout_branch, p_branch_name, result)) {
  162. UNIMPLEMENTED();
  163. }
  164. return result;
  165. }
  166. void EditorVCSInterface::pull(String p_remote) {
  167. if (!GDVIRTUAL_CALL(_pull, p_remote)) {
  168. UNIMPLEMENTED();
  169. }
  170. }
  171. void EditorVCSInterface::push(String p_remote, bool p_force) {
  172. if (!GDVIRTUAL_CALL(_push, p_remote, p_force)) {
  173. UNIMPLEMENTED();
  174. }
  175. }
  176. void EditorVCSInterface::fetch(String p_remote) {
  177. if (!GDVIRTUAL_CALL(_fetch, p_remote)) {
  178. UNIMPLEMENTED();
  179. }
  180. }
  181. List<EditorVCSInterface::DiffHunk> EditorVCSInterface::get_line_diff(String p_file_path, String p_text) {
  182. TypedArray<Dictionary> result;
  183. if (!GDVIRTUAL_CALL(_get_line_diff, p_file_path, p_text, result)) {
  184. UNIMPLEMENTED();
  185. return {};
  186. }
  187. List<DiffHunk> diff_hunks;
  188. for (int i = 0; i < result.size(); i++) {
  189. diff_hunks.push_back(_convert_diff_hunk(result[i]));
  190. }
  191. return diff_hunks;
  192. }
  193. bool EditorVCSInterface::shut_down() {
  194. bool result = false;
  195. if (!GDVIRTUAL_CALL(_shut_down, result)) {
  196. UNIMPLEMENTED();
  197. return false;
  198. }
  199. return result;
  200. }
  201. String EditorVCSInterface::get_vcs_name() {
  202. String result;
  203. if (!GDVIRTUAL_CALL(_get_vcs_name, result)) {
  204. UNIMPLEMENTED();
  205. return {};
  206. }
  207. return result;
  208. }
  209. Dictionary EditorVCSInterface::create_diff_line(int p_new_line_no, int p_old_line_no, String p_content, String p_status) {
  210. Dictionary diff_line;
  211. diff_line["new_line_no"] = p_new_line_no;
  212. diff_line["old_line_no"] = p_old_line_no;
  213. diff_line["content"] = p_content;
  214. diff_line["status"] = p_status;
  215. return diff_line;
  216. }
  217. Dictionary EditorVCSInterface::create_diff_hunk(int p_old_start, int p_new_start, int p_old_lines, int p_new_lines) {
  218. Dictionary diff_hunk;
  219. diff_hunk["new_lines"] = p_new_lines;
  220. diff_hunk["old_lines"] = p_old_lines;
  221. diff_hunk["new_start"] = p_new_start;
  222. diff_hunk["old_start"] = p_old_start;
  223. diff_hunk["diff_lines"] = TypedArray<Dictionary>();
  224. return diff_hunk;
  225. }
  226. Dictionary EditorVCSInterface::add_line_diffs_into_diff_hunk(Dictionary p_diff_hunk, TypedArray<Dictionary> p_line_diffs) {
  227. p_diff_hunk["diff_lines"] = p_line_diffs;
  228. return p_diff_hunk;
  229. }
  230. Dictionary EditorVCSInterface::create_diff_file(String p_new_file, String p_old_file) {
  231. Dictionary file_diff;
  232. file_diff["new_file"] = p_new_file;
  233. file_diff["old_file"] = p_old_file;
  234. file_diff["diff_hunks"] = TypedArray<Dictionary>();
  235. return file_diff;
  236. }
  237. Dictionary EditorVCSInterface::create_commit(String p_msg, String p_author, String p_id, int64_t p_unix_timestamp, int64_t p_offset_minutes) {
  238. Dictionary commit_info;
  239. commit_info["message"] = p_msg;
  240. commit_info["author"] = p_author;
  241. commit_info["unix_timestamp"] = p_unix_timestamp;
  242. commit_info["offset_minutes"] = p_offset_minutes;
  243. commit_info["id"] = p_id;
  244. return commit_info;
  245. }
  246. Dictionary EditorVCSInterface::add_diff_hunks_into_diff_file(Dictionary p_diff_file, TypedArray<Dictionary> p_diff_hunks) {
  247. p_diff_file["diff_hunks"] = p_diff_hunks;
  248. return p_diff_file;
  249. }
  250. Dictionary EditorVCSInterface::create_status_file(String p_file_path, ChangeType p_change, TreeArea p_area) {
  251. Dictionary sf;
  252. sf["file_path"] = p_file_path;
  253. sf["change_type"] = p_change;
  254. sf["area"] = p_area;
  255. return sf;
  256. }
  257. EditorVCSInterface::DiffLine EditorVCSInterface::_convert_diff_line(Dictionary p_diff_line) {
  258. DiffLine d;
  259. d.new_line_no = p_diff_line["new_line_no"];
  260. d.old_line_no = p_diff_line["old_line_no"];
  261. d.content = p_diff_line["content"];
  262. d.status = p_diff_line["status"];
  263. return d;
  264. }
  265. EditorVCSInterface::DiffHunk EditorVCSInterface::_convert_diff_hunk(Dictionary p_diff_hunk) {
  266. DiffHunk dh;
  267. dh.new_lines = p_diff_hunk["new_lines"];
  268. dh.old_lines = p_diff_hunk["old_lines"];
  269. dh.new_start = p_diff_hunk["new_start"];
  270. dh.old_start = p_diff_hunk["old_start"];
  271. TypedArray<Dictionary> diff_lines = p_diff_hunk["diff_lines"];
  272. for (int i = 0; i < diff_lines.size(); i++) {
  273. DiffLine dl = _convert_diff_line(diff_lines[i]);
  274. dh.diff_lines.push_back(dl);
  275. }
  276. return dh;
  277. }
  278. EditorVCSInterface::DiffFile EditorVCSInterface::_convert_diff_file(Dictionary p_diff_file) {
  279. DiffFile df;
  280. df.new_file = p_diff_file["new_file"];
  281. df.old_file = p_diff_file["old_file"];
  282. TypedArray<Dictionary> diff_hunks = p_diff_file["diff_hunks"];
  283. for (int i = 0; i < diff_hunks.size(); i++) {
  284. DiffHunk dh = _convert_diff_hunk(diff_hunks[i]);
  285. df.diff_hunks.push_back(dh);
  286. }
  287. return df;
  288. }
  289. EditorVCSInterface::Commit EditorVCSInterface::_convert_commit(Dictionary p_commit) {
  290. EditorVCSInterface::Commit c;
  291. c.msg = p_commit["message"];
  292. c.author = p_commit["author"];
  293. c.unix_timestamp = p_commit["unix_timestamp"];
  294. c.offset_minutes = p_commit["offset_minutes"];
  295. c.id = p_commit["id"];
  296. return c;
  297. }
  298. EditorVCSInterface::StatusFile EditorVCSInterface::_convert_status_file(Dictionary p_status_file) {
  299. StatusFile sf;
  300. sf.file_path = p_status_file["file_path"];
  301. sf.change_type = (ChangeType)(int)p_status_file["change_type"];
  302. sf.area = (TreeArea)(int)p_status_file["area"];
  303. return sf;
  304. }
  305. void EditorVCSInterface::_bind_methods() {
  306. // Proxy end points that implement the VCS specific operations that the editor demands.
  307. GDVIRTUAL_BIND(_initialize, "project_path");
  308. GDVIRTUAL_BIND(_set_credentials, "username", "password", "ssh_public_key_path", "ssh_private_key_path", "ssh_passphrase");
  309. GDVIRTUAL_BIND(_get_modified_files_data);
  310. GDVIRTUAL_BIND(_stage_file, "file_path");
  311. GDVIRTUAL_BIND(_unstage_file, "file_path");
  312. GDVIRTUAL_BIND(_discard_file, "file_path");
  313. GDVIRTUAL_BIND(_commit, "msg");
  314. GDVIRTUAL_BIND(_get_diff, "identifier", "area");
  315. GDVIRTUAL_BIND(_shut_down);
  316. GDVIRTUAL_BIND(_get_vcs_name);
  317. GDVIRTUAL_BIND(_get_previous_commits, "max_commits");
  318. GDVIRTUAL_BIND(_get_branch_list);
  319. GDVIRTUAL_BIND(_get_remotes);
  320. GDVIRTUAL_BIND(_create_branch, "branch_name");
  321. GDVIRTUAL_BIND(_remove_branch, "branch_name");
  322. GDVIRTUAL_BIND(_create_remote, "remote_name", "remote_url");
  323. GDVIRTUAL_BIND(_remove_remote, "remote_name");
  324. GDVIRTUAL_BIND(_get_current_branch_name);
  325. GDVIRTUAL_BIND(_checkout_branch, "branch_name");
  326. GDVIRTUAL_BIND(_pull, "remote");
  327. GDVIRTUAL_BIND(_push, "remote", "force");
  328. GDVIRTUAL_BIND(_fetch, "remote");
  329. GDVIRTUAL_BIND(_get_line_diff, "file_path", "text");
  330. ClassDB::bind_method(D_METHOD("create_diff_line", "new_line_no", "old_line_no", "content", "status"), &EditorVCSInterface::create_diff_line);
  331. ClassDB::bind_method(D_METHOD("create_diff_hunk", "old_start", "new_start", "old_lines", "new_lines"), &EditorVCSInterface::create_diff_hunk);
  332. ClassDB::bind_method(D_METHOD("create_diff_file", "new_file", "old_file"), &EditorVCSInterface::create_diff_file);
  333. ClassDB::bind_method(D_METHOD("create_commit", "msg", "author", "id", "unix_timestamp", "offset_minutes"), &EditorVCSInterface::create_commit);
  334. ClassDB::bind_method(D_METHOD("create_status_file", "file_path", "change_type", "area"), &EditorVCSInterface::create_status_file);
  335. ClassDB::bind_method(D_METHOD("add_diff_hunks_into_diff_file", "diff_file", "diff_hunks"), &EditorVCSInterface::add_diff_hunks_into_diff_file);
  336. ClassDB::bind_method(D_METHOD("add_line_diffs_into_diff_hunk", "diff_hunk", "line_diffs"), &EditorVCSInterface::add_line_diffs_into_diff_hunk);
  337. ClassDB::bind_method(D_METHOD("popup_error", "msg"), &EditorVCSInterface::popup_error);
  338. BIND_ENUM_CONSTANT(CHANGE_TYPE_NEW);
  339. BIND_ENUM_CONSTANT(CHANGE_TYPE_MODIFIED);
  340. BIND_ENUM_CONSTANT(CHANGE_TYPE_RENAMED);
  341. BIND_ENUM_CONSTANT(CHANGE_TYPE_DELETED);
  342. BIND_ENUM_CONSTANT(CHANGE_TYPE_TYPECHANGE);
  343. BIND_ENUM_CONSTANT(CHANGE_TYPE_UNMERGED);
  344. BIND_ENUM_CONSTANT(TREE_AREA_COMMIT);
  345. BIND_ENUM_CONSTANT(TREE_AREA_STAGED);
  346. BIND_ENUM_CONSTANT(TREE_AREA_UNSTAGED);
  347. }
  348. EditorVCSInterface *EditorVCSInterface::get_singleton() {
  349. return singleton;
  350. }
  351. void EditorVCSInterface::set_singleton(EditorVCSInterface *p_singleton) {
  352. singleton = p_singleton;
  353. }
  354. void EditorVCSInterface::create_vcs_metadata_files(VCSMetadata p_vcs_metadata_type, String &p_dir) {
  355. if (p_vcs_metadata_type == VCSMetadata::GIT) {
  356. Ref<FileAccess> f = FileAccess::open(p_dir.path_join(".gitignore"), FileAccess::WRITE);
  357. if (f.is_null()) {
  358. ERR_FAIL_MSG("Couldn't create .gitignore in project path.");
  359. } else {
  360. f->store_line("# Godot 4+ specific ignores");
  361. f->store_line(".godot/");
  362. }
  363. f = FileAccess::open(p_dir.path_join(".gitattributes"), FileAccess::WRITE);
  364. if (f.is_null()) {
  365. ERR_FAIL_MSG("Couldn't create .gitattributes in project path.");
  366. } else {
  367. f->store_line("# Normalize EOL for all files that Git considers text files.");
  368. f->store_line("* text=auto eol=lf");
  369. }
  370. }
  371. }