SubversionSCM.java 18.6 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
package hudson.scm;

import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.Util;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Project;
import hudson.model.TaskListener;
import hudson.util.ArgumentListBuilder;
import hudson.util.FormFieldValidator;
import org.apache.commons.digester.Digester;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.xml.sax.SAXException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Subversion.
 *
 * Check http://svn.collab.net/repos/svn/trunk/subversion/svn/schema/ for
 * various output formats.
 *
 * @author Kohsuke Kawaguchi
 */
public class SubversionSCM extends AbstractCVSFamilySCM {
    private final String modules;
    private boolean useUpdate;
    private String username;
    private String otherOptions;

    SubversionSCM( String modules, boolean useUpdate, String username, String otherOptions ) {
        StringBuilder normalizedModules = new StringBuilder();
        StringTokenizer tokens = new StringTokenizer(modules);
        while(tokens.hasMoreTokens()) {
            if(normalizedModules.length()>0)    normalizedModules.append(' ');
            String m = tokens.nextToken();
            if(m.endsWith("/"))
                // the normalized name is always without the trailing '/'
                m = m.substring(0,m.length()-1);
            normalizedModules.append(m);
       }

        this.modules = normalizedModules.toString();
        this.useUpdate = useUpdate;
        this.username = nullify(username);
        this.otherOptions = nullify(otherOptions);
    }

K
kohsuke 已提交
78 79 80 81
    /**
     * Whitespace-separated list of SVN URLs that represent
     * modules to be checked out.
     */
K
kohsuke 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    public String getModules() {
        return modules;
    }

    public boolean isUseUpdate() {
        return useUpdate;
    }

    public String getUsername() {
        return username;
    }

    public String getOtherOptions() {
        return otherOptions;
    }

    private Collection<String> getModuleDirNames() {
        List<String> dirs = new ArrayList<String>();
        StringTokenizer tokens = new StringTokenizer(modules);
        while(tokens.hasMoreTokens()) {
            dirs.add(getLastPathComponent(tokens.nextToken()));
        }
        return dirs;
    }

    private boolean calcChangeLog(Build build, File changelogFile, Launcher launcher, BuildListener listener) throws IOException {
        if(build.getPreviousBuild()==null) {
            // nothing to compare against
            return createEmptyChangeLog(changelogFile, listener, "log");
        }

        PrintStream logger = listener.getLogger();

        Map<String,Integer> previousRevisions = parseRevisionFile(build.getPreviousBuild());
        Map<String,Integer> thisRevisions     = parseRevisionFile(build);

        Map env = createEnvVarMap(true);

        for( String module : getModuleDirNames() ) {
            Integer prevRev = previousRevisions.get(module);
            if(prevRev==null) {
                logger.println("no revision recorded for "+module+" in the previous build");
                continue;
            }
            Integer thisRev = thisRevisions.get(module);
            if(thisRev!=null && thisRev.equals(prevRev)) {
                logger.println("no change for "+module+" since the previous build");
                continue;
            }

            String cmd = DESCRIPTOR.getSvnExe()+" log -v --xml --non-interactive -r "+(prevRev+1)+":BASE "+module;
            OutputStream os = new BufferedOutputStream(new FileOutputStream(changelogFile));
            try {
                int r = launcher.launch(cmd,env,os,build.getProject().getWorkspace()).join();
                if(r!=0) {
                    listener.fatalError("revision check failed");
                    // report the output
                    FileInputStream log = new FileInputStream(changelogFile);
                    try {
                        Util.copyStream(log,listener.getLogger());
                    } finally {
                        log.close();
                    }
                    return false;
                }
            } finally {
                os.close();
            }
        }

        return true;
    }

    /*package*/ static Map<String,Integer> parseRevisionFile(Build build) throws IOException {
        Map<String,Integer> revisions = new HashMap<String,Integer>(); // module -> revision
        {// read the revision file of the last build
            File file = getRevisionFile(build);
            if(!file.exists())
                // nothing to compare against
                return revisions;

            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while((line=br.readLine())!=null) {
                int index = line.indexOf('/');
                if(index<0) {
                    continue;   // invalid line?
                }
                try {
                    revisions.put(line.substring(0,index), Integer.parseInt(line.substring(index+1)));
                } catch (NumberFormatException e) {
                    // perhaps a corrupted line. ignore
                }
            }
        }

        return revisions;
    }

    public boolean checkout(Build build, Launcher launcher, FilePath workspace, BuildListener listener, File changelogFile) throws IOException {
        boolean result;

        if(useUpdate && isUpdatable(workspace,listener)) {
            result = update(launcher,workspace,listener);
            if(!result)
                return false;
        } else {
            workspace.deleteContents();
            StringTokenizer tokens = new StringTokenizer(modules);
            while(tokens.hasMoreTokens()) {
                ArgumentListBuilder cmd = new ArgumentListBuilder();
                cmd.add(DESCRIPTOR.getSvnExe(),"co","-q","--non-interactive");
                if(username!=null)
                    cmd.add("--username",username);
                if(otherOptions!=null)
                    cmd.add(Util.tokenize(otherOptions));
                cmd.add(tokens.nextToken());

                result = run(launcher,cmd,listener,workspace);
                if(!result)
                    return false;
            }
        }

        // write out the revision file
        PrintWriter w = new PrintWriter(new FileOutputStream(getRevisionFile(build)));
        try {
            Map<String,SvnInfo> revMap = buildRevisionMap(workspace,listener);
            for (Entry<String,SvnInfo> e : revMap.entrySet()) {
                w.println( e.getKey() +'/'+ e.getValue().revision );
            }
        } finally {
            w.close();
        }

        return calcChangeLog(build, changelogFile, launcher, listener);
    }

