@Signature public Memory remoteSetUrl(String name, String uri, boolean push) throws URISyntaxException, GitAPIException { RemoteSetUrlCommand command = getWrappedObject().remoteSetUrl(); command.setName(name); command.setUri(new URIish(uri)); command.setPush(push); return GitUtils.valueOf(command.call()); }
private Git cloneToBasedir(URI projectUrl, File destinationFolder) { String url = projectUrl.toString(); String projectGitUrl = url.endsWith(".git") ? url : url + ".git"; if (log.isDebugEnabled()) { log.debug("Project git url [" + projectGitUrl + "]"); } CloneCommand command = this.gitFactory.getCloneCommandByCloneRepository() .setURI(projectGitUrl).setDirectory(destinationFolder); try { Git git = command.call(); if (git.getRepository().getRemoteNames().isEmpty()) { log.info("No remote added. Will add remote of the cloned project"); git.remoteSetUrl().setUri(new URIish(projectGitUrl)); git.remoteSetUrl().setName("origin"); git.remoteSetUrl().setPush(true); } return git; } catch (GitAPIException | URISyntaxException e) { deleteBaseDirIfExists(); throw new IllegalStateException(e); } }
public static BackupService prepare(File localDir, URIish remoteUri, int backupTimeInMinutes) 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(), backupTimeInMinutes); } }
@Override public Status call() throws Exception { LOG.info("Starting repository synchronization"); try (Git git = getWorkingFileRepository()) { // Set fetch repo config.configure(git.remoteSetUrl(), fetchUrl, false).call(); config.configure(git.remoteSetUrl(), pushRepository, true).call(); // Fetch repo config.configure(git.fetch()).setCheckFetchedObjects(true).call(); // Push repo config.configure(git.push()).setForce(true).call(); // Sync working tree return pushRepository.call(); } finally { LOG.info("Finished repository synchronization"); // Close repository and free resources config.getWorkingFileRepository().close(); } }