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()); }
@Given("^I clone repository \"([^\"]*)\"$") public void iCloneRepository(String name) throws Throwable { workFolder = Files.createTempDir(); RepositoryModel repositoryModel = repositoriesApi.getRepository(name) .getRepositoryModel(); repositoryName = name; git = Git.init().setBare(false).setDirectory(workFolder).call(); RemoteAddCommand addCommand = git.remoteAdd(); addCommand.setName(REMOTE_ORIGIN); addCommand.setUri(new URIish(repositoryModel.getUrl() + ".git")); addCommand.call(); FetchResult fetchResult = git.fetch().setRemote(REMOTE_ORIGIN).call(); if (!fetchResult.getAdvertisedRefs().isEmpty()) { git.pull().setRemote(REMOTE_ORIGIN).setRemoteBranchName(MASTER_BRANCH_NAME).call(); } }
private RemoteConfig setupRemote() throws Exception { URIish uri = new URIish( db2.getDirectory().toURI().toURL() ); RemoteAddCommand cmd = git.remoteAdd(); cmd.setName( Constants.DEFAULT_REMOTE_NAME ); cmd.setUri( uri ); return cmd.call(); }
public static void addRemote(final Repository repo, final String remote, final String url) { try { // Add the new remote { final RemoteAddCommand remoteAdd = new Git(repo).remoteAdd(); remoteAdd.setName(remote); remoteAdd.setUri(new URIish(url)); remoteAdd.call(); } // Set up tracking { StoredConfig config = repo.getConfig(); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", "remote", remote); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", "merge", "refs/heads/master"); config.save(); } } catch (IOException | URISyntaxException | GitAPIException e) { throw new RuntimeException("Failed to issue git remote add", e); } }
@Test public void testPush() throws Exception { // Set remote Git git2 = new Git( db2 ); UIGit uiGit2 = new UIGit(); uiGit2.setGit( git2 ); URIish uri = new URIish( db2.getDirectory().toURI().toURL() ); RemoteAddCommand cmd = git.remoteAdd(); cmd.setName( Constants.DEFAULT_REMOTE_NAME ); cmd.setUri( uri ); cmd.call(); assertTrue( uiGit.hasRemote() ); // create some refs via commits and tag RevCommit commit = git.commit().setMessage( "initial commit" ).call(); Ref tagRef = git.tag().setName( "tag" ).call(); try { db2.resolve( commit.getId().getName() + "^{commit}" ); fail( "id shouldn't exist yet" ); } catch ( MissingObjectException e ) { // we should get here } boolean success = uiGit.push(); assertTrue( success ); assertEquals( commit.getId(), db2.resolve( commit.getId().getName() + "^{commit}" ) ); assertEquals( tagRef.getObjectId(), db2.resolve( tagRef.getObjectId().getName() ) ); // Push a tag EnterSelectionDialog esd = mock( EnterSelectionDialog.class ); doReturn( "tag" ).when( esd ).open(); doReturn( esd ).when( uiGit ).getEnterSelectionDialog( any(), anyString(), anyString() ); uiGit.push( IVCS.TYPE_TAG ); assertTrue( success ); assertTrue( uiGit2.getTags().contains( "tag" ) ); // Another commit and push a branch again writeTrashFile( "Test2.txt", "Hello world" ); git.add().addFilepattern( "Test2.txt" ).call(); commit = git.commit().setMessage( "second commit" ).call(); doReturn( Constants.MASTER ).when( esd ).open(); uiGit.push( IVCS.TYPE_BRANCH ); assertTrue( success ); assertEquals( commit.getId(), db2.resolve( commit.getId().getName() + "^{commit}" ) ); assertEquals( "refs/remotes/origin/master", uiGit.getExpandedName( "origin/master", "branch" ) ); }