Search in sources :

Example 11 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project gitblit by gitblit.

the class AddIndexedBranch method main.

public static void main(String... args) {
    Params params = new Params();
    CmdLineParser parser = new CmdLineParser(params);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException t) {
        System.err.println(t.getMessage());
        parser.printUsage(System.out);
        return;
    }
    // create a lowercase set of excluded repositories
    Set<String> exclusions = new TreeSet<String>();
    for (String exclude : params.exclusions) {
        exclusions.add(exclude.toLowerCase());
    }
    // determine available repositories
    File folder = new File(params.folder);
    List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
    int modCount = 0;
    int skipCount = 0;
    for (String repo : repoList) {
        boolean skip = false;
        for (String exclusion : exclusions) {
            if (StringUtils.fuzzyMatch(repo, exclusion)) {
                skip = true;
                break;
            }
        }
        if (skip) {
            System.out.println("skipping " + repo);
            skipCount++;
            continue;
        }
        try {
            // load repository config
            File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);
            Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
            StoredConfig config = repository.getConfig();
            config.load();
            Set<String> indexedBranches = new LinkedHashSet<String>();
            // add all local branches to index
            if (params.addAllLocalBranches) {
                List<RefModel> list = JGitUtils.getLocalBranches(repository, true, -1);
                for (RefModel refModel : list) {
                    System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", refModel.getName(), repo));
                    indexedBranches.add(refModel.getName());
                }
            } else {
                // add only one branch to index ('default' if not specified)
                System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));
                indexedBranches.add(params.branch);
            }
            String[] branches = config.getStringList("gitblit", null, "indexBranch");
            if (!ArrayUtils.isEmpty(branches)) {
                for (String branch : branches) {
                    indexedBranches.add(branch);
                }
            }
            config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));
            config.save();
            modCount++;
        } catch (Exception e) {
            System.err.println(repo);
            e.printStackTrace();
        }
    }
    System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RefModel(com.gitblit.models.RefModel) CmdLineParser(org.kohsuke.args4j.CmdLineParser) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) CmdLineException(org.kohsuke.args4j.CmdLineException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet) File(java.io.File) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 12 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project gitblit by gitblit.

the class JGitUtils method cloneRepository.

/**
 * Clone or Fetch a repository. If the local repository does not exist,
 * clone is called. If the repository does exist, fetch is called. By
 * default the clone/fetch retrieves the remote heads, tags, and notes.
 *
 * @param repositoriesFolder
 * @param name
 * @param fromUrl
 * @param bare
 * @param credentialsProvider
 * @return CloneResult
 * @throws Exception
 */
public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl, boolean bare, CredentialsProvider credentialsProvider) throws Exception {
    CloneResult result = new CloneResult();
    if (bare) {
        // bare repository, ensure .git suffix
        if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name += Constants.DOT_GIT_EXT;
        }
    } else {
        // normal repository, strip .git suffix
        if (name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
        }
    }
    result.name = name;
    File folder = new File(repositoriesFolder, name);
    if (folder.exists()) {
        File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
        Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    } else {
        CloneCommand clone = new CloneCommand();
        clone.setBare(bare);
        clone.setCloneAllBranches(true);
        clone.setURI(fromUrl);
        clone.setDirectory(folder);
        if (credentialsProvider != null) {
            clone.setCredentialsProvider(credentialsProvider);
        }
        Repository repository = clone.call().getRepository();
        // Now we have to fetch because CloneCommand doesn't fetch
        // refs/notes nor does it allow manual RefSpec.
        result.createdRepository = true;
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    }
    return result;
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 13 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project Aeron by real-logic.

the class TutorialPublishTask method getWikiUri.

private String getWikiUri() throws IOException, URISyntaxException {
    final File baseGitDir = new File(getProject().getRootDir(), ".git");
    if (!baseGitDir.exists() || !baseGitDir.isDirectory()) {
        throw new IllegalStateException("unable to find valid git repository at: " + baseGitDir);
    }
    final Repository baseGitRepo = new FileRepositoryBuilder().setGitDir(new File(getProject().getRootDir(), ".git")).build();
    final String origin = baseGitRepo.getConfig().getString(CONFIG_REMOTE_SECTION, requireNonNull(remoteName, "'remoteName' must be set, use origin as a default"), CONFIG_KEY_URL);
    if (null == origin) {
        throw new IllegalStateException("unable to find origin URI");
    }
    return GithubUtil.getWikiUriFromOriginUri(origin);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 14 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project camel by apache.

the class GitProducer method getLocalRepository.

private Repository getLocalRepository() throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = null;
    try {
        repo = // scan
        builder.setGitDir(new File(endpoint.getLocalPath(), ".git")).readEnvironment().findGitDir().build();
    } catch (IOException e) {
        LOG.error("There was an error, cannot open " + endpoint.getLocalPath() + " repository");
        throw e;
    }
    return repo;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File)

Example 15 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project eclipse.platform.releng by eclipse.

the class LocalDiskRepositoryTest method createRepository.

/**
 * Creates a new empty repository.
 *
 * @param bare
 *            true to create a bare repository; false to make a repository
 *            within its working directory
 * @return the newly created repository, opened for access
 * @throws IOException
 *             the repository could not be created in the temporary area
 */
private Repository createRepository(boolean bare) throws IOException {
    File gitdir = createUniqueTestGitDir(bare);
    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
    repositoryBuilder.setMustExist(true);
    repositoryBuilder.setGitDir(gitdir);
    Repository db = repositoryBuilder.build();
    Assert.assertFalse(gitdir.exists());
    db.create();
    toClose.add(db);
    return db;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Aggregations

FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)32 Repository (org.eclipse.jgit.lib.Repository)30 File (java.io.File)25 Git (org.eclipse.jgit.api.Git)12 IOException (java.io.IOException)8 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)6 ObjectId (org.eclipse.jgit.lib.ObjectId)4 Path (java.nio.file.Path)3 CloneCommand (org.eclipse.jgit.api.CloneCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 Ref (org.eclipse.jgit.lib.Ref)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)3 URIish (org.eclipse.jgit.transport.URIish)3 RefLogEntry (com.gitblit.models.RefLogEntry)2 Reader (java.io.Reader)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 TaskAction (org.gradle.api.tasks.TaskAction)2 Test (org.junit.Test)2 GHRepository (org.kohsuke.github.GHRepository)2