Path: blob/master/editor/version_control/editor_vcs_interface.cpp
9903 views
/**************************************************************************/1/* editor_vcs_interface.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_vcs_interface.h"3132#include "editor/editor_node.h"3334EditorVCSInterface *EditorVCSInterface::singleton = nullptr;3536void EditorVCSInterface::popup_error(const String &p_msg) {37// TRANSLATORS: %s refers to the name of a version control system (e.g. "Git").38EditorNode::get_singleton()->show_warning(p_msg.strip_edges(), vformat(TTR("%s Error"), get_vcs_name()));39}4041bool EditorVCSInterface::initialize(const String &p_project_path) {42bool result = false;43GDVIRTUAL_CALL(_initialize, p_project_path, result);44return result;45}4647void 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) {48GDVIRTUAL_CALL(_set_credentials, p_username, p_password, p_ssh_public_key, p_ssh_private_key, p_ssh_passphrase);49}5051List<String> EditorVCSInterface::get_remotes() {52TypedArray<String> result;53if (!GDVIRTUAL_CALL(_get_remotes, result)) {54return {};55}5657List<String> remotes;58for (int i = 0; i < result.size(); i++) {59remotes.push_back(result[i]);60}61return remotes;62}6364List<EditorVCSInterface::StatusFile> EditorVCSInterface::get_modified_files_data() {65TypedArray<Dictionary> result;66if (!GDVIRTUAL_CALL(_get_modified_files_data, result)) {67return {};68}6970List<EditorVCSInterface::StatusFile> status_files;71for (int i = 0; i < result.size(); i++) {72status_files.push_back(_convert_status_file(result[i]));73}74return status_files;75}7677void EditorVCSInterface::stage_file(const String &p_file_path) {78GDVIRTUAL_CALL(_stage_file, p_file_path);79}8081void EditorVCSInterface::unstage_file(const String &p_file_path) {82GDVIRTUAL_CALL(_unstage_file, p_file_path);83}8485void EditorVCSInterface::discard_file(const String &p_file_path) {86GDVIRTUAL_CALL(_discard_file, p_file_path);87}8889void EditorVCSInterface::commit(const String &p_msg) {90GDVIRTUAL_CALL(_commit, p_msg);91}9293List<EditorVCSInterface::DiffFile> EditorVCSInterface::get_diff(const String &p_identifier, TreeArea p_area) {94TypedArray<Dictionary> result;95if (!GDVIRTUAL_CALL(_get_diff, p_identifier, int(p_area), result)) {96return {};97}9899List<DiffFile> diff_files;100for (int i = 0; i < result.size(); i++) {101diff_files.push_back(_convert_diff_file(result[i]));102}103return diff_files;104}105106List<EditorVCSInterface::Commit> EditorVCSInterface::get_previous_commits(int p_max_commits) {107TypedArray<Dictionary> result;108if (!GDVIRTUAL_CALL(_get_previous_commits, p_max_commits, result)) {109return {};110}111112List<EditorVCSInterface::Commit> commits;113for (int i = 0; i < result.size(); i++) {114commits.push_back(_convert_commit(result[i]));115}116return commits;117}118119List<String> EditorVCSInterface::get_branch_list() {120TypedArray<String> result;121if (!GDVIRTUAL_CALL(_get_branch_list, result)) {122return {};123}124125List<String> branch_list;126for (int i = 0; i < result.size(); i++) {127branch_list.push_back(result[i]);128}129return branch_list;130}131132void EditorVCSInterface::create_branch(const String &p_branch_name) {133GDVIRTUAL_CALL(_create_branch, p_branch_name);134}135136void EditorVCSInterface::create_remote(const String &p_remote_name, const String &p_remote_url) {137GDVIRTUAL_CALL(_create_remote, p_remote_name, p_remote_url);138}139140void EditorVCSInterface::remove_branch(const String &p_branch_name) {141GDVIRTUAL_CALL(_remove_branch, p_branch_name);142}143144void EditorVCSInterface::remove_remote(const String &p_remote_name) {145GDVIRTUAL_CALL(_remove_remote, p_remote_name);146}147148String EditorVCSInterface::get_current_branch_name() {149String result;150GDVIRTUAL_CALL(_get_current_branch_name, result);151return result;152}153154bool EditorVCSInterface::checkout_branch(const String &p_branch_name) {155bool result = false;156GDVIRTUAL_CALL(_checkout_branch, p_branch_name, result);157return result;158}159160void EditorVCSInterface::pull(const String &p_remote) {161GDVIRTUAL_CALL(_pull, p_remote);162}163164void EditorVCSInterface::push(const String &p_remote, bool p_force) {165GDVIRTUAL_CALL(_push, p_remote, p_force);166}167168void EditorVCSInterface::fetch(const String &p_remote) {169GDVIRTUAL_CALL(_fetch, p_remote);170}171172List<EditorVCSInterface::DiffHunk> EditorVCSInterface::get_line_diff(const String &p_file_path, const String &p_text) {173TypedArray<Dictionary> result;174if (!GDVIRTUAL_CALL(_get_line_diff, p_file_path, p_text, result)) {175return {};176}177178List<DiffHunk> diff_hunks;179for (int i = 0; i < result.size(); i++) {180diff_hunks.push_back(_convert_diff_hunk(result[i]));181}182return diff_hunks;183}184185bool EditorVCSInterface::shut_down() {186bool result = false;187GDVIRTUAL_CALL(_shut_down, result);188return result;189}190191String EditorVCSInterface::get_vcs_name() {192String result;193GDVIRTUAL_CALL(_get_vcs_name, result);194return result;195}196197Dictionary EditorVCSInterface::create_diff_line(int p_new_line_no, int p_old_line_no, const String &p_content, const String &p_status) {198Dictionary diff_line;199diff_line["new_line_no"] = p_new_line_no;200diff_line["old_line_no"] = p_old_line_no;201diff_line["content"] = p_content;202diff_line["status"] = p_status;203204return diff_line;205}206207Dictionary EditorVCSInterface::create_diff_hunk(int p_old_start, int p_new_start, int p_old_lines, int p_new_lines) {208Dictionary diff_hunk;209diff_hunk["new_lines"] = p_new_lines;210diff_hunk["old_lines"] = p_old_lines;211diff_hunk["new_start"] = p_new_start;212diff_hunk["old_start"] = p_old_start;213diff_hunk["diff_lines"] = TypedArray<Dictionary>();214return diff_hunk;215}216217Dictionary EditorVCSInterface::add_line_diffs_into_diff_hunk(Dictionary p_diff_hunk, TypedArray<Dictionary> p_line_diffs) {218p_diff_hunk["diff_lines"] = p_line_diffs;219return p_diff_hunk;220}221222Dictionary EditorVCSInterface::create_diff_file(const String &p_new_file, const String &p_old_file) {223Dictionary file_diff;224file_diff["new_file"] = p_new_file;225file_diff["old_file"] = p_old_file;226file_diff["diff_hunks"] = TypedArray<Dictionary>();227return file_diff;228}229230Dictionary 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) {231Dictionary commit_info;232commit_info["message"] = p_msg;233commit_info["author"] = p_author;234commit_info["unix_timestamp"] = p_unix_timestamp;235commit_info["offset_minutes"] = p_offset_minutes;236commit_info["id"] = p_id;237return commit_info;238}239240Dictionary EditorVCSInterface::add_diff_hunks_into_diff_file(Dictionary p_diff_file, TypedArray<Dictionary> p_diff_hunks) {241p_diff_file["diff_hunks"] = p_diff_hunks;242return p_diff_file;243}244245Dictionary EditorVCSInterface::create_status_file(const String &p_file_path, ChangeType p_change, TreeArea p_area) {246Dictionary sf;247sf["file_path"] = p_file_path;248sf["change_type"] = p_change;249sf["area"] = p_area;250return sf;251}252253EditorVCSInterface::DiffLine EditorVCSInterface::_convert_diff_line(const Dictionary &p_diff_line) {254DiffLine d;255d.new_line_no = p_diff_line["new_line_no"];256d.old_line_no = p_diff_line["old_line_no"];257d.content = p_diff_line["content"];258d.status = p_diff_line["status"];259return d;260}261262EditorVCSInterface::DiffHunk EditorVCSInterface::_convert_diff_hunk(const Dictionary &p_diff_hunk) {263DiffHunk dh;264dh.new_lines = p_diff_hunk["new_lines"];265dh.old_lines = p_diff_hunk["old_lines"];266dh.new_start = p_diff_hunk["new_start"];267dh.old_start = p_diff_hunk["old_start"];268TypedArray<Dictionary> diff_lines = p_diff_hunk["diff_lines"];269for (int i = 0; i < diff_lines.size(); i++) {270DiffLine dl = _convert_diff_line(diff_lines[i]);271dh.diff_lines.push_back(dl);272}273return dh;274}275276EditorVCSInterface::DiffFile EditorVCSInterface::_convert_diff_file(const Dictionary &p_diff_file) {277DiffFile df;278df.new_file = p_diff_file["new_file"];279df.old_file = p_diff_file["old_file"];280TypedArray<Dictionary> diff_hunks = p_diff_file["diff_hunks"];281for (int i = 0; i < diff_hunks.size(); i++) {282DiffHunk dh = _convert_diff_hunk(diff_hunks[i]);283df.diff_hunks.push_back(dh);284}285return df;286}287288EditorVCSInterface::Commit EditorVCSInterface::_convert_commit(const Dictionary &p_commit) {289EditorVCSInterface::Commit c;290c.msg = p_commit["message"];291c.author = p_commit["author"];292c.unix_timestamp = p_commit["unix_timestamp"];293c.offset_minutes = p_commit["offset_minutes"];294c.id = p_commit["id"];295return c;296}297298EditorVCSInterface::StatusFile EditorVCSInterface::_convert_status_file(const Dictionary &p_status_file) {299StatusFile sf;300sf.file_path = p_status_file["file_path"];301sf.change_type = (ChangeType)(int)p_status_file["change_type"];302sf.area = (TreeArea)(int)p_status_file["area"];303return sf;304}305306void EditorVCSInterface::_bind_methods() {307// Proxy end points that implement the VCS specific operations that the editor demands.308GDVIRTUAL_BIND(_initialize, "project_path");309GDVIRTUAL_BIND(_set_credentials, "username", "password", "ssh_public_key_path", "ssh_private_key_path", "ssh_passphrase");310GDVIRTUAL_BIND(_get_modified_files_data);311GDVIRTUAL_BIND(_stage_file, "file_path");312GDVIRTUAL_BIND(_unstage_file, "file_path");313GDVIRTUAL_BIND(_discard_file, "file_path");314GDVIRTUAL_BIND(_commit, "msg");315GDVIRTUAL_BIND(_get_diff, "identifier", "area");316GDVIRTUAL_BIND(_shut_down);317GDVIRTUAL_BIND(_get_vcs_name);318GDVIRTUAL_BIND(_get_previous_commits, "max_commits");319GDVIRTUAL_BIND(_get_branch_list);320GDVIRTUAL_BIND(_get_remotes);321GDVIRTUAL_BIND(_create_branch, "branch_name");322GDVIRTUAL_BIND(_remove_branch, "branch_name");323GDVIRTUAL_BIND(_create_remote, "remote_name", "remote_url");324GDVIRTUAL_BIND(_remove_remote, "remote_name");325GDVIRTUAL_BIND(_get_current_branch_name);326GDVIRTUAL_BIND(_checkout_branch, "branch_name");327GDVIRTUAL_BIND(_pull, "remote");328GDVIRTUAL_BIND(_push, "remote", "force");329GDVIRTUAL_BIND(_fetch, "remote");330GDVIRTUAL_BIND(_get_line_diff, "file_path", "text");331332ClassDB::bind_method(D_METHOD("create_diff_line", "new_line_no", "old_line_no", "content", "status"), &EditorVCSInterface::create_diff_line);333ClassDB::bind_method(D_METHOD("create_diff_hunk", "old_start", "new_start", "old_lines", "new_lines"), &EditorVCSInterface::create_diff_hunk);334ClassDB::bind_method(D_METHOD("create_diff_file", "new_file", "old_file"), &EditorVCSInterface::create_diff_file);335ClassDB::bind_method(D_METHOD("create_commit", "msg", "author", "id", "unix_timestamp", "offset_minutes"), &EditorVCSInterface::create_commit);336ClassDB::bind_method(D_METHOD("create_status_file", "file_path", "change_type", "area"), &EditorVCSInterface::create_status_file);337ClassDB::bind_method(D_METHOD("add_diff_hunks_into_diff_file", "diff_file", "diff_hunks"), &EditorVCSInterface::add_diff_hunks_into_diff_file);338ClassDB::bind_method(D_METHOD("add_line_diffs_into_diff_hunk", "diff_hunk", "line_diffs"), &EditorVCSInterface::add_line_diffs_into_diff_hunk);339ClassDB::bind_method(D_METHOD("popup_error", "msg"), &EditorVCSInterface::popup_error);340341BIND_ENUM_CONSTANT(CHANGE_TYPE_NEW);342BIND_ENUM_CONSTANT(CHANGE_TYPE_MODIFIED);343BIND_ENUM_CONSTANT(CHANGE_TYPE_RENAMED);344BIND_ENUM_CONSTANT(CHANGE_TYPE_DELETED);345BIND_ENUM_CONSTANT(CHANGE_TYPE_TYPECHANGE);346BIND_ENUM_CONSTANT(CHANGE_TYPE_UNMERGED);347348BIND_ENUM_CONSTANT(TREE_AREA_COMMIT);349BIND_ENUM_CONSTANT(TREE_AREA_STAGED);350BIND_ENUM_CONSTANT(TREE_AREA_UNSTAGED);351}352353EditorVCSInterface *EditorVCSInterface::get_singleton() {354return singleton;355}356357void EditorVCSInterface::set_singleton(EditorVCSInterface *p_singleton) {358singleton = p_singleton;359}360361void EditorVCSInterface::create_vcs_metadata_files(VCSMetadata p_vcs_metadata_type, String &p_dir) {362if (p_vcs_metadata_type == VCSMetadata::GIT) {363Ref<FileAccess> f = FileAccess::open(p_dir.path_join(".gitignore"), FileAccess::WRITE);364if (f.is_null()) {365ERR_FAIL_MSG("Couldn't create .gitignore in project path.");366} else {367f->store_line("# Godot 4+ specific ignores");368f->store_line(".godot/");369f->store_line("/android/");370}371f = FileAccess::open(p_dir.path_join(".gitattributes"), FileAccess::WRITE);372if (f.is_null()) {373ERR_FAIL_MSG("Couldn't create .gitattributes in project path.");374} else {375f->store_line("# Normalize EOL for all files that Git considers text files.");376f->store_line("* text=auto eol=lf");377}378}379}380381382