private String retrieveRevisionToBuild(PushHook hook, GitSCM gitSCM) throws NoRevisionToBuildException { if (inNoBranchDelete(hook)) { if (gitSCM != null && gitSCM.getRepositories().size() == 1) { String repositoryName = gitSCM.getRepositories().get(0).getName(); return hook.getRef().replaceFirst("^refs/heads", "remotes/" + repositoryName); } else { return hook.getAfter(); } } else { throw new NoRevisionToBuildException(); } }
if (scm instanceof GitSCM) { GitSCM git = (GitSCM) scm; if (git.getExtensions().get(IgnoreNotifyCommit.class) != null) { return false; for (RemoteConfig repository : git.getRepositories()) { for (URIish remoteURL : repository.getURIs()) { if (GitStatus.looselyMatches(uri, remoteURL)) { for (BranchSpec branchSpec : git.getBranches()) { if (branchSpec.getName().contains("$")) { if (branchSpec.matches(repository.getName() + "/" + ref)) { return true;
private boolean isShallow(final GitSCM git) { CloneOption option = git.getExtensions().get(CloneOption.class); if (option != null) { return option.isShallow(); } return false; }
listener.getLogger().println("Using checkout strategy: " + getBuildChooser().getDisplayName()); BuildData previousBuildData = getBuildData(build.getPreviousBuild()); // read only BuildData buildData = copyBuildData(build.getPreviousBuild()); GitClient git = createClient(listener, environment, build, workspace); retrieveChanges(build, git, listener); Build revToBuild = determineRevisionToBuild(build, buildData, environment, git, listener); String localBranchName = getParamLocalBranch(build, listener); if (localBranch != null && localBranch.getName() != null) { // null for a detached HEAD String remoteBranchName = getBranchName(localBranch); environment.put(GIT_BRANCH, remoteBranchName); LocalBranch lb = getExtensions().get(LocalBranch.class); if (lb != null) { String lbn = lb.getLocalBranch(); if (lbn == null || lbn.equals("**")) { localBranchName = deriveLocalBranchName(remoteBranchName); CheckoutCommand checkoutCommand = git.checkout().branch(localBranchName).ref(revToBuild.revision.getSha1String()).deleteBranchIfExist(true); for (GitSCMExtension ext : this.getExtensions()) { ext.decorateCheckoutCommand(this, build, git, listener, checkoutCommand); printCommitMessageToLog(listener, git, revToBuild); } catch (GitException ge) { listener.getLogger().println("Exception logging commit message for " + revToBuild + ": " + ge.getMessage());
public void buildEnvironment(Run<?, ?> build, java.util.Map<String, String> env) { Revision rev = fixNull(getBuildData(build)).getLastBuiltRevision(); if (rev!=null) { Branch branch = Iterables.getFirst(rev.getBranches(), null); if (branch!=null && branch.getName()!=null) { String remoteBranchName = getBranchName(branch); env.put(GIT_BRANCH, remoteBranchName); LocalBranch lb = getExtensions().get(LocalBranch.class); if (lb != null) { if (localBranchName == null || localBranchName.equals("**")) { localBranchName = deriveLocalBranchName(remoteBranchName); String prevCommit = getLastBuiltCommitOfBranch(build, branch); if (prevCommit != null) { env.put(GIT_PREVIOUS_COMMIT, prevCommit); String prevSuccessfulCommit = getLastSuccessfulBuiltCommitOfBranch(build, branch); if (prevSuccessfulCommit != null) { env.put(GIT_PREVIOUS_SUCCESSFUL_COMMIT, prevSuccessfulCommit); getDescriptor().populateEnvironmentVariables(env); for (GitSCMExtension ext : extensions) { ext.populateEnvironmentVariables(this, env);
listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName()); final BuildData buildData = fixNull(getBuildData(lastBuild)); if (buildData.lastBuild != null) { listener.getLogger().println("[poll] Last Built Revision: " + buildData.lastBuild.revision); final String singleBranch = getSingleBranch(pollEnv); if (!requiresWorkspaceForPolling(pollEnv)) { GitClient git = createClient(listener, environment, project, Jenkins.getInstance(), null); for (RemoteConfig remoteConfig : getParamExpandedRepos(lastBuild, listener)) { String remote = remoteConfig.getName(); List<RefSpec> refSpecs = getRefSpecs(remoteConfig, environment); for (BranchSpec branchSpec : getBranches()) { for (Entry<String, ObjectId> entry : heads.entrySet()) { final String head = entry.getKey(); FilePath workingDirectory = workingDirectory(project,workspace,environment,listener); GitClient git = createClient(listener, environment, project, node, workingDirectory); for (RemoteConfig remoteRepository : getParamExpandedRepos(lastBuild, listener)) { fetchFrom(git, listener, remoteRepository); Collection<Revision> candidates = getBuildChooser().getCandidateRevisions( true, singleBranch, git, listener, buildData, new BuildChooserContextImpl(project, null, environment));
FilePath workspace = build.getWorkspace(); GitClient cli = scm.createClient(listener, environment, build, workspace); if(scm.getExtensions().get(RelativeTargetDirectory.class) != null) { relativeTargetDirectory = scm.getExtensions().get(RelativeTargetDirectory.class).getRelativeTargetDir();
List<String> revShow; if (buildData != null && buildData.lastBuild != null) { if (getExtensions().get(PathRestriction.class) != null) { revShow = git.showRevision(buildData.lastBuild.revision.getSha1(), r.getSha1()); } else { revShow = git.showRevision(buildData.lastBuild.revision.getSha1(), r.getSha1(), false); revShow = git.showRevision(r.getSha1()); for (String line : revShow) { if (line.startsWith("commit ") && idx!=0) { GitChangeSet change = new GitChangeSet(revShow.subList(start,idx), getExtensions().get(AuthorInChangelog.class)!=null);
ChangelogCommand changelog = git.changelog(); changelog.includes(revToBuild.getSha1()); try (Writer out = new OutputStreamWriter(changelogFile.write(),"UTF-8")) { boolean exclusion = false; ChangelogToBranch changelogToBranch = getExtensions().get(ChangelogToBranch.class); if (changelogToBranch != null) { listener.getLogger().println("Using 'Changelog to branch' strategy."); } else { for (Branch b : revToBuild.getBranches()) { Build lastRevWas = getBuildChooser().prevBuildForChangelog(b.getName(), previousBuildData, git, context); if (lastRevWas != null && lastRevWas.revision != null && git.isCommitInRepo(lastRevWas.getSHA1())) { changelog.excludes(lastRevWas.getSHA1()); exclusion = true;
EnvVars environment = build.getEnvironment(listener); final GitClient git = gitSCM.createClient(listener, environment, build, build.getWorkspace()); if (!gitSCM.getSkipTag()) { git.tag(buildnumber, "Jenkins Build #" + buildNumber); PreBuildMergeOptions mergeOptions = gitSCM.getMergeOptions(); remote = gitSCM.getParamExpandedRepo(environment, remote); listener.getLogger().println("Pushing HEAD to branch " + mergeTarget + " of " + remote.getName() + " repository"); RemoteConfig remote = gitSCM.getRepositoryByName(t.getTargetRepoName()); remote = gitSCM.getParamExpandedRepo(environment, remote); boolean tagExists = git.tagExists(tagName.replace(' ', '_')); RemoteConfig remote = gitSCM.getRepositoryByName(b.getTargetRepoName()); remote = gitSCM.getParamExpandedRepo(environment, remote); RemoteConfig remote = gitSCM.getRepositoryByName(b.getTargetRepoName()); remote = gitSCM.getParamExpandedRepo(environment, remote);
public String getRemoteUrl(String defaultRemoteUrl) { if (StringUtils.isBlank(defaultRemoteUrl)) { RemoteConfig remoteConfig = getJenkinsScm().getRepositories().get(0); URIish uri = remoteConfig.getURIs().get(0); return uri.toPrivateString(); } return defaultRemoteUrl; }
private void addRemoteRepoToConfig(GitClient client) throws InterruptedException { GitSCM gitScm = getJenkinsScm(); for (RemoteConfig uc : gitScm.getRepositories()) { if (client.getRemoteUrl(uc.getName()) == null) client.setRemoteUrl(uc.getName(), uc.getURIs().get(0).toPrivateASCIIString()); } }
final BuildData buildData = fixNull(getBuildData(lastBuild, false)); final String singleBranch = GitUtils.getSingleBranch(lastBuild, getRepositories(), getBranches()); final EnvVars environment = GitUtils.getPollEnvironment(project, workspace, launcher, listener, isPollSlaves()); IGitAPI git = new GitAPI(gitExe, workspace, listener, environment); String gitRepo = getParamExpandedRepos(lastBuild).get(0).getURIs().get(0).toString(); String headRevision = git.getHeadRev(gitRepo, getBranches().get(0).getName()); String lastBuildRevision = buildData.lastBuild.getRevision().getSha1String(); } else if (getExcludedCommits().contains(headRevision)) { listener.getLogger().println("Ignored commit " + headRevision + ": This commit has been explicitly excluded from triggering builds."); for (String commit : getExcludedCommits()) { listener.getLogger().println("[poll] Excluded commit: " + commit); if (!isPollSlaves()) { launcher = new Launcher.LocalLauncher(null); workspace = getLocalWorkspace(project, listener); if (workspace == null) { return PollingResult.NO_CHANGES; gitExe = getGitExe(Hudson.getInstance(), listener); } else { gitExe = getGitExe(label.getNodes().iterator().next(), listener); } else { gitExe = getGitExe(project.getLastBuiltOn(), listener);
scmFound = true; for (RemoteConfig repository : git.getRepositories()) { boolean repositoryMatches = false, branchMatches = false; for (URIish remoteURL : repository.getURIs()) { if (looselyMatches(uri, remoteURL)) { repositoryMatches = true; if (!repositoryMatches || git.isIgnoreNotifyCommit()) { continue; branchMatches = true; } else { for (BranchSpec branchSpec : git.getBranches()) { for (String branch : branches) { if (branchSpec.matches(repository.getName() + "/" + branch)) { branchMatches = true; break;
public ReleaseRepository getRemoteConfig(String defaultRemoteNameOrUrl) throws IOException { List<RemoteConfig> repositories = getJenkinsScm().getRepositories(); if (StringUtils.isBlank(defaultRemoteNameOrUrl)) { if (repositories == null || repositories.isEmpty()) { throw new GitException("Git remote config repositories are null or empty."); } return new ReleaseRepository(repositories.get(0).getURIs().get(0), repositories.get(0).getName()); } for (RemoteConfig remoteConfig : repositories) { if (remoteConfig.getName().equals(defaultRemoteNameOrUrl)) { return new ReleaseRepository(remoteConfig.getURIs().get(0), remoteConfig.getName()); } } if (checkGitValidUri(defaultRemoteNameOrUrl)) { return new ReleaseRepository(defaultRemoteNameOrUrl, "externalGitUrl"); } throw new IOException("Target Remote Name: " + defaultRemoteNameOrUrl + " ,doesn`t exist"); }
final String gitExe = getGitExe(build.getBuiltOn(), listener); final BuildData buildData = getBuildData(build.getPreviousBuild(), true); String confName = getGitConfigNameToUse(); if (StringUtils.isNotBlank(confName)) { environment.put(GitConstants.GIT_COMMITTER_NAME_ENV_VAR, confName); environment.put(GitConstants.GIT_AUTHOR_NAME_ENV_VAR, confName); String confEmail = getGitConfigEmailToUse(); if (StringUtils.isNotBlank(confEmail)) { environment.put(GitConstants.GIT_COMMITTER_EMAIL_ENV_VAR, confEmail); final String singleBranch = GitUtils.getSingleBranch(build, getRepositories(), getBranches()); final String paramLocalBranch = getParamLocalBranch(build); Revision tempParentLastBuiltRev = null; final List<RemoteConfig> paramRepos = getParamExpandedRepos(build); Map<String, List<RemoteConfig>> repoMap = getRemoteConfigMap(paramRepos); FilePath workingDirectory = workingDirectory(workspace); final Revision revToBuild = gerRevisionToBuild(listener, workingDirectory, gitExe, buildData, environment, singleBranch, repos, parentLastBuiltRev, rpa); buildConfig = getMergedBuildConfig(listener, workingDirectory, buildNumber, gitExe, buildData, environment, paramLocalBranch, revToBuild, internalTagName, internalTagComment);
@Nonnull @Override public SCM build(@Nonnull SCMHead head, @CheckForNull SCMRevision revision) { GitSCM scm; if (head instanceof GitLabSCMHead) { scm = ((GitLabSCMHead) head).createSCM(this); if (revision instanceof SCMRevisionImpl) { scm.getExtensions().add(new BuildChooserSetting(new SpecificRevisionBuildChooser((SCMRevisionImpl) revision))); } } else { scm = (GitSCM) super.build(head, revision); scm.setBrowser(getBrowser()); } return scm; }
@Override public GitSCM createScm(GitHubSCMSource scmSource, SCMHead scmHead, SCMRevision scmRevision) { return new GitSCM( gitSCM.getUserRemoteConfigs(), gitSCM.getBranches(), gitSCM.isDoGenerateSubmoduleConfigurations(), gitSCM.getSubmoduleCfg(), gitSCM.getBrowser(), gitSCM.getGitTool(), gitSCM.getExtensions() ); }
private String getSingleBranch(EnvVars env) { if (getBranches().size() != 1) { return null; String branch = getBranches().get(0).getName(); String repository = null; if (getRepositories().size() != 1) { for (RemoteConfig repo : getRepositories()) { if (branch.startsWith(repo.getName() + "/")) { repository = repo.getName(); break; repository = getRepositories().get(0).getName(); branch = getParameterString(branch, env);
if (scm instanceof GitSCM) { GitSCM gitSCM = (GitSCM) scm; List<RemoteConfig> repositories = gitSCM.getRepositories(); if (repositories != null && repositories.size() > 0) { RemoteConfig remoteConfig = repositories.get(0); List<URIish> urIs = remoteConfig.getURIs(); if (urIs != null && urIs.size() > 0) { URIish urIish = urIs.get(0); if (gitUrl != null && gitUrl.length() > 0) { String ref = null; List<BranchSpec> branches = gitSCM.getBranches(); if (branches != null && branches.size() > 0) { BranchSpec branchSpec = branches.get(0);