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 file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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");
This conversation was marked as resolved by edvbld

This comment has been minimized.

@rwestberg

rwestberg Nov 29, 2019 Member

Suggested change
System.err.println("error: the repository is in a detached HEAD state");
System.err.println("error: the repository is in a detached HEAD state. check out a branch to continue.. or something");

This comment has been minimized.

@edvbld

edvbld Nov 29, 2019 Author Member

I thought about this as well, but if you see the "detached HEAD" warning when doing e.g. git checkout HEAD~ there is just no way we are gonna be able to write all that 😃

So I figured that the next best thing was to write something concise that is very easy to search for. If the user does not know what "detached HEAD state" means then he/she will immediately search for it and will most likely stumble upon this explanation which is the first hit on both DuckDuckGo and Google for me.

I think that is the best alternative here, what do you say?

This comment has been minimized.

@rwestberg

rwestberg Nov 29, 2019 Member

Sounds good!

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 @@
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 void rebase(Hash hash, String committerName, String committerEmail) throw
}

@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")
This conversation was marked as resolved by edvbld

This comment has been minimized.

@rwestberg

rwestberg Nov 29, 2019 Member

Should this mention the detached head then as well perhaps?

This comment has been minimized.

@rwestberg

rwestberg Nov 29, 2019 Member

Scratch that, guess this is hg :)

This comment has been minimized.

@edvbld

edvbld Dec 2, 2019 Author Member

Yes, this is in HgRepository, so a "detached HEAD" is not possible. I actually don't think currentBranch can fail for HgRepository, since the current branch is part of the commit metadata in Mercurial (not just a ref as in git). I added a message anyhow to distinguish this scenario.

);
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());
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.