Created
July 31, 2013 08:44
-
-
Save artisonian/6120432 to your computer and use it in GitHub Desktop.
Git object storage in Go (see http://git-scm.com/book/en/Git-Internals-Git-Objects#Object-Storage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/zlib" | |
"crypto/sha1" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path" | |
) | |
func logAndExit(err error) { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
func main() { | |
content := "what is up, doc?" | |
fmt.Println(content) | |
header := fmt.Sprintf("blob %d\000", len(content)) | |
fmt.Printf("%q\n", header) | |
store := header + content | |
fmt.Printf("%q\n", store) | |
h := sha1.New() | |
fmt.Fprint(h, store) | |
sha := fmt.Sprintf("%x", h.Sum(nil)) | |
fmt.Println(sha) | |
var b bytes.Buffer | |
w := zlib.NewWriter(&b) | |
w.Write([]byte(store)) | |
w.Close() | |
fmt.Printf("%q\n", b.Bytes()) | |
filepath := ".git/objects/" + sha[0:2] + "/" + sha[2:] | |
err := os.MkdirAll(path.Dir(filepath), 0755) | |
if err != nil { | |
logAndExit(err) | |
} | |
err = ioutil.WriteFile(filepath, b.Bytes(), 0644) | |
if err != nil { | |
logAndExit(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment