Example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_URL

List of usage examples for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_URL

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_URL.

Prototype

String CONFIG_KEY_URL

To view the source code for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_URL.

Click Source Link

Document

The "url" key

Usage

From source file:com.puppetlabs.geppetto.forge.jenkins.RepositoryInfo.java

License:Open Source License

/**
 * Obtains the remote URL that is referenced by the given <code>branchName</code>
 *
 * @return The URL or <code>null</code> if it hasn't been configured
 *         for the given branch./*from   w  w  w .j  a va  2 s . c o m*/
 */
public static String getRemoteURL(Repository repository) throws IOException {
    StoredConfig repoConfig = repository.getConfig();
    String configuredRemote = repoConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
            repository.getBranch(), ConfigConstants.CONFIG_KEY_REMOTE);
    return configuredRemote == null ? null
            : repoConfig.getString(ConfigConstants.CONFIG_REMOTE_SECTION, configuredRemote,
                    ConfigConstants.CONFIG_KEY_URL);
}

From source file:com.tactfactory.harmony.utils.GitUtils.java

License:Open Source License

/**
 * Add a submodule to .gitmodules file./*from   w w  w.  j ava  2 s .c  om*/
 * @param repositoryPath Absolute path of the repository
 * @param submodulePath Absolute path of the submodule repository
 * @param submoduleUrl Url of the submodule
 * @throws IOException
 */
public static void addSubmodule(String repositoryPath, String submodulePath, String submoduleUrl)
        throws IOException {
    // Get main repository
    RepositoryBuilder repoBuilder = new RepositoryBuilder();
    Repository repository = repoBuilder.setWorkTree(new File(repositoryPath))
            .setGitDir(new File(repositoryPath + "/.git")).readEnvironment().findGitDir().build();

    // Get submodule relative path
    String path = TactFileUtils.absoluteToRelativePath(submodulePath, repositoryPath);
    path = path.substring(0, path.length() - 1);

    // Update .gitmodules file
    try {
        FileBasedConfig modulesConfig = new FileBasedConfig(
                new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES), repository.getFS());

        modulesConfig.load();

        modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_PATH,
                path);
        modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL,
                submoduleUrl);

        modulesConfig.save();
    } catch (ConfigInvalidException e) {
        ConsoleUtils.displayError(e);
    }

    repository.close();
}

From source file:edu.tum.cs.mylyn.provisioning.git.GitProvisioningUtil.java

License:Open Source License

public static URIish getCloneUri(Config config, String branch) {
    String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
            ConfigConstants.CONFIG_KEY_REMOTE);
    String uri = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote,
            ConfigConstants.CONFIG_KEY_URL);
    try {//from  ww w.  ja  v  a 2 s  .co  m
        return new URIish(uri);
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:net.polydawn.mdm.commands.MdmAddCommand.java

License:Open Source License

Config doSubmoduleConfig(Config gitmodulesCfg, File path) {
    // write gitmodule config for the new submodule
    String slashedPath = (File.separatorChar != '/') ? path.getPath().replace(File.separatorChar, '/')
            : path.getPath();/*from   w  w  w  .  j  av  a 2  s .c o m*/
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, slashedPath,
            ConfigConstants.CONFIG_KEY_PATH, slashedPath);
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, slashedPath,
            ConfigConstants.CONFIG_KEY_URL, url);
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, slashedPath,
            MdmConfigConstants.Module.MODULE_TYPE.toString(), MdmModuleType.DEPENDENCY.toString());
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, slashedPath,
            MdmConfigConstants.Module.DEPENDENCY_VERSION.toString(), version);
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, slashedPath,
            ConfigConstants.CONFIG_KEY_UPDATE, "none"); // since almost all git commands by default will pull down waaaay too much data if they operate naively on our dependencies, we tell them to ignore all dependencies by default.  And of course, commands like `git pull` just steamroll right ahead and ignore this anyway, so those require even more drastic counters.
    return gitmodulesCfg;
}

From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java

License:Open Source License

