PageRenderTime 43ms CodeModel.GetById 13ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/scm/ChangeLogSet.java

http://github.com/hudson/hudson
Java | 225 lines | 86 code | 22 blank | 117 comment | 7 complexity | 6fead1c383c1d31679fc260fd1bcb062 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2011, Oracle Corporation, Kohsuke Kawaguchi, Nikita Levyankov
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.scm;
 25
 26import com.google.common.collect.Iterators;
 27import hudson.MarkupText;
 28import hudson.Util;
 29import hudson.model.AbstractBuild;
 30import java.util.ArrayList;
 31import java.util.Collection;
 32import java.util.Iterator;
 33import java.util.List;
 34import org.apache.commons.collections.CollectionUtils;
 35import org.kohsuke.stapler.export.Exported;
 36import org.kohsuke.stapler.export.ExportedBean;
 37
 38/**
 39 * Represents SCM change list.
 40 * <p/>
 41 * <p/>
 42 * Use the "index" view of this object to render the changeset detail page,
 43 * and use the "digest" view of this object to render the summary page.
 44 * For the change list at project level, see {@link SCM}.
 45 * <p/>
 46 * <p/>
 47 * {@link Iterator} is expected to return recent changes first.
 48 *
 49 * @author Kohsuke Kawaguchi
 50 * @author Nikia Levyankov
 51 */
 52@ExportedBean(defaultVisibility = 999)
 53public abstract class ChangeLogSet<T extends ChangeLogSet.Entry> implements Iterable<T> {
 54
 55    /**
 56     * {@link AbstractBuild} whose change log this object represents.
 57     */
 58    //TODO: review and check whether we can do it private
 59    public final AbstractBuild<?, ?> build;
 60
 61    protected ChangeLogSet(AbstractBuild<?, ?> build) {
 62        this.build = build;
 63    }
 64
 65    public AbstractBuild<?, ?> getBuild() {
 66        return build;
 67    }
 68
 69    /**
 70     * Returns true if there's no change.
 71     *
 72     * @return if {@link #getLogs()}  returns empty or null list
 73     */
 74    public boolean isEmptySet() {
 75        return CollectionUtils.isEmpty(getLogs());
 76    }
 77
 78    /**
 79     * {@inheritDoc}
 80     */
 81    @SuppressWarnings("unchecked")
 82    public Iterator<T> iterator() {
 83        return isEmptySet()? Iterators.<T>emptyIterator(): getLogs().iterator();
 84    }
 85
 86    /**
 87     * All changes in this change set.
 88     */
 89    // method for the remote API.
 90    @Exported
 91    public final Object[] getItems() {
 92        List<T> r = new ArrayList<T>();
 93        for (T t : this)
 94            r.add(t);
 95        return r.toArray();
 96    }
 97
 98    /**
 99     * Optional identification of the kind of SCM being used.
100     * @return a short token, such as the SCM's main CLI executable name
101     * @since 1.284
102     */
103    @Exported
104    public String getKind() {
105        return null;
106    }
107
108    /**
109     * Returns change log entries.
110     *
111     * @return logs.
112     */
113    public abstract Collection<T> getLogs();
114
115    /**
116     * Constant instance that represents no changes.
117     */
118    public static ChangeLogSet<? extends ChangeLogSet.Entry> createEmpty(AbstractBuild build) {
119        return new EmptyChangeLogSet(build);
120    }
121
122    @ExportedBean(defaultVisibility = 999)
123    public static abstract class Entry implements ChangeLogEntry {
124        private ChangeLogSet parent;
125
126        public ChangeLogSet getParent() {
127            return parent;
128        }
129
130        /**
131         * Should be invoked before a {@link ChangeLogSet} is exposed to public.
132         */
133        protected void setParent(ChangeLogSet parent) {
134            this.parent = parent;
135        }
136
137        /**
138         * Returns a set of paths in the workspace that was
139         * affected by this change.
140         * <p>
141         * Noted: since this is a new interface, some of the SCMs may not have
142         * implemented this interface. The default implementation for this
143         * interface is throw UnsupportedOperationException
144         * <p>
145         * It doesn't throw NoSuchMethodException because I rather to throw a
146         * runtime exception
147         *
148         * @return AffectedFile never null.
149         * @since 1.309
150         */
151        public Collection<? extends AffectedFile> getAffectedFiles() {
152            String scm = getScmKind();
153	        throw new UnsupportedOperationException("getAffectedFiles() is not implemented by " + scm);
154        }
155
156        /**
157         * Gets the text fully marked up by {@link ChangeLogAnnotator}.
158         */
159        public String getMsgAnnotated() {
160            MarkupText markup = new MarkupText(getMsg());
161            for (ChangeLogAnnotator a : ChangeLogAnnotator.all())
162                a.annotate(parent.build,this,markup);
163
164            return markup.toString(false);
165        }
166
167        /**
168         * Message escaped for HTML
169         */
170        public String getMsgEscaped() {
171            return Util.escape(getMsg());
172        }
173
174        /**
175         * {@inheritDoc}
176         */
177        public String getCurrentRevision() {
178            String scm = getScmKind();
179            throw new UnsupportedOperationException("getCurrentRevision() is not implemented by " + scm);
180        }
181
182        /**
183         * Returns scm name.
184         * Help method used for throwing exception while executing unimplemented method.
185         *
186         * @return name.
187         */
188        private String getScmKind() {
189            String scm = "this SCM";
190            ChangeLogSet parent = getParent();
191            if (null != parent) {
192                String kind = parent.getKind();
193                if (null != kind && kind.trim().length() > 0) {
194                    scm = kind;
195                }
196            }
197            return scm;
198        }
199    }
200
201    /**
202     * Represents a file change. Contains filename, edit type, etc.
203     *
204     * I checked the API names against some some major SCMs and most SCMs
205     * can adapt to this interface with very little changes
206     *
207     * @see ChangeLogSet.Entry#getAffectedFiles()
208     */
209    public interface AffectedFile {
210        /**
211         * The path in the workspace that was affected
212         * <p>
213         * Contains string like 'foo/bar/zot'. No leading/trailing '/',
214         * and separator must be normalized to '/'.
215         *
216         * @return never null.
217         */
218        String getPath();
219
220        /**
221         * Return whether the file is new/modified/deleted
222         */
223        EditType getEditType();
224    }
225}