Search in sources :

Example 71 with ScmFile

use of org.apache.maven.scm.ScmFile in project maven-scm by apache.

the class StatusCommandTckTest method commit.

protected void commit(File workingDirectory, ScmRepository repository) throws Exception {
    CheckInScmResult result = getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), "No msg");
    assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
    List<ScmFile> committedFiles = result.getCheckedInFiles();
    assertEquals("Expected 2 files in the committed files list " + committedFiles, 2, committedFiles.size());
}
Also used : ScmFileSet(org.apache.maven.scm.ScmFileSet) CheckInScmResult(org.apache.maven.scm.command.checkin.CheckInScmResult) ScmFile(org.apache.maven.scm.ScmFile)

Example 72 with ScmFile

use of org.apache.maven.scm.ScmFile in project maven-scm by apache.

the class UpdateCommandTckTest method testUpdateCommand.

public void testUpdateCommand() throws Exception {
    deleteDirectory(getUpdatingCopy());
    assertFalse(getUpdatingCopy().exists());
    // deleteDirectory( getWorkingCopy() );
    // assertFalse( getUpdatingCopy().exists() );
    ScmRepository repository = makeScmRepository(getScmUrl());
    checkOut(getUpdatingCopy(), repository);
    // ----------------------------------------------------------------------
    // Change the files
    // ----------------------------------------------------------------------
    /*
         * readme.txt is changed (changed file in the root directory)
         * project.xml is added (added file in the root directory)
         * src/test/resources is untouched (a empty directory is left untouched)
         * src/test/java is untouched (a non empty directory is left untouched)
         * src/test/java/org (a empty directory is added)
         * src/main/java/org/Foo.java (a non empty directory is added)
         */
    // /readme.txt
    this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
    ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
    // /project.xml
    ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
    addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
    // /src/test/java/org
    ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
    addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), getScmRepository());
    // /src/main/java/org/Foo.java
    ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
    addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), getScmRepository());
    // src/main/java/org/Foo.java
    addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), getScmRepository());
    ScmManager scmManager = getScmManager();
    Date lastUpdate = new Date(System.currentTimeMillis() - 1000000);
    commit(getWorkingCopy(), getScmRepository());
    Thread.sleep(5000);
    // ----------------------------------------------------------------------
    // Update the project
    // ----------------------------------------------------------------------
    UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
    assertNotNull("The command returned a null result.", result);
    assertResultIsSuccess(result);
    List<ScmFile> updatedFiles = result.getUpdatedFiles();
    List<ChangeSet> changedSets = result.getChanges();
    assertEquals("Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size());
    assertNotNull("The changed files list is null", changedSets);
    assertFalse("The changed files list is empty ", changedSets.isEmpty());
    for (ChangeSet changeSet : changedSets) {
        System.out.println(changeSet.toXML());
    }
    // ----------------------------------------------------------------------
    // Assert the files in the updated files list
    // ----------------------------------------------------------------------
    Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
    // Foo.java
    ScmFile file = files.next();
    assertPath("/src/main/java/org/Foo.java", file.getPath());
    // TODO : Consolidate file status so that we can remove "|| ADDED" term
    assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
    // readme.txt
    file = files.next();
    assertPath("/readme.txt", file.getPath());
    assertTrue(file.getStatus().isUpdate());
    // project.xml
    file = files.next();
    assertPath("/project.xml", file.getPath());
    // TODO : Consolidate file status so that we can remove "|| ADDED" term
    assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
}
Also used : ScmRepository(org.apache.maven.scm.repository.ScmRepository) ScmFileSet(org.apache.maven.scm.ScmFileSet) ScmManager(org.apache.maven.scm.manager.ScmManager) UpdateScmResult(org.apache.maven.scm.command.update.UpdateScmResult) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) ChangeSet(org.apache.maven.scm.ChangeSet) Date(java.util.Date) ScmFile(org.apache.maven.scm.ScmFile)

Example 73 with ScmFile

use of org.apache.maven.scm.ScmFile in project maven-scm by apache.

the class CheckOutCommandTckTest method testCheckOutCommandTest.

