Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 11, 2024
1 parent 779e246 commit 51edc1b
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 72 deletions.
47 changes: 21 additions & 26 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,14 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) (pageC
return nil
}

_, n, replaced := s.pageMap.treePages.InsertIntoValuesDimensionWithLock(pi.Base(), ps)

if h.isRebuild() && replaced {
pt.BuildState.ChangedIdentities = append(pt.BuildState.ChangedIdentities, n.GetIdentity())
u, n, replaced := s.pageMap.treePages.InsertIntoValuesDimensionWithLock(pi.Base(), ps)

if h.isRebuild() {
if replaced {
pt.AddChange(n.GetIdentity())
} else {
pt.AddChange(u.GetIdentity())
}
}

return nil
Expand All @@ -313,7 +317,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) (pageC
_, n, replaced := s.pageMap.treeResources.InsertIntoValuesDimensionWithLock(rc.Path, rs)

if h.isRebuild() && replaced {
pt.BuildState.ChangedIdentities = append(pt.BuildState.ChangedIdentities, n.GetIdentity())
pt.AddChange(n.GetIdentity())
}
return nil
},
Expand All @@ -324,14 +328,20 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) (pageC

}

contentAdapter.BuildState.PrepareNextBuild()
handleBuildInfo := func(s *Site, bi pagesfromdata.BuildInfo) {
resourceCount += bi.NumResourcesAdded
pageCount += bi.NumPagesAdded
s.handleContentAdapterChanges(bi, whatChanged)
}

