| 1 |
# gitobj |
| 2 |
|
| 3 |
[](https://travis-ci.org/git-lfs/gitobj) [](https://godoc.org/github.com/git-lfs/gitobj) |
| 4 |
|
| 5 |
Package `gitobj` reads and writes loose and packed Git objects. |
| 6 |
|
| 7 |
## Getting Started |
| 8 |
|
| 9 |
To access a repository's objects, begin by "opening" that repository for use: |
| 10 |
|
| 11 |
```go |
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"github.com/git-lfs/gitobj" |
| 16 |
) |
| 17 |
|
| 18 |
func main() { |
| 19 |
repo, err := gitobj.FromFilesystem("/path/to/repo.git", "") |
| 20 |
if err != nil { |
| 21 |
panic(err) |
| 22 |
} |
| 23 |
defer repo.Close() |
| 24 |
} |
| 25 |
``` |
| 26 |
|
| 27 |
You can then open objects for inspection with the [`Blob()`][blob], |
| 28 |
[`Commit()`][commit], [`Tag()`][tag], or [`Tree()`][tree] functions: |
| 29 |
|
| 30 |
[blob]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.Blob |
| 31 |
[commit]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.Commit |
| 32 |
[tag]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.Tag |
| 33 |
[tree]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.Tree |
| 34 |
|
| 35 |
```go |
| 36 |
func main() { |
| 37 |
repo, err := gitobj.FromFilesystem("/path/to/repo.git", "") |
| 38 |
if err != nil { |
| 39 |
panic(err) |
| 40 |
} |
| 41 |
defer repo.Close() |
| 42 |
|
| 43 |
commit, err := repo.Commit([]byte{...}) |
| 44 |
if err != nil { |
| 45 |
panic(err) |
| 46 |
} |
| 47 |
} |
| 48 |
``` |
| 49 |
|
| 50 |
Once an object is opened or an instance is held, it can be saved to the object |
| 51 |
database using the [`WriteBlob()`][wblob], [`WriteCommit()`][wcommit], |
| 52 |
[`WriteTag()`][wtag], or [`WriteTree()`][wtree] functions: |
| 53 |
|
| 54 |
[wblob]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.WriteBlob |
| 55 |
[wcommit]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.WriteCommit |
| 56 |
[wtag]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.WriteTag |
| 57 |
[wtree]: https://godoc.org/github.com/git-lfs/gitobj#ObjectDatabase.WriteTree |
| 58 |
|
| 59 |
```go |
| 60 |
func main() { |
| 61 |
repo, err := gitobj.FromFilesystem("/path/to/repo.git", "") |
| 62 |
if err != nil { |
| 63 |
panic(err) |
| 64 |
} |
| 65 |
defer repo.Close() |
| 66 |
|
| 67 |
commit, err := repo.Commit([]byte{...}) |
| 68 |
if err != nil { |
| 69 |
panic(err) |
| 70 |
} |
| 71 |
|
| 72 |
commit.Message = "Hello from gitobj!" |
| 73 |
commit.ExtraHeaders = append(commit.ExtraHeaders, &gitobj.ExtraHeader{ |
| 74 |
K: "Signed-off-by", |
| 75 |
V: "Jane Doe <[email protected]>", |
| 76 |
}) |
| 77 |
|
| 78 |
if _, err := repository.WriteCommit(commit); err != nil { |
| 79 |
panic(err) |
| 80 |
} |
| 81 |
} |
| 82 |
``` |
| 83 |
|
| 84 |
### Packed Objects |
| 85 |
|
| 86 |
Package `gitobj` has support for reading "packed" objects (i.e., objects found |
| 87 |
in [packfiles][1]) via package `github.com/git-lfs/gitobj/pack`. Package `pack` |
| 88 |
implements searching pack index (`.idx`) files and locating the corresponding |
| 89 |
delta-base chain in the appropriate `pack` file. It understands both version |
| 90 |
1 and version 2 of the packfile specification. |
| 91 |
|
| 92 |
`gitobj` will always try to locate a loose object first. If a loose object |
| 93 |
cannot be found with the appropriate SHA-1, the repository's packfile(s) will |
| 94 |
be searched. If an object is located in a packfile, that object will be |
| 95 |
reconstructed along its delta-base chain and then returned transparently. |
| 96 |
|
| 97 |
### More information |
| 98 |
|
| 99 |
For more: https://godoc.org/github.com/git-lfs/gitobj. |
| 100 |
|
| 101 |
## License |
| 102 |
|
| 103 |
MIT. |
| 104 |
|
| 105 |
[1]: https://git-scm.com/book/en/v2/Git-Internals-Packfiles |