    /**
     * Output from "svn info" command.
     */
    public static class SvnInfo {
        /** The remote URL of this directory */
        String url;
        /** Current workspace revision. */
        int revision = -1;

        private SvnInfo() {}

        /**
         * Returns true if this object is fully populated.
         */
        public boolean isComplete() {
            return url!=null && revision!=-1;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public void setRevision(int revision) {
            this.revision = revision;
        }

        /**
         * Executes "svn info" command and returns the parsed output
         *
         * @param subject
         *      The target to run "svn info". Either local path or remote URL.
         */
        public static SvnInfo parse(String subject, Map env, FilePath workspace, TaskListener listener) throws IOException {
            String cmd = DESCRIPTOR.getSvnExe()+" info --xml "+subject;
            listener.getLogger().println("$ "+cmd);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int r = new Proc(cmd,env,baos,workspace.getLocal()).join();
            if(r!=0) {
                // failed. to allow user to diagnose the problem, send output to log
                listener.getLogger().write(baos.toByteArray());
                throw new IOException("svn info failed");
            }

            SvnInfo info = new SvnInfo();

            Digester digester = new Digester();
            digester.push(info);

            digester.addBeanPropertySetter("info/entry/url");
            digester.addSetProperties("info/entry/commit","revision","revision");  // set attributes. in particular @revision

            try {
                digester.parse(new ByteArrayInputStream(baos.toByteArray()));
            } catch (SAXException e) {
                // failed. to allow user to diagnose the problem, send output to log
                listener.getLogger().write(baos.toByteArray());
                e.printStackTrace(listener.fatalError("Failed to parse Subversion output"));
                throw new IOException("Unabled to parse svn info output");
            }

            if(!info.isComplete())
                throw new IOException("No revision in the svn info output");

            return info;
        }

    }

    /**
     * Checks .svn files in the workspace and finds out revisions of the modules
     * that the workspace has.
     *
     * @return
     *      null if the parsing somehow fails. Otherwise a map from module names to revisions.
     */
    private Map<String,SvnInfo> buildRevisionMap(FilePath workspace, TaskListener listener) throws IOException {
        PrintStream logger = listener.getLogger();

        Map<String/*module name*/,SvnInfo> revisions = new HashMap<String,SvnInfo>();

        Map env = createEnvVarMap(false);

        // invoke the "svn info"
        for( String module : getModuleDirNames() ) {
            // parse the output
            SvnInfo info = SvnInfo.parse(module,env,workspace,listener);
            revisions.put(module,info);
            logger.println("Revision:"+info.revision);
        }

        return revisions;
    }

    /**
     * Gets the file that stores the revision.
     */
    private static File getRevisionFile(Build build) {
        return new File(build.getRootDir(),"revision.txt");
    }

    public boolean update(Launcher launcher, FilePath remoteDir, BuildListener listener) throws IOException {
        ArgumentListBuilder cmd = new ArgumentListBuilder();
        cmd.add(DESCRIPTOR.getSvnExe(), "update", "-q", "--non-interactive");

        if(username!=null)
            cmd.add(" --username ",username);
        if(otherOptions!=null)
            cmd.add(Util.tokenize(otherOptions));

        StringTokenizer tokens = new StringTokenizer(modules);
        while(tokens.hasMoreTokens()) {
            if(!run(launcher,cmd,listener,new FilePath(remoteDir,getLastPathComponent(tokens.nextToken()))))
                return false;
        }
        return true;
    }

    /**
     * Returns true if we can use "svn update" instead of "svn checkout"
     */
    private boolean isUpdatable(FilePath workspace,BuildListener listener) {
        StringTokenizer tokens = new StringTokenizer(modules);
        while(tokens.hasMoreTokens()) {
            String url = tokens.nextToken();
            String moduleName = getLastPathComponent(url);
            File module = workspace.child(moduleName).getLocal();

            try {
                SvnInfo svnInfo = SvnInfo.parse(moduleName, createEnvVarMap(false), workspace, listener);
                if(!svnInfo.url.equals(url)) {
                    listener.getLogger().println("Checking out a fresh workspace because the workspace is not "+url);
                    return false;
                }
            } catch (IOException e) {
                listener.getLogger().println("Checking out a fresh workspace because Hudson failed to detect the current workspace "+module);
                e.printStackTrace(listener.error(e.getMessage()));
                return false;
            }
        }
        return true;
    }