// TODO1 do we need a mutex?
if err := contentAdapter.Execute(context.Background()); err != nil {
bi, err := contentAdapter.Execute(context.Background())
if err != nil {
return err
}
handleBuildInfo(s, bi)

if !rebuild && contentAdapter.BuildState.EnableAllLanguages {
if !rebuild && bi.EnableAllLanguages {
// Clone and insert the adapter for the other sites.
for _, ss := range s.h.Sites {
if s == ss {
Expand All @@ -341,33 +351,18 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) (pageC
clone := contentAdapter.CloneForSite(ss)

// Make sure it gets executed for the first time.
if err := clone.Execute(context.Background()); err != nil {
bi, err := clone.Execute(context.Background())
if err != nil {
return err
}
handleBuildInfo(ss, bi)

// Insert into the correct language tree so it get rebuilt on changes.
ss.pageMap.treePagesFromTemplateOptions.Insert(pi.Base(), clone)

}
}

resourceCount += contentAdapter.BuildState.NumResourcesAdded
pageCount += contentAdapter.BuildState.NumPagesAdded

if m.s.h.isRebuild() {
if len(contentAdapter.BuildState.ChangedIdentities) > 0 {
whatChanged.Add(contentAdapter.BuildState.ChangedIdentities...)
}

for _, p := range contentAdapter.BuildState.DeletedPaths {
// TODO1 language, resource etc.
pp := path.Join(pi.Base(), p)
if v, ok := m.treePages.Delete(pp); ok {
whatChanged.Add(v.GetIdentity())
}

}
}
return nil
}(); err != nil {
addErr = err
Expand Down
2 changes: 1 addition & 1 deletion hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ func (h *HugoSites) resolveAndClearStateForIdentities(
for _, id := range changes {
checkedCount++
if r := depsFinder.Contains(id, n.DependencyManager, 2); r > identity.FinderNotFound {
n.BuildState.Rebuild = true
n.MarkStale()
matchCount++
break
}
Expand Down
7 changes: 5 additions & 2 deletions hugolib/doctree/treeshifttree.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ func (t *TreeShiftTree[T]) Get(s string) T {
return t.trees[t.v].Get(s)
}

func (t *TreeShiftTree[T]) GetAll(s string, f func(v T)) {
func (t *TreeShiftTree[T]) DeleteAllFunc(s string, f func(s string, v T) bool) {
for _, tt := range t.trees {
if v := tt.Get(s); v != t.zero {
f(v)
if f(s, v) {
// Delete.
tt.tree.Delete(s)
}
}
}
}
Expand Down
73 changes: 57 additions & 16 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
conf := &config
if conf.whatChanged == nil {
// Assume everything has changed
conf.whatChanged = &whatChanged{contentChanged: true}
conf.whatChanged = &whatChanged{needsPagesAssemble: true}
}

var prepareErr error
Expand Down Expand Up @@ -224,7 +224,7 @@ func (h *HugoSites) initRebuild(config *BuildCfg) error {
})

for _, s := range h.Sites {
s.resetBuildState(config.whatChanged.contentChanged)
s.resetBuildState(config.whatChanged.needsPagesAssemble)
}

h.reset(config)
Expand Down Expand Up @@ -252,7 +252,15 @@ func (h *HugoSites) assemble(ctx context.Context, l logg.LevelLogger, bcfg *Buil
l = l.WithField("step", "assemble")
defer loggers.TimeTrackf(l, time.Now(), nil, "")

if !bcfg.whatChanged.contentChanged {
if !bcfg.whatChanged.needsPagesAssemble {
changes := bcfg.whatChanged.Changes()

if len(changes) > 0 {
// TODO1 consolidate.
if err := h.resolveAndClearStateForIdentities(ctx, l, nil, changes); err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -626,10 +634,10 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
logger := h.Log

var (
tmplAdded bool
tmplChanged bool
i18nChanged bool
contentChanged bool
tmplAdded bool
tmplChanged bool
i18nChanged bool
needsPagesAssemble bool
)

changedPaths := struct {
Expand Down Expand Up @@ -709,13 +717,21 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
changes = append(changes, ids...)
}
} else {
h.pageTrees.treePagesFromTemplateOptions.GetAll(pathInfo.Base(),
func(n *pagesfromdata.PagesFromTemplate) {
h.pageTrees.treePagesFromTemplateOptions.DeleteAllFunc(pathInfo.Base(),
func(s string, n *pagesfromdata.PagesFromTemplate) bool {
changes = append(changes, n.DependencyManager)

// Try to open the file to see if has been deleted.
f, err := n.GoTmplFi.Meta().Open()
if err == nil {
f.Close()
}
// TODO1 also remove all pages and resources below this path.
return err != nil
})
}

contentChanged = true
needsPagesAssemble = true

if config.RecentlyVisited != nil {
// Fast render mode. Adding them to the visited queue
Expand Down Expand Up @@ -778,13 +794,15 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
case files.ComponentFolderAssets:
logger.Println("Asset changed", pathInfo.Path())
changes = append(changes, pathInfo)
needsPagesAssemble = true
case files.ComponentFolderData:
logger.Println("Data changed", pathInfo.Path())

// This should cover all usage of site.Data.
// Currently very coarse grained.
changes = append(changes, siteidentities.Data)
h.init.data.Reset()
needsPagesAssemble = true
case files.ComponentFolderI18n:
logger.Println("i18n changed", pathInfo.Path())
i18nChanged = true
Expand Down Expand Up @@ -868,8 +886,8 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
resourceFiles := h.fileEventsContentPaths(addedOrChangedContent)

changed := &whatChanged{
contentChanged: contentChanged,
identitySet: make(identity.Identities),
needsPagesAssemble: needsPagesAssemble,
identitySet: make(identity.Identities),
}
changed.Add(changes...)

Expand Down Expand Up @@ -928,7 +946,7 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
}

if h.isRebuild() {
if err := h.processContentAdaptersOnRebuild(ctx); err != nil {
if err := h.processContentAdaptersOnRebuild(ctx, config); err != nil {
return err
}
}
Expand All @@ -955,16 +973,39 @@ func (h *HugoSites) processFull(ctx context.Context, l logg.LevelLogger, config
return err
}

func (h *HugoSites) processContentAdaptersOnRebuild(ctx context.Context) error {
func (s *Site) handleContentAdapterChanges(bi pagesfromdata.BuildInfo, whatChanged *whatChanged) {
if !s.h.isRebuild() {
return
}

if len(bi.ChangedIdentities) > 0 {
whatChanged.Add(bi.ChangedIdentities...)
}

for _, p := range bi.DeletedPaths {
pp := path.Join(bi.Path.Base(), p)
if v, ok := s.pageMap.treePages.Delete(pp); ok {
whatChanged.Add(v.GetIdentity())
}
}
}

func (h *HugoSites) processContentAdaptersOnRebuild(ctx context.Context, buildConfig *BuildCfg) error {
g := rungroup.Run[*pagesfromdata.PagesFromTemplate](ctx, rungroup.Config[*pagesfromdata.PagesFromTemplate]{
NumWorkers: h.numWorkers,
Handle: func(ctx context.Context, p *pagesfromdata.PagesFromTemplate) error {
return p.Execute(ctx)
bi, err := p.Execute(ctx)
if err != nil {
return err
}
s := p.Site.(*Site)
s.handleContentAdapterChanges(bi, buildConfig.whatChanged)
return nil
},
})

h.pageTrees.treePagesFromTemplateOptions.WalkPrefixRaw(doctree.LockTypeRead, "", func(key string, p *pagesfromdata.PagesFromTemplate) (bool, error) {
if p.BuildState.Rebuild {
if p.StaleVersion() > 0 {
g.Enqueue(p)
}
return false, nil
Expand Down
Loading

0 comments on commit 51edc1b

Please sign in to comment.