use of com.gitblit.models.RefModel in project gitblit by gitblit.
the class JGitUtilsTest method testRefs.
@Test
public void testRefs() throws Exception {
Repository repository = GitBlitSuite.getJGitRepository();
Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
repository.close();
assertTrue(map.size() > 0);
for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
List<RefModel> list = entry.getValue();
for (RefModel ref : list) {
if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
assertEquals("refs/tags/spearce-gpg-pub", ref.toString());
assertEquals("8bbde7aacf771a9afb6992434f1ae413e010c6d8", ref.getObjectId().getName());
assertEquals("spearce@spearce.org", ref.getAuthorIdent().getEmailAddress());
assertTrue(ref.getShortMessage().startsWith("GPG key"));
assertTrue(ref.getFullMessage().startsWith("GPG key"));
assertEquals(Constants.OBJ_BLOB, ref.getReferencedObjectType());
} else if (ref.displayName.equals("refs/tags/v0.12.1")) {
assertTrue(ref.isAnnotatedTag());
}
}
}
}
use of com.gitblit.models.RefModel in project gitblit by gitblit.
the class JGitUtilsTest method testBranches.
@Test
public void testBranches() throws Exception {
Repository repository = GitBlitSuite.getJGitRepository();
assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
assertTrue(model.getName().startsWith(Constants.R_HEADS));
assertTrue(model.equals(model));
assertFalse(model.equals(""));
assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode() + model.getName().hashCode());
assertTrue(model.getShortMessage().equals(model.getShortMessage()));
}
for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
assertTrue(model.getName().startsWith(Constants.R_REMOTES));
assertTrue(model.equals(model));
assertFalse(model.equals(""));
assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode() + model.getName().hashCode());
assertTrue(model.getShortMessage().equals(model.getShortMessage()));
}
assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
repository.close();
}
use of com.gitblit.models.RefModel in project gitblit by gitblit.
the class AddIndexedBranch method main.
public static void main(String... args) {
Params params = new Params();
CmdLineParser parser = new CmdLineParser(params);
try {
parser.parseArgument(args);
} catch (CmdLineException t) {
System.err.println(t.getMessage());
parser.printUsage(System.out);
return;
}
// create a lowercase set of excluded repositories
Set<String> exclusions = new TreeSet<String>();
for (String exclude : params.exclusions) {
exclusions.add(exclude.toLowerCase());
}
// determine available repositories
File folder = new File(params.folder);
List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
int modCount = 0;
int skipCount = 0;
for (String repo : repoList) {
boolean skip = false;
for (String exclusion : exclusions) {
if (StringUtils.fuzzyMatch(repo, exclusion)) {
skip = true;
break;
}
}
if (skip) {
System.out.println("skipping " + repo);
skipCount++;
continue;
}
try {
// load repository config
File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);
Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
StoredConfig config = repository.getConfig();
config.load();
Set<String> indexedBranches = new LinkedHashSet<String>();
// add all local branches to index
if (params.addAllLocalBranches) {
List<RefModel> list = JGitUtils.getLocalBranches(repository, true, -1);
for (RefModel refModel : list) {
System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", refModel.getName(), repo));
indexedBranches.add(refModel.getName());
}
} else {
// add only one branch to index ('default' if not specified)
System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));
indexedBranches.add(params.branch);
}
String[] branches = config.getStringList("gitblit", null, "indexBranch");
if (!ArrayUtils.isEmpty(branches)) {
for (String branch : branches) {
indexedBranches.add(branch);
}
}
config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));
config.save();
modCount++;
} catch (Exception e) {
System.err.println(repo);
e.printStackTrace();
}
}
System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));
}
use of com.gitblit.models.RefModel in project gitblit by gitblit.
the class LuceneService method updateIndex.
/**
* Updates a repository index incrementally from the last indexed commits.
*
* @param model
* @param repository
* @return IndexResult
*/
private IndexResult updateIndex(RepositoryModel model, Repository repository) {
IndexResult result = new IndexResult();
try {
FileBasedConfig config = getConfig(repository);
config.load();
// build a quick lookup of annotated tags
Map<String, List<String>> tags = new HashMap<String, List<String>>();
for (RefModel tag : JGitUtils.getTags(repository, false, -1)) {
if (!tag.isAnnotatedTag()) {
// skip non-annotated tags
continue;
}
if (!tags.containsKey(tag.getObjectId().getName())) {
tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>());
}
tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
}
// detect branch deletion
// first assume all branches are deleted and then remove each
// existing branch from deletedBranches during indexing
Set<String> deletedBranches = new TreeSet<String>();
for (String alias : config.getNames(CONF_ALIAS)) {
String branch = config.getString(CONF_ALIAS, null, alias);
deletedBranches.add(branch);
}
// get the local branches
List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
// sort them by most recently updated
Collections.sort(branches, new Comparator<RefModel>() {
@Override
public int compare(RefModel ref1, RefModel ref2) {
return ref2.getDate().compareTo(ref1.getDate());
}
});
// reorder default branch to first position
RefModel defaultBranch = null;
ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
for (RefModel branch : branches) {
if (branch.getObjectId().equals(defaultBranchId)) {
defaultBranch = branch;
break;
}
}
branches.remove(defaultBranch);
branches.add(0, defaultBranch);
// walk through each branches
for (RefModel branch : branches) {
String branchName = branch.getName();
boolean indexBranch = false;
if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH) && branch.equals(defaultBranch)) {
// indexing "default" branch
indexBranch = true;
} else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) {
// ignore internal meta branches
indexBranch = false;
} else {
// normal explicit branch check
indexBranch = model.indexedBranches.contains(branch.getName());
}
// if this branch is not specifically indexed then skip
if (!indexBranch) {
continue;
}
// remove this branch from the deletedBranches set
deletedBranches.remove(branchName);
// determine last commit
String keyName = getBranchKey(branchName);
String lastCommit = config.getString(CONF_BRANCH, null, keyName);
List<RevCommit> revs;
if (StringUtils.isEmpty(lastCommit)) {
// new branch/unindexed branch, get all commits on branch
revs = JGitUtils.getRevLog(repository, branchName, 0, -1);
} else {
// pre-existing branch, get changes since last commit
revs = JGitUtils.getRevLog(repository, lastCommit, branchName);
}
if (revs.size() > 0) {
result.branchCount += 1;
}
// reverse the list of commits so we start with the first commit
Collections.reverse(revs);
for (RevCommit commit : revs) {
// index a commit
result.add(index(model.name, repository, branchName, commit));
}
// update the config
config.setString(CONF_ALIAS, null, keyName, branchName);
config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
config.save();
}
// unless a branch really was deleted and no longer exists
if (deletedBranches.size() > 0) {
for (String branch : deletedBranches) {
IndexWriter writer = getIndexWriter(model.name);
writer.deleteDocuments(new Term(FIELD_BRANCH, branch));
writer.commit();
}
}
result.success = true;
} catch (Throwable t) {
logger.error(MessageFormat.format("Exception while updating {0} Lucene index", model.name), t);
}
return result;
}
use of com.gitblit.models.RefModel in project gitblit by gitblit.
the class LuceneService method index.
/**
* Incrementally update the index with the specified commit for the
* repository.
*
* @param repositoryName
* @param repository
* @param branch
* the fully qualified branch name (e.g. refs/heads/master)
* @param commit
* @return true, if successful
*/
private IndexResult index(String repositoryName, Repository repository, String branch, RevCommit commit) {
IndexResult result = new IndexResult();
try {
String[] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
List<PathChangeModel> changedPaths = JGitUtils.getFilesInCommit(repository, commit);
String revDate = DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE);
IndexWriter writer = getIndexWriter(repositoryName);
for (PathChangeModel path : changedPaths) {
if (path.isSubmodule()) {
continue;
}
// delete the indexed blob
deleteBlob(repositoryName, branch, path.name);
// re-index the blob
if (!ChangeType.DELETE.equals(path.changeType)) {
result.blobCount++;
Document doc = new Document();
doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), StringField.TYPE_STORED));
doc.add(new Field(FIELD_BRANCH, branch, TextField.TYPE_STORED));
doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED));
doc.add(new Field(FIELD_PATH, path.path, TextField.TYPE_STORED));
doc.add(new Field(FIELD_DATE, revDate, StringField.TYPE_STORED));
doc.add(new Field(FIELD_AUTHOR, getAuthor(commit), TextField.TYPE_STORED));
doc.add(new Field(FIELD_COMMITTER, getCommitter(commit), TextField.TYPE_STORED));
// determine extension to compare to the extension
// blacklist
String ext = null;
String name = path.name.toLowerCase();
if (name.indexOf('.') > -1) {
ext = name.substring(name.lastIndexOf('.') + 1);
}
if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) {
// read the blob content
String str = JGitUtils.getStringContent(repository, commit.getTree(), path.path, encodings);
if (str != null) {
doc.add(new Field(FIELD_CONTENT, str, TextField.TYPE_STORED));
writer.addDocument(doc);
}
}
}
}
writer.commit();
// get any annotated commit tags
List<String> commitTags = new ArrayList<String>();
for (RefModel ref : JGitUtils.getTags(repository, false, -1)) {
if (ref.isAnnotatedTag() && ref.getReferencedObjectId().equals(commit.getId())) {
commitTags.add(ref.displayName);
}
}
// create and write the Lucene document
Document doc = createDocument(commit, commitTags);
doc.add(new Field(FIELD_BRANCH, branch, TextField.TYPE_STORED));
result.commitCount++;
result.success = index(repositoryName, doc);
} catch (Exception e) {
logger.error(MessageFormat.format("Exception while indexing commit {0} in {1}", commit.getId().getName(), repositoryName), e);
}
return result;
}
Aggregations