| 1 | // Package lfs brings together the core LFS functionality |
| 2 | // NOTE: Subject to change, do not rely on this package from outside git-lfs source |
| 3 | package lfs |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/git-lfs/git-lfs/v3/config" |
| 12 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 13 | "github.com/git-lfs/git-lfs/v3/tools" |
| 14 | "github.com/git-lfs/git-lfs/v3/tq" |
| 15 | "github.com/rubyist/tracerx" |
| 16 | ) |
| 17 | |
| 18 | func Environ(cfg *config.Configuration, manifest tq.Manifest, envOverrides map[string]string) []string { |
| 19 | osEnviron := os.Environ() |
| 20 | env := make([]string, 0, len(osEnviron)+7) |
| 21 | |
| 22 | api := lfsapi.NewClient(cfg) |
| 23 | defer api.Close() |
| 24 | |
| 25 | if envOverrides == nil { |
| 26 | envOverrides = make(map[string]string, 0) |
| 27 | } |
| 28 | |
| 29 | download := api.Endpoints.AccessFor(api.Endpoints.Endpoint("download", cfg.Remote()).Url) |
| 30 | upload := api.Endpoints.AccessFor(api.Endpoints.Endpoint("upload", cfg.PushRemote()).Url) |
| 31 | |
| 32 | dltransfers := manifest.GetDownloadAdapterNames() |
| 33 | sort.Strings(dltransfers) |
| 34 | ultransfers := manifest.GetUploadAdapterNames() |
| 35 | sort.Strings(ultransfers) |
| 36 | |
| 37 | fetchPruneConfig := NewFetchPruneConfig(cfg.Git) |
| 38 | |
| 39 | references := strings.Join(cfg.LocalReferenceDirs(), ", ") |
| 40 | |
| 41 | env = append(env, |
| 42 | fmt.Sprintf("LocalWorkingDir=%s", cfg.LocalWorkingDir()), |
| 43 | fmt.Sprintf("LocalGitDir=%s", cfg.LocalGitDir()), |
| 44 | fmt.Sprintf("LocalGitStorageDir=%s", cfg.LocalGitStorageDir()), |
| 45 | fmt.Sprintf("LocalMediaDir=%s", cfg.LFSObjectDir()), |
| 46 | fmt.Sprintf("LocalReferenceDirs=%s", references), |
| 47 | fmt.Sprintf("TempDir=%s", cfg.TempDir()), |
| 48 | fmt.Sprintf("ConcurrentTransfers=%d", api.ConcurrentTransfers()), |
| 49 | fmt.Sprintf("TusTransfers=%v", cfg.TusTransfersAllowed()), |
| 50 | fmt.Sprintf("BasicTransfersOnly=%v", cfg.BasicTransfersOnly()), |
| 51 | fmt.Sprintf("SkipDownloadErrors=%v", cfg.SkipDownloadErrors()), |
| 52 | fmt.Sprintf("FetchRecentAlways=%v", fetchPruneConfig.FetchRecentAlways), |
| 53 | fmt.Sprintf("FetchRecentRefsDays=%d", fetchPruneConfig.FetchRecentRefsDays), |
| 54 | fmt.Sprintf("FetchRecentCommitsDays=%d", fetchPruneConfig.FetchRecentCommitsDays), |
| 55 | fmt.Sprintf("FetchRecentRefsIncludeRemotes=%v", fetchPruneConfig.FetchRecentRefsIncludeRemotes), |
| 56 | fmt.Sprintf("PruneOffsetDays=%d", fetchPruneConfig.PruneOffsetDays), |
| 57 | fmt.Sprintf("PruneVerifyRemoteAlways=%v", fetchPruneConfig.PruneVerifyRemoteAlways), |
| 58 | fmt.Sprintf("PruneVerifyUnreachableAlways=%v", fetchPruneConfig.PruneVerifyUnreachableAlways), |
| 59 | fmt.Sprintf("PruneRemoteName=%s", fetchPruneConfig.PruneRemoteName), |
| 60 | fmt.Sprintf("LfsStorageDir=%s", cfg.LFSStorageDir()), |
| 61 | fmt.Sprintf("AccessDownload=%s", download.Mode()), |
| 62 | fmt.Sprintf("AccessUpload=%s", upload.Mode()), |
| 63 | fmt.Sprintf("DownloadTransfers=%s", strings.Join(dltransfers, ",")), |
| 64 | fmt.Sprintf("UploadTransfers=%s", strings.Join(ultransfers, ",")), |
| 65 | ) |
| 66 | if len(cfg.FetchExcludePaths()) > 0 { |
| 67 | env = append(env, fmt.Sprintf("FetchExclude=%s", strings.Join(cfg.FetchExcludePaths(), ", "))) |
| 68 | } |
| 69 | if len(cfg.FetchIncludePaths()) > 0 { |
| 70 | env = append(env, fmt.Sprintf("FetchInclude=%s", strings.Join(cfg.FetchIncludePaths(), ", "))) |
| 71 | } |
| 72 | for _, ext := range cfg.Extensions() { |
| 73 | env = append(env, fmt.Sprintf("Extension[%d]=%s", ext.Priority, ext.Name)) |
| 74 | } |
| 75 | |
| 76 | for _, e := range osEnviron { |
| 77 | key := strings.SplitN(e, "=", 2)[0] |
| 78 | if !strings.HasPrefix(key, "GIT_") { |
| 79 | continue |
| 80 | } |
| 81 | if val, ok := envOverrides[key]; ok { |
| 82 | env = append(env, fmt.Sprintf("%s=%s", key, val)) |
| 83 | } else { |
| 84 | env = append(env, e) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return env |
| 89 | } |
| 90 | |
| 91 | func init() { |
| 92 | tracerx.DefaultKey = "GIT" |
| 93 | tracerx.Prefix = "trace git-lfs: " |
| 94 | if len(os.Getenv("GIT_TRACE")) < 1 { |
| 95 | if tt := os.Getenv("GIT_TRANSFER_TRACE"); len(tt) > 0 { |
| 96 | os.Setenv("GIT_TRACE", tt) |
| 97 | } else if cv := os.Getenv("GIT_CURL_VERBOSE"); len(cv) > 0 { |
| 98 | os.Setenv("GIT_TRACE", cv) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | const ( |
| 104 | gitExt = ".git" |
| 105 | gitPtrPrefix = "gitdir: " |
| 106 | ) |
| 107 | |
| 108 | func LinkOrCopyFromReference(cfg *config.Configuration, oid string, size int64) error { |
| 109 | if cfg.LFSObjectExists(oid, size) { |
| 110 | return nil |
| 111 | } |
| 112 | altMediafiles := cfg.Filesystem().ObjectReferencePaths(oid) |
| 113 | mediafile, err := cfg.Filesystem().ObjectPath(oid) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | for _, altMediafile := range altMediafiles { |
| 118 | tracerx.Printf("altMediafile: %s", altMediafile) |
| 119 | if altMediafile != "" && tools.FileExistsOfSize(altMediafile, size) { |
| 120 | err = LinkOrCopy(cfg, altMediafile, mediafile) |
| 121 | if err == nil { |
| 122 | break |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return err |
| 127 | } |