Debian Codegraph @

commands/command_lock.go

raw symbols SCIP
1 package commands
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strings"
8
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/locking"
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 lockRemote string
19 )
20
21 func lockCommand(cmd *cobra.Command, args []string) {
22 if len(lockRemote) > 0 {
23 cfg.SetRemote(lockRemote)
24 cfg.SetPushRemote(lockRemote)
25 }
26
27 lockData, err := computeLockData()
28 if err != nil {
29 ExitWithError(err)
30 }
31
32 refUpdate := git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil)
33 lockClient := newLockClient()
34 lockClient.RemoteRef = refUpdate.RemoteRef()
35 defer lockClient.Close()
36
37 success := true
38 locks := make([]locking.Lock, 0, len(args))
39 for _, path := range args {
40 path, err := lockPath(lockData, path)
41 if err != nil {
42 Error(err.Error())
43 success = false
44 continue
45 }
46
47 lock, err := lockClient.LockFile(path)
48 if err != nil {
49 Error(tr.Tr.Get("Locking %s failed: %v", path, errors.Cause(err)))
50 success = false
51 continue
52 }
53
54 locks = append(locks, lock)
55
56 if locksCmdFlags.JSON {
57 continue
58 }
59
60 Print(tr.Tr.Get("Locked %s", path))
61 }
62
63 if locksCmdFlags.JSON {
64 if err := json.NewEncoder(os.Stdout).Encode(locks); err != nil {
65 Error(err.Error())
66 success = false
67 }
68 }
69
70 if !success {
71 lockClient.Close()
72 ExitWithCode(2)
73 }
74 }
75
76 type lockData struct {
77 rootDir string
78 workingDir string
79 }
80
81 // computeLockData computes data about the given repository and working
82 // directory to use in lockPath.
83 func computeLockData() (*lockData, error) {
84 wd, err := tools.Getwd()
85 if err != nil {
86 return nil, err
87 }
88 wd, err = tools.CanonicalizeSystemPath(wd)
89 if err != nil {
90 return nil, err
91 }
92 return &lockData{
93 rootDir: cfg.LocalWorkingDir(),
94 workingDir: wd,
95 }, nil
96 }
97
98 // lockPath relativizes the given filepath such that it is relative to the root
99 // path of the repository it is contained within, taking into account the
100 // working directory of the caller.
101 //
102 // lockPaths also respects different filesystem directory separators, so that a
103 // Windows path of "\foo\bar" will be normalized to "foo/bar".
104 //
105 // If the file path cannot be determined, an error will be returned. If the file
106 // in question is actually a directory, an error will be returned. Otherwise,
107 // the cleaned path will be returned.
108 //
109 // For example:
110 // - Working directory: /code/foo/bar/
111 // - Repository root: /code/foo/
112 // - File to lock: ./baz
113 // - Resolved path bar/baz
114 func lockPath(data *lockData, file string) (string, error) {
115 var abs string
116 var err error
117
118 if filepath.IsAbs(file) {
119 abs, err = tools.CanonicalizeSystemPath(file)
120 if err != nil {
121 return "", errors.New(tr.Tr.Get("unable to canonicalize path %q: %v", file, err))
122 }
123 } else {
124 abs = filepath.Join(data.workingDir, file)
125 }
126 path, err := filepath.Rel(data.rootDir, abs)
127 if err != nil {
128 return "", err
129 }
130
131 path = filepath.ToSlash(path)
132 if strings.HasPrefix(path, "../") {
133 return "", errors.New(tr.Tr.Get("unable to canonicalize path %q", path))
134 }
135
136 if stat, err := os.Stat(abs); err == nil && stat.IsDir() {
137 return path, errors.New(tr.Tr.Get("cannot lock directory: %s", file))
138 }
139
140 return filepath.ToSlash(path), nil
141 }
142
143 func init() {
144 RegisterCommand("lock", lockCommand, func(cmd *cobra.Command) {
145 cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", "specify which remote to use when interacting with locks")
146 cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "j", false, "Give the output in a stable JSON format for scripts")
147 })
148 }