Make change types explicit

Without explicit change types, we cannot tell copy and rename apart.
This commit is contained in:
René Pfeuffer
2020-05-19 23:08:19 +02:00
parent 5c7491c254
commit 054f320455
5 changed files with 81 additions and 28 deletions

View File

@@ -26,7 +26,6 @@ package sonia.scm.api.v2.resources;
import com.github.sdorra.spotter.ContentTypes;
import com.github.sdorra.spotter.Language;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffFile;
@@ -42,7 +41,7 @@ import java.util.OptionalInt;
import static de.otto.edison.hal.Links.linkingTo;
/**
* TODO conflicts, copy and rename
* TODO conflicts
*/
class DiffResultToDiffResultDtoMapper {
@@ -83,21 +82,29 @@ class DiffResultToDiffResultDtoMapper {
String oldPath = file.getOldPath();
String path;
if (isFilePath(newPath) && isFileNull(oldPath)) {
path = newPath;
dto.setType("add");
} else if (isFileNull(newPath) && isFilePath(oldPath)) {
path = oldPath;
dto.setType("delete");
} else if (!newPath.equals(oldPath)) {
path = newPath;
dto.setType("rename");
} else if (isFilePath(newPath) && isFilePath(oldPath)) {
path = newPath;
dto.setType("modify");
} else {
// TODO copy?
throw new IllegalStateException("no file without path");
switch (file.getChangeType()) {
case ADD:
path = newPath;
dto.setType("add");
break;
case DELETE:
path = oldPath;
dto.setType("delete");
break;
case RENAME:
path = newPath;
dto.setType("rename");
break;
case MODIFY:
path = newPath;
dto.setType("modify");
break;
case COPY:
path = newPath;
dto.setType("copy");
break;
default:
throw new IllegalArgumentException("unknown change type: " + file.getChangeType());
}
dto.setNewPath(newPath);
@@ -119,14 +126,6 @@ class DiffResultToDiffResultDtoMapper {
return dto;
}
private boolean isFilePath(String path) {
return !isFileNull(path);
}
private boolean isFileNull(String path) {
return Strings.isNullOrEmpty(path) || "/dev/null".equals(path);
}
private DiffResultDto.HunkDto mapHunk(Hunk hunk) {
DiffResultDto.HunkDto dto = new DiffResultDto.HunkDto();
dto.setContent(hunk.getRawHeader());