Search in sources :

Example 11 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project gitblit by gitblit.

the class ITicketService method updateMilestone.

/**
	 * Updates a milestone.
	 *
	 * @param repository
	 * @param milestone
	 * @param createdBy
	 * @return true if successful
	 * @since 1.4.0
	 */
public synchronized boolean updateMilestone(RepositoryModel repository, TicketMilestone milestone, String createdBy) {
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        StoredConfig config = db.getConfig();
        config.setString(MILESTONE, milestone.name, STATUS, milestone.status.name());
        config.setString(MILESTONE, milestone.name, COLOR, milestone.color);
        if (milestone.due != null) {
            config.setString(MILESTONE, milestone.name, DUE, new SimpleDateFormat(DUE_DATE_PATTERN).format(milestone.due));
        }
        config.save();
        milestonesCache.remove(repository.name);
        return true;
    } catch (IOException e) {
        log.error("failed to update milestone " + milestone + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project gitblit by gitblit.

the class ITicketService method renameLabel.

/**
	 * Renames a label.
	 *
	 * @param repository
	 * @param oldName
	 * @param newName
	 * @param createdBy
	 * @return true if the rename was successful
	 * @since 1.4.0
	 */
public synchronized boolean renameLabel(RepositoryModel repository, String oldName, String newName, String createdBy) {
    if (StringUtils.isEmpty(newName)) {
        throw new IllegalArgumentException("new label can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        TicketLabel label = getLabel(repository, oldName);
        StoredConfig config = db.getConfig();
        config.unsetSection(LABEL, oldName);
        config.setString(LABEL, newName, COLOR, label.color);
        config.save();
        for (QueryResult qr : label.tickets) {
            Change change = new Change(createdBy);
            change.unlabel(oldName);
            change.label(newName);
            updateTicket(repository, qr.number, change);
        }
        return true;
    } catch (IOException e) {
        log.error("failed to rename label " + oldName + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException)

Example 13 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project gitblit by gitblit.

the class ITicketService method getMilestones.

/**
	 * Returns the list of milestones for the repository.
	 *
	 * @param repository
	 * @return the list of milestones
	 * @since 1.4.0
	 */
public List<TicketMilestone> getMilestones(RepositoryModel repository) {
    String key = repository.name;
    if (milestonesCache.containsKey(key)) {
        return milestonesCache.get(key);
    }
    List<TicketMilestone> list = new ArrayList<TicketMilestone>();
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        StoredConfig config = db.getConfig();
        Set<String> names = config.getSubsections(MILESTONE);
        for (String name : names) {
            TicketMilestone milestone = new TicketMilestone(name);
            milestone.status = Status.fromObject(config.getString(MILESTONE, name, STATUS), milestone.status);
            milestone.color = config.getString(MILESTONE, name, COLOR);
            String due = config.getString(MILESTONE, name, DUE);
            if (!StringUtils.isEmpty(due)) {
                try {
                    milestone.due = new SimpleDateFormat(DUE_DATE_PATTERN).parse(due);
                } catch (ParseException e) {
                    log.error("failed to parse {} milestone {} due date \"{}\"", new Object[] { repository, name, due });
                }
            }
            list.add(milestone);
        }
        milestonesCache.put(key, Collections.unmodifiableList(list));
    } catch (Exception e) {
        log.error("invalid tickets settings for " + repository, e);
    } finally {
        db.close();
    }
    return list;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 14 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project gitblit by gitblit.

the class JGitUtils method repairFetchSpecs.

/**
	 * 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
	 * @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;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) RefSpec(org.eclipse.jgit.transport.RefSpec) ArrayList(java.util.ArrayList) RevisionSyntaxException(org.eclipse.jgit.errors.RevisionSyntaxException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) GitBlitException(com.gitblit.GitBlitException) AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) IOException(java.io.IOException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) StopWalkException(org.eclipse.jgit.errors.StopWalkException)

Example 15 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.

the class JGitConnection method remoteList.

@Override
public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
    if (remoteName != null && remoteNames.contains(remoteName)) {
        remoteNames.clear();
        remoteNames.add(remoteName);
    }
    List<Remote> result = new ArrayList<>(remoteNames.size());
    for (String remote : remoteNames) {
        try {
            List<URIish> uris = new RemoteConfig(config, remote).getURIs();
            result.add(newDto(Remote.class).withName(remote).withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
        } catch (URISyntaxException exception) {
            throw new GitException(exception.getMessage(), exception);
        }
    }
    return result;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) GitException(org.eclipse.che.api.git.exception.GitException) ArrayList(java.util.ArrayList) Remote(org.eclipse.che.api.git.shared.Remote) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) HashSet(java.util.HashSet)

Aggregations

StoredConfig (org.eclipse.jgit.lib.StoredConfig)40 IOException (java.io.IOException)26 Repository (org.eclipse.jgit.lib.Repository)19 File (java.io.File)9 ArrayList (java.util.ArrayList)8 URISyntaxException (java.net.URISyntaxException)6 GitException (org.eclipse.che.api.git.exception.GitException)6 SimpleDateFormat (java.text.SimpleDateFormat)5 GitBlitException (com.gitblit.GitBlitException)4 RepositoryModel (com.gitblit.models.RepositoryModel)4 Point (java.awt.Point)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)4 TeamModel (com.gitblit.models.TeamModel)3 Change (com.gitblit.models.TicketModel.Change)3 UserModel (com.gitblit.models.UserModel)3 HashSet (java.util.HashSet)3 Git (org.eclipse.jgit.api.Git)3 RefSpec (org.eclipse.jgit.transport.RefSpec)3 AccessPermission (com.gitblit.Constants.AccessPermission)2