Skip to content
Permalink
Browse files
hg: add support for skara and focus on webrev, defpath and jcheck
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 23, 2020
1 parent 2ccceab commit 334899a73fd561b0bb9c334cf8c53f4cac9b6c62
Showing 3 changed files with 96 additions and 131 deletions.
@@ -28,41 +28,63 @@

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class GitSkara {

private static final Map<String, Main> commands = new TreeMap<>();
private static final Set<String> mercurialCommands = Set.of("webrev", "defpath", "jcheck");

private static void usage(String[] args) {
var names = new ArrayList<String>();
names.addAll(commands.keySet());
var isMercurial = args.length > 0 && args[0].equals("--mercurial");
var skaraCommands = Set.of("help", "version", "update");

System.out.println("usage: git skara <" + String.join("|", names) + ">");
var names = new ArrayList<String>();
if (isMercurial) {
names.addAll(mercurialCommands);
names.addAll(skaraCommands);
} else {
names.addAll(commands.keySet());
}

var vcs = isMercurial ? "hg" : "git";
System.out.println("usage: " + vcs + " skara <" + String.join("|", names) + ">");
System.out.println("");
System.out.println("Additional available git commands:");
System.out.println("Additional available " + vcs + " commands:");
for (var name : names) {
if (!skaraCommands.contains(name)) {
System.out.println("- git " + name);
if (isMercurial) {
if (mercurialCommands.contains(name)) {
System.out.println("- hg " + name);
}
} else {
System.out.println("- git " + name);
}
}
}
System.out.println("");
System.out.println("For more information, please see the Skara wiki:");
System.out.println("");
System.out.println(" https://wiki.openjdk.java.net/display/skara");
if (isMercurial) {
System.out.println(" https://wiki.openjdk.java.net/display/SKARA/Mercurial");
} else {
System.out.println(" https://wiki.openjdk.java.net/display/skara");
}
System.out.println("");
System.exit(0);
}

private static void version(String[] args) {
System.out.println("git skara version: " + Version.fromManifest().orElse("unknown"));
var isMercurial = args.length > 0 && args[0].equals("--mercurial");
var vcs = isMercurial ? "hg" : "git";
System.out.println(vcs + " skara version: " + Version.fromManifest().orElse("unknown"));
System.exit(0);
}

private static List<String> config(String key) throws IOException, InterruptedException {
var pb = new ProcessBuilder("git", "config", key);
private static List<String> config(String key, boolean isMercurial) throws IOException, InterruptedException {
var vcs = isMercurial ? "hg" : "git";
var pb = new ProcessBuilder(vcs, "config", key);
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
var p = pb.start();
@@ -72,14 +94,35 @@ private static List<String> config(String key) throws IOException, InterruptedEx
}

private static void update(String[] args) throws IOException, InterruptedException {
var lines = config("include.path");
var path = lines.stream().filter(l -> l.endsWith("skara.gitconfig")).map(Path::of).findFirst();
if (path.isEmpty()) {
System.err.println("error: could not find skara repository");
System.exit(1);
var isMercurial = args.length > 0 && args[0].equals("--mercurial");

String line = null;
if (isMercurial) {
var lines = config("extensions.skara", true);
if (lines.size() == 1) {
line = lines.get(0);
} else {
System.err.println("error: could not find skara repository");
System.exit(1);
}
} else {
var lines = config("include.path", false);
var entry = lines.stream().filter(l -> l.endsWith("skara.gitconfig")).findFirst();
if (entry.isEmpty()) {
System.err.println("error: could not find skara repository");
System.exit(1);
}
line = entry.get();
}

var parent = path.get().getParent();
var expanded = line.startsWith("~") ?
System.getProperty("user.home") + line.substring(1) : line;
var path = Path.of(expanded);
if (Files.exists(path)) {
System.err.println("error: " + path + " does not exist");
System.exit(1);
}
var parent = path.getParent();
var repo = Repository.get(parent);
if (repo.isEmpty()) {
System.err.println("error: could not find skara repository");
150 skara.py
@@ -1,4 +1,4 @@
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@@ -20,12 +20,13 @@
# questions.

import mercurial
import os
import os.path
import subprocess
import sys
import shutil

testedwith = '4.9.2'
testedwith = '4.9.2 5.3'

cmdtable = {}
if hasattr(mercurial, 'registrar') and hasattr(mercurial.registrar, 'command'):
@@ -40,20 +41,15 @@ def decorator(func):
return decorator

def _skara(ui, args, **opts):
for k in opts:
if opts[k] == True:
args.append('--' + k.replace('_', '-'))
elif opts[k] != '' and opts[k] != False:
args.append('--' + k)
args.append(opts[k])
skara = os.path.dirname(os.path.realpath(__file__))
git_skara = os.path.join(skara, 'bin', 'bin', 'git-skara')
if not os.path.isfile(git_skara):
ui.status("Bootstrapping Skara itself...\n")
p = subprocess.Popen(['/bin/sh', 'gradlew'], cwd=skara)
ui.status('Compiling ...\n')
cmd = ['gradlew.bat'] if os.name == 'nt' else ['/bin/sh', 'gradlew']
p = subprocess.Popen(cmd, cwd=skara)
ret = p.wait()
if ret != 0:
ui.error("Error: could not bootstrap Skara\n")
ui.error("Error: could not compile Skara\n")
sys.exit(1)

skara_bin = os.path.join(skara, 'bin')
@@ -63,63 +59,24 @@ def _skara(ui, args, **opts):
shutil.rmtree(skara_bin)
shutil.move(skara_build, skara_bin)

sys.exit(subprocess.call([git_skara] + args))

def _web_url(url):
if url.startswith('git+'):
url = url[len('git+'):]

if url.startswith('http'):
return url

if not url.startswith('ssh://'):
raise ValueError('Unexpected url: ' + url)

without_protocol = url[len('ssh://'):]
first_slash = without_protocol.index('/')
host = without_protocol[:first_slash]

ssh_config = os.path.join(os.path.expanduser('~'), '.ssh', 'config')
if os.path.exists(ssh_config):
with open(ssh_config) as f:
lines = f.readlines()
current = None
for line in lines:
if line.startswith('Host '):
current = line.split(' ')[1].strip()
if line.strip().lower().startswith('hostname') and host == current:
host = line.strip().split(' ')[1]
break

return 'https://' + host + without_protocol[first_slash:]

def _username(ui, opts, url):
web_url = _web_url(url)
username = None
if opts.get('username') == '':
username = ui.config('credential "' + web_url + '"', 'username')
if username == None:
protocol, rest = web_url.split('://')
hostname = rest[:rest.index('/')]
username = ui.config('credential "' + protocol + '://' + hostname + '"', 'username')
if username == None:
username = ui.config('credential', 'username')
return username
for k in opts:
if opts[k] == True:
args.append('--' + k.replace('_', '-'))
elif opts[k] != '' and opts[k] != False:
args.append('--' + k)
args.append(opts[k])
return subprocess.call([git_skara] + args)

fork_opts = [
('u', 'username', '', 'Username on host'),
skara_opts = [
]
@command('fork', fork_opts, 'hg fork URL [DEST]', norepo=True)
def fork(ui, url, dest=None, **opts):
username = _username(ui, opts, url)
args = ['fork', '--mercurial']
if username != None:
args.append("--username")
args.append(username)
args.append(url)
if dest != None:
args.append(dest)
_skara(ui, args)
@command('skara', skara_opts, 'hg skara <defpath|help|jcheck|version|webrev|update>')
def skara(ui, repo, action=None, **opts):
"""
Invoke, list or update Mercurial commands from project Skara
"""
if action == None:
action = 'help'
sys.exit(_skara(ui, [action, '--mercurial'], **opts))

webrev_opts = [
('r', 'rev', '', 'Compare against specified revision'),
@@ -131,11 +88,14 @@ def fork(ui, url, dest=None, **opts):
('b', 'b', False, 'Do not ignore changes in whitespace'),
('C', 'no-comments', False, "Don't show comments"),
('N', 'no-outgoing', False, "Do not compare against remote, use only 'status'"),

]
@command('webrev', webrev_opts, 'hg webrev')
def webrev(ui, repo, **opts):
_skara(ui, ['webrev', '--mercurial'], **opts)
"""
Builds a set of HTML files suitable for doing a
code review of source changes via a web page
"""
sys.exit(_skara(ui, ['webrev', '--mercurial'], **opts))

jcheck_opts = [
('r', 'rev', '', 'Check the specified revision or range (default: tip)'),
@@ -148,59 +108,21 @@ def webrev(ui, repo, **opts):
]
@command('jcheck', jcheck_opts, 'hg jcheck')
def jcheck(ui, repo, **opts):
_skara(ui, ['jcheck', '--mercurial'], **opts)
"""
OpenJDK changeset checker
"""
sys.exit(_skara(ui, ['jcheck', '--mercurial'], **opts))

defpath_opts = [
('u', 'username', '', 'Username for push URL'),
('r', 'remote', '', 'Remote for which to set paths'),
('s', 'secondary', '', 'Secondary peer repostiory base URL'),
('d', 'default', False, 'Use current default path to compute push path'),
('g', 'gated', False, 'Created gated push URL'),
('n', 'dry-run', False, 'Do not perform actions, just print output'),
]
@command('defpath', defpath_opts, 'hg defpath')
def defpath(ui, repo, **opts):
_skara(ui, ['defpath', '--mercurial'], **opts)

info_opts = [
('', 'no-decoration', False, 'Do not prefix lines with any decoration'),
('', 'issues', False, 'Show issues'),
('', 'reviewers', False, 'Show reviewers'),
('', 'summary', False, 'Show summary (if present)'),
('', 'sponsor', False, 'Show sponsor (if present)'),
('', 'author', False, 'Show author'),
('', 'contributors', False, 'Show contributors')
]
@command('info', info_opts, 'hg info')
def info(ui, repo, rev, **opts):
_skara(ui, ['info', '--mercurial', rev], **opts)

pr_opts = [
('u', 'username', '', 'Username on host'),
('r', 'remote', '', 'Name of path, defaults to "default"'),
('b', 'branch', '', 'Name of target branch, defaults to "default"'),
('', 'authors', '', 'Comma separated list of authors'),
('', 'assignees', '', 'Comma separated list of assignees'),
('', 'labels', '', 'Comma separated list of labels'),
('', 'columns', '', 'Comma separated list of columns to show'),
('', 'no-decoration', False, 'Do not prefix lines with any decoration')
]
@command('pr', pr_opts, 'hg pr <list|fetch|show|checkout|apply|integrate|approve|create|close|update>')
def pr(ui, repo, action, n=None, **opts):
path = opts.get('remote')
if path == '':
path = 'default'
url = ui.config('paths', path)
username = _username(ui, opts, url)
args = ['pr', '--mercurial']
if username != None:
args.append('--username')
args.append(username)
args.append(action)
if n != None:
args.append(n)
_skara(ui, args, **opts)

@command('skara', [], 'hg skara')
def skara(ui, repo, action, **opts):
_skara(ui, [action, '--mercurial'], **opts)
"""
Examine and manipulate default path settings
"""
sys.exit(_skara(ui, ['defpath', '--mercurial'], **opts))
@@ -46,4 +46,4 @@ if [ "${OS}" = "Linux" -o "${OS}" = "Darwin" ]; then
exec "${DIR}/bin/bin/git-skara" "${@}"
else
exec "${DIR}/bin/bin/git-skara.bat" "${@}"
fi
fi

0 comments on commit 334899a

Please sign in to comment.