Search in sources :

Example 51 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project fabric8 by jboss-fuse.

the class FabricPatchServiceImpl method synchronize.

@Override
public String synchronize(final boolean verbose) throws Exception {
    final String[] remoteUrl = new String[] { null };
    patchManagement.pushPatchInfo(verbose);
    GitOperation operation = new GitOperation() {

        @Override
        public Object call(Git git, GitContext context) throws Exception {
            ProfileRegistry registry = fabricService.adapt(ProfileRegistry.class);
            Map<String, String> properties = registry.getDataStoreProperties();
            String username;
            String password;
            if (properties != null && properties.containsKey("gitRemoteUser") && properties.containsKey("gitRemotePassword")) {
                username = properties.get("gitRemoteUser");
                password = properties.get("gitRemotePassword");
            } else {
                username = ZooKeeperUtils.getContainerLogin(runtimeProperties);
                password = ZooKeeperUtils.generateContainerToken(runtimeProperties, curator);
            }
            remoteUrl[0] = git.getRepository().getConfig().getString("remote", "origin", "url");
            Iterable<PushResult> results = git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)).setPushTags().setPushAll().call();
            logPushResult(results, git.getRepository(), verbose);
            return null;
        }
    };
    try {
        gitDataStore.gitOperation(new GitContext(), operation, null);
    } catch (FabricException e) {
        if (e.getCause() != null && e.getCause() instanceof TransportException) {
            LOG.warn("Problem when synchronizing patch information: " + e.getCause().getMessage());
        } else {
            throw e;
        }
    }
    return remoteUrl[0];
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GitOperation(io.fabric8.git.internal.GitOperation) Git(org.eclipse.jgit.api.Git) GitContext(io.fabric8.api.GitContext) FabricException(io.fabric8.api.FabricException) ProfileRegistry(io.fabric8.api.ProfileRegistry) PushResult(org.eclipse.jgit.transport.PushResult) TransportException(org.eclipse.jgit.api.errors.TransportException)

Example 52 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project Corgi by kevinYin.

the class RepoUtil method getGitAllBranchInfo.

/**
 * 获取git 分支的所有信息
 *
 * @param gitRemoteURL git 地址
 * @param userName     git账户名
 * @param password     git 密码
 * @return key是分支名,value是 版本号 (前8位)
 */
public static Map<String, String> getGitAllBranchInfo(String gitRemoteURL, String userName, String password) throws GitAPIException {
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider(userName, password);
    Collection<Ref> refs = Git.lsRemoteRepository().setHeads(true).setCredentialsProvider(cp).setRemote(gitRemoteURL).setTags(true).call();
    HashMap<String, String> branch2VersionMap = Maps.newHashMap();
    for (Ref ref : refs) {
        String versionNo = ref.getObjectId().getName().substring(0, 8);
        if (ref.getName().startsWith("refs/heads/master")) {
            branch2VersionMap.put("master", versionNo);
            continue;
        }
        if (ref.getName().startsWith("refs/heads/")) {
            String branchName = ref.getName().replaceFirst("refs/heads/", "/branches/");
            branch2VersionMap.put(branchName, versionNo);
            continue;
        }
        if (ref.getName().startsWith("refs/tags/")) {
            String branchName = ref.getName().replaceFirst("refs/tags/", "/tags/");
            branch2VersionMap.put(branchName, versionNo);
        }
    }
    return branch2VersionMap;
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Ref(org.eclipse.jgit.lib.Ref) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider)

Example 53 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project archi-modelrepository-plugin by archi-contribs.

the class LoadModelFromRepositoryProvider method cloneModel.

private void cloneModel(String url, File cloneFolder, String userName, String password) throws GitAPIException, IOException {
    FileUtils.deleteFolder(cloneFolder);
    // Make dir
    cloneFolder.mkdirs();
    CloneCommand cloneCommand = Git.cloneRepository();
    cloneCommand.setDirectory(cloneFolder);
    cloneCommand.setURI(url);
    cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password));
    try (Git git = cloneCommand.call()) {
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git)

Example 54 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project archi-modelrepository-plugin by archi-contribs.

the class ArchiRepository method pullFromRemote.

@Override
public PullResult pullFromRemote(String userName, String userPassword, ProgressMonitor monitor) throws IOException, GitAPIException {
    try (Git git = Git.open(getLocalRepositoryFolder())) {
        PullCommand pullCommand = git.pull();
        pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, userPassword));
        // Merge, not rebase
        pullCommand.setRebase(false);
        pullCommand.setProgressMonitor(monitor);
        return pullCommand.call();
    }
}
Also used : PullCommand(org.eclipse.jgit.api.PullCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git)

Example 55 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project archi-modelrepository-plugin by archi-contribs.

the class ArchiRepository method pushToRemote.

@Override
public Iterable<PushResult> pushToRemote(String userName, String userPassword, ProgressMonitor monitor) throws IOException, GitAPIException {
    try (Git git = Git.open(getLocalRepositoryFolder())) {
        PushCommand pushCommand = git.push();
        pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, userPassword));
        pushCommand.setProgressMonitor(monitor);
        return pushCommand.call();
    }
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) PushCommand(org.eclipse.jgit.api.PushCommand)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)75 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)33 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)18 Test (org.junit.Test)18 CloneCommand (org.eclipse.jgit.api.CloneCommand)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)15 RepositoryModel (com.gitblit.models.RepositoryModel)12 PushResult (org.eclipse.jgit.transport.PushResult)12 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)9 FileOutputStream (java.io.FileOutputStream)8 PushCommand (org.eclipse.jgit.api.PushCommand)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 Date (java.util.Date)7 Ref (org.eclipse.jgit.lib.Ref)7 Repository (org.eclipse.jgit.lib.Repository)7 RefSpec (org.eclipse.jgit.transport.RefSpec)7