function Branch() { var _id = Math.round(Math.random() * 10); this.getId = function () { return _id; }; Branch.branches.push(this); } Branch.branches = []; Branch.getIds = function () { var ids = []; for (var i in Branch.branches) ids.push(Branch.branches[i].getId()); return ids; }; // test code for (var i = 0; i < 7; i++) { new Branch(); } console.log("Branch IDs:\n" + Branch.getIds());
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; }
/** * Returns branch name and SHA1 hash. * @return branch name and SHA1 hash */ @Override public String toString() { return "Branch " + name + "(" + getSHA1String() + ")"; }
Branch branch = new Branch(...); branch.setCompany(company); // mandatory company.getBranches().add(branch); // not mandatory, but advised to have a consistent graph em.persist(branch);
for (Iterator<Branch> j = revision.getBranches().iterator(); j.hasNext(); ) { Branch branch = j.next(); if (spec.matches(branch.getName(), env) || HEAD.matches(branch.getName(), env)) { j.remove(); break; if (revision.getBranches().isEmpty()) { i.remove(); if (buildData.hasBeenBuilt(r.getSha1())) { i.remove(); if (!isPollCall && branchRevs.isEmpty() && buildData.getLastBuiltRevision() != null) { listener.getLogger().println(Messages.BuildChooser_BuildingLastRevision()); return Collections.singletonList(buildData.getLastBuiltRevision()); return utils.git.withRepository(new RepositoryCallback<List<Revision>>() { public List<Revision> invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException { Collections.sort(in,new CommitTimeComparator(repo));
@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; } }
if (rev.containsBranchName(remoteBranchRef)) return rev; ObjectId target = git.revParse(remoteBranchRef); CheckoutCommand checkoutCommand = git.checkout().branch(paramLocalBranch).ref(remoteBranchRef).deleteBranchIfExist(true); for (GitSCMExtension ext : scm.getExtensions()) ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand); 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); bd.saveBuild(new Build(marked,rev, build.getNumber(), FAILURE)); } else { listener.getLogger().println("Was not possible to get build data"); Revision mergeRevision = new GitUtils(listener,git).getRevisionForSHA1(git.revParse(HEAD)); mergeRevision.getBranches().add(new Branch(remoteBranchRef, target)); return mergeRevision;
/** * Return a list of "Revisions" - where a revision knows about all the branch names that refer to * a SHA1. * @return list of revisions * @throws IOException on input or output error * @throws GitException on git error * @throws InterruptedException when interrupted */ public Collection<Revision> getAllBranchRevisions() throws GitException, IOException, InterruptedException { Map<ObjectId, Revision> revisions = new HashMap<>(); for (Branch b : git.getRemoteBranches()) { Revision r = revisions.get(b.getSHA1()); if (r == null) { r = new Revision(b.getSHA1()); revisions.put(b.getSHA1(), r); } r.getBranches().add(b); } for (String tag : git.getTagNames(null)) { String tagRef = Constants.R_TAGS + tag; ObjectId objectId = git.revParse(tagRef); Revision r = revisions.get(objectId); if (r == null) { r = new Revision(objectId); revisions.put(objectId, r); } r.getBranches().add(new Branch(tagRef, objectId)); } return revisions.values(); }
private Revision objectId2Revision(String singleBranch, ObjectId sha1) { Revision revision = new Revision(sha1); revision.getBranches().add(new Branch(singleBranch, sha1)); return revision; }
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);
public Revision sortBranchesForRevision(Revision revision, List<BranchSpec> branchOrder, EnvVars env) { ArrayList<Branch> orderedBranches = new ArrayList<>(revision.getBranches().size()); ArrayList<Branch> revisionBranches = new ArrayList<>(revision.getBranches()); for(BranchSpec branchSpec : branchOrder) { for (Iterator<Branch> i = revisionBranches.iterator(); i.hasNext();) { Branch b = i.next(); if (branchSpec.matches(b.getName(), env)) { i.remove(); orderedBranches.add(b); } } } orderedBranches.addAll(revisionBranches); return new Revision(revision.getSha1(), orderedBranches); }
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);
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());
private String getLastSuccessfulBuiltCommitOfBranch(Run<?, ?> build, Branch branch) { String prevCommit = null; if (build.getPreviousSuccessfulBuild() != null) { final Build lastSuccessfulBuildOfBranch = fixNull(getBuildData(build.getPreviousSuccessfulBuild())).getLastBuildOfBranch(branch.getName()); if (lastSuccessfulBuildOfBranch != null) { Revision previousRev = lastSuccessfulBuildOfBranch.getRevision(); if (previousRev != null) { prevCommit = previousRev.getSha1String(); } } } return prevCommit; }
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()); gerritCause.setEvent(patchsetCreated); when(b.getCause(GerritCause.class)).thenReturn(gerritCause); 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());
ChangelogCommand changelog = git.changelog(); changelog.includes(revToBuild.getSha1()); try (Writer out = new OutputStreamWriter(changelogFile.write(),"UTF-8")) { boolean exclusion = false; exclusion = true; } 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;
/** * Return a list of "Revisions" - where a revision knows about all the branch names that refer to * a SHA1. * * @return * @throws IOException * @throws GitException */ public Collection<Revision> getAllBranchRevisions() throws GitException, IOException { Map<ObjectId, Revision> revisions = new HashMap<ObjectId, Revision>(); List<Branch> branches = git.getRemoteBranches(); for (Branch b : branches) { Revision r = revisions.get(b.getSHA1()); if (r == null) { r = new Revision(b.getSHA1()); revisions.put(b.getSHA1(), r); } r.getBranches().add(b); } return revisions.values(); }
public boolean branchMatchesInterest(Branch br) { for (String regex : branches) { if (!Pattern.matches(regex, br.getName())) { return false; } } return true; }
protected List<Branch> parseRefList(List<Ref> refList) { List<Branch> result = new ArrayList<Branch>(); if (CollectionUtils.isNotEmpty(refList)) { for (Ref ref : refList) { Branch buildBranch = new Branch(ref); result.add(buildBranch); listener.getLogger().println(Messages.GitAPI_Branch_BranchInRepoMsg(buildBranch.getName())); } } return result; }
/** * This method is aimed to normalize all the branches to the same naming * convention, as {@link GitClient#getBranchesContaining(String, boolean)} * returns branches with "remotes/" prefix. * @param branches branches, retrieved from git client * @return list of branches without the "remote/" prefix. */ private List<Branch> normalizeBranches(List<Branch> branches) { final List<Branch> normalBranches = new ArrayList<>(branches.size()); final String remotesPrefix = "remotes/"; for (Branch initialBranch : branches) { final String initialBranchName = initialBranch.getName(); final Branch normalBranch; if (initialBranchName.startsWith(remotesPrefix)) { final String normalName = initialBranchName.substring(remotesPrefix.length()); normalBranch = new Branch(normalName, initialBranch.getSHA1()); } else { normalBranch = initialBranch; } normalBranches.add(normalBranch); } return normalBranches; }