()
| 131 | } |
| 132 | |
| 133 | func (w *PackWriter) save() error { |
| 134 | base := w.fs.Join(objectsPath, packPath, fmt.Sprintf("pack-%s", w.checksum)) |
| 135 | |
| 136 | // Pack files are content addressable. Each file is checked |
| 137 | // individually — if it already exists on disk, skip creating it. |
| 138 | idxPath := fmt.Sprintf("%s.idx", base) |
| 139 | exists, err := fileExists(w.fs, idxPath) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | if !exists { |
| 144 | idx, err := w.fs.Create(idxPath) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | if err := w.encodeIdx(idx); err != nil { |
| 150 | _ = idx.Close() |
| 151 | return err |
| 152 | } |
| 153 | |
| 154 | if err := idx.Close(); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | fixPermissions(w.fs, idxPath) |
| 158 | } |
| 159 | |
| 160 | packPath := fmt.Sprintf("%s.pack", base) |
| 161 | exists, err = fileExists(w.fs, packPath) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | if !exists { |
| 166 | if err := w.fs.Rename(w.fw.Name(), packPath); err != nil { |
| 167 | return err |
| 168 | } |
| 169 | fixPermissions(w.fs, packPath) |
| 170 | } else { |
| 171 | // Pack already exists, clean up the temp file. |
| 172 | return w.clean() |
| 173 | } |
| 174 | |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | // fileExists checks whether path already exists as a regular file. |
| 179 | // It returns (true, nil) for an existing regular file, (false, nil) when the |
no test coverage detected