Example usage for org.eclipse.jgit.lib Repository getConfig

List of usage examples for org.eclipse.jgit.lib Repository getConfig

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getConfig.

Prototype

@NonNull
public abstract StoredConfig getConfig();

Source Link

Document

Get the configuration of this repository.

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Automatic repair of (some) invalid refspecs.  These are the result of a
 * bug in JGit cloning where a double forward-slash was injected.  :(
 *
 * @param repository//  ww  w. j  a v a2  s  . co  m
 * @return true, if the refspecs were repaired
 */
public static boolean repairFetchSpecs(Repository repository) {
    StoredConfig rc = repository.getConfig();

    // auto-repair broken fetch ref specs
    for (String name : rc.getSubsections("remote")) {
        int invalidSpecs = 0;
        int repairedSpecs = 0;
        List<String> specs = new ArrayList<String>();
        for (String spec : rc.getStringList("remote", name, "fetch")) {
            try {
                RefSpec rs = new RefSpec(spec);
                // valid spec
                specs.add(spec);
            } catch (IllegalArgumentException e) {
                // invalid spec
                invalidSpecs++;
                if (spec.contains("//")) {
                    // auto-repair this known spec bug
                    spec = spec.replace("//", "/");
                    specs.add(spec);
                    repairedSpecs++;
                }
            }
        }

        if (invalidSpecs == repairedSpecs && repairedSpecs > 0) {
            // the fetch specs were automatically repaired
            rc.setStringList("remote", name, "fetch", specs);
            try {
                rc.save();
                rc.load();
                LOGGER.debug("repaired {} invalid fetch refspecs for {}", repairedSpecs,
                        repository.getDirectory());
                return true;
            } catch (Exception e) {
                LOGGER.error(null, e);
            }
        } else if (invalidSpecs > 0) {
            LOGGER.error("mirror executor found {} invalid fetch refspecs for {}", invalidSpecs,
                    repository.getDirectory());
        }
    }
    return false;
}

From source file:com.google.gdt.eclipse.gph.egit.wizard.CloneRepositoryWizardPage.java

License:Open Source License

private File getLocalDirForRepo(String repoURL) {
    RepositoryUtil repoUtil = Activator.getDefault().getRepositoryUtil();

    for (String configuredRepo : repoUtil.getConfiguredRepositories()) {
        try {/*from  w  ww.  j a v a2s  .  c  o m*/
            File repoFile = new File(configuredRepo);

            Repository repository = Activator.getDefault().getRepositoryCache().lookupRepository(repoFile);

            try {
                RemoteConfig originConfig = new RemoteConfig(repository.getConfig(), "origin");

                for (URIish uri : originConfig.getURIs()) {
                    String uriStr = uri.toString();

                    if (repoURL.equals(uriStr)) {
                        return repoFile.getParentFile();
                    }
                }
            } catch (URISyntaxException exception) {

            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    return null;
}

From source file:com.google.gerrit.server.change.CreateMergePatchSet.java

License:Apache License

private RevCommit createMergeCommit(MergePatchSetInput in, ProjectControl projectControl, Branch.NameKey dest,
        Repository git, ObjectInserter oi, RevWalk rw, RevCommit currentPsCommit, RevCommit sourceCommit,
        PersonIdent author, ObjectId changeId)
        throws ResourceNotFoundException, MergeIdenticalTreeException, MergeConflictException, IOException {

    ObjectId parentCommit;/*from  w  w w  .  java 2 s. c  o  m*/
    if (in.inheritParent) {
        // inherit first parent from previous patch set
        parentCommit = currentPsCommit.getParent(0);
    } else {
        // get the current branch tip of destination branch
        Ref destRef = git.getRefDatabase().exactRef(dest.get());
        if (destRef != null) {
            parentCommit = destRef.getObjectId();
        } else {
            throw new ResourceNotFoundException("cannot find destination branch");
        }
    }
    RevCommit mergeTip = rw.parseCommit(parentCommit);

    String commitMsg;
    if (Strings.emptyToNull(in.subject) != null) {
        commitMsg = ChangeIdUtil.insertId(in.subject, changeId);
    } else {
        // reuse previous patch set commit message
        commitMsg = currentPsCommit.getFullMessage();
    }

    String mergeStrategy = MoreObjects.firstNonNull(Strings.emptyToNull(in.merge.strategy),
            mergeUtilFactory.create(projectControl.getProjectState()).mergeStrategyName());

    return MergeUtil.createMergeCommit(oi, git.getConfig(), mergeTip, sourceCommit, mergeStrategy, author,
            commitMsg, rw);
}

From source file:com.google.gerrit.server.git.GarbageCollection.java

License:Apache License

private static void logGcConfiguration(Project.NameKey projectName, Repository repo, boolean aggressive) {
    StringBuilder b = new StringBuilder();
    Config cfg = repo.getConfig();
    b.append("gc.aggressive=").append(aggressive).append("; ");
    b.append(formatConfigValues(cfg, ConfigConstants.CONFIG_GC_SECTION, null));
    for (String subsection : cfg.getSubsections(ConfigConstants.CONFIG_GC_SECTION)) {
        b.append(formatConfigValues(cfg, ConfigConstants.CONFIG_GC_SECTION, subsection));
    }/*  w w  w  .  j  av  a 2s  . co m*/
    if (b.length() == 0) {
        b.append("no set");
    }

    logGcInfo(projectName, "gc config: " + b.toString());
    logGcInfo(projectName, "pack config: " + (new PackConfig(repo)).toString());
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private Repository createRepository(Path path, Project.NameKey name)
        throws RepositoryNotFoundException, RepositoryCaseMismatchException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }//from w  w w  .j a  va  2s.c  o m

    File dir = FileKey.resolve(path.resolve(name.get()).toFile(), FS.DETECTED);
    FileKey loc;
    if (dir != null) {
        // Already exists on disk, use the repository we found.
        //
        loc = FileKey.exact(dir, FS.DETECTED);

        if (!names.contains(name)) {
            throw new RepositoryCaseMismatchException(name);
        }
    } else {
        // It doesn't exist under any of the standard permutations
        // of the repository name, so prefer the standard bare name.
        //
        String n = name.get() + Constants.DOT_GIT_EXT;
        loc = FileKey.exact(path.resolve(n).toFile(), FS.DETECTED);
    }

    try {
        Repository db = RepositoryCache.open(loc, false);
        db.create(true /* bare */);

        StoredConfig config = db.getConfig();
        config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
                ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, true);
        config.save();

        // JGit only writes to the reflog for refs/meta/config if the log file
        // already exists.
        //
        File metaConfigLog = new File(db.getDirectory(), "logs/" + RefNames.REFS_CONFIG);
        if (!metaConfigLog.getParentFile().mkdirs() || !metaConfigLog.createNewFile()) {
            log.error(String.format("Failed to create ref log for %s in repository %s", RefNames.REFS_CONFIG,
                    name));
        }

        onCreateProject(name);

        return db;
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot create repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}

From source file:com.google.gitiles.DefaultAccess.java

License:Open Source License

private String loadDescriptionText(Repository repo) throws IOException {
    String desc = null;/*from w w w .ja  va  2s.  c  o m*/
    StoredConfig config = repo.getConfig();
    IOException configError = null;
    try {
        config.load();
        desc = config.getString("gitweb", null, "description");
    } catch (ConfigInvalidException e) {
        configError = new IOException(e);
    }
    if (desc == null) {
        File descFile = new File(repo.getDirectory(), "description");
        if (descFile.exists()) {
            desc = new String(IO.readFully(descFile));
        } else if (configError != null) {
            throw configError;
        }
    }
    return desc;
}

From source file:com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitMerger.java

License:Eclipse Distribution License

/**
 * @param local//from w w  w  . j  a v a  2  s .c o m
 * @param inCore
 */
protected UnleashGitMerger(Repository local, boolean inCore, MergeClient mergeClient) {
    super(local);
    this.mergeClient = mergeClient;
    SupportedAlgorithm diffAlg = local.getConfig().getEnum(ConfigConstants.CONFIG_DIFF_SECTION, null,
            ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM);
    this.mergeAlgorithm = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg));
    this.commitNames = new String[] { "BASE", "OURS", "THEIRS" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    this.inCore = inCore;

    if (inCore) {
        this.implicitDirCache = false;
        this.dircache = DirCache.newInCore();
    } else {
        this.implicitDirCache = true;
    }
}

From source file:com.madgag.agit.git.Repos.java

License:Open Source License

public static RemoteConfig remoteConfigFor(Repository repository, String remoteName) {
    try {// w  w  w. j  a v  a 2 s.c o  m
        return new RemoteConfig(repository.getConfig(), remoteName);
    } catch (Exception e) {
        Log.e(TAG, "Couldn't parse config", e);
        throw new RuntimeException(e);
    }
}

From source file:com.microsoft.gittf.core.config.ChangesetCommitMap.java

License:Open Source License

/**
 * Used for upgrade, copoes the entries from repository config to the new
 * config location/* www .j a v a2s  . c  o m*/
 * 
 * @param newConfigLocation
 * @param repository
 */
public static void copyConfigurationEntriesFromRepositoryConfigToNewConfig(Repository repository,
        File newConfigLocation) {
    if (!newConfigLocation.exists()) {
        FileBasedConfig configFile = new FileBasedConfig(newConfigLocation, FS.DETECTED);

        GitTFConfiguration repositoryConfiguration = GitTFConfiguration.loadFrom(repository);
        if (repositoryConfiguration != null) {
            copyConfigurationEntries(repository.getConfig(), configFile);
        }
    }
}

From source file:com.microsoft.gittf.core.config.GitTFConfiguration.java

License:Open Source License

public static String getUsername(final Repository repository) {
    /*/*from   w  w w. j  a  v  a  2s  .  co  m*/
     * The repository could be not initialized yet, in this case we'll get
     * the value (if any) from a global config file
     */
    return repository.getConfig().getString(ConfigurationConstants.CONFIGURATION_SECTION,
            ConfigurationConstants.SERVER_SUBSECTION, ConfigurationConstants.USERNAME);

}