]> source.dussan.org Git - gitblit.git/commitdiff
Documentation. Added JavaDoc. Clarified repository access restriction.
authorJames Moger <james.moger@gitblit.com>
Tue, 5 Jul 2011 21:32:45 +0000 (17:32 -0400)
committerJames Moger <james.moger@gitblit.com>
Tue, 5 Jul 2011 21:32:45 +0000 (17:32 -0400)
build.xml
distrib/gitblit.properties
docs/01_features.mkd
src/com/gitblit/GitBlit.java
src/com/gitblit/utils/JGitUtils.java
src/com/gitblit/wicket/pages/SummaryPage.java
tests/com/gitblit/tests/JGitUtilsTest.java

index 5d3864876be1a6b448cc4a413e09eeda7389288d..67c3910b1b1605b503a5e301c749449f6641720c 100644 (file)
--- a/build.xml
+++ b/build.xml
                         projectname="gitblit" \r
                         filename="${distribution.zipfile}" \r
                         targetfilename="gitblit-${gb.version}.zip"\r
-                        summary="Standalone, integrated Gitblit server v${gb.version}"\r
+                        summary="Gitblit GO v${gb.version} (standalone, integrated Gitblit server)"\r
                         labels="Featured, Type-Package, OpSys-All" />\r
                        \r
                <!-- Upload WAR file -->\r
                     projectname="gitblit" \r
                     filename="${distribution.warfile}" \r
                     targetfilename="gitblit-${gb.version}.war"\r
-                    summary="Gitblit WAR v${gb.version} for your servlet container"\r
+                    summary="Gitblit WAR v${gb.version} (standard WAR webapp for servlet containers)"\r
                     labels="Featured, Type-Package, OpSys-All" />\r
        </target>\r
 \r
index 37fdd3444953414e91d6423e99e42c07ffc3f707..93b306a2bfd601bad1e7a204de4c1a23f208d930 100644 (file)
@@ -20,6 +20,10 @@ git.repositoriesFolder = git
 git.searchRepositoriesSubfolders = true\r
 \r
 # Allow push/pull over http/https with JGit servlet.\r
+# If you do NOT want to allow Git clients to clone/push to Gitblit set this\r
+# to false.  You might want to do this if you are only using ssh:// or git://.\r
+# If you set this false, consider changing the [web.otherUrls] setting to\r
+# indicate your clone/push urls.\r
 #\r
 # SINCE 0.5.0\r
 git.enableGitServlet = true\r
index 7c522a7c80467927b35b4acbee9dd55bd76c23bb..f3b1707165c7ede82766ead00c1231908095d104 100644 (file)
@@ -1,7 +1,7 @@
 ## Gitblit Features\r
 - JGit SmartHTTP servlet\r
 - Browser and git client authentication\r
-- Four repository access control configurations with a Read-Only control flag\r
+- Four *per-repository* access control configurations with a Read-Only control flag\r
     <ul class='noBullets'>\r
     <li>![anonymous](blank.png) *Anonymous View, Clone & Push*</li>\r
     <li>![push](lock_go_16x16.png) *Authenticated Push*</li>\r
