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

92: GitRepository::currentBranch fails when on a detached HEAD #278

Closed
wants to merge 1 commit 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
@@ -519,7 +519,11 @@ public static void main(String[] args) throws IOException, InterruptedException

System.exit(0);
}
var currentBranch = repo.currentBranch();
var currentBranch = repo.currentBranch().orElseGet(() -> {
System.err.println("error: the repository is in a detached HEAD state");
edvbld marked this conversation as resolved.
Show resolved Hide resolved
System.exit(1);
return null;
});
if (currentBranch.equals(repo.defaultBranch())) {
System.err.println("error: you should not create pull requests from the 'master' branch");
System.err.println("");
@@ -85,6 +85,11 @@ public static void main(String[] args) throws IOException, InterruptedException
var repo = Repository.get(cwd).or(die("error: no repository found at " + cwd.toString())).get();
var remote = arguments.at(0).orString("origin");

System.exit(pushAndTrack(remote, repo.currentBranch()));
var currentBranch = repo.currentBranch();
if (currentBranch.isEmpty()) {
System.err.println("error: the repository is in a detached HEAD state");
System.exit(1);
}
System.exit(pushAndTrack(remote, repo.currentBranch().get()));
}
}
@@ -206,15 +206,18 @@ private static void generate(String[] args) throws IOException {
}
if (issue == null) {
var pattern = Pattern.compile("(?:(JDK|CODETOOLS|JMC)-)?([0-9]+).*");
var branch = repo.currentBranch().name().toUpperCase();
var m = pattern.matcher(branch);
if (m.matches()) {
var project = m.group(1);
if (project == null) {
project = "JDK";
var currentBranch = repo.currentBranch();
if (currentBranch.isPresent()) {
var branchName = currentBranch.get().name().toUpperCase();
var m = pattern.matcher(branchName);
if (m.matches()) {
var project = m.group(1);
if (project == null) {
project = "JDK";
}
var id = m.group(2);
issue = "https://bugs.openjdk.java.net/browse/" + project + "-" + id;
}
var id = m.group(2);
issue = "https://bugs.openjdk.java.net/browse/" + project + "-" + id;
}
}

@@ -36,8 +36,8 @@ class TestRepository implements ReadOnlyRepository {
private Tag defaultTag = null;
private List<Tag> tags = new ArrayList<Tag>();

public Branch currentBranch() throws IOException {
return currentBranch;
public Optional<Branch> currentBranch() throws IOException {
return Optional.empty();
}

void setCurrentBranch(Branch branch) {
@@ -30,7 +30,7 @@

public interface ReadOnlyRepository {
Hash head() throws IOException;
Branch currentBranch() throws IOException;
Optional<Branch> currentBranch() throws IOException;
Optional<Bookmark> currentBookmark() throws IOException;
Branch defaultBranch() throws IOException;
List<Branch> branches() throws IOException;
@@ -661,13 +661,13 @@ public Optional<Hash> resolve(String ref) throws IOException {
}

@Override
public Branch currentBranch() throws IOException {
public Optional<Branch> currentBranch() throws IOException {
try (var p = capture("git", "symbolic-ref", "--short", "HEAD")) {
var res = await(p);
if (res.stdout().size() != 1) {
throw new IOException("Unexpected output\n" + res);
var res = p.await();
if (res.status() == 0 && res.stdout().size() == 1) {
return Optional.of(new Branch(res.stdout().get(0)));
}
return new Branch(res.stdout().get(0));
return Optional.empty();
}
}

@@ -599,21 +599,23 @@ public boolean isAncestor(Hash ancestor, Hash descendant) throws IOException {

@Override
public void rebase(Hash hash, String committerName, String committerEmail) throws IOException {
var current = currentBranch().name();
var current = currentBranch().orElseThrow(() ->
new IOException("No current branch to rebase upon")
edvbld marked this conversation as resolved.
Show resolved Hide resolved
);
try (var p = capture("hg", "--config", "extensions.rebase=",
"rebase", "--dest", hash.hex(), "--base", current)) {
"rebase", "--dest", hash.hex(), "--base", current.name())) {
await(p);
}
}

@Override
public Branch currentBranch() throws IOException {
public Optional<Branch> currentBranch() throws IOException {
try (var p = capture("hg", "branch")) {
var res = await(p);
if (res.stdout().size() != 1) {
throw new IllegalStateException("No current branch\n" + res);
return Optional.empty();
}
return new Branch(res.stdout().get(0));
return Optional.of(new Branch(res.stdout().get(0)));
}
}

@@ -156,7 +156,7 @@ void testConfig(VCS vcs) throws IOException {
void testCurrentBranchOnEmptyRepository(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), vcs);
assertEquals(r.defaultBranch(), r.currentBranch());
assertEquals(r.defaultBranch(), r.currentBranch().get());
}
}