editor_vcs_interface.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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(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(String p_project_path) {
  38. return call("_initialize", p_project_path);
  39. }
  40. void EditorVCSInterface::set_credentials(String p_username, String p_password, String p_ssh_public_key, String p_ssh_private_key, String p_ssh_passphrase) {
  41. call("_set_credentials", p_username, p_password, p_ssh_public_key, p_ssh_private_key, p_ssh_passphrase);
  42. }
  43. List<String> EditorVCSInterface::get_remotes() {
  44. List<String> remotes;
  45. Array result = call("_get_remotes");
  46. for (int i = 0; i < result.size(); i++) {
  47. remotes.push_back(result[i]);
  48. }
  49. return remotes;
  50. }
  51. List<EditorVCSInterface::StatusFile> EditorVCSInterface::get_modified_files_data() {
  52. List<EditorVCSInterface::StatusFile> status_files;
  53. Array result = call("_get_modified_files_data");
  54. for (int i = 0; i < result.size(); i++) {
  55. status_files.push_back(_convert_status_file(result[i]));
  56. }
  57. return status_files;
  58. }
  59. void EditorVCSInterface::stage_file(String p_file_path) {
  60. call("_stage_file", p_file_path);
  61. }
  62. void EditorVCSInterface::unstage_file(String p_file_path) {
  63. call("_unstage_file", p_file_path);
  64. }
  65. void EditorVCSInterface::discard_file(String p_file_path) {
  66. call("_discard_file", p_file_path);
  67. }
  68. void EditorVCSInterface::commit(String p_msg) {
  69. call("_commit", p_msg);
  70. }
  71. List<EditorVCSInterface::DiffFile> EditorVCSInterface::get_diff(String p_identifier, TreeArea p_area) {
  72. List<DiffFile> diff_files;
  73. Array result = call("_get_diff", p_identifier, p_area);
  74. for (int i = 0; i < result.size(); i++) {
  75. diff_files.push_back(_convert_diff_file(result[i]));
  76. }
  77. return diff_files;
  78. }
  79. List<EditorVCSInterface::Commit> EditorVCSInterface::get_previous_commits(int p_max_commits) {
  80. List<EditorVCSInterface::Commit> commits;
  81. Array result = call("_get_previous_commits", p_max_commits);
  82. for (int i = 0; i < result.size(); i++) {
  83. commits.push_back(_convert_commit(result[i]));
  84. }
  85. return commits;
  86. }
  87. List<String> EditorVCSInterface::get_branch_list() {
  88. List<String> branch_list;
  89. Array result = call("_get_branch_list");
  90. for (int i = 0; i < result.size(); i++) {
  91. branch_list.push_back(result[i]);
  92. }
  93. return branch_list;
  94. }
  95. void EditorVCSInterface::create_branch(String p_branch_name) {
  96. call("_create_branch", p_branch_name);
  97. }
  98. void EditorVCSInterface::create_remote(String p_remote_name, String p_remote_url) {
  99. call("_create_remote", p_remote_name, p_remote_url);
  100. }
  101. void EditorVCSInterface::remove_branch(String p_branch_name) {
  102. call("_remove_branch", p_branch_name);
  103. }
  104. void EditorVCSInterface::remove_remote(String p_remote_name) {
  105. call("_remove_remote", p_remote_name);
  106. }
  107. String EditorVCSInterface::get_current_branch_name() {
  108. return call("_get_current_branch_name");
  109. }
  110. bool EditorVCSInterface::checkout_branch(String p_branch_name) {
  111. return call("_checkout_branch", p_branch_name);
  112. }
  113. void EditorVCSInterface::pull(String p_remote) {
  114. call("_pull", p_remote);
  115. }
  116. void EditorVCSInterface::push(String p_remote, bool p_force) {
  117. call("_push", p_remote, p_force);
  118. }
  119. void EditorVCSInterface::fetch(String p_remote) {
  120. call("_fetch", p_remote);
  121. }
  122. List<EditorVCSInterface::DiffHunk> EditorVCSInterface::get_line_diff(String p_file_path, String p_text) {
  123. List<DiffHunk> diff_hunks;
  124. Array result = call("_get_line_diff", p_file_path, p_text);
  125. for (int i = 0; i < result.size(); i++) {
  126. diff_hunks.push_back(_convert_diff_hunk(result[i]));
  127. }
  128. return diff_hunks;
  129. }
  130. bool EditorVCSInterface::shut_down() {
  131. return call("_shut_down");
  132. }
  133. String EditorVCSInterface::get_vcs_name() {
  134. return call("_get_vcs_name");
  135. }
  136. Dictionary EditorVCSInterface::create_diff_line(int p_new_line_no, int p_old_line_no, String p_content, String p_status) {
  137. Dictionary diff_line;
  138. diff_line["new_line_no"] = p_new_line_no;
  139. diff_line["old_line_no"] = p_old_line_no;
  140. diff_line["content"] = p_content;
  141. diff_line["status"] = p_status;
  142. return diff_line;
  143. }
  144. Dictionary EditorVCSInterface::create_diff_hunk(int p_old_start, int p_new_start, int p_old_lines, int p_new_lines) {
  145. Dictionary diff_hunk;
  146. diff_hunk["new_lines"] = p_new_lines;
  147. diff_hunk["old_lines"] = p_old_lines;
  148. diff_hunk["new_start"] = p_new_start;
  149. diff_hunk["old_start"] = p_old_start;
  150. diff_hunk["diff_lines"] = Array();
  151. return diff_hunk;
  152. }
  153. Dictionary EditorVCSInterface::add_line_diffs_into_diff_hunk(Dictionary p_diff_hunk, Array p_line_diffs) {
  154. p_diff_hunk["diff_lines"] = p_line_diffs;
  155. return p_diff_hunk;
  156. }
  157. Dictionary EditorVCSInterface::create_diff_file(String p_new_file, String p_old_file) {
  158. Dictionary file_diff;
  159. file_diff["new_file"] = p_new_file;
  160. file_diff["old_file"] = p_old_file;
  161. file_diff["diff_hunks"] = Array();
  162. return file_diff;
  163. }
  164. Dictionary EditorVCSInterface::create_commit(String p_msg, String p_author, String p_id, int64_t p_unix_timestamp, int64_t p_offset_minutes) {
  165. Dictionary commit_info;
  166. commit_info["message"] = p_msg;
  167. commit_info["author"] = p_author;
  168. commit_info["unix_timestamp"] = p_unix_timestamp;
  169. commit_info["offset_minutes"] = p_offset_minutes;
  170. commit_info["id"] = p_id;
  171. return commit_info;
  172. }
  173. Dictionary EditorVCSInterface::add_diff_hunks_into_diff_file(Dictionary p_diff_file, Array p_diff_hunks) {
  174. p_diff_file["diff_hunks"] = p_diff_hunks;
  175. return p_diff_file;
  176. }
  177. Dictionary EditorVCSInterface::create_status_file(String p_file_path, ChangeType p_change, TreeArea p_area) {
  178. Dictionary sf;
  179. sf["file_path"] = p_file_path;
  180. sf["change_type"] = p_change;
  181. sf["area"] = p_area;
  182. return sf;
  183. }
  184. EditorVCSInterface::DiffLine EditorVCSInterface::_convert_diff_line(Dictionary p_diff_line) {
  185. DiffLine d;
  186. d.new_line_no = p_diff_line["new_line_no"];
  187. d.old_line_no = p_diff_line["old_line_no"];
  188. d.content = p_diff_line["content"];
  189. d.status = p_diff_line["status"];
  190. return d;
  191. }
  192. EditorVCSInterface::DiffHunk EditorVCSInterface::_convert_diff_hunk(Dictionary p_diff_hunk) {
  193. DiffHunk dh;
  194. dh.new_lines = p_diff_hunk["new_lines"];
  195. dh.old_lines = p_diff_hunk["old_lines"];
  196. dh.new_start = p_diff_hunk["new_start"];
  197. dh.old_start = p_diff_hunk["old_start"];
  198. Array diff_lines = p_diff_hunk["diff_lines"];
  199. for (int i = 0; i < diff_lines.size(); i++) {
  200. DiffLine dl = _convert_diff_line(diff_lines[i]);
  201. dh.diff_lines.push_back(dl);
  202. }
  203. return dh;
  204. }
  205. EditorVCSInterface::DiffFile EditorVCSInterface::_convert_diff_file(Dictionary p_diff_file) {
  206. DiffFile df;
  207. df.new_file = p_diff_file["new_file"];
  208. df.old_file = p_diff_file["old_file"];
  209. Array diff_hunks = p_diff_file["diff_hunks"];
  210. for (int i = 0; i < diff_hunks.size(); i++) {
  211. DiffHunk dh = _convert_diff_hunk(diff_hunks[i]);
  212. df.diff_hunks.push_back(dh);
  213. }
  214. return df;
  215. }
  216. EditorVCSInterface::Commit EditorVCSInterface::_convert_commit(Dictionary p_commit) {
  217. EditorVCSInterface::Commit c;
  218. c.msg = p_commit["message"];
  219. c.author = p_commit["author"];
  220. c.unix_timestamp = p_commit["unix_timestamp"];
  221. c.offset_minutes = p_commit["offset_minutes"];
  222. c.id = p_commit["id"];
  223. return c;
  224. }
  225. EditorVCSInterface::StatusFile EditorVCSInterface::_convert_status_file(Dictionary p_status_file) {
  226. StatusFile sf;
  227. sf.file_path = p_status_file["file_path"];
  228. sf.change_type = (ChangeType)(int)p_status_file["change_type"];
  229. sf.area = (TreeArea)(int)p_status_file["area"];
  230. return sf;
  231. }
  232. void EditorVCSInterface::_bind_methods() {
  233. // Proxy end points that implement the VCS specific operations that the editor demands.
  234. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_initialize", PropertyInfo(Variant::STRING, "project_path")));
  235. BIND_VMETHOD(MethodInfo("_set_credentials", PropertyInfo(Variant::STRING, "username"), PropertyInfo(Variant::STRING, "password"), PropertyInfo(Variant::STRING, "ssh_public_key_path"), PropertyInfo(Variant::STRING, "ssh_private_key_path"), PropertyInfo(Variant::STRING, "ssh_passphrase")));
  236. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_remotes"));
  237. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_vcs_name"));
  238. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_shut_down"));
  239. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_modified_files_data"));
  240. BIND_VMETHOD(MethodInfo("_commit", PropertyInfo(Variant::STRING, "msg")));
  241. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_diff", PropertyInfo(Variant::STRING, "identifier"), PropertyInfo(Variant::INT, "area")));
  242. BIND_VMETHOD(MethodInfo("_stage_file", PropertyInfo(Variant::STRING, "file_path")));
  243. BIND_VMETHOD(MethodInfo("_unstage_file", PropertyInfo(Variant::STRING, "file_path")));
  244. BIND_VMETHOD(MethodInfo("_discard_file", PropertyInfo(Variant::STRING, "file_path")));
  245. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_previous_commits", PropertyInfo(Variant::INT, "max_commits")));
  246. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_branch_list"));
  247. BIND_VMETHOD(MethodInfo("_create_branch", PropertyInfo(Variant::STRING, "branch_name")));
  248. BIND_VMETHOD(MethodInfo("_remove_branch", PropertyInfo(Variant::STRING, "branch_name")));
  249. BIND_VMETHOD(MethodInfo("_create_remote", PropertyInfo(Variant::STRING, "remote_name"), PropertyInfo(Variant::STRING, "remote_url")));
  250. BIND_VMETHOD(MethodInfo("_remove_remote", PropertyInfo(Variant::STRING, "remote_name")));
  251. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_current_branch_name"));
  252. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_checkout_branch", PropertyInfo(Variant::STRING, "branch_name")));
  253. BIND_VMETHOD(MethodInfo("_push", PropertyInfo(Variant::STRING, "remote"), PropertyInfo(Variant::BOOL, "force")));
  254. BIND_VMETHOD(MethodInfo("_pull", PropertyInfo(Variant::STRING, "remote")));
  255. BIND_VMETHOD(MethodInfo("_fetch", PropertyInfo(Variant::STRING, "remote")));
  256. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_line_diff", PropertyInfo(Variant::STRING, "file_path"), PropertyInfo(Variant::STRING, "text")));
  257. ClassDB::bind_method(D_METHOD("create_diff_line", "new_line_no", "old_line_no", "content", "status"), &EditorVCSInterface::create_diff_line);
  258. ClassDB::bind_method(D_METHOD("create_diff_hunk", "old_start", "new_start", "old_lines", "new_lines"), &EditorVCSInterface::create_diff_hunk);
  259. ClassDB::bind_method(D_METHOD("create_diff_file", "new_file", "old_file"), &EditorVCSInterface::create_diff_file);
  260. ClassDB::bind_method(D_METHOD("create_commit", "msg", "author", "id", "unix_timestamp", "offset_minutes"), &EditorVCSInterface::create_commit);
  261. ClassDB::bind_method(D_METHOD("create_status_file", "file_path", "change_type", "area"), &EditorVCSInterface::create_status_file);
  262. ClassDB::bind_method(D_METHOD("add_diff_hunks_into_diff_file", "diff_file", "diff_hunks"), &EditorVCSInterface::add_diff_hunks_into_diff_file);
  263. ClassDB::bind_method(D_METHOD("add_line_diffs_into_diff_hunk", "diff_hunk", "line_diffs"), &EditorVCSInterface::add_line_diffs_into_diff_hunk);
  264. ClassDB::bind_method(D_METHOD("popup_error", "msg"), &EditorVCSInterface::popup_error);
  265. BIND_ENUM_CONSTANT(CHANGE_TYPE_NEW);
  266. BIND_ENUM_CONSTANT(CHANGE_TYPE_MODIFIED);
  267. BIND_ENUM_CONSTANT(CHANGE_TYPE_RENAMED);
  268. BIND_ENUM_CONSTANT(CHANGE_TYPE_DELETED);
  269. BIND_ENUM_CONSTANT(CHANGE_TYPE_TYPECHANGE);
  270. BIND_ENUM_CONSTANT(CHANGE_TYPE_UNMERGED);
  271. BIND_ENUM_CONSTANT(TREE_AREA_COMMIT);
  272. BIND_ENUM_CONSTANT(TREE_AREA_STAGED);
  273. BIND_ENUM_CONSTANT(TREE_AREA_UNSTAGED);
  274. }
  275. EditorVCSInterface *EditorVCSInterface::get_singleton() {
  276. return singleton;
  277. }
  278. void EditorVCSInterface::set_singleton(EditorVCSInterface *p_singleton) {
  279. singleton = p_singleton;
  280. }