atom/open-on-github — pull request #21 by izuzak (2014-03-14 15:30:10+00:00)
Title: Don't use regexes to determine the boundary between remote and branch name

Fixes https://github.com/atom/open-on-github/issues/18

The package constructs URLs which are opened in a browser by parsing the remote name and upstream branch name from the full upstream ref (e.g. `"refs/remotes/origin/master"`):

https://github.com/atom/open-on-github/blob/master/lib/github-file.coffee#L109-L117I

Currently, a regex is used to parse this information:

```
/^refs\/remotes\/(.+)\/.*$/
```

This regex assumes that both the remote and the branch name don't have any slashes in them. However, if a branch name has a slash (e.g. `foo/my-branch`), then the remote part will be parsed as `origin/foo` because `.+` in the regex is greedy.

A bad solution to this problem would be to change the regex to match the remote part in a non-greedy way:

```
/^refs\/remotes\/(.+?)\/.*$/
```

That wouldn't solve the problem but would just push it in another direction. Since Git allows that both branch names and remote names can have slashes, using regexes to determine where the boundary is between the two is not possible in the general case. In other words, for a full ref name `refs/remotes/origin/foo/bar`, it's impossible to determine if the remote is `origin` or `origin/foo` by just looking at that string.

This PR removes regexes as a way to determine remote and branch names, and instead uses the information stored in the .git/config file. The approach is the same as the one used in the implementation of the [`getUpstreamBranch()`](https://github.com/atom/git-utils/blob/d72c9a2c430aa99237942ffef7679c0ea747ab5d/src/git.coffee#L54-L68) method, which constructs the full ref name from these two parts.

cc @jasonrudolph @kevinsawicki