editor_vcs_interface.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* editor_vcs_interface.h */
  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. #pragma once
  31. #include "core/object/gdvirtual.gen.inc"
  32. #include "core/string/ustring.h"
  33. #include "core/variant/typed_array.h"
  34. class EditorVCSInterface : public Object {
  35. GDCLASS(EditorVCSInterface, Object)
  36. public:
  37. enum ChangeType {
  38. CHANGE_TYPE_NEW = 0,
  39. CHANGE_TYPE_MODIFIED = 1,
  40. CHANGE_TYPE_RENAMED = 2,
  41. CHANGE_TYPE_DELETED = 3,
  42. CHANGE_TYPE_TYPECHANGE = 4,
  43. CHANGE_TYPE_UNMERGED = 5
  44. };
  45. enum TreeArea {
  46. TREE_AREA_COMMIT = 0,
  47. TREE_AREA_STAGED = 1,
  48. TREE_AREA_UNSTAGED = 2
  49. };
  50. struct DiffLine {
  51. int new_line_no;
  52. int old_line_no;
  53. String content;
  54. String status;
  55. String old_text;
  56. String new_text;
  57. };
  58. struct DiffHunk {
  59. int new_start;
  60. int old_start;
  61. int new_lines;
  62. int old_lines;
  63. List<DiffLine> diff_lines;
  64. };
  65. struct DiffFile {
  66. String new_file;
  67. String old_file;
  68. List<DiffHunk> diff_hunks;
  69. };
  70. struct Commit {
  71. String author;
  72. String msg;
  73. String id;
  74. int64_t unix_timestamp;
  75. int64_t offset_minutes;
  76. };
  77. struct StatusFile {
  78. TreeArea area;
  79. ChangeType change_type;
  80. String file_path;
  81. };
  82. protected:
  83. static EditorVCSInterface *singleton;
  84. static void _bind_methods();
  85. DiffLine _convert_diff_line(const Dictionary &p_diff_line);
  86. DiffHunk _convert_diff_hunk(const Dictionary &p_diff_hunk);
  87. DiffFile _convert_diff_file(const Dictionary &p_diff_file);
  88. Commit _convert_commit(const Dictionary &p_commit);
  89. StatusFile _convert_status_file(const Dictionary &p_status_file);
  90. // Proxy endpoints for extensions to implement
  91. GDVIRTUAL1R_REQUIRED(bool, _initialize, String);
  92. GDVIRTUAL5_REQUIRED(_set_credentials, String, String, String, String, String);
  93. GDVIRTUAL0R_REQUIRED(TypedArray<Dictionary>, _get_modified_files_data);
  94. GDVIRTUAL1_REQUIRED(_stage_file, String);
  95. GDVIRTUAL1_REQUIRED(_unstage_file, String);
  96. GDVIRTUAL1_REQUIRED(_discard_file, String);
  97. GDVIRTUAL1_REQUIRED(_commit, String);
  98. GDVIRTUAL2R_REQUIRED(TypedArray<Dictionary>, _get_diff, String, int);
  99. GDVIRTUAL0R_REQUIRED(bool, _shut_down);
  100. GDVIRTUAL0R_REQUIRED(String, _get_vcs_name);
  101. GDVIRTUAL1R_REQUIRED(TypedArray<Dictionary>, _get_previous_commits, int);
  102. GDVIRTUAL0R_REQUIRED(TypedArray<String>, _get_branch_list);
  103. GDVIRTUAL0R_REQUIRED(TypedArray<String>, _get_remotes);
  104. GDVIRTUAL1_REQUIRED(_create_branch, String);
  105. GDVIRTUAL1_REQUIRED(_remove_branch, String);
  106. GDVIRTUAL2_REQUIRED(_create_remote, String, String);
  107. GDVIRTUAL1_REQUIRED(_remove_remote, String);
  108. GDVIRTUAL0R_REQUIRED(String, _get_current_branch_name);
  109. GDVIRTUAL1R_REQUIRED(bool, _checkout_branch, String);
  110. GDVIRTUAL1_REQUIRED(_pull, String);
  111. GDVIRTUAL2_REQUIRED(_push, String, bool);
  112. GDVIRTUAL1_REQUIRED(_fetch, String);
  113. GDVIRTUAL2R_REQUIRED(TypedArray<Dictionary>, _get_line_diff, String, String);
  114. public:
  115. static EditorVCSInterface *get_singleton();
  116. static void set_singleton(EditorVCSInterface *p_singleton);
  117. enum class VCSMetadata {
  118. NONE,
  119. GIT,
  120. };
  121. static void create_vcs_metadata_files(VCSMetadata p_vcs_metadata_type, String &p_dir);
  122. // Proxies to the editor for use
  123. bool initialize(const String &p_project_path);
  124. void set_credentials(const String &p_username, const String &p_password, const String &p_ssh_public_key_path, const String &p_ssh_private_key_path, const String &p_ssh_passphrase);
  125. List<StatusFile> get_modified_files_data();
  126. void stage_file(const String &p_file_path);
  127. void unstage_file(const String &p_file_path);
  128. void discard_file(const String &p_file_path);
  129. void commit(const String &p_msg);
  130. List<DiffFile> get_diff(const String &p_identifier, TreeArea p_area);
  131. bool shut_down();
  132. String get_vcs_name();
  133. List<Commit> get_previous_commits(int p_max_commits);
  134. List<String> get_branch_list();
  135. List<String> get_remotes();
  136. void create_branch(const String &p_branch_name);
  137. void remove_branch(const String &p_branch_name);
  138. void create_remote(const String &p_remote_name, const String &p_remote_url);
  139. void remove_remote(const String &p_remote_name);
  140. String get_current_branch_name();
  141. bool checkout_branch(const String &p_branch_name);
  142. void pull(const String &p_remote);
  143. void push(const String &p_remote, bool p_force);
  144. void fetch(const String &p_remote);
  145. List<DiffHunk> get_line_diff(const String &p_file_path, const String &p_text);
  146. // Helper functions to create and convert Dictionary into data structures
  147. Dictionary create_diff_line(int p_new_line_no, int p_old_line_no, const String &p_content, const String &p_status);
  148. Dictionary create_diff_hunk(int p_old_start, int p_new_start, int p_old_lines, int p_new_lines);
  149. Dictionary create_diff_file(const String &p_new_file, const String &p_old_file);
  150. Dictionary create_commit(const String &p_msg, const String &p_author, const String &p_id, int64_t p_unix_timestamp, int64_t p_offset_minutes);
  151. Dictionary create_status_file(const String &p_file_path, ChangeType p_change, TreeArea p_area);
  152. Dictionary add_line_diffs_into_diff_hunk(Dictionary p_diff_hunk, TypedArray<Dictionary> p_line_diffs);
  153. Dictionary add_diff_hunks_into_diff_file(Dictionary p_diff_file, TypedArray<Dictionary> p_diff_hunks);
  154. void popup_error(const String &p_msg);
  155. };
  156. VARIANT_ENUM_CAST(EditorVCSInterface::ChangeType);
  157. VARIANT_ENUM_CAST(EditorVCSInterface::TreeArea);