use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.
the class PatchsetReceivePack method updateReflog.
private void updateReflog(RefUpdate ru) {
if (ru == null) {
return;
}
ReceiveCommand.Type type = null;
switch(ru.getResult()) {
case NEW:
type = Type.CREATE;
break;
case FAST_FORWARD:
type = Type.UPDATE;
break;
case FORCED:
type = Type.UPDATE_NONFASTFORWARD;
break;
default:
LOGGER.error(MessageFormat.format("unexpected ref update type {0} for {1}", ru.getResult(), ru.getName()));
return;
}
ReceiveCommand cmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getName(), type);
RefLogUtils.updateRefLog(user, getRepository(), Arrays.asList(cmd));
}
use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.
the class MirrorService method run.
@Override
public void run() {
if (!isReady()) {
return;
}
running.set(true);
for (String repositoryName : repositoryManager.getRepositoryList()) {
if (forceClose.get()) {
break;
}
if (repositoryManager.isCollectingGarbage(repositoryName)) {
logger.debug("mirror is skipping {} garbagecollection", repositoryName);
continue;
}
RepositoryModel model = null;
Repository repository = null;
try {
model = repositoryManager.getRepositoryModel(repositoryName);
if (!model.isMirror && !model.isBare) {
// repository must be a valid bare git mirror
logger.debug("mirror is skipping {} !mirror !bare", repositoryName);
continue;
}
repository = repositoryManager.getRepository(repositoryName);
if (repository == null) {
logger.warn(MessageFormat.format("MirrorExecutor is missing repository {0}?!?", repositoryName));
continue;
}
// automatically repair (some) invalid fetch ref specs
if (!repairAttempted.contains(repositoryName)) {
repairAttempted.add(repositoryName);
JGitUtils.repairFetchSpecs(repository);
}
// find the first mirror remote - there should only be one
StoredConfig rc = repository.getConfig();
RemoteConfig mirror = null;
List<RemoteConfig> configs = RemoteConfig.getAllRemoteConfigs(rc);
for (RemoteConfig config : configs) {
if (config.isMirror()) {
mirror = config;
break;
}
}
if (mirror == null) {
// repository does not have a mirror remote
logger.debug("mirror is skipping {} no mirror remote found", repositoryName);
continue;
}
logger.debug("checking {} remote {} for ref updates", repositoryName, mirror.getName());
final boolean testing = false;
Git git = new Git(repository);
FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call();
Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();
if (refUpdates.size() > 0) {
ReceiveCommand ticketBranchCmd = null;
for (TrackingRefUpdate ru : refUpdates) {
StringBuilder sb = new StringBuilder();
sb.append("updated mirror ");
sb.append(repositoryName);
sb.append(" ");
sb.append(ru.getRemoteName());
sb.append(" -> ");
sb.append(ru.getLocalName());
if (ru.getResult() == Result.FORCED) {
sb.append(" (forced)");
}
sb.append(" ");
sb.append(ru.getOldObjectId() == null ? "" : ru.getOldObjectId().abbreviate(7).name());
sb.append("..");
sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());
logger.info(sb.toString());
if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {
ReceiveCommand.Type type = null;
switch(ru.getResult()) {
case NEW:
type = Type.CREATE;
break;
case FAST_FORWARD:
type = Type.UPDATE;
break;
case FORCED:
type = Type.UPDATE_NONFASTFORWARD;
break;
default:
type = null;
break;
}
if (type != null) {
ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getLocalName(), type);
}
}
}
if (ticketBranchCmd != null) {
repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));
}
}
} catch (Exception e) {
logger.error("Error updating mirror " + repositoryName, e);
} finally {
// cleanup
if (repository != null) {
repository.close();
}
}
}
running.set(false);
}
use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.
the class RefLogUtils method createIndex.
/**
* Creates an in-memory index of the reflog entry.
*
* @param repo
* @param headId
* @param commands
* @return an in-memory index
* @throws IOException
*/
private static DirCache createIndex(Repository repo, ObjectId headId, Collection<ReceiveCommand> commands) throws IOException {
DirCache inCoreIndex = DirCache.newInCore();
DirCacheBuilder dcBuilder = inCoreIndex.builder();
ObjectInserter inserter = repo.newObjectInserter();
long now = System.currentTimeMillis();
Set<String> ignorePaths = new TreeSet<String>();
try {
// add receive commands to the temporary index
for (ReceiveCommand command : commands) {
// use the ref names as the path names
String path = command.getRefName();
ignorePaths.add(path);
StringBuilder change = new StringBuilder();
change.append(command.getType().name()).append(' ');
switch(command.getType()) {
case CREATE:
change.append(ObjectId.zeroId().getName());
change.append(' ');
change.append(command.getNewId().getName());
break;
case UPDATE:
case UPDATE_NONFASTFORWARD:
change.append(command.getOldId().getName());
change.append(' ');
change.append(command.getNewId().getName());
break;
case DELETE:
change = null;
break;
}
if (change == null) {
// ref deleted
continue;
}
String content = change.toString();
// create an index entry for this attachment
final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setLength(content.length());
dcEntry.setLastModified(now);
dcEntry.setFileMode(FileMode.REGULAR_FILE);
// insert object
dcEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, content.getBytes("UTF-8")));
// add to temporary in-core index
dcBuilder.add(dcEntry);
}
// Traverse HEAD to add all other paths
TreeWalk treeWalk = new TreeWalk(repo);
int hIdx = -1;
if (headId != null)
hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
treeWalk.setRecursive(true);
while (treeWalk.next()) {
String path = treeWalk.getPathString();
CanonicalTreeParser hTree = null;
if (hIdx != -1)
hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
if (!ignorePaths.contains(path)) {
// add entries from HEAD for all other paths
if (hTree != null) {
// create a new DirCacheEntry with data retrieved from
// HEAD
final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setObjectId(hTree.getEntryObjectId());
dcEntry.setFileMode(hTree.getEntryFileMode());
// add to temporary in-core index
dcBuilder.add(dcEntry);
}
}
}
// release the treewalk
treeWalk.close();
// finish temporary in-core index used for this commit
dcBuilder.finish();
} finally {
inserter.close();
}
return inCoreIndex;
}
use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.
the class BranchTicketService method onRefsChanged.
/**
* Listen for tickets branch changes and (re)index tickets, as appropriate
*/
@Override
public synchronized void onRefsChanged(RefsChangedEvent event) {
if (!(event instanceof ReceiveCommandEvent)) {
return;
}
ReceiveCommandEvent branchUpdate = (ReceiveCommandEvent) event;
RepositoryModel repository = branchUpdate.model;
ReceiveCommand cmd = branchUpdate.cmd;
try {
switch(cmd.getType()) {
case CREATE:
case UPDATE_NONFASTFORWARD:
// reindex everything
reindex(repository);
break;
case UPDATE:
// incrementally index ticket updates
resetCaches(repository);
long start = System.nanoTime();
log.info("incrementally indexing {} ticket branch due to received ref update", repository.name);
Repository db = repositoryManager.getRepository(repository.name);
try {
Set<Long> ids = new HashSet<Long>();
List<PathChangeModel> paths = JGitUtils.getFilesInRange(db, cmd.getOldId().getName(), cmd.getNewId().getName());
for (PathChangeModel path : paths) {
String name = path.name.substring(path.name.lastIndexOf('/') + 1);
if (!JOURNAL.equals(name)) {
continue;
}
String tid = path.path.split("/")[2];
long ticketId = Long.parseLong(tid);
if (!ids.contains(ticketId)) {
ids.add(ticketId);
TicketModel ticket = getTicket(repository, ticketId);
log.info(MessageFormat.format("indexing ticket #{0,number,0}: {1}", ticketId, ticket.title));
indexer.index(ticket);
}
}
long end = System.nanoTime();
log.info("incremental indexing of {0} ticket(s) completed in {1} msecs", ids.size(), TimeUnit.NANOSECONDS.toMillis(end - start));
} finally {
db.close();
}
break;
default:
log.warn("Unexpected receive type {} in BranchTicketService.onRefsChanged" + cmd.getType());
break;
}
} catch (Exception e) {
log.error("failed to reindex " + repository.name, e);
}
}
use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.
the class GroovyScriptTest method testProtectRefsFastForward.
@Test
public void testProtectRefsFastForward() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(0, logger.messages.size());
}
Aggregations