The Unified and Autonomous Development Platform
Add PathAndContent class
robin shine350877b1e1e8
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.javaindex 6af6b6b658..576f56b604 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@@ -20,7 +20,6 @@ import org.eclipse.jgit.revwalk.RevTree;import org.eclipse.jgit.revwalk.RevWalk;import org.eclipse.jgit.treewalk.TreeWalk;-import com.google.common.base.Charsets;import com.google.common.base.Objects;import com.google.common.base.Preconditions;import com.google.common.base.Splitter;@@ -37,29 +36,22 @@ public class FileEdit implements Serializable {private final String oldPath;- private final String newPath;+ private final PathAndContent newFile;- private final String content;-- public FileEdit(@Nullable String oldPath, @Nullable String newPath, String content) {+ public FileEdit(@Nullable String oldPath, @Nullable PathAndContent newFile) {this.oldPath = GitUtils.normalizePath(oldPath);- this.newPath= GitUtils.normalizePath(newPath);- this.content = content;+ this.newFile = newFile;- Preconditions.checkArgument(this.oldPath != null || this.newPath != null,- "Either oldPath or newPath should be defined");+ 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 String getContent() {- return content;+ public PathAndContent getNewFile() {+ return newFile;}private ObjectId insertTree(RevTree revTree, TreeWalk treeWalk, ObjectInserter inserter,@@ -77,7 +69,7 @@ public class FileEdit implements Serializable {oldPathFound = true;if (name.equals(currentNewPath)) {newPathFound = true;- ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content.getBytes(Charsets.UTF_8));+ ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());formatter.append(name, FileMode.REGULAR_FILE, blobId);appended = true;}@@ -135,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.getBytes(Charsets.UTF_8));+ childId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());childMode = FileMode.REGULAR_FILE;} else {TreeFormatter childFormatter = new TreeFormatter();@@ -198,7 +190,7 @@ public class FileEdit implements Serializable {treeWalk.addTree(revTree);CommitBuilder commit = new CommitBuilder();- commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newPath));+ commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newFile.getPath()));commit.setAuthor(authorAndCommitter);commit.setCommitter(authorAndCommitter);commit.setParentId(parentCommitId);diff --git a/commons.git/src/main/java/com/pmease/commons/git/PathAndContent.java b/commons.git/src/main/java/com/pmease/commons/git/PathAndContent.javanew file mode 100644index 0000000000..95f09d0ae7--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/PathAndContent.java@@ -0,0 +1,28 @@+package com.pmease.commons.git;++import java.io.Serializable;++import com.google.common.base.Preconditions;++public class PathAndContent implements Serializable {++ private static final long serialVersionUID = 1L;++ private final String path;++ private final byte[] content;++ public PathAndContent(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.javaindex fca681d543..2dcf5f0f4c 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);@@ -95,7 +95,7 @@ public class FileEditTest extends AbstractGitTest {PersonIdent person = new PersonIdent(repo);FileEdit edit = new FileEdit("server/src/com/example/a/a.java",- "client/c.java", "a");+ new PathAndContent("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();@@ -125,7 +125,7 @@ public class FileEditTest extends AbstractGitTest {PersonIdent person = new PersonIdent(repo);FileEdit edit = new FileEdit("server/src/com/example/a",- "client/c.java", "a");+ new PathAndContent("client/c.java", "a".getBytes()));try {edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");assertTrue("A NotFileException should be thrown", false);@@ -151,7 +151,7 @@ public class FileEditTest extends AbstractGitTest {PersonIdent person = new PersonIdent(repo);FileEdit edit = new FileEdit("server/src/com/example/a/a.java",- "client/a.java/a.java", "a");+ new PathAndContent("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);@@ -176,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");+ FileEdit edit = new FileEdit(null, new PathAndContent("/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)) {@@ -189,7 +189,7 @@ public class FileEditTest extends AbstractGitTest {}oldCommitId = newCommitId;- edit = new FileEdit(null, "/common/common.java", "common");+ edit = new FileEdit(null, new PathAndContent("/common/common.java", "common".getBytes()));newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");try (RevWalk revWalk = new RevWalk(repo)) {@@ -220,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");+ FileEdit edit = new FileEdit(null, new PathAndContent("client/a.java", "a".getBytes()));try {edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");assertTrue("An ObjectAlreadyExistException should be thrown", false);@@ -242,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");+ FileEdit edit = new FileEdit(null, new PathAndContent("/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/editsave/EditSavePanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.htmlindex 8bf13185b0..6f4783ff2f 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@@ -9,12 +9,6 @@<a wicket:id="changes">See what changed.</a></div><form wicket:id="form">- <wicket:enclosure child="fileName">- <div class="form-group">- <label>File Name</label>- <input wicket:id="fileName" class="form-control"></input>- </div>- </wicket:enclosure><div class="form-group"><label>Summary Commit Message</label><input wicket:id="summaryCommitMessage" class="form-control"></input>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.javaindex f77a63a0d8..90226bcb85 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@@ -29,7 +29,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;import com.google.common.base.Preconditions;import com.pmease.commons.git.FileEdit;-import com.pmease.commons.git.ParentPathAndName;+import com.pmease.commons.git.PathAndContent;import com.pmease.commons.git.exception.NotTreeException;import com.pmease.commons.git.exception.ObjectAlreadyExistException;import com.pmease.commons.git.exception.ObsoleteCommitException;@@ -46,9 +46,9 @@ public abstract class EditSavePanel extends Panel {private final String refName;- private final ParentPathAndName parentPathAndName;+ private final String oldPath;- private final IModel<String> contentModel;+ private final IModel<PathAndContent> newFileModel;private final CancelListener cancelListener;@@ -56,43 +56,54 @@ public abstract class EditSavePanel extends Panel {private ObjectId currentCommitId;- private String newName;-private String summaryCommitMessage;private String detailCommitMessage;public EditSavePanel(String id, IModel<Repository> repoModel, String refName,- ParentPathAndName parentPathAndName, @Nullable IModel<String> contentModel,+ @Nullable String oldPath, @Nullable IModel<PathAndContent> newFileModel,ObjectId prevCommitId, @Nullable CancelListener cancelListener) {super(id);this.repoModel = repoModel;this.refName = refName;- this.parentPathAndName = parentPathAndName;- this.contentModel = contentModel;+ this.oldPath = oldPath;+ this.newFileModel = newFileModel;this.cancelListener = cancelListener;this.prevCommitId = prevCommitId;-- newName = parentPathAndName.getName();}private String getDefaultCommitMessage() {- String name = parentPathAndName.getName();- if (contentModel != null) {- if (name != null) {- if (name.equals(newName))- return "Edit " + name;+ String oldName;+ if (oldPath != null && oldPath.contains("/"))+ oldName = StringUtils.substringAfterLast(oldPath, "/");+ else+ oldName = oldPath;++ PathAndContent newFile = getNewFile();+ if (newFile == null) {+ return "Delete " + oldName;+ } else {+ String newPath = newFile.getPath();++ String newName;+ if (newPath != null && newPath.contains("/"))+ newName = StringUtils.substringAfterLast(newPath, "/");+ else+ newName = newPath;++ if (oldPath == null) {+ if (newName != null)+ return "Add " + newName;else- return "Rename " + name;- } else if (newName != null) {- return "Add " + newName;+ return "Add new file";+ } else if (oldPath.equals(newPath)) {+ return "Edit " + oldPath;} else {- return "Add new file";+ return "Rename " + oldPath;}- } else {- return "Delete " + name;}+}@Override@@ -118,33 +129,6 @@ public abstract class EditSavePanel extends Panel {Form<?> form = new Form<Void>("form");add(form);- form.add(new TextField<String>("fileName", new IModel<String>() {-- @Override- public void detach() {- }-- @Override- public String getObject() {- return newName;- }-- @Override- public void setObject(String object) {- newName = object;- }-- }) {-- @Override- protected void onConfigure() {- super.onConfigure();-- setVisible(contentModel != null);- }-- });-form.add(new TextField<String>("summaryCommitMessage", new IModel<String>() {@Override@@ -195,7 +179,7 @@ public abstract class EditSavePanel extends Panel {protected void onSubmit(AjaxRequestTarget target, Form<?> form) {super.onSubmit(target, form);- if (contentModel != null && StringUtils.isBlank(newName)) {+ if (newFileModel != null && StringUtils.isBlank(newFileModel.getObject().getPath())) {EditSavePanel.this.error("Please specify file name.");target.add(feedback);return;@@ -209,19 +193,7 @@ public abstract class EditSavePanel extends Panel {commitMessage += "\n\n" + detailCommitMessage;User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());- String path = parentPathAndName.getPath();-- String newPath;- String content;- if (contentModel != null) {- newPath = parentPathAndName.getPath(newName);- content = contentModel.getObject();- } else {- newPath = null;- content = null;- }-- FileEdit edit = new FileEdit(path, newPath, content);+ FileEdit edit = new FileEdit(oldPath, getNewFile());ObjectId newCommitId = null;while(newCommitId == null) {try {@@ -243,8 +215,8 @@ public abstract class EditSavePanel extends Panel {if (!treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))|| !treeWalk.getFileMode(0).equals(treeWalk.getFileMode(1))) {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;@@ -260,8 +232,8 @@ public abstract class EditSavePanel extends Panel {}}}- 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))@@ -311,8 +283,12 @@ public abstract class EditSavePanel extends Panel {setOutputMarkupId(true);}- public String getNewName() {- return newName;+ @Nullable+ private PathAndContent getNewFile() {+ if (newFileModel != null)+ return newFileModel.getObject();+ else+ return null;}@Override@@ -331,8 +307,8 @@ public abstract class EditSavePanel extends Panel {@Overrideprotected void onDetach() {repoModel.detach();- if (contentModel != null)- contentModel.detach();+ if (newFileModel != null)+ newFileModel.detach();super.onDetach();}