file

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package file writes files atomically, so readers see either the old contents or the new, never a partial write, and finds git projects on disk.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotInGitProject = errors.New("not inside a git project")

ErrNotInGitProject is returned by EnclosingGitProject when no directory above the path is a git project.

Functions

func EnclosingGitProject added in v0.0.3

func EnclosingGitProject(path string) (string, error)

EnclosingGitProject returns the root of the git project containing path: the nearest of path and its ancestors with a .git entry, as an absolute path. Within a worktree or submodule, that's the worktree or submodule. path needn't exist.

func SkipHidden added in v0.0.3

func SkipHidden(_ string, d fs.DirEntry) bool

SkipHidden is a GitWalkOptions.Skip that leaves out hidden directories.

func WalkGitProjects added in v0.0.3

func WalkGitProjects(root string, opts GitWalkOptions) ([]string, error)

WalkGitProjects returns the paths of the git projects at or under root, in lexical order. A project is any directory with a .git entry, which is a file rather than a directory for worktrees and submodules. Projects aren't looked inside, so nested projects and submodules aren't returned, and symlinks below root aren't followed.

An error is returned only if root isn't a readable directory; directories below it that can't be read are left out.

func Write

func Write(path string, data []byte, perm os.FileMode) error

Write atomically replaces the file at path with data, leaving it with exactly perm whatever the umask and whatever mode an existing file had. It behaves like WriteFunc.

func WriteFunc

func WriteFunc(path string, perm os.FileMode, write func(io.Writer) error) error

WriteFunc atomically replaces the file at path with whatever write writes, leaving it with exactly perm whatever the umask and whatever mode an existing file had.

The content goes to a temporary file in the same directory, which is synced and renamed over path, and then the directory is synced so the rename survives a crash (except on Windows, where directories can't be synced). If write or any step before the rename fails, the temporary file is removed and path is left untouched. If only the directory sync fails, path has already been replaced and the error is still returned.

Types

type GitWalkOptions added in v0.0.3

type GitWalkOptions struct {
	// MaxDepth is how many directories below the root a project can be, so 1
	// finds only the root's immediate children. Zero means no limit.
	MaxDepth int
	// Skip, if set, is asked about each directory below the root before it's
	// checked for a project, and returning true leaves it and everything
	// under it out.
	Skip func(path string, d fs.DirEntry) bool
}

GitWalkOptions limits how far WalkGitProjects looks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL