Java Code Examples for org.jenkinsci.plugins.gitclient.GitClient#checkoutBranch()

The following examples show how to use org.jenkinsci.plugins.gitclient.GitClient#checkoutBranch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitCloneReadSaveRequest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
byte[] read() throws IOException {
    try {
        GitClient git = cloneRepo();
        try {
            // thank you test for how to use something...
            // https://github.com/jenkinsci/git-client-plugin/blob/master/src/test/java/org/jenkinsci/plugins/gitclient/GitClientTest.java#L1108
            git.checkoutBranch(branch, "origin/" + branch);
        } catch(Exception e) {
            throw new RuntimeException("Branch not found: " + branch);
        }
        File f = new File(repositoryPath, filePath);
        if (f.canRead()) {
            return FileUtils.readFileToByteArray(f);
        }
        return null;
    } catch (InterruptedException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to read " + filePath, ex);
    } finally {
        cleanupRepo();
    }
}
 
Example 2
Source File: GitCloneReadSaveRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
void save() throws IOException {
    try {
        GitClient git = cloneRepo();
        try {
            git.checkoutBranch(sourceBranch, "origin/" + sourceBranch);
        } catch(Exception e) {
            throw new RuntimeException("Branch not found: " + sourceBranch);
        }
        if (!sourceBranch.equals(branch)) {
            //git.branch(branch);
            git.checkoutBranch(branch, "origin/" + sourceBranch);
        }
        File f = new File(repositoryPath, filePath);
        // commit will fail if the contents hasn't changed
        if (!f.exists() || !Arrays.equals(FileUtils.readFileToByteArray(f), contents)) {
            FileUtils.writeByteArrayToFile(f, contents);
            git.add(filePath);
            git.commit(commitMessage);
        }
        git.push().ref(branch).to(new URIish(gitSource.getRemote())).execute();
    } catch (InterruptedException | GitException | URISyntaxException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to save " + filePath, ex);
    } finally {
        cleanupRepo();
    }
}