Skip to content

Commit a90988d

Browse files
authored
Update repo's default branch when adding new files in an empty one (#25017)
Fix #25014 Only API needs this fix. On the Web UI, users could only add new file on the default branch.
1 parent a5acec3 commit a90988d

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ cpu.out
4747

4848
*.db
4949
*.log
50+
*.log.*.gz
5051

5152
/gitea
5253
/gitea-vet

services/repository/files/update.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type ChangeRepoFile struct {
5050
Options *RepoFileOptions
5151
}
5252

53-
// UpdateRepoFilesOptions holds the repository files update options
53+
// ChangeRepoFilesOptions holds the repository files update options
5454
type ChangeRepoFilesOptions struct {
5555
LastCommitID string
5656
OldBranch string
@@ -159,7 +159,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
159159
return nil, err
160160
}
161161

162-
treePaths := []string{}
162+
var treePaths []string
163163
for _, file := range opts.Files {
164164
// If FromTreePath is not set, set it to the opts.TreePath
165165
if file.TreePath != "" && file.FromTreePath == "" {
@@ -302,7 +302,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
302302
return nil, err
303303
}
304304
default:
305-
return nil, fmt.Errorf("Invalid file operation: %s %s, supported operations are create, update, delete", file.Operation, file.Options.treePath)
305+
return nil, fmt.Errorf("invalid file operation: %s %s, supported operations are create, update, delete", file.Operation, file.Options.treePath)
306306
}
307307
}
308308

@@ -334,16 +334,16 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
334334
return nil, err
335335
}
336336

337-
filesReponse, err := GetFilesResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePaths)
337+
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePaths)
338338
if err != nil {
339339
return nil, err
340340
}
341341

342342
if repo.IsEmpty {
343-
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false}, "is_empty")
343+
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
344344
}
345345

346-
return filesReponse, nil
346+
return filesResponse, nil
347347
}
348348

349349
// handles the check for various issues for ChangeRepoFiles
@@ -437,7 +437,7 @@ func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRep
437437
return nil
438438
}
439439

440-
// handle creating or updating a file for ChangeRepoFiles
440+
// CreateOrUpdateFile handles creating or updating a file for ChangeRepoFiles
441441
func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file *ChangeRepoFile, contentStore *lfs.ContentStore, repoID int64, hasOldBranch bool) error {
442442
// Get the two paths (might be the same if not moving) from the index if they exist
443443
filesInIndex, err := t.LsFiles(file.TreePath, file.FromTreePath)
@@ -540,7 +540,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
540540
if !exist {
541541
if err := contentStore.Put(lfsMetaObject.Pointer, strings.NewReader(file.Content)); err != nil {
542542
if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repoID, lfsMetaObject.Oid); err2 != nil {
543-
return fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)
543+
return fmt.Errorf("unable to remove failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)
544544
}
545545
return err
546546
}

tests/integration/empty_repo_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ func TestEmptyRepoAddFileByAPI(t *testing.T) {
137137
req = NewRequest(t, "GET", "/user30/empty/src/branch/new_branch/new-file.txt")
138138
resp = session.MakeRequest(t, req, http.StatusOK)
139139
assert.Contains(t, resp.Body.String(), "newly-added-api-file")
140+
141+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/user30/empty?token=%s", token))
142+
resp = session.MakeRequest(t, req, http.StatusOK)
143+
var apiRepo api.Repository
144+
DecodeJSON(t, resp, &apiRepo)
145+
assert.Equal(t, "new_branch", apiRepo.DefaultBranch)
140146
}

0 commit comments

Comments
 (0)