List of usage examples for org.eclipse.jgit.api Git push
public PushCommand push()
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void testGetAllBranches() throws IOException, GitAPIException { FakeApplication app = support.Helpers.makeTestApplication(); Helpers.start(app);// w ww.j av a 2 s .co m // Given String userName = "wansoon"; String projectName = "test"; Project project = createProject(userName, projectName); project.save(); String email = "test@email.com"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + "clone-" + projectName + ".git"; String repoPath = wcPath + "/.git"; String dirName = "dir"; String fileName = "file"; GitRepository gitRepository = new GitRepository(userName, projectName); gitRepository.create(); Repository repository = new RepositoryBuilder().setGitDir(new File(repoPath)).build(); repository.create(false); Git git = new Git(repository); FileUtils.forceMkdir(new File(wcPath + "/" + dirName)); FileUtils.touch(new File(wcPath + "/" + fileName)); git.add().addFilepattern(dirName).call(); git.add().addFilepattern(fileName).call(); git.commit().setMessage("test").setAuthor(userName, email).call(); String branchName = "testBranch"; git.branchCreate().setName(branchName).setForce(true).call(); git.push().setRemote(GitRepository.getGitDirectoryURL(project)) .setRefSpecs(new RefSpec(branchName + ":" + branchName)).setForce(true).call(); repository.close(); // When List<GitBranch> gitBranches = gitRepository.getBranches(); gitRepository.close(); // Then assertThat(gitBranches.size()).isEqualTo(1); assertThat(gitBranches.get(0).getShortName()).isEqualTo(branchName); Helpers.stop(app); }
From source file:ru.nikitenkogleb.androidtools.newappwizard.GitSupport.java
/** Send changes to remote repository */ final void close(String userName, String userEmail, String initMessage) { if (!isSuccessful()) return;/*www . j a va2 s. co m*/ FileRepository mRepository = null; Git mGit = null; try { mRepository = new FileRepository(new File(mProjectPath, ".git")); mGit = new Git(mRepository); mGit.add().addFilepattern(".").call(); mGit.commit().setAll(true).setAuthor(userName, userEmail).setCommitter(userName, userEmail) .setMessage(initMessage).call(); final PushCommand pushCommand = mGit.push().setRemote("origin").setPushAll(); if (mSshConfigCallback != null) pushCommand.setTransportConfigCallback(mSshConfigCallback); else pushCommand.setCredentialsProvider(mHttpsCredentialsProvider); pushCommand.call(); } catch (GitAPIException | IOException e) { e.printStackTrace(); } if (mGit != null) mGit.close(); if (mRepository != null) mRepository.close(); }
From source file:se.kth.karamel.backend.github.GithubApi.java
public synchronized static void removeFile(String owner, String repoName, String fileName) throws KaramelException { File repoDir = getRepoDirectory(repoName); Git git = null; try {// ww w. j a va2 s . c o m git = Git.open(repoDir); new File(repoDir + File.separator + fileName).delete(); git.add().addFilepattern(fileName).call(); git.commit().setAuthor(user, email).setMessage("File removed by Karamel.").setAll(true).call(); git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call(); RepoItem toRemove = null; List<RepoItem> repos = cachedRepos.get(owner); for (RepoItem r : repos) { if (r.getName().compareToIgnoreCase(repoName) == 0) { toRemove = r; } } if (toRemove != null) { repos.remove(toRemove); } } catch (IOException | GitAPIException ex) { throw new KaramelException(ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:se.kth.karamel.backend.github.GithubApi.java
/** * Synchronizes your updates on your local repository with github. * * @param owner//from w w w . j a va 2 s. c om * @param repoName * @throws KaramelException */ public synchronized static void commitPush(String owner, String repoName) throws KaramelException { if (email == null || user == null) { throw new KaramelException("You forgot to call registerCredentials. You must call this method first."); } File repoDir = getRepoDirectory(repoName); Git git = null; try { git = Git.open(repoDir); git.commit().setAuthor(user, email).setMessage("Code generated by Karamel.").setAll(true).call(); git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call(); } catch (IOException | GitAPIException ex) { logger.error("error during github push", ex); throw new KaramelException(ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:sh.isaac.provider.sync.git.SyncServiceGIT.java
License:Apache License
/** * Link and fetch from remote.//from w w w. j av a2s. co m * * @param remoteAddress the remote address * @param username the username * @param password the password * @throws IllegalArgumentException the illegal argument exception * @throws IOException Signals that an I/O exception has occurred. * @throws AuthenticationException the authentication exception * @see sh.isaac.api.sync.SyncFiles#linkAndFetchFromRemote(java.io.File, java.lang.String, java.lang.String, java.lang.String) */ @Override public void linkAndFetchFromRemote(String remoteAddress, String username, char[] password) throws IllegalArgumentException, IOException, AuthenticationException { LOG.info("linkAndFetchFromRemote called - folder: {}, remoteAddress: {}, username: {}", this.localFolder, remoteAddress, username); Repository r = null; Git git = null; try { final File gitFolder = new File(this.localFolder, ".git"); r = new FileRepository(gitFolder); if (!gitFolder.isDirectory()) { LOG.debug("Root folder does not contain a .git subfolder. Creating new git repository."); r.create(); } relinkRemote(remoteAddress, username, password); git = new Git(r); final CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, ((password == null) ? new char[] {} : password)); LOG.debug("Fetching"); final FetchResult fr = git.fetch().setCheckFetchedObjects(true).setCredentialsProvider(cp).call(); LOG.debug("Fetch messages: {}", fr.getMessages()); boolean remoteHasMaster = false; final Collection<Ref> refs = git.lsRemote().setCredentialsProvider(cp).call(); for (final Ref ref : refs) { if ("refs/heads/master".equals(ref.getName())) { remoteHasMaster = true; LOG.debug("Remote already has 'heads/master'"); break; } } if (remoteHasMaster) { // we need to fetch and (maybe) merge - get onto origin/master. LOG.debug("Fetching from remote"); final String fetchResult = git.fetch().setCredentialsProvider(cp).call().getMessages(); LOG.debug("Fetch Result: {}", fetchResult); LOG.debug("Resetting to origin/master"); git.reset().setMode(ResetType.MIXED).setRef("origin/master").call(); // Get the files from master that we didn't have in our working folder LOG.debug("Checking out missing files from origin/master"); for (final String missing : git.status().call().getMissing()) { LOG.debug("Checkout {}", missing); git.checkout().addPath(missing).call(); } for (final String newFile : makeInitialFilesAsNecessary(this.localFolder)) { LOG.debug("Adding and committing {}", newFile); git.add().addFilepattern(newFile).call(); git.commit().setMessage("Adding " + newFile).setAuthor(username, "42").call(); for (final PushResult pr : git.push().setCredentialsProvider(cp).call()) { LOG.debug("Push Message: {}", pr.getMessages()); } } } else { // just push // make sure we have something to push for (final String newFile : makeInitialFilesAsNecessary(this.localFolder)) { LOG.debug("Adding and committing {}", newFile); git.add().addFilepattern(newFile).call(); } git.commit().setMessage("Adding initial files").setAuthor(username, "42").call(); LOG.debug("Pushing repository"); for (final PushResult pr : git.push().setCredentialsProvider(cp).call()) { LOG.debug("Push Result: {}", pr.getMessages()); } } LOG.info("linkAndFetchFromRemote Complete. Current status: " + statusToString(git.status().call())); } catch (final TransportException te) { if (te.getMessage().contains("Auth fail") || te.getMessage().contains("not authorized")) { LOG.info("Auth fail", te); throw new AuthenticationException("Auth fail"); } else { LOG.error("Unexpected", te); throw new IOException("Internal error", te); } } catch (final GitAPIException e) { LOG.error("Unexpected", e); throw new IOException("Internal error", e); } finally { if (git != null) { git.close(); } if (r != null) { r.close(); } } }