Resolve issues using empty notebooks

This commit is contained in:
Trevor Slocum 2019-04-18 04:37:56 -07:00
parent 7a796ba0ea
commit 5d39cbf04f
8 changed files with 82 additions and 58 deletions

1
.gitignore vendored
View File

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

View File

@ -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
View File

@ -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()

View File

@ -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 Normal file
View File

@ -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 Normal file
View File

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

View File

@ -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

49
web.go
View File

@ -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
ref, err := r.Head()
CheckError(err)
commit, err := r.CommitObject(ref.Hash())
CheckError(err)
tree, err := commit.Tree()
CheckError(err)
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 {
return nil
}
if note.ModifiedAt > newModified {
newModified = note.ModifiedAt
}
snb.Notes[noteID] = note
return nil
})
CheckError(err)
ref, err := notebook.Repository.Head()
if err == nil { // Non-empty repository
commit, err := notebook.Repository.CommitObject(ref.Hash())
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.Notes[noteID] = note
return nil
})
CheckError(err)
}
response["notebooks"].(map[string]*ServedNotebook)[snb.ID] = snb
}