use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class GitMoveDeleteHook method deleteFile.
@Override
public boolean deleteFile(final IResourceTree tree, final IFile file, final int updateFlags, final IProgressMonitor monitor) {
if (!org.eclipse.egit.core.Activator.autoStageDeletion()) {
return false;
}
// Linked resources are not files, hence not tracked by git
if (file.isLinked())
return false;
final boolean force = (updateFlags & IResource.FORCE) == IResource.FORCE;
if (!force && !tree.isSynchronized(file, IResource.DEPTH_ZERO))
return false;
final RepositoryMapping map = RepositoryMapping.getMapping(file);
if (map == null)
return false;
String repoRelativePath = map.getRepoRelativePath(file);
IndexDiffCache indexDiffCache = Activator.getDefault().getIndexDiffCache();
IndexDiffCacheEntry indexDiffCacheEntry = indexDiffCache.getIndexDiffCacheEntry(map.getRepository());
if (indexDiffCacheEntry == null) {
return false;
}
IndexDiffData indexDiff = indexDiffCacheEntry.getIndexDiff();
if (indexDiff != null) {
if (indexDiff.getUntracked().contains(repoRelativePath))
return false;
if (indexDiff.getIgnoredNotInIndex().contains(repoRelativePath))
return false;
}
if (!file.exists())
return false;
if (file.isDerived())
return false;
DirCache dirc = null;
try {
dirc = map.getRepository().lockDirCache();
final int first = dirc.findEntry(repoRelativePath);
if (first < 0) {
dirc.unlock();
return false;
}
final DirCacheBuilder edit = dirc.builder();
if (first > 0)
edit.keep(0, first);
final int next = dirc.nextEntry(first);
if (next < dirc.getEntryCount())
edit.keep(next, dirc.getEntryCount() - next);
if (!edit.commit())
tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, CoreText.MoveDeleteHook_operationError, null));
tree.standardDeleteFile(file, updateFlags, monitor);
} catch (LockFailedException e) {
// FIXME The index is currently locked. This notably happens during
// rebase operations. auto-staging deletions should be queued... and
// the queued job will have to double-check whether the file has
// truly been deleted or if it was only deleted to be replaced by
// another version.
// This hook only exists to automatically add changes to the index.
// If the index is currently locked, do not accept the
// responsibility of deleting the file, return false to tell the
// workspace it can continue with the standard deletion. The user
// will have to stage the deletion later on _if_ this was truly
// needed, which won't happen for calls triggered by merge
// operations from the merge strategies.
Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.getPluginId(), MessageFormat.format(CoreText.MoveDeleteHook_cannotAutoStageDeletion, file.getLocation())));
return FINISH_FOR_ME;
} catch (IOException e) {
tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, CoreText.MoveDeleteHook_operationError, e));
} finally {
if (dirc != null)
dirc.unlock();
}
return true;
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class GitMoveDeleteHook method mapProject.
private void mapProject(final IProject source, final IProjectDescription description, final IProgressMonitor monitor, IPath gitDir) throws CoreException, TeamException {
IProject destination = source.getWorkspace().getRoot().getProject(description.getName());
RepositoryMapping repositoryMapping = RepositoryMapping.create(destination, gitDir.toFile());
if (repositoryMapping != null) {
GitProjectData projectData = new GitProjectData(destination);
projectData.setRepositoryMappings(Arrays.asList(repositoryMapping));
projectData.store();
GitProjectData.add(destination, projectData);
RepositoryProvider.map(destination, GitProvider.class.getName());
destination.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class GitMoveDeleteHook method moveProject.
@Override
public boolean moveProject(final IResourceTree tree, final IProject source, final IProjectDescription description, final int updateFlags, final IProgressMonitor monitor) {
final RepositoryMapping srcm = RepositoryMapping.getMapping(source);
if (srcm == null)
return false;
IPath newLocation = null;
if (description.getLocationURI() != null)
newLocation = URIUtil.toPath(description.getLocationURI());
else
newLocation = source.getWorkspace().getRoot().getLocation().append(description.getName());
IPath sourceLocation = source.getLocation();
// Prevent a serious error.
if (sourceLocation.isPrefixOf(newLocation) && sourceLocation.segmentCount() != newLocation.segmentCount() && !"true".equals(System.getProperty("egit.assume_307140_fixed"))) {
// $NON-NLS-1$//$NON-NLS-2$
// Graceful handling of bug, i.e. refuse to destroy your code
tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, // $NON-NLS-1$
"Cannot move project. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=307140 (not resolved in 3.7)", null));
return true;
}
File newLocationFile = newLocation.toFile();
// check if new location is below the same repository
Path workTree = new Path(srcm.getRepository().getWorkTree().getAbsolutePath());
int matchingFirstSegments = workTree.matchingFirstSegments(newLocation);
if (matchingFirstSegments == workTree.segmentCount()) {
return moveProjectHelperMoveOnlyProject(tree, source, description, updateFlags, monitor, srcm, newLocationFile);
} else {
int dstAboveSrcRepo = newLocation.matchingFirstSegments(srcm.getGitDirAbsolutePath());
int srcAboveSrcRepo = sourceLocation.matchingFirstSegments(srcm.getGitDirAbsolutePath());
if (dstAboveSrcRepo > 0 && srcAboveSrcRepo > 0) {
return moveProjectHelperMoveRepo(tree, source, description, updateFlags, monitor, srcm, newLocation, sourceLocation);
} else {
return FINISH_FOR_ME;
}
}
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class GitProjectSetCapability method asReference.
@Nullable
private String asReference(IProject project) throws TeamException {
RepositoryMapping mapping = RepositoryMapping.getMapping(project);
if (mapping == null) {
return null;
}
String branch;
try {
branch = mapping.getRepository().getBranch();
} catch (IOException e) {
throw new TeamException(NLS.bind(CoreText.GitProjectSetCapability_ExportCouldNotGetBranch, project.getName()));
}
StoredConfig config = mapping.getRepository().getConfig();
String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
String url = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote, ConfigConstants.CONFIG_KEY_URL);
if (url == null)
throw new TeamException(NLS.bind(CoreText.GitProjectSetCapability_ExportNoRemote, project.getName()));
String projectPath = mapping.getRepoRelativePath(project);
if (projectPath == null) {
return null;
}
if (// $NON-NLS-1$
projectPath.equals(""))
// $NON-NLS-1$
projectPath = ".";
return asReference(url, branch, projectPath);
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class GitModelSynchronizeParticipant method saveState.
@Override
public void saveState(IMemento memento) {
super.saveState(memento);
for (GitSynchronizeData gsd : gsds) {
Repository repo = gsd.getRepository();
RepositoryMapping mapping = RepositoryMapping.findRepositoryMapping(repo);
if (mapping != null) {
IMemento child = memento.createChild(DATA_NODE_KEY);
child.putString(CONTAINER_PATH_KEY, getPathForResource(mapping.getContainer()));
child.putString(SRC_REV_KEY, gsd.getSrcRev());
child.putString(DST_REV_KEY, gsd.getDstRev());
child.putBoolean(INCLUDE_LOCAL_KEY, gsd.shouldIncludeLocal());
Set<IResource> includedResources = gsd.getIncludedResources();
if (includedResources != null && !includedResources.isEmpty()) {
IMemento paths = child.createChild(INCLUDED_PATHS_NODE_KEY);
for (IResource resource : includedResources) {
String path = getPathForResource(resource);
paths.createChild(INCLUDED_PATH_KEY).putString(INCLUDED_PATH_KEY, path);
}
}
}
}
memento.putBoolean(FORCE_FETCH_KEY, gsds.forceFetch());
}
Aggregations