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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
package commands
import (
"bytes"
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/git-lfs/git-lfs/v3/config"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/filepathfilter"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/lfs"
"github.com/git-lfs/git-lfs/v3/lfsapi"
"github.com/git-lfs/git-lfs/v3/locking"
"github.com/git-lfs/git-lfs/v3/subprocess"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tq"
"github.com/git-lfs/git-lfs/v3/tr"
)
// Populate man pages
//go:generate go run ../docs/man/mangen.go
var (
Debugging = false
ErrorBuffer = &bytes.Buffer{}
ErrorWriter = newMultiWriter(os.Stderr, ErrorBuffer)
OutputWriter = newMultiWriter(os.Stdout, ErrorBuffer)
ManPages = make(map[string]string, 20)
tqManifest = make(map[string]tq.Manifest)
cfg *config.Configuration
apiClient *lfsapi.Client
global sync.Mutex
oldEnv = make(map[string]string)
includeArg string
excludeArg string
)
// getTransferManifest builds a tq.Manifest from the global os and git
// environments.
func getTransferManifest() tq.Manifest {
return getTransferManifestOperationRemote("", "")
}
// getTransferManifestOperationRemote builds a tq.Manifest from the global os
// and git environments and operation-specific and remote-specific settings.
// Operation must be "download", "upload", or the empty string.
func getTransferManifestOperationRemote(operation, remote string) tq.Manifest {
c := getAPIClient()
global.Lock()
defer global.Unlock()
k := fmt.Sprintf("%s.%s", operation, remote)
if tqManifest[k] == nil {
tqManifest[k] = tq.NewManifest(cfg.Filesystem(), c, operation, remote)
}
return tqManifest[k]
}
func getAPIClient() *lfsapi.Client {
global.Lock()
defer global.Unlock()
if apiClient == nil {
c, err := lfsapi.NewClient(cfg)
if err != nil {
ExitWithError(err)
}
apiClient = c
}
return apiClient
}
func closeAPIClient() error {
global.Lock()
defer global.Unlock()
if apiClient == nil {
return nil
}
return apiClient.Close()
}
func newLockClient() *locking.Client {
lockClient, err := locking.NewClient(cfg.PushRemote(), getAPIClient(), cfg)
if err == nil {
tools.MkdirAll(cfg.LFSStorageDir(), cfg)
err = lockClient.SetupFileCache(cfg.LFSStorageDir())
}
if err != nil {
Exit(tr.Tr.Get("Unable to create lock system: %v", err.Error()))
}
// Configure dirs
lockClient.LocalWorkingDir = cfg.LocalWorkingDir()
lockClient.LocalGitDir = cfg.LocalGitDir()
lockClient.SetLockableFilesReadOnly = cfg.SetLockableFilesReadOnly()
return lockClient
}
// newDownloadCheckQueue builds a checking queue, checks that objects are there but doesn't download
func newDownloadCheckQueue(manifest tq.Manifest, remote string, options ...tq.Option) *tq.TransferQueue {
return newDownloadQueue(manifest, remote, append(options,
tq.DryRun(true),
)...)
}
// newDownloadQueue builds a DownloadQueue, allowing concurrent downloads.
func newDownloadQueue(manifest tq.Manifest, remote string, options ...tq.Option) *tq.TransferQueue {
return tq.NewTransferQueue(tq.Download, manifest, remote, append(options,
tq.RemoteRef(currentRemoteRef()),
tq.WithBatchSize(cfg.TransferBatchSize()),
)...)
}
func currentRemoteRef() *git.Ref {
return git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil).RemoteRef()
}
func buildFilepathFilter(config *config.Configuration, includeArg, excludeArg *string, useFetchOptions bool) *filepathfilter.Filter {
return buildFilepathFilterWithPatternType(config, includeArg, excludeArg, useFetchOptions, filepathfilter.GitIgnore)
}
func buildFilepathFilterWithPatternType(config *config.Configuration, includeArg, excludeArg *string, useFetchOptions bool, patternType filepathfilter.PatternType) *filepathfilter.Filter {
inc, exc := determineIncludeExcludePaths(config, includeArg, excludeArg, useFetchOptions)
return filepathfilter.New(inc, exc, patternType)
}
func downloadTransfer(p *lfs.WrappedPointer) (name, path, oid string, size int64, missing bool, err error) {
path, err = cfg.Filesystem().ObjectPath(p.Oid)
return p.Name, path, p.Oid, p.Size, false, err
}
// Get user-readable manual install steps for hooks
func getHookInstallSteps() string {
hookDir, err := cfg.HookDir()
if err != nil {
ExitWithError(err)
}
hooks := lfs.LoadHooks(hookDir, cfg)
hookDir = filepath.ToSlash(hookDir)
workingDir := filepath.ToSlash(fmt.Sprintf("%s%c", cfg.LocalWorkingDir(), os.PathSeparator))
steps := make([]string, 0, len(hooks))
for _, h := range hooks {
steps = append(steps, fmt.Sprintf("%s\n\n%s",
tr.Tr.Get("Add the following to '%s/%s':", strings.TrimPrefix(hookDir, workingDir), h.Type),
tools.Indent(h.Contents)))
}
return strings.Join(steps, "\n\n")
}
func installHooks(force bool) error {
hookDir, err := cfg.HookDir()
if err != nil {
return err
}
hooks := lfs.LoadHooks(hookDir, cfg)
for _, h := range hooks {
if err := h.Install(force); err != nil {
return err
}
}
return nil
}
// uninstallHooks removes all hooks in range of the `hooks` var.
func uninstallHooks() error {
if !cfg.InRepo() {
return errors.New(tr.Tr.Get("Not in a Git repository"))
}
hookDir, err := cfg.HookDir()
if err != nil {
return err
}
hooks := lfs.LoadHooks(hookDir, cfg)
for _, h := range hooks {
if err := h.Uninstall(); err != nil {
return err
}
}
return nil
}
// Error prints a formatted message to Stderr. It also gets printed to the
// panic log if one is created for this command.
func Error(format string, args ...interface{}) {
if len(args) == 0 {
fmt.Fprintln(ErrorWriter, format)
return
}
fmt.Fprintf(ErrorWriter, format+"\n", args...)
}
// Print prints a formatted message to Stdout. It also gets printed to the
// panic log if one is created for this command.
func Print(format string, args ...interface{}) {
if len(args) == 0 {
fmt.Fprintln(OutputWriter, format)
return
}
fmt.Fprintf(OutputWriter, format+"\n", args...)
}
// Exit prints a formatted message and exits.
func Exit(format string, args ...interface{}) {
Error(format, args...)
os.Exit(2)
}
// ExitWithError either panics with a full stack trace for fatal errors, or
// simply prints the error message and exits immediately.
func ExitWithError(err error) {
errorWith(err, Panic, Exit)
}
// FullError prints either a full stack trace for fatal errors, or just the
// error message.
func FullError(err error) {
errorWith(err, LoggedError, Error)
}
func errorWith(err error, fatalErrFn func(error, string, ...interface{}), errFn func(string, ...interface{})) {
if Debugging || errors.IsFatalError(err) {
fatalErrFn(err, "%s", err)
return
}
errFn("%s", err)
}
// Debug prints a formatted message if debugging is enabled. The formatted
// message also shows up in the panic log, if created.
func Debug(format string, args ...interface{}) {
if !Debugging {
return
}
log.Printf(format, args...)
}
// LoggedError prints the given message formatted with its arguments (if any) to
// Stderr. If an empty string is passed as the "format" argument, only the
// standard error logging message will be printed, and the error's body will be
// omitted.
//
// It also writes a stack trace for the error to a log file without exiting.
func LoggedError(err error, format string, args ...interface{}) {
if len(format) > 0 {
Error(format, args...)
}
file := handlePanic(err)
if len(file) > 0 {
fmt.Fprintf(os.Stderr, "\n%s\n", tr.Tr.Get("Errors logged to '%s'.\nUse `git lfs logs last` to view the log.", file))
}
}
// Panic prints a formatted message, and writes a stack trace for the error to
// a log file before exiting.
func Panic(err error, format string, args ...interface{}) {
LoggedError(err, format, args...)
os.Exit(2)
}
func Cleanup() {
if err := cfg.Cleanup(); err != nil {
fmt.Fprintln(os.Stderr, tr.Tr.Get("Error clearing old temporary files: %s", err))
}
}
func requireStdin(msg string) {
var out string
stat, err := os.Stdin.Stat()
if err != nil {
out = tr.Tr.Get("Cannot read from STDIN: %s (%s)", msg, err)
} else if (stat.Mode() & os.ModeCharDevice) != 0 {
out = tr.Tr.Get("Cannot read from STDIN: %s", msg)
}
if len(out) > 0 {
Error(out)
os.Exit(1)
}
}
func requireInRepo() {
if !cfg.InRepo() {
Print(tr.Tr.Get("Not in a Git repository."))
os.Exit(128)
}
}
// requireWorkingCopy requires that the working directory be a work tree, i.e.,
// that it not be bare. If it is bare (or the state of the repository could not
// be determined), this function will terminate the program.
func requireWorkingCopy() {
if cfg.LocalWorkingDir() == "" {
Print(tr.Tr.Get("This operation must be run in a work tree."))
os.Exit(128)
}
}
func setupRepository() {
requireInRepo()
bare, err := git.IsBare()
if err != nil {
ExitWithError(errors.Wrap(
err, tr.Tr.Get("Could not determine bareness")))
}
verifyRepositoryVersion()
if !bare {
changeToWorkingCopy()
}
}
func verifyRepositoryVersion() {
key := "lfs.repositoryformatversion"
val := cfg.FindGitLocalKey(key)
if val == "" {
cfg.SetGitLocalKey(key, "0")
} else if val != "0" {
Print(tr.Tr.Get("Unknown repository format version: %s", val))
os.Exit(128)
}
}
func setupWorkingCopy() {
requireInRepo()
requireWorkingCopy()
verifyRepositoryVersion()
changeToWorkingCopy()
}
func changeToWorkingCopy() {
workingDir := cfg.LocalWorkingDir()
cwd, err := tools.Getwd()
if err != nil {
ExitWithError(errors.Wrap(
err, tr.Tr.Get("Could not determine current working directory")))
}
cwd, err = tools.CanonicalizeSystemPath(cwd)
if err != nil {
ExitWithError(errors.Wrap(
err, tr.Tr.Get("Could not canonicalize current working directory")))
}
// If the current working directory is not within the repository's
// working directory, then let's change directories accordingly. This
// should only occur if GIT_WORK_TREE is set.
if !(strings.HasPrefix(cwd, workingDir) && (cwd == workingDir || (len(cwd) > len(workingDir) && cwd[len(workingDir)] == os.PathSeparator))) {
os.Chdir(workingDir)
}
}
func canonicalizeEnvironment() {
vars := []string{"GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR"}
for _, v := range vars {
val, ok := os.LookupEnv(v)
if ok {
path, err := tools.CanonicalizePath(val, true)
// We have existing code which relies on users being
// able to pass invalid paths, so don't fail if the path
// cannot be canonicalized.
if err == nil {
oldEnv[v] = val
os.Setenv(v, path)
}
}
}
subprocess.ResetEnvironment()
}
func handlePanic(err error) string {
if err == nil {
return ""
}
return logPanic(err)
}
func logPanic(loggedError error) string {
var (
fmtWriter io.Writer = os.Stderr
lineEnding string = "\n"
)
now := time.Now()
name := now.Format("20060102T150405.999999999")
full := filepath.Join(cfg.LocalLogDir(), name+".log")
if err := tools.MkdirAll(cfg.LocalLogDir(), cfg); err != nil {
full = ""
fmt.Fprintf(fmtWriter, "%s\n\n", tr.Tr.Get("Unable to log panic to '%s': %s", cfg.LocalLogDir(), err.Error()))
} else if file, err := os.Create(full); err != nil {
filename := full
full = ""
defer func() {
fmt.Fprintf(fmtWriter, "%s\n\n", tr.Tr.Get("Unable to log panic to '%s'", filename))
logPanicToWriter(fmtWriter, err, lineEnding)
}()
} else {
fmtWriter = file
lineEnding = gitLineEnding(cfg.Git)
defer file.Close()
}
logPanicToWriter(fmtWriter, loggedError, lineEnding)
return full
}
func ipAddresses() []string {
ips := make([]string, 0, 1)
ifaces, err := net.Interfaces()
if err != nil {
ips = append(ips, tr.Tr.Get("Error getting network interface: %s", err.Error()))
return ips
}
for _, i := range ifaces {
if i.Flags&net.FlagUp == 0 {
continue // interface down
}
if i.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, _ := i.Addrs()
l := make([]string, 0, 1)
if err != nil {
ips = append(ips, tr.Tr.Get("Error getting IP address: %s", err.Error()))
continue
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
l = append(l, ip.String())
}
if len(l) > 0 {
ips = append(ips, strings.Join(l, " "))
}
}
return ips
}
func logPanicToWriter(w io.Writer, loggedError error, le string) {
// log the version
gitV, err := git.Version()
if err != nil {
gitV = tr.Tr.Get("Error getting Git version: %s", err.Error())
}
fmt.Fprint(w, config.VersionDesc, le)
fmt.Fprint(w, gitV, le)
// log the command that was run
fmt.Fprint(w, le)
fmt.Fprintf(w, "$ %s", filepath.Base(os.Args[0]))
if len(os.Args) > 0 {
fmt.Fprintf(w, " %s", strings.Join(os.Args[1:], " "))
}
fmt.Fprint(w, le)
// log the error message and stack trace
w.Write(ErrorBuffer.Bytes())
fmt.Fprint(w, le)
fmt.Fprintf(w, "%+v%s", loggedError, le)
for key, val := range errors.Context(err) {
fmt.Fprintf(w, "%s=%v%s", key, val, le)
}
fmt.Fprint(w, le, tr.Tr.Get("Current time in UTC:"), le)
fmt.Fprint(w, time.Now().UTC().Format("2006-01-02 15:04:05"), le)
fmt.Fprint(w, le, tr.Tr.Get("Environment:"), le)
// log the environment
for _, env := range lfs.Environ(cfg, getTransferManifest(), oldEnv) {
fmt.Fprint(w, env, le)
}
fmt.Fprint(w, le, tr.Tr.Get("Client IP addresses:"), le)
for _, ip := range ipAddresses() {
fmt.Fprint(w, ip, le)
}
}
func determineIncludeExcludePaths(config *config.Configuration, includeArg, excludeArg *string, useFetchOptions bool) (include, exclude []string) {
if includeArg == nil {
if useFetchOptions {
include = config.FetchIncludePaths()
} else {
include = []string{}
}
} else {
include = tools.CleanPaths(*includeArg, ",")
}
if excludeArg == nil {
if useFetchOptions {
exclude = config.FetchExcludePaths()
} else {
exclude = []string{}
}
} else {
exclude = tools.CleanPaths(*excludeArg, ",")
}
return
}
func buildProgressMeter(dryRun bool, d tq.Direction) *tq.Meter {
m := tq.NewMeter(cfg)
m.Logger = m.LoggerFromEnv(cfg.Os)
m.DryRun = dryRun
m.Direction = d
return m
}
func requireGitVersion() {
minimumGit := "1.8.2"
if !git.IsGitVersionAtLeast(minimumGit) {
gitver, err := git.Version()
if err != nil {
Exit(tr.Tr.Get("Error getting Git version: %s", err))
}
Exit(tr.Tr.Get("Git version %s or higher is required for Git LFS; your version: %s", minimumGit, gitver))
}
}
|