From 1c70dd6d219d2336b0d505739fe4c17677d5cc8c Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Mon, 14 Nov 2016 11:00:34 -0800 Subject: Add {get,set}GitwebDescription to Repository This method pair allows the caller to read and modify the description file that is traditionally used by gitweb and cgit when rendering a repository on the web. Gerrit Code Review has offered this feature for years as part of its GitRepositoryManager interface, but its fundamentally a feature of JGit and its Repository abstraction. git-core typically initializes a repository with a default value inside the description file. During getDescription() this string is converted to null as it is never a useful description. Change-Id: I0a457026c74e9c73ea27e6f070d5fbaca3439be5 --- .../src/org/eclipse/jgit/internal/JGitText.java | 1 + .../jgit/internal/storage/file/FileRepository.java | 64 ++++++++++++++++++++-- .../src/org/eclipse/jgit/lib/Repository.java | 28 +++++++++- 3 files changed, 87 insertions(+), 6 deletions(-) (limited to 'org.eclipse.jgit/src') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java index 212cb7fdef..3577c4b286 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java @@ -734,6 +734,7 @@ public class JGitText extends TranslationBundle { /***/ public String unsupportedOperationNotAddAtEnd; /***/ public String unsupportedPackIndexVersion; /***/ public String unsupportedPackVersion; + /***/ public String unsupportedRepositoryDescription; /***/ public String updatingHeadFailed; /***/ public String updatingReferences; /***/ public String updatingRefFailed; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java index 5b93399a76..ed1281bc6c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java @@ -55,8 +55,10 @@ import java.io.IOException; import java.text.MessageFormat; import java.text.ParseException; import java.util.HashSet; +import java.util.Objects; import java.util.Set; +import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.attributes.AttributesNode; import org.eclipse.jgit.attributes.AttributesNodeProvider; @@ -85,6 +87,8 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.storage.pack.PackConfig; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; +import org.eclipse.jgit.util.IO; +import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.StringUtils; import org.eclipse.jgit.util.SystemReader; @@ -114,16 +118,13 @@ import org.eclipse.jgit.util.SystemReader; * */ public class FileRepository extends Repository { - private final FileBasedConfig systemConfig; + private static final String UNNAMED = "Unnamed repository; edit this file to name it for gitweb."; //$NON-NLS-1$ + private final FileBasedConfig systemConfig; private final FileBasedConfig userConfig; - private final FileBasedConfig repoConfig; - private final RefDatabase refs; - private final ObjectDirectory objectDatabase; - private FileSnapshot snapshot; /** @@ -420,6 +421,59 @@ public class FileRepository extends Repository { return repoConfig; } + @Override + @Nullable + public String getGitwebDescription() throws IOException { + String d; + try { + d = RawParseUtils.decode(IO.readFully(descriptionFile())); + } catch (FileNotFoundException err) { + return null; + } + if (d != null) { + d = d.trim(); + if (d.isEmpty() || UNNAMED.equals(d)) { + return null; + } + } + return d; + } + + @Override + public void setGitwebDescription(@Nullable String description) + throws IOException { + String old = getGitwebDescription(); + if (Objects.equals(old, description)) { + return; + } + + File path = descriptionFile(); + LockFile lock = new LockFile(path); + if (!lock.lock()) { + throw new IOException(MessageFormat.format(JGitText.get().lockError, + path.getAbsolutePath())); + } + try { + String d = description; + if (d != null) { + d = d.trim(); + if (!d.isEmpty()) { + d += '\n'; + } + } else { + d = ""; //$NON-NLS-1$ + } + lock.write(Constants.encode(d)); + lock.commit(); + } finally { + lock.unlock(); + } + } + + private File descriptionFile() { + return new File(getDirectory(), "description"); //$NON-NLS-1$ + } + /** * Objects known to exist but not expressed by {@link #getAllRefs()}. *

diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index 1909037eeb..19d9610d0f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -246,7 +246,6 @@ public abstract class Repository implements AutoCloseable { @NonNull public abstract AttributesNodeProvider createAttributesNodeProvider(); - /** * @return the used file system abstraction, or or {@code null} if * repository isn't local. @@ -1436,6 +1435,33 @@ public abstract class Repository implements AutoCloseable { return null; } + /** + * Read the {@code GIT_DIR/description} file for gitweb. + * + * @return description text; null if no description has been configured. + * @throws IOException + * description cannot be accessed. + * @since 4.6 + */ + @Nullable + public String getGitwebDescription() throws IOException { + return null; + } + + /** + * Set the {@code GIT_DIR/description} file for gitweb. + * + * @param description + * new description; null to clear the description. + * @throws IOException + * description cannot be persisted. + * @since 4.6 + */ + public void setGitwebDescription(@Nullable String description) + throws IOException { + throw new IOException(JGitText.get().unsupportedRepositoryDescription); + } + /** * @param refName * @return a {@link ReflogReader} for the supplied refname, or {@code null} -- cgit v1.2.3