// Copyright (C) 2012 The Android Open Source Project
//
// 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.transport.ssh.commands;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.sshd.server.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SshCommand extends BaseCommand {

	protected Logger log = LoggerFactory.getLogger(getClass());
	protected PrintWriter stdout;
	protected PrintWriter stderr;

	@Override
	public void start(Environment env) throws IOException {
		startThread(new CommandRunnable() {
			@Override
			public void run() throws Exception {
				parseCommandLine();
				stdout = toPrintWriter(out);
				stderr = toPrintWriter(err);
				try {
					SshCommand.this.run();
				} finally {
					stdout.flush();
					stderr.flush();
				}
			}
		});
	}

	protected abstract void run() throws UnloggedFailure, Failure, Exception;
}

@@ -702,16 +702,108 @@
 tWriter;
+%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.text.MessageFormat;
 %0A%0Aimport
@@ -839,16 +839,59 @@
 onment;%0A
+import org.eclipse.jgit.util.SystemReader;%0A
 import o
@@ -940,16 +940,117 @@
 ctory;%0A%0A
+import com.gitblit.Keys;%0Aimport com.gitblit.manager.IGitblit;%0Aimport com.gitblit.utils.StringUtils;%0A%0A
 public a
@@ -1577,16 +1577,16 @@
 %09%7D%0A%09%09%09%7D%0A
-
 %09%09%7D);%0A%09%7D
@@ -1587,16 +1587,952 @@
 %7D);%0A%09%7D%0A%0A
+%09protected String getHostname() %7B%0A%09%09IGitblit gitblit = getContext().getGitblit();%0A%09%09String host = null;%0A%09%09String url = gitblit.getSettings().getString(Keys.web.canonicalUrl, %22https://localhost:8443%22);%0A%09%09if (url != null) %7B%0A%09%09%09try %7B%0A%09%09%09%09host = new URL(url).getHost();%0A%09%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (StringUtils.isEmpty(host)) %7B%0A%09%09%09host = SystemReader.getInstance().getHostname();%0A%09%09%7D%0A%09%09return host;%0A%09%7D%0A%0A%09protected String getRepositoryUrl(String repository) %7B%0A%09%09String username = getContext().getClient().getUsername();%0A%09%09String hostname = getHostname();%0A%09%09int port = getContext().getGitblit().getSettings().getInteger(Keys.git.sshPort, 0);%0A%09%09if (port == 22) %7B%0A%09%09%09// standard port%0A%09%09%09return MessageFormat.format(%22%7B0%7D@%7B1%7D/%7B2%7D.git%22, username, hostname, repository);%0A%09%09%7D else %7B%0A%09%09%09// non-standard port%0A%09%09%09return MessageFormat.format(%22ssh://%7B0%7D@%7B1%7D:%7B2,number,0%7D/%7B3%7D%22,%0A%09%09%09%09%09username, hostname, port, repository);%0A%09%09%7D%0A%09%7D%0A%0A
 %09protect

Add hostname and ssh repo url methodso to SshCommand