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