public void parse(Namespace args) {
    name = args.getString("name");
    path = args.getString("repo");

    // check if we're in a repo root.  we'll suggest slightly different default values if we are, as well as generate a gitlink in this repo to the new release repo.
    asSubmodule = isInRepoRoot();//from  w w  w.j a va2  s.c  o m

    // pick out the name, if not given.
    if (name == null) {
        String prompt = "what's the name of this project";
        String nameSuggest = new File(System.getProperty("user.dir")).getName();
        if (args.getBoolean("use_defaults"))
            name = nameSuggest;
        else if (asSubmodule)
            prompt += " [default: " + nameSuggest + "] ";
        else
            nameSuggest = null;

        while (name == null) {
            name = inputPrompt(os, prompt + "?");
            if (name.equals("") && nameSuggest != null)
                name = nameSuggest;
            name = name.trim();
            if (name.equals(""))
                name = null;
        }
    }

    // the rest of args parsing is only valid if we're making releases repo that's a submodule.
    if (!asSubmodule)
        return;

    // ask for remote url.
    remotePublicUrl = args.getString("remote_url");
    if (remotePublicUrl == null)
        if (args.getBoolean("use_defaults")) {
            String parentRemote = repo.getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
                    ConfigConstants.CONFIG_KEY_URL);
            if (parentRemote == null)
                parentRemote = System.getProperty("user.dir");
            remotePublicUrl = "../" + name + "-releases.git";
            try {
                remotePublicUrl = new URI(parentRemote + "/").resolve(remotePublicUrl).toString();
            } catch (URISyntaxException e) {
            }
        } else
            remotePublicUrl = inputPrompt(os, "Configure a remote url where this repo will be accessible?\n"
                    + "This will be committed to the project's .gitmodules file, and so should be a publicly accessible url.\n"
                    + "remote url: ");
    remotePublicUrl = remotePublicUrl.trim();

    // and another.
    remotePublishUrl = args.getString("remote_publish_url");
    if (remotePublishUrl == null)
        if (args.getBoolean("use_defaults"))
            remotePublishUrl = remotePublicUrl;
        else
            remotePublishUrl = inputPrompt(os,
                    "Configure a remote url you'll use to push this repo when making releases?\n"
                            + "This will not be committed to the project; just set in your local config.\n"
                            + "remote url [leave blank to use the same public url]: ");
    remotePublishUrl = remotePublishUrl.trim();
    if (remotePublishUrl.equals(""))
        remotePublishUrl = remotePublicUrl;
}

From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java

License:Open Source License

/**
 * Writes a new submodule block to the parentRepo's .gitmodules file declaring the
 * release repo, and initializes the local .git/config file to match.
 *
 * @param parentRepo/*from  ww w  .  j a  v  a  2  s. c  o  m*/
 * @throws IOException
 * @throws ConfigInvalidException
 */
void writeParentGitmoduleConfig(Repository parentRepo) throws IOException, ConfigInvalidException {
    // write gitmodule config for the new submodule
    StoredConfig gitmodulesCfg = new FileBasedConfig(
            new File(parentRepo.getWorkTree(), Constants.DOT_GIT_MODULES), parentRepo.getFS());
    gitmodulesCfg.load();
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_PATH,
            path);
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL,
            remotePublicUrl);
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
            MdmConfigConstants.Module.MODULE_TYPE.toString(), MdmModuleType.RELEASES.toString());
    gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_UPDATE,
            "none");
    gitmodulesCfg.save();

    // initialize local parent repo config for the submodule
    MdmModuleRelease module = MdmModuleRelease.load(parentRepo, path, gitmodulesCfg);
    Plumbing.initLocalConfig(parentRepo, module);
    parentRepo.getConfig().save();
}

From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java

License:Open Source License

/**
 * Write origin and fetch config into the release module. Only performed when
 * operating in submodule mode, since otherwise we don't have any requirement to
 * request a value for {@link #remotePublishUrl}.
 *
 * @param releaserepo//from  w  ww.  j ava 2  s. c om
 * @throws IOException
 */
