dream_snippets/git

Runs git for the diff blocks and the blocks of code at an older release: tells whether a release tag is in the local clone, reads the git diff of one file from a tag to the working tree or to another tag, and reads a file as it was at a tag.

git starts directly, with no shell between, and its options fix the output format, so the settings of a user cannot change what the check sees. Only dream_snippets calls these functions for a real project; the checks in dream_snippets/check get them through a CheckContext, so tests can give fake ones.

Types

The git program to run, and the directory to run it in.

program is the name of the program, which is looked up on the PATH (normally "git"). directory is the repository root, for example ".".

pub type Git {
  Git(program: String, directory: String)
}

Constructors

  • Git(program: String, directory: String)

Why git could not answer.

pub type GitError {
  GitNotFound
  GitFailed(status: Int, output: String)
}

Constructors

  • GitNotFound

    The program is not on the PATH.

  • GitFailed(status: Int, output: String)

    git stopped with an exit status that is not 0. output is what it wrote to standard output.

Values

pub fn describe_error(error: GitError) -> String

Describes why git could not answer, in a few words, for example git stopped with exit status 128.

Examples

Describe why git failed Tested snippet

import dream_snippets/git.{GitFailed}

pub fn git_failure_message() -> String {
  // Returns "git stopped with exit status 128"
  git.describe_error(GitFailed(status: 128, output: ""))
}
pub fn diff_between_tags(
  git: Git,
  old_tag: String,
  new_tag: String,
  path: String,
) -> Result(String, GitError)

Returns the text of git diff for one path, from an old tag to a new tag. It is the diff of a diff block in the release notes or changelog section of an older release, whose new side is the tag of that release.

It uses the same fixed options as diff_from_tag.

Parameters

  • git: the program and the repository directory
  • old_tag and new_tag: the two tags. Each must pass check.is_safe_tag.
  • path: the file, relative to the repository root. It must pass check.is_safe_path.

Returns

The diff text, or an empty string when the file is the same at both tags.

Examples

Read the diff of a file between two tags Tested snippet

import dream_snippets/git.{type GitError, Git}

pub fn changes_in_second_release(
  directory: String,
) -> Result(String, GitError) {
  let repository = Git(program: "git", directory:)
  // Returns Ok("") when src/app.gleam is the same at the tags 1.0.0 and 1.1.0
  git.diff_between_tags(repository, "1.0.0", "1.1.0", "src/app.gleam")
}

Errors

GitNotFound or GitFailed, as for tag_exists.

pub fn diff_from_tag(
  git: Git,
  tag: String,
  path: String,
) -> Result(String, GitError)

Returns the text of git diff for one path, from a tag to the working tree.

The options fix the output format, so that the git settings of a user (colours, prefixes, external diff tools, the diff algorithm, the number of context lines) cannot change it. The path comes after --, so git never reads it as an option.

Parameters

  • git: the program and the repository directory
  • tag: the old tag. It must pass check.is_safe_tag.
  • path: the file, relative to the repository root. It must pass check.is_safe_path.

Returns

The diff text, or an empty string when the file did not change.

Examples

Read the diff of a file from a tag Tested snippet

import dream_snippets/git.{type GitError, Git}

pub fn changes_since_first_release(
  directory: String,
) -> Result(String, GitError) {
  // Returns Ok("") when src/app.gleam did not change after the tag 1.0.0
  git.diff_from_tag(Git(program: "git", directory:), "1.0.0", "src/app.gleam")
}

Errors

GitNotFound or GitFailed, as for tag_exists.

pub fn file_at_tag(
  git: Git,
  tag: String,
  path: String,
) -> Result(option.Option(String), GitError)

Returns the text of a file as it was at a tag.

It runs git ls-tree --full-tree refs/tags/<tag> -- <path> to find out whether the path is a file at the tag. When it is, it runs git show --no-textconv refs/tags/<tag>:<path> and returns the output. The path comes after -- or after the :, so git never reads it as an option.

Parameters

  • git: the program and the repository directory
  • tag: the tag. It must pass check.is_safe_tag.
  • path: the file, relative to the repository root. It must pass check.is_safe_path.

Returns

Ok(Some(text)) when the path is a file at the tag, and Ok(None) when the tag has no file at that path (no entry, or a directory).

Examples

Read a file at a tag Tested snippet

import dream_snippets/git.{type GitError, Git}
import gleam/option.{type Option}

pub fn app_at_first_release(
  directory: String,
) -> Result(Option(String), GitError) {
  // Returns Ok(Some(text)) when src/app.gleam is a file at the tag 1.0.0,
  // and Ok(None) when it is not
  git.file_at_tag(Git(program: "git", directory:), "1.0.0", "src/app.gleam")
}

Errors

GitNotFound or GitFailed, as for tag_exists. A tag that is not in the clone is a GitFailed, so check it with tag_exists first.

pub fn run(
  git: Git,
  arguments: List(String),
) -> Result(String, GitError)

Runs git with the arguments in the directory of git, with no shell between, and returns its standard output. Standard error is not captured: git prints it to the terminal.

Examples

Run git Tested snippet

import dream_snippets/git.{type GitError, Git}

pub fn list_tags(directory: String) -> Result(String, GitError) {
  // Returns Ok("1.0.0\n") in a repository with the one tag 1.0.0
  git.run(Git(program: "git", directory:), ["tag", "--list"])
}

Errors

GitNotFound when the program is not on the PATH, and GitFailed(status, output) when it exits with a status that is not 0.

pub fn tag_exists(
  git: Git,
  tag: String,
) -> Result(Bool, GitError)

Tells whether a tag is in the local clone.

It runs git rev-parse --verify --quiet refs/tags/<tag>^{commit}. git exits with status 1 and no output when the tag is missing.

Parameters

  • git: the program and the repository directory
  • tag: the tag name. It must pass check.is_safe_tag.

Returns

Ok(True) when the tag names a commit, Ok(False) when it does not.

Examples

Find a release tag Tested snippet

import dream_snippets/git.{type GitError, Git}

pub fn has_first_release(directory: String) -> Result(Bool, GitError) {
  // Returns Ok(True) in a repository with the tag 1.0.0
  git.tag_exists(Git(program: "git", directory:), "1.0.0")
}

Errors

GitNotFound when the program is not on the PATH, and GitFailed for any other failure of git, for example a directory that is not a git repository.

✨ Search Document