Search in sources :

Example 1 with LocalScmMetadata

use of org.apache.maven.scm.provider.local.metadata.LocalScmMetadata in project maven-scm by apache.

the class LocalUpdateCommandTckTest method testDeletion.

/**
 * Tests that a file that has been deleted from repository after checkout will be removed by scm-local. Local
 * additions must not be deleted.
 */
public void testDeletion() throws Exception {
    FileUtils.deleteDirectory(getUpdatingCopy());
    ScmRepository repository = makeScmRepository(getScmUrl());
    checkOut(getUpdatingCopy(), repository);
    // Check preconditions
    File readmeFileLocal = new File(getUpdatingCopy(), "readme.txt");
    assertTrue(readmeFileLocal.exists());
    File newFileLocal = new File(getUpdatingCopy(), "newfile.xml");
    assertTrue(!newFileLocal.exists());
    // Delete readme.txt from repository
    File readmeFileRepo = new File(getRepositoryRoot(), moduleName + "/readme.txt");
    assertTrue(readmeFileRepo.exists());
    assertTrue("Could not delete", readmeFileRepo.delete());
    assertFalse(readmeFileRepo.exists());
    // Make local addition to updating copy - this one must not be touched
    ScmTestCase.makeFile(getUpdatingCopy(), "newfile.xml", "added newfile.xml locally");
    assertTrue(newFileLocal.exists());
    // ----------------------------------------------------------------------
    // Update the project
    // ----------------------------------------------------------------------
    ScmManager scmManager = getScmManager();
    Date lastUpdate = new Date(System.currentTimeMillis());
    Thread.sleep(1000);
    UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
    assertNotNull("The command returned a null result.", result);
    assertResultIsSuccess(result);
    List<ScmFile> updatedFiles = result.getUpdatedFiles();
    assertEquals("Expected 1 files in the updated files list " + updatedFiles, 1, updatedFiles.size());
    // ----------------------------------------------------------------------
    // Assert the files in the updated files list
    // ----------------------------------------------------------------------
    Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
    // readme.txt
    ScmFile file = (ScmFile) files.next();
    assertPath("/readme.txt", file.getPath());
    assertTrue(file.getStatus().isUpdate());
    // ----------------------------------------------------------------------
    // Assert working directory contents
    // ----------------------------------------------------------------------
    // readme.txt
    assertTrue("Expected local copy of readme.txt to be deleted", !readmeFileLocal.exists());
    // newfile.xml
    assertTrue("Expected local copy of newfile.xml NOT to be deleted", newFileLocal.exists());
    // ----------------------------------------------------------------------
    // Assert metadata file
    // ----------------------------------------------------------------------
    File metadataFile = new File(getUpdatingCopy(), ".maven-scm-local");
    assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
    Reader reader = new FileReader(metadataFile);
    LocalScmMetadata metadata;
    try {
        metadata = new LocalScmMetadataXpp3Reader().read(reader);
    } finally {
        IOUtil.close(reader);
    }
    File root = new File(getRepositoryRoot() + "/" + moduleName);
    @SuppressWarnings("unchecked") List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
    assertEquals(fileNames, metadata.getRepositoryFileNames());
}
Also used : ScmRepository(org.apache.maven.scm.repository.ScmRepository) ScmFileSet(org.apache.maven.scm.ScmFileSet) LocalScmMetadata(org.apache.maven.scm.provider.local.metadata.LocalScmMetadata) UpdateScmResult(org.apache.maven.scm.command.update.UpdateScmResult) Reader(java.io.Reader) FileReader(java.io.FileReader) LocalScmMetadataXpp3Reader(org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader) Date(java.util.Date) ScmFile(org.apache.maven.scm.ScmFile) LocalScmMetadataXpp3Reader(org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader) ScmManager(org.apache.maven.scm.manager.ScmManager) FileReader(java.io.FileReader) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File)

Example 2 with LocalScmMetadata

use of org.apache.maven.scm.provider.local.metadata.LocalScmMetadata in project maven-scm by apache.

the class LocalUpdateCommand method executeUpdateCommand.

/**
 * {@inheritDoc}
 */
protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version) throws ScmException {
    LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
    if (version != null) {
        throw new ScmException("The local scm doesn't support tags.");
    }
    File root = new File(repository.getRoot());
    String module = repository.getModule();
    File source = new File(root, module);
    File baseDestination = fileSet.getBasedir();
    if (!baseDestination.exists()) {
        throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
    }
    if (!root.exists()) {
        throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
    }
    if (!source.exists()) {
        throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
    }
    if (!baseDestination.exists() && !baseDestination.isDirectory()) {
        throw new ScmException("The destination directory isn't a directory or doesn't exist (" + baseDestination.getAbsolutePath() + ").");
    }
    List<ScmFile> updatedFiles;
    try {
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Updating '" + baseDestination.getAbsolutePath() + "' from '" + source.getAbsolutePath() + "'.");
        }
        @SuppressWarnings("unchecked") List<File> fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null);
        List<File> list = fileList;
        updatedFiles = update(source, baseDestination, list);
        // process deletions in repository
        LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils(getLogger());
        LocalScmMetadata originalMetadata = metadataUtils.readMetadata(baseDestination);
        if (originalMetadata != null) {
            LocalScmMetadata newMetadata = metadataUtils.buildMetadata(source);
            for (Iterator<String> it = originalMetadata.getRepositoryFileNames().iterator(); it.hasNext(); ) {
                String filename = it.next();
                if (!newMetadata.getRepositoryFileNames().contains(filename)) {
                    File localFile = new File(baseDestination, filename);
                    if (localFile.exists()) {
                        localFile.delete();
                        updatedFiles.add(new ScmFile("/" + filename, ScmFileStatus.UPDATED));
                    }
                }
            }
        }
        // rewrite metadata file
        metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source));
    } catch (IOException ex) {
        throw new ScmException("Error while checking out the files.", ex);
    }
    return new LocalUpdateScmResult(null, updatedFiles);
}
Also used : ScmException(org.apache.maven.scm.ScmException) LocalScmProviderRepository(org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository) LocalScmMetadata(org.apache.maven.scm.provider.local.metadata.LocalScmMetadata) LocalScmMetadataUtils(org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils) IOException(java.io.IOException) ScmFile(org.apache.maven.scm.ScmFile) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File)

Example 3 with LocalScmMetadata

use of org.apache.maven.scm.provider.local.metadata.LocalScmMetadata in project maven-scm by apache.

the class LocalCheckOutCommandTckTest method testMetadata.

/**
 * Tests that the metadata file .maven-scm-local is written correctly
 */
public void testMetadata() throws Exception {
    FileUtils.deleteDirectory(getWorkingCopy());
    CheckOutScmResult result = checkOut(getWorkingCopy(), getScmRepository());
    assertResultIsSuccess(result);
    List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
    assertEquals(4, checkedOutFiles.size());
    // ----------------------------------------------------------------------
    // Assert metadata file
    // ----------------------------------------------------------------------
    File metadataFile = new File(getWorkingCopy(), ".maven-scm-local");
    assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
    Reader reader = new FileReader(metadataFile);
    LocalScmMetadata metadata;
    try {
        metadata = new LocalScmMetadataXpp3Reader().read(reader);
    } finally {
        IOUtil.close(reader);
    }
    File root = new File(getRepositoryRoot() + "/" + module);
    @SuppressWarnings("unchecked") List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
    assertEquals(fileNames, metadata.getRepositoryFileNames());
}
Also used : LocalScmMetadata(org.apache.maven.scm.provider.local.metadata.LocalScmMetadata) LocalScmMetadataXpp3Reader(org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader) FileReader(java.io.FileReader) Reader(java.io.Reader) LocalScmMetadataXpp3Reader(org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) FileReader(java.io.FileReader) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) ScmFile(org.apache.maven.scm.ScmFile)

Aggregations

File (java.io.File)3 ScmFile (org.apache.maven.scm.ScmFile)3 LocalScmMetadata (org.apache.maven.scm.provider.local.metadata.LocalScmMetadata)3 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 LocalScmMetadataXpp3Reader (org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader)2 IOException (java.io.IOException)1 Date (java.util.Date)1 ScmException (org.apache.maven.scm.ScmException)1 ScmFileSet (org.apache.maven.scm.ScmFileSet)1 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)1 UpdateScmResult (org.apache.maven.scm.command.update.UpdateScmResult)1 ScmManager (org.apache.maven.scm.manager.ScmManager)1 LocalScmMetadataUtils (org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils)1 LocalScmProviderRepository (org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository)1 ScmRepository (org.apache.maven.scm.repository.ScmRepository)1