Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt repositories #12920

Merged
merged 29 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0cc1bda
Don't automatically delete repository files if they are present
zeripath Aug 2, 2020
2fd0292
Update swagger
zeripath Aug 3, 2020
bd1d182
Fix tests and migrate overwrite
zeripath Aug 3, 2020
5075257
as per @lunny
zeripath Aug 4, 2020
e8fa2e6
Merge remote-tracking branch 'origin/master' into adopt-repositories
zeripath Sep 6, 2020
b06c510
Use setting.Repository.DefaultBranch for the default branch
zeripath Sep 6, 2020
d885a69
Always set setting.Repository.DefaultBranch
zeripath Sep 6, 2020
45a6873
Merge remote-tracking branch 'origin/master' into adopt-repositories
zeripath Sep 17, 2020
608031d
update swagger
zeripath Sep 17, 2020
ca482b7
update templates
zeripath Sep 17, 2020
4c143ff
Merge branch 'master' into adopt-repositories
zeripath Sep 17, 2020
ee66a29
ensure repo closed
zeripath Sep 18, 2020
d74f3c9
Merge remote-tracking branch 'origin/master' into adopt-repositories
zeripath Sep 19, 2020
c18ba9b
Rewrite of adoption as per @6543 and @lunny
zeripath Sep 19, 2020
f535fea
Merge remote-tracking branch 'origin/master' into adopt-repositories-2
zeripath Sep 21, 2020
0dc0824
Apply suggestions from code review
zeripath Sep 21, 2020
d4a7c0c
update swagger
zeripath Sep 22, 2020
08a333f
missing not
zeripath Sep 23, 2020
d0d7ff8
add modals and flash reporting
zeripath Sep 23, 2020
81bf964
Merge remote-tracking branch 'origin/master' into adopt-repositories-2
zeripath Sep 23, 2020
1911965
Make the unadopted page searchable
zeripath Sep 23, 2020
b96ba62
Add API
zeripath Sep 23, 2020
971585b
Fix swagger
zeripath Sep 23, 2020
89e59ce
fix swagger
zeripath Sep 24, 2020
56b6e16
Merge remote-tracking branch 'origin/master' into adopt-repositories-2
zeripath Sep 24, 2020
8c7f896
Handle empty and non-master branched repositories
zeripath Sep 24, 2020
4a65d77
placate lint
zeripath Sep 24, 2020
05030b1
remove commented out code
zeripath Sep 24, 2020
d1a4365
Merge branch 'master' into adopt-repositories-2
techknowlogick Sep 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ PREFIX_ARCHIVE_FILES = true
DISABLE_MIRRORS = false
; The default branch name of new repositories
DEFAULT_BRANCH=master
; Allow adoption of unadopted repositories
ALLOW_ADOPTION_OF_UNADOPTED_REPOSITORIES=false
; Allow deletion of unadopted repositories
ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES=false

[repository.editor]
; List of file extensions for which lines should be wrapped in the Monaco editor
Expand Down
16 changes: 16 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,22 @@ func (err ErrRepoAlreadyExist) Error() string {
return fmt.Sprintf("repository already exists [uname: %s, name: %s]", err.Uname, err.Name)
}

// ErrRepoFilesAlreadyExist represents a "RepoFilesAlreadyExist" kind of error.
type ErrRepoFilesAlreadyExist struct {
Uname string
Name string
}

// IsErrRepoFilesAlreadyExist checks if an error is a ErrRepoAlreadyExist.
func IsErrRepoFilesAlreadyExist(err error) bool {
_, ok := err.(ErrRepoFilesAlreadyExist)
return ok
}

func (err ErrRepoFilesAlreadyExist) Error() string {
return fmt.Sprintf("repository files already exist [uname: %s, name: %s]", err.Uname, err.Name)
}

// ErrForkAlreadyExist represents a "ForkAlreadyExist" kind of error.
type ErrForkAlreadyExist struct {
Uname string
Expand Down
27 changes: 24 additions & 3 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (repo *Repository) IsBeingCreated() bool {
func (repo *Repository) AfterLoad() {
// FIXME: use models migration to solve all at once.
if len(repo.DefaultBranch) == 0 {
repo.DefaultBranch = "master"
repo.DefaultBranch = setting.Repository.DefaultBranch
}

repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func (repo *Repository) CloneLink() (cl *CloneLink) {
}

// CheckCreateRepository check if could created a repository
func CheckCreateRepository(doer, u *User, name string) error {
func CheckCreateRepository(doer, u *User, name string, overwriteOrAdopt bool) error {
if !doer.CanCreateRepo() {
return ErrReachLimitOfRepo{u.MaxRepoCreation}
}
Expand All @@ -1063,6 +1063,10 @@ func CheckCreateRepository(doer, u *User, name string) error {
} else if has {
return ErrRepoAlreadyExist{u.Name, name}
}

if !overwriteOrAdopt && com.IsExist(RepoPath(u.Name, name)) {
return ErrRepoFilesAlreadyExist{u.Name, name}
}
return nil
}

Expand Down Expand Up @@ -1116,11 +1120,15 @@ var (

// IsUsableRepoName returns true when repository is usable
func IsUsableRepoName(name string) error {
if alphaDashDotPattern.MatchString(name) {
// Note: usually this error is normally caught up earlier in the UI
return ErrNameCharsNotAllowed{Name: name}
}
return isUsableName(reservedRepoNames, reservedRepoPatterns, name)
}

// CreateRepository creates a repository for the user/organization.
func CreateRepository(ctx DBContext, doer, u *User, repo *Repository) (err error) {
func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteOrAdopt bool) (err error) {
if err = IsUsableRepoName(repo.Name); err != nil {
return err
}
Expand All @@ -1132,6 +1140,15 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository) (err error
return ErrRepoAlreadyExist{u.Name, repo.Name}
}

repoPath := RepoPath(u.Name, repo.Name)
if !overwriteOrAdopt && com.IsExist(repoPath) {
log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
return ErrRepoFilesAlreadyExist{
Uname: u.Name,
Name: repo.Name,
}
}

if _, err = ctx.e.Insert(repo); err != nil {
return err
}
Expand Down Expand Up @@ -1856,6 +1873,10 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error)
cond = cond.And(builder.Eq{"is_private": false})
}

if opts.LowerNames != nil && len(opts.LowerNames) > 0 {
cond = cond.And(builder.In("lower_name", opts.LowerNames))
}

sess := x.NewSession()
defer sess.Close()

Expand Down
2 changes: 2 additions & 0 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type SearchRepoOptions struct {
// True -> include just has milestones
// False -> include just has no milestone
HasMilestones util.OptionalBool
// LowerNames represents valid lower names to restrict to
LowerNames []string
}

//SearchOrderBy is used to sort the result
Expand Down
4 changes: 2 additions & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ func (u *User) GetOrganizationCount() (int64, error) {
}

// GetRepositories returns repositories that user owns, including private repositories.
func (u *User) GetRepositories(listOpts ListOptions) (err error) {
u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts})
func (u *User) GetRepositories(listOpts ListOptions, names ...string) (err error) {
u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts, LowerNames: names})
return err
}

Expand Down
5 changes: 5 additions & 0 deletions modules/git/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (repo *Repository) SetDefaultBranch(name string) error {
return err
}

// GetDefaultBranch gets default branch of repository.
func (repo *Repository) GetDefaultBranch() (string, error) {
return NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
}

// GetBranches returns all branches of the repository.
func (repo *Repository) GetBranches() ([]string, error) {
var branchNames []string
Expand Down
Loading