如何使用JGIT将带有项目的文件夹添加到repo(在本例中是GitHub repo),而无需克隆等操作?我会用文件夹创建一个本地回购协议,然后将其与远程回购协议合并吗?
我不想克隆的原因是回购协议将变得非常庞大,我不希望克隆需要很长时间.此外,这是在一个插口插件命令中,但我认为这不会改变任何事情.当我运行命令时,它会给我一个错误.
最后一件事是,当我将文件夹复制到repo文件夹时.它只创建了一个名为"Minecraft服务器"的文件夹,其中没有任何文件.我做错了什么?
源代码:
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;
}
}