\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
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
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
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
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
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
}\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
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
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
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
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
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
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
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
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
} 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
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
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
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
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
}\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
}\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
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
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
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
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
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