List of usage examples for org.eclipse.jgit.api Git push
public PushCommand push()
From source file:com.uber.stream.kafka.mirrormaker.controller.core.GitBackUpHandler.java
License:Apache License
public void writeToFile(String fileName, String data) throws Exception { Repository backupRepo = null;//w ww . j a va 2 s. com BufferedWriter output = null; Git git = null; Git result = null; try { try { FileUtils.deleteDirectory(new File(localPath)); } catch (IOException e) { LOGGER.error("Error deleting exisiting backup directory"); throw e; } try { result = Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call(); } catch (Exception e) { LOGGER.error("Error cloning backup git repo"); throw e; } try { backupRepo = new FileRepository(localPath + "/.git"); } catch (IOException e) { throw e; } git = new Git(backupRepo); File myfile = new File(localPath + "/" + fileName); try { output = new BufferedWriter(new FileWriter(myfile)); output.write(data); output.flush(); } catch (IOException e) { LOGGER.error("Error writing backup to the file with name " + fileName); throw e; } try { git.add().addFilepattern(".").call(); } catch (GitAPIException e) { LOGGER.error("Error adding files to git"); throw e; } try { git.commit().setMessage("Taking backup on " + new Date()).call(); } catch (GitAPIException e) { LOGGER.error("Error commiting files to git"); throw e; } try { git.push().call(); } catch (GitAPIException e) { LOGGER.error("Error pushing files to git"); throw e; } } catch (Exception e) { throw e; } finally { output.close(); git.close(); if (result != null) result.getRepository().close(); backupRepo.close(); } }
From source file:com.wadpam.gimple.GimpleMojo.java
License:Open Source License
@Override public void execute() throws MojoExecutionException, MojoFailureException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); try {//w ww . jav a2s .c om Repository repo = builder.setGitDir(new File(new File(gitDir), ".git")).readEnvironment().findGitDir() .build(); final String branch = repo.getBranch(); if (null != branch) { if (!currentVersion.endsWith(SUFFIX_SNAPSHOT)) { throw new MojoExecutionException("Maven project version not in SNAPSHOT: " + currentVersion); } getLog().info("gimple executing on " + gitDir); getLog().info("branch " + branch); if (null == releaseVersion) { releaseVersion = currentVersion.substring(0, currentVersion.length() - SUFFIX_SNAPSHOT.length()); } getLog().info( "Transforming version from " + currentVersion + " to release version " + releaseVersion); Git git = new Git(repo); StatusCommand statusCommand = git.status(); Status status = statusCommand.call(); if (!status.isClean()) { throw new MojoExecutionException("Git project is not clean: " + status.getUncommittedChanges()); } // versions:set releaseVersion transformPomVersions(git, releaseVersion); // tag release Ref tagRef = git.tag().setMessage(GIMPLE_MAVEN_PLUGIN + "tagging release " + releaseVersion) .setName(releaseVersion).call(); // next development version if (null == nextVersion) { nextVersion = getNextDevelopmentVersion(releaseVersion); } // versions:set nextVersion RevCommit nextRef = transformPomVersions(git, nextVersion); // push it all String developerConnection = mavenProject.getScm().getDeveloperConnection(); if (developerConnection.startsWith(PREFIX_SCM_GIT)) { developerConnection = developerConnection.substring(PREFIX_SCM_GIT.length()); } RefSpec spec = new RefSpec(branch + ":" + branch); git.push().setRemote(developerConnection).setRefSpecs(spec).add(tagRef).call(); } } catch (IOException e) { throw new MojoExecutionException("executing", e); } catch (GitAPIException e) { throw new MojoExecutionException("status", e); } }
From source file:com.worldline.easycukes.scm.utils.GitHelper.java
License:Open Source License
/** * Adds all the files of the specified directory in the local git repository * (git add .), then commits the changes (git commit .), and finally pushes * the changes on the remote repository (git push) * * @param directory the directory in which the local git repository is located * @param username the username to be used while pushing * @param password the password matching with the provided username to be used * for authentication/* w w w .j a v a2s .c om*/ * @param message the commit message to be used * @throws GitAPIException if something's going wrong while interacting with Git * @throws IOException if something's going wrong while manipulating the local * repository */ public static void commitAndPush(@NonNull File directory, String username, String password, String message) throws GitAPIException, IOException { try { final Git git = Git.open(directory); // run the add final AddCommand addCommand = git.add(); for (final String filePath : directory.list()) if (!".git".equals(filePath)) addCommand.addFilepattern(filePath); addCommand.call(); log.info("Added content of the directory" + directory + " in the Git repository located in " + directory.toString()); // and then commit final PersonIdent author = new PersonIdent(username, ""); git.commit().setCommitter(author).setMessage(message).setAuthor(author).call(); log.info("Commited the changes in the Git repository..."); // and finally push final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider( username, password); git.push().setCredentialsProvider(userCredential).call(); log.info("Pushed the changes in remote Git repository..."); } catch (final GitAPIException e) { log.error(e.getMessage(), e); throw e; } }
From source file:com.worldline.easycukes.scm.utils.GitHelper.java
License:Open Source License
/** * Create a new branch in the local git repository * (git checkout -b branchname) and finally pushes new branch on the remote repository (git push) * * @param directory the directory in which the local git repository is located * @param username the username to be used while pushing * @param password the password matching with the provided username to be used * for authentication//from w w w .j a v a 2 s. c om * @param message the commit message to be used */ public static void createBranch(@NonNull File directory, String branchName, String username, String password, String message) throws GitAPIException { try { final Git git = Git.open(directory); final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider( username, password); CreateBranchCommand branchCommand = git.branchCreate(); branchCommand.setName(branchName); branchCommand.call(); // and then commit final PersonIdent author = new PersonIdent(username, ""); git.commit().setCommitter(author).setMessage(message).setAuthor(author).call(); log.info(message); git.push().setCredentialsProvider(userCredential).call(); log.info("Pushed the changes in remote Git repository..."); } catch (final GitAPIException | IOException e) { log.error(e.getMessage(), e); } }
From source file:com.worldline.easycukes.scm.utils.GitHelper.java
License:Open Source License
/** * Delete a branch in the local git repository * (git branch -d branchname) and finally pushes new branch on the remote repository (git push) * * @param directory the directory in which the local git repository is located * @param username the username to be used while pushing * @param password the password matching with the provided username to be used * for authentication/*from w w w .j a v a2s . c o m*/ * @param message the commit message to be used */ public static void deleteBranch(@NonNull File directory, String branchName, String username, String password, String message) { try { final Git git = Git.open(directory); final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider( username, password); DeleteBranchCommand deleteBranchCommand = git.branchDelete(); deleteBranchCommand.setBranchNames(branchName); deleteBranchCommand.call(); log.info("Develop branch deleted"); // and then commit final PersonIdent author = new PersonIdent(username, ""); git.commit().setCommitter(author).setMessage(message).setAuthor(author).call(); log.info(message); git.push().setCredentialsProvider(userCredential).call(); log.info("Pushed the changes in remote Git repository..."); } catch (final GitAPIException | IOException e) { log.error(e.getMessage(), e); } }
From source file:com.worldline.easycukes.scm.utils.GitHelper.java
License:Open Source License
/** * Create a new tag in the local git repository * (git checkout tagname) and finally pushes new branch on the remote repository (git push) * * @param directory the directory in which the local git repository is located * @param username the username to be used while pushing * @param password the password matching with the provided username to be used * for authentication//from w w w . ja v a 2 s. com * @param message the commit message to be used */ public static void createTag(@NonNull File directory, String tagName, String username, String password, String message) { try { final Git git = Git.open(directory); final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider( username, password); TagCommand tagCommand = git.tag(); tagCommand.setName(tagName); tagCommand.setMessage(message); tagCommand.call(); log.info("Tag created"); // and then commit final PersonIdent author = new PersonIdent(username, ""); git.commit().setCommitter(author).setMessage(message).setAuthor(author).call(); log.info(message); git.push().setCredentialsProvider(userCredential).call(); log.info("Pushed the changes in remote Git repository..."); } catch (final GitAPIException | IOException e) { log.error(e.getMessage(), e); } }
From source file:com.worldline.easycukes.scm.utils.GitHelper.java
License:Open Source License
/** * Delete a tag in the local git repository * (git tag -d tagname) and finally pushes new branch on the remote repository (git push) * * @param directory the directory in which the local git repository is located * @param username the username to be used while pushing * @param password the password matching with the provided username to be used * for authentication// ww w .j a va 2s . c o m * @param message the commit message to be used */ public static void deleteTag(@NonNull File directory, String tagName, String username, String password, String message) { try { final Git git = Git.open(directory); final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider( username, password); DeleteTagCommand deleteTagCommand = git.tagDelete(); deleteTagCommand.setTags(tagName); deleteTagCommand.call(); log.info("Tag deleted"); // and then commit final PersonIdent author = new PersonIdent(username, ""); git.commit().setCommitter(author).setMessage(message).setAuthor(author).call(); log.info(message); git.push().setCredentialsProvider(userCredential).call(); log.info("Pushed the changes in remote Git repository..."); } catch (final GitAPIException | IOException e) { log.error(e.getMessage(), e); } }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
private MergeConflict savePageInternal(String projectName, String branchName, String path, String suffix, Page page, String baseCommit, String rootDir, User user, ILockedRepository repo, boolean push) throws IOException, GitAPIException { Git git = Git.wrap(repo.r()); String headCommit = CommitUtils.getHead(repo.r()).getName(); if ((baseCommit != null) && headCommit.equals(baseCommit)) { baseCommit = null;/*ww w .j av a 2 s. c o m*/ } String editBranchName = "_edit_" + String.valueOf((long) (Math.random() * Long.MAX_VALUE)); //$NON-NLS-1$ if (baseCommit != null) { git.branchCreate().setName(editBranchName).setStartPoint(baseCommit).call(); git.checkout().setName(editBranchName).call(); } Map<String, Object> metaMap = new HashMap<String, Object>(); metaMap.put(TITLE, page.getTitle()); metaMap.put(CONTENT_TYPE, page.getContentType()); if (!page.getTags().isEmpty()) { metaMap.put(TAGS, page.getTags()); } metaMap.put(VIEW_RESTRICTION_ROLE, page.getViewRestrictionRole()); Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(metaMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, rootDir); File workingFile = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); PageData pageData = page.getData(); if (pageData != null) { workingFile = Util.toFile(pagesDir, path + suffix); FileUtils.writeByteArrayToFile(workingFile, pageData.getData()); } AddCommand addCommand = git.add().addFilepattern(rootDir + "/" + path + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ if (pageData != null) { addCommand.addFilepattern(rootDir + "/" + path + suffix); //$NON-NLS-1$ } addCommand.call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(rootDir + "/" + path + suffix).call(); //$NON-NLS-1$ MergeConflict conflict = null; if (baseCommit != null) { git.rebase().setUpstream(branchName).call(); if (repo.r().getRepositoryState() != RepositoryState.SAFE) { String text = FileUtils.readFileToString(workingFile, Charsets.UTF_8); conflict = new MergeConflict(text, headCommit); git.rebase().setOperation(RebaseCommand.Operation.ABORT).call(); } git.checkout().setName(branchName).call(); if (conflict == null) { git.merge().include(repo.r().resolve(editBranchName)).call(); } git.branchDelete().setBranchNames(editBranchName).setForce(true).call(); } if (push && (conflict == null)) { git.push().call(); } page.setParentPagePath(getParentPagePath(path, repo.r())); if (conflict == null) { PageUtil.updateProjectEditTime(projectName); } return conflict; }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void deletePage(String projectName, String branchName, final String path, User user) throws IOException { Assert.hasLength(projectName);// ww w. java 2 s .c o m Assert.hasLength(branchName); Assert.hasLength(path); Assert.notNull(user); ILockedRepository repo = null; List<String> oldPagePaths; boolean deleted = false; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME); File oldSubPagesDir = Util.toFile(pagesDir, path); oldPagePaths = listPagePaths(oldSubPagesDir, true); oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return path + "/" + p; //$NON-NLS-1$ } })); oldPagePaths.add(path); Git git = Git.wrap(repo.r()); File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX); if (file.isFile()) { git.rm().addFilepattern( DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } file = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX); if (file.isFile()) { git.rm().addFilepattern( DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } file = Util.toFile(pagesDir, path); if (file.isDirectory()) { git.rm().addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$ .call(); deleted = true; } File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME); file = Util.toFile(attachmentsDir, path); if (file.isDirectory()) { git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + path) //$NON-NLS-1$ .call(); deleted = true; } if (deleted) { PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + path) //$NON-NLS-1$ .call(); git.push().call(); PageUtil.updateProjectEditTime(projectName); } } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } if (deleted) { eventBus.post(new PagesDeletedEvent(projectName, branchName, Sets.newHashSet(oldPagePaths))); } }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void relocatePage(final String projectName, final String branchName, final String path, String newParentPagePath, User user) throws IOException { Assert.hasLength(projectName);/*from w ww. j a va 2 s.c om*/ Assert.hasLength(branchName); Assert.hasLength(path); Assert.hasLength(newParentPagePath); Assert.notNull(user); // check if pages exist by trying to load them getPage(projectName, branchName, path, false); getPage(projectName, branchName, newParentPagePath, false); ILockedRepository repo = null; List<String> oldPagePaths; List<String> deletedPagePaths; List<String> newPagePaths; List<PagePathChangedEvent> pagePathChangedEvents; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); String pageName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path; //$NON-NLS-1$ //$NON-NLS-2$ final String newPagePath = newParentPagePath + "/" + pageName; //$NON-NLS-1$ File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File oldSubPagesDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), path); oldPagePaths = listPagePaths(oldSubPagesDir, true); oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return path + "/" + p; //$NON-NLS-1$ } })); oldPagePaths.add(path); File deletedPagesSubDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), newPagePath); deletedPagePaths = listPagePaths(deletedPagesSubDir, true); deletedPagePaths = Lists.newArrayList(Lists.transform(deletedPagePaths, new Function<String, String>() { @Override public String apply(String p) { return newPagePath + "/" + p; //$NON-NLS-1$ } })); deletedPagePaths.add(newPagePath); Git git = Git.wrap(repo.r()); AddCommand addCommand = git.add(); for (String dirName : Sets.newHashSet(DocumentrConstants.PAGES_DIR_NAME, DocumentrConstants.ATTACHMENTS_DIR_NAME)) { File dir = new File(workingDir, dirName); File newSubPagesDir = Util.toFile(dir, newPagePath); if (newSubPagesDir.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath).call(); //$NON-NLS-1$ } File newPageFile = Util.toFile(dir, newPagePath + DocumentrConstants.PAGE_SUFFIX); if (newPageFile.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$ } File newMetaFile = Util.toFile(dir, newPagePath + DocumentrConstants.META_SUFFIX); if (newMetaFile.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$ } File newParentPageDir = Util.toFile(dir, newParentPagePath); File subPagesDir = Util.toFile(dir, path); if (subPagesDir.exists()) { FileUtils.copyDirectoryToDirectory(subPagesDir, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path).call(); //$NON-NLS-1$ addCommand.addFilepattern(dirName + "/" + newParentPagePath + "/" + pageName); //$NON-NLS-1$ //$NON-NLS-2$ } File pageFile = Util.toFile(dir, path + DocumentrConstants.PAGE_SUFFIX); if (pageFile.exists()) { FileUtils.copyFileToDirectory(pageFile, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$ addCommand.addFilepattern( dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$ } File metaFile = Util.toFile(dir, path + DocumentrConstants.META_SUFFIX); if (metaFile.exists()) { FileUtils.copyFileToDirectory(metaFile, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$ addCommand.addFilepattern( dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$ } } addCommand.call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident) .setMessage("move " + path + " to " + newParentPagePath + "/" + pageName) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ .call(); git.push().call(); newPagePaths = Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return newPagePath + StringUtils.removeStart(p, path); } }); pagePathChangedEvents = Lists.transform(oldPagePaths, new Function<String, PagePathChangedEvent>() { @Override public PagePathChangedEvent apply(String oldPath) { String newPath = newPagePath + StringUtils.removeStart(oldPath, path); return new PagePathChangedEvent(projectName, branchName, oldPath, newPath); } }); PageUtil.updateProjectEditTime(projectName); } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } Set<String> allDeletedPagePaths = Sets.newHashSet(oldPagePaths); allDeletedPagePaths.addAll(deletedPagePaths); eventBus.post(new PagesDeletedEvent(projectName, branchName, allDeletedPagePaths)); for (String newPath : newPagePaths) { eventBus.post(new PageChangedEvent(projectName, branchName, newPath)); } for (PagePathChangedEvent event : pagePathChangedEvents) { eventBus.post(event); } }