Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger;
  
  
  import hudson.Extension;
 import  org.eclipse.jgit.lib.ObjectId;
 
 
Used by the git plugin to determine the revision to build.

Author(s):
Andrew Bayer
 
 public class GerritTriggerBuildChooser extends BuildChooser {
     private final String separator = "#";

    
Default constructor.
 
     public GerritTriggerBuildChooser() {
     }
 
     //CS IGNORE RedundantThrows FOR NEXT 30 LINES. REASON: Informative, and could happen.
     
Determines which Revisions to build. Doesn't care about branches.

Parameters:
isPollCall whether this is being called from Git polling
singleBranch The branch
git The GitAPI object
listener TaskListener for logging, etc
data the historical BuildData object
Returns:
A Collection containing the new revision.
Throws:
GitException in case of error
IOException In case of error
 
     @Override
     public Collection<RevisiongetCandidateRevisions(boolean isPollCallString singleBranch,
                                                       IGitAPI gitTaskListener listenerBuildData data)
         throws GitExceptionIOException {
 
         try {
             ObjectId sha1 = git.revParse("FETCH_HEAD");
 
             Revision revision = new Revision(sha1);
             revision.getBranches().add(new Branch(singleBranchsha1));
 
             return Collections.singletonList(revision);
         } catch (GitException e) {
             // branch does not exist, there is nothing to build
             return Collections.<Revision>emptyList();
         }
     }
 
     @Override
     public Build prevBuildForChangelog(String singleBranchBuildData dataIGitAPI git) {
         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(ObjectId.toString(sha1), git);
         Revision parentRev = new Revision(parentSha1);
         parentRev.getBranches().add(new Branch(singleBranchparentSha1));
 
         int prevBuildNum = 0;
         Result r = null;
 
         Build lastBuild = data.getLastBuildOfBranch(singleBranch);
         if (lastBuild != null) {
             prevBuildNum = lastBuild.getBuildNumber();
             r = lastBuild.getBuildResult();
         }
 
         Build newLastBuild = new Build(parentRevprevBuildNumr);
 
         return newLastBuild;
     }
 
     //CS IGNORE RedundantThrows FOR NEXT 30 LINES. REASON: Informative, and could happen.
     
Gets the top parent of the given revision.

Parameters:
revName Revision
git GitAPI object
Returns:
object id of Revision's parent, or of Revision itself if there is no parent
Throws:
GitException In case of error in git call
    private ObjectId getFirstParent(String revNameIGitAPI gitthrows GitException {
        String result = ((GitAPI)git).launchCommand("log""-1""--pretty=format:%P"revName);
        // If this is the first commit in the git, there is no parent and the
        // git log command returns an empty string, which will cause NPE later.
        if (result.isEmpty()) {
            // Get the revision of this commit instead.  If git log still returns
            // an empty string, raise an exception.
            result = ((GitAPI)git).launchCommand("log""-1""--pretty=format:%H");
            if (result.isEmpty()) {
                throw new GitException("git log returned an empty string");
            }
        }
        String parents = firstLine(result).trim();
        String firstParent = parents.split(" ")[0];
        return ObjectId.fromString(firstParent);
    }

    
Gets the first line of the string.

Parameters:
result String to get first line of
Returns:
first line of string
    private String firstLine(String result) {
        BufferedReader reader = new BufferedReader(new StringReader(result));
        String line;
        try {
            line = reader.readLine();
            if (line == null) {
                return null;
            }
            if (reader.readLine() != null) {
                throw new GitException("Result has multiple lines");
            }
        } catch (IOException e) {
            throw new GitException("Error parsing result"e);
        }
        return line;
    }

    
Descriptor for GerritTriggerBuildChooser.
    @Extension
    public static final class DescriptorImpl extends BuildChooserDescriptor {
        @Override
        public String getDisplayName() {
            return Messages.DisplayName();
        }
        @Override
        public String getLegacyId() {
            return Messages.DisplayName();
        }
    }
New to GrepCode? Check out our FAQ X