Skip to content

Commit

Permalink
fix: deletion of files in large repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-tamashiro-form3 committed Jan 11, 2024
1 parent 81d2f97 commit e34e96f
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions codeowners/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
file := expandFile(d)

// Check whether the file exists.
_, err := githubfileutils.GetFile(context.Background(), config.client, file.RepositoryOwner, file.RepositoryName, file.Branch, codeownersPath)
fileContent, err := githubfileutils.GetFile(context.Background(), config.client, file.RepositoryOwner, file.RepositoryName, file.Branch, codeownersPath)
if err != nil {
if err == githubfileutils.ErrNotFound {
return nil
Expand All @@ -243,19 +243,15 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
oldTree, _, err := config.client.Git.GetTree(context.Background(), file.RepositoryOwner, file.RepositoryName, s, true)
if err != nil {
return err
}

// Remove the target file from the list of entries for the new tree.
// NOTE: Entries of type "tree" must be removed as well, otherwise deletion won't take place.
newTree := make([]*github.TreeEntry, 0, len(oldTree.Entries))
for _, entry := range oldTree.Entries {
if *entry.Type != "tree" && *entry.Path != codeownersPath {
newTree = append(newTree, entry)
}
}
newTree := []*github.TreeEntry{{
SHA: nil, // delete the file
Path: fileContent.Path,
Mode: github.String("100644"),
Type: github.String("blob"),
}}

// Create a commit based on the new tree.
if err := githubcommitutils.CreateCommit(context.Background(), config.client, &githubcommitutils.CommitOptions{
Expand All @@ -265,7 +261,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
GpgPassphrase: config.gpgPassphrase,
GpgPrivateKey: config.gpgKey,
Changes: newTree,
BaseTreeOverride: github.String(""),
BaseTreeOverride: &s,
Branch: file.Branch,
Username: config.ghUsername,
Email: config.ghEmail,
Expand Down

0 comments on commit e34e96f

Please sign in to comment.