MCPcopy
hub / github.com/git-lfs/git-lfs / Save

Method Save

tools/kv/keyvaluestore.go:120–158  ·  view source on GitHub ↗

Save persists the changes made to disk If any changes have been written by other code they will be merged

()

Source from the content-addressed store, hash-verified

118// Save persists the changes made to disk
119// If any changes have been written by other code they will be merged
120func (k *Store) Save() error {
121 k.mu.Lock()
122 defer k.mu.Unlock()
123
124 // Short-circuit if we have no changes
125 if len(k.log) == 0 {
126 return nil
127 }
128
129 // firstly peek at version; open read/write to keep lock between check & write
130 f, err := os.OpenFile(k.filename, os.O_RDWR|os.O_CREATE, 0664)
131 if err != nil {
132 return err
133 }
134
135 defer f.Close()
136
137 // Only try to merge if > 0 bytes, ignore empty files (decoder will fail)
138 if stat, _ := f.Stat(); stat.Size() > 0 {
139 k.loadAndMergeReaderIfNeeded(f)
140 // Now we overwrite the file
141 f.Seek(0, io.SeekStart)
142 f.Truncate(0)
143 }
144
145 k.version++
146
147 enc := gob.NewEncoder(f)
148 if err := enc.Encode(k.version); err != nil {
149 return errors.New(tr.Tr.Get("error while writing version data to %v: %v", k.filename, err))
150 }
151 if err := enc.Encode(k.db); err != nil {
152 return errors.New(tr.Tr.Get("error while writing new key/value data to %v: %v", k.filename, err))
153 }
154 // Clear log now that it's saved
155 k.log = nil
156
157 return nil
158}
159
160// Reads as little as possible from the passed in file to determine if the
161// contents are different from the version already held. If so, reads the

Callers 3

TestStoreSimpleFunction · 0.95
TestStoreReduceSizeFunction · 0.95

Calls 9

NewFunction · 0.92
SizeMethod · 0.80
SeekMethod · 0.80
EncodeMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65

Tested by 3

TestStoreSimpleFunction · 0.76
TestStoreReduceSizeFunction · 0.76