public Revision toRevision(GitClient git) throws InterruptedException { if (revision != null) { return revision; } ObjectId sha1 = git.revParse(commit); Revision revision = new Revision(sha1); // Here we do not have any local branches, containing the commit. So... // we are to get all the remote branches, and show them to users, as // they are local final List<Branch> branches = normalizeBranches(git.getBranchesContaining( ObjectId.toString(sha1), true)); revision.getBranches().addAll(branches); return revision; }
/** {@inheritDoc} */ public <T> T withRepository(RepositoryCallback<T> callable) throws IOException, InterruptedException { return proxy.withRepository(callable); }
/** * {@inheritDoc} */ @Override public void beforeCheckout(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener) throws IOException, InterruptedException, GitException { listener.getLogger().println("Wiping out workspace first."); git.getWorkTree().deleteContents(); }
public void commitWorkingCopy(final String commitMessage) throws IOException, InterruptedException { GitClient client = getGitClient(null); debuggingLogger.fine("Adding all files in the current directory"); client.add("-u"); debuggingLogger.fine(String.format("Committing working copy with message '%s'", commitMessage)); client.commit(commitMessage); }
/** * {@inheritDoc} */ @Override public GitClient decorate(GitSCM scm, GitClient git) throws IOException, InterruptedException, GitException { GitSCM.DescriptorImpl d = scm.getDescriptor(); String n = d.getGlobalConfigName(); if (name!=null) n = name; String e = d.getGlobalConfigEmail(); if (email!=null) e = email; git.setAuthor(n,e); git.setCommitter(n,e); return git; }
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
listener.getLogger().println("Using checkout strategy: " + getBuildChooser().getDisplayName()); BuildData previousBuildData = getBuildData(build.getPreviousBuild()); // read only BuildData buildData = copyBuildData(build.getPreviousBuild()); 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 environment.put(GIT_BRANCH, remoteBranchName); LocalBranch lb = getExtensions().get(LocalBranch.class); if (lb != null) { String lbn = lb.getLocalBranch(); CheckoutCommand checkoutCommand = git.checkout().branch(localBranchName).ref(revToBuild.revision.getSha1String()).deleteBranchIfExist(true); for (GitSCMExtension ext : this.getExtensions()) { ext.decorateCheckoutCommand(this, build, git, listener, checkoutCommand); computeChangeLog(git, revToBuild.revision, listener, previousBuildData, new FilePath(changelogFile), new BuildChooserContextImpl(build.getParent(), build, environment));
@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;
directory.act(new GitPushDryRunCallable( credentials.getUsername(), credentials.getPassword().getPlainText(), releaseRepository.getTargetRepoPrivateUri(), client.getWorkTree().toURI())); log(buildListener,"Push dry-run completed successfully");
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()); }
@Override public Blames blame(final Report report) { try { report.logInfo("Invoking Git blamer to create author and commit information for all affected files"); report.logInfo("GIT_COMMIT env = '%s'", gitCommit); report.logInfo("Git working tree = '%s'", git.getWorkTree()); ObjectId headCommit = git.revParse(gitCommit); if (headCommit == null) { report.logError("Could not retrieve HEAD commit, aborting"); return new Blames(); } report.logInfo("Git commit ID = '%s'", headCommit.getName()); String workspacePath = getWorkspacePath(); report.logInfo("Job workspace = '%s'", workspacePath); return git.withRepository(new BlameCallback(report, headCommit, workspacePath)); } catch (IOException exception) { report.logException(exception, "Computing blame information failed with an exception:"); } catch (GitException exception) { report.logException(exception, "Can't determine head commit using 'git rev-parse'. Skipping blame."); } catch (InterruptedException e) { // nothing to do, already logged } return new Blames(); }
GitUtils utils = new GitUtils(listener, git); List<Revision> branchRevs = new ArrayList<>(utils.getAllBranchRevisions()); List<BranchSpec> specifiedBranches = gitSCM.getBranches(); for (Iterator<Branch> j = revision.getBranches().iterator(); j.hasNext(); ) { Branch branch = j.next(); if (revision.getBranches().isEmpty()) { i.remove(); if (buildData.hasBeenBuilt(r.getSha1())) { i.remove(); 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));
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); ObjectId headSha1 = git.revParse("HEAD"); git.checkout(sha1.name()); makeCombination(combination);
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);
@Override public SCMRevision run(GitClient client, String remoteName) throws IOException, InterruptedException { String hash; try { hash = client.revParse(revision).name(); } catch (GitException x) { // Try prepending remote name in case it was a branch. try { hash = client.revParse(context.remoteName() + "/" + revision).name(); } catch (GitException x2) { listener.getLogger().println(x.getMessage()); listener.getLogger().println(x2.getMessage()); return null; } } return new SCMRevisionImpl(new SCMHead(revision), hash); } },
@Override public SCMRevision run(GitClient client, String remoteName) throws IOException, InterruptedException { if (head instanceof GitTagSCMHead) { try { ObjectId objectId = client.revParse(Constants.R_TAGS + head.getName()); return new GitTagSCMRevision((GitTagSCMHead) head, objectId.name()); } catch (GitException e) { // tag does not exist return null; } } else { for (Branch b : client.getRemoteBranches()) { String branchName = StringUtils.removeStart(b.getName(), remoteName + "/"); if (branchName.equals(head.getName())) { return new SCMRevisionImpl(head, b.getSHA1String()); } } } return null; } },
ObjectId sha1 = git.revParse(rev); Revision revision = new Revision(sha1); revision.getBranches().add(new Branch(refspec, sha1));
ObjectId sha1 = git.revParse(branchSpec); Revision revision = new Revision(sha1); revision.getBranches().add(new Branch("detached", sha1)); verbose(listener,"Will build the detached SHA1 {0}",sha1); return Collections.singletonList(revision); for (RemoteConfig config : gitSCM.getRepositories()) { String repository = config.getName(); String fqbn = repository + "/" + branchSpec; for (RemoteConfig config : gitSCM.getRepositories()) { String repository = config.getName(); String fqbn;
/** * 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 void checkout(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, Revision rev) throws InterruptedException, IOException, GitException { CheckoutCommand checkoutCommand = git.checkout().ref(rev.getSha1String()); for (GitSCMExtension ext : scm.getExtensions()) { ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand); } checkoutCommand.execute(); } }