/** * Gets the push uris from the given remoteConfig. * * @param remoteConfig the remote config * @return the push ur is */ private static Collection<URIish> getPushURIs(RemoteConfig remoteConfig) { List<URIish> pushURIs = new ArrayList<URIish>(); for (URIish uri : remoteConfig.getPushURIs()) { pushURIs.add(uri); } if (pushURIs.isEmpty() && !remoteConfig.getURIs().isEmpty()) { pushURIs.add(remoteConfig.getURIs().get(0)); } return pushURIs; }
private List<RemoteConfig> getUsableConfigs(final List<RemoteConfig> remotes) { if (remotes == null) return null; List<RemoteConfig> result = new ArrayList<RemoteConfig>(); for (RemoteConfig config : remotes) if ((sourceSelection && !config.getURIs().isEmpty() || !sourceSelection && (!config.getPushURIs().isEmpty() || !config.getURIs().isEmpty()))) result.add(config); if (!result.isEmpty()) return result; return null; }
public static List<URIish> getDefaultRemoteURIs(IProject project) throws CoreException { RemoteConfig remoteConfig = getRemoteConfig(getRepository(project)); if (remoteConfig != null) { return remoteConfig.getURIs(); } return new ArrayList<URIish>(); }
/** * Returns the URIish's for the remote on the given project. * * @param remoteName * @param project * @return * @throws CoreException */ public static List<URIish> getRemoteURIs(String remoteName, IProject project) throws CoreException { List<URIish> uris = Collections.emptyList(); RemoteConfig remoteConfig = getRemoteByName(remoteName, getRepository(project)); if (remoteConfig != null) { uris = remoteConfig.getURIs(); } return uris; }
public void push(RemoteConfig repository, String refspec) throws GitException { ArgumentListBuilder args = new ArgumentListBuilder(); args.add("push", repository.getURIs().get(0).toPrivateString()); if (refspec != null) args.add(refspec); launchCommand(args); // Ignore output for now as there's many different formats // That are possible. }
private boolean hasRepository(String repoUrl, GitSCM scm) { for (RemoteConfig remote : ((GitSCM) scm).getRepositories()) { for (URIish uri : remote.getURIs()) { if (uri.toString().equals(repoUrl)) { return true; } } } return false; }
/** * Returns all uris of alls remotes for the given project. * * @param project the project to get all remotes and all uris from * @return all uris * @throws CoreException */ public static List<URIish> getAllRemoteURIs(IProject project) throws CoreException { List<RemoteConfig> remoteConfigs = getAllRemoteConfigs(getRepository(project)); List<URIish> uris = new ArrayList<URIish>(); if (remoteConfigs != null) { for (RemoteConfig remoteConfig : remoteConfigs) { uris.addAll(remoteConfig.getURIs()); } } return uris; }
public static boolean hasRemoteUrl(Pattern pattern, RemoteConfig config) { if (config == null) { return false; } for (URIish uri : config.getURIs()) { Matcher matcher = pattern.matcher(uri.toString()); if (matcher.find()) { return true; } } return false; }
/** @since 1.1 */ protected void addRepositories(Set<GitHubRepositoryName> r, SCM scm) { if (scm instanceof GitSCM) { GitSCM git = (GitSCM) scm; for (RemoteConfig rc : git.getRepositories()) { for (URIish uri : rc.getURIs()) { String url = uri.toString(); GitHubRepositoryName repo = GitHubRepositoryName.create(url); if (repo != null) { r.add(repo); } } } } }
/** * Start from scratch and clone the whole repository. Cloning into an existing directory is not * allowed, so the workspace is first deleted entirely, then <tt>git clone</tt> is performed. * * @param remoteConfig remote config * @throws GitException if deleting or cloning the workspace fails */ public void clone(final RemoteConfig remoteConfig) throws GitException { listener.getLogger().println("Cloning repository " + remoteConfig.getName()); final int[] gitVer = getGitVersion(); // TODO: Not here! try { workspace.deleteRecursive(); } catch (Exception e) { e.printStackTrace(listener.error("Failed to clean the workspace")); throw new GitException("Failed to delete workspace", e); } // Assume only 1 URL for this repository final String source = remoteConfig.getURIs().get(0).toPrivateString(); try { workspace.act( new FileCallable<String>() { private static final long serialVersionUID = 1L; public String invoke(File workspace, VirtualChannel channel) throws IOException { final ArgumentListBuilder args = new ArgumentListBuilder(); args.add("clone"); if ((gitVer[0] >= 1) && (gitVer[1] >= 7)) { args.add("--progress"); } if (reference != null) { File referencePath = new File(reference); if (referencePath.exists() && referencePath.isDirectory()) { args.add("--reference", reference); } } args.add("-o", remoteConfig.getName()); args.add(source); args.add(workspace.getAbsolutePath()); return launchCommandIn(args, null); } }); } catch (Exception e) { throw new GitException("Could not clone " + source, e); } }
private static RevCommit fetchRefSpec( IProgressMonitor monitor, Repository repository, RemoteConfig remote, RefSpec refSpec) throws URISyntaxException, CoreException, MissingObjectException, IncorrectObjectTypeException, IOException { List<RefSpec> refSpecs = Collections.singletonList(refSpec); FetchOperationUI op = new FetchOperationUI( repository, remote.getURIs().get(0), refSpecs, Activator.getDefault() .getPreferenceStore() .getInt(UIPreferences.REMOTE_CONNECTION_TIMEOUT), false); op.setCredentialsProvider(new EGitCredentialsProvider()); FetchResult result = op.execute(monitor); ObjectId resultRef = result.getAdvertisedRef(refSpec.getSource()).getObjectId(); return new RevWalk(repository).parseCommit(resultRef); }
public void fetch(RemoteConfig remoteRepository) throws GitException { // Assume there is only 1 URL / refspec for simplicity fetch( remoteRepository.getURIs().get(0).toPrivateString(), remoteRepository.getFetchRefSpecs().get(0).toString()); }
public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSCM git) throws IOException, InterruptedException { Map<String, String> paramList = new LinkedHashMap<String, String>(); // for (AbstractProject<?,?> project : // Hudson.getInstance().getItems(AbstractProject.class)) { if (project.getSomeWorkspace() == null) { this.errorMessage = "noWorkspace"; } EnvVars environment = null; try { environment = project.getSomeBuildWithWorkspace().getEnvironment(TaskListener.NULL); } catch (Exception e) { } for (RemoteConfig repository : git.getRepositories()) { LOGGER.log( Level.INFO, "generateContents contenttype " + type + " RemoteConfig " + repository.getURIs()); for (URIish remoteURL : repository.getURIs()) { GitClient newgit = git.createClient( TaskListener.NULL, environment, new Run(project) {}, project.getSomeWorkspace()); FilePath wsDir = null; if (project.getSomeBuildWithWorkspace() != null) { wsDir = project.getSomeBuildWithWorkspace().getWorkspace(); if (wsDir == null || !wsDir.exists()) { LOGGER.log( Level.WARNING, "generateContents create wsDir " + wsDir + " for " + remoteURL); wsDir.mkdirs(); if (!wsDir.exists()) { LOGGER.log(Level.SEVERE, "generateContents wsDir.mkdirs() failed."); String errMsg = "!Failed To Create Workspace"; return Collections.singletonMap(errMsg, errMsg); } newgit.init(); newgit.clone(remoteURL.toASCIIString(), "origin", false, null); LOGGER.log(Level.INFO, "generateContents clone done"); } } else { // probably our first build. We cannot yet fill in any // values. LOGGER.log(Level.INFO, "getSomeBuildWithWorkspace is null"); String errMsg = "!No workspace. Please build the project at least once"; return Collections.singletonMap(errMsg, errMsg); } long time = -System.currentTimeMillis(); FetchCommand fetch = newgit.fetch_().from(remoteURL, repository.getFetchRefSpecs()); fetch.execute(); LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch"); if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) { List<ObjectId> oid; if (this.branch != null && !this.branch.isEmpty()) { oid = newgit.revList(this.branch); } else { oid = newgit.revListAll(); } for (ObjectId noid : oid) { Revision r = new Revision(noid); paramList.put(r.getSha1String(), prettyRevisionInfo(newgit, r)); } } if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) { Set<String> tagSet = newgit.getTagNames(tagFilter); ArrayList<String> orderedTagNames; if (this.getSortMode().getIsSorting()) { orderedTagNames = sortByName(tagSet); if (this.getSortMode().getIsDescending()) Collections.reverse(orderedTagNames); } else { orderedTagNames = new ArrayList<String>(tagSet); } for (String tagName : orderedTagNames) { paramList.put(tagName, tagName); } } if (type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) { time = -System.currentTimeMillis(); Set<String> branchSet = new HashSet<String>(); final boolean wildcard = "*".equals(branchfilter); for (Branch branch : newgit.getRemoteBranches()) { // It'd be nice if we could filter on remote branches via the GitClient, // but that's not an option. final String branchName = branch.getName(); if (wildcard || branchName.matches(branchfilter)) { branchSet.add(branchName); } } LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch branches"); time = -System.currentTimeMillis(); List<String> orderedBranchNames; if (this.getSortMode().getIsSorting()) { orderedBranchNames = sortByName(branchSet); if (this.getSortMode().getIsDescending()) Collections.reverse(orderedBranchNames); } else { orderedBranchNames = new ArrayList<String>(branchSet); } for (String branchName : orderedBranchNames) { paramList.put(branchName, branchName); } LOGGER.finest( "Took " + (time + System.currentTimeMillis()) + "ms to sort and add to param list."); } } break; } return paramList; }
public void createControl(Composite parent) { Clipboard clipboard = new Clipboard(parent.getDisplay()); String clipText = (String) clipboard.getContents(TextTransfer.getInstance()); String defaultUri = null; String defaultCommand = null; String defaultChange = null; if (clipText != null) { final String pattern = "git fetch (\\w+:\\S+) (refs/changes/\\d+/\\d+/\\d+) && git (\\w+) FETCH_HEAD"; //$NON-NLS-1$ Matcher matcher = Pattern.compile(pattern).matcher(clipText); if (matcher.matches()) { defaultUri = matcher.group(1); defaultChange = matcher.group(2); defaultCommand = matcher.group(3); } } Composite main = new Composite(parent, SWT.NONE); main.setLayout(new GridLayout(2, false)); GridDataFactory.fillDefaults().grab(true, true).applyTo(main); new Label(main, SWT.NONE).setText(UIText.FetchGerritChangePage_UriLabel); uriCombo = new Combo(main, SWT.DROP_DOWN); GridDataFactory.fillDefaults().grab(true, false).applyTo(uriCombo); uriCombo.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { changeRefs = null; } }); new Label(main, SWT.NONE).setText(UIText.FetchGerritChangePage_ChangeLabel); refText = new Text(main, SWT.BORDER); if (defaultChange != null) refText.setText(defaultChange); GridDataFactory.fillDefaults().grab(true, false).applyTo(refText); addRefContentProposalToText(refText); Group checkoutGroup = new Group(main, SWT.SHADOW_ETCHED_IN); checkoutGroup.setLayout(new GridLayout(2, false)); GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(checkoutGroup); checkoutGroup.setText(UIText.FetchGerritChangePage_AfterFetchGroup); // radio: create local branch createBranch = new Button(checkoutGroup, SWT.RADIO); GridDataFactory.fillDefaults().span(2, 1).applyTo(createBranch); createBranch.setText(UIText.FetchGerritChangePage_LocalBranchRadio); createBranch.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { checkPage(); } }); branchTextlabel = new Label(checkoutGroup, SWT.NONE); GridDataFactory.defaultsFor(branchTextlabel).exclude(false).applyTo(branchTextlabel); branchTextlabel.setText(UIText.FetchGerritChangePage_BranchNameText); branchText = new Text(checkoutGroup, SWT.SINGLE | SWT.BORDER); GridDataFactory.fillDefaults().grab(true, false).applyTo(branchText); branchText.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { checkPage(); } }); // radio: create tag createTag = new Button(checkoutGroup, SWT.RADIO); GridDataFactory.fillDefaults().span(2, 1).applyTo(createTag); createTag.setText(UIText.FetchGerritChangePage_TagRadio); createTag.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { checkPage(); } }); tagTextlabel = new Label(checkoutGroup, SWT.NONE); GridDataFactory.defaultsFor(tagTextlabel).exclude(true).applyTo(tagTextlabel); tagTextlabel.setText(UIText.FetchGerritChangePage_TagNameText); tagText = new Text(checkoutGroup, SWT.SINGLE | SWT.BORDER); GridDataFactory.fillDefaults().exclude(true).grab(true, false).applyTo(tagText); tagText.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { checkPage(); } }); // radio: checkout FETCH_HEAD checkout = new Button(checkoutGroup, SWT.RADIO); GridDataFactory.fillDefaults().span(2, 1).applyTo(checkout); checkout.setText(UIText.FetchGerritChangePage_CheckoutRadio); checkout.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { checkPage(); } }); // radio: don't checkout dontCheckout = new Button(checkoutGroup, SWT.RADIO); GridDataFactory.fillDefaults().span(2, 1).applyTo(checkout); dontCheckout.setText(UIText.FetchGerritChangePage_UpdateRadio); dontCheckout.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { checkPage(); } }); if ("checkout".equals(defaultCommand)) // $NON-NLS-1$ checkout.setSelection(true); else createBranch.setSelection(true); warningAdditionalRefNotActive = new Composite(main, SWT.NONE); GridDataFactory.fillDefaults() .span(2, 1) .grab(true, false) .exclude(true) .applyTo(warningAdditionalRefNotActive); warningAdditionalRefNotActive.setLayout(new GridLayout(2, false)); warningAdditionalRefNotActive.setVisible(false); activateAdditionalRefs = new Button(warningAdditionalRefNotActive, SWT.CHECK); activateAdditionalRefs.setText(UIText.FetchGerritChangePage_ActivateAdditionalRefsButton); activateAdditionalRefs.setToolTipText( UIText.FetchGerritChangePage_ActivateAdditionalRefsTooltip); refText.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { Change change = Change.fromRef(refText.getText()); if (change != null) { branchText.setText( NLS.bind( UIText.FetchGerritChangePage_SuggestedRefNamePattern, change.getChangeNumber(), change.getPatchSetNumber())); tagText.setText(branchText.getText()); } else { branchText.setText(""); // $NON-NLS-1$ tagText.setText(""); // $NON-NLS-1$ } checkPage(); } }); // get all available URIs from the repository SortedSet<String> uris = new TreeSet<String>(); try { for (RemoteConfig rc : RemoteConfig.getAllRemoteConfigs(repository.getConfig())) { if (rc.getURIs().size() > 0) uris.add(rc.getURIs().get(0).toPrivateString()); for (URIish u : rc.getPushURIs()) uris.add(u.toPrivateString()); } } catch (URISyntaxException e) { Activator.handleError(e.getMessage(), e, false); setErrorMessage(e.getMessage()); } for (String aUri : uris) uriCombo.add(aUri); if (defaultUri != null) uriCombo.setText(defaultUri); else selectLastUsedUri(); refText.setFocus(); Dialog.applyDialogFont(main); setControl(main); checkPage(); }