Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FindFork.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scm.svn;
  21. import java.io.File;
  22. import java.nio.file.Path;
  23. import java.time.Instant;
  24. import java.util.Optional;
  25. import javax.annotation.CheckForNull;
  26. import org.sonar.api.scanner.ScannerSide;
  27. import org.sonar.api.utils.log.Logger;
  28. import org.sonar.api.utils.log.Loggers;
  29. import org.tmatesoft.svn.core.ISVNLogEntryHandler;
  30. import org.tmatesoft.svn.core.SVNException;
  31. import org.tmatesoft.svn.core.SVNLogEntry;
  32. import org.tmatesoft.svn.core.SVNLogEntryPath;
  33. import org.tmatesoft.svn.core.wc.SVNClientManager;
  34. import org.tmatesoft.svn.core.wc.SVNRevision;
  35. import org.tmatesoft.svn.core.wc.SVNStatus;
  36. import static org.sonar.scm.svn.SvnScmSupport.newSvnClientManager;
  37. @ScannerSide
  38. public class FindFork {
  39. private static final Logger LOG = Loggers.get(FindFork.class);
  40. private final SvnConfiguration configuration;
  41. public FindFork(SvnConfiguration configuration) {
  42. this.configuration = configuration;
  43. }
  44. @CheckForNull
  45. public Instant findDate(Path location, String referenceBranch) throws SVNException {
  46. ForkPoint forkPoint = find(location, referenceBranch);
  47. if (forkPoint != null) {
  48. return forkPoint.date();
  49. }
  50. return null;
  51. }
  52. @CheckForNull
  53. public ForkPoint find(Path location, String referenceBranch) throws SVNException {
  54. SVNClientManager clientManager = newSvnClientManager(configuration);
  55. SVNRevision revision = getSvnRevision(location, clientManager);
  56. LOG.debug("latest revision is " + revision);
  57. String svnRefBranch = "/" + referenceBranch;
  58. SVNLogEntryHolder handler = new SVNLogEntryHolder();
  59. SVNRevision endRevision = SVNRevision.create(1);
  60. SVNRevision startRevision = SVNRevision.create(revision.getNumber());
  61. do {
  62. clientManager.getLogClient().doLog(new File[] {location.toFile()}, startRevision, endRevision, true, true, -1, handler);
  63. SVNLogEntry lastEntry = handler.getLastEntry();
  64. Optional<SVNLogEntryPath> copyFromReference = lastEntry.getChangedPaths().values().stream()
  65. .filter(e -> e.getCopyPath() != null && e.getCopyPath().equals(svnRefBranch))
  66. .findFirst();
  67. if (copyFromReference.isPresent()) {
  68. return new ForkPoint(String.valueOf(copyFromReference.get().getCopyRevision()), Instant.ofEpochMilli(lastEntry.getDate().getTime()));
  69. }
  70. if (lastEntry.getChangedPaths().isEmpty()) {
  71. // shouldn't happen since it should only stop in revisions with changed paths
  72. return null;
  73. }
  74. SVNLogEntryPath firstChangedPath = lastEntry.getChangedPaths().values().iterator().next();
  75. if (firstChangedPath.getCopyPath() == null) {
  76. // we walked the history to the root, and the last commit found had no copy reference. Must be the trunk, there is no fork point
  77. return null;
  78. }
  79. // TODO Looks like a revision can have multiple changed paths. Should we iterate through all of them?
  80. startRevision = SVNRevision.create(firstChangedPath.getCopyRevision());
  81. } while (true);
  82. }
  83. private static SVNRevision getSvnRevision(Path location, SVNClientManager clientManager) throws SVNException {
  84. SVNStatus svnStatus = clientManager.getStatusClient().doStatus(location.toFile(), false);
  85. return svnStatus.getRevision();
  86. }
  87. /**
  88. * Handler keeping only the last entry, and count how many entries have been seen.
  89. */
  90. private static class SVNLogEntryHolder implements ISVNLogEntryHandler {
  91. SVNLogEntry value;
  92. public SVNLogEntry getLastEntry() {
  93. return value;
  94. }
  95. @Override
  96. public void handleLogEntry(SVNLogEntry svnLogEntry) {
  97. this.value = svnLogEntry;
  98. }
  99. }
  100. }