List of usage examples for org.eclipse.jgit.api Git fetch
public FetchCommand fetch()
From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java
License:Open Source License
@SuppressWarnings("restriction") @Override//from w w w . j a v a 2 s .co m public List<IReviewDescriptor> getReviews(URI uri) throws InvalidReviewRepositoryException { List<IReviewDescriptor> changeIds = new LinkedList<>(); try { // connect to the local git repository Git git = Git.open(new File(uri)); try { // Assume that origin refers to the remote gerrit repository // list all remote refs from origin Collection<Ref> remoteRefs = git.lsRemote().setTimeout(60).call(); Pattern changeRefPattern = Pattern.compile(CHANGE_REF_PATTERN); // search for change refs for (Ref ref : remoteRefs) { Matcher matcher = changeRefPattern.matcher(ref.getName()); if (matcher.matches()) { String changePk = matcher.group(CHANGE_REF_PATTERN_GROUP_CHANGE_PK); String changeId = "<unknown>"; GerritReviewDescriptor reviewDescriptor; try { reviewDescriptor = new GerritReviewDescriptor(Integer.parseInt(changePk), changeId); } catch (NumberFormatException nfe) { // FIXME ignore it or throw an exception? break; } if (!changeIds.contains(reviewDescriptor)) { changeIds.add(reviewDescriptor); /* * the change id is present in all commit messages, * so we extract it from the commit message of the * current ref */ FetchResult fetchResult = git.fetch().setRefSpecs(new RefSpec(ref.getName())).call(); Ref localRef = fetchResult.getAdvertisedRef(ref.getName()); RevWalk revWalk = new RevWalk(git.getRepository()); RevCommit commit = revWalk.parseCommit(localRef.getObjectId()); String[] paragraphs = commit.getFullMessage().split("\n"); String lastParagraph = paragraphs[paragraphs.length - 1]; Pattern pattern = Pattern.compile(".*Change-Id: (I[^ \n]*).*"); Matcher changeIdMatcher = pattern.matcher(lastParagraph); if (changeIdMatcher.matches()) { changeId = changeIdMatcher.group(1); reviewDescriptor.setChangeId(changeId); ; } else { logger.warn(MessageFormat.format( "Could not find the change id for Gerrit change with primary key {0}", changePk)); } revWalk.close(); } } } } catch (GitAPIException e) { throw new RepositoryIOException("Error during loading all remote changes", e); } } catch (IOException e) { throw new InvalidReviewRepositoryException("Could not open local git repository", e); } return changeIds; }
From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java
License:Open Source License
@Override public ModelReview loadReview(URI uri, String id, User currentReviewer, IProgressMonitor monitor) throws InvalidReviewRepositoryException, InvalidReviewException, RepositoryIOException { /*/*from w ww .j av a2 s.c o m*/ * Fetch all refs to the patch sets for the particular change and create * the model instance from it */ monitor.beginTask("Connecting to repository", IProgressMonitor.UNKNOWN); try { Git git = Git.open(new File(uri)); int iId = Integer.parseInt(id); // First of all: fetch the patch sets // git fetch origin // +refs/changes/id%100/<cid>/*:refs/changes/id%100/<cid>/* // Refspec of a patchset: // +refs/changes/id%100/<cid>/<psId>:refs/changes/id%100/<cid>/<psId> monitor.beginTask("Fetching change ref", IProgressMonitor.UNKNOWN); git.fetch() .setRefSpecs(new RefSpec(MessageFormat.format( "+refs/changes/{0,number,00}/{1}/*:refs/changes/{0,number,00}/{1}/*", iId % 100, iId))) .call(); // create model instance ModelReview modelReview = modelReviewFactory.createModelReview(); modelReview.setId(id); modelReview.setRepositoryURI(uri.toString()); modelReview.setCurrentReviewer(currentReviewer); EList<PatchSet> patchSets = modelReview.getPatchSets(); Repository repository = git.getRepository(); Map<String, Ref> allRefs = repository.getAllRefs(); Pattern changeRefPattern = Pattern.compile(CHANGE_REF_PATTERN); monitor.beginTask("Loading Patch Sets", allRefs.size()); List<ResourceSet> resourceSets = new LinkedList<>(); for (Entry<String, Ref> refEntry : allRefs.entrySet()) { Matcher matcher = changeRefPattern.matcher(refEntry.getValue().getName()); if (matcher.matches() && matcher.group(CHANGE_REF_PATTERN_GROUP_CHANGE_PK).equals(id)) { PatchSet patchSet = modelReviewFactory.createPatchSet(); patchSets.add(patchSet); patchSet.setId(matcher.group(CHANGE_REF_PATTERN_GROUP_PATCH_SET_ID)); monitor.subTask("Loading Patch Set #" + patchSet.getId()); // load patched files loadPatches(patchSet, refEntry.getValue(), git); // load involved models resourceSets.addAll(loadInvolvedModelsAndDiagrams(patchSet, refEntry.getValue(), git)); // compare the involved models patchSet.setModelComparison(compareModels(patchSet)); // compare the involved diagrams patchSet.setDiagramComparison(compareDiagrams(patchSet)); } monitor.worked(1); } monitor.beginTask("Sorting Patch Sets", IProgressMonitor.UNKNOWN); /* * sort by their identifiers, numeric identifiers before string * identifiers (gerrit currently has only numeric patch set * identifiers, but to be on the save side also consider non-numeric * identifiers ) */ ECollections.sort(patchSets, new Comparator<PatchSet>() { @Override public int compare(PatchSet o1, PatchSet o2) { String psId1 = o1.getId(); String psId2 = o2.getId(); Integer iPsId1 = null; Integer iPsId2 = null; try { iPsId1 = Integer.valueOf(psId1); } catch (NumberFormatException nfe) { } try { iPsId2 = Integer.valueOf(psId2); } catch (NumberFormatException nfe) { } if (iPsId1 != null && iPsId2 != null) { // both numeric ids return iPsId1.compareTo(iPsId2); } else if (iPsId1 != null && iPsId2 == null) { // only one is numeric, the numeric id is always less // than the string id return -1; } else if (iPsId1 == null && iPsId2 != null) { // only one is numeric, the numeric id is always less // than the string id return 1; } // fallback to string sort return psId1.compareTo(psId2); } }); monitor.beginTask("Loading Comments", IProgressMonitor.UNKNOWN); loadComments(repository, modelReview, resourceSets); monitor.done(); return modelReview; } catch (IOException e) { throw new InvalidReviewRepositoryException("Could not open local git repository", e); } catch (NumberFormatException e) { throw new InvalidReviewException(MessageFormat.format("Invalid review id: {0}", id)); } catch (GitAPIException e) { throw new RepositoryIOException("Error occured during reading from the git repository", e); } }
From source file:br.com.ingenieux.mojo.beanstalk.bundle.CodeCommitFastDeployMojo.java
License:Apache License
@Override protected Git getGitRepo() throws Exception { if (stagingDirectory.exists() && new File(stagingDirectory, "HEAD").exists()) { Git git = Git.open(stagingDirectory); git.fetch().setRemote(getRemoteUrl(null, null)).setProgressMonitor(new TextProgressMonitor()) .setRefSpecs(new RefSpec("refs/heads/master")).call(); } else {/*from w w w . j ava 2 s .com*/ Git.cloneRepository().setURI(getRemoteUrl(null, null)).setProgressMonitor(new TextProgressMonitor()) .setDirectory(stagingDirectory).setNoCheckout(true).setBare(true).call(); } Repository r = null; RepositoryBuilder b = new RepositoryBuilder().setGitDir(stagingDirectory).setWorkTree(sourceDirectory); r = b.build(); final Git git = Git.wrap(r); return git; }
From source file:ch.uzh.ifi.seal.permo.history.git.core.model.jgit.JGitRepository.java
License:Apache License
private void fetchFromUpstream() throws InvalidRemoteException, TransportException, GitAPIException { final boolean fetchAutomatically = Preferences.getStore() .getBoolean(Preferences.PERMO__FETCH_AUTOMATICALLY); if (fetchAutomatically) { final Git git = new Git(repository); git.fetch().setRemote(JGitUtil.getDefaultRemote(repository)).call(); }//from www. j a v a2 s .c o m }
From source file:com.barchart.jenkins.cascade.PluginScmGit.java
License:BSD License
/** * See {@link Git#fetch()}/* www . j a v a2 s . c om*/ */ public static FetchResult doFetch(final File workspace, final String remote, final RefSpec spec) { try { final Git git = Git.open(workspace); return git.fetch().setRemote(remote).setRefSpecs(spec).call(); } catch (final Throwable e) { throw new RuntimeException(e); } }
From source file:com.denimgroup.threadfix.service.repository.GitServiceImpl.java
License:Mozilla Public License
@Override public boolean testConfiguration(Application application, String repo, String branch) throws GitAPIException { InitCommand initCommand = new InitCommand(); File applicationDirectory = DiskUtils.getScratchFile(baseDirectory + application.getId() + "-test"); initCommand.setDirectory(applicationDirectory); Git otherGit = initCommand.call(); otherGit.getRepository().getConfig().setString("remote", "origin", "url", repo); String targetRefSpec = branch == null || branch.isEmpty() ? Constants.R_HEADS + "*:refs/remotes/origin/*" : Constants.R_HEADS + branch; FetchCommand fetchCommand = otherGit.fetch() .setCredentialsProvider(getUnencryptedApplicationCredentials(application)).setDryRun(true) .setRefSpecs(new RefSpec(targetRefSpec)).setRemote("origin"); fetchCommand.call();/*from w w w .j a v a 2 s. co m*/ return true; }
From source file:com.gitblit.manager.GitblitManager.java
License:Apache License
/** * Creates a personal fork of the specified repository. The clone is view * restricted by default and the owner of the source repository is given * access to the clone.//from w ww. jav a 2s . co m * * @param repository * @param user * @return the repository model of the fork, if successful * @throws GitBlitException */ @Override public RepositoryModel fork(RepositoryModel repository, UserModel user) throws GitBlitException { String cloneName = MessageFormat.format("{0}/{1}.git", user.getPersonalPath(), StringUtils.stripDotGit(StringUtils.getLastPathElement(repository.name))); String fromUrl = MessageFormat.format("file://{0}/{1}", repositoryManager.getRepositoriesFolder().getAbsolutePath(), repository.name); // clone the repository try { Repository canonical = getRepository(repository.name); File folder = new File(repositoryManager.getRepositoriesFolder(), cloneName); CloneCommand clone = new CloneCommand(); clone.setBare(true); // fetch branches with exclusions Collection<Ref> branches = canonical.getRefDatabase().getRefs(Constants.R_HEADS).values(); List<String> branchesToClone = new ArrayList<String>(); for (Ref branch : branches) { String name = branch.getName(); if (name.startsWith(Constants.R_TICKET)) { // exclude ticket branches continue; } branchesToClone.add(name); } clone.setBranchesToClone(branchesToClone); clone.setURI(fromUrl); clone.setDirectory(folder); Git git = clone.call(); // fetch tags FetchCommand fetch = git.fetch(); fetch.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*")); fetch.call(); git.getRepository().close(); } catch (Exception e) { throw new GitBlitException(e); } // create a Gitblit repository model for the clone RepositoryModel cloneModel = repository.cloneAs(cloneName); // owner has REWIND/RW+ permissions cloneModel.addOwner(user.username); // ensure initial access restriction of the fork // is not lower than the source repository (issue-495/ticket-167) if (repository.accessRestriction.exceeds(cloneModel.accessRestriction)) { cloneModel.accessRestriction = repository.accessRestriction; } repositoryManager.updateRepositoryModel(cloneName, cloneModel, false); // add the owner of the source repository to the clone's access list if (!ArrayUtils.isEmpty(repository.owners)) { for (String owner : repository.owners) { UserModel originOwner = userManager.getUserModel(owner); if (originOwner != null && !originOwner.canClone(cloneModel)) { // origin owner can't yet clone fork, grant explicit clone access originOwner.setRepositoryPermission(cloneName, AccessPermission.CLONE); reviseUser(originOwner.username, originOwner); } } } // grant origin's user list clone permission to fork List<String> users = repositoryManager.getRepositoryUsers(repository); List<UserModel> cloneUsers = new ArrayList<UserModel>(); for (String name : users) { if (!name.equalsIgnoreCase(user.username)) { UserModel cloneUser = userManager.getUserModel(name); if (cloneUser.canClone(repository) && !cloneUser.canClone(cloneModel)) { // origin user can't yet clone fork, grant explicit clone access cloneUser.setRepositoryPermission(cloneName, AccessPermission.CLONE); } cloneUsers.add(cloneUser); } } userManager.updateUserModels(cloneUsers); // grant origin's team list clone permission to fork List<String> teams = repositoryManager.getRepositoryTeams(repository); List<TeamModel> cloneTeams = new ArrayList<TeamModel>(); for (String name : teams) { TeamModel cloneTeam = userManager.getTeamModel(name); if (cloneTeam.canClone(repository) && !cloneTeam.canClone(cloneModel)) { // origin team can't yet clone fork, grant explicit clone access cloneTeam.setRepositoryPermission(cloneName, AccessPermission.CLONE); } cloneTeams.add(cloneTeam); } userManager.updateTeamModels(cloneTeams); // add this clone to the cached model repositoryManager.addToCachedRepositoryList(cloneModel); if (pluginManager != null) { for (RepositoryLifeCycleListener listener : pluginManager .getExtensions(RepositoryLifeCycleListener.class)) { try { listener.onFork(repository, cloneModel); } catch (Throwable t) { logger.error(String.format("failed to call plugin onFork %s", repository.name), t); } } } return cloneModel; }
From source file:com.gitblit.service.MirrorService.java
License:Apache License
@Override public void run() { if (!isReady()) { return;/*from www . ja va 2 s. c o m*/ } running.set(true); for (String repositoryName : repositoryManager.getRepositoryList()) { if (forceClose.get()) { break; } if (repositoryManager.isCollectingGarbage(repositoryName)) { logger.debug("mirror is skipping {} garbagecollection", repositoryName); continue; } RepositoryModel model = null; Repository repository = null; try { model = repositoryManager.getRepositoryModel(repositoryName); if (!model.isMirror && !model.isBare) { // repository must be a valid bare git mirror logger.debug("mirror is skipping {} !mirror !bare", repositoryName); continue; } repository = repositoryManager.getRepository(repositoryName); if (repository == null) { logger.warn( MessageFormat.format("MirrorExecutor is missing repository {0}?!?", repositoryName)); continue; } // automatically repair (some) invalid fetch ref specs if (!repairAttempted.contains(repositoryName)) { repairAttempted.add(repositoryName); JGitUtils.repairFetchSpecs(repository); } // find the first mirror remote - there should only be one StoredConfig rc = repository.getConfig(); RemoteConfig mirror = null; List<RemoteConfig> configs = RemoteConfig.getAllRemoteConfigs(rc); for (RemoteConfig config : configs) { if (config.isMirror()) { mirror = config; break; } } if (mirror == null) { // repository does not have a mirror remote logger.debug("mirror is skipping {} no mirror remote found", repositoryName); continue; } logger.debug("checking {} remote {} for ref updates", repositoryName, mirror.getName()); final boolean testing = false; Git git = new Git(repository); FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call(); Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates(); if (refUpdates.size() > 0) { ReceiveCommand ticketBranchCmd = null; for (TrackingRefUpdate ru : refUpdates) { StringBuilder sb = new StringBuilder(); sb.append("updated mirror "); sb.append(repositoryName); sb.append(" "); sb.append(ru.getRemoteName()); sb.append(" -> "); sb.append(ru.getLocalName()); if (ru.getResult() == Result.FORCED) { sb.append(" (forced)"); } sb.append(" "); sb.append(ru.getOldObjectId() == null ? "" : ru.getOldObjectId().abbreviate(7).name()); sb.append(".."); sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name()); logger.info(sb.toString()); if (BranchTicketService.BRANCH.equals(ru.getLocalName())) { ReceiveCommand.Type type = null; switch (ru.getResult()) { case NEW: type = Type.CREATE; break; case FAST_FORWARD: type = Type.UPDATE; break; case FORCED: type = Type.UPDATE_NONFASTFORWARD; break; default: type = null; break; } if (type != null) { ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getLocalName(), type); } } } if (ticketBranchCmd != null) { repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd)); } } } catch (Exception e) { logger.error("Error updating mirror " + repositoryName, e); } finally { // cleanup if (repository != null) { repository.close(); } } } running.set(false); }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Fetch updates from the remote repository. If refSpecs is unspecifed, * remote heads, tags, and notes are retrieved. * * @param credentialsProvider/*from w w w. j a v a2s. c o m*/ * @param repository * @param refSpecs * @return FetchResult * @throws Exception */ public static FetchResult fetchRepository(CredentialsProvider credentialsProvider, Repository repository, RefSpec... refSpecs) throws Exception { Git git = new Git(repository); FetchCommand fetch = git.fetch(); List<RefSpec> specs = new ArrayList<RefSpec>(); if (refSpecs == null || refSpecs.length == 0) { specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); specs.add(new RefSpec("+refs/tags/*:refs/tags/*")); specs.add(new RefSpec("+refs/notes/*:refs/notes/*")); } else { specs.addAll(Arrays.asList(refSpecs)); } if (credentialsProvider != null) { fetch.setCredentialsProvider(credentialsProvider); } fetch.setRefSpecs(specs); FetchResult fetchRes = fetch.call(); return fetchRes; }
From source file:com.googlesource.gerrit.plugins.github.git.PullRequestImportJob.java
License:Apache License
private void fetchGitHubPullRequest(Repository gitRepo, GHPullRequest pr) throws GitAPIException, InvalidRemoteException, TransportException { status.update(Code.SYNC, "Fetching", "Fetching PullRequests from GitHub"); Git git = Git.wrap(gitRepo); FetchCommand fetch = git.fetch(); fetch.setRemote(ghRepository.getCloneUrl()); fetch.setRefSpecs(// w w w . java2 s. co m new RefSpec("+refs/pull/" + pr.getNumber() + "/head:refs/remotes/origin/pr/" + pr.getNumber())); fetch.setProgressMonitor(this); fetch.call(); }