Skip to content

Commit fe901b8

Browse files
committed
hugolib, commands: Improve live-reload on directory structure changes
This issue is more visible now that we support nested sections. This commit makes operations like pasting new content folders or deleting content folders during server watch just work. Fixes #3570
1 parent b396893 commit fe901b8

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

‎commands/hugo.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,11 @@ func (c *commandeer) newWatcher(port int) error {
829829
if err := watcher.Add(path); err != nil {
830830
return err
831831
}
832+
} else if !c.isStatic(path) {
833+
// Hugo's rebuilding logic is entirely file based. When you drop a new folder into
834+
// /content on OSX, the above logic will handle future watching of those files,
835+
// but the initial CREATE is lost.
836+
dynamicEvents = append(dynamicEvents, fsnotify.Event{Name: path, Op: fsnotify.Create})
832837
}
833838
return nil
834839
}
@@ -841,9 +846,7 @@ func (c *commandeer) newWatcher(port int) error {
841846
}
842847
}
843848

844-
isstatic := strings.HasPrefix(ev.Name, c.PathSpec().GetStaticDirPath()) || (len(c.PathSpec().GetThemesDirPath()) > 0 && strings.HasPrefix(ev.Name, c.PathSpec().GetThemesDirPath()))
845-
846-
if isstatic {
849+
if c.isStatic(ev.Name) {
847850
staticEvents = append(staticEvents, ev)
848851
} else {
849852
dynamicEvents = append(dynamicEvents, ev)
@@ -999,6 +1002,10 @@ func (c *commandeer) newWatcher(port int) error {
9991002
return nil
10001003
}
10011004

1005+
func (c *commandeer) isStatic(path string) bool {
1006+
return strings.HasPrefix(path, c.PathSpec().GetStaticDirPath()) || (len(c.PathSpec().GetThemesDirPath()) > 0 && strings.HasPrefix(path, c.PathSpec().GetThemesDirPath()))
1007+
}
1008+
10021009
// isThemeVsHugoVersionMismatch returns whether the current Hugo version is
10031010
// less than the theme's min_version.
10041011
func (c *commandeer) isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) {

‎hugolib/page.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ func (ps Pages) FindPagePosByFilePath(inPath string) int {
322322
return -1
323323
}
324324

325+
func (ps Pages) FindPagePosByFilePathPrefix(prefix string) int {
326+
if prefix == "" {
327+
return -1
328+
}
329+
for i, x := range ps {
330+
if strings.HasPrefix(x.Source.Path(), prefix) {
331+
return i
332+
}
333+
}
334+
return -1
335+
}
336+
325337
// FindPagePos Given a page, it will find the position in Pages
326338
// will return -1 if not found
327339
func (ps Pages) FindPagePos(page *Page) int {

‎hugolib/page_collections.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ func (c *PageCollections) addPage(page *Page) {
137137
c.rawAllPages = append(c.rawAllPages, page)
138138
}
139139

140+
// When we get a REMOVE event we're not always getting all the individual files,
141+
// so we need to remove all below a given path.
142+
func (c *PageCollections) removePageByPathPrefix(path string) {
143+
for {
144+
i := c.rawAllPages.FindPagePosByFilePathPrefix(path)
145+
if i == -1 {
146+
break
147+
}
148+
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
149+
}
150+
}
151+
140152
func (c *PageCollections) removePageByPath(path string) {
141153
if i := c.rawAllPages.FindPagePosByFilePath(path); i >= 0 {
142154
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)

‎hugolib/site.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,11 @@ func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) {
667667
seen[ev] = true
668668

669669
if s.isContentDirEvent(ev) {
670-
logger.Println("Source changed", ev.Name)
670+
logger.Println("Source changed", ev)
671671
sourceChanged = append(sourceChanged, ev)
672672
}
673673
if s.isLayoutDirEvent(ev) {
674-
logger.Println("Template changed", ev.Name)
674+
logger.Println("Template changed", ev)
675675
tmplChanged = append(tmplChanged, ev)
676676

677677
if strings.Contains(ev.Name, "shortcodes") {
@@ -682,11 +682,11 @@ func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) {
682682
}
683683
}
684684
if s.isDataDirEvent(ev) {
685-
logger.Println("Data changed", ev.Name)
685+
logger.Println("Data changed", ev)
686686
dataChanged = append(dataChanged, ev)
687687
}
688688
if s.isI18nEvent(ev) {
689-
logger.Println("i18n changed", ev.Name)
689+
logger.Println("i18n changed", ev)
690690
i18nChanged = append(dataChanged, ev)
691691
}
692692
}
@@ -761,7 +761,7 @@ func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) {
761761
if ev.Op&fsnotify.Remove == fsnotify.Remove {
762762
//remove the file & a create will follow
763763
path, _ := helpers.GetRelativePath(ev.Name, s.getContentDir(ev.Name))
764-
s.removePageByPath(path)
764+
s.removePageByPathPrefix(path)
765765
continue
766766
}
767767

0 commit comments

Comments
 (0)