Skip to content

Commit d0a03c3

Browse files
fix: deletion of files in large repositories (#46)
Github api limits the nubmer of entries in file trees for post operations. A new mechanism of specifying a nil SHA can be used instead to delete files
1 parent ceed630 commit d0a03c3

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

githubfile/resource_file.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
120120
f := expandFile(d)
121121

122122
// Check whether the file exists.
123-
_, err := ghfileutils.GetFile(context.Background(), c.githubClient, f.repositoryOwner, f.repositoryName, f.branch, f.path)
123+
fileContent, err := ghfileutils.GetFile(context.Background(),
124+
c.githubClient,
125+
f.repositoryOwner,
126+
f.repositoryName,
127+
f.branch,
128+
f.path,
129+
)
124130
if err != nil {
125131
if err == ghfileutils.ErrNotFound {
126132
return nil
@@ -129,24 +135,21 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
129135
}
130136

131137
// Get the tree that corresponds to the target branch.
132-
s, err := branch.GetSHAForBranch(context.Background(), c.githubClient, f.repositoryOwner, f.repositoryName, f.branch)
133-
if err != nil {
134-
return err
135-
}
136-
oldTree, _, err := c.githubClient.Git.GetTree(context.Background(), f.repositoryOwner, f.repositoryName, s, true)
138+
s, err := branch.GetSHAForBranch(context.Background(),
139+
c.githubClient,
140+
f.repositoryOwner,
141+
f.repositoryName,
142+
f.branch)
137143
if err != nil {
138144
return err
139145
}
140146

141-
// Remove the target file from the list of entries for the new tree.
142-
// NOTE: Entries of type "tree" must be removed as well, otherwise deletion won't take place.
143-
newTree := make([]*github.TreeEntry, 0, len(oldTree.Entries))
144-
for _, entry := range oldTree.Entries {
145-
if *entry.Type != "tree" && *entry.Path != f.path {
146-
newTree = append(newTree, entry)
147-
}
148-
}
149-
147+
newTree := []*github.TreeEntry{{
148+
SHA: nil, // delete the file
149+
Path: fileContent.Path,
150+
Mode: github.String("100644"),
151+
Type: github.String("blob"),
152+
}}
150153
// Create a commit based on the new tree.
151154
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
152155
RepoOwner: f.repositoryOwner,
@@ -157,12 +160,13 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
157160
Username: c.githubUsername,
158161
Email: c.githubEmail,
159162
Changes: newTree,
160-
BaseTreeOverride: github.String(""),
163+
BaseTreeOverride: &s,
161164
PullRequestSourceBranchName: fmt.Sprintf("terraform-provider-githubfile-%d", time.Now().UnixNano()),
162165
PullRequestBody: "",
163166
MaxRetries: 3,
164167
RetryBackoff: 5 * time.Second,
165-
}); err != nil {
168+
},
169+
); err != nil {
166170
return fmt.Errorf("failed to create commit: %v", err)
167171
}
168172
return nil
@@ -179,7 +183,12 @@ func resourceFileRead(d *schema.ResourceData, m interface{}) error {
179183
c := m.(*providerConfiguration)
180184
f := expandFile(d)
181185

182-
h, err := ghfileutils.GetFile(context.Background(), c.githubClient, f.repositoryOwner, f.repositoryName, f.branch, f.path)
186+
h, err := ghfileutils.GetFile(context.Background(),
187+
c.githubClient,
188+
f.repositoryOwner,
189+
f.repositoryName,
190+
f.branch,
191+
f.path)
183192
if err == ghfileutils.ErrNotFound {
184193
d.SetId("")
185194
return nil

0 commit comments

Comments
 (0)