Skip to content

Commit

Permalink
Friendly URL for EditFile Page #974
Browse files Browse the repository at this point in the history
+ Prevent Edit of old doc version
  • Loading branch information
paulsputer committed Mar 27, 2016
1 parent 5851e12 commit d9606a2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/gitblit/utils/JGitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2727,5 +2727,22 @@ public static boolean commitIndex(Repository db, String branch, DirCache index,
}
return success;
}

/**
* Returns true if the commit identified by commitId is at the tip of it's branch.
*
* @param repository
* @param commitId
* @return true if the given commit is the tip
*/
public static boolean isTip(Repository repository, String commitId) {
try {
RefModel tip = getBranch(repository, commitId);
return (tip != null);
} catch (Exception e) {
LOGGER.error("Failed to determine isTip", e);
}
return false;
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/gitblit/wicket/GitBlitWebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.gitblit.wicket.pages.ComparePage;
import com.gitblit.wicket.pages.DocPage;
import com.gitblit.wicket.pages.DocsPage;
import com.gitblit.wicket.pages.EditFilePage;
import com.gitblit.wicket.pages.EditMilestonePage;
import com.gitblit.wicket.pages.EditRepositoryPage;
import com.gitblit.wicket.pages.EditTicketPage;
Expand Down Expand Up @@ -230,6 +231,7 @@ public void init() {
// setup the markup document urls
mount("/docs", DocsPage.class, "r", "h");
mount("/doc", DocPage.class, "r", "h", "f");
mount("/editfile", EditFilePage.class, "r", "h", "f");

// federation urls
mount("/proposal", ReviewProposalPage.class, "t");
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/gitblit/wicket/GitBlitWebApp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,9 @@ gb.filestore = filestore
gb.filestoreStats = Filestore contains {0} files with a total size of {1}. ({2} remaining)
gb.statusChangedOn = status changed on
gb.statusChangedBy = status changed by
gb.filestoreHelp = How to use the Filestore?
gb.filestoreHelp = How to use the Filestore?
gb.editFile = edit file
gb.continueEditing = Continue Editing
gb.commitChanges = Commit Changes
gb.fileNotMergeable = Unable to commit {0}. This file can not be automatically merged.
gb.fileCommitted = Successfully committed {0}.
4 changes: 2 additions & 2 deletions src/main/java/com/gitblit/wicket/pages/EditFilePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ <h3>Commit Document Changes</h3>
<textarea style="width:100%; resize:none" wicket:id="commitMessage"></textarea>
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn">Continue editing</a>
<a href="#" onclick="commitChanges()" class="btn btn-primary">Commit changes</a>
<a href="#" data-dismiss="modal" class="btn"><wicket:message key="gb.continueEditing"></wicket:message></a>
<a href="#" onclick="commitChanges()" class="btn btn-primary"><wicket:message key="gb.commitChanges"></wicket:message></a>
</div>
</div>
</form>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/gitblit/wicket/pages/EditFilePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -51,7 +52,7 @@
import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
import com.gitblit.wicket.WicketUtils;

@CacheControl(LastModified.BOOT)
@CacheControl(LastModified.REPOSITORY)
public class EditFilePage extends RepositoryPage {

public EditFilePage(final PageParameters params) {
Expand Down Expand Up @@ -96,9 +97,9 @@ public EditFilePage(final PageParameters params) {
Fragment fragment;
String displayedCommitId = commit.getId().getName();
MarkupDocument markupDoc = processor.parse(repositoryName, displayedCommitId, documentPath, markupText);
logger.info("Loading Edit File page: " + displayedCommitId);
logger.trace("Loading Edit File page: " + displayedCommitId);

if (currentUser.canEdit(getRepositoryModel())) {
if (currentUser.canEdit(getRepositoryModel()) && JGitUtils.isTip(getRepository(), objectId.toString())) {

final Model<String> documentContent = new Model<String>(markupDoc.markup);
final Model<String> commitMessage = new Model<String>("Document update");
Expand All @@ -112,7 +113,6 @@ public EditFilePage(final PageParameters params) {

@Override
protected void onSubmit() {

final Repository repository = getRepository();
final String document = documentContent.getObject();
final String message = commitMessage.getObject();
Expand All @@ -125,7 +125,7 @@ protected void onSubmit() {
try {
ObjectId docAtLoad = getRepository().resolve(commitIdAtLoad.getObject());

logger.info("Commiting Edit File page: " + commitIdAtLoad.getObject());
logger.trace("Commiting Edit File page: " + commitIdAtLoad.getObject());

DirCache index = DirCache.newInCore();
DirCacheBuilder builder = index.builder();
Expand Down Expand Up @@ -156,12 +156,12 @@ protected void onSubmit() {
}

if (success == false) {
EditFilePage.this.error("Unable to commit document " + path, false);
getSession().error(MessageFormat.format(getString("gb.fileNotMergeable"),path));
return;
}

getSession().info(MessageFormat.format(getString("gb.fileCommitted"),path));
setResponsePage(EditFilePage.class, params);
return;
}
};

Expand Down Expand Up @@ -197,12 +197,12 @@ protected void onSubmit() {
fragment.add(new ExternalLink("rawLink", rawUrl));

add(fragment);

}

@Override
protected String getPageName() {
return getString("gb.docs");
return getString("gb.editFile");
}

@Override
Expand All @@ -212,7 +212,7 @@ protected boolean isCommitPage() {

@Override
protected Class<? extends BasePage> getRepoNavPageClass() {
return DocsPage.class;
return EditFilePage.class;
}


Expand Down

0 comments on commit d9606a2

Please sign in to comment.