The Unified and Autonomous Development Platform
Midway of FileEdit.commit
robin shine67f36b100602
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.javanew file mode 100644index 0000000000..e3ccf33924--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java@@ -0,0 +1,223 @@+package com.pmease.commons.git;++import java.io.IOException;+import java.io.Serializable;+import java.util.List;++import javax.annotation.Nullable;++import org.eclipse.jgit.errors.RevisionSyntaxException;+import org.eclipse.jgit.lib.CommitBuilder;+import org.eclipse.jgit.lib.Constants;+import org.eclipse.jgit.lib.FileMode;+import org.eclipse.jgit.lib.ObjectId;+import org.eclipse.jgit.lib.ObjectInserter;+import org.eclipse.jgit.lib.PersonIdent;+import org.eclipse.jgit.lib.RefUpdate;+import org.eclipse.jgit.lib.Repository;+import org.eclipse.jgit.lib.TreeFormatter;+import org.eclipse.jgit.revwalk.RevTree;+import org.eclipse.jgit.revwalk.RevWalk;+import org.eclipse.jgit.treewalk.TreeWalk;++import com.google.common.base.Objects;+import com.google.common.base.Preconditions;+import com.google.common.base.Splitter;+import com.pmease.commons.git.exception.NotFileException;+import com.pmease.commons.git.exception.NotTreeException;+import com.pmease.commons.git.exception.ObjectAlreadyExistException;+import com.pmease.commons.git.exception.ObjectNotExistException;+import com.pmease.commons.git.exception.ObsoleteCommitException;+import com.pmease.commons.git.exception.RefUpdateException;++public class FileEdit implements Serializable {++ private static final long serialVersionUID = 1L;++ private final String oldPath;++ private final String newPath;++ private final byte[] content;++ public FileEdit(@Nullable String oldPath, @Nullable String newPath, byte[] content) {+ 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;+ }++ public String getOldPath() {+ return oldPath;+ }++ public String getNewPath() {+ return newPath;+ }++ public byte[] getContent() {+ return content;+ }++ private ObjectId insertTree(RevTree revTree, TreeWalk treeWalk, ObjectInserter inserter,+ @Nullable String currentOldPath, @Nullable String currentNewPath, byte[] content) {+ try {+ TreeFormatter formatter = new TreeFormatter();+ boolean appended = false;+ boolean oldPathFound = false;+ boolean newPathFound = false;+ while (treeWalk.next()) {+ String name = treeWalk.getNameString();+ if (name.equals(currentOldPath)) {+ if ((treeWalk.getFileMode(0).getBits() & FileMode.TYPE_FILE) == 0)+ throw new NotFileException("Path does not represent a file: " + treeWalk.getPathString());+ oldPathFound = true;+ if (name.equals(currentNewPath)) {+ newPathFound = true;+ ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content);+ formatter.append(name, FileMode.REGULAR_FILE, blobId);+ appended = true;+ }+ } else if (name.equals(currentNewPath)) {+ throw new ObjectAlreadyExistException("Path already exist: " + treeWalk.getPathString());+ } else if (currentOldPath != null && currentOldPath.startsWith(name + "/")) {+ oldPathFound = true;+ TreeWalk childTreeWalk = TreeWalk.forPath(treeWalk.getObjectReader(), treeWalk.getPathString(), revTree);+ Preconditions.checkNotNull(childTreeWalk);+ childTreeWalk.enterSubtree();+ String childOldPath = currentOldPath.substring(name.length()+1);+ String childNewPath;+ if (currentNewPath != null && currentNewPath.startsWith(name + "/")) {+ childNewPath = currentNewPath.substring(name.length()+1);+ newPathFound = true;+ } else {+ childNewPath = null;+ }+ ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter, childOldPath, childNewPath, content);+ if (childTreeId != null) {+ formatter.append(name, FileMode.TREE, childTreeId);+ appended = true;+ }+ } else if (currentNewPath != null && currentNewPath.startsWith(name + "/")) {+ 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);+ Preconditions.checkNotNull(childTreeWalk);+ childTreeWalk.enterSubtree();+ String childNewPath = currentNewPath.substring(name.length()+1);+ ObjectId childTreeId = insertTree(revTree, childTreeWalk, inserter, null, childNewPath, content);+ if (childTreeId != null) {+ formatter.append(name, treeWalk.getFileMode(0), childTreeId);+ appended = true;+ }+ } else {+ formatter.append(name, treeWalk.getFileMode(0), treeWalk.getObjectId(0));+ appended = true;+ }+ }++ if (currentOldPath != null && !oldPathFound)+ throw new ObjectNotExistException("Path not exist: " + oldPath);++ if (currentNewPath != null && !newPathFound) {+ List<String> splitted = Splitter.on('/').splitToList(currentNewPath);++ ObjectId childId = null;+ FileMode childMode = null;+ String childName = null;++ for (int i=splitted.size()-1; i>=0; i--) {+ if (childId == null) {+ childName = splitted.get(i);+ childId = inserter.insert(Constants.OBJ_BLOB, content);+ childMode = FileMode.REGULAR_FILE;+ } else {+ TreeFormatter childFormatter = new TreeFormatter();+ childFormatter.append(childName, childMode, childId);+ childName = splitted.get(i);+ childId = inserter.insert(childFormatter);+ childMode = FileMode.TREE;+ }+ }++ Preconditions.checkState(childId!=null && childMode != null && childName != null);+ formatter.append(childName, childMode, childId);+ appended = true;+ }+ if (appended)+ return inserter.insert(formatter);+ else+ return null;+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }++ /**+ * Commit specified file into specified repository.+ *+ * @param repo+ * repository to make the new commit+ * @param refName+ * ref name to associate the new commit with+ * @param expectedOldCommitId+ * expected old commit id of above ref, use <tt>null</tt> to expect a non-existent ref+ * @param parentCommitId+ * parent commit id of the new commit+ * @param authorAndCommitter+ * author and committer person ident for the new commit+ * @param commitMessage+ * commit message for the new commit+ * @return+ * id of new commit+ * @throws ObsoleteCommitException+ * if expected old commit id of the ref does not equal to+ * expectedOldCommitId, or if expectedOldCommitId is specified as <tt>null</tt> and+ * ref exists+ * @throws ObjectNotExistException+ * if file to delete does not exist when oldPath!=null&&newFile==null+ * @throws ObjectAlreadyExistException+ * if added/renamed file already exists when newFile!=null && (oldPath==null || !oldPath.equals(newFile.getPath()))+ *+ */+ public ObjectId commit(Repository repo, String refName,+ @Nullable ObjectId expectedOldCommitId, ObjectId parentCommitId,+ PersonIdent authorAndCommitter, String commitMessage) {++ try ( RevWalk revWalk = new RevWalk(repo);+ TreeWalk treeWalk = new TreeWalk(repo);+ ObjectInserter inserter = repo.newObjectInserter();) {++ RevTree revTree = revWalk.parseCommit(parentCommitId).getTree();+ treeWalk.addTree(revTree);+ CommitBuilder commit = new CommitBuilder();+ commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newPath, content));+ commit.setAuthor(authorAndCommitter);+ commit.setCommitter(authorAndCommitter);+ commit.setParentId(parentCommitId);+ commit.setMessage(commitMessage);++ ObjectId commitId = inserter.insert(commit);+ inserter.flush();++ RefUpdate ru = repo.updateRef(refName);+ ru.setRefLogIdent(authorAndCommitter);+ ru.setNewObjectId(commitId);+ ru.setExpectedOldObjectId(expectedOldCommitId);+ RefUpdate.Result result = ru.update();+ if (result == RefUpdate.Result.LOCK_FAILURE+ && !Objects.equal(expectedOldCommitId, ru.getOldObjectId())) {+ throw new ObsoleteCommitException(ru.getOldObjectId());+ } else if (result != RefUpdate.Result.FAST_FORWARD) {+ throw new RefUpdateException(result);+ } else {+ return commitId;+ }+ } catch (RevisionSyntaxException | IOException e) {+ throw new RuntimeException(e);+ }+ }+}diff --git a/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.javaindex dec3de9ba5..eee156132a 100644--- a/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java+++ b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java@@ -1,6 +1,5 @@package com.pmease.commons.git;-import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.List;@@ -10,19 +9,8 @@ import javax.annotation.Nullable;import org.apache.commons.lang3.StringUtils;import org.eclipse.jgit.diff.DiffEntry;-import org.eclipse.jgit.errors.RevisionSyntaxException;-import org.eclipse.jgit.lib.CommitBuilder;-import org.eclipse.jgit.lib.Constants;import org.eclipse.jgit.lib.FileMode;-import org.eclipse.jgit.lib.ObjectId;-import org.eclipse.jgit.lib.ObjectInserter;import org.eclipse.jgit.lib.PersonIdent;-import org.eclipse.jgit.lib.RefUpdate;-import org.eclipse.jgit.lib.Repository;-import org.eclipse.jgit.lib.TreeFormatter;-import org.eclipse.jgit.revwalk.RevTree;-import org.eclipse.jgit.revwalk.RevWalk;-import org.eclipse.jgit.treewalk.TreeWalk;import org.eclipse.jgit.util.SystemReader;import com.google.common.base.Joiner;@@ -171,137 +159,4 @@ public class GitUtils {return joinPath(splitPath(path));}- private static ObjectId insertTree(RevTree revTree, TreeWalk treeWalk, ObjectInserter inserter,- String path, byte[] content) {- try {- TreeFormatter formatter = new TreeFormatter();- boolean appended = false;- boolean found = false;- while (treeWalk.next()) {- String name = treeWalk.getNameString();- if (name.equals(path)) {- if (content != null) {- ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content);- formatter.append(name, FileMode.REGULAR_FILE, blobId);- appended = true;- }- found = true;- } else if (path.startsWith(name + "/")) {- TreeWalk subtreeWalk = TreeWalk.forPath(treeWalk.getObjectReader(), treeWalk.getPathString(), revTree);- Preconditions.checkNotNull(subtreeWalk);- subtreeWalk.enterSubtree();- String subpath = path.substring(name.length()+1);- ObjectId subtreeId = insertTree(revTree, subtreeWalk, inserter, subpath, content);- if (subtreeId != null) {- formatter.append(name, treeWalk.getFileMode(0), subtreeId);- appended = true;- }- if (subtreeId == null || !subtreeId.equals(treeWalk.getObjectId(0)))- found = true;- } else {- formatter.append(name, treeWalk.getFileMode(0), treeWalk.getObjectId(0));- appended = true;- }- }- if (!found && content != null) {- List<String> splitted = Splitter.on('/').splitToList(path);-- ObjectId childId = null;- FileMode childMode = null;- String childName = null;-- for (int i=splitted.size()-1; i>=0; i--) {- if (childId == null) {- childName = splitted.get(i);- childId = inserter.insert(Constants.OBJ_BLOB, content);- childMode = FileMode.REGULAR_FILE;- } else {- TreeFormatter childFormatter = new TreeFormatter();- childFormatter.append(childName, childMode, childId);- childName = splitted.get(i);- childId = inserter.insert(childFormatter);- childMode = FileMode.TREE;- }- }-- Preconditions.checkState(childId!=null && childMode != null && childName != null);- formatter.append(childName, childMode, childId);- appended = true;- }- if (appended)- return inserter.insert(formatter);- else- return null;- } catch (IOException e) {- throw new RuntimeException(e);- }- }-- /**- * Commit specified file into specified repository.- *- * @param repo- * repository to make the new commit- * @param refName- * ref name to associate the new commit with- * @param expectedOldCommitId- * expected old commit id of above ref, use <tt>null</tt> to expect a non-existent ref- * @param parentCommitId- * parent commit id of the new commit- * @param authorAndCommitter- * author and committer person ident for the new commit- * @param commitMessage- * commit message for the new commit- * @param path- * path of the file- * @param content- * content of the file, use <tt>null</tt> to delete the file- * @return- * id of new commit- * @throws- * ObsoleteOldCommitException if expected old commit id of the ref does not equal to- * expectedOldCommitId, or if expectedOldCommitId is specified as <tt>null</tt> and- * ref exists- *- */- public static ObjectId commitFile(Repository repo, String refName,- @Nullable ObjectId expectedOldCommitId, ObjectId parentCommitId,- PersonIdent authorAndCommitter, String commitMessage,- String path, byte[] content) {- try ( RevWalk revWalk = new RevWalk(repo);- TreeWalk treeWalk = new TreeWalk(repo);- ObjectInserter inserter = repo.newObjectInserter();) {-- path = normalizePath(path);-- RevTree revTree = revWalk.parseCommit(parentCommitId).getTree();- treeWalk.addTree(revTree);- CommitBuilder commit = new CommitBuilder();- commit.setTreeId(insertTree(revTree, treeWalk, inserter, path, content));- commit.setAuthor(authorAndCommitter);- commit.setCommitter(authorAndCommitter);- commit.setParentId(parentCommitId);- commit.setMessage(commitMessage);-- ObjectId commitId = inserter.insert(commit);- inserter.flush();-- RefUpdate ru = repo.updateRef(refName);- ru.setRefLogIdent(authorAndCommitter);- ru.setNewObjectId(commitId);- ru.setExpectedOldObjectId(expectedOldCommitId);- RefUpdate.Result result = ru.update();- if (result == RefUpdate.Result.LOCK_FAILURE- && !Objects.equal(expectedOldCommitId, ru.getOldObjectId())) {- throw new ObsoleteOldCommitException(ru.getOldObjectId());- } else if (result != RefUpdate.Result.FAST_FORWARD) {- throw new RefUpdateException(result);- } else {- return commitId;- }- } catch (RevisionSyntaxException | IOException e) {- throw new RuntimeException(e);- }- }-}diff --git a/commons.git/src/main/java/com/pmease/commons/git/ObjectNotFoundException.java b/commons.git/src/main/java/com/pmease/commons/git/ObjectNotFoundException.javadeleted file mode 100644index e532f5e8d3..0000000000--- a/commons.git/src/main/java/com/pmease/commons/git/ObjectNotFoundException.java+++ /dev/null@@ -1,11 +0,0 @@-package com.pmease.commons.git;--public class ObjectNotFoundException extends GitException {-- private static final long serialVersionUID = 1L;-- public ObjectNotFoundException(String message) {- super(message);- }--}diff --git a/commons.git/src/main/java/com/pmease/commons/git/GitException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/GitException.javasimilarity index 89%rename from commons.git/src/main/java/com/pmease/commons/git/GitException.javarename to commons.git/src/main/java/com/pmease/commons/git/exception/GitException.javaindex d4458bbf8f..5284ba2604 100644--- a/commons.git/src/main/java/com/pmease/commons/git/GitException.java+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/GitException.java@@ -1,4 +1,4 @@-package com.pmease.commons.git;+package com.pmease.commons.git.exception;public class GitException extends RuntimeException {diff --git a/commons.git/src/main/java/com/pmease/commons/git/exception/NotFileException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/NotFileException.javanew file mode 100644index 0000000000..96948fe239--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/NotFileException.java@@ -0,0 +1,11 @@+package com.pmease.commons.git.exception;++public class NotFileException extends GitException {++ private static final long serialVersionUID = 1L;++ public NotFileException(String message) {+ super(message);+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/exception/NotTreeException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/NotTreeException.javanew file mode 100644index 0000000000..88dfcdb1d6--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/NotTreeException.java@@ -0,0 +1,11 @@+package com.pmease.commons.git.exception;++public class NotTreeException extends GitException {++ private static final long serialVersionUID = 1L;++ public NotTreeException(String message) {+ super(message);+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectAlreadyExistException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectAlreadyExistException.javanew file mode 100644index 0000000000..821437bc39--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectAlreadyExistException.java@@ -0,0 +1,11 @@+package com.pmease.commons.git.exception;++public class ObjectAlreadyExistException extends GitException {++ private static final long serialVersionUID = 1L;++ public ObjectAlreadyExistException(String message) {+ super(message);+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectNotExistException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectNotExistException.javanew file mode 100644index 0000000000..b2585d029a--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/ObjectNotExistException.java@@ -0,0 +1,11 @@+package com.pmease.commons.git.exception;++public class ObjectNotExistException extends GitException {++ private static final long serialVersionUID = 1L;++ public ObjectNotExistException(String message) {+ super(message);+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/ObsoleteOldCommitException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/ObsoleteCommitException.javasimilarity index 62%rename from commons.git/src/main/java/com/pmease/commons/git/ObsoleteOldCommitException.javarename to commons.git/src/main/java/com/pmease/commons/git/exception/ObsoleteCommitException.javaindex 6d98d7c34e..1f5d0cd9c9 100644--- a/commons.git/src/main/java/com/pmease/commons/git/ObsoleteOldCommitException.java+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/ObsoleteCommitException.java@@ -1,16 +1,16 @@-package com.pmease.commons.git;+package com.pmease.commons.git.exception;import javax.annotation.Nullable;import org.eclipse.jgit.lib.ObjectId;-public class ObsoleteOldCommitException extends GitException {+public class ObsoleteCommitException extends GitException {private static final long serialVersionUID = 1L;private final ObjectId oldCommitId;- public ObsoleteOldCommitException(@Nullable ObjectId oldCommitId) {+ public ObsoleteCommitException(@Nullable ObjectId oldCommitId) {this.oldCommitId = oldCommitId;}diff --git a/commons.git/src/main/java/com/pmease/commons/git/RefUpdateException.java b/commons.git/src/main/java/com/pmease/commons/git/exception/RefUpdateException.javasimilarity index 88%rename from commons.git/src/main/java/com/pmease/commons/git/RefUpdateException.javarename to commons.git/src/main/java/com/pmease/commons/git/exception/RefUpdateException.javaindex d08a5478ab..010320f141 100644--- a/commons.git/src/main/java/com/pmease/commons/git/RefUpdateException.java+++ b/commons.git/src/main/java/com/pmease/commons/git/exception/RefUpdateException.java@@ -1,4 +1,4 @@-package com.pmease.commons.git;+package com.pmease.commons.git.exception;import org.eclipse.jgit.lib.RefUpdate;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.javanew file mode 100644index 0000000000..9e49d33aae--- /dev/null+++ b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java@@ -0,0 +1,252 @@+package com.pmease.commons.git;++import static org.junit.Assert.assertNotNull;+import static org.junit.Assert.assertNull;+import static org.junit.Assert.assertTrue;++import java.io.File;+import java.io.IOException;++import org.eclipse.jgit.internal.storage.file.FileRepository;+import org.eclipse.jgit.lib.ObjectId;+import org.eclipse.jgit.lib.PersonIdent;+import org.eclipse.jgit.lib.Repository;+import org.eclipse.jgit.revwalk.RevTree;+import org.eclipse.jgit.revwalk.RevWalk;+import org.eclipse.jgit.treewalk.TreeWalk;+import org.junit.Test;++import com.pmease.commons.git.exception.NotFileException;+import com.pmease.commons.git.exception.NotTreeException;+import com.pmease.commons.git.exception.ObjectAlreadyExistException;+import com.pmease.commons.git.exception.ObjectNotExistException;+import com.pmease.commons.git.exception.ObsoleteCommitException;++public class FileEditTest extends AbstractGitTest {++ @Test+ public void testRemoveFile() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ FileEdit edit = new FileEdit("/server/src/com/example/a//a.java", null, null);+ ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");++ try (RevWalk revWalk = new RevWalk(repo)) {+ RevTree revTree = revWalk.parseCommit(newCommitId).getTree();+ assertNull(TreeWalk.forPath(repo, "server/src/com/example/a", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));+ }+ }+ }++ @Test+ public void testRemoveNonExistentFile() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ FileEdit edit = new FileEdit("/server/src/com/example/c//c.java", null, null);+ try {+ edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");+ assertTrue("An ObjectNotExistException should be thrown", false);+ } catch (ObjectNotExistException e) {+ }+ }+ }++ @Test+ public void testRenameFile() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ 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());+ ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename");+ try (RevWalk revWalk = new RevWalk(repo)) {+ RevTree revTree = revWalk.parseCommit(newCommitId).getTree();+ assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/c.java", revTree));+ assertNull(TreeWalk.forPath(repo, "server/src/com/example/a", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));+ }+ }+ }++ @Test+ public void shouldFailIfOldPathIsTreeWhenRename() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ FileEdit edit = new FileEdit("server/src/com/example/a", "client/c.java", "a".getBytes());+ try {+ edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");+ assertTrue("A NotFileException should be thrown", false);+ } catch (NotFileException e) {+ }+ }+ }++ @Test+ public void shouldFailIfNewPathIsNotUnderTreeWhenRename() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ 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());+ try {+ edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");+ assertTrue("A NotTreeException should be thrown", false);+ } catch (NotTreeException e) {+ }+ }+ }++ @Test+ public void testAddFile() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ FileEdit edit = new FileEdit(null, "/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)) {+ RevTree revTree = revWalk.parseCommit(newCommitId).getTree();+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/a/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/c/c.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));+ }++ oldCommitId = newCommitId;+ edit = new FileEdit(null, "/common/common.java", "common".getBytes());+ newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");++ try (RevWalk revWalk = new RevWalk(repo)) {+ RevTree revTree = revWalk.parseCommit(newCommitId).getTree();+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/a/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/c/c.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));+ assertNotNull(TreeWalk.forPath(repo, "common/common.java", revTree));+ }+ }+ }++ @Test+ public void testAddExistFile() throws IOException {+ createDir("client");+ addFileAndCommit("client/a.java", "a", "add a");+ addFileAndCommit("client/b.java", "b", "add b");++ createDir("server/src/com/example/a");+ createDir("server/src/com/example/b");+ addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");+ addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ FileEdit edit = new FileEdit(null, "client/a.java", "a".getBytes());+ try {+ edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");+ assertTrue("An ObjectAlreadyExistException should be thrown", false);+ } catch (ObjectAlreadyExistException e) {+ }+ }+ }++ @Test+ public void testObsoleteOldCommit() throws IOException {+ addFileAndCommit("a.java", "a", "add a");++ try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {+ String refName = "refs/heads/master";+ ObjectId oldCommitId = repo.resolve(refName);+ PersonIdent person = new PersonIdent(repo);++ addFileAndCommit("b.java", "b", "add b");++ ObjectId newCommitId = repo.resolve(refName);++ FileEdit edit = new FileEdit(null, "/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);+ } catch (ObsoleteCommitException e) {+ assertTrue(newCommitId.equals(e.getOldCommitId()));+ }+ }+ }++}diff --git a/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java b/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.javaindex 852f339850..f809746222 100644--- a/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java@@ -1,17 +1,8 @@package com.pmease.commons.git;-import static org.junit.Assert.*;+import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertTrue;-import java.io.File;-import java.io.IOException;--import org.eclipse.jgit.internal.storage.file.FileRepository;-import org.eclipse.jgit.lib.ObjectId;-import org.eclipse.jgit.lib.PersonIdent;-import org.eclipse.jgit.lib.Repository;-import org.eclipse.jgit.revwalk.RevTree;-import org.eclipse.jgit.revwalk.RevWalk;-import org.eclipse.jgit.treewalk.TreeWalk;import org.junit.Test;import com.google.common.collect.Lists;@@ -39,129 +30,4 @@ public class GitUtilsTest extends AbstractGitTest {assertTrue(GitUtils.comparePath("dir12", "dir1/dir2")>0);}- @Test- public void testRemoveFile() throws IOException {- createDir("client");- addFileAndCommit("client/a.java", "a", "add a");- addFileAndCommit("client/b.java", "b", "add b");-- createDir("server/src/com/example/a");- createDir("server/src/com/example/b");- addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");- addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");-- try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {- String refName = "refs/heads/master";- ObjectId oldCommitId = repo.resolve(refName);- PersonIdent person = new PersonIdent(repo);-- ObjectId newCommitId = GitUtils.commitFile(repo, refName, oldCommitId, oldCommitId,- person, "test delete", "/server/src/com/example/a//a.java", null);-- try (RevWalk revWalk = new RevWalk(repo)) {- RevTree revTree = revWalk.parseCommit(newCommitId).getTree();- assertNull(TreeWalk.forPath(repo, "server/src/com/example/a", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));- }- }- }-- @Test- public void testRemoveNonExistentFile() throws IOException {- createDir("client");- addFileAndCommit("client/a.java", "a", "add a");- addFileAndCommit("client/b.java", "b", "add b");-- createDir("server/src/com/example/a");- createDir("server/src/com/example/b");- addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");- addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");-- try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {- String refName = "refs/heads/master";- ObjectId oldCommitId = repo.resolve(refName);- PersonIdent person = new PersonIdent(repo);-- ObjectId newCommitId = GitUtils.commitFile(repo, refName, oldCommitId, oldCommitId,- person, "test delete", "/server/src/com/example/c//c.java", null);-- try (RevWalk revWalk = new RevWalk(repo)) {- RevTree revTree = revWalk.parseCommit(newCommitId).getTree();- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/a/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));- }- }- }-- @Test- public void testAddFile() throws IOException {- createDir("client");- addFileAndCommit("client/a.java", "a", "add a");- addFileAndCommit("client/b.java", "b", "add b");-- createDir("server/src/com/example/a");- createDir("server/src/com/example/b");- addFileAndCommit("server/src/com/example/a/a.java", "a", "add a");- addFileAndCommit("server/src/com/example/b/b.java", "b", "add b");-- try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {- String refName = "refs/heads/master";- ObjectId oldCommitId = repo.resolve(refName);- PersonIdent person = new PersonIdent(repo);-- ObjectId newCommitId = GitUtils.commitFile(repo, refName, oldCommitId, oldCommitId,- person, "test add", "/server/src/com/example/c/c.java", "c".getBytes());-- try (RevWalk revWalk = new RevWalk(repo)) {- RevTree revTree = revWalk.parseCommit(newCommitId).getTree();- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/a/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/c/c.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));- }-- oldCommitId = newCommitId;- newCommitId = GitUtils.commitFile(repo, refName, oldCommitId, oldCommitId,- person, "test add", "/common/common.java", "common".getBytes());-- try (RevWalk revWalk = new RevWalk(repo)) {- RevTree revTree = revWalk.parseCommit(newCommitId).getTree();- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/a/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/b/b.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "server/src/com/example/c/c.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/a.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "client/b.java", revTree));- assertNotNull(TreeWalk.forPath(repo, "common/common.java", revTree));- }- }- }-- @Test- public void testObsoleteOldCommit() throws IOException {- addFileAndCommit("a.java", "a", "add a");-- try (Repository repo = new FileRepository(new File(git.repoDir(), ".git"))) {- String refName = "refs/heads/master";- ObjectId oldCommitId = repo.resolve(refName);- PersonIdent person = new PersonIdent(repo);-- addFileAndCommit("b.java", "b", "add b");-- ObjectId newCommitId = repo.resolve(refName);-- try {- GitUtils.commitFile(repo, refName, oldCommitId, oldCommitId,- person, "test add", "/server/src/com/example/c/c.java", "c".getBytes());- assertTrue("An ObsoleteOldCommitException should be thrown", false);- } catch (ObsoleteOldCommitException e) {- assertTrue(newCommitId.equals(e.getOldCommitId()));- }- }- }-}diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.javaindex 4b3041f8da..2680b42b9e 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java@@ -57,8 +57,8 @@ import com.pmease.commons.git.BlobText;import com.pmease.commons.git.Change;import com.pmease.commons.git.Commit;import com.pmease.commons.git.Git;-import com.pmease.commons.git.ObjectNotFoundException;import com.pmease.commons.git.Submodule;+import com.pmease.commons.git.exception.ObjectNotExistException;import com.pmease.commons.hibernate.AbstractEntity;import com.pmease.commons.loader.AppLoader;import com.pmease.commons.util.FileUtils;@@ -387,7 +387,7 @@ public class Repository extends AbstractEntity implements UserBelonging {* @return* blob of specified blob ident* @throws- * ObjectNotFoundException if blob of specified ident can not be found in repository+ * ObjectNotExistException if blob of specified ident can not be found in repository**/public Blob getBlob(BlobIdent ident) {@@ -404,7 +404,7 @@ public class Repository extends AbstractEntity implements UserBelonging {if (ident.isGitLink()) {String url = getSubmodules(ident.revision).get(ident.path);if (url == null)- throw new ObjectNotFoundException("Unable to find submodule '" + ident.path + "' in .gitmodules");+ throw new ObjectNotExistException("Unable to find submodule '" + ident.path + "' in .gitmodules");String hash = treeWalk.getObjectId(0).name();blob = new Blob(ident, new Submodule(url, hash).toString().getBytes());} else {@@ -422,7 +422,7 @@ public class Repository extends AbstractEntity implements UserBelonging {}blobCache.put(ident, blob);} else {- throw new ObjectNotFoundException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");+ throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");}} catch (IOException e) {throw new RuntimeException(e);@@ -440,7 +440,7 @@ public class Repository extends AbstractEntity implements UserBelonging {ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));return objectLoader.openStream();} else {- throw new ObjectNotFoundException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");+ throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");}} catch (IOException e) {throw new RuntimeException(e);@@ -497,7 +497,7 @@ public class Repository extends AbstractEntity implements UserBelonging {}}if (mustExist && !optional.isPresent())- throw new ObjectNotFoundException("Unable to find revision '" + revision + "'");+ throw new ObjectNotExistException("Unable to find revision '" + revision + "'");return optional.orNull();}diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.javaindex 536f929783..55e0564d7e 100644--- a/gitplex.product/src/test/java/gitplex/product/Test.java+++ b/gitplex.product/src/test/java/gitplex/product/Test.java@@ -15,15 +15,13 @@ public class Test {@org.junit.Testpublic void test() throws IOException, RevisionSyntaxException, GitAPIException {- try ( Repository repo = new FileRepository(new File("w:\\temp\\repo\\.git"));+ try ( Repository repo = new FileRepository(new File("w:\\android\\.git"));RevWalk revWalk = new RevWalk(repo)) {- RevTree prevTree = revWalk.parseCommit(repo.resolve("master~1")).getTree();- RevTree currentTree = revWalk.parseCommit(repo.resolve("master")).getTree();+ RevTree revTree = revWalk.parseCommit(repo.resolve("master")).getTree();- TreeWalk treeWalk = TreeWalk.forPath(repo, "dir/file", prevTree.getId(), currentTree.getId());-- System.out.println(treeWalk.getObjectId(0));- System.out.println(treeWalk.getObjectId(1));+ TreeWalk treeWalk = TreeWalk.forPath(repo, "api/current.txt", revTree);+ treeWalk.enterSubtree();+ System.out.println(treeWalk.getDepth());}}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.javaindex b77d15a25a..86f193a451 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@@ -32,7 +32,7 @@ import com.pmease.commons.git.BlobIdent;import com.pmease.commons.git.Git;import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;import com.pmease.gitplex.core.model.Repository;-import com.pmease.gitplex.web.component.savechange.SaveChangePanel;+import com.pmease.gitplex.web.component.editsave.EditSavePanel;import com.pmease.gitplex.web.page.repository.file.HistoryState;import com.pmease.gitplex.web.resource.BlobResource;import com.pmease.gitplex.web.resource.BlobResourceReference;@@ -204,7 +204,7 @@ public abstract class BlobViewPanel extends Panel {context.getState().file.revision, true);final BlobIdent blobIdent = context.getState().file;- SaveChangePanel saveChangePanel = new SaveChangePanel(panelId, repoModel, blobIdent, commitId, null) {+ EditSavePanel saveChangePanel = new EditSavePanel(panelId, repoModel, blobIdent, commitId, null) {@Overrideprotected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.htmlsimilarity index 94%rename from gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.htmlrename to gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.htmlindex 7a082ab82c..fa1b115264 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.html+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html@@ -4,6 +4,7 @@Add New Commit</div><div class="body">+ <div wicket:id="feedback"></div><div wicket:id="hasChanges" class="has-changes alert alert-danger">Someone has committed since you start editing.<a wicket:id="changes">See what changed.</a></div>diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.javasimilarity index 57%rename from gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.javarename to gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.javaindex 0f247f2b16..7da8a61d43 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/SaveChangePanel.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java@@ -1,9 +1,7 @@-package com.pmease.gitplex.web.component.savechange;+package com.pmease.gitplex.web.component.editsave;import java.io.IOException;-import javax.annotation.Nullable;-import org.apache.commons.lang3.StringUtils;import org.apache.wicket.ajax.AjaxRequestTarget;import org.apache.wicket.ajax.markup.html.AjaxLink;@@ -24,26 +22,28 @@ import org.apache.wicket.request.resource.JavaScriptResourceReference;import org.eclipse.jgit.internal.storage.file.FileRepository;import org.eclipse.jgit.lib.ObjectId;import org.eclipse.jgit.revwalk.RevCommit;-import org.eclipse.jgit.revwalk.RevTree;import org.eclipse.jgit.revwalk.RevWalk;import org.eclipse.jgit.treewalk.TreeWalk;import com.google.common.base.Preconditions;-import com.pmease.commons.git.BlobIdent;-import com.pmease.commons.git.Git;-import com.pmease.commons.git.GitUtils;-import com.pmease.commons.git.ObsoleteOldCommitException;+import com.pmease.commons.git.FileEdit;+import com.pmease.commons.git.exception.NotTreeException;+import com.pmease.commons.git.exception.ObjectAlreadyExistException;+import com.pmease.commons.git.exception.ObsoleteCommitException;+import com.pmease.commons.wicket.component.feedback.FeedbackPanel;import com.pmease.gitplex.core.GitPlex;import com.pmease.gitplex.core.manager.UserManager;import com.pmease.gitplex.core.model.Repository;import com.pmease.gitplex.core.model.User;@SuppressWarnings("serial")-public abstract class SaveChangePanel extends Panel {+public abstract class EditSavePanel extends Panel {private final IModel<Repository> repoModel;- private final BlobIdent blobIdent;+ private final IModel<String> refModel;++ private final IModel<FileEdit> editModel;private ObjectId prevCommitId;@@ -53,40 +53,36 @@ public abstract class SaveChangePanel extends Panel {private String detailCommitMessage;- private String defaultCommitMessage;-- private byte[] content;-- public SaveChangePanel(String id, IModel<Repository> repoModel, BlobIdent blobIdent,- ObjectId prevCommitId, @Nullable byte[] content) {+ public EditSavePanel(String id, IModel<Repository> repoModel, IModel<String> refModel,+ IModel<FileEdit> editModel, ObjectId prevCommitId) {super(id);this.repoModel = repoModel;- this.blobIdent = blobIdent;+ this.refModel = refModel;+ this.editModel = editModel;this.prevCommitId = prevCommitId;- this.content = content;-- if (content != null) {- try ( FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo();- RevWalk revWalk = new RevWalk(jgitRepo)) {- RevTree revTree = revWalk.parseCommit(prevCommitId).getTree();- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, blobIdent.path, revTree);- if (treeWalk != null)- defaultCommitMessage = "Change " + blobIdent.getName();- else- defaultCommitMessage = "Add " + blobIdent.getName();- } catch (IOException e) {- throw new RuntimeException(e);- }+ }++ 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();+ else+ return "Add " + edit.getNewPath();} else {- defaultCommitMessage = "Delete " + blobIdent.getName();+ return "Delete " + edit.getOldPath();}}-+@Overrideprotected void onInitialize() {super.onInitialize();+ final FeedbackPanel feedback = new FeedbackPanel("feedback", this);+final WebMarkupContainer hasChangesContainer = new WebMarkupContainer("hasChanges");hasChangesContainer.setVisibilityAllowed(false);hasChangesContainer.setOutputMarkupPlaceholderTag(true);@@ -102,6 +98,9 @@ public abstract class SaveChangePanel extends Panel {Form<?> form = new Form<Void>("form");add(form);+ feedback.setOutputMarkupPlaceholderTag(true);+ add(feedback);+form.add(new TextField<String>("summaryCommitMessage", new IModel<String>() {@Override@@ -123,7 +122,7 @@ public abstract class SaveChangePanel extends Panel {@Overrideprotected void onComponentTag(ComponentTag tag) {super.onComponentTag(tag);- tag.put("placeholder", defaultCommitMessage);+ tag.put("placeholder", getDefaultCommitMessage());}});@@ -155,41 +154,69 @@ public abstract class SaveChangePanel extends Panel {try (FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo()) {String commitMessage = summaryCommitMessage;if (StringUtils.isBlank(commitMessage))- commitMessage = defaultCommitMessage;+ commitMessage = getDefaultCommitMessage();if (StringUtils.isNotBlank(detailCommitMessage))commitMessage += "\n\n" + detailCommitMessage;User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());- String refName = blobIdent.revision;- if (!refName.startsWith("refs/"))- refName = Git.REFS_HEADS + refName;-+ FileEdit edit = editModel.getObject();ObjectId newCommitId = null;while(newCommitId == null) {try {- newCommitId = GitUtils.commitFile(jgitRepo, refName, prevCommitId, prevCommitId,- user.asPerson(), commitMessage, blobIdent.path, content);- } catch (ObsoleteOldCommitException e) {+ newCommitId = edit.commit(jgitRepo, refModel.getObject(),+ prevCommitId, prevCommitId, user.asPerson(), commitMessage);+ } catch (ObsoleteCommitException e) {currentCommitId = e.getOldCommitId();try (RevWalk revWalk = new RevWalk(jgitRepo)) {RevCommit prevCommit = revWalk.parseCommit(prevCommitId);RevCommit currentCommit = revWalk.parseCommit(currentCommitId);- // the new commit introduced by other user does not affect our file, so- // pick up the new commit and try againprevCommitId = currentCommitId;-- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, blobIdent.path,- prevCommit.getTree().getId(), currentCommit.getTree().getId());- if (treeWalk != null && !treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))) {- hasChangesContainer.setVisibilityAllowed(true);+ hasChangesContainer.setVisibilityAllowed(false);+ if (edit.getOldPath() != null) {+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getOldPath(),+ 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);+ if (treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {+ if (edit.getNewPath() != null) {+ edit = new FileEdit(null, edit.getNewPath(), edit.getContent());+ } else {+ newCommitId = currentCommitId;+ break;+ }+ }+ }+ }+ }+ if (edit.getNewPath() != null && !edit.getNewPath().equals(edit.getOldPath())) {+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getNewPath(),+ 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);+ }+ }+ }++ if (hasChangesContainer.isVisibilityAllowed()) {target.add(hasChangesContainer);break;}+} catch (IOException e2) {throw new RuntimeException(e2);}+ } catch (ObjectAlreadyExistException e) {+ EditSavePanel.this.error("A file with same name already exists. "+ + "Please choose a different name and try again.");+ } catch (NotTreeException e) {+ EditSavePanel.this.error("A file exists where you’re trying to create a subdirectory. "+ + "Choose a new path and try again..");}}if (newCommitId != null)@@ -216,10 +243,10 @@ public abstract class SaveChangePanel extends Panel {super.renderHead(response);response.render(JavaScriptHeaderItem.forReference(- new JavaScriptResourceReference(SaveChangePanel.class, "save-change.js")));+ new JavaScriptResourceReference(EditSavePanel.class, "edit-save.js")));response.render(CssHeaderItem.forReference(- new CssResourceReference(SaveChangePanel.class, "save-change.css")));- response.render(OnDomReadyHeaderItem.forScript(String.format("gitplex.saveChange.init('%s');", getMarkupId())));+ new CssResourceReference(EditSavePanel.class, "edit-save.css")));+ response.render(OnDomReadyHeaderItem.forScript(String.format("gitplex.editSave.init('%s');", getMarkupId())));}protected abstract void onCommitted(AjaxRequestTarget target, ObjectId newCommitId);@@ -229,6 +256,8 @@ public abstract class SaveChangePanel extends Panel {@Overrideprotected void onDetach() {repoModel.detach();+ refModel.detach();+ editModel.detach();super.onDetach();}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/save-change.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.csssimilarity index 100%rename from gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/save-change.cssrename to gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.cssdiff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.jsnew file mode 100644index 0000000000..c94c4919df--- /dev/null+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/edit-save.js@@ -0,0 +1,29 @@+gitplex.editSave = {+ init: function(containerId) {+ var $editSave = $("#" + containerId + ">.edit-save");+ $editSave.scroll(function() {+ pmease.commons.history.setScrollPos({left: $body.scrollLeft(), top: $body.scrollTop()});+ });++ gitplex.expandable.getScrollTop = function() {+ return $editSave.scrollTop();+ };+ gitplex.expandable.setScrollTop = function(scrollTop) {+ $editSave.scrollTop(scrollTop);+ };++ $editSave.on("autofit", function(event, width, height) {+ event.stopPropagation();+ $editSave.outerWidth(width);+ $editSave.outerHeight(height);++ var scrollPos = pmease.commons.history.getScrollPos();+ if (scrollPos) {+ $editSave.scrollLeft(scrollPos.left);+ $editSave.scrollTop(scrollPos.top);+ }+ });++ $(window).resize();+ }+}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/save-change.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/save-change.jsdeleted file mode 100644index 370d539f52..0000000000--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/savechange/save-change.js+++ /dev/null@@ -1,29 +0,0 @@-gitplex.saveChange = {- init: function(containerId) {- var $saveChange = $("#" + containerId + ">.save-change");- $saveChange.scroll(function() {- pmease.commons.history.setScrollPos({left: $body.scrollLeft(), top: $body.scrollTop()});- });-- gitplex.expandable.getScrollTop = function() {- return $saveChange.scrollTop();- };- gitplex.expandable.setScrollTop = function(scrollTop) {- $saveChange.scrollTop(scrollTop);- };-- $saveChange.on("autofit", function(event, width, height) {- event.stopPropagation();- $saveChange.outerWidth(width);- $saveChange.outerHeight(height);-- var scrollPos = pmease.commons.history.getScrollPos();- if (scrollPos) {- $saveChange.scrollLeft(scrollPos.left);- $saveChange.scrollTop(scrollPos.top);- }- });-- $(window).resize();- }-}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.javaindex b2970f366f..aed8418cb5 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java@@ -36,7 +36,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;import com.google.common.base.Objects;import com.pmease.commons.git.BlobIdent;import com.pmease.commons.git.GitUtils;-import com.pmease.commons.git.ObjectNotFoundException;+import com.pmease.commons.git.exception.ObjectNotExistException;import com.pmease.commons.hibernate.UnitOfWork;import com.pmease.commons.lang.TokenPosition;import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;@@ -117,7 +117,7 @@ public class RepoFilePage extends RepositoryPage {RevTree revTree = revWalk.parseCommit(getCommitId()).getTree();TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, state.file.path, revTree);if (treeWalk == null) {- throw new ObjectNotFoundException("Unable to find blob path '" + state.file.path+ throw new ObjectNotExistException("Unable to find blob path '" + state.file.path+ "' in revision '" + state.file.revision + "'");}state.file.mode = treeWalk.getRawMode(0);