    public boolean pollChanges(Project project, Launcher launcher, FilePath workspace, TaskListener listener) throws IOException {
        // current workspace revision
        Map<String,SvnInfo> wsRev = buildRevisionMap(workspace,listener);

        Map env = createEnvVarMap(false);

        // check the corresponding remote revision
        for (SvnInfo localInfo : wsRev.values()) {
            SvnInfo remoteInfo = SvnInfo.parse(localInfo.url,env,workspace,listener);
K
kohsuke 已提交
373 374
            listener.getLogger().println("Revision:"+remoteInfo.revision);
            if(remoteInfo.revision > localInfo.revision)
K
kohsuke 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
                return true;    // change found
        }

        return false; // no change
    }

    public ChangeLogParser createChangeLogParser() {
        return new SubversionChangeLogParser();
    }


    public DescriptorImpl getDescriptor() {
        return DESCRIPTOR;
    }

    public void buildEnvVars(Map env) {
        // no environment variable
    }

    public FilePath getModuleRoot(FilePath workspace) {
        String s;

        // if multiple URLs are specified, pick the first one
        int idx = modules.indexOf(' ');
        if(idx>=0)  s = modules.substring(0,idx);
        else        s = modules;

        return workspace.child(getLastPathComponent(s));
    }

    private String getLastPathComponent(String s) {
        String[] tokens = s.split("/");
        return tokens[tokens.length-1]; // return the last token
    }

    static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();

    public static final class DescriptorImpl extends Descriptor<SCM> {
413 414 415 416 417
        /**
         * Path to <tt>svn.exe</tt>. Null to default.
         */
        private String svnExe;

K
kohsuke 已提交
418 419 420 421
        DescriptorImpl() {
            super(SubversionSCM.class);
        }

422 423 424 425
        protected void convert(Map<String, Object> oldPropertyBag) {
            svnExe = (String)oldPropertyBag.get("svn_exe");
        }

K
kohsuke 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439
        public String getDisplayName() {
            return "Subversion";
        }

        public SCM newInstance(StaplerRequest req) {
            return new SubversionSCM(
                req.getParameter("svn_modules"),
                req.getParameter("svn_use_update")!=null,
                req.getParameter("svn_username"),
                req.getParameter("svn_other_options")
            );
        }

        public String getSvnExe() {
440
            String value = svnExe;
K
kohsuke 已提交
441 442 443 444 445 446
            if(value==null)
                value = "svn";
            return value;
        }

        public void setSvnExe(String value) {
447
            svnExe = value;
K
kohsuke 已提交
448 449 450 451
            save();
        }

        public boolean configure( HttpServletRequest req ) {
452
            svnExe = req.getParameter("svn_exe");
K
kohsuke 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
            return true;
        }

        /**
         * Returns the Subversion version information.
         *
         * @return
         *      null if failed to obtain.
         */
        public Version version(Launcher l, String svnExe) {
            try {
                if(svnExe==null || svnExe.equals(""))    svnExe="svn";

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                l.launch(new String[]{svnExe,"--version"},new String[0],out,FilePath.RANDOM).join();

                // parse the first line for version
                BufferedReader r = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray())));
                String line;
                while((line = r.readLine())!=null) {
                    Matcher m = SVN_VERSION.matcher(line);
                    if(m.matches())
                        return new Version(Integer.parseInt(m.group(2)), m.group(1));
                }

                // ancient version of subversions didn't have the fixed version number line.
                // or maybe something else is going wrong.
                LOGGER.log(Level.WARNING, "Failed to parse the first line from svn output: "+line);
                return new Version(0,"(unknown)");
            } catch (IOException e) {
                // Stack trace likely to be overkill for a problem that isn't necessarily a problem at all:
                LOGGER.log(Level.WARNING, "Failed to check svn version: {0}", e.toString());
                return null; // failed to obtain
            }
        }

        // web methods

        public void doVersionCheck(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
            // this method runs a new process, so it needs to be protected
            new FormFieldValidator(req,rsp,true) {
                protected void check() throws IOException, ServletException {
                    String svnExe = request.getParameter("exe");

                    Version v = version(new Launcher(TaskListener.NULL),svnExe);
                    if(v==null) {
                        error("Failed to check subversion version info. Is this a valid path?");
                        return;
                    }
                    if(v.isOK()) {
                        ok();
                    } else {
                        error("Version "+v.versionId+" found, but 1.3.0 is required");
                    }
                }
            }.process();
        }
    }

    public static final class Version {
        private final int revision;
        private String versionId;

        public Version(int revision, String versionId) {
            this.revision = revision;
            this.versionId = versionId;
        }

        /**
         * Repository revision ID of this build.
         */
        public int getRevision() {
            return revision;
        }

        /**
         * Human-readable version string.
         */
        public String getVersionId() {
            return versionId;
        }

        /**
         * We use "svn info --xml", which is new in 1.3.0
         */
        public boolean isOK() {
            return revision>=17949;
        }
    }

    private static final Pattern SVN_VERSION = Pattern.compile("svn, .+ ([0-9.]+) \\(r([0-9]+)\\)");

    private static final Logger LOGGER = Logger.getLogger(SubversionSCM.class.getName());
}