private static String getBuildRevision(Run<?, ?> build) { GitLabWebHookCause cause = build.getCause(GitLabWebHookCause.class); if (cause != null) { return cause.getData().getLastCommit(); } BuildData action = build.getAction(BuildData.class); if (action == null) { throw new IllegalStateException("No (git-plugin) BuildData associated to current build"); } Revision lastBuiltRevision = action.getLastBuiltRevision(); if (lastBuiltRevision == null) { throw new IllegalStateException("Last build has no associated commit"); } return action.getLastBuild(lastBuiltRevision.getSha1()).getMarked().getSha1String(); }
public static Run<?, ?> getBuildByBranch(Job<?, ?> project, String branchName) { for (Run<?, ?> build : project.getBuilds()) { BuildData data = build.getAction(BuildData.class); MergeRecord merge = build.getAction(MergeRecord.class); if (hasLastBuild(data) && isNoMergeBuild(data, merge)) { for (Branch branch : data.lastBuild.getRevision().getBranches()) { if (branch.getName().endsWith("/" + branchName)) { return build; } } } } return null; }
if (StringUtils.isBlank(baseHash)) { try { baseObjectId = git.revParse(Constants.R_REFS + baseName); } catch (GitException e) { listener.getLogger().printf("Unable to determine head revision of %s prior to merge with PR%n", baseObjectId = ObjectId.fromString(baseHash); baseName, baseObjectId.name(), rev.getSha1String() ); checkout(scm, build, git, listener, rev); try { git.setAuthor("Jenkins", "nobody@nowhere"); git.setCommitter("Jenkins", "nobody@nowhere"); MergeCommand cmd = git.merge().setRevisionToMerge(baseObjectId); for (GitSCMExtension ext : scm.getExtensions()) { new MergeRecord(baseName, baseObjectId.getName())); // does not seem to be used, but just in case ObjectId mergeRev = git.revParse(Constants.HEAD); listener.getLogger().println("Merge succeeded, producing " + mergeRev.name()); return new Revision(mergeRev, rev.getBranches()); // note that this ensures Build.revision != Build.marked
@Override public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, Revision marked, Revision rev) throws IOException, InterruptedException { String remoteBranchRef = GitSCM.getParameterString(options.getRef(), build.getEnvironment(listener)); if (rev.containsBranchName(remoteBranchRef)) return rev; ObjectId target = git.revParse(remoteBranchRef); String paramLocalBranch = scm.getParamLocalBranch(build, listener); CheckoutCommand checkoutCommand = git.checkout().branch(paramLocalBranch).ref(remoteBranchRef).deleteBranchIfExist(true); for (GitSCMExtension ext : scm.getExtensions()) ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand); checkoutCommand.execute(); MergeCommand cmd = git.merge().setRevisionToMerge(rev.getSha1()); for (GitSCMExtension ext : scm.getExtensions()) ext.decorateMergeCommand(scm, build, git, listener, cmd); checkoutCommand = git.checkout().branch(paramLocalBranch).ref(rev.getSha1String()).deleteBranchIfExist(true); for (GitSCMExtension ext : scm.getExtensions()) ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand); build.addAction(new MergeRecord(remoteBranchRef,target.getName())); Revision mergeRevision = new GitUtils(listener,git).getRevisionForSHA1(git.revParse(HEAD)); mergeRevision.getBranches().add(new Branch(remoteBranchRef, target)); return mergeRevision;
public int difference(Map<IndexEntry, Revision> item, List<IndexEntry> entries) { int difference = 0; if (entries.size() != item.keySet().size()) return -1; for (IndexEntry entry : entries) { Revision b = null; for (Map.Entry<IndexEntry, Revision> entryAndRevision : item.entrySet()) { IndexEntry e = entryAndRevision.getKey(); if (e.getFile().equals(entry.getFile())) b = entryAndRevision.getValue(); } if (b == null) return -1; if (!entry.getObject().equals(b.getSha1().getName())) difference++; } return difference; }
for (Branch b : revToBuild.getBranches()) { Build lastRevWas = buildChooser.prevBuildForChangelog(b.getName(), buildData, git); if (lastRevWas != null) { if (git.isCommitInRepo(lastRevWas.getSHA1().name())) { changeLog.append(putChangelogDiffsIntoFile(git, b.name, lastRevWas.getSHA1().name(), revToBuild.getSha1().name())); histories++; } else { listener.getLogger().println("No change to record in branch " + b.getName());
public Build getLastBuild(ObjectId sha1) { // fast check by first checking most recent build if (lastBuild != null && (lastBuild.revision.getSha1().equals(sha1) || lastBuild.marked.getSha1().equals(sha1))) return lastBuild; try { for(Build b : buildsByBranchName.values()) { if(b.revision.getSha1().equals(sha1) || b.marked.getSha1().equals(sha1)) return b; } return null; } catch(Exception ex) { return null; } }
git.checkoutBranch(paramLocalBranch, target.name()); git.merge(revToBuild.getSha1().name()); } catch (Exception ex) { listener git.checkoutBranch(paramLocalBranch, revToBuild.getSha1().name()); buildData.saveBuild(new Build(revToBuild, buildNumber, Result.FAILURE)); return new BuildConfig(null, buildData); buildData.saveBuild(build); GitUtils gu = new GitUtils(listener, git); build.mergeRevision = gu.getRevisionForSHA1(target); if (getClean()) { listener.getLogger().println("Cleaning workspace"); git.clean();
for (Iterator<Branch> j = r.getBranches().iterator(); j.hasNext();) { Branch b = j.next(); boolean keep = false; for (BranchSpec bspec : gitSCM.getBranches()) { if (bspec.matches(b.getName(), env)) { keep = true; break; if (r.getBranches().size() > 1) { for (Iterator<Branch> j = r.getBranches().iterator(); j.hasNext();) { Branch b = j.next(); if (HEAD.matches(b.getName(), env)) { verbose(listener, "Ignoring {0} because there''s named branch for this revision", b.getName()); j.remove(); if (r.getBranches().size() == 0) { verbose(listener, "Ignoring {0} because we don''t care about any of the branches that point to it", r); i.remove(); if (data.hasBeenBuilt(r.getSha1())) { i.remove(); if (lastBuiltRevision != null && lastBuiltRevision.getSha1().equals(r.getSha1())) { lastBuiltRevision = r; verbose(listener, "Nothing seems worth building, so falling back to the previously built revision: {0}", data.getLastBuiltRevision()); return Collections.singletonList(utils.sortBranchesForRevision(lastBuiltRevision, gitSCM.getBranches(), env));
public void testGerritTriggerBuildChooser() throws Exception { GerritTriggerBuildChooser chooser = new GerritTriggerBuildChooser(); final ObjectId fetchHead = ObjectId.fromString("7f3547c6d55946e25e99a847b5160d69e59994ba"); final ObjectId patchsetRevision = ObjectId.fromString("38b0940738376ee1b66c332a2cb6d4d37bafa4e4"); final String singleBranch = "origin/master"; final String patchsetRefspec = "refs/changes/98/99498/2"; FreeStyleBuild b = mock(FreeStyleBuild.class); GitClient git = mock(GitClient.class); when(git.revParse("FETCH_HEAD")).thenReturn(fetchHead); assertEquals(1, revs.iterator().next().getBranches().size()); assertEquals(singleBranch, revs.iterator().next().getBranches().iterator().next().getName()); assertEquals(fetchHead, revs.iterator().next().getBranches().iterator().next().getSHA1()); patchsetCreated.getPatchSet().setRevision(patchsetRevision.toString()); when(git.revParse(patchsetRefspec)).thenReturn(patchsetRevision); when(git.revParse(patchsetRevision.toString())).thenReturn(patchsetRevision); assertEquals(1, revs.iterator().next().getBranches().size()); assertEquals(patchsetRefspec, revs.iterator().next().getBranches().iterator().next().getName()); assertEquals(patchsetRevision, revs.iterator().next().getBranches().iterator().next().getSHA1());
protected void makeCombination(Map<IndexEntry, Revision> settings) { // Assume we are checked out String name = "combine-" + tid + "-" + (idx++); git.branch(name); git.checkout(name); String commit = "Hudson generated combination of:\n"; for (IndexEntry submodule : settings.keySet()) { Revision branch = settings.get(submodule); commit += " " + submodule.getFile() + " " + branch.toString() + "\n"; } listener.getLogger().print(commit); for (IndexEntry submodule : settings.keySet()) { Revision branch = settings.get(submodule); File subdir = new File(workspace, submodule.getFile()); IGitAPI subGit = new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment()); try { subGit.checkout(branch.sha1.name()); git.add(submodule.file); } finally { subGit.close(); } } git.commit(commit); }
protected void makeCombination(Map<IndexEntry, Revision> settings) throws InterruptedException { // Assume we are checked out String name = "combine-" + tid + "-" + (idx++); git.branch(name); git.checkout(name); StringBuilder commit = new StringBuilder( "Jenkins generated combination of:\n"); for (Entry<IndexEntry, Revision> setting : settings.entrySet()) { commit.append(' ').append(' '); commit.append(setting.getKey().getFile()); commit.append(' '); commit.append(setting.getValue()); commit.append('\n'); } listener.getLogger().print(commit); for (Entry<IndexEntry, Revision> setting : settings.entrySet()) { IndexEntry submodule = setting.getKey(); Revision branch = setting.getValue(); GitClient subGit = git.subGit(submodule.getFile()); subGit.checkout(branch.getSha1().name()); git.add(submodule.getFile()); } git.commit(commit.toString()); }
for (Iterator<Branch> j = r.getBranches().iterator(); j.hasNext();) { Branch b = j.next(); boolean keep = false; for (BranchSpec bspec : gitSCM.getBranches()) { if (bspec.matches(b.getName())) { keep = true; break; if (r.getBranches().size() == 0) { verbose(listener, "Ignoring {0} because we don't care about any of the branches that point to it", r); i.remove(); verbose(listener, "Removing what's already been built: {0}", data.getBuildsByBranchName()); for (Iterator<Revision> i = revs.iterator(); i.hasNext();) { Revision r = i.next(); if (data.hasBeenBuilt(r.getSha1())) { i.remove(); if (!isPollCall && revs.isEmpty() && data.getLastBuiltRevision() != null) { if (!utils.git.isCommitInRepo(data.getLastBuiltRevision().getSha1String())) { return revs;
public void createSubmoduleCombinations() throws GitException, IOException, InterruptedException { Map<IndexEntry, Collection<Revision>> moduleBranches = new HashMap<>(); for (IndexEntry submodule : git.getSubmodules("HEAD")) { GitClient subGit = git.subGit(submodule.getFile()); "Submodule " + submodule.getKey().getFile() + " branches"); for (Revision br : submodule.getValue()) listener.getLogger().print(" " + br.toString()); listener.getLogger().print('\n'); for (ObjectId sha1 : git.revListAll()) { List<IndexEntry> entries = git.getSubmodules(sha1.name()); entriesMap.put(sha1, entries); git.checkout(sha1.name()); makeCombination(combination);
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); BuildData parentBuildData = parentBuild.getAction(BuildData.class); if (parentBuildData != null) { tempParentLastBuiltRev = parentBuildData.getLastBuiltRevision(); environment.put(GIT_COMMIT, revToBuild.getSha1String()); if (mergeOptions.doMerge() && !revToBuild.containsBranchName(mergeOptions.getRemoteBranchName())) { buildConfig = getMergedBuildConfig(listener, workingDirectory, buildNumber, gitExe, buildData, environment, paramLocalBranch, revToBuild, internalTagName, internalTagComment);
@Override public Build prevBuildForChangelog(String singleBranch, BuildData data, GitClient git, BuildChooserContext context) throws InterruptedException, IOException { if (data != null) { ObjectId sha1 = git.revParse("FETCH_HEAD"); // Now we cheat and add the parent as the last build on the branch, so we can // get the changelog working properly-ish. ObjectId parentSha1 = getFirstParent(sha1, git); Revision parentRev = new Revision(parentSha1); parentRev.getBranches().add(new Branch(singleBranch, parentSha1)); int prevBuildNum = 0; Result r = null; Build lastBuild = data.getLastBuildOfBranch(singleBranch); if (lastBuild != null) { prevBuildNum = lastBuild.getBuildNumber(); r = lastBuild.getBuildResult(); } return new Build(parentRev, prevBuildNum, r); } else { //Hmm no sure what to do here, but the git plugin can handle us returning null here return null; } }
listener.getLogger().println("Using checkout strategy: " + getBuildChooser().getDisplayName()); BuildData previousBuildData = getBuildData(build.getPreviousBuild()); // read only BuildData buildData = copyBuildData(build.getPreviousBuild()); List<BuildData> actions = build.getActions(BuildData.class); for (BuildData d: actions) { if (d.similarTo(buildData)) { buildDataAlreadyPresent = true; break; buildData.setIndex(actions.size()+1); environment.put(GIT_COMMIT, revToBuild.revision.getSha1String()); Branch localBranch = Iterables.getFirst(revToBuild.revision.getBranches(),null); 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); CheckoutCommand checkoutCommand = git.checkout().branch(localBranchName).ref(revToBuild.revision.getSha1String()).deleteBranchIfExist(true); for (GitSCMExtension ext : this.getExtensions()) { ext.decorateCheckoutCommand(this, build, git, listener, checkoutCommand); } catch (GitLockFailedException e) { throw new IOException("Could not checkout " + revToBuild.revision.getSha1String(), e);
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); String sha1 = fixEmpty(rev.getSha1String()); if (sha1 != null && !sha1.isEmpty()) { env.put(GIT_COMMIT, sha1);
listener.getLogger().println("Using strategy: " + buildChooser.getDisplayName()); final FilePath workingDirectory = workingDirectory(workspace); final String gitExe = getGitExe(build.getBuiltOn(), listener); final String internalTagName = new StringBuilder() .append(GitConstants.INTERNAL_TAG_NAME_PREFIX) final BuildData buildData = getBuildData(build.getPreviousBuild(), true); BuildData parentBuildData = parentBuild.getAction(BuildData.class); if (parentBuildData != null) { tempParentLastBuiltRev = parentBuildData.getLastBuiltRevision(); environment.put(GIT_COMMIT, revToBuild.getSha1String()); if (mergeOptions.doMerge() && !revToBuild.containsBranchName(mergeOptions.getRemoteBranchName())) { buildConfig = getMergedBuildConfig(listener, workingDirectory, buildNumber, gitExe, buildData, environment, paramLocalBranch, revToBuild, internalTagName, internalTagComment);