如何使用 JGIT 將帶有專案的檔案夾添加到存盤庫(在本例中為 GitHub 存盤庫)而無需克隆等?我是否只需使用該檔案夾創建一個本地存盤庫,然后將其與遠程存盤庫合并?
我不想克隆的原因是 repo 會變得非常大,我不希望它永遠克隆。此外,這是在 Spigot 插件命令中,但我認為這不會改變任何內容。當我運行命令時,它給了我一個錯誤。
最后一件事是當我將檔案夾復制到 repo 檔案夾時。它只是創建了一個名為“Minecraft Servers”的檔案夾,其中沒有任何檔案。我究竟做錯了什么?

源代碼:
public class SaveServer implements CommandExecutor {
private final CrucialPlugin plugin;
public SaveServer(CrucialPlugin plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (args.length == 0) {
player.sendMessage(ChatColor.RED "You need to enter some arguments");
player.sendMessage(ChatColor.YELLOW "To save the server network to the GitHub repo: /saveserver <what you built/did>");
} else if (args.length == 1) {
if (plugin.getConfig().getString("Repository URL") != null && plugin.getConfig().getString("GitHub username") != null && plugin.getConfig().getString("GitHub password") != null) {
Path repofolder = Path.of("Server Network Repo");
File test = new File(String.valueOf(repofolder));
if (Files.exists(repofolder)) {
test.delete();
}
player.sendMessage(ChatColor.GREEN "Saving server with caption: " args[0] "...");
File curdir = new File(System.getProperty("user.dir"));
Path networkFolder = curdir.toPath().getParent();
Path finalFolder = Path.of(repofolder File.separator "Minecraft Servers");
Date date = new Date();
char subColon = '\uA789';
String filename = date.toString().replace(':', subColon) "-" args[0].toUpperCase().replace("_", " ") ".txt";
File txt = new File(networkFolder File.separator filename);
try {
txt.createNewFile();
Git.init().setDirectory(new File("Server Network Repo")).call();
Git git = Git.open(new File("Server Network Repo"));
git.add().addFilepattern(networkFolder.toString()).call();
Files.copy(networkFolder, finalFolder, REPLACE_EXISTING);
// add remote repo:
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish(plugin.getConfig().getString("Repository URL")));
remoteAddCommand.call();
// push to remote:
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(plugin.getConfig().getString("GitHub username"), plugin.getConfig().getString("GitHub password")));
pushCommand.call();
} catch (GitAPIException | IOException | URISyntaxException e) {
player.sendMessage(ChatColor.RED "Unable to retrieve repository.");
}
player.sendMessage("Filename test: " filename);
} else {
player.sendMessage(ChatColor.RED "You need to fill in all the fields under Save Server in the configuration file before you can use this feature.");
}
} else {
player.sendMessage(ChatColor.RED "Too many arguments");
player.sendMessage(ChatColor.YELLOW "To save the server network to the GitHub repo: /saveserver <what you built/did>");
}
}
return true;
}
}
uj5u.com熱心網友回復:
這個問題似乎可以通過使用淺克隆和稀疏檢出來解決,這些 git 功能允許克隆存盤庫而無需實際提取整個內容和歷史記錄。問題是截至 2022 年 4 月,該功能尚未在 jGit 中實作。因此,根據您的情況,您可以通過某種妥協來解決:
如果您可以控制代碼將要執行的環境,您可以嘗試呼叫 shell 命令并呼叫真正的 git 而不是 jgit。這當然會降低您的解決方案的可移植性,并給環境帶來額外的配置負擔,(期望安裝 git,期望它的版本正確,期望代碼運行的權限足以呼叫系統命令,git 身份驗證現在發生在外部Java 代碼等)。
如果您打算只將特定的更新檔案推送到主分支的頂部,并且您不關心所有 git 功能,例如分支、差異和合并,而只需使用一個非常特定的 github 存盤庫作為您的一種配置存盤有時需要用 Java 更新,那么你可能想完全跳過 git,而使用 Github REST API。然后,您將更新檔案和提交資訊(例如訊息和作者)作為 http POST 請求發送,并讓 Github 將其轉換為其他用戶可以與常規 git 客戶端一起使用的提交。有現成的用于 API 的 Java 包裝器,例如這個專案。當然,如果你只是碰巧使用 Github,并且實際代碼預計可以與任何git 存盤庫和服務器后端,那么這種方法將不起作用。它還具有對 API 的額外依賴的缺點,這種依賴不如常規 git 穩定,因此由于 Github 中的更改而導致某些東西被破壞的可能性更大。
從另一個角度解決問題并嘗試最小化存盤庫大小。例如,放棄 git 歷史記錄并讓每個提交成為一個修改提交強制推送以覆寫前一個。可能是 git 反模式串列中的前 10 項,并且明顯濫用了 git 的意圖,但如果它解決了您的問題,那么一切都是最好的。沒有解決由于充滿大檔案而導致存盤庫很大的問題,而不僅僅是因為歷史悠久。
雇用一個團隊并解決這個和/或這個。很多人都會受益,所以這是一個道德選擇。據我所知,沒有缺點;)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/467450.html
