public static BackupService prepare(File localDir, URIish remoteUri) throws GitAPIException, IOException { Git local; try { local = Git.open(localDir); } catch (RepositoryNotFoundException e) { log.info("Initialising " + fullPath(localDir) + " as a git repo for backup purposes"); local = Git.init().setDirectory(localDir).setBare(false).call(); } log.info("Setting backup URL to " + remoteUri); if (local.remoteList().call().stream().anyMatch(remoteConfig -> remoteConfig.getName().equals("origin"))) { RemoteSetUrlCommand remoteSetUrlCommand = local.remoteSetUrl(); remoteSetUrlCommand.setName("origin"); remoteSetUrlCommand.setUri(remoteUri); remoteSetUrlCommand.call(); } else { RemoteAddCommand remoteAddCommand = local.remoteAdd(); remoteAddCommand.setName("origin"); remoteAddCommand.setUri(remoteUri); remoteAddCommand.call(); } URL inputUrl = BackupService.class.getResource("/dataDirGitIgnore.txt"); FileUtils.copyURLToFile(inputUrl, new File(localDir, ".gitignore")); return new BackupService(local, remoteUri.toString()); }
public static void setOriginOnProjectToTmp(File origin, File project) throws GitAPIException, MalformedURLException { try(Git git = openGitProject(project)) { RemoteRemoveCommand remove = git.remoteRemove(); remove.setName("origin"); remove.call(); RemoteSetUrlCommand command = git.remoteSetUrl(); command.setUri(new URIish(origin.toURI().toURL())); command.setName("origin"); command.setPush(true); command.call(); } }
default RemoteSetUrlCommand configure(RemoteSetUrlCommand cmd, URIish uri, boolean push) { return cmd; }
default RemoteSetUrlCommand configure(RemoteSetUrlCommand cmd, SyncableRepository repo, boolean push) { return configure(cmd, repo.getUri(), push); }