Browse Source

Resolve issues using empty notebooks

master 0.1.0
Trevor Slocum 4 years ago
parent
commit
5d39cbf04f
  1. 1
      .gitignore
  2. 18
      goreleaser.yml
  3. 21
      main.go
  4. 8
      notebook.go
  5. 31
      platformall.go
  6. 5
      platformwin.go
  7. 7
      stick.go
  8. 43
      web.go

1
.gitignore vendored

@ -3,3 +3,4 @@ dist/
*.sh
rice-box.go
stick
vendor/

18
goreleaser.yml

@ -9,20 +9,20 @@ builds:
pre: rice embed-go
post: rm rice-box.go
goos:
- darwin
- freebsd
# - darwin
# - freebsd
- linux
- windows
goarch:
- 386
- amd64
- arm
- arm64
- ppc64
- ppc64le
goarm:
- 6
- 7
# - arm
# - arm64
# - ppc64
# - ppc64le
# goarm:
# - 6
# - 7
archive:
replacements:
386: i386

21
main.go

@ -5,8 +5,6 @@ import (
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
@ -105,24 +103,7 @@ func main() {
stick.Config.Serve = address
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGUSR1, syscall.SIGUSR2)
go func() {
for sig := range sigc {
switch sig {
case syscall.SIGUSR1:
// TODO: Reload config
case syscall.SIGUSR2:
log.Println("Compacting notebooks...")
stick.NotebooksLock.Lock()
for _, notebook := range stick.Notebooks {
notebook.Compact()
}
stick.NotebooksLock.Unlock()
log.Println("Compacted notebooks")
}
}
}()
registerSignalHandlers()
log.Printf("serving shared notebooks on %s", stick.Config.Serve)
serveWeb()

8
notebook.go

@ -55,7 +55,9 @@ func (n *Notebook) getNote(id string) *Note {
r := n.Repository
ref, err := r.Head()
CheckError(err)
if err != nil {
return nil // Empty repository
}
cIter, err := r.Log(&git.LogOptions{From: ref.Hash(), Order: git.LogOrderCommitterTime, FileName: &file.Name})
CheckError(err)
@ -181,7 +183,9 @@ func (n *Notebook) File(id string) *object.File {
var file *object.File
ref, err := n.Repository.Head()
CheckError(err)
if err != nil {
return nil // Empty repository
}
commit, err := n.Repository.CommitObject(ref.Hash())
CheckError(err)

31
platformall.go

@ -0,0 +1,31 @@
// +build !windows
package main
import (
"log"
"os"
"os/signal"
"syscall"
)
func registerSignalHandlers() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGUSR1, syscall.SIGUSR2)
go func() {
for sig := range sigc {
switch sig {
case syscall.SIGUSR1:
// TODO: Reload config
case syscall.SIGUSR2:
log.Println("Compacting notebooks...")
stick.NotebooksLock.Lock()
for _, notebook := range stick.Notebooks {
notebook.Compact()
}
stick.NotebooksLock.Unlock()
log.Println("Compacted notebooks")
}
}
}()
}

5
platformwin.go

@ -0,0 +1,5 @@
// +build windows
package main
func registerSignalHandlers() {} // Unsupported

7
stick.go

@ -1,7 +1,10 @@
package main
import (
"log"
"sync"
"github.com/pkg/errors"
)
type Stick struct {
@ -43,7 +46,9 @@ func (s *Stick) loadNotebooks() {
stick.Notebooks = make(map[string]*Notebook)
for _, nbconfig := range stick.Config.Notebooks {
notebook, err := LoadNotebook(nbconfig.Repo)
CheckError(err)
if err != nil {
log.Fatal(errors.Wrapf(err, "failed to load %s", nbconfig.Repo))
}
notebook.ID = hash(nbconfig.Label)
notebook.Label = nbconfig.Label

43
web.go

@ -372,34 +372,31 @@ func sendNotesSince(ss *StickSocket, modified int64) {
response["author"] = author.Name
response["notebooks"] = map[string]*ServedNotebook{}
for _, notebook := range author.Notebooks {
r := notebook.Repository
config, _ := notebook.Repository.Worktree()
_ = config
snb := &ServedNotebook{Notebook: notebook, Notes: make(map[string]*Note)}
ref, err := r.Head()
CheckError(err)
ref, err := notebook.Repository.Head()
if err == nil { // Non-empty repository
commit, err := notebook.Repository.CommitObject(ref.Hash())
CheckError(err)
commit, err := r.CommitObject(ref.Hash())
CheckError(err)
tree, err := commit.Tree()
CheckError(err)
tree, err := commit.Tree()
CheckError(err)
err = tree.Files().ForEach(func(f *object.File) error {
noteID := hash(f.Name)
note := notebook.getNote(noteID)
if note == nil || note.ModifiedAt < modified {
return nil
}
if note.ModifiedAt > newModified {
newModified = note.ModifiedAt
}
snb := &ServedNotebook{Notebook: notebook, Notes: make(map[string]*Note)}
err = tree.Files().ForEach(func(f *object.File) error {
noteID := hash(f.Name)
note := notebook.getNote(noteID)
if note == nil || note.ModifiedAt < modified {
snb.Notes[noteID] = note
return nil
}
if note.ModifiedAt > newModified {
newModified = note.ModifiedAt
}
snb.Notes[noteID] = note
return nil
})
CheckError(err)
})
CheckError(err)
}
response["notebooks"].(map[string]*ServedNotebook)[snb.ID] = snb
}

Loading…
Cancel
Save