void writeReleaseRepoConfig(Repository releaserepo) throws IOException {
    releaserepo.getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
            ConfigConstants.CONFIG_KEY_URL, remotePublishUrl);
    releaserepo.getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "fetch",
            "+refs/heads/*:refs/remotes/origin/*");
    releaserepo.getConfig().save();
}

From source file:net.polydawn.mdm.MdmModule.java

License:Open Source License

/**
 * @param repo/*from ww  w .ja  v  a 2s .  c o  m*/
 *                required.
 *
 *                well, unless you're about to create one, in which case not even.
 * @param handle
 *                required.
 * @param parent
 *                null if repository stands alone, otherwise if we are a submodule
 *                required.
 * @param gitmodulesCfg
 *                ignored if parent null, otherwise required.
 * @param indexId
 *                ignored if parent null, otherwise will be automatically loaded
 *                if not provided.
 *
 *                the commit hash known to the parent repo index for this
 *                submodule (or null if it's not handy; we'll load it in that
 *                case).
 *
 * @throws MdmModuleTypeException
 *                 if the {@code gitmodulesCfg} entries for {@code handle} don't
 *                 concur with the {@code type} expected.
 * @throws MdmRepositoryIOException
 *                 if disks reject our advances
 */
protected MdmModule(Repository repo, String handle, Repository parent, Config gitmodulesCfg, ObjectId indexId)
        throws MdmRepositoryIOException, MdmModuleTypeException {
    handle = (File.separatorChar != '/') ? handle.replace(File.separatorChar, '/') : handle;
    this.handle = handle;
    this.repo = repo;

    if (repo == null) {
        this.headId = null;
        this.dirtyFiles = false;
    } else {
        try {
            this.headId = repo.resolve(Constants.HEAD);
        } catch (IOException e) {
            throw new MdmRepositoryIOException(false, handle, e);
        }

        boolean dirtyFiles;
        try {
            dirtyFiles = !new Git(repo).status().call().isClean();
        } catch (NoWorkTreeException e) {
            throw new RuntimeException("wat", e);
        } catch (GitAPIException e) {
            dirtyFiles = false;
        }
        this.dirtyFiles = dirtyFiles;
    }

    if (parent != null) {
        // sanity check the expected module type if we're a submodule (if we're not a submodule, we can't make any such check since there's no gitmodules file to refer to).
        MdmModuleType type = getType();
        MdmModuleType type_configured = MdmModuleType
                .fromString(gitmodulesCfg.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, handle,
                        MdmConfigConstants.Module.MODULE_TYPE.toString()));
        if (type == null)
            throw new MdmModuleTypeException("expected module of type " + type + " for repository " + handle
                    + ", but gitmodules file has no known type for this module.");
        if (type != type_configured)
            throw new MdmModuleTypeException("expected module of type " + type + " for repository " + handle
                    + ", but gitmodules file states this is a " + type_configured + " module.");

        // load real path from gitmodule config (probably same as handle, but theoretically allowed to be different)
        String path = gitmodulesCfg.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, handle,
                ConfigConstants.CONFIG_KEY_PATH);
        this.path = (File.separatorChar != '/') ? path.replace(File.separatorChar, '/') : path;

        // load remote urls
        this.urlHistoric = gitmodulesCfg.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, handle,
                ConfigConstants.CONFIG_KEY_URL);
        this.urlLocal = parent.getConfig().getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, handle,
                ConfigConstants.CONFIG_KEY_URL);

        // load indexId the parent expects the submodule to be at (if not already provided)
        if (indexId != null)
            this.indexId = indexId;
        else
            try {
                SubmoduleWalk generator = SubmoduleWalk.forIndex(parent).setFilter(PathFilter.create(path));
                this.indexId = generator.next() ? generator.getObjectId() : null;
            } catch (IOException e) {
                throw new MdmRepositoryIOException(false, parent.getWorkTree().getPath(), e);
            }

        // review the submodule and summarize a status.
        SubmoduleStatusType statusType;
        if (path == null)
            // jgit report SubmoduleStatusType.MISSING if no path in .gitmodules file, but I don't even want to deal with that.
            throw new MdmModuleTypeException("no path for module " + handle + " listed in gitmodules file.");
        else if (urlLocal == null)
            // Report uninitialized if no URL in config file
            statusType = SubmoduleStatusType.UNINITIALIZED;
        else if (repo == null)
            // Report uninitialized if no submodule repository
            statusType = SubmoduleStatusType.UNINITIALIZED;
        else if (headId == null)
            // Report uninitialized if no HEAD commit in submodule repository
            statusType = SubmoduleStatusType.UNINITIALIZED;
        else if (!headId.equals(indexId))
            // Report checked out if HEAD commit is different than index commit
            statusType = SubmoduleStatusType.REV_CHECKED_OUT;
        else
            // Report initialized if HEAD commit is the same as the index commit
            statusType = SubmoduleStatusType.INITIALIZED;
        this.status = new SubmoduleStatus(statusType, path, indexId, headId);
    } else {
        this.path = handle;
        this.indexId = null;
        this.urlHistoric = null;
        this.urlLocal = null;
        this.status = null;
    }
}

