Skip to content

The Unified and Autonomous Development Platform

Able to specify sub directory when upload files

robin shen02f44f843965
diff --git a/core/src/main/java/com/gitplex/server/git/BlobEdits.java b/core/src/main/java/com/gitplex/server/git/BlobEdits.java
index 9e090722c9..f01c45ca62 100644
--- a/core/src/main/java/com/gitplex/server/git/BlobEdits.java
+++ b/core/src/main/java/com/gitplex/server/git/BlobEdits.java
@@ -3,13 +3,15 @@ package com.gitplex.server.git;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
@@ -29,7 +31,7 @@ import com.gitplex.server.git.exception.ObjectAlreadyExistsException;
import com.gitplex.server.git.exception.ObjectNotFoundException;
import com.gitplex.server.git.exception.ObsoleteCommitException;
import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
+import com.google.common.collect.Sets;
public class BlobEdits implements Serializable {
@@ -48,7 +50,7 @@ public class BlobEdits implements Serializable {
else
throw new IllegalArgumentException("Invalid old path: " + oldPath);
}
- this.newBlobs = new HashMap<>();
+ this.newBlobs = new LinkedHashMap<>();
for (Map.Entry<String, BlobContent> entry: newBlobs.entrySet()) {
String normalizedPath = GitUtils.normalizePath(entry.getKey());
if (normalizedPath != null)
@@ -91,7 +93,7 @@ public class BlobEdits implements Serializable {
it.remove();
}
}
- Map<String, BlobContent> childNewBlobs = new HashMap<>();
+ Map<String, BlobContent> childNewBlobs = new LinkedHashMap<>();
for (Iterator<Map.Entry<String, BlobContent>> it = currentNewBlobs.entrySet().iterator();
it.hasNext();) {
Map.Entry<String, BlobContent> entry = it.next();
@@ -127,29 +129,41 @@ public class BlobEdits implements Serializable {
}
if (!currentNewBlobs.isEmpty()) {
+ Set<String> files = new HashSet<>();
for (Map.Entry<String, BlobContent> entry: currentNewBlobs.entrySet()) {
- List<String> splitted = Splitter.on('/').splitToList(entry.getKey());
-
- 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, entry.getValue().getBytes());
- childMode = entry.getValue().getMode();
+ String path = entry.getKey();
+ if (!path.contains("/")) {
+ files.add(path);
+ entries.add(new TreeFormatterEntry(path, entry.getValue().getMode(),
+ inserter.insert(Constants.OBJ_BLOB, entry.getValue().getBytes())));
+ files.add(path);
+ }
+ }
+ Set<String> topLevelPathSegments = new LinkedHashSet<>();
+ for (String path: currentNewBlobs.keySet()) {
+ if (path.contains("/")) {
+ String topLevelPathSegment = StringUtils.substringBefore(path, "/");
+ if (files.contains(topLevelPathSegment)) {
+ String blobPath = topLevelPathSegment;
+ if (parentPath != null)
+ blobPath = parentPath + "/" + path;
+ throw new ObjectAlreadyExistsException("Blob path already exists: " + blobPath);
} else {
- TreeFormatter childFormatter = new TreeFormatter();
- childFormatter.append(childName, childMode, childId);
- childName = splitted.get(i);
- childId = inserter.insert(childFormatter);
- childMode = FileMode.TREE;
+ topLevelPathSegments.add(topLevelPathSegment);
}
}
-
- Preconditions.checkState(childId!=null && childMode != null && childName != null);
- entries.add(new TreeFormatterEntry(childName, childMode, childId));
+ }
+ for (String topLevelPathSegment: topLevelPathSegments) {
+ Map<String, BlobContent> childNewBlobs = new LinkedHashMap<>();
+ for (Map.Entry<String, BlobContent> entry: currentNewBlobs.entrySet()) {
+ String path = entry.getKey();
+ if (path.startsWith(topLevelPathSegment + "/"))
+ childNewBlobs.put(path.substring(topLevelPathSegment.length()+1), entry.getValue());
+ }
+ ObjectId childTreeId = insertTree(revTree, treeWalk, inserter, treeWalk.getPathString(),
+ Sets.newHashSet(), childNewBlobs);
+ if (childTreeId != null)
+ entries.add(new TreeFormatterEntry(topLevelPathSegment, FileMode.TREE, childTreeId));
}
}
if (!entries.isEmpty()) {
@@ -210,7 +224,7 @@ public class BlobEdits implements Serializable {
treeWalk.addTree(revTree);
ObjectId treeId = insertTree(revTree, treeWalk, inserter, null, new HashSet<>(oldPaths),
- new HashMap<>(newBlobs));
+ new LinkedHashMap<>(newBlobs));
if (treeId != null)
commit.setTreeId(treeId);
diff --git a/core/src/test/java/com/gitplex/server/git/BlobEditsTest.java b/core/src/test/java/com/gitplex/server/git/BlobEditsTest.java
index 39c7469b24..410de8f243 100644
--- a/core/src/test/java/com/gitplex/server/git/BlobEditsTest.java
+++ b/core/src/test/java/com/gitplex/server/git/BlobEditsTest.java
@@ -7,7 +7,7 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -92,7 +92,7 @@ public class BlobEditsTest extends AbstractGitTest {
Set<String> oldPaths = Sets.newHashSet("server/src/com/example/a/a.java",
"server/src/com/example/b/b.java");
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("client/c.java", new BlobContent.Immutable("a".getBytes(), FileMode.REGULAR_FILE));
newBlobs.put("client/d.java", new BlobContent.Immutable("a".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(oldPaths, newBlobs);
@@ -122,7 +122,7 @@ public class BlobEditsTest extends AbstractGitTest {
String refName = "refs/heads/master";
ObjectId oldCommitId = git.getRepository().resolve(refName);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("client/c.java", new BlobContent.Immutable("a".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet("server/src/com/example/a"), newBlobs);
ObjectId newCommitId = edits.commit(git.getRepository(), refName, oldCommitId, oldCommitId, user,
@@ -148,7 +148,7 @@ public class BlobEditsTest extends AbstractGitTest {
String refName = "refs/heads/master";
ObjectId oldCommitId = git.getRepository().resolve(refName);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("client/a.java/a.java", new BlobContent.Immutable("a".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet("server/src/com/example/a/a.java"), newBlobs);
try {
@@ -172,7 +172,7 @@ public class BlobEditsTest extends AbstractGitTest {
String refName = "refs/heads/master";
ObjectId oldCommitId = git.getRepository().resolve(refName);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("/server/src/com/example/c/c.java", new BlobContent.Immutable("c".getBytes(), FileMode.REGULAR_FILE));
newBlobs.put("/server/src/com/example/d/d.java", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet(), newBlobs);
@@ -189,7 +189,7 @@ public class BlobEditsTest extends AbstractGitTest {
}
oldCommitId = newCommitId;
- newBlobs = new HashMap<>();
+ newBlobs = new LinkedHashMap<>();
newBlobs.put("/common/common.java", new BlobContent.Immutable("common".getBytes(), FileMode.REGULAR_FILE));
edits = new BlobEdits(Sets.newHashSet(), newBlobs);
newCommitId = edits.commit(git.getRepository(), refName, oldCommitId, oldCommitId, user, "test add");
@@ -205,6 +205,68 @@ public class BlobEditsTest extends AbstractGitTest {
}
}
+ @Test
+ public void testAddDuplicates() throws IOException {
+ addFileAndCommit("file", "", "first commit");
+
+ String refName = "refs/heads/master";
+ ObjectId oldCommitId = git.getRepository().resolve(refName);
+
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
+ newBlobs.put("/dir/file", new BlobContent.Immutable("c".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("/dir/file/a.java", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ BlobEdits edits = new BlobEdits(Sets.newHashSet(), newBlobs);
+ try {
+ edits.commit(git.getRepository(), refName, oldCommitId, oldCommitId, user, "test add");
+ assertTrue("An ObjectAlreadyException should be thrown", false);
+ } catch (ObjectAlreadyExistsException e) {
+ }
+ }
+
+ @Test
+ public void testOrder() throws IOException {
+ addFileAndCommit("file", "", "first commit");
+ createDir("client/security");
+ addFileAndCommit("client/security/file", "a", "add a");
+
+ String refName = "refs/heads/master";
+ ObjectId oldCommitId = git.getRepository().resolve(refName);
+
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
+ newBlobs.put("client/dir2/file4", new BlobContent.Immutable("c".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("client/security/dir1/file2", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("client/security/dir2/file3", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("client/dir1/file1", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("client/security/dir1/subdir/file", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("client/dir1/file0", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("server/file2", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ newBlobs.put("server/file1", new BlobContent.Immutable("d".getBytes(), FileMode.REGULAR_FILE));
+ BlobEdits edits = new BlobEdits(Sets.newHashSet(), newBlobs);
+ ObjectId newCommitId = edits.commit(git.getRepository(), refName, oldCommitId, oldCommitId, user, "test add");
+
+ List<String> paths = new ArrayList<>();
+ try ( RevWalk revWalk = new RevWalk(git.getRepository());
+ TreeWalk treeWalk = new TreeWalk(git.getRepository());) {
+ treeWalk.addTree(revWalk.parseCommit(newCommitId).getTree());
+ treeWalk.setRecursive(true);
+ while (treeWalk.next()) {
+ paths.add(treeWalk.getPathString());
+ }
+ }
+
+ assertEquals(10, paths.size());
+ assertEquals("client/security/file", paths.get(0));
+ assertEquals("client/security/dir1/file2", paths.get(1));
+ assertEquals("client/security/dir1/subdir/file", paths.get(2));
+ assertEquals("client/security/dir2/file3", paths.get(3));
+ assertEquals("client/dir2/file4", paths.get(4));
+ assertEquals("client/dir1/file1", paths.get(5));
+ assertEquals("client/dir1/file0", paths.get(6));
+ assertEquals("file", paths.get(7));
+ assertEquals("server/file2", paths.get(8));
+ assertEquals("server/file1", paths.get(9));
+ }
+
@Test
public void testModifyFile() throws IOException {
createDir("a");
@@ -224,7 +286,7 @@ public class BlobEditsTest extends AbstractGitTest {
oldPaths.add(treeWalk.getPathString());
}
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("a.b/file.java", new BlobContent.Immutable("hello".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet("a.b/file.java"), newBlobs);
ObjectId newCommitId = edits.commit(git.getRepository(), refName, oldCommitId, oldCommitId, user, "test modify");
@@ -254,7 +316,7 @@ public class BlobEditsTest extends AbstractGitTest {
String refName = "refs/heads/master";
ObjectId oldCommitId = git.getRepository().resolve(refName);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("client/a.java", new BlobContent.Immutable("a".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet(), newBlobs);
try {
@@ -275,7 +337,7 @@ public class BlobEditsTest extends AbstractGitTest {
ObjectId newCommitId = git.getRepository().resolve(refName);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
newBlobs.put("/server/src/com/example/c/c.java", new BlobContent.Immutable("c".getBytes(), FileMode.REGULAR_FILE));
BlobEdits edits = new BlobEdits(Sets.newHashSet(), newBlobs);
try {
diff --git a/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.html b/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.html
index 94e72f01a0..e0776c34ed 100644
--- a/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.html
+++ b/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.html
@@ -9,6 +9,10 @@
<div class="form-group">
<div wicket:id="files"></div>
</div>
+ <div class="form-group">
+ <label>Sub Directory</label>
+ <input wicket:id="subDirectory" class="form-control" placeholder="Optionally specify sub directory to put uploaded files"></input>
+ </div>
<div class="form-group">
<label>Summary Commit Message</label>
<input wicket:id="summaryCommitMessage" class="form-control" placeholder="Add files via upload"></input>
diff --git a/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.java b/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.java
index bc496d2d39..5de6102bb9 100644
--- a/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.java
+++ b/web/src/main/java/com/gitplex/server/web/page/project/blob/BlobUploadPanel.java
@@ -2,7 +2,7 @@ package com.gitplex.server.web.page.project.blob;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
@@ -44,6 +44,8 @@ abstract class BlobUploadPanel extends Panel {
private final BlobRenderContext context;
+ private String subDirectory;
+
private String summaryCommitMessage;
private String detailCommitMessage;
@@ -86,11 +88,25 @@ abstract class BlobUploadPanel extends Panel {
super.onSubmit(target, form);
ProjectManager projectManager = GitPlex.getInstance(ProjectManager.class);
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
+
+ String directory;
+ if (context.getBlobIdent().path != null) {
+ if (subDirectory != null)
+ directory = context.getBlobIdent().path + "/" + subDirectory;
+ else
+ directory = context.getBlobIdent().path;
+ } else {
+ if (subDirectory != null)
+ directory = subDirectory;
+ else
+ directory = null;
+ }
+
for (FileUpload upload: uploads) {
String blobPath = upload.getClientFileName();
- if (context.getBlobIdent().path != null)
- blobPath = context.getBlobIdent().path + "/" + blobPath;
+ if (directory != null)
+ blobPath = directory + "/" + blobPath;
if (projectManager.isModificationNeedsQualityCheck(SecurityUtils.getUser(), context.getProject(),
context.getBlobIdent().revision, blobPath)) {
@@ -146,6 +162,7 @@ abstract class BlobUploadPanel extends Panel {
});
+ form.add(new TextField<String>("subDirectory", new PropertyModel<String>(this, "subDirectory")));
form.add(new TextField<String>("summaryCommitMessage",
new PropertyModel<String>(this, "summaryCommitMessage")));
form.add(new TextArea<String>("detailCommitMessage", new PropertyModel<String>(this, "detailCommitMessage")));
diff --git a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/commitoption/CommitOptionPanel.java b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/commitoption/CommitOptionPanel.java
index ebcc9521f9..39d7415b3b 100644
--- a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/commitoption/CommitOptionPanel.java
+++ b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/commitoption/CommitOptionPanel.java
@@ -2,8 +2,8 @@ package com.gitplex.server.web.page.project.blob.render.commitoption;
import java.io.IOException;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.HashSet;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
@@ -315,7 +315,7 @@ public class CommitOptionPanel extends Panel {
Repository repository = context.getProject().getRepository();
ObjectId newCommitId = null;
- Map<String, BlobContent> newBlobs = new HashMap<>();
+ Map<String, BlobContent> newBlobs = new LinkedHashMap<>();
if (newContentProvider != null) {
if (GitPlex.getInstance(ProjectManager.class).isModificationNeedsQualityCheck(SecurityUtils.getUser(),