Gitpro.Git (gitpro v0.1.0)

Copy Markdown View Source

Which GitHub repository the working directory belongs to.

gitpro takes no repository argument in the usual case: you are standing in a checkout, and that is the answer. So the directory is handed to git and the origin remote it reports is parsed into an owner and a name.

Remotes come in more shapes than one:

git@github.com:owner/repo.git
ssh://git@github.com/owner/repo.git
https://github.com/owner/repo.git
https://user@github.com/owner/repo

parse_remote/1 takes all of them, and is a pure function — the part worth testing is the parsing, not the shelling out.

Summary

Functions

Turns a remote URL into {:ok, %{owner:, name:}}.

The URL of the origin remote of the repository at dir.

The repository the directory dir is a checkout of.

Types

repo()

@type repo() :: %{owner: String.t(), name: String.t()}

Functions

parse_remote(url)

@spec parse_remote(String.t()) :: {:ok, repo()} | {:error, String.t()}

Turns a remote URL into {:ok, %{owner:, name:}}.

The .git suffix is optional, the scheme may be missing, and any user information in front of the host is ignored — what matters is the last two path segments of a github.com URL.

remote_url(dir)

@spec remote_url(Path.t()) :: {:ok, String.t()} | {:error, String.t()}

The URL of the origin remote of the repository at dir.

repo(dir)

@spec repo(Path.t()) :: {:ok, repo()} | {:error, String.t()}

The repository the directory dir is a checkout of.

Answers {:error, message} rather than raising: a message that can be printed before the UI starts is more use than a stack trace, and every way this fails — not a repository, no remote, a remote that is not GitHub — is something the person at the keyboard can act on.