Skip to content

Commit

Permalink
feat: add the option "-D" to delete non existent files
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Sep 14, 2024
1 parent d731e32 commit b8b42c6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck7
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI=
github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064 h1:RCQBSFx5JrsbHltqTtJ+kN3U0Y3a/N/GlVdmRSoxzyE=
Expand All @@ -37,6 +39,7 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -88,6 +91,7 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (r *Runner) newCommitCmd(ctx context.Context, gOpts *globalOptions) *cobra.
DeletedPaths: o.DeletedPaths,
NoFileMode: o.NoFileMode,
DryRun: o.DryRun,
Delete: o.Delete,
}
if err := ir.CommitUseCase.Do(ctx, in); err != nil {
ir.Logger.Debugf("Stacktrace:\n%+v", err)
Expand All @@ -101,6 +102,7 @@ type commitOptions struct {
DryRun bool
DeletedPaths []string
DeletedFile string
Delete bool
}

func (o commitOptions) validate() error {
Expand Down Expand Up @@ -130,6 +132,7 @@ func (o *commitOptions) register(f *pflag.FlagSet) {
f.BoolVar(&o.NoParent, "no-parent", false, "Create a commit without a parent")
f.BoolVar(&o.NoFileMode, "no-file-mode", false, "Ignore executable bit of file and treat as 0644")
f.BoolVar(&o.DryRun, "dry-run", false, "Upload files but do not update the branch actually")
f.BoolVarP(&o.Delete, "delete-not-found", "D", false, "Delete non existent files")
f.StringSliceVarP(&o.DeletedPaths, "delete", "d", nil, "A list of deleted file paths")
f.StringVar(&o.DeletedFile, "delete-file", "", "A file including deleted file paths")
o.commitAttributeOptions.register(f)
Expand Down
25 changes: 23 additions & 2 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"encoding/base64"
"errors"
"fmt"
"io"
"os"
Expand All @@ -21,6 +22,7 @@ var Set = wire.NewSet(
type Interface interface {
FindFiles(paths []string, filter FindFilesFilter) ([]File, error)
ReadAsBase64EncodedContent(filename string) (string, error)
SetDelete(delete bool)
}

// FindFilesFilter is an interface to filter directories and files.
Expand All @@ -37,10 +39,17 @@ func (*nullFindFilesFilter) ExcludeFile(string) bool { return false }
type File struct {
Path string
Executable bool
Deleted bool
}

// FileSystem provides manipulation of file system.
type FileSystem struct{}
type FileSystem struct {
Delete bool
}

func (fs *FileSystem) SetDelete(delete bool) {
fs.Delete = delete
}

// FindFiles returns a list of files in the paths.
// If the filter is nil, it returns any files.
Expand All @@ -50,6 +59,18 @@ func (fs *FileSystem) FindFiles(paths []string, filter FindFilesFilter) ([]File,
}
var files []File
for _, path := range paths {
if fs.Delete {
if _, err := os.Stat(path); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("error while checking if the file exists: %w", err)
}
files = append(files, File{
Path: path,
Deleted: true,
})
continue
}
}
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error while walk: %w", err)
Expand All @@ -66,7 +87,7 @@ func (fs *FileSystem) FindFiles(paths []string, filter FindFilesFilter) ([]File,
}
files = append(files, File{
Path: path,
Executable: info.Mode()&0100 != 0, // mask the executable bit of owner
Executable: info.Mode()&0o100 != 0, // mask the executable bit of owner
})
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/fs/mock_fs/mock_fs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions pkg/usecases/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Input struct {
NoFileMode bool
DryRun bool

ForceUpdate bool //TODO: support force-update as well
ForceUpdate bool // TODO: support force-update as well
Delete bool
}

// Commit commits files to the default/given branch on the repository.
Expand All @@ -60,6 +61,8 @@ func (u *Commit) Do(ctx context.Context, in Input) error {
return errors.New("you must set commit message")
}

u.FileSystem.SetDelete(in.Delete)

files, err := u.FileSystem.FindFiles(in.Paths, &pathFilter{Logger: u.Logger})
if err != nil {
return fmt.Errorf("could not find files: %w", err)
Expand All @@ -68,6 +71,18 @@ func (u *Commit) Do(ctx context.Context, in Input) error {
return errors.New("no file exists in given paths")
}

if in.Delete {
arr := make([]fs.File, 0, len(files))
for _, file := range files {
if file.Deleted {
in.DeletedPaths = append(in.DeletedPaths, file.Path)
continue
}
arr = append(arr, file)
}
files = arr
}

if in.TargetBranchName == "" {
q, err := u.GitHub.QueryDefaultBranch(ctx, github.QueryDefaultBranchInput{
HeadRepository: in.TargetRepository,
Expand Down Expand Up @@ -143,7 +158,7 @@ func (u *Commit) createNewBranch(ctx context.Context, in Input, files []fs.File,
return fmt.Errorf("unknown commit strategy %+v", in.CommitStrategy)
}

u.Logger.Debugf("Creating a commit with the %d file(s)", len(gitObj.Files))
u.Logger.Debugf("Creating a commit with the %d file(s)", len(gitObj.Files)+len(gitObj.DeletedFiles))
commit, err := u.CreateGitObject.Do(ctx, gitObj)
if err != nil {
return fmt.Errorf("error while creating a commit: %w", err)
Expand Down

0 comments on commit b8b42c6

Please sign in to comment.