tools

package
v2.5.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2018 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package tools contains other helper functions too small to justify their own package NOTE: Subject to change, do not rely on this package from outside git-lfs source

Set is a modification of https://github.com/deckarep/golang-set The MIT License (MIT) Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com)

Index

Constants

View Source
const (
	BtrfsIocClone = C.BTRFS_IOC_CLONE
)

Variables

This section is empty.

Functions

func ClampInt

func ClampInt(n, min, max int) int

ClampInt returns the integer "n" bounded between "min" and "max".

func CleanPaths

func CleanPaths(paths, delim string) (cleaned []string)

CleanPaths splits the given `paths` argument by the delimiter argument, and then "cleans" that path according to the path.Clean function (see https://golang.org/pkg/path#Clean). Note always cleans to '/' path separators regardless of platform (git friendly)

func CloneFile

func CloneFile(writer io.Writer, reader io.Reader) (bool, error)

func CopyWithCallback

func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb CopyCallback) (int64, error)

CopyWithCallback copies reader to writer while performing a progress callback

func DirExists

func DirExists(path string) bool

DirExists determines if a dir (NOT file) exists.

func FastWalkGitRepo

func FastWalkGitRepo(rootDir string, cb FastWalkCallback)

FastWalkGitRepo is a more optimal implementation of filepath.Walk for a Git repo. The callback guaranteed to be called sequentially. The function returns once all files and errors have triggered callbacks. It differs in the following ways:

  • Uses goroutines to parallelise large dirs and descent into subdirs
  • Does not provide sorted output; parents will always be before children but there are no other guarantees. Use parentDir argument in the callback to determine absolute path rather than tracking it yourself
  • Automatically ignores any .git directories
  • Respects .gitignore contents and skips ignored files/dirs

rootDir - Absolute path to the top of the repository working directory

func FileExists

func FileExists(path string) bool

FileExists determines if a file (NOT dir) exists.

func FileExistsOfSize

func FileExistsOfSize(path string, sz int64) bool

FileExistsOfSize determines if a file exists and is of a specific size.

func FileOrDirExists

func FileOrDirExists(path string) (exists bool, isDir bool)

FileOrDirExists determines if a file/dir exists, returns IsDir() results too.

func Getwd

func Getwd() (dir string, err error)

func Indent

func Indent(str string) string

Indent returns a string which prepends "\t" TAB characters to the beginning of each line in the given string "str".

func IsExpiredAtOrIn

func IsExpiredAtOrIn(from time.Time, until time.Duration, at time.Time, in time.Duration) (time.Time, bool)

IsExpiredAtOrIn returns whether or not the result of calling TimeAtOrIn is "expired" within "until" units of time from now.

func Ljust

func Ljust(strs []string) []string

Ljust returns a copied string slice where each element is left justified to match the width of the longest element in the set.

func Longest

func Longest(strs []string) string

Longest returns the longest element in the string slice in O(n) time and O(1) space. If strs is empty or nil, an empty string will be returned.

func MaxInt

func MaxInt(a, b int) int

MaxInt returns the greater of two `int`s, "a", or "b".

func MaxInt64

func MaxInt64(a, b int64) int64

MaxInt64 returns the greater of two `int`s, "a", or "b".

func MinInt

func MinInt(a, b int) int

MinInt returns the smaller of two `int`s, "a", or "b".

func MinInt64

func MinInt64(a, b int64) int64

MinInt64 returns the smaller of two `int`s, "a", or "b".

func NewLfsContentHash

func NewLfsContentHash() hash.Hash

Get a new Hash instance of the type used to hash LFS content

func NewRetriableReader

func NewRetriableReader(r io.Reader) io.Reader

func QuotedFields

func QuotedFields(s string) []string

