private void deleteStaleTrackingRefs(FetchResult result, BatchRefUpdate batch) throws IOException { Set<Ref> processed = new HashSet<>(); for (Ref ref : localRefs().values()) { if (ref.isSymbolic()) { continue; } String refname = ref.getName(); for (RefSpec spec : toFetch) { if (spec.matchDestination(refname)) { RefSpec s = spec.expandFromDestination(refname); if (result.getAdvertisedRef(s.getSource()) == null && processed.add(ref)) { deleteTrackingRef(result, batch, s, ref); } } } } }
protected FetchResult fetch(Git git, String label) { FetchCommand fetch = git.fetch(); fetch.setRemote("origin"); fetch.setTagOpt(TagOpt.FETCH_TAGS); fetch.setRemoveDeletedRefs(deleteUntrackedBranches); if (this.refreshRate > 0) { this.setLastRefresh(System.currentTimeMillis()); } configureCommand(fetch); try { FetchResult result = fetch.call(); if (result.getTrackingRefUpdates() != null && result.getTrackingRefUpdates().size() > 0) { logger.info("Fetched for remote " + label + " and found " + result.getTrackingRefUpdates().size() + " updates"); } return result; } catch (Exception ex) { String message = "Could not fetch remote for " + label + " remote: " + git .getRepository().getConfig().getString("remote", "origin", "url"); warn(message, ex); return null; } }
private Ref findBranchToCheckout(FetchResult result) { final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD); ObjectId headId = idHEAD != null ? idHEAD.getObjectId() : null; if (headId == null) { return null; } Ref master = result.getAdvertisedRef(Constants.R_HEADS + Constants.MASTER); ObjectId objectId = master != null ? master.getObjectId() : null; if (headId.equals(objectId)) { return master; } Ref foundBranch = null; for (Ref r : result.getAdvertisedRefs()) { final String n = r.getName(); if (!n.startsWith(Constants.R_HEADS)) continue; if (headId.equals(r.getObjectId())) { foundBranch = r; break; } } return foundBranch; }
protected void showFetchResult(final FetchResult r) throws IOException { ObjectReader reader = db.newObjectReader(); try { boolean shownURI = false; for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) { if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE) continue; final char type = shortTypeOf(u.getResult()); final String longType = longTypeOf(reader, u); final String src = abbreviateRef(u.getRemoteName(), false); final String dst = abbreviateRef(u.getLocalName(), true); if (!shownURI) { outw.println(MessageFormat.format(CLIText.get().fromURI, r.getURI())); shownURI = true; } outw.format(" %c %-17s %-10s -> %s", valueOf(type), longType, //$NON-NLS-1$ src, dst); outw.println(); } } finally { reader.release(); } showRemoteMessages(errw, r.getMessages()); }
private Collection<Ref> fetch(final Repository repository, final RemoteConfig remoteConfig, final CharSequence repoBranch) { try (Transport transport = Transport.open(repository, remoteConfig)) { configure(transport); transport.setCheckFetchedObjects(false); transport.setRemoveDeletedRefs(false); transport.setDryRun(false); transport.setTagOpt(TagOpt.FETCH_TAGS); transport.setFetchThin(false); final ProgressMonitor monitor = new EmptyProgressMonitor() { }; FetchResult result = transport.fetch(monitor, Arrays.asList( new RefSpec("refs/heads/" + repoBranch) )); logger.debug(String.format("Fetched %s: %s", result.getURI(), result.getMessages())); result.getAdvertisedRefs().stream().forEach(ref -> { logger.debug(String.format("Remote Ref: %s = %s", ref.getName(), ref.getObjectId())); }); return result.getAdvertisedRefs(); } catch (Throwable e) { throw new RuntimeException(e); } }
private void pull() throws GitAPIException { Git git = recordGit(Git.wrap(repository)); PullCommand pull = git.pull() .setRebase(true); PullResult result = pull.call(); if (!result.isSuccessful()) { LOGGER.log(Level.WARNING, () -> String.format("Cannot pull from git '%s', branch '%s'", uri.toASCIIString(), branch)); if (LOGGER.isLoggable(Level.FINEST)) { Status status = git.status().call(); LOGGER.finest(() -> "git status cleanliness: " + status.isClean()); if (!status.isClean()) { LOGGER.finest(() -> "git status uncommitted changes: " + status.getUncommittedChanges()); LOGGER.finest(() -> "git status untracked: " + status.getUntracked()); } } } else { LOGGER.fine("Pull was successful."); } LOGGER.finest(() -> "git rebase result: " + result.getRebaseResult().getStatus().name()); LOGGER.finest(() -> "git fetch result: " + result.getFetchResult().getMessages()); }
public RevCommit gitFetch() throws Exception { FetchResult result = git.fetch().setRemote("origin").call(); System.out.println("Fetch : " + result.getURI().toString()); return revWalk.parseCommit(result.getAdvertisedRef("refs/heads/" + localRepo.getBranch()).getTarget().getObjectId()); }
@Override public List<Ref> getRemoteBranches(final Git repo) throws GitAPIException { List<Ref> results = new ArrayList<>(); try { FetchResult fetch = repo.fetch().setRemote(GIT_REMOTE_ORIGIN).call(); Collection<Ref> refs = fetch.getAdvertisedRefs(); for (Ref ref : refs) { if (ref.getName().startsWith(GIT_REFS_HEADS)) { results.add(ref); } } } catch (InvalidRemoteException e) { e.printStackTrace(); } return results; }
private void updateFETCH_HEAD(FetchResult result) throws IOException { File meta = transport.local.getDirectory(); if (meta == null) return; final LockFile lock = new LockFile(new File(meta, "FETCH_HEAD")); //$NON-NLS-1$ try { if (lock.lock()) { try (Writer w = new OutputStreamWriter( lock.getOutputStream(), UTF_8)) { for (FetchHeadRecord h : fetchHeadUpdates) { h.write(w); result.add(h); } } lock.commit(); } } finally { lock.unlock(); } }
final FetchResult result = new FetchResult(); new FetchProcess(this, toFetch).execute(monitor, result);
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().length() >= THRESHOLD ){ FetchResult fetchResult = new FetchResult(); fetchResult.execute(s.toString()); } }
conn = transport.openFetch(); try { result.setAdvertisedRefs(transport.getURI(), conn.getRefsMap()); result.peerUserAgent = conn.getPeerUserAgent(); final Set<Ref> matched = new HashSet<Ref>(); deleteStaleTrackingRefs(result, batch); for (TrackingRefUpdate u : localUpdates) { result.add(u); batch.addCommand(u.asReceiveCommand());
/** * @param parentShell * @param localDb * @param result * @param sourceString */ public FetchResultDialog(final Shell parentShell, final Repository localDb, final FetchResult result, final String sourceString) { super(parentShell); setShellStyle(getShellStyle() & ~SWT.APPLICATION_MODAL | SWT.RESIZE); setBlockOnOpen(false); this.localDb = localDb; this.result = new FetchOperationResult(result.getURI(), result); this.sourceString = sourceString; }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { System.out.println("Starting fetch"); try (Git git = new Git(repository)) { FetchResult result = git.fetch().setCheckFetchedObjects(true).call(); System.out.println("Messages: " + result.getMessages()); } } } }
/** * Fetches all refs from "fabric git repo" to "patch git repo" - very important operation that keeps * both repositories synchronized. * @param mainRepository * @throws GitAPIException */ private void fetchFabricPatchData(Git mainRepository) throws GitAPIException { if (mainRepository.getRepository().getConfig() != null) { String url = mainRepository.getRepository().getConfig().getString("remote", "origin", "url"); if (url != null) { Activator.log(LogService.LOG_INFO, "Fetching data from " + url); // let's fetch from origin to check if someone else already does fabric patch management FetchResult result = mainRepository.fetch() .setRemote("origin") .setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")) .setTagOpt(TagOpt.FETCH_TAGS) .call(); Set<String> tags = new TreeSet<>(); for (Ref ref : result.getAdvertisedRefs()) { if (ref.getName().startsWith("refs/tags/baseline")) { tags.add(ref.getName().substring("refs/tags/".length())); } } Activator.log(LogService.LOG_INFO, "Available tags: " + tags); } else { Activator.log(LogService.LOG_WARNING, "Repository " + mainRepository.getRepository().getWorkTree() + " is not connected with Fabric git repository"); } } }
private void addUpdateBatchCommands(FetchResult result, BatchRefUpdate batch) throws TransportException { Map<String, ObjectId> refs = new HashMap<>(); for (TrackingRefUpdate u : localUpdates) { // Try to skip duplicates if they'd update to the same object ID ObjectId existing = refs.get(u.getLocalName()); if (existing == null) { refs.put(u.getLocalName(), u.getNewObjectId()); result.add(u); batch.addCommand(u.asReceiveCommand()); } else if (!existing.equals(u.getNewObjectId())) { throw new TransportException(MessageFormat .format(JGitText.get().duplicateRef, u.getLocalName())); } } }
final FetchResult result = new FetchResult(); new FetchProcess(this, toFetch).execute(monitor, result); return result;
head = result.getAdvertisedRef(branch); if (head == null) head = result.getAdvertisedRef(Constants.R_HEADS + branch); if (head == null) head = result.getAdvertisedRef(Constants.R_TAGS + branch);
private static Ref guessHEAD(final FetchResult result) { final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD); Ref head = null; for (final Ref r : result.getAdvertisedRefs()) { final String n = r.getName(); if (!n.startsWith(Constants.R_HEADS)) continue; if (idHEAD == null || head != null) continue; if (r.getObjectId().equals(idHEAD.getObjectId())) head = r; } if (idHEAD != null && head == null) head = idHEAD; return head; }
FetchResult fetchStatus = fetch(git, label); if (deleteUntrackedBranches && fetchStatus != null) { deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);