Categorygithub.com/fardream/gitrim
modulepackage
0.2.0
Repository: https://github.com/fardream/gitrim.git
Documentation: pkg.go.dev

# README

gitrim

Git Trim is a deterministic tool to manipulate trees contained in git commits.

Go Reference

Trim/Filter Git History

Often, the history and files contained in a git repository need to be filtered in some way. One simple case will be a contributor is only allowed to access part of the repo. This can be done through git-filter-branch, although no so user-friendly.

gitrim does just that:

  1. read git commit history.
  2. from start, filter the tree contained in the commit and copy over author, committor, commit message. The parents are replaced with the newly created commits, and GPG signatures are omitted.

As long as the filters don't change, the generated git history is deterministic and can be one-to-one mapped back to the original repo.

Modifications made in the trimmed/filtered repo can be recreated by

  1. filter the changes
  2. apply them back to the original repo, copying over author, committor, commit message, and add the original commits as parents. GPG signatures are again omitted.

The commits in the filtered/trimmed repo will match the commit reproduced from original repo if they are without GPG signatures.

Filters

The filter all implements the Filter interface

The pattern used is a more restricted version of the pattern used by .gitignore.

  • ** is for multi level directories, and it can only appear once in the match.
  • * is for match one level of names.
  • ! and escapes are unsupported.
  • paths are always relative to the root. For example, LICENSE will only match LICENSE in the root of the repo. To match LICENSE at all directory levels, use **/LICENSE.

Refer to documentation on PatternFilter

Example

See Example

CLI

  • filter-git-hist filters the history of a git repo and output it to another git repo.
  • [expand-git-commit](cmd/expand-git-commit] expands the new commit back to the original repo.
  • dump-git-tree prints the files of a branch/tree/commit/head. Optionally filters can be applied.
  • remve-git-gpg removes gpg signatures for commits.

# Packages

cmd package contains helper functions for various commands.

# Functions

CopyTree copies the given tree into the [storer.Storer].
DumpTree writes the file entries in this tree and its sub trees to an [io.Writer].
ExpandCommit added the changes contained in the filteredNew to filteredOrig and try to apply them to target, it will generate a new commit.
ExpandTree apply the changes made in the filteredNew tree to filteredOrig tree and apply them to target tree, it returns a new tree.
FilterCommit creates a new [object.Commit] in the given [storer.Storer] by applying filters to the tree in the input [object.Commit].
FilterDFSPath filters a slice of [object.Commit] that comes from a Depth First Search from a commit - this means the earlier commits should come first in the input slice dfspath, and the head/latest commit should come the last.
FilterLinearHistory performs filters on a sequence of commits of a linear history and produces new commits in the provided [storer.Store].
FilterPath calls [Filter] f on fullpath string.
FilterResultsAnd perform and operation on the filter results: - if out and int, out - if out and dir_dive, out - if dir_dive and in, dir_dive.
FilterResultsOr perform or operation on filter results: - If out and in, in - If out and dir_dive, dir_dive - If dir_dive and in, in This is equivalent to take the max value of the input.
FilterTree filters the entries of the tree by the filter and stores it in the given [storer.Storer].
GetDFSPath gets a deterministic depth first search path from a head commit, the returned slice has the head commit as the last one in the slice, and one of the root commits as the first of the slice.
GetHash returns the hash of the.
GetLinearHistory produces the linear history from a given head commit.
LoadPatternFilterFromString loads the string content of a pattern file like .gitignore.
LoadPatternStringFromString loads from the string content of a pattern file like .gitignore.
NewAndFilter creates a new filter with and operations.
No description provided by the author
No description provided by the author
NewOrFilterForPatterns creates a new Or filter for all the patterns.
No description provided by the author
No description provided by the author
PatternDirFilter filters the directory according to a directory filter.
RemoveGPGForDFSPath removes gpg signatures from a depth first search graph and save the nwe commits into s.
RemoveGPGForLinearHistory removes gpg signature from the commits and save the new commits into s.
SetLogger sets the logger used by gitrim.

# Constants

# Structs

AndFilter combines multiple [Filter] into one [Filter] with an "and" operation, the path will only be included when all the filters include it.
CachedFilter records the paths it sees - the cache is no concurrent safe.
FilePatchError is an error containing the information about the invalid file patch.
OrFilter combines multiple [Filter] into one [Filter] with an "or" operation, the path will be inclueded if any one of the filters includes it.
PatternFilter filters the entries according to a restricted pattern of [gitignore] - `**` is for multi level directories, and it can only appear once in the match.
TrueFilter always return [FilterResult_In] for any input.

# Interfaces

Filter is the interface used to filter the path of the tree.

# Type aliases

FilterResult indicates the result of a filter, it can be - the input is out - the input is directory, and its entries should be filtered - the input is in the logic or operation for filter result: - If out and in, in - If out and dir_dive, dir_dive - If dir_dive and in, in the logic and operation for filter result: - if out and int, out - if out and dir_dive, out - if dir_dive and in, dir_dive Notice that the enum values has [FilterResult_In] at 2, [FilterResult_DirDive] at 1, and [FilterResult_Out] at 0, therefore the or operation is finding the max, and and operation is finding the min.
PatternFilterSegment is a segment in [PatternFilter].