From source file:net.polydawn.mdm.Plumbing.java

License:Open Source License

/**
 * Similar to calling `git submodule init [module]`.  Also updates the MdmModule cache of values.
 * @return true if repo.getConfig() has been modified and should be saved.
 *//*from   w w  w .j a  va 2  s .  c o m*/
public static boolean initLocalConfig(Repository repo, MdmModule module) {
    // Ignore entry if URL is already present in config file
    if (module.getUrlLocal() != null)
        return false;

    // Copy 'url' and 'update' fields to local repo config
    module.urlLocal = module.getUrlHistoric();
    repo.getConfig().setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, module.getHandle(),
            ConfigConstants.CONFIG_KEY_URL, module.getUrlLocal());
    repo.getConfig().setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, module.getHandle(),
            ConfigConstants.CONFIG_KEY_UPDATE, "none");
    return true;
}

From source file:net.polydawn.mdm.Plumbing.java

License:Open Source License

/**
 * Copy in `url` git config keys from the parent repo config into the submodule config.
 * This allows for easily having per-project 'insteadof' url rewrites which apply even
 * when mdm is doing the creation of new repos (which is otherwise a tad hard to get at with git submodules).
 * @return true if module.getRepo().getConfig() has been modified and should be saved.
 * @throws ConfigInvalidException//from  w  w w  . ja v a2  s  .  c  om
 * @throws IOException
 */
public static boolean initModuleConfig(Repository repo, MdmModule module)
        throws IOException, ConfigInvalidException {
    Config moduleConfig = module.getRepo().getConfig();
    // have to explicitly load the parent repo config in isolate, because `repo.getConfig` includes views of the system and user gitconfig, which we won't want to proxy here.
    FileBasedConfig parentConfig = new FileBasedConfig(new File(repo.getDirectory(), "config"), repo.getFS());
    try {
        parentConfig.load();
    } catch (IOException e) {
        throw new MdmRepositoryIOException(false, "the local git configuration file", e);
    }

    // copy any url_insteadof patterns from the parent repo's git config into the module's git config.
    // note that we do not strip out any additional insteadof's the module may have; if you've added those, it's none of our business (though at this point, we do overwrite).
    // see org.eclipse.jgit.transport.RemoteConfig for how these actually get used.
    for (String url : parentConfig.getSubsections(ConfigConstants.CONFIG_KEY_URL))
        for (String insteadOf : parentConfig.getStringList(ConfigConstants.CONFIG_KEY_URL, url, "insteadof"))
            moduleConfig.setString(ConfigConstants.CONFIG_KEY_URL, url, "insteadof", insteadOf);
    for (String url : parentConfig.getSubsections(ConfigConstants.CONFIG_KEY_URL))
        for (String insteadOf : parentConfig.getStringList(ConfigConstants.CONFIG_KEY_URL, url,
                "pushinsteadof"))
            moduleConfig.setString(ConfigConstants.CONFIG_KEY_URL, url, "pushinsteadof", insteadOf);
    return true;
}