editor_vcs_interface.cpp 14 KB

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