/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.wicket.pages;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;

import com.gitblit.utils.DiffUtils;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.CacheControl;
import com.gitblit.wicket.CacheControl.LastModified;
import com.gitblit.wicket.GitBlitWebSession;
import com.gitblit.wicket.WicketUtils;

@CacheControl(LastModified.BOOT)
public class PatchPage extends SessionPage {

	public PatchPage(PageParameters params) {
		super(params);

		if (!params.containsKey("r")) {
			GitBlitWebSession.get().cacheErrorMessage(getString("gb.repositoryNotSpecified"));
			redirectToInterceptPage(new RepositoriesPage());
			return;
		}

		final String repositoryName = WicketUtils.getRepositoryName(params);
		final String baseObjectId = WicketUtils.getBaseObjectId(params);
		final String objectId = WicketUtils.getObject(params);
		final String blobPath = WicketUtils.getPath(params);

		Repository r = app().repositories().getRepository(repositoryName);
		if (r == null) {
			GitBlitWebSession.get().cacheErrorMessage(getString("gb.canNotLoadRepository") + " " + repositoryName);
			redirectToInterceptPage(new RepositoriesPage());
			return;
		}

		RevCommit commit = JGitUtils.getCommit(r, objectId);
		if (commit == null) {
			GitBlitWebSession.get().cacheErrorMessage(getString("gb.commitIsNull"));
			redirectToInterceptPage(new RepositoriesPage());
			return;
		}

		RevCommit baseCommit = null;
		if (!StringUtils.isEmpty(baseObjectId)) {
			baseCommit = JGitUtils.getCommit(r, baseObjectId);
		}
		String patch = DiffUtils.getCommitPatch(r, baseCommit, commit, blobPath);
		add(new Label("patchText", patch));
		r.close();
	}
}

@@ -817,24 +817,106 @@
 vCommit;%0D%0A%0D%0A
+import com.gitblit.models.RepositoryModel;%0D%0Aimport com.gitblit.models.UserModel;%0D%0A
 import com.g
@@ -1299,16 +1299,22 @@
 tchPage(
+final 
 PagePara
@@ -1393,49 +1393,13 @@
 %0A%09%09%09
-GitBlitWebSession.get().cacheErrorMessage
+error
 (get
@@ -1486,36 +1486,24 @@
 esPage());%0D%0A
-%09%09%09return;%0D%0A
 %09%09%7D%0D%0A%0D%0A%09%09fin
@@ -1759,20 +1759,126 @@
 %0D%0A%09%09
-Repository r
+GitBlitWebSession session = GitBlitWebSession.get();%0D%0A%09%09UserModel user = session.getUser();%0D%0A%0D%0A%09%09RepositoryModel model
  = a
@@ -1910,17 +1910,28 @@
 pository
-(
+Model(user, 
 reposito
@@ -1946,17 +1946,21 @@
 %0D%0A%09%09if (
-r
+model
  == null
@@ -1971,49 +1971,284 @@
 %0A%09%09%09
-GitBlitWebSession.get().cacheErrorMessage
+// user does not have permission%0D%0A%09%09%09error(getString(%22gb.canNotLoadRepository%22) + %22 %22 + repositoryName);%0D%0A%09%09%09redirectToInterceptPage(new RepositoriesPage());%0D%0A%09%09%09return;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09Repository r = app().repositories().getRepository(repositoryName);%0D%0A%09%09if (r == null) %7B%0D%0A%09%09%09error
 (get
@@ -2880,11 +2880,13 @@
 );%0D%0A%09%7D%0D%0A
+%0D%0A
 %7D%0D%0A

Enforce repository permissions in patch page