Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 JFrog Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *  http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.jfrog.hudson.release.scm.git;
 
 import hudson.EnvVars;
 import  org.eclipse.jgit.transport.RemoteConfig;
 import  org.eclipse.jgit.transport.URIish;
 
 import java.io.File;
Interacts with Git repository for the various release operations.

Author(s):
Yossi Shaul
 
 public class GitManager extends AbstractScmManager<GitSCM> {
     private static Logger debuggingLogger = Logger.getLogger(GitManager.class.getName());
 
     public GitManager(AbstractBuild<?, ?> buildTaskListener buildListener) {
         super(buildbuildListener);
     }
 
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         return workspace
                 .act(new CurrentCommitCallable(gitSCMgetWorkingDirectory(gitSCMworkspace), ));
     }
 
     public String checkoutBranch(final String branchfinal boolean createthrows IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         FilePath workingDirectory = getWorkingDirectory(gitSCMworkspace);
         return workspace
                 .act(new CheckoutBranchCallable(createbranchgitSCMworkingDirectory));
     }
 
     public void commitWorkingCopy(final String commitMessagethrows IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         FilePath workingDirectory = getWorkingDirectory(gitSCMworkspace);
         workspace.act(new CommitWorkingCopyCallable(commitMessagegitSCM,
                 workingDirectory));
     }
 
     public void createTag(final String tagNamefinal String commitMessage)
             throws IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         FilePath workingDirectory = getWorkingDirectory(gitSCMworkspace);
         workspace.act(new CreateTagCallable(tagNamecommitMessagegitSCMworkingDirectory));
     }
 
     public String push(final String remoteRepositoryfinal String branchthrows IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         FilePath workingDirectory = getWorkingDirectory(gitSCMworkspace);
         return workspace
                 .act(new PushCallable(branchremoteRepositorygitSCMworkingDirectory));
     }
 
     public String pushTag(final String remoteRepositoryfinal String tagName)
             throws IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         GitSCM gitSCM = getJenkinsScm();
         FilePath directory = getWorkingDirectory(gitSCMworkspace);
         return workspace
                 .act(new PushTagCallable(tagNameremoteRepositorygitSCMdirectory));
     }
 
     public String pull(final String remoteRepositoryfinal String branchthrows IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        return workspace
                .act(new PullCallable(remoteRepositorybranchgetJenkinsScm(), ,
                        directory));
    }
    public void revertWorkingCopy(final String commitIshthrows IOExceptionInterruptedException {
        FilePath workspace = .getWorkspace();
        FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        workspace.act(new RevertCallable(commitIshgetJenkinsScm(), directory));
    }
    public String deleteLocalBranch(final String branchthrows IOExceptionInterruptedException {
        FilePath workspace = .getWorkspace();
        FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        return workspace.act(new DeleteLocalBranchCallable(branchgetJenkinsScm(), directory));
    }
    public String deleteRemoteBranch(final String remoteRepositoryfinal String branch)
            throws IOExceptionInterruptedException {
        FilePath workspace = .getWorkspace();
        FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        return workspace
                .act(new DeleteRemoteBranchCallable(branchremoteRepositorygetJenkinsScm(), ,
                        directory));
    }
    public String deleteLocalTag(final String tagthrows IOExceptionInterruptedException {
        FilePath workspace = .getWorkspace();
        FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        return workspace.act(new DeleteLocalTagCallable(taggetJenkinsScm(), ,
                directory));
    }
    public String deleteRemoteTag(final String remoteRepositoryfinal String tag)
            throws IOExceptionInterruptedException {
        FilePath workspace = .getWorkspace();
        FilePath directory = getWorkingDirectory(getJenkinsScm(), workspace);
        return workspace.act(
                new DeleteRemoteTagCallable(tagremoteRepositorygetJenkinsScm(), directory));
    }
    public String getRemoteUrl() {
        RemoteConfig remoteConfig = getJenkinsScm().getRepositories().get(0);
        URIish uri = remoteConfig.getURIs().get(0);
        return uri.toPrivateString();
    }
    private static GitAPI createGitAPI(EnvVars gitEnvironment,
                                       TaskListener buildListenerString gitExeFilePath workingCopyString confNameString confEmail)
            throws IOException {
        if (StringUtils.isNotBlank(confName)) {
            gitEnvironment.put("GIT_COMMITTER_NAME"confName);
            gitEnvironment.put("GIT_AUTHOR_NAME"confName);
        }
        if (StringUtils.isNotBlank(confEmail)) {
            gitEnvironment.put("GIT_COMMITTER_EMAIL"confEmail);
            gitEnvironment.put("GIT_AUTHOR_EMAIL"confEmail);
        }
        GitAPI git = new GitAPI(gitExeworkingCopybuildListenernew EnvVars());
        return git;
    }
    private static FilePath getWorkingDirectory(GitSCM gitSCMFilePath wsthrows IOException {
        // working directory might be relative to the workspace
        String relativeTargetDir = gitSCM.getRelativeTargetDir() == null ? "" : gitSCM.getRelativeTargetDir();
        return new FilePath(wsrelativeTargetDir);
    }
    private static class CurrentCommitCallable implements FilePath.FileCallable<String> {
        private final FilePath workingCopy;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final String confName;
        private final String confEmail;
        private CurrentCommitCallable(GitSCM gitSCMFilePath workingCopyAbstractBuild build,
                                      TaskListener listenerthrows IOExceptionInterruptedException {
            this. = workingCopy;
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                GitAPI git = createGitAPI();
                // commit all the modified files
                String baseCommit = git.launchCommand("rev-parse""--verify""HEAD").trim();
                .fine(String.format("Base commit hash%s"baseCommit));
                return baseCommit;
            } catch (GitException e) {
                throw new IOException("Failed retrieving current commit hash: " + e.getMessage());
            }
        }
    }
    private static class CheckoutBranchCallable implements FilePath.FileCallable<String> {
        private final boolean create;
        private final String branch;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public CheckoutBranchCallable(boolean createString branchGitSCM gitSCMTaskListener listener,
                                      AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = create;
            this. = branch;
            this. = listener;
            this. = workingCopy;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                // commit all the modified files
                ArgumentListBuilder args = new ArgumentListBuilder("checkout");
                if () {
                    args.add("-b"); // force create new branch
                }
                args.add();
                GitAPI git = createGitAPI();
                String checkoutResult = git.launchCommand(args);
                .fine(String.format("Checkout result: %s"checkoutResult));
                return checkoutResult;
            } catch (GitException e) {
                throw new IOException("Failed checkout branch: " + e.getMessage());
            }
        }
    }
    private static class CommitWorkingCopyCallable implements FilePath.FileCallable<String> {
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final String commitMessage;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public CommitWorkingCopyCallable(String commitMessageGitSCM gitSCMTaskListener listener,
                                         AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = commitMessage;
            this. = workingCopy;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                // commit all the modified files
                GitAPI git = createGitAPI();
                String commitOutput = git.launchCommand("commit""--all""-m");
                .fine(String.format("Reset command output:%n%s"commitOutput));
                return commitOutput;
            } catch (GitException e) {
                throw new IOException("Git working copy commit failed: " + e.getMessage());
            }
        }
    }
    private static class CreateTagCallable implements FilePath.FileCallable<String> {
        private final String tagName;
        private final String commitMessage;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        public CreateTagCallable(String tagNameString commitMessageGitSCM gitSCMTaskListener listener,
                                 AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = tagName;
            this. = commitMessage;
            this. = workingCopy;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                String escapedTagName = .replace(' ''_');
                log(, String.format("Creating tag '%s'"escapedTagName));
                GitAPI git = createGitAPI();
                if (git.tagExists(escapedTagName)) {
                    throw new IOException("Git tag '" + escapedTagName + "' already exists");
                }
                String tagOutput = git.launchCommand("tag""-a"escapedTagName"-m");
                .fine(String.format("Tag command output:%n%s"tagOutput));
                return tagOutput;
            } catch (GitException e) {
                throw new IOException("Git tag creation failed: " + e.getMessage());
            }
        }
    }
    private static class PushCallable implements FilePath.FileCallable<String> {
        private final String branch;
        private final String remoteRepository;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        public PushCallable(String branchString remoteRepositoryGitSCM gitSCMTaskListener listener,
                            AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = branch;
            this. = remoteRepository;
            this. = workingCopy;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log(, String.format("Pushing branch '%s' to '%s'"));
                GitAPI git = createGitAPI();
                String pushOutput = git.launchCommand("push""refs/heads/" + );
                .fine(String.format("Push command output:%n%s"pushOutput));
                return pushOutput;
            } catch (GitException e) {
                throw new IOException("Failed to push: " + e.getMessage());
            }
        }
    }
    private static class PushTagCallable implements FilePath.FileCallable<String> {
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final String tagName;
        private final String remoteRepository;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public PushTagCallable(String tagNameString remoteRepositoryGitSCM gitSCMTaskListener listener,
                               AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = tagName;
            this. = remoteRepository;
            this. = workingCopy;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                String escapedTagName = .replace(' ''_');
                log(, String.format("Pushing tag '%s' to '%s'"));
                GitAPI git = createGitAPI();
                String pushOutput = git.launchCommand("push""refs/tags/" + escapedTagName);
                .fine(String.format("Push tag command output:%n%s"pushOutput));
                return pushOutput;
            } catch (GitException e) {
                throw new IOException("Failed to push tag: " + e.getMessage());
            }
        }
    }
    private static class PullCallable implements FilePath.FileCallable<String> {
        private final String remoteRepository;
        private final String branch;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public PullCallable(String remoteRepositoryString branchGitSCM gitSCMTaskListener listener,
                            AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = remoteRepository;
            this. = branch;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log("Pulling changes");
                GitAPI git = createGitAPI();
                String pushOutput = git.launchCommand("pull");
                .fine(String.format("Pull command output:%n%s"pushOutput));
                return pushOutput;
            } catch (GitException e) {
                throw new IOException("Failed to pull: " + e.getMessage());
            }
        }
    }
    private static class RevertCallable implements FilePath.FileCallable<String> {
        private final String commitIsh;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public RevertCallable(String commitIshGitSCM gitSCMTaskListener listener,
                              AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = commitIsh;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log("Reverting git working copy back to initial commit: " + );
                GitAPI git = createGitAPI();
                String resetOutput = git.launchCommand("reset""--hard");
                .fine(String.format("Reset command output:%n%s"resetOutput));
                return resetOutput;
            } catch (GitException e) {
                throw new IOException("Failed to reset working copy: " + e.getMessage());
            }
        }
    }
    private static class DeleteLocalBranchCallable implements FilePath.FileCallable<String> {
        private final String branch;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public DeleteLocalBranchCallable(String branchGitSCM gitSCMTaskListener listener,
                                         AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = branch;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log("Deleting local git branch: " + );
                GitAPI git = createGitAPI();
                String output = git.launchCommand("branch""-D");
                .fine(String.format("Delete branch output:%n%s"output));
                return output;
            } catch (GitException e) {
                throw new IOException("Git branch deletion failed: " + e.getMessage());
            }
        }
    }
    private static class DeleteRemoteBranchCallable implements FilePath.FileCallable<String> {
        private final String branch;
        private final String remoteRepository;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public DeleteRemoteBranchCallable(String branchString remoteRepositoryGitSCM gitSCMTaskListener listener,
                                          AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = branch;
            this. = remoteRepository;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOException {
            try {
                log(, String.format("Deleting remote branch '%s' on '%s'"));
                GitAPI git = createGitAPI();
                String pushOutput = git.launchCommand("push"":refs/heads/" + );
                .fine(String.format("Push command output:%n%s"pushOutput));
                return pushOutput;
            } catch (GitException e) {
                throw new IOException("Failed to push: " + e.getMessage());
            }
        }
    }
    private static class DeleteLocalTagCallable implements FilePath.FileCallable<String> {
        private final String tag;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public DeleteLocalTagCallable(String tagGitSCM gitSCMTaskListener listener,
                                      AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = tag;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log("Deleting local tag: " + );
                GitAPI git = createGitAPI();
                String output = git.launchCommand("tag""-d");
                .fine(String.format("Delete tag output:%n%s"output));
                return output;
            } catch (GitException e) {
                throw new IOException("Git tag deletion failed: " + e.getMessage());
            }
        }
    }
    private static class DeleteRemoteTagCallable implements FilePath.FileCallable<String> {
        private final String tag;
        private final String remoteRepository;
        private final TaskListener listener;
        private final EnvVars envVars;
        private final String gitExe;
        private final FilePath workingCopy;
        private final String confName;
        private final String email;
        public DeleteRemoteTagCallable(String tagString remoteRepositoryGitSCM gitSCMTaskListener listener,
                                       AbstractBuild buildFilePath workingCopythrows IOExceptionInterruptedException {
            this. = tag;
            this. = remoteRepository;
            this. = workingCopy;
            this. = build.getEnvironment(listener);
            this. = gitSCM.getGitExe(build.getBuiltOn(), listener);
            this. = listener;
            this. = gitSCM.getGitConfigNameToUse();
            this. = gitSCM.getGitConfigEmailToUse();
        }
        public String invoke(File wsVirtualChannel channelthrows IOExceptionInterruptedException {
            try {
                log(, String.format("Deleting remote tag '%s' from '%s'"));
                GitAPI git = createGitAPI();
                String output = git.launchCommand("push"":refs/tags/" + );
                .fine(String.format("Delete tag output:%n%s"output));
                return output;
            } catch (GitException e) {
                throw new IOException("Git tag deletion failed: " + e.getMessage());
            }
        }
    }
New to GrepCode? Check out our FAQ X