Skip to content

Instantly share code, notes, and snippets.

@artisonian
Created July 31, 2013 08:44
Show Gist options
  • Save artisonian/6120432 to your computer and use it in GitHub Desktop.
Save artisonian/6120432 to your computer and use it in GitHub Desktop.
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