ChangeLogSet.java 9.1 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
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
K
kohsuke 已提交
24 25
package hudson.scm;

26
import hudson.MarkupText;
27
import hudson.Util;
28
import hudson.model.AbstractBuild;
J
Jesse Glick 已提交
29
import hudson.model.Run;
30
import hudson.model.User;
K
kohsuke 已提交
31 32
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
K
kohsuke 已提交
33

K
kohsuke 已提交
34
import java.util.ArrayList;
35
import java.util.Collection;
36
import java.util.Date;
K
kohsuke 已提交
37
import java.util.Iterator;
K
kohsuke 已提交
38
import java.util.List;
39
import java.util.logging.Logger;
J
Jesse Glick 已提交
40
import org.jenkinsci.bytecode.AdaptField;
K
kohsuke 已提交
41 42 43 44

/**
 * Represents SCM change list.
 *
K
kohsuke 已提交
45
 * <p>
K
kohsuke 已提交
46 47
 * Use the "index" view of this object to render the changeset detail page,
 * and use the "digest" view of this object to render the summary page.
48
 * For the change list at project level, see {@link SCM}.
K
kohsuke 已提交
49
 *
K
kohsuke 已提交
50 51 52
 * <p>
 * {@link Iterator} is expected to return recent changes first.
 *
K
kohsuke 已提交
53 54
 * @author Kohsuke Kawaguchi
 */
K
kohsuke 已提交
55
@ExportedBean(defaultVisibility=999)
K
kohsuke 已提交
56
public abstract class ChangeLogSet<T extends ChangeLogSet.Entry> implements Iterable<T> {
57 58

    /**
J
Jesse Glick 已提交
59
     * Build whose change log this object represents.
60
     */
61 62 63
    private final Run<?,?> run;
    @Deprecated
    public final AbstractBuild<?,?> build;
64
    private final RepositoryBrowser</* ideally T */?> browser;
65

66 67 68
    protected ChangeLogSet(Run<?,?> run, RepositoryBrowser<?> browser) {
        this.run = run;
        build = run instanceof AbstractBuild ? (AbstractBuild) run : null;
69
        this.browser = browser;
70 71
    }

J
Jesse Glick 已提交
72 73
    @Deprecated
    protected ChangeLogSet(AbstractBuild<?, ?> build) {
J
Jesse Glick 已提交
74 75 76 77 78 79 80
        this((Run) build, browserFromBuild(build));
    }
    private static RepositoryBrowser<?> browserFromBuild(AbstractBuild<?,?> build) {
        if (build == null) { // not generally allowed, but sometimes done in unit tests
            return null;
        }
        return build.getParent().getScm().getEffectiveBrowser();
J
Jesse Glick 已提交
81 82
    }

83 84
    public Run<?,?> getRun() {
        return run;
J
Jesse Glick 已提交
85 86
    }

87 88 89 90
    public RepositoryBrowser<?> getBrowser() {
        return browser;
    }

K
kohsuke 已提交
91 92 93 94 95
    /**
     * Returns true if there's no change.
     */
    public abstract boolean isEmptySet();

K
kohsuke 已提交
96
    /**
97
     * All changes in this change set. 
K
kohsuke 已提交
98 99
     */
    // method for the remote API.
K
kohsuke 已提交
100
    @Exported
K
kohsuke 已提交
101 102 103 104 105
    public final Object[] getItems() {
        List<T> r = new ArrayList<T>();
        for (T t : this)
            r.add(t);
        return r.toArray();
106 107 108 109 110 111 112 113 114 115
    }

    /**
     * Optional identification of the kind of SCM being used.
     * @return a short token, such as the SCM's main CLI executable name
     * @since 1.284
     */
    @Exported
    public String getKind() {
        return null;
K
kohsuke 已提交
116 117
    }

K
kohsuke 已提交
118 119 120
    /**
     * Constant instance that represents no changes.
     */
J
Jesse Glick 已提交
121
    public static ChangeLogSet<? extends ChangeLogSet.Entry> createEmpty(Run build) {
122
        return new EmptyChangeLogSet(build);
123
    }
K
kohsuke 已提交
124

J
Jesse Glick 已提交
125 126 127 128 129
    @Deprecated
    public static ChangeLogSet<? extends ChangeLogSet.Entry> createEmpty(AbstractBuild build) {
        return createEmpty((Run) build);
    }

K
kohsuke 已提交
130
    @ExportedBean(defaultVisibility=999)
K
kohsuke 已提交
131
    public static abstract class Entry {
132 133 134 135 136 137 138 139 140 141 142 143 144
        private ChangeLogSet parent;

        public ChangeLogSet getParent() {
            return parent;
        }

