/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. */
  24. package hudson.scm;
  25. import com.google.common.collect.Iterators;
  26. import hudson.MarkupText;
  27. import hudson.Util;
  28. import hudson.model.AbstractBuild;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.Iterator;
  32. import java.util.List;
  33. import org.apache.commons.collections.CollectionUtils;
  34. import org.kohsuke.stapler.export.Exported;
  35. import org.kohsuke.stapler.export.ExportedBean;
  36. /**
  37. * Represents SCM change list.
  38. * <p/>
  39. * <p/>
  40. * Use the "index" view of this object to render the changeset detail page,
  41. * and use the "digest" view of this object to render the summary page.
  42. * For the change list at project level, see {@link SCM}.
  43. * <p/>
  44. * <p/>
  45. * {@link Iterator} is expected to return recent changes first.
  46. *
  47. * @author Kohsuke Kawaguchi
  48. * @author Nikia Levyankov
  49. */
  50. @ExportedBean(defaultVisibility = 999)
  51. public abstract class ChangeLogSet<T extends ChangeLogSet.Entry> implements Iterable<T> {
  52. /**
  53. * {@link AbstractBuild} whose change log this object represents.
  54. */
  55. //TODO: review and check whether we can do it private
  56. public final AbstractBuild<?, ?> build;
  57. protected ChangeLogSet(AbstractBuild<?, ?> build) {
  58. this.build = build;
  59. }
  60. public AbstractBuild<?, ?> getBuild() {
  61. return build;
  62. }
  63. /**
  64. * Returns true if there's no change.
  65. *
  66. * @return if {@link #getLogs()} returns empty or null list
  67. */
  68. public boolean isEmptySet() {
  69. return CollectionUtils.isEmpty(getLogs());
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. @SuppressWarnings("unchecked")
  75. public Iterator<T> iterator() {
  76. return isEmptySet()? Iterators.<T>emptyIterator(): getLogs().iterator();
  77. }
  78. /**
  79. * All changes in this change set.
  80. */
  81. // method for the remote API.
  82. @Exported
  83. public final Object[] getItems() {
  84. List<T> r = new ArrayList<T>();
  85. for (T t : this)
  86. r.add(t);
  87. return r.toArray();
  88. }
  89. /**
  90. * Optional identification of the kind of SCM being used.
  91. * @return a short token, such as the SCM's main CLI executable name
  92. * @since 1.284
  93. */
  94. @Exported
  95. public String getKind() {
  96. return null;
  97. }
  98. /**
  99. * Returns change log entries.
  100. *
  101. * @return logs.
  102. */
  103. public abstract Collection<T> getLogs();
  104. /**
  105. * Constant instance that represents no changes.
  106. */
  107. public static ChangeLogSet<? extends ChangeLogSet.Entry> createEmpty(AbstractBuild build) {
  108. return new EmptyChangeLogSet(build);
  109. }
  110. @ExportedBean(defaultVisibility = 999)
  111. public static abstract class Entry implements ChangeLogEntry {
  112. private ChangeLogSet parent;
  113. public ChangeLogSet getParent() {
  114. return parent;
  115. }
  116. /**
  117. * Should be invoked before a {@link ChangeLogSet} is exposed to public.
  118. */
  119. protected void setParent(ChangeLogSet parent) {
  120. this.parent = parent;
  121. }
  122. /**
  123. * Returns a set of paths in the workspace that was
  124. * affected by this change.
  125. * <p>
  126. * Noted: since this is a new interface, some of the SCMs may not have
  127. * implemented this interface. The default implementation for this
  128. * interface is throw UnsupportedOperationException
  129. * <p>
  130. * It doesn't throw NoSuchMethodException because I rather to throw a
  131. * runtime exception
  132. *
  133. * @return AffectedFile never null.
  134. * @since 1.309
  135. */
  136. public Collection<? extends AffectedFile> getAffectedFiles() {
  137. String scm = getScmKind();
  138. throw new UnsupportedOperationException("getAffectedFiles() is not implemented by " + scm);
  139. }
  140. /**
  141. * Gets the text fully marked up by {@link ChangeLogAnnotator}.
  142. */
  143. public String getMsgAnnotated() {
  144. MarkupText markup = new MarkupText(getMsg());
  145. for (ChangeLogAnnotator a : ChangeLogAnnotator.all())
  146. a.annotate(parent.build,this,markup);
  147. return markup.toString(false);
  148. }
  149. /**
  150. * Message escaped for HTML
  151. */
  152. public String getMsgEscaped() {
  153. return Util.escape(getMsg());
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public String getCurrentRevision() {
  159. String scm = getScmKind();
  160. throw new UnsupportedOperationException("getCurrentRevision() is not implemented by " + scm);
  161. }
  162. /**
  163. * Returns scm name.
  164. * Help method used for throwing exception while executing unimplemented method.
  165. *
  166. * @return name.
  167. */
  168. private String getScmKind() {
  169. String scm = "this SCM";
  170. ChangeLogSet parent = getParent();
  171. if (null != parent) {
  172. String kind = parent.getKind();
  173. if (null != kind && kind.trim().length() > 0) {
  174. scm = kind;
  175. }
  176. }
  177. return scm;
  178. }
  179. }
  180. /**
  181. * Represents a file change. Contains filename, edit type, etc.
  182. *
  183. * I checked the API names against some some major SCMs and most SCMs
  184. * can adapt to this interface with very little changes
  185. *
  186. * @see ChangeLogSet.Entry#getAffectedFiles()
  187. */
  188. public interface AffectedFile {
  189. /**
  190. * The path in the workspace that was affected
  191. * <p>
  192. * Contains string like 'foo/bar/zot'. No leading/trailing '/',
  193. * and separator must be normalized to '/'.
  194. *
  195. * @return never null.
  196. */
  197. String getPath();
  198. /**
  199. * Return whether the file is new/modified/deleted
  200. */
  201. EditType getEditType();
  202. }
  203. }