|
| 1 | +package hudson.plugins.scm_sync_configuration.scms.customproviders.git.gitexe; |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import org.apache.commons.lang.StringUtils; |
| 29 | +import org.apache.maven.scm.ScmFile; |
| 30 | +import org.apache.maven.scm.ScmFileStatus; |
| 31 | +import org.apache.maven.scm.log.ScmLogger; |
| 32 | +import org.codehaus.plexus.util.cli.StreamConsumer; |
| 33 | + |
| 34 | +import com.google.common.base.Strings; |
| 35 | + |
| 36 | +/** |
| 37 | + * Copied from org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer (maven-scm 1.9.1) |
| 38 | + * and fixed to account for https://issues.apache.org/jira/browse/SCM-772 . |
| 39 | + */ |
| 40 | +public class FixedGitStatusConsumer |
| 41 | + implements StreamConsumer |
| 42 | +{ |
| 43 | + |
| 44 | + /** |
| 45 | + * The pattern used to match added file lines |
| 46 | + */ |
| 47 | + private static final Pattern ADDED_PATTERN = Pattern.compile( "^A[ M]* (.*)$" ); |
| 48 | + |
| 49 | + /** |
| 50 | + * The pattern used to match modified file lines |
| 51 | + */ |
| 52 | + private static final Pattern MODIFIED_PATTERN = Pattern.compile( "^ *M[ M]* (.*)$" ); |
| 53 | + |
| 54 | + /** |
| 55 | + * The pattern used to match deleted file lines |
| 56 | + */ |
| 57 | + private static final Pattern DELETED_PATTERN = Pattern.compile( "^ *D * (.*)$" ); |
| 58 | + |
| 59 | + /** |
| 60 | + * The pattern used to match renamed file lines |
| 61 | + */ |
| 62 | + private static final Pattern RENAMED_PATTERN = Pattern.compile( "^R (.*) -> (.*)$" ); |
| 63 | + |
| 64 | + private ScmLogger logger; |
| 65 | + |
| 66 | + private File workingDirectory; |
| 67 | + |
| 68 | + /** |
| 69 | + * Entries are relative to working directory, not to the repositoryroot |
| 70 | + */ |
| 71 | + private List<ScmFile> changedFiles = new ArrayList<ScmFile>(); |
| 72 | + |
| 73 | + private String relativeRepositoryPath; |
| 74 | + |
| 75 | + private final File repositoryRoot; |
| 76 | + |
| 77 | + // ---------------------------------------------------------------------- |
| 78 | + // |
| 79 | + // ---------------------------------------------------------------------- |
| 80 | + |
| 81 | + public FixedGitStatusConsumer (ScmLogger logger, File workingDirectory, File repositoryRoot) { |
| 82 | + this.logger = logger; |
| 83 | + this.workingDirectory = workingDirectory; |
| 84 | + if (repositoryRoot != null) { |
| 85 | + String absoluteRepositoryRoot = repositoryRoot.getAbsolutePath(); // Make sure all separators are File.separator |
| 86 | + // The revparse runs with fileset.getBasedir(). That of course must be under the repo root. |
| 87 | + String basePath = workingDirectory.getAbsolutePath(); |
| 88 | + if (!absoluteRepositoryRoot.endsWith(File.separator)) { |
| 89 | + absoluteRepositoryRoot += File.separator; |
| 90 | + } |
| 91 | + if (basePath.startsWith(absoluteRepositoryRoot)) { |
| 92 | + String pathInsideRepo = basePath.substring(absoluteRepositoryRoot.length()); |
| 93 | + if (!Strings.isNullOrEmpty(pathInsideRepo)) { |
| 94 | + relativeRepositoryPath = pathInsideRepo; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + this.repositoryRoot = repositoryRoot; |
| 99 | + // Either the workingDirectory == repositoryRoot: we have no relativeRepositoryPath set |
| 100 | + // Or the working directory was a subdirectory (in the workspace!) of repositoryRoot, then |
| 101 | + // relativeRepositoryPath contains now the relative path to the working directory. |
| 102 | + // |
| 103 | + // It would appear that git status --porcelain always returns paths relative to the repository |
| 104 | + // root. |
| 105 | + } |
| 106 | + |
| 107 | + // ---------------------------------------------------------------------- |
| 108 | + // StreamConsumer Implementation |
| 109 | + // ---------------------------------------------------------------------- |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritDoc} |
| 113 | + */ |
| 114 | + public void consumeLine( String line ) |
| 115 | + { |
| 116 | + if ( logger.isDebugEnabled() ) |
| 117 | + { |
| 118 | + logger.debug( line ); |
| 119 | + } |
| 120 | + if ( StringUtils.isEmpty( line ) ) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + ScmFileStatus status = null; |
| 126 | + |
| 127 | + List<String> files = new ArrayList<String>(); |
| 128 | + |
| 129 | + Matcher matcher; |
| 130 | + if ( ( matcher = ADDED_PATTERN.matcher( line ) ).find() ) |
| 131 | + { |
| 132 | + status = ScmFileStatus.ADDED; |
| 133 | + files.add(ScmSyncGitUtils.dequote(matcher.group(1))); |
| 134 | + } |
| 135 | + else if ( ( matcher = MODIFIED_PATTERN.matcher( line ) ).find() ) |
| 136 | + { |
| 137 | + status = ScmFileStatus.MODIFIED; |
| 138 | + files.add(ScmSyncGitUtils.dequote(matcher.group(1))); |
| 139 | + } |
| 140 | + else if ( ( matcher = DELETED_PATTERN.matcher( line ) ) .find() ) |
| 141 | + { |
| 142 | + status = ScmFileStatus.DELETED; |
| 143 | + files.add(ScmSyncGitUtils.dequote(matcher.group(1))); |
| 144 | + } |
| 145 | + else if ( ( matcher = RENAMED_PATTERN.matcher( line ) ).find() ) |
| 146 | + { |
| 147 | + status = ScmFileStatus.RENAMED; |
| 148 | + files.add(ScmSyncGitUtils.dequote(matcher.group(1))); |
| 149 | + files.add(ScmSyncGitUtils.dequote(matcher.group(2))); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + logger.warn( "Ignoring unrecognized line: " + line ); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + // If the file isn't a file; don't add it. |
| 158 | + if (files.isEmpty() || status == null) { |
| 159 | + return; |
| 160 | + } |
| 161 | + File checkDir = repositoryRoot; |
| 162 | + if (workingDirectory != null && relativeRepositoryPath != null) { |
| 163 | + // Make all paths relative to this directory. |
| 164 | + List<String> relativeNames = new ArrayList<String>(); |
| 165 | + for (String repoRelativeName : files) { |
| 166 | + relativeNames.add(ScmSyncGitUtils.relativizePath(relativeRepositoryPath, new File(repoRelativeName).getPath())); |
| 167 | + } |
| 168 | + files = relativeNames; |
| 169 | + checkDir = workingDirectory; |
| 170 | + } |
| 171 | + // Now check them all against the checkDir. This check has been taken over from the base implementation |
| 172 | + // in maven-scm's GitStatusConsumer, but I'm not really sure this makes sense. Who said the workspace |
| 173 | + // had to be equal to the git index (staging area) here? |
| 174 | + if (status == ScmFileStatus.RENAMED) { |
| 175 | + String oldFilePath = files.get( 0 ); |
| 176 | + String newFilePath = files.get( 1 ); |
| 177 | + if (new File(checkDir, oldFilePath).isFile()) { |
| 178 | + logger.debug("file '" + oldFilePath + "' still exists after rename"); |
| 179 | + return; |
| 180 | + } |
| 181 | + if (!new File(checkDir, newFilePath).isFile()) { |
| 182 | + logger.debug("file '" + newFilePath + "' does not exist after rename"); |
| 183 | + return; |
| 184 | + } |
| 185 | + } else if (status == ScmFileStatus.DELETED) { |
| 186 | + if (new File(checkDir, files.get(0)).isFile()) { |
| 187 | + return; |
| 188 | + } |
| 189 | + } else { |
| 190 | + if (!new File(checkDir, files.get(0)).isFile()) { |
| 191 | + return; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + for (String file : files) { |
| 196 | + changedFiles.add(new ScmFile(file.replaceAll(File.separator, "/"), status)); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + public List<ScmFile> getChangedFiles() |
| 201 | + { |
| 202 | + return changedFiles; |
| 203 | + } |
| 204 | +} |
0 commit comments