Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git-pr: do not assume a master branch #415

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -233,9 +233,9 @@ private static List<String> issuesFromPullRequest(PullRequest pr) {
return issues;
}

private static String jbsProjectFromJcheckConf(Repository repo) throws IOException {
var conf = JCheckConfiguration.from(repo, repo.resolve("master").orElseThrow(() ->
new IOException("Could not resolve 'master' branch")
private static String jbsProjectFromJcheckConf(Repository repo, String targetBranch) throws IOException {
var conf = JCheckConfiguration.from(repo, repo.resolve(targetBranch).orElseThrow(() ->
new IOException("Could not resolve '" + targetBranch + "' branch")
));

return conf.general().jbs();
@@ -898,9 +898,57 @@ public static void main(String[] args) throws IOException, InterruptedException
}
}

var remoteRepo = host.repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
if (token == null) {
GitCredentials.approve(credentials);
}
var parentRepo = remoteRepo.parent().orElseThrow(() ->
new IOException("error: remote repository " + remotePullPath + " is not a fork of any repository")
);

var targetBranch = getOption("branch", "create", arguments);
if (targetBranch == null) {
targetBranch = "master";
var upstreamBranchNames = repo.remoteBranches(parentRepo.webUrl().toString())
.stream()
.map(r -> r.name())
.collect(Collectors.toSet());
var remoteBranches = repo.branches(remote);
var candidates = new ArrayList<Branch>();
for (var b : remoteBranches) {
var withoutRemotePrefix = b.name().substring(remote.length() + 1);
if (upstreamBranchNames.contains(withoutRemotePrefix)) {
candidates.add(b);
}
}

var localBranches = repo.branches();
Branch closest = null;
var shortestDistance = Integer.MAX_VALUE;
for (var b : candidates) {
var from = b.name();
for (var localBranch : localBranches) {
var trackingBranch = repo.upstreamFor(localBranch);
if (trackingBranch.isPresent() &&
trackingBranch.get().equals(b.name())) {
from = localBranch.name();
}
}
var distance = repo.commitMetadata(from + "..." + currentBranch.name()).size();
if (distance < shortestDistance) {
closest = b;
shortestDistance = distance;
}
}

if (closest != null) {
targetBranch = closest.name().substring(remote.length() + 1);
} else {
System.err.println("error: cannot automatically infer target branch");
System.err.println(" use --branch to specify target branch");
System.exit(1);
}
}
var commits = repo.commits(targetBranch + ".." + upstream.get()).asList();
if (commits.isEmpty()) {
@@ -918,16 +966,7 @@ public static void main(String[] args) throws IOException, InterruptedException
}
}

var remoteRepo = host.repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
if (token == null) {
GitCredentials.approve(credentials);
}
var parentRepo = remoteRepo.parent().orElseThrow(() ->
new IOException("error: remote repository " + remotePullPath + " is not a fork of any repository"));

var project = jbsProjectFromJcheckConf(repo);
var project = jbsProjectFromJcheckConf(repo, targetBranch);
var issue = getIssue(currentBranch, project);
var file = Files.createTempFile("PULL_REQUEST_", ".md");
if (issue.isPresent()) {
@@ -34,6 +34,7 @@ public interface ReadOnlyRepository {
Optional<Bookmark> currentBookmark() throws IOException;
Branch defaultBranch() throws IOException;
List<Branch> branches() throws IOException;
List<Branch> branches(String remote) throws IOException;
Optional<Tag> defaultTag() throws IOException;
List<Tag> tags() throws IOException;
Commits commits() throws IOException;
@@ -120,6 +120,15 @@ public List<Branch> branches() throws IOException {
}
}

public List<Branch> branches(String remote) throws IOException {
try (var p = capture("git", "for-each-ref", "--format=%(refname:short)", "refs/remotes/" + remote + "/")) {
return await(p).stdout()
.stream()
.map(Branch::new)
.collect(Collectors.toList());
}
}

public List<Tag> tags() throws IOException {
try (var p = capture("git", "for-each-ref", "--format=%(refname:short)", "refs/tags")) {
return await(p).stdout()
@@ -132,6 +132,12 @@ public List<Branch> branches() throws IOException {
}
}

@Override
public List<Branch> branches(String remote) throws IOException {
// Mercurial does not have namespacing of branch names
return branches();
}

@Override
public List<Tag> tags() throws IOException {
try (var p = capture("hg", "tags")) {