Skip to content

The Unified and Autonomous Development Platform

Refined file deletion.

robin shine5cf559de9ee1
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
index e3ccf33924..d32a348ea4 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
@@ -36,33 +36,26 @@ public class FileEdit implements Serializable {
private final String oldPath;
- private final String newPath;
+ private final File newFile;
- private final byte[] content;
-
- public FileEdit(@Nullable String oldPath, @Nullable String newPath, byte[] content) {
+ public FileEdit(@Nullable String oldPath, @Nullable File newFile) {
this.oldPath = GitUtils.normalizePath(oldPath);
- this.newPath = GitUtils.normalizePath(newPath);
- Preconditions.checkArgument(this.oldPath != null || this.newPath != null,
- "Either oldPath or newPath should be defined");
-
- this.content = content;
+ this.newFile = newFile;
+
+ Preconditions.checkArgument(this.oldPath != null || this.newFile != null,
+ "Either oldPath or newFile should be defined");
}
public String getOldPath() {
return oldPath;
}
- public String getNewPath() {
- return newPath;
- }
-
- public byte[] getContent() {
- return content;
+ public File getNewFile() {
+ return newFile;
}
private ObjectId insertTree(RevTree revTree, TreeWalk treeWalk, ObjectInserter inserter,
- @Nullable String currentOldPath, @Nullable String currentNewPath, byte[] content) {
+ @Nullable String currentOldPath, @Nullable String currentNewPath) {
try {
TreeFormatter formatter = new TreeFormatter();
boolean appended = false;
@@ -76,7 +69,7 @@ public class FileEdit implements Serializable {
oldPathFound = true;
if (name.equals(currentNewPath)) {
newPathFound = true;
- ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content);
+ ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());
formatter.append(name, FileMode.REGULAR_FILE, blobId);
appended = true;
}
@@ -95,7 +88,8 @@ public class FileEdit implements Serializable {
} else {
childNewPath = null;
}
- ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter, childOldPath, childNewPath, content);
+ ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter,
+ childOldPath, childNewPath);
if (childTreeId != null) {
formatter.append(name, FileMode.TREE, childTreeId);
appended = true;
@@ -104,11 +98,12 @@ public class FileEdit implements Serializable {
if ((treeWalk.getFileMode(0).getBits() & FileMode.TYPE_TREE) == 0)
throw new NotTreeException("Path does not represent a tree: " + treeWalk.getPathString());
newPathFound = true;
- TreeWalk childTreeWalk = TreeWalk.forPath(treeWalk.getObjectReader(), treeWalk.getPathString(), revTree);
+ TreeWalk childTreeWalk = TreeWalk.forPath(treeWalk.getObjectReader(),
+ treeWalk.getPathString(), revTree);
Preconditions.checkNotNull(childTreeWalk);
childTreeWalk.enterSubtree();
String childNewPath = currentNewPath.substring(name.length()+1);
- ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter, null, childNewPath, content);
+ ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter, null, childNewPath);
if (childTreeId != null) {
formatter.append(name, treeWalk.getFileMode(0), childTreeId);
appended = true;
@@ -132,7 +127,7 @@ public class FileEdit implements Serializable {
for (int i=splitted.size()-1; i>=0; i--) {
if (childId == null) {
childName = splitted.get(i);
- childId = inserter.insert(Constants.OBJ_BLOB, content);
+ childId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());
childMode = FileMode.REGULAR_FILE;
} else {
TreeFormatter childFormatter = new TreeFormatter();
@@ -194,7 +189,13 @@ public class FileEdit implements Serializable {
RevTree revTree = revWalk.parseCommit(parentCommitId).getTree();
treeWalk.addTree(revTree);
CommitBuilder commit = new CommitBuilder();
- commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newPath, content));
+
+ String newPath;
+ if (newFile != null)
+ newPath = newFile.getPath();
+ else
+ newPath = null;
+ commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newPath));
commit.setAuthor(authorAndCommitter);
commit.setCommitter(authorAndCommitter);
commit.setParentId(parentCommitId);
@@ -220,4 +221,27 @@ public class FileEdit implements Serializable {
throw new RuntimeException(e);
}
}
+
+ public static class File implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String path;
+
+ private final byte[] content;
+
+ public File(String path, byte[] content) {
+ this.path = Preconditions.checkNotNull(GitUtils.normalizePath(path));
+ this.content = content;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public byte[] getContent() {
+ return content;
+ }
+
+ }
}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
index 9e49d33aae..62d318c4b6 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
@@ -40,7 +40,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("/server/src/com/example/a//a.java", null, null);
+ FileEdit edit = new FileEdit("/server/src/com/example/a//a.java", null);
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -69,7 +69,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("/server/src/com/example/c//c.java", null, null);
+ FileEdit edit = new FileEdit("/server/src/com/example/c//c.java", null);
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");
assertTrue("An ObjectNotExistException should be thrown", false);
@@ -94,7 +94,8 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("server/src/com/example/a/a.java", "client/c.java", "a".getBytes());
+ FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
+ new FileEdit.File("client/c.java", "a".getBytes()));
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename");
try (RevWalk revWalk = new RevWalk(repo)) {
RevTree revTree = revWalk.parseCommit(newCommitId).getTree();
@@ -123,7 +124,8 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("server/src/com/example/a", "client/c.java", "a".getBytes());
+ FileEdit edit = new FileEdit("server/src/com/example/a",
+ new FileEdit.File("client/c.java", "a".getBytes()));
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotFileException should be thrown", false);
@@ -148,7 +150,8 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("server/src/com/example/a/a.java", "client/a.java/a.java", "a".getBytes());
+ FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
+ new FileEdit.File("client/a.java/a.java", "a".getBytes()));
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotTreeException should be thrown", false);
@@ -173,7 +176,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
+ FileEdit edit = new FileEdit(null, new FileEdit.File("/server/src/com/example/c/c.java", "c".getBytes()));
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -186,7 +189,7 @@ public class FileEditTest extends AbstractGitTest {
}
oldCommitId = newCommitId;
- edit = new FileEdit(null, "/common/common.java", "common".getBytes());
+ edit = new FileEdit(null, new FileEdit.File("/common/common.java", "common".getBytes()));
newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -217,7 +220,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, "client/a.java", "a".getBytes());
+ FileEdit edit = new FileEdit(null, new FileEdit.File("client/a.java", "a".getBytes()));
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("An ObjectAlreadyExistException should be thrown", false);
@@ -239,7 +242,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId newCommitId = repo.resolve(refName);
- FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
+ FileEdit edit = new FileEdit(null, new FileEdit.File("/server/src/com/example/c/c.java", "c".getBytes()));
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
assertTrue("An ObsoleteCommitException should be thrown", false);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
index 86f193a451..325e44ebe4 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
@@ -18,6 +18,7 @@ import org.apache.wicket.markup.html.link.ResourceLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.eclipse.jgit.internal.storage.file.FileRepository;
@@ -29,6 +30,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.BlobIdent;
+import com.pmease.commons.git.FileEdit;
import com.pmease.commons.git.Git;
import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;
import com.pmease.gitplex.core.model.Repository;
@@ -203,17 +205,19 @@ public abstract class BlobViewPanel extends Panel {
ObjectId commitId = context.getRepository().getObjectId(
context.getState().file.revision, true);
- final BlobIdent blobIdent = context.getState().file;
- EditSavePanel saveChangePanel = new EditSavePanel(panelId, repoModel, blobIdent, commitId, null) {
+ final BlobIdent file = context.getState().file;
+ String refName = Git.REFS_HEADS + file.revision;
+ EditSavePanel saveChangePanel = new EditSavePanel(panelId, repoModel, refName,
+ file.path, Model.of((FileEdit.File)null), commitId) {
@Override
protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
Repository repo = context.getRepository();
- repo.cacheObjectId(blobIdent.revision, newCommitId);
+ repo.cacheObjectId(file.revision, newCommitId);
try ( FileRepository jgitRepo = repo.openAsJGitRepo();
RevWalk revWalk = new RevWalk(jgitRepo)) {
RevTree revTree = revWalk.parseCommit(newCommitId).getTree();
- String parentPath = StringUtils.substringBeforeLast(blobIdent.path, "/");
+ String parentPath = StringUtils.substringBeforeLast(file.path, "/");
while (TreeWalk.forPath(jgitRepo, parentPath, revTree) == null) {
if (parentPath.contains("/")) {
parentPath = StringUtils.substringBeforeLast(parentPath, "/");
@@ -222,7 +226,7 @@ public abstract class BlobViewPanel extends Panel {
break;
}
}
- BlobIdent parentBlobIdent = new BlobIdent(blobIdent.revision, parentPath, FileMode.TREE.getBits());
+ BlobIdent parentBlobIdent = new BlobIdent(file.revision, parentPath, FileMode.TREE.getBits());
context.onSelect(target, parentBlobIdent, null);
} catch (IOException e) {
throw new RuntimeException(e);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
index fa1b115264..348eabe612 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
@@ -1,5 +1,5 @@
<wicket:panel>
- <div class="save-change autofit">
+ <div class="edit-save autofit">
<div class="head">
Add New Commit
</div>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
index 7da8a61d43..bff94b8c12 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
@@ -41,9 +41,11 @@ public abstract class EditSavePanel extends Panel {
private final IModel<Repository> repoModel;
- private final IModel<String> refModel;
+ private final String refName;
- private final IModel<FileEdit> editModel;
+ private final String oldPath;
+
+ private final IModel<FileEdit.File> newFileModel;
private ObjectId prevCommitId;
@@ -53,27 +55,28 @@ public abstract class EditSavePanel extends Panel {
private String detailCommitMessage;
- public EditSavePanel(String id, IModel<Repository> repoModel, IModel<String> refModel,
- IModel<FileEdit> editModel, ObjectId prevCommitId) {
+ public EditSavePanel(String id, IModel<Repository> repoModel, String refName,
+ String oldPath, IModel<FileEdit.File> newFileModel, ObjectId prevCommitId) {
super(id);
this.repoModel = repoModel;
- this.refModel = refModel;
- this.editModel = editModel;
+ this.refName = refName;
+ this.oldPath = oldPath;
+ this.newFileModel = newFileModel;
this.prevCommitId = prevCommitId;
}
private String getDefaultCommitMessage() {
- FileEdit edit = editModel.getObject();
- if (edit.getNewPath() != null) {
- if (edit.getNewPath().equals(edit.getOldPath()))
- return "Edit " + edit.getOldPath();
- else if (edit.getOldPath() != null)
- return "Rename " + edit.getOldPath();
+ FileEdit.File newFile = newFileModel.getObject();
+ if (newFile != null) {
+ if (newFile.getPath().equals(oldPath))
+ return "Edit " + oldPath;
+ else if (oldPath != null)
+ return "Rename " + oldPath;
else
- return "Add " + edit.getNewPath();
+ return "Add " + newFile.getPath();
} else {
- return "Delete " + edit.getOldPath();
+ return "Delete " + oldPath;
}
}
@@ -159,11 +162,11 @@ public abstract class EditSavePanel extends Panel {
commitMessage += "\n\n" + detailCommitMessage;
User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());
- FileEdit edit = editModel.getObject();
+ FileEdit edit = new FileEdit(oldPath, newFileModel.getObject());
ObjectId newCommitId = null;
while(newCommitId == null) {
try {
- newCommitId = edit.commit(jgitRepo, refModel.getObject(),
+ newCommitId = edit.commit(jgitRepo, refName,
prevCommitId, prevCommitId, user.asPerson(), commitMessage);
} catch (ObsoleteCommitException e) {
currentCommitId = e.getOldCommitId();
@@ -180,34 +183,37 @@ public abstract class EditSavePanel extends Panel {
if (treeWalk != null) {
if (!treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))
|| !treeWalk.getFileMode(0).equals(treeWalk.getFileMode(1))) {
- hasChangesContainer.setVisibilityAllowed(true);
if (treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {
- if (edit.getNewPath() != null) {
- edit = new FileEdit(null, edit.getNewPath(), edit.getContent());
+ if (edit.getNewFile() != null) {
+ edit = new FileEdit(null, edit.getNewFile());
+ hasChangesContainer.setVisibilityAllowed(true);
+ target.add(hasChangesContainer);
+ break;
} else {
newCommitId = currentCommitId;
break;
}
+ } else {
+ hasChangesContainer.setVisibilityAllowed(true);
+ target.add(hasChangesContainer);
+ break;
}
}
}
}
- if (edit.getNewPath() != null && !edit.getNewPath().equals(edit.getOldPath())) {
- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getNewPath(),
+ if (edit.getNewFile() != null && !edit.getNewFile().getPath().equals(edit.getOldPath())) {
+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getNewFile().getPath(),
prevCommit.getTree().getId(), currentCommit.getTree().getId());
if (treeWalk != null) {
if (!treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))
|| !treeWalk.getFileMode(0).equals(treeWalk.getFileMode(1))) {
hasChangesContainer.setVisibilityAllowed(true);
+ target.add(hasChangesContainer);
+ break;
}
}
}
- if (hasChangesContainer.isVisibilityAllowed()) {
- target.add(hasChangesContainer);
- break;
- }
-
} catch (IOException e2) {
throw new RuntimeException(e2);
}
@@ -256,8 +262,7 @@ public abstract class EditSavePanel extends Panel {
@Override
protected void onDetach() {
repoModel.detach();
- refModel.detach();
- editModel.detach();
+ newFileModel.detach();
super.onDetach();
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.css
index 73f4824024..6e1d9aac17 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.css
@@ -1,8 +1,8 @@
-.save-change {
+.edit-save {
padding: 20px;
overflow: auto;
}
-.save-change>.head {
+.edit-save>.head {
font-weight: bold;
font-size: 16px;
margin-bottom: 20px;