QuotedFields is an alternative to strings.Fields (see: https://golang.org/pkg/strings#Fields) that respects spaces between matching pairs of quotation delimeters.

For instance, the quoted fields of the string "foo bar 'baz etc'" would be:

[]string{"foo", "bar", "baz etc"}

Whereas the same argument given to strings.Fields, would return:

[]string{"foo", "bar", "'baz", "etc'"}

func RenameFileCopyPermissions

func RenameFileCopyPermissions(srcfile, destfile string) error

RenameFileCopyPermissions moves srcfile to destfile, replacing destfile if necessary and also copying the permissions of destfile if it already exists

func ResolveSymlinks(path string) string

ResolveSymlinks ensures that if the path supplied is a symlink, it is resolved to the actual concrete path

func Rjust

func Rjust(strs []string) []string

Rjust returns a copied string slice where each element is right justified to match the width of the longest element in the set.

func SetFileWriteFlag

func SetFileWriteFlag(path string, writeEnabled bool) error

SetFileWriteFlag changes write permissions on a file Used to make a file read-only or not. When writeEnabled = false, the write bit is removed for all roles. When writeEnabled = true, the behaviour is different per platform: On Mac & Linux, the write bit is set only on the owner as per default umask. All other bits are unaffected. On Windows, all the write bits are set since Windows doesn't support Unix permissions.

func Spool

func Spool(to io.Writer, from io.Reader, dir string) (n int64, err error)

Spool spools the contents from 'from' to 'to' by buffering the entire contents of 'from' into a temprorary file created in the directory "dir". That buffer is held in memory until the file grows to larger than 'memoryBufferLimit`, then the remaining contents are spooled to disk.

The temporary file is cleaned up after the copy is complete.

The number of bytes written to "to", as well as any error encountered are returned.

func TimeAtOrIn

func TimeAtOrIn(from, at time.Time, in time.Duration) time.Time

TimeAtOrIn returns either "at", or the "in" duration added to the current time. TimeAtOrIn prefers to add a duration rather than return the "at" parameter.

func TranslateCygwinPath

func TranslateCygwinPath(path string) (string, error)

func Undent

func Undent(str string) string

Undent removes all leading tabs in the given string "str", line-wise.

func VerifyFileHash

func VerifyFileHash(oid, path string) error

VerifyFileHash reads a file and verifies whether the SHA is correct Returns an error if there is a problem

Types

type BaseChannelWrapper

type BaseChannelWrapper struct {
	// contains filtered or unexported fields
}

Base implementation of channel wrapper to just deal with errors

func NewBaseChannelWrapper

func NewBaseChannelWrapper(errChan <-chan error) *BaseChannelWrapper

func (*BaseChannelWrapper) Wait

func (w *BaseChannelWrapper) Wait() error

type BodyWithCallback

type BodyWithCallback struct {
	ReadSeekCloser
	// contains filtered or unexported fields
}

func NewBodyWithCallback

func NewBodyWithCallback(body ReadSeekCloser, totalSize int64, cb CopyCallback) *BodyWithCallback

func NewByteBodyWithCallback

func NewByteBodyWithCallback(by []byte, totalSize int64, cb CopyCallback) *BodyWithCallback

func (*BodyWithCallback) Read

func (r *BodyWithCallback) Read(p []byte) (int, error)

Read wraps the underlying Reader's "Read" method. It also captures the number of bytes read, and calls the callback.

func (*BodyWithCallback) ResetProgress

func (r *BodyWithCallback) ResetProgress() error

ResetProgress calls the callback with a negative read size equal to the total number of bytes read so far, effectively "resetting" the progress.

func (*BodyWithCallback) Seek

func (r *BodyWithCallback) Seek(offset int64, whence int) (int64, error)

Seek wraps the underlying Seeker's "Seek" method, updating the number of bytes that have been consumed by this reader.

type CallbackReader

type CallbackReader struct {
	C         CopyCallback
	TotalSize int64
	ReadSize  int64
	io.Reader
}

func (*CallbackReader) Read

func (w *CallbackReader) Read(p []byte) (int, error)

type ChannelWrapper

type ChannelWrapper interface {
	// Call this after processing results channel to check for async errors
	Wait() error
}

Interface for all types of wrapper around a channel of results and an error channel Implementors will expose a type-specific channel for results Call the Wait() function after processing the results channel to catch any errors that occurred during the async processing

type CopyCallback

type CopyCallback func(totalSize int64, readSoFar int64, readSinceLast int) error

type FastWalkCallback

type FastWalkCallback func(parentDir string, info os.FileInfo, err error)

FastWalkCallback is the signature for the callback given to FastWalkGitRepo()

type HashingReader

type HashingReader struct {
	// contains filtered or unexported fields
}

HashingReader wraps a reader and calculates the hash of the data as it is read

func NewHashingReader

func NewHashingReader(r io.Reader) *HashingReader

func NewHashingReaderPreloadHash

func NewHashingReaderPreloadHash(r io.Reader, hash hash.Hash) *HashingReader

func (*HashingReader) Hash

func (r *HashingReader) Hash() string

func (*HashingReader) Read

func (r *HashingReader) Read(b []byte) (int, error)

type OrderedSet

type OrderedSet struct {
	// contains filtered or unexported fields
}

OrderedSet is a unique set of strings that maintains insertion order.

func NewOrderedSet

func NewOrderedSet() *OrderedSet

NewOrderedSet creates an ordered set with no values.

func NewOrderedSetFromSlice

func NewOrderedSetFromSlice(s []string) *OrderedSet

NewOrderedSetFromSlice returns a new ordered set with the elements given in the slice "s".

func NewOrderedSetWithCapacity

func NewOrderedSetWithCapacity(capacity int) *OrderedSet

NewOrderedSetWithCapacity creates a new ordered set with no values. The returned ordered set can be appended to "capacity" number of times before it grows internally.

func (*OrderedSet) Add

func (s *OrderedSet) Add(i string) bool

Add adds the given element "i" to the ordered set, unless the element is already present. It returns whether or not the element was added.

func (*OrderedSet) Cardinality

func (s *OrderedSet) Cardinality() int

Cardinality returns the cardinality of this set.

func (*OrderedSet) Clear

func (s *OrderedSet) Clear()

Clear removes all elements from this set.

func (*OrderedSet) Clone

func (s *OrderedSet) Clone() *OrderedSet

Clone returns a deep copy of this set.

func (*OrderedSet) Contains

func (s *OrderedSet) Contains(i string) bool

Contains returns whether or not the given "i" is contained in this ordered set. It is a constant-time operation.

func (*OrderedSet) ContainsAll

func (s *OrderedSet) ContainsAll(i ...string) bool

ContainsAll returns whether or not all of the given items in "i" are present in the ordered set.

func (*OrderedSet) Difference

func (s *OrderedSet) Difference(other *OrderedSet) *OrderedSet

Difference returns the elements that are in this set, but not included in other.

func (*OrderedSet) Equal

func (s *OrderedSet) Equal(other *OrderedSet) bool

Equal returns whether this element has the same number, identity and ordering elements as given in "other".

func (*OrderedSet) Intersect

func (s *OrderedSet) Intersect(other *OrderedSet) *OrderedSet

Intersect returns the elements that are in both this set and then given "ordered" set. It is an O(min(n, m)) (in other words, O(n)) operation.

func (*OrderedSet) IsSubset

func (s *OrderedSet) IsSubset(other *OrderedSet) bool

IsSubset returns whether other is a subset of this ordered set. In other words, it returns whether or not all of the elements in "other" are also present in this set.

func (*OrderedSet) IsSuperset

func (s *OrderedSet) IsSuperset(other *OrderedSet) bool

IsSuperset returns whether or not this set is a superset of "other". In other words, it returns whether or not all of the elements in this set are also in the set "other".

func (*OrderedSet) Iter

func (s *OrderedSet) Iter() <-chan string

Iter returns a channel which yields the elements in this set in insertion order.

func (*OrderedSet) Remove

func (s *OrderedSet) Remove(i string)

Remove removes the given element "i" from this set.

func (*OrderedSet) SymmetricDifference

func (s *OrderedSet) SymmetricDifference(other *OrderedSet) *OrderedSet

SymmetricDifference returns the elements that are not present in both sets.

func (*OrderedSet) Union

func (s *OrderedSet) Union(other *OrderedSet) *OrderedSet

Union returns a union of this set with the given set "other". It returns the items that are in either set while maintaining uniqueness constraints. It preserves ordered within each set, and orders the elements in this set before the elements in "other".

It is an O(n+m) operation.

type ReadSeekCloser

type ReadSeekCloser interface {
	io.Seeker
	io.ReadCloser
}

prevent import cycle

func NewByteBody

func NewByteBody(by []byte) ReadSeekCloser

type RetriableReader

type RetriableReader struct {
	// contains filtered or unexported fields
}

RetriableReader wraps a error response of reader as RetriableError()

func (*RetriableReader) Read

func (r *RetriableReader) Read(b []byte) (int, error)

type StringSet

type StringSet map[string]struct{}

The primary type that represents a set

func NewStringSet

func NewStringSet() StringSet

Creates and returns a reference to an empty set.

func NewStringSetFromSlice

func NewStringSetFromSlice(s []string) StringSet

Creates and returns a reference to a set from an existing slice

func NewStringSetWithCapacity

func NewStringSetWithCapacity(capacity int) StringSet

Creates and returns a reference to an empty set with a capacity.

func (StringSet) Add

func (set StringSet) Add(i string) bool

Adds an item to the current set if it doesn't already exist in the set.

func (StringSet) Cardinality

func (set StringSet) Cardinality() int

Cardinality returns how many items are currently in the set.

func (*StringSet) Clear

func (set *StringSet) Clear()

Clears the entire set to be the empty set.

func (StringSet) Clone

func (set StringSet) Clone() StringSet

Returns a clone of the set. Does NOT clone the underlying elements.

func (StringSet) Contains

func (set StringSet) Contains(i string) bool

Determines if a given item is already in the set.

func (StringSet) ContainsAll

func (set StringSet) ContainsAll(i ...string) bool

Determines if the given items are all in the set

func (StringSet) Difference

func (set StringSet) Difference(other StringSet) StringSet

Returns a new set with items in the current set but not in the other set

func (StringSet) Equal

func (set StringSet) Equal(other StringSet) bool

Equal determines if two sets are equal to each other. If they both are the same size and have the same items they are considered equal. Order of items is not relevent for sets to be equal.

func (StringSet) Intersect

func (set StringSet) Intersect(other StringSet) StringSet

Returns a new set with items that exist only in both sets.

func (StringSet) IsSubset

func (set StringSet) IsSubset(other StringSet) bool

Determines if every item in the other set is in this set.

func (StringSet) IsSuperset

func (set StringSet) IsSuperset(other StringSet) bool

Determines if every item of this set is in the other set.

func (StringSet) Iter

func (set StringSet) Iter() <-chan string

Iter() returns a channel of type string that you can range over.

func (StringSet) Remove

func (set StringSet) Remove(i string)

Allows the removal of a single item in the set.

func (StringSet) SymmetricDifference

func (set StringSet) SymmetricDifference(other StringSet) StringSet

Returns a new set with items in the current set or the other set but not in both.

func (StringSet) Union

func (set StringSet) Union(other StringSet) StringSet

Returns a new set with all items in both sets.

type SyncWriter

type SyncWriter struct {
	// contains filtered or unexported fields
}

SyncWriter provides a wrapper around an io.Writer that synchronizes all write after they occur, if the underlying writer supports synchronization.

func NewSyncWriter

func NewSyncWriter(w io.Writer) *SyncWriter

NewSyncWriter returns a new instance of the *SyncWriter that sends all writes to the given io.Writer.

func (*SyncWriter) Close

func (w *SyncWriter) Close() error

Close will call Close() on the underlying file

func (*SyncWriter) Write

func (w *SyncWriter) Write(b []byte) error

Write will write to the file and perform a Sync() if writing succeeds.

Directories

Path Synopsis
package humanize is designed to parse and format "humanized" versions of numbers with units.
package humanize is designed to parse and format "humanized" versions of numbers with units.

Jump to

Keyboard shortcuts

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