@SneakyThrows protected void pull() { try ( Repository db = createRepository(); Git git = Git.wrap(db); ) { String branchName = gitProperties.getBranchName(); try { git.checkout().setName(branchName).call(); git.pull().setCredentialsProvider(createCredentialsProvider()).call(); } catch (RefNotFoundException e) { log.info("Branch {} not found in local repository", branchName); git.fetch().setCredentialsProvider(createCredentialsProvider()).call(); git.checkout() .setCreateBranch(true) .setName(branchName) .setUpstreamMode(TRACK) .setStartPoint(DEFAULT_REMOTE_NAME + "/" + branchName). call(); git.pull().setCredentialsProvider(createCredentialsProvider()).call(); } } }
private static void visit( File directory ) throws IOException, RefNotFoundException, DetachedHeadException, WrongRepositoryStateException, InvalidRemoteException, InvalidConfigurationException, CanceledException { for ( final File child : directory.listFiles() ) { if ( child.isDirectory() ) { visit( child ); } final String name = child.getName(); if ( name.equals( ".git" ) ) { Thread t = new Thread( new Runnable() { public void run() { try { Git git = Git.open( child.getParentFile() ); PullResult pullResult = git.pull().call(); System.out.println( "pulled on " + child.getParentFile().getName() + ", pullResult = " + pullResult.isSuccessful() + ", " + pullResult.getFetchedFrom() + ", fetch messages: " + pullResult.getFetchResult().getMessages() ); } catch ( Exception e ) { e.printStackTrace(); } } } ); t.start(); } } }
/** * Asks the user if they want to switch to the review branch, and performs * the switch if so. */ private void promptSwitchToReviewBranch(TaskRepository taskRepository, String reviewBranch) { MessageDialog dialog = new MessageDialog(null, "Appraise Review", null, "Do you want to switch to the review branch (" + reviewBranch + ")", MessageDialog.QUESTION, new String[] {"Yes", "No"}, 0); int result = dialog.open(); if (result == 0) { Repository repo = AppraisePluginUtils.getGitRepoForRepository(taskRepository); try (Git git = new Git(repo)) { previousBranch = repo.getFullBranch(); git.checkout().setName(reviewBranch).call(); } catch (RefNotFoundException rnfe) { MessageDialog alert = new MessageDialog(null, "Oops", null, "Branch " + reviewBranch + " not found", MessageDialog.INFORMATION, new String[] {"OK"}, 0); alert.open(); } catch (Exception e) { AppraiseUiPlugin.logError("Unable to switch to review branch: " + reviewBranch, e); } } }
/** * Switch to the main branch and delete the temporary branch. * * @throws GitAPIException * @throws RefAlreadyExistsException * @throws RefNotFoundException * @throws InvalidRefNameException * @throws CheckoutConflictException * @throws NotMergedException * @throws CannotDeleteCurrentBranchException */ private void switchToMainAndDeleteFrom(final String tempBranch) throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, NotMergedException, CannotDeleteCurrentBranchException { try { repository.reset().setMode(ResetType.HARD).call(); } finally { try { repository.checkout().setCreateBranch(false) .setName(mainBranchName).setForce(true).call(); } finally { try { repository.reset().setMode(ResetType.HARD).call(); } finally { repository.branchDelete().setForce(true) .setBranchNames(tempBranch).call(); } } } }
private static TagVersionAndCount fixCommitCount(TagVersionAndCount resolved, Repository repo) throws RefNotFoundException, GitAPIException { Git git = new Git(repo); ObjectId target, head; LogCommand logCommand; try { target = repo.getRef(resolved.getVersion()).getPeeledObjectId(); logCommand = git.log(); logCommand.add(target); } catch (IOException e) { throw new SemverGitflowPlugin.VersionApplicationException(e); } int count = 0; for (RevCommit commit : logCommand.call()) { count ++; } return new TagVersionAndCount(resolved.getVersion(), count); }
@Override public Ref call() throws GitAPIException, RefNotFoundException, CheckoutConflictException, InvalidRefNameException, RefAlreadyExistsException { this.checkCallable(); try { this.processOptions(); this.checkoutStartPoint(); RefUpdate update = this.getRepository().updateRef(Constants.HEAD); Result r = update.link(this.getBranchName()); if (EnumSet.of(Result.NEW, Result.FORCED).contains(r) == false) { throw new JGitInternalException(MessageFormat.format( JGitText.get().checkoutUnexpectedResult, r.name())); } this.setCallable(false); return this.getRepository().getRef(Constants.HEAD); } catch (IOException e) { throw new JGitInternalException(e.getMessage(), e); } }
public Git call(final GitOperationsStep gitOperationsStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws IllegalArgumentException, IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException { git.pull().setCredentialsProvider(cp).setRebase(rebase).call(); return git; }
public static TagVersionAndCount resolveLatestTagVersionAndCount( Repository repo, TagVersionAndCount curTag, int recur) throws IOException, RefNotFoundException, GitAPIException { Git git = new Git(repo); String described = git.describe().setTarget(curTag.getVersion()).call(); if (described == null) return null; TagVersionAndCount describedTag = parseDescribeOutput(described); if (!SemanticVersions.isValid(GitRepos.stripVFromVersionString(describedTag.getVersion()))) { RevWalk revWalk = new RevWalk(repo); RevCommit describedRev = revWalk.parseCommit(repo.resolve(describedTag.getVersion())); TagVersionAndCount mostRecentParentTag = new TagVersionAndCount("", Integer.MAX_VALUE); for (RevCommit parent : describedRev.getParents()) { TagVersionAndCount parentTag = new TagVersionAndCount(parent.name(), -1); TagVersionAndCount resolvedParentTag = resolveLatestTagVersionAndCount(repo, parentTag, recur + 1); if (resolvedParentTag == null) continue; if (resolvedParentTag.getCount() < mostRecentParentTag.getCount()) { mostRecentParentTag = resolvedParentTag; } } if (mostRecentParentTag.getCount() == Integer.MAX_VALUE) { return null; } return mostRecentParentTag; } else { if (recur != 0) return new TagVersionAndCount(describedTag.getVersion(), -1); return describedTag; } }
public static TagVersionAndCount getLatestTagVersionAndCount(Repository repo) throws IOException, RefNotFoundException, GitAPIException { TagVersionAndCount tac = resolveLatestTagVersionAndCount(repo, new TagVersionAndCount("HEAD", 0), 0); if (tac.getCount() == -1) return fixCommitCount(tac, repo); return tac; }
private static Git initializeGitFlow(Repository repo) throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, GitAPIException { Git git = new Git(repo); git.commit().setCommitter(COMMITTER).setMessage("initial commit").call(); return git; }
protected void checkoutStartPoint() throws GitAPIException, RefNotFoundException, CheckoutConflictException, IOException { ObjectId sp = this.getStartPoint(); if (sp != null) { this.checkout(sp); } }
protected ObjectId getStartPoint() throws RefNotFoundException, IOException { if (this.startCommit != null) { return this.startCommit.getId(); } if (StringUtils.isEmptyOrNull(this.startPoint) == false) { ObjectId oid = this.getRepository().resolve(this.startPoint); if (oid == null) { throw new RefNotFoundException(MessageFormat.format( JGitText.get().refNotResolved, this.startPoint)); } return oid; } return null; }
/** * Get a list of workflows from a directory * @param gitInfo The Git directory information * @return The list of workflow overviews */ public List<WorkflowOverview> getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException { List<WorkflowOverview> workflowsInDir = new ArrayList<>(); try { boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); Git repo = null; while (repo == null) { try { repo = gitService.getRepository(gitInfo, safeToAccess); } catch (RefNotFoundException ex) { // Attempt slashes in branch fix GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo); if (correctedForSlash != null) { gitInfo = correctedForSlash; } else { throw ex; } } } Path localPath = repo.getRepository().getWorkTree().toPath(); Path pathToDirectory = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath(); Path root = Paths.get("/").toAbsolutePath(); if (pathToDirectory.equals(root)) { pathToDirectory = localPath; } else if (!pathToDirectory.startsWith(localPath.normalize().toAbsolutePath())) { // Prevent path traversal attacks throw new WorkflowNotFoundException(); } File directory = new File(pathToDirectory.toString()); if (directory.exists() && directory.isDirectory()) { for (final File file : directory.listFiles()) { int eIndex = file.getName().lastIndexOf('.') + 1; if (eIndex > 0) { String extension = file.getName().substring(eIndex); if (extension.equals("cwl")) { WorkflowOverview overview = cwlService.getWorkflowOverview(file); if (overview != null) { workflowsInDir.add(overview); } } } } } } finally { gitSemaphore.release(gitInfo.getRepoUrl()); } return workflowsInDir; }
public static void main( String[] args ) throws IOException, RefNotFoundException, DetachedHeadException, WrongRepositoryStateException, InvalidRemoteException, InvalidConfigurationException, CanceledException { visit( new File( "C:\\workingcopy\\phet-little-gits" ) ); }