1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
package lfs
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/git-lfs/git-lfs/v3/config"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/tasklog"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tr"
)
type Platform int
const (
PlatformWindows = Platform(iota)
PlatformLinux = Platform(iota)
PlatformOSX = Platform(iota)
PlatformOther = Platform(iota) // most likely a *nix variant e.g. freebsd
PlatformUndetermined = Platform(iota)
)
var currentPlatform = PlatformUndetermined
func join(parts ...string) string {
return strings.Join(parts, "/")
}
func (f *GitFilter) CopyCallbackFile(event, filename string, index, totalFiles int) (tools.CopyCallback, *os.File, error) {
logPath, _ := f.cfg.Os.Get("GIT_LFS_PROGRESS")
if len(logPath) == 0 || len(filename) == 0 || len(event) == 0 {
return nil, nil, nil
}
if !filepath.IsAbs(logPath) {
return nil, nil, errors.New(tr.Tr.Get("GIT_LFS_PROGRESS must be an absolute path"))
}
cbDir := filepath.Dir(logPath)
if err := tools.MkdirAll(cbDir, f.cfg); err != nil {
return nil, nil, wrapProgressError(err, event, logPath)
}
file, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, file, wrapProgressError(err, event, logPath)
}
var prevWritten int64
deadline := f.clk.Now().Add(tasklog.DefaultLoggingThrottle)
cb := tools.CopyCallback(func(total int64, written int64, current int) error {
now := f.clk.Now()
if written != prevWritten && (!now.Before(deadline) || written >= total) {
_, err := fmt.Fprintf(file, "%s %d/%d %d/%d %s\n", event, index, totalFiles, written, total, filename)
file.Sync()
prevWritten = written
deadline = now.Add(tasklog.DefaultLoggingThrottle)
return wrapProgressError(err, event, logPath)
}
return nil
})
return cb, file, nil
}
func wrapProgressError(err error, event, filename string) error {
if err != nil {
return errors.New(tr.Tr.Get("error writing Git LFS %s progress to %s: %s", event, filename, err.Error()))
}
return nil
}
var localDirSet = tools.NewStringSetFromSlice([]string{".", "./", ".\\"})
func GetPlatform() Platform {
if currentPlatform == PlatformUndetermined {
switch runtime.GOOS {
case "windows":
currentPlatform = PlatformWindows
case "linux":
currentPlatform = PlatformLinux
case "darwin":
currentPlatform = PlatformOSX
default:
currentPlatform = PlatformOther
}
}
return currentPlatform
}
type PathConverter interface {
Convert(string) string
}
// Convert filenames expressed relative to the root of the repo relative to the
// current working dir. Useful when needing to calling git with results from a rooted command,
// but the user is in a subdir of their repo
func NewRepoToCurrentPathConverter(cfg *config.Configuration) (PathConverter, error) {
r, c, p, err := pathConverterArgs(cfg)
if err != nil {
return nil, err
}
return &repoToCurrentPathConverter{
repoDir: r,
currDir: c,
passthrough: p,
}, nil
}
type repoToCurrentPathConverter struct {
repoDir string
currDir string
passthrough bool
}
func (p *repoToCurrentPathConverter) Convert(filename string) string {
if p.passthrough {
return filename
}
abs := join(p.repoDir, filename)
rel, err := filepath.Rel(p.currDir, abs)
if err != nil {
// Use absolute file instead
return abs
}
return filepath.ToSlash(rel)
}
// Convert filenames expressed relative to the current directory to be
// relative to the repo root. Useful when calling git with arguments that requires them
// to be rooted but the user is in a subdir of their repo & expects to use relative args
func NewCurrentToRepoPathConverter(cfg *config.Configuration) (PathConverter, error) {
r, c, p, err := pathConverterArgs(cfg)
if err != nil {
return nil, err
}
return ¤tToRepoPathConverter{
repoDir: r,
currDir: c,
passthrough: p,
}, nil
}
type currentToRepoPathConverter struct {
repoDir string
currDir string
passthrough bool
}
func (p *currentToRepoPathConverter) Convert(filename string) string {
if p.passthrough {
return filename
}
var abs string
if filepath.IsAbs(filename) {
abs = tools.ResolveSymlinks(filename)
} else {
abs = join(p.currDir, filename)
}
reltoroot, err := filepath.Rel(p.repoDir, abs)
if err != nil {
// Can't do this, use absolute as best fallback
return abs
}
return filepath.ToSlash(reltoroot)
}
// Convert filenames expressed relative to the current directory to be relative
// to the repo root and convert them into wildmatch patterns.
func NewCurrentToRepoPatternConverter(cfg *config.Configuration) (PathConverter, error) {
r, c, p, err := pathConverterArgs(cfg)
if err != nil {
return nil, err
}
return ¤tToRepoPatternConverter{
c: ¤tToRepoPathConverter{
repoDir: r,
currDir: c,
passthrough: p,
},
}, nil
}
type currentToRepoPatternConverter struct {
c *currentToRepoPathConverter
}
func (p *currentToRepoPatternConverter) Convert(filename string) string {
pattern := p.c.Convert(filename)
if st, err := os.Stat(filename); err == nil && st.IsDir() {
pattern += "/"
}
if strings.HasPrefix(pattern, "./") {
pattern = pattern[2:]
if len(pattern) == 0 {
pattern = "**"
}
}
return pattern
}
func pathConverterArgs(cfg *config.Configuration) (string, string, bool, error) {
currDir, err := os.Getwd()
if err != nil {
return "", "", false, errors.New(tr.Tr.Get("unable to get working dir: %v", err))
}
currDir = tools.ResolveSymlinks(currDir)
return cfg.LocalWorkingDir(), currDir, cfg.LocalWorkingDir() == currDir, nil
}
// Are we running on Windows? Need to handle some extra path shenanigans
func IsWindows() bool {
return GetPlatform() == PlatformWindows
}
func CopyFileContents(cfg *config.Configuration, src string, dst string) error {
tmp, err := TempFile(cfg, filepath.Base(dst))
if err != nil {
return err
}
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(tmp, in)
if err != nil {
return err
}
err = tmp.Close()
if err != nil {
return err
}
return os.Rename(tmp.Name(), dst)
}
func LinkOrCopy(cfg *config.Configuration, src string, dst string) error {
if src == dst {
return nil
}
err := os.Link(src, dst)
if err == nil {
return err
}
return CopyFileContents(cfg, src, dst)
}
// TempFile creates a temporary file in the temporary directory specified by the
// configuration that has the proper permissions for the repository. On
// success, it returns an open, non-nil *os.File, and the caller is responsible
// for closing and/or removing it. On failure, the temporary file is
// automatically cleaned up and an error returned.
//
// This function is designed to handle only temporary files that will be renamed
// into place later somewhere within the Git repository.
func TempFile(cfg *config.Configuration, pattern string) (*os.File, error) {
return tools.TempFile(cfg.TempDir(), pattern, cfg)
}
|