WIP syncinclude

This commit is contained in:
Shreyas Goenka 2022-09-23 12:10:46 +02:00
parent 6bcb33bf07
commit c979694224
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
5 changed files with 37 additions and 7 deletions

View File

@ -93,6 +93,9 @@ func (d diff) String() string {
}
func (s snapshot) diff(all []git.File) (change diff) {
fmt.Println("[INFO] all files: ", all)
currentFilenames := map[string]bool{}
for _, f := range all {
// create set of current files to figure out if removals are needed

View File

@ -22,7 +22,7 @@ func TestDiff(t *testing.T) {
assert.NoError(t, err)
defer f2.Close()
fileSet := git.NewFileSet(projectDir)
fileSet := git.NewFileSet(projectDir, nil)
files, err := fileSet.All()
assert.NoError(t, err)
state := snapshot{}

View File

@ -22,6 +22,8 @@ var syncCmd = &cobra.Command{
prj := project.Get(ctx)
wsc := prj.WorkspacesClient()
fmt.Println("[INFO] sync string array: ", *syncInclude)
if *remotePath == "" {
me, err := prj.Me()
if err != nil {
@ -44,7 +46,7 @@ var syncCmd = &cobra.Command{
}
root := prj.Root()
fileSet := git.NewFileSet(root)
fileSet := git.NewFileSet(root, syncInclude)
if err != nil {
return err
}
@ -61,9 +63,12 @@ var remotePath *string
var persistSnapshot *bool
var syncInclude *[]string
func init() {
root.RootCmd.AddCommand(syncCmd)
interval = syncCmd.Flags().Duration("interval", 1*time.Second, "project files polling interval")
remotePath = syncCmd.Flags().String("remote-path", "", "remote path to store repo in. eg: /Repos/me@example.com/test-repo")
persistSnapshot = syncCmd.Flags().Bool("persist-snapshot", true, "whether to store local snapshots of sync state")
syncInclude = syncCmd.Flags().StringArray("sync-include", []string{}, "list of regex patterns for which files to sync. Overrides .gitignore")
}

View File

@ -34,17 +34,25 @@ func (f File) Modified() (ts time.Time) {
type FileSet struct {
root string
ignore *ignore.GitIgnore
// if include is not nil, it will override ignore
include *ignore.GitIgnore
}
// GetFileSet retrieves FileSet from Git repository checkout root
// or panics if no root is detected.
func GetFileSet() (FileSet, error) {
root, err := Root()
return NewFileSet(root), err
return NewFileSet(root, nil), err
}
// Retuns FileSet for the repository located at `root`
func NewFileSet(root string) FileSet {
func NewFileSet(root string, syncInclude *[]string) FileSet {
if syncInclude != nil {
return FileSet{
root: root,
include: ignore.CompileIgnoreLines(*syncInclude...),
}
}
lines := []string{".git", ".bricks"}
rawIgnore, err := os.ReadFile(fmt.Sprintf("%s/.gitignore", root))
if err == nil {
@ -82,9 +90,23 @@ func (w *FileSet) RecursiveListFiles(dir string) (fileList []File, err error) {
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
if w.ignore.MatchesPath(current.Relative) {
// If w.include is empty and the current file matches one of the
// patterns in w.ignore then we do not list the file
if w.include == nil && w.ignore.MatchesPath(current.Relative) {
continue
}
// if w.include is not empty and current file does not match any
// patterns in w.include then we do not list the file
if w.include != nil && !w.include.MatchesPath(current.Relative) {
continue
}
// we list the file if one of the following holds true
// 1. w.include is empty and current file does not match any of the
// patterns in w.ignore
// 2. w.include is not empty and current file matches any one of the
// patters in w.include
if !current.IsDir() {
fileList = append(fileList, current)
continue

View File

@ -26,7 +26,7 @@ func TestRecusiveListFile(t *testing.T) {
// config file is returned
// .gitignore is not because we explictly ignore it in .gitignore
fileSet := NewFileSet(projectDir)
fileSet := NewFileSet(projectDir, nil)
files, err := fileSet.RecursiveListFiles(projectDir)
assert.NoError(t, err)
assert.Len(t, files, 1)
@ -63,7 +63,7 @@ func TestFileSetNonCleanRoot(t *testing.T) {
// Path simplification is done by most filepath functions.
root := "./../git"
require.NotEqual(t, root, filepath.Clean(root))
fs := NewFileSet(root)
fs := NewFileSet(root, nil)
files, err := fs.All()
require.NoError(t, err)