Skip to content

Commit

Permalink
changes, install, pull: detect mode changing
Browse files Browse the repository at this point in the history
  • Loading branch information
lufia committed Feb 7, 2022
1 parent 930f278 commit 0001110
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
20 changes: 19 additions & 1 deletion changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,27 @@ func runChanges(r *Repository, args []string, w io.Writer) error {
if err != nil {
return err
}
if h != state.Hash {
modeDiff, err := isModeChanged(state.Target, state.Source)
if err != nil {
return err
}
if modeDiff || h != state.Hash {
fmt.Fprintln(w, state.Target)
}
return nil
})
}

func isModeChanged(s1, s2 string) (bool, error) {
f1, err := os.Stat(s1)
if err != nil {
return false, err
}
f2, err := os.Stat(s2)
if err != nil {
return false, err
}
m1 := f1.Mode() & os.ModePerm
m2 := f2.Mode() & os.ModePerm
return m1 != m2, nil
}
6 changes: 5 additions & 1 deletion changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func TestRunChanges(t *testing.T) {
},
{
file: "testdata/changes/modified.txtar",
want: "~/out/.exrc\n~/out/lib/profile\n",
want: strings.Join([]string{
"~/out/.exrc",
"~/out/lib/newstime",
"~/out/lib/profile",
}, "\n") + "\n",
},
}
for i, tt := range tests {
Expand Down
6 changes: 5 additions & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (r *Repository) CopyFile(dest, p string, overwrite bool) error {
if err != nil {
return err
}
fout, err := os.OpenFile(dest, flags, fi.Mode()&os.ModePerm)
mode := fi.Mode() & os.ModePerm
fout, err := os.OpenFile(dest, flags, mode)
if err != nil {
return err
}
Expand All @@ -70,6 +71,9 @@ func (r *Repository) CopyFile(dest, p string, overwrite bool) error {
if err := fout.Sync(); err != nil {
return err
}
if err := os.Chmod(dest, mode); err != nil {
return err
}
s := fmt.Sprintf("%x %s\n", h.Sum(nil), dest)
file := r.StateFile(slug)
return writeFile(file, []byte(s), 0644)
Expand Down
7 changes: 5 additions & 2 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func runPull(r *Repository, args []string, w io.Writer) error {
if err := f.Parse(args); err != nil {
return err
}
_ = *dryRun
dir := filepath.Join(r.StateDir, "store")
err := filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
if err != nil {
Expand All @@ -33,7 +32,11 @@ func runPull(r *Repository, args []string, w io.Writer) error {
if err != nil {
return err
}
if h == state.Hash {
modeDiff, err := isModeChanged(state.Target, state.Source)
if err != nil {
return err
}
if !modeDiff && h == state.Hash {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestRunPull(t *testing.T) {
}{
{
file: "testdata/pull/updated.txtar",
slugs: []string{".exrc", "lib/profile"},
slugs: []string{".exrc", "lib/profile", "bin/ct"},
},
}
for i, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions testdata/changes/modified.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
set ts=4
-- dotfiles/lib/profile --
font=/mnt/font/GoMono/14a/font
-- dotfiles/lib/newstime --
-- out/.exrc --
set ts=8
-- out/lib/profile --
font=/mnt/font/GoMono/22a/font
-- out/lib/newstime!755 --
-- .local/state/dotsync/store/.exrc --
7970153163bfaaba0780d262461fb7b692380f6d249da3aef109f93ba2a7d643 $TEST_DIR/out/.exrc
-- .local/state/dotsync/store/lib/profile --
8218f9d74a96ef2e70a2ffb069418dacce41e00aed892ea8a8968272ea03ad9e $TEST_DIR/out/lib/profile
-- .local/state/dotsync/store/lib/newstime --
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 $TEST_DIR/out/lib/newstime
8 changes: 8 additions & 0 deletions testdata/pull/updated.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
set ts=8
-- dotfiles/lib/profile --
font=/mnt/font/GoMono/22a/font
-- dotfiles/bin/ct!755 --
echo hello
-- out/.exrc --
set ts=4
-- out/lib/profile --
font=/mnt/font/GoMono/14a/font
-- out/bin/ct --
echo hello
-- out/.exrc.golden --
set ts=8
-- out/lib/profile.golden --
font=/mnt/font/GoMono/22a/font
-- out/bin/ct.golden!755 --
echo hello
-- .local/state/dotsync/store/.exrc --
7970153163bfaaba0780d262461fb7b692380f6d249da3aef109f93ba2a7d643 $TEST_DIR/out/.exrc
-- .local/state/dotsync/store/lib/profile --
8218f9d74a96ef2e70a2ffb069418dacce41e00aed892ea8a8968272ea03ad9e $TEST_DIR/out/lib/profile
-- .local/state/dotsync/store/bin/ct --
5dbad7dd0b9b122dcd9956884390f4aac4738caba8ff53498a7ab6718b176c30 $TEST_DIR/out/bin/ct

0 comments on commit 0001110

Please sign in to comment.