Search in sources :

Example 1 with GitCommand

use of com.thoughtworks.go.domain.materials.git.GitCommand in project gocd by gocd.

the class GitMaterial method git.

private GitCommand git(ProcessOutputStreamConsumer outputStreamConsumer, final File workingFolder, int preferredCloneDepth, SubprocessExecutionContext executionContext) throws Exception {
    if (isSubmoduleFolder()) {
        return new GitCommand(getFingerprint(), new File(workingFolder.getPath()), GitMaterialConfig.DEFAULT_BRANCH, true, executionContext.getDefaultEnvironmentVariables(), secrets());
    }
    GitCommand gitCommand = new GitCommand(getFingerprint(), workingFolder, getBranch(), false, executionContext.getDefaultEnvironmentVariables(), secrets());
    if (!isGitRepository(workingFolder) || isRepositoryChanged(gitCommand, workingFolder)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Invalid git working copy or repository changed. Delete folder: " + workingFolder);
        }
        deleteDirectoryNoisily(workingFolder);
    }
    createParentFolderIfNotExist(workingFolder);
    if (!workingFolder.exists()) {
        TransactionSynchronizationManager txManager = new TransactionSynchronizationManager();
        if (txManager.isActualTransactionActive()) {
            txManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    if (status != TransactionSynchronization.STATUS_COMMITTED) {
                        FileUtils.deleteQuietly(workingFolder);
                    }
                }
            });
        }
        int cloneDepth = shallowClone ? preferredCloneDepth : Integer.MAX_VALUE;
        int returnValue;
        if (executionContext.isServer()) {
            returnValue = gitCommand.cloneWithNoCheckout(outputStreamConsumer, url.forCommandline());
        } else {
            returnValue = gitCommand.clone(outputStreamConsumer, url.forCommandline(), cloneDepth);
        }
        bombIfFailedToRunCommandLine(returnValue, "Failed to run git clone command");
    }
    return gitCommand;
}
Also used : TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) File(java.io.File) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) GitCommand(com.thoughtworks.go.domain.materials.git.GitCommand)

Example 2 with GitCommand

use of com.thoughtworks.go.domain.materials.git.GitCommand in project gocd by gocd.

the class GitMaterial method git.

private GitCommand git(ConsoleOutputStreamConsumer outputStreamConsumer, final File workingFolder, int preferredCloneDepth, SubprocessExecutionContext executionContext) throws Exception {
    if (isSubmoduleFolder()) {
        return new GitCommand(getFingerprint(), new File(workingFolder.getPath()), GitMaterialConfig.DEFAULT_BRANCH, true, secrets());
    }
    GitCommand gitCommand = new GitCommand(getFingerprint(), workingFolder, refSpecOrBranch, false, secrets());
    if (!isGitRepository(workingFolder) || isRepositoryChanged(gitCommand, workingFolder)) {
        LOG.debug("Invalid git working copy or repository changed. Delete folder: {}", workingFolder);
        deleteDirectoryNoisily(workingFolder);
    }
    createParentFolderIfNotExist(workingFolder);
    if (!workingFolder.exists()) {
        TransactionSynchronizationManager txManager = new TransactionSynchronizationManager();
        if (txManager.isActualTransactionActive()) {
            txManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    if (status != TransactionSynchronization.STATUS_COMMITTED) {
                        FileUtils.deleteQuietly(workingFolder);
                    }
                }
            });
        }
        int cloneDepth = shallowClone ? preferredCloneDepth : Integer.MAX_VALUE;
        int returnValue;
        if (executionContext.isServer()) {
            returnValue = gitCommand.cloneWithNoCheckout(outputStreamConsumer, urlForCommandLine());
        } else {
            returnValue = gitCommand.clone(outputStreamConsumer, urlForCommandLine(), cloneDepth);
        }
        bombIfFailedToRunCommandLine(returnValue, "Failed to run git clone command");
    }
    return gitCommand;
}
Also used : TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) File(java.io.File) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) GitCommand(com.thoughtworks.go.domain.materials.git.GitCommand)

Example 3 with GitCommand

use of com.thoughtworks.go.domain.materials.git.GitCommand in project gocd by gocd.

the class GitMaterial method updateTo.

public void updateTo(ProcessOutputStreamConsumer outputStreamConsumer, File baseDir, RevisionContext revisionContext, final SubprocessExecutionContext execCtx) {
    Revision revision = revisionContext.getLatestRevision();
    try {
        outputStreamConsumer.stdOutput(format("[%s] Start updating %s at revision %s from %s", GoConstants.PRODUCT_NAME, updatingTarget(), revision.getRevision(), url));
        File workingDir = execCtx.isServer() ? baseDir : workingdir(baseDir);
        GitCommand git = git(outputStreamConsumer, workingDir, revisionContext.numberOfModifications() + 1, execCtx);
        git.fetch(outputStreamConsumer);
        unshallowIfNeeded(git, outputStreamConsumer, revisionContext.getOldestRevision(), baseDir);
        git.resetWorkingDir(outputStreamConsumer, revision);
        outputStreamConsumer.stdOutput(format("[%s] Done.\n", GoConstants.PRODUCT_NAME));
    } catch (Exception e) {
        bomb(e);
    }
}
Also used : File(java.io.File) GitCommand(com.thoughtworks.go.domain.materials.git.GitCommand)

Example 4 with GitCommand

use of com.thoughtworks.go.domain.materials.git.GitCommand in project gocd by gocd.

the class GitMaterial method updateTo.

@Override
public void updateTo(ConsoleOutputStreamConsumer outputStreamConsumer, File baseDir, RevisionContext revisionContext, final SubprocessExecutionContext execCtx) {
    Revision revision = revisionContext.getLatestRevision();
    try {
        outputStreamConsumer.stdOutput(format("[%s] Start updating %s at revision %s from %s", GoConstants.PRODUCT_NAME, updatingTarget(), revision.getRevision(), getUriForDisplay()));
        File workingDir = execCtx.isServer() ? baseDir : workingdir(baseDir);
        GitCommand git = git(outputStreamConsumer, workingDir, revisionContext.numberOfModifications() + 1, execCtx);
        git.fetch(outputStreamConsumer);
        unshallowIfNeeded(git, outputStreamConsumer, revisionContext.getOldestRevision());
        git.resetWorkingDir(outputStreamConsumer, revision, shallowClone);
        outputStreamConsumer.stdOutput(format("[%s] Done.\n", GoConstants.PRODUCT_NAME));
    } catch (Exception e) {
        bomb(e);
    }
}
Also used : File(java.io.File) URISyntaxException(java.net.URISyntaxException) GitCommand(com.thoughtworks.go.domain.materials.git.GitCommand)

Aggregations

GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)4 File (java.io.File)4 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)2 URISyntaxException (java.net.URISyntaxException)1