index 467d6620fe37e58e44b701e0dd31c55352b4c937..9fe6d201e4a864c46a8e042df3254f08a0928b81 100644 (file)
@@ -433,7 +433,7 @@ public class GitBlit implements ServletContextListener {
                RepositoryModel model = new RepositoryModel();\r
                model.name = repositoryName;\r
                model.hasCommits = JGitUtils.hasCommits(r);\r
-               model.lastChange = JGitUtils.getLastChange(r);\r
+               model.lastChange = JGitUtils.getLastChange(r, null);\r
                StoredConfig config = JGitUtils.readConfig(r);\r
                if (config != null) {\r
                        model.description = getConfig(config, "description", "");\r
index 1c607ca794c7695e7b1741e61badd8661286a5f4..a965aff38f4f7dafe695b428ac3d34c0291ab1c6 100644 (file)
@@ -84,6 +84,13 @@ public class JGitUtils {
 \r
        static final Logger LOGGER = LoggerFactory.getLogger(JGitUtils.class);\r
 \r
+       /**\r
+        * Returns the displayable name of the person in the form "Real Name <email\r
+        * address>".  If the email address is empty, just "Real Name" is returned.\r
+        * \r
+        * @param person\r
+        * @return "Real Name <email address>" or "Real Name"\r
+        */\r
        public static String getDisplayName(PersonIdent person) {\r
                if (StringUtils.isEmpty(person.getEmailAddress())) {\r
                        return person.getName();\r
@@ -96,6 +103,17 @@ public class JGitUtils {
                return r.toString().trim();\r
        }\r
 \r
+       /**\r
+        * Clone or Fetch a repository. If the local repository does not exist,\r
+        * clone is called. If the repository does exist, fetch is called. By\r
+        * default the clone/fetch retrieves the remote heads, tags, and notes.\r
+        * \r
+        * @param repositoriesFolder\r
+        * @param name\r
+        * @param fromUrl\r
+        * @return FetchResult\r
+        * @throws Exception\r
+        */\r
        public static FetchResult cloneRepository(File repositoriesFolder, String name, String fromUrl)\r
                        throws Exception {\r
                FetchResult result = null;\r
@@ -125,6 +143,15 @@ public class JGitUtils {
                return result;\r
        }\r
 \r
+       /**\r
+        * Fetch updates from the remote repository. If refSpecs is unspecifed,\r
+        * remote heads, tags, and notes are retrieved.\r
+        * \r
+        * @param repository\r
+        * @param refSpecs\r
+        * @return FetchResult\r
+        * @throws Exception\r
+        */\r
        public static FetchResult fetchRepository(Repository repository, RefSpec... refSpecs)\r
                        throws Exception {\r
                Git git = new Git(repository);\r
@@ -143,11 +170,29 @@ public class JGitUtils {
                return result;\r
        }\r
 \r
+       /**\r
+        * Creates a bare repository.\r
+        * \r
+        * @param repositoriesFolder\r
+        * @param name\r
+        * @return Repository\r
+        */\r
        public static Repository createRepository(File repositoriesFolder, String name) {\r
                Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();\r
                return git.getRepository();\r
        }\r
 \r
+       /**\r
+        * Returns a list of repository names in the specified folder.\r
+        * \r
+        * @param repositoriesFolder\r
+        * @param exportAll\r
+        *            if true, all repositories are listed. If false only the\r
+        *            repositories with a "git-daemon-export-ok" file are included\r
+        * @param searchSubfolders\r
+        *            recurse into subfolders to find grouped repositories\r
+        * @return list of repository names\r
+        */\r
        public static List<String> getRepositoryList(File repositoriesFolder, boolean exportAll,\r
                        boolean searchSubfolders) {\r
                List<String> list = new ArrayList<String>();\r
@@ -160,6 +205,20 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
+       /**\r
+        * Recursive function to find git repositories.\r
+        * \r
+        * @param basePath\r
+        *            basePath is stripped from the repository name as repositories\r
+        *            are relative to this path\r
+        * @param searchFolder\r
+        * @param exportAll\r
+        *            if true all repositories are listed. If false only the\r
+        *            repositories with a "git-daemon-export-ok" file are included\r
+        * @param searchSubfolders\r
+        *            recurse into subfolders to find grouped repositories\r
+        * @return\r
+        */\r
        private static List<String> getRepositoryList(String basePath, File searchFolder,\r
                        boolean exportAll, boolean searchSubfolders) {\r
                List<String> list = new ArrayList<String>();\r
@@ -186,8 +245,17 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
-       public static RevCommit getFirstCommit(Repository r, String branch) {\r
-               if (!hasCommits(r)) {\r
+       /**\r
+        * Returns the first commit on a branch. If the repository does not exist or\r
+        * is empty, null is returned.\r
+        * \r
+        * @param repository\r
+        * @param branch\r
+        *            if unspecified, HEAD is assumed.\r
+        * @return RevCommit\r
+        */\r
+       public static RevCommit getFirstCommit(Repository repository, String branch) {\r
+               if (!hasCommits(repository)) {\r
                        return null;\r
                }\r
                if (StringUtils.isEmpty(branch)) {\r
@@ -195,9 +263,9 @@ public class JGitUtils {
                }\r
                RevCommit commit = null;\r
                try {\r
-                       RevWalk walk = new RevWalk(r);\r
+                       RevWalk walk = new RevWalk(repository);\r
                        walk.sort(RevSort.REVERSE);\r
-                       RevCommit head = walk.parseCommit(r.resolve(branch));\r
+                       RevCommit head = walk.parseCommit(repository.resolve(branch));\r
                        walk.markStart(head);\r
                        commit = walk.next();\r
                        walk.dispose();\r
@@ -207,44 +275,89 @@ public class JGitUtils {
                return commit;\r
        }\r
 \r
-       public static Date getFirstChange(Repository r, String branch) {\r
-               RevCommit commit = getFirstCommit(r, branch);\r
+       /**\r
+        * Returns the date of the first commit on a branch. If the repository does\r
+        * not exist, Date(0) is returned. If the repository does exist bit is\r
+        * empty, the last modified date of the repository folder is returned.\r
+        * \r
+        * @param repository\r
+        * @param branch\r
+        *            if unspecified, HEAD is assumed.\r
+        * @return Date of the first commit on a branch\r
+        */\r
+       public static Date getFirstChange(Repository repository, String branch) {\r
+               RevCommit commit = getFirstCommit(repository, branch);\r
                if (commit == null) {\r
-                       if (r == null || !r.getDirectory().exists()) {\r
+                       if (repository == null || !repository.getDirectory().exists()) {\r
                                return new Date(0);\r
                        }\r
                        // fresh repository\r
-                       return new Date(r.getDirectory().lastModified());\r
+                       return new Date(repository.getDirectory().lastModified());\r
                }\r
                return getCommitDate(commit);\r
        }\r
 \r
-       public static boolean hasCommits(Repository r) {\r
-               if (r != null && r.getDirectory().exists()) {\r
-                       return new File(r.getDirectory(), Constants.R_HEADS).list().length > 0;\r
+       /**\r
+        * Determine if a repository has any commits. This is determined by checking\r
+        * for 1 or more heads.\r
+        * \r
+        * @param repository\r
+        * @return true if the repository has commits\r
+        */\r
+       public static boolean hasCommits(Repository repository) {\r
+               if (repository != null && repository.getDirectory().exists()) {\r
+                       return new File(repository.getDirectory(), Constants.R_HEADS).list().length > 0;\r
                }\r
                return false;\r
        }\r
 \r
-       public static Date getLastChange(Repository r) {\r
-               if (!hasCommits(r)) {\r
+       /**\r
+        * Returns the date of the most recent commit on a branch. If the repository\r
+        * does not exist Date(0) is returned. If it does exist but is empty, the\r
+        * last modified date of the repository folder is returned.\r
+        * \r
+        * @param repository\r
+        * @param branch\r
+        *            if unspecified, HEAD is assumed.\r
+        * @return\r
+        */\r
+       public static Date getLastChange(Repository repository, String branch) {\r
+               if (!hasCommits(repository)) {\r
                        // null repository\r
-                       if (r == null) {\r
+                       if (repository == null) {\r
                                return new Date(0);\r
                        }\r
                        // fresh repository\r
-                       return new Date(r.getDirectory().lastModified());\r
+                       return new Date(repository.getDirectory().lastModified());\r
                }\r
-               RevCommit commit = getCommit(r, Constants.HEAD);\r
+               if (StringUtils.isEmpty(branch)) {\r
+                       branch = Constants.HEAD;\r
+               }\r
+               RevCommit commit = getCommit(repository, branch);\r
                return getCommitDate(commit);\r
        }\r
 \r
+       /**\r
+        * Retrieves a Java Date from a Git commit.\r
+        * \r
+        * @param commit\r
+        * @return date of the commit\r
+        */\r
        public static Date getCommitDate(RevCommit commit) {\r
                return new Date(commit.getCommitTime() * 1000L);\r
        }\r
 \r
-       public static RevCommit getCommit(Repository r, String objectId) {\r
-               if (!hasCommits(r)) {\r
+       /**\r
+        * Returns the specified commit from the repository. If the repository does\r
+        * not exist or is empty, null is returned.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @return RevCommit\r
+        */\r
+       public static RevCommit getCommit(Repository repository, String objectId) {\r
+               if (!hasCommits(repository)) {\r
                        return null;\r
                }\r
                RevCommit commit = null;\r
@@ -252,8 +365,8 @@ public class JGitUtils {
                        if (StringUtils.isEmpty(objectId)) {\r
                                objectId = Constants.HEAD;\r
                        }\r
-                       ObjectId object = r.resolve(objectId);\r
-                       RevWalk walk = new RevWalk(r);\r
+                       ObjectId object = repository.resolve(objectId);\r
+                       RevWalk walk = new RevWalk(repository);\r
                        RevCommit rev = walk.parseCommit(object);\r
                        commit = rev;\r
                        walk.dispose();\r
@@ -263,27 +376,23 @@ public class JGitUtils {
                return commit;\r
        }\r
 \r
-       public static Map<ObjectId, List<RefModel>> getAllRefs(Repository r) {\r
-               List<RefModel> list = getRefs(r, org.eclipse.jgit.lib.RefDatabase.ALL, true, -1);\r
-               Map<ObjectId, List<RefModel>> refs = new HashMap<ObjectId, List<RefModel>>();\r
-               for (RefModel ref : list) {\r
-                       ObjectId objectid = ref.getReferencedObjectId();\r
-                       if (!refs.containsKey(objectid)) {\r
-                               refs.put(objectid, new ArrayList<RefModel>());\r
-                       }\r
-                       refs.get(objectid).add(ref);\r
-               }\r
-               return refs;\r
-       }\r
-\r
-       public static byte[] getByteContent(Repository r, RevTree tree, final String path) {\r
-               RevWalk rw = new RevWalk(r);\r
-               TreeWalk tw = new TreeWalk(r);\r
+       /**\r
+        * Retrieves the raw byte content of a file in the specified tree.\r
+        * \r
+        * @param repository\r
+        * @param tree\r
+        *            if null, the RevTree from HEAD is assumed.\r
+        * @param path\r
+        * @return content as a byte []\r
+        */\r
+       public static byte[] getByteContent(Repository repository, RevTree tree, final String path) {\r
+               RevWalk rw = new RevWalk(repository);\r
+               TreeWalk tw = new TreeWalk(repository);\r
                tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));\r
                byte[] content = null;\r
                try {\r
                        if (tree == null) {\r
-                               ObjectId object = r.resolve(Constants.HEAD);\r
+                               ObjectId object = repository.resolve(Constants.HEAD);\r
                                RevCommit commit = rw.parseCommit(object);\r
                                tree = commit.getTree();\r
                        }\r
@@ -298,7 +407,7 @@ public class JGitUtils {
                                RevObject ro = rw.lookupAny(entid, entmode.getObjectType());\r
                                rw.parseBody(ro);\r
                                ByteArrayOutputStream os = new ByteArrayOutputStream();\r
-                               ObjectLoader ldr = r.open(ro.getId(), Constants.OBJ_BLOB);\r
+                               ObjectLoader ldr = repository.open(ro.getId(), Constants.OBJ_BLOB);\r
                                byte[] tmp = new byte[4096];\r
                                InputStream in = ldr.openStream();\r
                                int n;\r
@@ -317,22 +426,38 @@ public class JGitUtils {
                return content;\r
        }\r
 \r
-       public static String getStringContent(Repository r, RevTree tree, String blobPath) {\r
-               byte[] content = getByteContent(r, tree, blobPath);\r
+       /**\r
+        * Returns the UTF-8 string content of a file in the specified tree.\r
+        * \r
+        * @param repository\r
+        * @param tree\r
+        *            if null, the RevTree from HEAD is assumed.\r
+        * @param blobPath\r
+        * @return UTF-8 string content\r
+        */\r
+       public static String getStringContent(Repository repository, RevTree tree, String blobPath) {\r
+               byte[] content = getByteContent(repository, tree, blobPath);\r
                if (content == null) {\r
                        return null;\r
                }\r
                return new String(content, Charset.forName(Constants.CHARACTER_ENCODING));\r
        }\r
 \r
-       public static byte[] getByteContent(Repository r, String objectId) {\r
-               RevWalk rw = new RevWalk(r);\r
+       /**\r
+        * Gets the raw byte content of the specified blob object.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        * @return byte [] blob content\r
+        */\r
+       public static byte[] getByteContent(Repository repository, String objectId) {\r
+               RevWalk rw = new RevWalk(repository);\r
                byte[] content = null;\r
                try {\r
                        RevBlob blob = rw.lookupBlob(ObjectId.fromString(objectId));\r
                        rw.parseBody(blob);\r
                        ByteArrayOutputStream os = new ByteArrayOutputStream();\r
-                       ObjectLoader ldr = r.open(blob.getId(), Constants.OBJ_BLOB);\r
+                       ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);\r
                        byte[] tmp = new byte[4096];\r
                        InputStream in = ldr.openStream();\r
                        int n;\r
@@ -349,27 +474,47 @@ public class JGitUtils {
                return content;\r
        }\r
 \r
-       public static String getStringContent(Repository r, String objectId) {\r
-               byte[] content = getByteContent(r, objectId);\r
+       /**\r
+        * Gets the UTF-8 string content of the blob specified by objectId.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        * @return UTF-8 string content\r
+        */\r
+       public static String getStringContent(Repository repository, String objectId) {\r
+               byte[] content = getByteContent(repository, objectId);\r
                if (content == null) {\r
                        return null;\r
                }\r
                return new String(content, Charset.forName(Constants.CHARACTER_ENCODING));\r
        }\r
 \r
-       public static List<PathModel> getFilesInPath(Repository r, String basePath, RevCommit commit) {\r
+       /**\r
+        * Returns the list of files in the specified folder at the specified\r
+        * commit. If the repository does not exist or is empty, an empty list is\r
+        * returned.\r
+        * \r
+        * @param repository\r
+        * @param path\r
+        *            if unspecified, root folder is assumed.\r
+        * @param commit\r
+        *            if null, HEAD is assumed.\r
+        * @return list of files in specified path\r
+        */\r
+       public static List<PathModel> getFilesInPath(Repository repository, String path,\r
+                       RevCommit commit) {\r
                List<PathModel> list = new ArrayList<PathModel>();\r
-               if (!hasCommits(r)) {\r
+               if (!hasCommits(repository)) {\r
                        return list;\r
                }\r
                if (commit == null) {\r
-                       commit = getCommit(r, Constants.HEAD);\r
+                       commit = getCommit(repository, Constants.HEAD);\r
                }\r
-               final TreeWalk tw = new TreeWalk(r);\r
+               final TreeWalk tw = new TreeWalk(repository);\r
                try {\r
                        tw.addTree(commit.getTree());\r
-                       if (!StringUtils.isEmpty(basePath)) {\r
-                               PathFilter f = PathFilter.create(basePath);\r
+                       if (!StringUtils.isEmpty(path)) {\r
+                               PathFilter f = PathFilter.create(path);\r
                                tw.setFilter(f);\r
                                tw.setRecursive(false);\r
                                boolean foundFolder = false;\r
@@ -377,12 +522,12 @@ public class JGitUtils {
                                        if (!foundFolder && tw.isSubtree()) {\r
                                                tw.enterSubtree();\r
                                        }\r
-                                       if (tw.getPathString().equals(basePath)) {\r
+                                       if (tw.getPathString().equals(path)) {\r
                                                foundFolder = true;\r
                                                continue;\r
                                        }\r
                                        if (foundFolder) {\r
-                                               list.add(getPathModel(tw, basePath, commit));\r
+                                               list.add(getPathModel(tw, path, commit));\r
                                        }\r
                                }\r
                        } else {\r
@@ -400,17 +545,29 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
-       public static List<PathChangeModel> getFilesInCommit(Repository r, RevCommit commit) {\r
+       /**\r
+        * Returns the list of files changed in a specified commit. If the\r
+        * repository does not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param commit\r
+        *            if null, HEAD is assumed.\r
+        * @return list of files changed in a commit\r
+        */\r
+       public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit) {\r
                List<PathChangeModel> list = new ArrayList<PathChangeModel>();\r
-               RevWalk rw = new RevWalk(r);\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
+               RevWalk rw = new RevWalk(repository);\r
                try {\r
                        if (commit == null) {\r
-                               ObjectId object = r.resolve(Constants.HEAD);\r
+                               ObjectId object = repository.resolve(Constants.HEAD);\r
                                commit = rw.parseCommit(object);\r
                        }\r
 \r
                        if (commit.getParentCount() == 0) {\r
-                               TreeWalk tw = new TreeWalk(r);\r
+                               TreeWalk tw = new TreeWalk(repository);\r
                                tw.reset();\r
                                tw.setRecursive(true);\r
                                tw.addTree(commit.getTree());\r
@@ -422,7 +579,7 @@ public class JGitUtils {
                        } else {\r
                                RevCommit parent = rw.parseCommit(commit.getParent(0).getId());\r
                                DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);\r
-                               df.setRepository(r);\r
+                               df.setRepository(repository);\r
                                df.setDiffComparator(RawTextComparator.DEFAULT);\r
                                df.setDetectRenames(true);\r
                                List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());\r
@@ -446,10 +603,22 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
-       public static List<PathModel> getDocuments(Repository r, List<String> extensions) {\r
+       /**\r
+        * Returns the list of files in the repository that match one of the\r
+        * specified extensions. This is a CASE-SENSITIVE search. If the repository\r
+        * does not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param extensions\r
+        * @return list of files in repository with a matching extension\r
+        */\r
+       public static List<PathModel> getDocuments(Repository repository, List<String> extensions) {\r
                List<PathModel> list = new ArrayList<PathModel>();\r
-               RevCommit commit = getCommit(r, Constants.HEAD);\r
-               final TreeWalk tw = new TreeWalk(r);\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
+               RevCommit commit = getCommit(repository, Constants.HEAD);\r
+               final TreeWalk tw = new TreeWalk(repository);\r
                try {\r
                        tw.addTree(commit.getTree());\r
                        if (extensions != null && extensions.size() > 0) {\r
@@ -478,6 +647,14 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
+       /**\r
+        * Returns a path model of the current file in the treewalk.\r
+        * \r
+        * @param tw\r
+        * @param basePath\r
+        * @param commit\r
+        * @return a path model of the current file in the treewalk\r
+        */\r
        private static PathModel getPathModel(TreeWalk tw, String basePath, RevCommit commit) {\r
                String name;\r
                long size = 0;\r
@@ -497,6 +674,12 @@ public class JGitUtils {
                                commit.getName());\r
        }\r
 \r
+       /**\r
+        * Returns a permissions representation of the mode bits.\r
+        * \r
+        * @param mode\r
+        * @return string representation of the mode bits\r
+        */\r
        public static String getPermissionsFromMode(int mode) {\r
                if (FileMode.TREE.equals(mode)) {\r
                        return "drwxr-xr-x";\r
@@ -515,29 +698,69 @@ public class JGitUtils {
                return "missing";\r
        }\r
 \r
-       public static List<RevCommit> getRevLog(Repository r, int maxCount) {\r
-               return getRevLog(r, Constants.HEAD, 0, maxCount);\r
+       /**\r
+        * Returns a list of commits starting from HEAD and working backwards.\r
+        * \r
+        * @param repository\r
+        * @param maxCount\r
+        *            if < 0, all commits for the repository are returned.\r
+        * @return list of commits\r
+        */\r
+       public static List<RevCommit> getRevLog(Repository repository, int maxCount) {\r
+               return getRevLog(repository, Constants.HEAD, 0, maxCount);\r
        }\r
 \r
-       public static List<RevCommit> getRevLog(Repository r, String objectId, int offset, int maxCount) {\r
-               return getRevLog(r, objectId, null, offset, maxCount);\r
+       /**\r
+        * Returns a list of commits starting from the specified objectId using an\r
+        * offset and maxCount for paging. This is similar to LIMIT n OFFSET p in\r
+        * SQL. If the repository does not exist or is empty, an empty list is\r
+        * returned.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @param offset\r
+        * @param maxCount\r
+        *            if < 0, all commits are returned.\r
+        * @return a paged list of commits\r
+        */\r
+       public static List<RevCommit> getRevLog(Repository repository, String objectId, int offset,\r
+                       int maxCount) {\r
+               return getRevLog(repository, objectId, null, offset, maxCount);\r
        }\r
 \r
-       public static List<RevCommit> getRevLog(Repository r, String objectId, String path, int offset,\r
-                       int maxCount) {\r
+       /**\r
+        * Returns a list of commits for the repository or a path within the\r
+        * repository. Caller may specify ending revision with objectId. Caller may\r
+        * specify offset and maxCount to achieve pagination of results. If the\r
+        * repository does not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @param path\r
+        *            if unspecified, commits for repository are returned. If\r
+        *            specified, commits for the path are returned.\r
+        * @param offset\r
+        * @param maxCount\r
+        *            if < 0, all commits are returned.\r
+        * @return a paged list of commits\r
+        */\r
+       public static List<RevCommit> getRevLog(Repository repository, String objectId, String path,\r
+                       int offset, int maxCount) {\r
                List<RevCommit> list = new ArrayList<RevCommit>();\r
                if (maxCount == 0) {\r
                        return list;\r
                }\r
-               if (!hasCommits(r)) {\r
+               if (!hasCommits(repository)) {\r
                        return list;\r
                }\r
                try {\r
                        if (StringUtils.isEmpty(objectId)) {\r
                                objectId = Constants.HEAD;\r
                        }\r
-                       RevWalk rw = new RevWalk(r);\r
-                       ObjectId object = r.resolve(objectId);\r
+                       RevWalk rw = new RevWalk(repository);\r
+                       ObjectId object = repository.resolve(objectId);\r
                        rw.markStart(rw.parseCommit(object));\r
                        if (!StringUtils.isEmpty(path)) {\r
                                TreeFilter filter = AndTreeFilter.create(\r
@@ -590,25 +813,44 @@ public class JGitUtils {
                }\r
        }\r
 \r
-       public static List<RevCommit> searchRevlogs(Repository r, String objectId, String value,\r
-                       final SearchType type, int offset, int maxCount) {\r
+       /**\r
+        * Search the commit history for a case-insensitive match to the value.\r
+        * Search results require a specified SearchType of AUTHOR, COMMITTER, or\r
+        * COMMIT. Results may be paginated using offset and maxCount. If the\r
+        * repository does not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @param value\r
+        * @param type\r
+        *            AUTHOR, COMMITTER, COMMIT\r
+        * @param offset\r
+        * @param maxCount\r
+        *            if < 0, all matches are returned\r
+        * @return matching list of commits\r
+        */\r
+       public static List<RevCommit> searchRevlogs(Repository repository, String objectId,\r
+                       String value, final SearchType type, int offset, int maxCount) {\r
                final String lcValue = value.toLowerCase();\r
                List<RevCommit> list = new ArrayList<RevCommit>();\r
                if (maxCount == 0) {\r
                        return list;\r
                }\r
-               if (!hasCommits(r)) {\r
+               if (!hasCommits(repository)) {\r
                        return list;\r
                }\r
                try {\r
                        if (StringUtils.isEmpty(objectId)) {\r
                                objectId = Constants.HEAD;\r
                        }\r
-                       RevWalk rw = new RevWalk(r);\r
+                       RevWalk rw = new RevWalk(repository);\r
                        rw.setRevFilter(new RevFilter() {\r
 \r
                                @Override\r
                                public RevFilter clone() {\r
+                                       // FindBugs complains about this method name.\r
+                                       // This is part of JGit design and unrelated to Cloneable.\r
                                        return this;\r
                                }\r
 \r
@@ -636,7 +878,7 @@ public class JGitUtils {
                                }\r
 \r
                        });\r
-                       ObjectId object = r.resolve(objectId);\r
+                       ObjectId object = repository.resolve(objectId);\r
                        rw.markStart(rw.parseCommit(object));\r
                        Iterable<RevCommit> revlog = rw;\r
                        if (offset > 0) {\r
@@ -665,30 +907,118 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
-       public static List<RefModel> getTags(Repository r, boolean fullName, int maxCount) {\r
-               return getRefs(r, Constants.R_TAGS, fullName, maxCount);\r
+       /**\r
+        * Returns all refs grouped by their associated object id.\r
+        * \r
+        * @param repository\r
+        * @return all refs grouped by their referenced object id\r
+        */\r
+       public static Map<ObjectId, List<RefModel>> getAllRefs(Repository repository) {\r
+               List<RefModel> list = getRefs(repository, org.eclipse.jgit.lib.RefDatabase.ALL, true, -1);\r
+               Map<ObjectId, List<RefModel>> refs = new HashMap<ObjectId, List<RefModel>>();\r
+               for (RefModel ref : list) {\r
+                       ObjectId objectid = ref.getReferencedObjectId();\r
+                       if (!refs.containsKey(objectid)) {\r
+                               refs.put(objectid, new ArrayList<RefModel>());\r
+                       }\r
+                       refs.get(objectid).add(ref);\r
+               }\r
+               return refs;\r
        }\r
 \r
-       public static List<RefModel> getLocalBranches(Repository r, boolean fullName, int maxCount) {\r
-               return getRefs(r, Constants.R_HEADS, fullName, maxCount);\r
+       /**\r
+        * Returns the list of tags in the repository. If repository does not exist\r
+        * or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param fullName\r
+        *            if true, /refs/tags/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all tags are returned\r
+        * @return list of tags\r
+        */\r
+       public static List<RefModel> getTags(Repository repository, boolean fullName, int maxCount) {\r
+               return getRefs(repository, Constants.R_TAGS, fullName, maxCount);\r
        }\r
 \r
-       public static List<RefModel> getRemoteBranches(Repository r, boolean fullName, int maxCount) {\r
-               return getRefs(r, Constants.R_REMOTES, fullName, maxCount);\r
+       /**\r
+        * Returns the list of local branches in the repository. If repository does\r
+        * not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param fullName\r
+        *            if true, /refs/heads/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all local branches are returned\r
+        * @return list of local branches\r
+        */\r
+       public static List<RefModel> getLocalBranches(Repository repository, boolean fullName,\r
+                       int maxCount) {\r
+               return getRefs(repository, Constants.R_HEADS, fullName, maxCount);\r
        }\r
 \r
-       public static List<RefModel> getNotesRefs(Repository r, boolean fullName, int maxCount) {\r
-               return getRefs(r, Constants.R_NOTES, fullName, maxCount);\r
+       /**\r
+        * Returns the list of remote branches in the repository. If repository does\r
+        * not exist or is empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param fullName\r
+        *            if true, /refs/remotes/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all remote branches are returned\r
+        * @return list of remote branches\r
+        */\r
+       public static List<RefModel> getRemoteBranches(Repository repository, boolean fullName,\r
+                       int maxCount) {\r
+               return getRefs(repository, Constants.R_REMOTES, fullName, maxCount);\r
        }\r
 \r
-       private static List<RefModel> getRefs(Repository r, String refs, boolean fullName, int maxCount) {\r
+       /**\r
+        * Returns the list of note branches. If repository does not exist or is\r
+        * empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param fullName\r
+        *            if true, /refs/notes/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all note branches are returned\r
+        * @return list of note branches\r
+        */\r
+       public static List<RefModel> getNoteBranches(Repository repository, boolean fullName,\r
+                       int maxCount) {\r
+               return getRefs(repository, Constants.R_NOTES, fullName, maxCount);\r
+       }\r
+\r
+       /**\r
+        * Returns a list of references in the repository matching "refs". If the\r
+        * repository is null or empty, an empty list is returned.\r
+        * \r
+        * @param repository\r
+        * @param refs\r
+        *            if unspecified, all refs are returned\r
+        * @param fullName\r
+        *            if true, /refs/something/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all references are returned\r
+        * @return list of references\r
+        */\r
+       private static List<RefModel> getRefs(Repository repository, String refs, boolean fullName,\r
+                       int maxCount) {\r
                List<RefModel> list = new ArrayList<RefModel>();\r
                if (maxCount == 0) {\r
                        return list;\r
                }\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
                try {\r
-                       Map<String, Ref> map = r.getRefDatabase().getRefs(refs);\r
-                       RevWalk rw = new RevWalk(r);\r
+                       Map<String, Ref> map = repository.getRefDatabase().getRefs(refs);\r
+                       RevWalk rw = new RevWalk(repository);\r
                        for (Entry<String, Ref> entry : map.entrySet()) {\r
                                Ref ref = entry.getValue();\r
                                RevObject object = rw.parseAny(ref.getObjectId());\r
@@ -710,10 +1040,22 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
+       /**\r
+        * Returns the list of notes entered about the commit from the refs/notes\r
+        * namespace. If the repository does not exist or is empty, an empty list is\r
+        * returned.\r
+        * \r
+        * @param repository\r
+        * @param commit\r
+        * @return list of notes\r
+        */\r
        public static List<GitNote> getNotesOnCommit(Repository repository, RevCommit commit) {\r
                List<GitNote> list = new ArrayList<GitNote>();\r
-               List<RefModel> notesRefs = getNotesRefs(repository, true, -1);\r
-               for (RefModel notesRef : notesRefs) {\r
+               if (!hasCommits(repository)) {\r
+                       return list;\r
+               }\r
+               List<RefModel> noteBranches = getNoteBranches(repository, true, -1);\r
+               for (RefModel notesRef : noteBranches) {\r
                        RevTree notesTree = JGitUtils.getCommit(repository, notesRef.getName()).getTree();\r
                        StringBuilder sb = new StringBuilder(commit.getName());\r
                        sb.insert(2, '/');\r
@@ -730,8 +1072,14 @@ public class JGitUtils {
                return list;\r
        }\r
 \r
-       public static StoredConfig readConfig(Repository r) {\r
-               StoredConfig c = r.getConfig();\r
+       /**\r
+        * Returns a StoredConfig object for the repository.\r
+        * \r
+        * @param repository\r
+        * @return the StoredConfig of the repository\r
+        */\r
+       public static StoredConfig readConfig(Repository repository) {\r
+               StoredConfig c = repository.getConfig();\r
                try {\r
                        c.load();\r
                } catch (ConfigInvalidException cex) {\r
@@ -742,19 +1090,32 @@ public class JGitUtils {
                return c;\r
        }\r
 \r
-       public static boolean zip(Repository r, String basePath, String objectId, OutputStream os)\r
-                       throws Exception {\r
-               RevCommit commit = getCommit(r, objectId);\r
+       /**\r
+        * Zips the contents of the tree at the (optionally) specified revision and\r
+        * the (optionally) specified basepath to the supplied outputstream.\r
+        * \r
+        * @param repository\r
+        * @param basePath\r
+        *            if unspecified, entire repository is assumed.\r
+        * @param objectId\r
+        *            if unspecified, HEAD is assumed.\r
+        * @param os\r
+        * @return true if repository was successfully zipped to supplied output\r
+        *         stream\r
+        */\r
+       public static boolean zip(Repository repository, String basePath, String objectId,\r
+                       OutputStream os) {\r
+               RevCommit commit = getCommit(repository, objectId);\r
                if (commit == null) {\r
                        return false;\r
                }\r
                boolean success = false;\r
-               RevWalk rw = new RevWalk(r);\r
-               TreeWalk tw = new TreeWalk(r);\r
+               RevWalk rw = new RevWalk(repository);\r
+               TreeWalk tw = new TreeWalk(repository);\r
                try {\r
                        tw.addTree(commit.getTree());\r
                        ZipOutputStream zos = new ZipOutputStream(os);\r
-                       zos.setComment("Generated by Git:Blit");\r
+                       zos.setComment("Generated by Gitblit");\r
                        if (!StringUtils.isEmpty(basePath)) {\r
                                PathFilter f = PathFilter.create(basePath);\r
                                tw.setFilter(f);\r
@@ -772,7 +1133,7 @@ public class JGitUtils {
                                RevBlob blob = (RevBlob) rw.lookupAny(entid, entmode.getObjectType());\r
                                rw.parseBody(blob);\r
 \r
-                               ObjectLoader ldr = r.open(blob.getId(), Constants.OBJ_BLOB);\r
+                               ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);\r
                                byte[] tmp = new byte[4096];\r
                                InputStream in = ldr.openStream();\r
                                int n;\r
index 39b8a97e9c923132d66c1ff7bf13c990d31003bf..e753e5d0494b2cfd41ee2563595dc166a375b2f8 100644 (file)
@@ -78,7 +78,7 @@ public class SummaryPage extends RepositoryPage {
                add(new Label("repositoryDescription", getRepositoryModel().description));\r
                add(new Label("repositoryOwner", getRepositoryModel().owner));\r
 \r
-               add(WicketUtils.createTimestampLabel("repositoryLastChange", JGitUtils.getLastChange(r),\r
+               add(WicketUtils.createTimestampLabel("repositoryLastChange", JGitUtils.getLastChange(r, null),\r
                                getTimeZone()));\r
                if (metricsTotal == null) {\r
                        add(new Label("branchStats", ""));\r
index daf0cfee6e130430c031d66bf0d406c2fd7d276d..0841da3c96ce2f796ffb0e14b9816ce7d60f5264 100644 (file)
@@ -85,11 +85,11 @@ public class JGitUtilsTest extends TestCase {
        }\r
 \r
        public void testLastCommit() throws Exception {\r
-               assertTrue(JGitUtils.getLastChange(null).equals(new Date(0)));\r
+               assertTrue(JGitUtils.getLastChange(null, null).equals(new Date(0)));\r
 \r
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                assertTrue(JGitUtils.getCommit(repository, null) != null);\r
-               Date date = JGitUtils.getLastChange(repository);\r
+               Date date = JGitUtils.getLastChange(repository, null);\r
                repository.close();\r
                assertTrue("Could not get last repository change date!", date != null);\r
        }\r
@@ -106,7 +106,7 @@ public class JGitUtilsTest extends TestCase {
                        assertTrue(JGitUtils.getFirstCommit(repository, null) == null);\r
                        assertTrue(JGitUtils.getFirstChange(repository, null).getTime() == folder\r
                                        .lastModified());\r
-                       assertTrue(JGitUtils.getLastChange(repository).getTime() == folder.lastModified());\r
+                       assertTrue(JGitUtils.getLastChange(repository, null).getTime() == folder.lastModified());\r
                        assertTrue(JGitUtils.getCommit(repository, null) == null);\r
                        repository.close();\r
                        assertTrue(GitBlit.self().deleteRepository(repositoryName));\r