Skip to content

Commit

Permalink
internal/vcs: only consider actual sub-directory for status checks
Browse files Browse the repository at this point in the history
Files outside of the module directory can be ignored
for the purposes of cleanliness checking.

Fixes #3110

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ifeff4ee85d80751f292390ef1cd1c63e21dcac9e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194494
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed May 9, 2024
1 parent d5802f6 commit cf3cf2f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/cue/cmd/testdata/script/registry_publish_with_git.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ cd $WORK/example
exec git init .
exec git add .
exec git -c user.name=noone -c [email protected] commit -m 'initial commit'

# Remove an file that's outside the module directory, which
# makes the git repository unclean but the module subdirectory
# remains clean.
rm $WORK/example/otherfile

cd cuemodule
exec cue mod publish v0.0.1
stdout '^published x.example/[email protected] to [^ ]+/x.example/e:v0.0.1@sha256:[0-9a-f]+$'
Expand Down Expand Up @@ -90,3 +96,6 @@ e: true
package e

sensitive: true
-- example/otherfile --
this will be removed but is outside the module so
that shouldn't stop the publish working.
14 changes: 11 additions & 3 deletions internal/vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

type gitVCS struct {
root string
root string
subDir string
}

func newGitVCS(dir string) (VCS, error) {
Expand All @@ -37,7 +38,8 @@ func newGitVCS(dir string) (VCS, error) {
}
}
return gitVCS{
root: root,
root: root,
subDir: dir,
}, nil
}

Expand All @@ -64,7 +66,13 @@ func (v gitVCS) ListFiles(ctx context.Context, dir string) ([]string, error) {

// Status implements [VCS.Status].
func (v gitVCS) Status(ctx context.Context) (Status, error) {
out, err := runCmd(ctx, v.root, "git", "status", "--porcelain")
// We only care about the module's subdirectory status - if anything
// else is dirty, it won't go into the module so we don't care.
// TODO this will change if/when we include license files
// from outside the module directory. It also over-reports dirtiness
// because there might be nested modules that aren't included, but
// are nonetheless included in the status check.
out, err := runCmd(ctx, v.root, "git", "status", "--porcelain", v.subDir)
if err != nil {
return Status{}, err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var vcsTypes = map[string]func(dir string) (VCS, error){
// version control system of the given type that
// controls the given directory.
//
// Status checks apply only to the given directory; other
// directories controlled by the VCS will not be considered.
//
// It returns an error if a VCS of the specified type
// cannot be found.
func New(vcsType string, dir string) (VCS, error) {
Expand Down
11 changes: 11 additions & 0 deletions internal/vcs/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func TestGit(t *testing.T) {
mustRunCmd(t, dir, "git", "init")
v, err := New("git", filepath.Join(dir, "subdir"))
qt.Assert(t, qt.IsNil(err))

// The status shows that we have uncommitted files
// because we haven't yet added the files after doing
// git init.
status, err := v.Status(ctx)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.IsTrue(status.Uncommitted))
Expand Down Expand Up @@ -83,6 +87,13 @@ func TestGit(t *testing.T) {
"subdir/bar/baz",
"subdir/foo",
}))

// Change a file that's not in subdir. The status should remain the same.
err = os.WriteFile(filepath.Join(dir, "bar.txt"), []byte("something else"), 0o666)
qt.Assert(t, qt.IsNil(err))
status1, err := v.Status(ctx)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.DeepEquals(status1, status))
}

// initTestEnv sets up the environment so that
Expand Down

0 comments on commit cf3cf2f

Please sign in to comment.