Debian Codegraph @

commands/command_dedup.go

raw symbols SCIP
1 package commands
2
3 import (
4 "os"
5 "path/filepath"
6 "sync/atomic"
7
8 "github.com/git-lfs/git-lfs/v3/config"
9 "github.com/git-lfs/git-lfs/v3/errors"
10 "github.com/git-lfs/git-lfs/v3/git"
11 "github.com/git-lfs/git-lfs/v3/lfs"
12 "github.com/git-lfs/git-lfs/v3/tools"
13 "github.com/git-lfs/git-lfs/v3/tr"
14 "github.com/spf13/cobra"
15 )
16
17 var (
18 dedupFlags = struct {
19 test bool
20 }{}
21 dedupStats = &struct {
22 totalProcessedCount int64
23 totalProcessedSize int64
24 }{}
25 )
26
27 func dedupTestCommand(*cobra.Command, []string) {
28 setupRepository()
29
30 if supported, err := tools.CheckCloneFileSupported(cfg.TempDir()); err != nil || !supported {
31 if err == nil {
32 err = errors.New(tr.Tr.Get("Unknown reason"))
33 }
34 Exit(tr.Tr.Get("This system does not support de-duplication: %s", err))
35 }
36
37 if len(cfg.Extensions()) > 0 {
38 Exit(tr.Tr.Get("This platform supports file de-duplication, however, Git LFS extensions are configured and therefore de-duplication can not be used."))
39 }
40
41 Print(tr.Tr.Get("OK: This platform and repository support file de-duplication."))
42 }
43
44 func dedupCommand(cmd *cobra.Command, args []string) {
45 if dedupFlags.test {
46 dedupTestCommand(cmd, args)
47 return
48 }
49
50 setupRepository()
51 if gitDir, err := git.GitDir(); err != nil {
52 ExitWithError(err)
53 } else if supported, err := tools.CheckCloneFileSupported(gitDir); err != nil || !supported {
54 Exit(tr.Tr.Get("This system does not support de-duplication."))
55 }
56
57 if len(cfg.Extensions()) > 0 {
58 Exit(tr.Tr.Get("This platform supports file de-duplication, however, Git LFS extensions are configured and therefore de-duplication can not be used."))
59 }
60
61 if dirty, err := git.IsWorkingCopyDirty(); err != nil {
62 ExitWithError(err)
63 } else if dirty {
64 Exit(tr.Tr.Get("Working tree is dirty. Please commit or reset your change."))
65 }
66
67 // We assume working tree is clean.
68 gitScanner := lfs.NewGitScanner(config.New(), func(p *lfs.WrappedPointer, err error) {
69 if err != nil {
70 Exit(tr.Tr.Get("Could not scan for Git LFS tree: %s", err))
71 return
72 }
73
74 if success, err := dedup(p); err != nil {
75 // TRANSLATORS: Leading spaces should be included on
76 // the second line so the format specifier aligns with
77 // with the first format specifier on the first line.
78 Error(tr.Tr.Get("Skipped: %s (Size: %d)\n %s", p.Name, p.Size, err))
79 } else if !success {
80 Error(tr.Tr.Get("Skipped: %s (Size: %d)", p.Name, p.Size))
81 } else if success {
82 Print(tr.Tr.Get("Success: %s (Size: %d)", p.Name, p.Size))
83
84 atomic.AddInt64(&dedupStats.totalProcessedCount, 1)
85 atomic.AddInt64(&dedupStats.totalProcessedSize, p.Size)
86 }
87 })
88
89 if err := gitScanner.ScanTree("HEAD", nil); err != nil {
90 ExitWithError(err)
91 }
92
93 // TRANSLATORS: The second and third strings should have the colons
94 // aligned in a column.
95 Print("\n\n%s\n %s\n %s", tr.Tr.Get("Finished successfully."),
96 tr.Tr.GetN(
97 "De-duplicated size: %d byte",
98 "De-duplicated size: %d bytes",
99 int(dedupStats.totalProcessedSize),
100 dedupStats.totalProcessedSize),
101 tr.Tr.Get(" count: %d", dedupStats.totalProcessedCount))
102 }
103
104 // dedup executes
105 // Precondition: working tree MUST clean. We can replace working tree files from mediafile safely.
106 func dedup(p *lfs.WrappedPointer) (success bool, err error) {
107 // PRECONDITION, check ofs object exists or skip this file.
108 if !cfg.LFSObjectExists(p.Oid, p.Size) { // Not exists,
109 // Basically, this is not happens because executing 'git status' in `git.IsWorkingCopyDirty()` recover it.
110 return false, errors.New(tr.Tr.Get("Git LFS object file does not exist"))
111 }
112
113 // DO de-dup
114 // Gather original state
115 originalStat, err := os.Stat(p.Name)
116 if err != nil {
117 return false, err
118 }
119
120 // Do clone
121 srcFile := cfg.Filesystem().ObjectPathname(p.Oid)
122 if srcFile == os.DevNull {
123 return true, nil
124 }
125
126 dstFile := filepath.Join(cfg.LocalWorkingDir(), p.Name)
127
128 // Clone the file. This overwrites the destination if it exists.
129 if ok, err := tools.CloneFileByPath(dstFile, srcFile); err != nil {
130 return false, err
131 } else if !ok {
132 return false, errors.New(tr.Tr.Get("unknown clone file error"))
133 }
134
135 // Recover original state
136 if err := os.Chmod(dstFile, originalStat.Mode()); err != nil {
137 return false, err
138 }
139
140 return true, nil
141 }
142
143 func init() {
144 RegisterCommand("dedup", dedupCommand, func(cmd *cobra.Command) {
145 cmd.Flags().BoolVarP(&dedupFlags.test, "test", "t", false, "test")
146 })
147 }