public void testCheckOutCommandTest() throws Exception {
    deleteDirectory(getWorkingCopy());
    CheckOutScmResult result = checkOut(getWorkingCopy(), getScmRepository());
    assertResultIsSuccess(result);
    List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
    if (checkedOutFiles.size() != 4) {
        SortedSet<ScmFile> files = new TreeSet<ScmFile>(checkedOutFiles);
        int i = 0;
        for (Iterator<ScmFile> it = files.iterator(); it.hasNext(); i++) {
            ScmFile scmFile = it.next();
            System.out.println("" + i + ": " + scmFile);
        }
        fail("Expected 4 files in the updated files list, was " + checkedOutFiles.size());
    }
}
Also used : TreeSet(java.util.TreeSet) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) ScmFile(org.apache.maven.scm.ScmFile)

Example 74 with ScmFile

use of org.apache.maven.scm.ScmFile in project maven-scm by apache.

the class GitCheckInCommand method executeCheckInCommand.

/**
 * {@inheritDoc}
 */
protected CheckInScmResult executeCheckInCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
    try {
        FileUtils.fileWrite(messageFile.getAbsolutePath(), message);
    } catch (IOException ex) {
        return new CheckInScmResult(null, "Error while making a temporary file for the commit message: " + ex.getMessage(), null, false);
    }
    try {
        if (!fileSet.getFileList().isEmpty()) {
            // if specific fileSet is given, we have to git-add them first
            // otherwise we will use 'git-commit -a' later
            Commandline clAdd = GitAddCommand.createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
            exitCode = GitCommandLineUtils.execute(clAdd, stdout, stderr, getLogger());
            if (exitCode != 0) {
                return new CheckInScmResult(clAdd.toString(), "The git-add command failed.", stderr.getOutput(), false);
            }
        }
        // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with
        // relativeRepositoryPath
        Commandline clRevparse = GitStatusCommand.createRevparseShowToplevelCommand(fileSet);
        stdout = new CommandLineUtils.StringStreamConsumer();
        stderr = new CommandLineUtils.StringStreamConsumer();
        URI relativeRepositoryPath = null;
        exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr, getLogger());
        if (exitCode != 0) {
            // git-status returns non-zero if nothing to do
            if (getLogger().isInfoEnabled()) {
                getLogger().info("Could not resolve toplevel");
            }
        } else {
            relativeRepositoryPath = GitStatusConsumer.resolveURI(stdout.getOutput().trim(), fileSet.getBasedir().toURI());
        }
        // git-commit doesn't show single files, but only summary :/
        // so we must run git-status and consume the output
        // borrow a few things from the git-status command
        Commandline clStatus = GitStatusCommand.createCommandLine(repository, fileSet);
        GitStatusConsumer statusConsumer = new GitStatusConsumer(getLogger(), fileSet.getBasedir(), relativeRepositoryPath);
        exitCode = GitCommandLineUtils.execute(clStatus, statusConsumer, stderr, getLogger());
        if (exitCode != 0) {
            // git-status returns non-zero if nothing to do
            if (getLogger().isInfoEnabled()) {
                getLogger().info("nothing added to commit but untracked files present (use \"git add\" to " + "track)");
            }
        }
        if (statusConsumer.getChangedFiles().isEmpty()) {
            return new CheckInScmResult(null, statusConsumer.getChangedFiles());
        }
        Commandline clCommit = createCommitCommandLine(repository, fileSet, messageFile);
        exitCode = GitCommandLineUtils.execute(clCommit, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckInScmResult(clCommit.toString(), "The git-commit command failed.", stderr.getOutput(), false);
        }
        if (repo.isPushChanges()) {
            Commandline cl = createPushCommandLine(getLogger(), repository, fileSet, version);
            exitCode = GitCommandLineUtils.execute(cl, stdout, stderr, getLogger());
            if (exitCode != 0) {
                return new CheckInScmResult(cl.toString(), "The git-push command failed.", stderr.getOutput(), false);
            }
        }
        List<ScmFile> checkedInFiles = new ArrayList<ScmFile>(statusConsumer.getChangedFiles().size());
        // rewrite all detected files to now have status 'checked_in'
        for (ScmFile changedFile : statusConsumer.getChangedFiles()) {
            ScmFile scmfile = new ScmFile(changedFile.getPath(), ScmFileStatus.CHECKED_IN);
            if (fileSet.getFileList().isEmpty()) {
                checkedInFiles.add(scmfile);
            } else {
                // if a specific fileSet is given, we have to check if the file is really tracked
                for (File f : fileSet.getFileList()) {
                    if (FilenameUtils.separatorsToUnix(f.getPath()).equals(scmfile.getPath())) {
                        checkedInFiles.add(scmfile);
                    }
                }
            }
        }
        return new CheckInScmResult(clCommit.toString(), checkedInFiles);
    } finally {
        try {
            FileUtils.forceDelete(messageFile);
        } catch (IOException ex) {
        // ignore
        }
    }
}
Also used : GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CheckInScmResult(org.apache.maven.scm.command.checkin.CheckInScmResult) URI(java.net.URI) ScmFile(org.apache.maven.scm.ScmFile) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) GitStatusConsumer(org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File)

