public class SVNLogRunner {
private boolean myIsEntriesChanged;
private boolean myIsWCPropertiesChanged;
public void runCommand(SVNAdminArea adminArea, String name, Map attributes, int count) throws SVNException {
SVNException error = null;
String fileName = (String) attributes.get(SVNLog.NAME_ATTR);
if (SVNLog.DELETE_ENTRY.equals(name)) {
File path = adminArea.getFile(fileName);
SVNAdminArea dir = adminArea.getWCAccess().probeRetrieve(path);
SVNEntry entry = dir.getWCAccess().getEntry(path, false);
if (entry == null) {
return;
}
try {
if (entry.isDirectory()) {
try {
SVNAdminArea childDir = dir.getWCAccess().retrieve(path);
// it should be null when there is no dir already.
if (childDir != null) {
childDir.removeFromRevisionControl(childDir.getThisDirName(), true, false);
} else {
SVNErrorManager.error(SVNErrorMessage.create(SVNErrorCode.WC_NOT_LOCKED));
}
} catch (SVNException e) {
if (e.getErrorMessage().getErrorCode() == SVNErrorCode.WC_NOT_LOCKED) {
if (!entry.isScheduledForAddition()) {
adminArea.deleteEntry(fileName);
adminArea.saveEntries(false);
}
} else {
throw e;
}
}
} else if (entry.isFile()) {
adminArea.removeFromRevisionControl(fileName, true, false);
}
} catch (SVNException e) {
if (e.getErrorMessage().getErrorCode() != SVNErrorCode.WC_LEFT_LOCAL_MOD) {
error = e;
}
}
} else if (SVNLog.MODIFY_ENTRY.equals(name)) {
try {
Map entryAttrs = new HashMap(attributes);
entryAttrs.remove("");
entryAttrs.remove(SVNLog.NAME_ATTR);
if (entryAttrs.containsKey(SVNProperty.shortPropertyName(SVNProperty.TEXT_TIME))) {
String value = (String) entryAttrs.get(SVNProperty.shortPropertyName(SVNProperty.TEXT_TIME));
if (SVNLog.WC_TIMESTAMP.equals(value)) {
File file = adminArea.getFile(fileName);
value = SVNTimeUtil.formatDate(new Date(file.lastModified()));
entryAttrs.put(SVNProperty.shortPropertyName(SVNProperty.TEXT_TIME), value);
}
}
if (entryAttrs.containsKey(SVNProperty.shortPropertyName(SVNProperty.PROP_TIME))) {
String value = (String) entryAttrs.get(SVNProperty.shortPropertyName(SVNProperty.PROP_TIME));
if (SVNLog.WC_TIMESTAMP.equals(value)) {
SVNEntry entry = adminArea.getEntry(fileName, false);
if (entry == null) {
return;
}
value = adminArea.getPropertyTime(fileName);
entryAttrs.put(SVNProperty.shortPropertyName(SVNProperty.PROP_TIME), value);
}
}
try {
adminArea.modifyEntry(fileName, entryAttrs, false, false);
} catch (SVNException svne) {
SVNErrorCode code = count <= 1 ? SVNErrorCode.WC_BAD_ADM_LOG_START : SVNErrorCode.WC_BAD_ADM_LOG;
SVNErrorMessage err = SVNErrorMessage.create(code, "Error modifying entry for ''{0}''", fileName);
SVNErrorManager.error(err, svne);
}
setEntriesChanged(true);
} catch (SVNException svne) {
error = svne;
}
} else if (SVNLog.MODIFY_WC_PROPERTY.equals(name)) {
try {
SVNVersionedProperties wcprops = adminArea.getWCProperties(fileName);
if (wcprops != null) {
String propName = (String) attributes .get(SVNLog.PROPERTY_NAME_ATTR);
String propValue = (String) attributes.get(SVNLog.PROPERTY_VALUE_ATTR);
wcprops.setPropertyValue(propName, propValue);
setWCPropertiesChanged(true);
}
} catch (SVNException svne) {
error = svne;
}
} else if (SVNLog.DELETE_LOCK.equals(name)) {
try {
SVNEntry entry = adminArea.getEntry(fileName, true);
if (entry != null) {
entry.setLockToken(null);
entry.setLockOwner(null);
entry.setLockCreationDate(null);
entry.setLockComment(null);
setEntriesChanged(true);
}
} catch (SVNException svne) {
SVNErrorCode code = count <= 1 ? SVNErrorCode.WC_BAD_ADM_LOG_START : SVNErrorCode.WC_BAD_ADM_LOG;
SVNErrorMessage err = SVNErrorMessage.create(code, "Error removing lock from entry for ''{0}''", fileName);
error = new SVNException(err, svne);
}
} else if (SVNLog.DELETE.equals(name)) {
File file = adminArea.getFile(fileName);
SVNFileUtil.deleteFile(file);
} else if (SVNLog.READONLY.equals(name)) {
File file = adminArea.getFile(fileName);
SVNFileUtil.setReadonly(file, true);
} else if (SVNLog.MOVE.equals(name)) {
File src = adminArea.getFile(fileName);
File dst = adminArea.getFile((String) attributes.get(SVNLog.DEST_ATTR));
try {
SVNFileUtil.rename(src, dst);
} catch (SVNException svne) {
error = new SVNException(svne.getErrorMessage().wrap("Can't move source to dest"), svne);
}
} else if (SVNLog.APPEND.equals(name)) {
File src = adminArea.getFile(fileName);
File dst = adminArea.getFile((String) attributes.get(SVNLog.DEST_ATTR));
OutputStream os = null;
InputStream is = null;
try {
os = SVNFileUtil.openFileForWriting(dst, true);
is = SVNFileUtil.openFileForReading(src);
while (true) {
int r = is.read();
if (r < 0) {
break;
}
os.write(r);
}
} catch (IOException e) {
if (src.exists()) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Cannot write to ''{0}'': {1}", new Object[] {dst, e.getLocalizedMessage()});
error = new SVNException(err, e);
}
} catch (SVNException svne) {
if (src.exists()) {
error = svne;
}