From f6b9cd98683f19489b10ea6153d2585cb306e88e Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Wed, 15 May 2013 17:31:16 -0400 Subject: [PATCH] Fixed regression in repository url panel layout --- src/main/java/com/gitblit/utils/JGitUtils.java | 71 ++++++++++++++++++++++++++++++++++- 1 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gitblit/utils/JGitUtils.java b/src/main/java/com/gitblit/utils/JGitUtils.java index 1f2ae94..1e0ea5d 100644 --- a/src/main/java/com/gitblit/utils/JGitUtils.java +++ b/src/main/java/com/gitblit/utils/JGitUtils.java @@ -19,12 +19,14 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.text.DecimalFormat; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -33,6 +35,7 @@ import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.FetchCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.TagCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry.ChangeType; @@ -64,7 +67,7 @@ import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter; import org.eclipse.jgit.revwalk.filter.RevFilter; -import org.eclipse.jgit.storage.file.FileRepository; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.FetchResult; import org.eclipse.jgit.transport.RefSpec; @@ -195,7 +198,7 @@ File folder = new File(repositoriesFolder, name); if (folder.exists()) { File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED); - FileRepository repository = new FileRepository(gitDir); + Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build(); result.fetchResult = fetchRepository(credentialsProvider, repository); repository.close(); } else { @@ -1689,6 +1692,70 @@ } /** + * this method creates an incremental revision number as a tag according to + * the amount of already existing tags, which start with a defined prefix. + * + * @param repository + * @param objectId + * @param tagger + * @param prefix + * @param intPattern + * @param message + * @return true if operation was successful, otherwise false + */ + public static boolean createIncrementalRevisionTag(Repository repository, + String objectId, PersonIdent tagger, String prefix, String intPattern, String message) { + boolean result = false; + Iterator<Entry<String, Ref>> iterator = repository.getTags().entrySet().iterator(); + long lastRev = 0; + while (iterator.hasNext()) { + Entry<String, Ref> entry = iterator.next(); + if (entry.getKey().startsWith(prefix)) { + try { + long val = Long.parseLong(entry.getKey().substring(prefix.length())); + if (val > lastRev) { + lastRev = val; + } + } catch (Exception e) { + // this tag is NOT an incremental revision tag + } + } + } + DecimalFormat df = new DecimalFormat(intPattern); + result = createTag(repository, objectId, tagger, prefix + df.format((lastRev + 1)), message); + return result; + } + + /** + * creates a tag in a repository + * + * @param repository + * @param objectId, the ref the tag points towards + * @param tagger, the person tagging the object + * @param tag, the string label + * @param message, the string message + * @return boolean, true if operation was successful, otherwise false + */ + public static boolean createTag(Repository repository, String objectId, PersonIdent tagger, String tag, String message) { + try { + Git gitClient = Git.open(repository.getDirectory()); + TagCommand tagCommand = gitClient.tag(); + tagCommand.setTagger(tagger); + tagCommand.setMessage(message); + if (objectId != null) { + RevObject revObj = getCommit(repository, objectId); + tagCommand.setObjectId(revObj); + } + tagCommand.setName(tag); + Ref call = tagCommand.call(); + return call != null ? true : false; + } catch (Exception e) { + error(e, repository, "Failed to create tag {1} in repository {0}", objectId, tag); + } + return false; + } + + /** * Create an orphaned branch in a repository. * * @param repository -- Gitblit v1.9.1