boolean revertFile = false;
boolean removeFromRevisionControl = false;
boolean unlinkFile = false;
boolean cleanUp = false;
boolean ignoreExternals = isIgnoreExternals();
SVNAdminArea targetArea = null;
try {
try {
targetArea = wcAccess.retrieve(anchor);
} catch (SVNException svne) {
SVNErrorMessage err = svne.getErrorMessage();
if (err.getErrorCode() == SVNErrorCode.WC_NOT_LOCKED) {
SVNWCAccess targetAccess = SVNWCAccess.newInstance(null);
targetArea = targetAccess.open(anchor, true, 1);
closeTarget = true;
SVNURL dstWCReposRootURL = getReposRoot(anchor, null, SVNRevision.BASE, targetArea, targetAccess);
if (!reposRootURL.equals(dstWCReposRootURL)) {
SVNErrorMessage err1 = SVNErrorMessage.create(SVNErrorCode.RA_REPOS_ROOT_URL_MISMATCH,
"Cannot insert a file external from ''{0}'' into a working copy from a different repository rooted at ''{1}''",
new Object[] { url, dstWCReposRootURL });
SVNErrorManager.error(err1, SVNLogType.WC);
}
} else {
throw svne;
}
}
if (targetArea.getFormatVersion() < SVNAdminArea16.WC_FORMAT) {
dispatchEvent(SVNEventFactory.createSVNEvent(path, SVNNodeKind.FILE, null, SVNRepository.INVALID_REVISION,
SVNEventAction.SKIP, SVNEventAction.UPDATE_EXTERNAL, null, null));
return;
}
SVNEntry entry = targetArea.getEntry(target, false);
if (entry != null) {
if (entry.getExternalFilePath() == null) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.CLIENT_FILE_EXTERNAL_OVERWRITE_VERSIONED,
"The file external from ''{0}'' cannot overwrite the existing versioned item at ''{1}''",
new Object[] { url, path });
SVNErrorManager.error(err, SVNLogType.WC);
}
} else {
targetArea.getVersionedEntry(targetArea.getThisDirName(), false);
boolean hasPropConflicts = targetArea.hasPropConflict(targetArea.getThisDirName());
boolean hasTreeConflicts = targetArea.hasTreeConflict(targetArea.getThisDirName());
if (hasPropConflicts || hasTreeConflicts) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.WC_FOUND_CONFLICT,
"The file external from ''{0}'' cannot be written to ''{1}'' while ''{2}'' remains in conflict",
new Object[] { url, path, anchor });
SVNErrorManager.error(err, SVNLogType.WC);
}
if (!path.exists()) {
SVNFileUtil.createEmptyFile(path);
unlinkFile = true;
}
ISVNEventHandler eventHandler = targetArea.getWCAccess().getEventHandler();
try {
targetArea.getWCAccess().setEventHandler(null);
SVNWCManager.add(path, targetArea, null, SVNRepository.INVALID_REVISION, SVNDepth.INFINITY);
} catch (SVNException svne) {
cleanUp = true;
throw svne;
} finally {
if (eventHandler != null) {
targetArea.getWCAccess().setEventHandler(eventHandler);
}
}
revertFile = true;
try {
targetArea.setFileExternalLocation(target, url, pegRevision, revision, reposRootURL);
} catch (SVNException svne) {
cleanUp = true;
throw svne;
}
}
setIgnoreExternals(true);
try {
doSwitchImpl(targetArea.getWCAccess(), path, url, pegRevision, revision, SVNDepth.EMPTY, false, false);
} catch (SVNException svne) {
cleanUp = true;
throw svne;
}
if (unlinkFile) {
revertFile = false;
removeFromRevisionControl = true;
}
} catch (SVNException svne) {
if (cleanUp) {
if (revertFile) {
SVNWCClient wcClient = new SVNWCClient(getRepositoryPool(), getOptions());
try {
wcClient.doRevert(new File[] { path }, SVNDepth.EMPTY, null);
} catch (SVNException svne2) {
//ignore
}
}
if (removeFromRevisionControl) {
try {
targetArea.removeFromRevisionControl(target, true, false);
} catch (SVNException svne2) {
//ignore
}
}
if (unlinkFile) {
try {
SVNFileUtil.deleteFile(path);
} catch (SVNException svne2) {
//ignore
}
}
}
throw svne;
} finally {
setIgnoreExternals(ignoreExternals);
if (closeTarget) {
targetArea.getWCAccess().close();
}
}
}