use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.
the class GitCloneReadSaveRequest method save.
@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();
}
}
use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.
the class GitCloneReadSaveRequest method cloneRepo.
GitClient cloneRepo() throws InterruptedException, IOException {
EnvVars environment = new EnvVars();
TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
String gitExe = gitTool.getGitExe();
GitClient git = Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
git.addCredentials(gitSource.getRemote(), getCredential());
try {
git.clone(gitSource.getRemote(), "origin", true, null);
log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
} catch (GitException e) {
// check if this is an empty repository
boolean isEmptyRepo = false;
try {
if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
isEmptyRepo = true;
}
} catch (GitException ge) {
// *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
if ("unexpected ls-remote output ".equals(ge.getMessage())) {
// blank line, command succeeded
isEmptyRepo = true;
}
// ignore other reasons
}
if (isEmptyRepo) {
git.init();
git.addRemoteUrl("origin", gitSource.getRemote());
log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
} else {
throw e;
}
}
return git;
}
use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.
the class GitBareRepoReadSaveRequest method cloneRepo.
GitClient cloneRepo() throws InterruptedException, IOException {
EnvVars environment = new EnvVars();
TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
String gitExe = gitTool.getGitExe();
GitClient git = org.jenkinsci.plugins.gitclient.Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
git.addCredentials(gitSource.getRemote(), getCredential());
try {
git.clone(gitSource.getRemote(), "origin", true, null);
log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
} catch (GitException e) {
// check if this is an empty repository
boolean isEmptyRepo = false;
try {
if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
isEmptyRepo = true;
}
} catch (GitException ge) {
// *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
if ("unexpected ls-remote output ".equals(ge.getMessage())) {
// blank line, command succeeded
isEmptyRepo = true;
}
// ignore other reasons
}
if (isEmptyRepo) {
git.init();
git.addRemoteUrl("origin", gitSource.getRemote());
log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
} else {
throw e;
}
}
return git;
}
use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.
the class GitCacheCloneReadSaveRequest method invokeOnScm.
<T> T invokeOnScm(final GitSCMFileSystem.FSFunction<T> function) throws IOException {
try {
GitSCMFileSystem fs = getFilesystem();
if (fs == null) {
// Fall back to a git clone if we can't get the repository filesystem
GitCloneReadSaveRequest gitClone = new GitCloneReadSaveRequest(gitSource, branch, commitMessage, sourceBranch, filePath, contents);
GitClient git = gitClone.cloneRepo();
try {
return git.withRepository(new RepositoryCallbackToFSFunctionAdapter<>(function));
} finally {
gitClone.cleanupRepo();
}
}
return fs.invoke(function);
} catch (InterruptedException ex) {
throw new ServiceException.UnexpectedErrorException("Unable to save " + filePath, ex);
}
}
use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.
the class GitCloneReadSaveRequest method read.
@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();
}
}
Aggregations