Example 75 with ScmFile

use of org.apache.maven.scm.ScmFile in project maven-scm by apache.

the class JGitUtils method addAllFiles.

/**
 * Adds all files in the given fileSet to the repository.
 *
 * @param git     the repo to add the files to
 * @param fileSet the set of files within the workspace, the files are added
 *                relative to the basedir of this fileset
 * @return a list of added files
 * @throws GitAPIException
 * @throws NoFilepatternException
 */
public static List<ScmFile> addAllFiles(Git git, ScmFileSet fileSet) throws GitAPIException, NoFilepatternException {
    URI baseUri = fileSet.getBasedir().toURI();
    AddCommand add = git.add();
    for (File file : fileSet.getFileList()) {
        if (!file.isAbsolute()) {
            file = new File(fileSet.getBasedir().getPath(), file.getPath());
        }
        if (file.exists()) {
            String path = relativize(baseUri, file);
            add.addFilepattern(path);
            add.addFilepattern(file.getAbsolutePath());
        }
    }
    add.call();
    Status status = git.status().call();
    Set<String> allInIndex = new HashSet<String>();
    allInIndex.addAll(status.getAdded());
    allInIndex.addAll(status.getChanged());
    // System.out.println("All in index: "+allInIndex.size());
    List<ScmFile> addedFiles = new ArrayList<ScmFile>(allInIndex.size());
    // rewrite all detected files to now have status 'checked_in'
    for (String entry : allInIndex) {
        ScmFile scmfile = new ScmFile(entry, ScmFileStatus.ADDED);
        // really tracked
        for (Iterator<File> itfl = fileSet.getFileList().iterator(); itfl.hasNext(); ) {
            String path = FilenameUtils.normalizeFilename(relativize(baseUri, itfl.next()));
            if (path.equals(FilenameUtils.normalizeFilename(scmfile.getPath()))) {
                addedFiles.add(scmfile);
            }
        }
    }
    return addedFiles;
}
Also used : Status(org.eclipse.jgit.api.Status) ScmFileStatus(org.apache.maven.scm.ScmFileStatus) ArrayList(java.util.ArrayList) URI(java.net.URI) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand) HashSet(java.util.HashSet) ScmFile(org.apache.maven.scm.ScmFile)

Aggregations

ScmFile (org.apache.maven.scm.ScmFile)198 File (java.io.File)102 ArrayList (java.util.ArrayList)51 ScmException (org.apache.maven.scm.ScmException)34 BufferedReader (java.io.BufferedReader)21 DefaultLog (org.apache.maven.scm.log.DefaultLog)20 ScmFileStatus (org.apache.maven.scm.ScmFileStatus)19 ScmFileSet (org.apache.maven.scm.ScmFileSet)17 InputStreamReader (java.io.InputStreamReader)16 ScmResult (org.apache.maven.scm.ScmResult)15 StatusScmResult (org.apache.maven.scm.command.status.StatusScmResult)15 IOException (java.io.IOException)14 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)13 Matcher (java.util.regex.Matcher)11 AddScmResult (org.apache.maven.scm.command.add.AddScmResult)11 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)10 UpdateScmResult (org.apache.maven.scm.command.update.UpdateScmResult)10 Commandline (org.codehaus.plexus.util.cli.Commandline)10 SynergyScmProviderRepository (org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository)9 FileInputStream (java.io.FileInputStream)8