1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.local.command.update;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.maven.scm.ScmException;
28 import org.apache.maven.scm.ScmFile;
29 import org.apache.maven.scm.ScmFileSet;
30 import org.apache.maven.scm.ScmFileStatus;
31 import org.apache.maven.scm.ScmVersion;
32 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
33 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
34 import org.apache.maven.scm.command.update.UpdateScmResult;
35 import org.apache.maven.scm.provider.ScmProviderRepository;
36 import org.apache.maven.scm.provider.local.command.LocalCommand;
37 import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand;
38 import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
39 import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils;
40 import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
41 import org.codehaus.plexus.util.FileUtils;
42
43
44
45
46
47
48 public class LocalUpdateCommand extends AbstractUpdateCommand implements LocalCommand {
49
50 protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version)
51 throws ScmException {
52 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
53
54 if (version != null) {
55 throw new ScmException("The local scm doesn't support tags.");
56 }
57
58 File root = new File(repository.getRoot());
59
60 String module = repository.getModule();
61
62 File source = new File(root, module);
63
64 File baseDestination = fileSet.getBasedir();
65
66 if (!baseDestination.exists()) {
67 throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
68 }
69
70 if (!root.exists()) {
71 throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
72 }
73
74 if (!source.exists()) {
75 throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
76 }
77
78 if (!baseDestination.exists() && !baseDestination.isDirectory()) {
79 throw new ScmException("The destination directory isn't a directory or doesn't exist ("
80 + baseDestination.getAbsolutePath() + ").");
81 }
82
83 List<ScmFile> updatedFiles;
84
85 try {
86 if (logger.isInfoEnabled()) {
87 logger.info("Updating '" + baseDestination.getAbsolutePath() + "' from '" + source.getAbsolutePath()
88 + "'.");
89 }
90
91 List<File> fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null);
92 updatedFiles = update(source, baseDestination, fileList);
93
94
95 LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils();
96 LocalScmMetadata originalMetadata = metadataUtils.readMetadata(baseDestination);
97 if (originalMetadata != null) {
98 LocalScmMetadata newMetadata = metadataUtils.buildMetadata(source);
99 for (Iterator<String> it =
100 originalMetadata.getRepositoryFileNames().iterator();
101 it.hasNext(); ) {
102 String filename = it.next();
103 if (!newMetadata.getRepositoryFileNames().contains(filename)) {
104 File localFile = new File(baseDestination, filename);
105 if (localFile.exists()) {
106 localFile.delete();
107 updatedFiles.add(new ScmFile("/" + filename, ScmFileStatus.UPDATED));
108 }
109 }
110 }
111 }
112
113
114 metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source));
115
116 } catch (IOException ex) {
117 throw new ScmException("Error while checking out the files.", ex);
118 }
119
120 return new LocalUpdateScmResult(null, updatedFiles);
121 }
122
123 private List<ScmFile> update(File source, File baseDestination, List<File> files) throws ScmException, IOException {
124 String sourcePath = source.getAbsolutePath();
125
126 List<ScmFile> updatedFiles = new ArrayList<>();
127
128 for (Iterator<File> i = files.iterator(); i.hasNext(); ) {
129 File repositoryFile = i.next();
130
131 File repositoryDirectory = repositoryFile.getParentFile();
132
133 String dest = repositoryFile.getAbsolutePath().substring(sourcePath.length() + 1);
134
135 File destinationFile = new File(baseDestination, dest);
136
137 String repositoryFileContents = FileUtils.fileRead(repositoryFile);
138
139 if (destinationFile.exists()) {
140 String destionationFileContents = FileUtils.fileRead(destinationFile);
141
142 if (repositoryFileContents.equals(destionationFileContents)) {
143 continue;
144 }
145 }
146
147 File destinationDirectory = destinationFile.getParentFile();
148
149 if (!destinationDirectory.exists() && !destinationDirectory.mkdirs()) {
150 throw new ScmException(
151 "Could not create destination directory '" + destinationDirectory.getAbsolutePath() + "'.");
152 }
153
154 ScmFileStatus status;
155
156 if (destinationFile.exists()) {
157 status = ScmFileStatus.UPDATED;
158 } else {
159 status = ScmFileStatus.ADDED;
160 }
161
162 FileUtils.copyFileToDirectory(repositoryFile, destinationDirectory);
163
164 int chop = baseDestination.getAbsolutePath().length();
165
166 String fileName = destinationFile.getAbsolutePath().substring(chop + 1);
167
168 updatedFiles.add(new ScmFile(fileName, status));
169 }
170
171 return updatedFiles;
172 }
173
174
175 protected ChangeLogCommand getChangeLogCommand() {
176 return new LocalChangeLogCommand();
177 }
178 }