How do I add a folder with item to a repo (in this case a GitHub repo) using JGIT without having to clone and such? Would I just create a local repo with the folder then merge it with the remote one?
The reason I don't want to have to clone is that the repo is going to get very large and I don't want it to take forever to clone. Also, this is inside a Spigot plugin command, but I don't think that would change anything. It gives me an error when I run the command.
One last thing is that when I copy the folder to the repo folder. It just creates a folder called "Minecraft Servers" and none of the files in it. What am I doing wrong?
Source code:
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;
}
}
