| 1 |
package packfile |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 7 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 8 |
) |
| 9 |
|
| 10 |
var signature = []byte{'P', 'A', 'C', 'K'} |
| 11 |
|
| 12 |
const ( |
| 13 |
// VersionSupported is the packfile version supported by this package |
| 14 |
VersionSupported uint32 = 2 |
| 15 |
|
| 16 |
firstLengthBits = uint8(4) // the first byte into object header has 4 bits to store the length |
| 17 |
lengthBits = uint8(7) // subsequent bytes has 7 bits to store the length |
| 18 |
maskFirstLength = 15 // 0000 1111 |
| 19 |
maskContinue = 0x80 // 1000 0000 |
| 20 |
maskLength = uint8(127) // 0111 1111 |
| 21 |
maskType = uint8(112) // 0111 0000 |
| 22 |
) |
| 23 |
|
| 24 |
// UpdateObjectStorage updates the storer with the objects in the given |
| 25 |
// packfile. |
| 26 |
func UpdateObjectStorage(s storer.Storer, packfile io.Reader) error { |
| 27 |
if pw, ok := s.(storer.PackfileWriter); ok { |
| 28 |
return WritePackfileToObjectStorage(pw, packfile) |
| 29 |
} |
| 30 |
|
| 31 |
p, err := NewParserWithStorage(NewScanner(packfile), s) |
| 32 |
if err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
|
| 36 |
_, err = p.Parse() |
| 37 |
return err |
| 38 |
} |
| 39 |
|
| 40 |
// WritePackfileToObjectStorage writes all the packfile objects into the given |
| 41 |
// object storage. |
| 42 |
func WritePackfileToObjectStorage( |
| 43 |
sw storer.PackfileWriter, |
| 44 |
packfile io.Reader, |
| 45 |
) (err error) { |
| 46 |
w, err := sw.PackfileWriter() |
| 47 |
if err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
|
| 51 |
defer ioutil.CheckClose(w, &err) |
| 52 |
|
| 53 |
var n int64 |
| 54 |
n, err = io.Copy(w, packfile) |
| 55 |
if err == nil && n == 0 { |
| 56 |
return ErrEmptyPackfile |
| 57 |
} |
| 58 |
|
| 59 |
return err |
| 60 |
} |