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