        /**
         * Should be invoked before a {@link ChangeLogSet} is exposed to public.
         */
        protected void setParent(ChangeLogSet parent) {
            this.parent = parent;
        }

K
kohsuke 已提交
145
        /**
146 147 148 149 150 151 152 153 154 155 156
         * Returns a human readable display name of the commit number, revision number, and such thing
         * that identifies this entry.
         *
         * <p>
         * This method is primarily intended for visualization of the data.
         *
         * @return
         *      null if such a concept doesn't make sense for the implementation. For example,
         *      in CVS there's no single identifier for commits. Each file gets a different revision number.
         * @since 1.405
         */
157
        @Exported
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        public String getCommitId() {
            return null;
        }

        /**
         * Returns the timestamp of this commit in the {@link Date#getTime()} format.
         *
         * <p>
         * This method is primarily intended for visualization of the data.
         *
         * @return
         *      -1 if the implementation doesn't support it (for example, in CVS a commit
         *      spreads over time between multiple changes on multiple files, so there's no single timestamp.)
         * @since 1.405
         */
173
        @Exported
174 175 176 177 178
        public long getTimestamp() {
            return -1;
        }

        /**
K
kohsuke 已提交
179 180 181 182 183 184 185 186
         * Gets the "commit message".
         *
         * <p>
         * The exact definition depends on the individual SCM implementation.
         *
         * @return
         *      Can be empty but never null.
         */
187
        @Exported
K
kohsuke 已提交
188 189 190 191 192 193 194 195
        public abstract String getMsg();

        /**
         * The user who made this change.
         *
         * @return
         *      never null.
         */
196
        @Exported
K
kohsuke 已提交
197 198
        public abstract User getAuthor();

199 200 201 202 203 204 205 206 207 208
        /**
         * Returns a set of paths in the workspace that was
         * affected by this change.
         *
         * <p>
         * Contains string like 'foo/bar/zot'. No leading/trailing '/',
         * and separator must be normalized to '/'.
         *
         * @return never null.
         */
209
        @Exported
210
        public abstract Collection<String> getAffectedPaths();
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        
        /**
         * Returns a set of paths in the workspace that was
         * affected by this change.
         * <p>
         * Noted: since this is a new interface, some of the SCMs may not have 
         * implemented this interface. The default implementation for this 
         * interface is throw UnsupportedOperationException
         * <p>
         * It doesn't throw NoSuchMethodException because I rather to throw a 
         * runtime exception
         *
         * @return AffectedFile never null.
         * @since 1.309
         */
226
        public Collection<? extends AffectedFile> getAffectedFiles() {
227 228 229 230 231 232 233 234
	        String scm = "this SCM";
	        ChangeLogSet parent = getParent();
	        if ( null != parent ) {
		        String kind = parent.getKind();
		        if ( null != kind && kind.trim().length() > 0 ) scm = kind;
	        }
	        throw new UnsupportedOperationException("getAffectedFiles() is not implemented by " + scm);
        }
235

236 237 238 239
        /**
         * Gets the text fully marked up by {@link ChangeLogAnnotator}.
         */
        public String getMsgAnnotated() {
240
            MarkupText markup = new MarkupText(getMsg());
241
            for (ChangeLogAnnotator a : ChangeLogAnnotator.all())
242
                try {
243
                    a.annotate(parent.run, this, markup);
244
                } catch(Exception e) {
245 246 247
                    LOGGER.info("ChangeLogAnnotator " + a.toString() + " failed to annotate message '" + getMsg() + "'; " + e.getMessage());
                } catch(Error e) {
                    LOGGER.severe("ChangeLogAnnotator " + a.toString() + " failed to annotate message '" + getMsg() + "'; " + e.getMessage());
248
                }
249

250
            return markup.toString(false);
251 252
        }

K
kohsuke 已提交
253 254 255 256
        /**
         * Message escaped for HTML
         */
        public String getMsgEscaped() {
257
            return Util.escape(getMsg());
K
kohsuke 已提交
258
        }
259 260
        
        static final Logger LOGGER = Logger.getLogger(ChangeLogSet.Entry.class.getName());
K
kohsuke 已提交
261
    }
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    
    /**
     * Represents a file change. Contains filename, edit type, etc.
     * 
     * I checked the API names against some some major SCMs and most SCMs
     * can adapt to this interface with very little changes
     *
     * @see ChangeLogSet.Entry#getAffectedFiles()
     */
    public interface AffectedFile {
        /**
         * The path in the workspace that was affected
         * <p>
         * Contains string like 'foo/bar/zot'. No leading/trailing '/',
         * and separator must be normalized to '/'.
         *
         * @return never null.
         */
K
kohsuke 已提交
280
        String getPath();
281 282
	    
	    
283 284 285
        /**
         * Return whether the file is new/modified/deleted
         */
K
kohsuke 已提交
286
        EditType getEditType();
287
    }
K
kohsuke 已提交
288
}