PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/bamboo/plugins/git/CommitOutputHandler.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 191 lines | 168 code | 14 blank | 9 comment | 49 complexity | 1e4db099199d4fda2b73f25a40bc6c44 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git;
  2. import com.atlassian.bamboo.author.AuthorImpl;
  3. import com.atlassian.bamboo.commit.CommitContext;
  4. import com.atlassian.bamboo.commit.CommitFileImpl;
  5. import com.atlassian.bamboo.commit.CommitImpl;
  6. import com.atlassian.utils.process.LineOutputHandler;
  7. import com.google.common.collect.Lists;
  8. import org.apache.log4j.Logger;
  9. import org.jetbrains.annotations.NotNull;
  10. import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
  11. import javax.annotation.concurrent.NotThreadSafe;
  12. import java.util.Date;
  13. import java.util.List;
  14. import java.util.Set;
  15. @NotThreadSafe
  16. public class CommitOutputHandler extends LineOutputHandler implements GitCommandProcessor.GitOutputHandler
  17. {
  18. private static final Logger log = Logger.getLogger(CommitOutputHandler.class);
  19. // ------------------------------------------------------------------------------------------------------- Constants
  20. static final String SALT = "[d31bfa5_BAM_";
  21. static final String HASH = SALT + "hash]";
  22. static final String COMMITER_NAME = SALT + "commiter_name]";
  23. static final String COMMITER_EMAIL = SALT + "commiter_email]";
  24. static final String TIMESTAMP = SALT + "timestamp]";
  25. static final String COMMIT_MESSAGE = SALT + "commit_message]";
  26. static final String END_OF_COMMIT_MESSAGE = SALT + "commit_message_end]";
  27. static final String FILE_LIST = SALT + "file_list]";
  28. public static final String LOG_COMMAND_FORMAT_STRING = HASH+"%H%n"+COMMITER_NAME+"%cN%n"+COMMITER_EMAIL+"%ce%n"+TIMESTAMP+"%ct%n"+COMMIT_MESSAGE+"%s%n%b"+ END_OF_COMMIT_MESSAGE +"%n" +FILE_LIST;
  29. private enum CommitParserState
  30. {
  31. INFO,
  32. COMMIT_MESSAGE,
  33. COMMIT_MESSAGE_COMPLETE,
  34. FILE_LIST,
  35. TOO_MANY_COMMITS
  36. }
  37. // ------------------------------------------------------------------------------------------------- Type Properties
  38. private List<CommitContext> extractedCommits = Lists.newArrayList();
  39. private Set<String> shallows;
  40. private CommitImpl currentCommit = null;
  41. private String commiterName = null;
  42. private int skippedCommitCount;
  43. private int maxCommitNumber;
  44. private StringBuilder commitMessage = null;
  45. CommitParserState parserState = CommitParserState.INFO;
  46. // ---------------------------------------------------------------------------------------------------- Dependencies
  47. // ---------------------------------------------------------------------------------------------------- Constructors
  48. public CommitOutputHandler(@NotNull Set<String> shallows)
  49. {
  50. super(GitCommandProcessor.GIT_OUTPUT_ENCODING);
  51. this.shallows = shallows;
  52. maxCommitNumber = Integer.MAX_VALUE;
  53. }
  54. public CommitOutputHandler(@NotNull Set<String> shallows, int maxCommitNumber)
  55. {
  56. super(GitCommandProcessor.GIT_OUTPUT_ENCODING);
  57. this.shallows = shallows;
  58. this.maxCommitNumber = maxCommitNumber;
  59. }
  60. // ----------------------------------------------------------------------------------------------- Interface Methods
  61. @Override
  62. public String getStdout()
  63. {
  64. return "";
  65. }
  66. @Override
  67. protected void processLine(final int lineNum, final String line)
  68. {
  69. if (parserState != CommitParserState.TOO_MANY_COMMITS)
  70. {
  71. if (parserState != CommitParserState.COMMIT_MESSAGE && line.startsWith(HASH))
  72. {
  73. if (extractedCommits.size() < maxCommitNumber)
  74. {
  75. parserState = CommitParserState.INFO;
  76. currentCommit = new CommitImpl();
  77. commiterName = null;
  78. currentCommit.setAuthor(new AuthorImpl(AuthorImpl.UNKNOWN_AUTHOR));
  79. currentCommit.setChangeSetId(getLineContent(HASH,line));
  80. extractedCommits.add(currentCommit);
  81. }
  82. else
  83. {
  84. currentCommit = null;
  85. commiterName = null;
  86. skippedCommitCount++;
  87. parserState = CommitParserState.TOO_MANY_COMMITS;
  88. }
  89. }
  90. if (parserState == CommitParserState.COMMIT_MESSAGE)
  91. {
  92. if (line.startsWith(END_OF_COMMIT_MESSAGE))
  93. {
  94. if (currentCommit != null && commitMessage != null)
  95. {
  96. currentCommit.setComment(commitMessage.toString());
  97. commitMessage = null;
  98. }
  99. parserState = CommitParserState.COMMIT_MESSAGE_COMPLETE;
  100. }
  101. else if (line.startsWith(FILE_LIST))
  102. {
  103. //the commit message didn't contain EOL: we need to strip 'end of message' marker
  104. if (currentCommit != null && commitMessage != null)
  105. {
  106. currentCommit.setComment(commitMessage.substring(0, commitMessage.lastIndexOf(END_OF_COMMIT_MESSAGE)));
  107. commitMessage = null;
  108. }
  109. parserState = CommitParserState.FILE_LIST;
  110. }
  111. else
  112. {
  113. commitMessage.append('\n');
  114. commitMessage.append(line);
  115. }
  116. }
  117. else if (parserState == CommitParserState.COMMIT_MESSAGE_COMPLETE && line.startsWith(FILE_LIST))
  118. {
  119. parserState = CommitParserState.FILE_LIST;
  120. }
  121. else if (parserState == CommitParserState.FILE_LIST && currentCommit != null && !StringUtils.isBlank(line) && !shallows.contains(currentCommit.getChangeSetId()))
  122. {
  123. currentCommit.addFile(new CommitFileImpl(currentCommit.getChangeSetId(), line.trim()));
  124. }
  125. else if (parserState == CommitParserState.INFO)
  126. {
  127. if (line.startsWith(COMMITER_NAME))
  128. {
  129. if (currentCommit != null)
  130. {
  131. commiterName = getLineContent(COMMITER_NAME, line);
  132. }
  133. }
  134. else if (line.startsWith(COMMITER_EMAIL))
  135. {
  136. if (currentCommit != null && !StringUtils.isBlank(commiterName))
  137. {
  138. String email = getLineContent(COMMITER_EMAIL, line);
  139. currentCommit.setAuthor(new AuthorImpl(String.format("%s <%s>", commiterName, email), null, email));
  140. }
  141. }
  142. else if (line.startsWith(TIMESTAMP))
  143. {
  144. if (currentCommit != null)
  145. {
  146. String timestampString = getLineContent(TIMESTAMP, line);
  147. currentCommit.setDate(new Date(Long.parseLong(timestampString)*1000));
  148. }
  149. }
  150. else if (line.startsWith(COMMIT_MESSAGE))
  151. {
  152. commitMessage = new StringBuilder(getLineContent(COMMIT_MESSAGE, line));
  153. parserState = CommitParserState.COMMIT_MESSAGE;
  154. }
  155. }
  156. }
  157. else if (line.startsWith(HASH)) //too many commits
  158. {
  159. skippedCommitCount++;
  160. }
  161. }
  162. private String getLineContent(final String keyword, final String line)
  163. {
  164. return line.substring(keyword.length()).trim();
  165. }
  166. // -------------------------------------------------------------------------------------------------- Action Methods
  167. // -------------------------------------------------------------------------------------------------- Public Methods
  168. public List<CommitContext> getExtractedCommits()
  169. {
  170. return extractedCommits;
  171. }
  172. public int getSkippedCommitCount()
  173. {
  174. return skippedCommitCount;
  175. }
  176. // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
  177. }