The Unified and Autonomous Development Platform
Display changes when commit files if the file has been modified by others
robin shineb4dbab1618e7
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 455e9d75eb..d9d548f22c 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@@ -424,7 +424,7 @@ public class Repository extends AbstractEntity implements UserBelonging {* diffs of multiple blob changes concurrently, and this method will be* accessed concurrently in that special case.*- * @param ident+ * @param blobIdent* ident of the blob* @return* blob of specified blob ident@@ -432,50 +432,50 @@ public class Repository extends AbstractEntity implements UserBelonging {* ObjectNotExistException if blob of specified ident can not be found in repository**/- public Blob getBlob(BlobIdent ident) {- Preconditions.checkArgument(ident.revision!=null && ident.path!=null && ident.mode!=null,+ public Blob getBlob(BlobIdent blobIdent) {+ Preconditions.checkArgument(blobIdent.revision!=null && blobIdent.path!=null && blobIdent.mode!=null,"Revision, path and mode of ident param should be specified");- Blob blob = getBlobCache().get(ident);+ Blob blob = getBlobCache().get(blobIdent);if (blob == null) {- if (ident.id != null) {+ if (blobIdent.id != null) {try (FileRepository jgitRepo = openAsJGitRepo()) {- if (ident.isGitLink()) {- String url = getSubmodules(ident.revision).get(ident.path);+ if (blobIdent.isGitLink()) {+ String url = getSubmodules(blobIdent.revision).get(blobIdent.path);if (url == null)- throw new ObjectNotExistException("Unable to find submodule '" + ident.path + "' in .gitmodules");- blob = new Blob(ident, new Submodule(url, ident.id).toString().getBytes());- } else if (ident.isTree()) {- throw new NotFileException("Path '" + ident.path + "' is a tree");+ throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");+ blob = new Blob(blobIdent, new Submodule(url, blobIdent.id).toString().getBytes());+ } else if (blobIdent.isTree()) {+ throw new NotFileException("Path '" + blobIdent.path + "' is a tree");} else {- ObjectLoader objectLoader = jgitRepo.open(ObjectId.fromString(ident.id), Constants.OBJ_BLOB);- blob = readBlob(objectLoader, ident);+ ObjectLoader objectLoader = jgitRepo.open(ObjectId.fromString(blobIdent.id), Constants.OBJ_BLOB);+ blob = readBlob(objectLoader, blobIdent);}- getBlobCache().put(ident, blob);+ getBlobCache().put(blobIdent, blob);} catch (IOException e) {throw new RuntimeException(e);}} else {try (FileRepository jgitRepo = openAsJGitRepo(); RevWalk revWalk = new RevWalk(jgitRepo)) {- ObjectId commitId = getObjectId(ident.revision);+ ObjectId commitId = getObjectId(blobIdent.revision);RevTree revTree = revWalk.parseCommit(commitId).getTree();- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, ident.path, revTree);+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, blobIdent.path, revTree);if (treeWalk != null) {- if (ident.isGitLink()) {- String url = getSubmodules(ident.revision).get(ident.path);+ if (blobIdent.isGitLink()) {+ String url = getSubmodules(blobIdent.revision).get(blobIdent.path);if (url == null)- throw new ObjectNotExistException("Unable to find submodule '" + ident.path + "' in .gitmodules");+ throw new ObjectNotExistException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules");String hash = treeWalk.getObjectId(0).name();- blob = new Blob(ident, new Submodule(url, hash).toString().getBytes());- } else if (ident.isTree()) {- throw new NotFileException("Path '" + ident.path + "' is a tree");+ blob = new Blob(blobIdent, new Submodule(url, hash).toString().getBytes());+ } else if (blobIdent.isTree()) {+ throw new NotFileException("Path '" + blobIdent.path + "' is a tree");} else {ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));- blob = readBlob(objectLoader, ident);+ blob = readBlob(objectLoader, blobIdent);}- getBlobCache().put(ident, blob);+ getBlobCache().put(blobIdent, blob);} else {- throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");+ throw new ObjectNotExistException("Unable to find blob path '" + blobIdent.path + "' in revision '" + blobIdent.revision + "'");}} catch (IOException e) {throw new RuntimeException(e);diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.htmlindex 9954a5bba0..a30cd451ac 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.html+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.html@@ -5,8 +5,11 @@</div><div class="body"><div wicket:id="feedback"></div>- <div wicket:id="hasChanges" class="has-changes alert alert-danger"><i class="fa fa-warning"></i> Someone has committed since you start editing.- <a wicket:id="changes">See what changed.</a>+ <div wicket:id="changed" class="changed">+ <div class="alert alert-danger"><i class="fa fa-warning"></i> Someone has committed since you start editing.+ <a onclick="$(this).parent().next().show();">See what changed.</a>+ </div>+ <div wicket:id="changes" class="changes"></div></div><form wicket:id="form" class="dirty-aware leave-confirm"><div class="form-group">diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.javaindex 7b80654fa2..117e267728 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/EditSavePanel.java@@ -20,9 +20,11 @@ import org.apache.wicket.markup.html.form.TextArea;import org.apache.wicket.markup.html.form.TextField;import org.apache.wicket.markup.html.panel.Panel;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.apache.wicket.util.time.Duration;+import org.eclipse.jgit.diff.DiffEntry;import org.eclipse.jgit.internal.storage.file.FileRepository;import org.eclipse.jgit.lib.ObjectId;import org.eclipse.jgit.revwalk.RevCommit;@@ -30,7 +32,11 @@ import org.eclipse.jgit.revwalk.RevWalk;import org.eclipse.jgit.treewalk.TreeWalk;import com.google.common.base.Preconditions;+import com.pmease.commons.git.Blob;+import com.pmease.commons.git.BlobChange;+import com.pmease.commons.git.BlobIdent;import com.pmease.commons.git.FileEdit;+import com.pmease.commons.git.LineProcessor;import com.pmease.commons.git.PathAndContent;import com.pmease.commons.git.exception.NotTreeException;import com.pmease.commons.git.exception.ObjectAlreadyExistException;@@ -39,8 +45,13 @@ import com.pmease.commons.wicket.behavior.DirtyIgnoreBehavior;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.Comment;+import com.pmease.gitplex.core.model.PullRequest;import com.pmease.gitplex.core.model.Repository;import com.pmease.gitplex.core.model.User;+import com.pmease.gitplex.web.component.diff.blob.BlobDiffPanel;+import com.pmease.gitplex.web.component.diff.revision.DiffMode;+import com.pmease.gitplex.web.component.diff.revision.LineProcessOption;@SuppressWarnings("serial")public abstract class EditSavePanel extends Panel {@@ -63,6 +74,8 @@ public abstract class EditSavePanel extends Panel {private String detailCommitMessage;+ private BlobChange change;+public EditSavePanel(String id, IModel<Repository> repoModel, String refName,@Nullable String oldPath, @Nullable PathAndContent newFile,ObjectId prevCommitId, @Nullable CancelListener cancelListener) {@@ -108,6 +121,24 @@ public abstract class EditSavePanel extends Panel {}+ private void newChangedContainer(@Nullable AjaxRequestTarget target) {+ WebMarkupContainer changedContainer = new WebMarkupContainer("changed");+ changedContainer.setVisible(change != null);+ changedContainer.setOutputMarkupPlaceholderTag(true);+ if (change != null) {+ changedContainer.add(new BlobDiffPanel("changes", repoModel,+ new Model<PullRequest>(null), new Model<Comment>(null), change, DiffMode.UNIFIED));+ } else {+ changedContainer.add(new WebMarkupContainer("changes"));+ }+ if (target != null) {+ replace(changedContainer);+ target.add(changedContainer);+ } else {+ add(changedContainer);+ }+ }+@Overrideprotected void onInitialize() {super.onInitialize();@@ -116,17 +147,7 @@ public abstract class EditSavePanel extends Panel {feedback.setOutputMarkupPlaceholderTag(true);add(feedback);- final WebMarkupContainer hasChangesContainer = new WebMarkupContainer("hasChanges");- hasChangesContainer.setVisibilityAllowed(false);- hasChangesContainer.setOutputMarkupPlaceholderTag(true);- hasChangesContainer.add(new AjaxLink<Void>("changes") {-- @Override- public void onClick(AjaxRequestTarget target) {- }-- });- add(hasChangesContainer);+ newChangedContainer(null);Form<?> form = new Form<Void>("form");add(form);@@ -181,89 +202,90 @@ public abstract class EditSavePanel extends Panel {protected void onSubmit(AjaxRequestTarget target, Form<?> form) {super.onSubmit(target, form);+ change = null;+if (newFile != null && StringUtils.isBlank(newFile.getPath())) {EditSavePanel.this.error("Please specify file name.");target.add(feedback);- return;- }-- try (FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo()) {- String commitMessage = summaryCommitMessage;- if (StringUtils.isBlank(commitMessage))- commitMessage = getDefaultCommitMessage();- if (StringUtils.isNotBlank(detailCommitMessage))- commitMessage += "\n\n" + detailCommitMessage;- User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());-- FileEdit edit = new FileEdit(oldPath, newFile);- ObjectId newCommitId = null;- while(newCommitId == null) {- try {- newCommitId = edit.commit(jgitRepo, refName,- 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);+ } else {+ try (FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo()) {+ String commitMessage = summaryCommitMessage;+ if (StringUtils.isBlank(commitMessage))+ commitMessage = getDefaultCommitMessage();+ if (StringUtils.isNotBlank(detailCommitMessage))+ commitMessage += "\n\n" + detailCommitMessage;+ User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());- prevCommitId = currentCommitId;+ FileEdit edit = new FileEdit(oldPath, newFile);+ ObjectId newCommitId = null;+ while(newCommitId == null) {+ try {+ newCommitId = edit.commit(jgitRepo, refName,+ 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);+ prevCommitId = currentCommitId;- 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))) {- if (treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {- if (edit.getNewFile() != null) {- edit = new FileEdit(null, edit.getNewFile());- hasChangesContainer.setVisibilityAllowed(true);- target.add(hasChangesContainer);- break;+ 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))) {+ // mark changed if original file exists and content or mode has been modified+ // by others+ if (treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {+ if (edit.getNewFile() != null) {+ edit = new FileEdit(null, edit.getNewFile());+ change = getChange(treeWalk, prevCommit, currentCommit);+ break;+ } else {+ newCommitId = currentCommitId;+ break;+ }} else {- newCommitId = currentCommitId;+ change = getChange(treeWalk, prevCommit, currentCommit);break;}- } else {- hasChangesContainer.setVisibilityAllowed(true);- target.add(hasChangesContainer);- break;}}}- }- 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 (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))) {+ // if added/renamed file exists and content or mode has been modified+ // by others+ change = getChange(treeWalk, prevCommit, currentCommit);+ break;+ }}- }- }-- } catch (IOException e2) {- throw new RuntimeException(e2);+ }+ } 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.");+ target.add(feedback);+ break;+ } catch (NotTreeException e) {+ EditSavePanel.this.error("A file exists where you’re trying to create a subdirectory. "+ + "Choose a new path and try again..");+ target.add(feedback);+ break;}- } catch (ObjectAlreadyExistException e) {- EditSavePanel.this.error("A file with same name already exists. "- + "Please choose a different name and try again.");- target.add(feedback);- break;- } catch (NotTreeException e) {- EditSavePanel.this.error("A file exists where you’re trying to create a subdirectory. "- + "Choose a new path and try again..");- target.add(feedback);- break;}+ if (newCommitId != null)+ onCommitted(target, newCommitId);+ else+ newChangedContainer(target);}- if (newCommitId != null)- onCommitted(target, newCommitId);}}@@ -286,6 +308,41 @@ public abstract class EditSavePanel extends Panel {setOutputMarkupId(true);}++ private BlobChange getChange(TreeWalk treeWalk, RevCommit oldCommit, RevCommit newCommit) {+ DiffEntry.ChangeType changeType = DiffEntry.ChangeType.MODIFY;+ BlobIdent oldBlobIdent = new BlobIdent();+ oldBlobIdent.revision = oldCommit.name();+ if (!treeWalk.getObjectId(0).equals(ObjectId.zeroId())) {+ oldBlobIdent.path = treeWalk.getPathString();+ oldBlobIdent.mode = treeWalk.getRawMode(0);+ } else {+ changeType = DiffEntry.ChangeType.ADD;+ }++ BlobIdent newBlobIdent = new BlobIdent();+ newBlobIdent.revision = newCommit.name();+ if (!treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {+ newBlobIdent.path = treeWalk.getPathString();+ newBlobIdent.mode = treeWalk.getRawMode(1);+ } else {+ changeType = DiffEntry.ChangeType.DELETE;+ }++ return new BlobChange(changeType, oldBlobIdent, newBlobIdent) {++ @Override+ public Blob getBlob(BlobIdent blobIdent) {+ return repoModel.getObject().getBlob(blobIdent);+ }++ @Override+ public LineProcessor getLineProcessor() {+ return LineProcessOption.IGNORE_NOTHING;+ }++ };+ }@Overridepublic void renderHead(IHeaderResponse response) {diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/edit-save.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/edit-save.cssindex 6e1d9aac17..135e471a19 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/edit-save.css+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/editsave/edit-save.css@@ -7,3 +7,19 @@font-size: 16px;margin-bottom: 20px;}+.edit-save>.body>.changed>.changes {+ margin-bottom: 20px;+ display: none;+}+.edit-save .no-diff>.head, .edit-save .text-diff>.head {+ display: none;+}+.edit-save .text-diff>.body>tbody>tr:first-child>td:first-child {+ border-radius: 4px 0 0 0;+}+.edit-save .text-diff>.body>tbody>tr:first-child>td:last-child {+ border-radius: 0 4px 0 0;+}+.edit-save .text-diff>.body>tbody>tr:first-child>td.expander a {+ border-radius: 4px 0 0 0;+}