@ -16,15 +16,15 @@ import (
)
)
// ForkRepository forks a repository
// ForkRepository forks a repository
func ForkRepository ( doer , owner * models . User , oldRepo * models . Repository , name , desc string ) ( _ * models . Repository , err error ) {
func ForkRepository ( doer , owner * models . User , opts models . ForkRepoOptions ) ( _ * models . Repository , err error ) {
forkedRepo , err := old Repo . GetUserFork ( owner . ID )
forkedRepo , err := opts . Base Repo . GetUserFork ( owner . ID )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
if forkedRepo != nil {
if forkedRepo != nil {
return nil , models . ErrForkAlreadyExist {
return nil , models . ErrForkAlreadyExist {
Uname : owner . Name ,
Uname : owner . Name ,
RepoName : old Repo . FullName ( ) ,
RepoName : opts . Base Repo . FullName ( ) ,
ForkName : forkedRepo . FullName ( ) ,
ForkName : forkedRepo . FullName ( ) ,
}
}
}
}
@ -33,17 +33,17 @@ func ForkRepository(doer, owner *models.User, oldRepo *models.Repository, name,
OwnerID : owner . ID ,
OwnerID : owner . ID ,
Owner : owner ,
Owner : owner ,
OwnerName : owner . Name ,
OwnerName : owner . Name ,
Name : n ame,
Name : opts . N ame,
LowerName : strings . ToLower ( n ame) ,
LowerName : strings . ToLower ( opts . N ame) ,
Description : desc ,
Description : opts . Description ,
DefaultBranch : old Repo . DefaultBranch ,
DefaultBranch : opts . Base Repo . DefaultBranch ,
IsPrivate : old Repo . IsPrivate || old Repo . Owner . Visibility == structs . VisibleTypePrivate ,
IsPrivate : opts . Base Repo . IsPrivate || opts . Base Repo . Owner . Visibility == structs . VisibleTypePrivate ,
IsEmpty : old Repo . IsEmpty ,
IsEmpty : opts . Base Repo . IsEmpty ,
IsFork : true ,
IsFork : true ,
ForkID : old Repo . ID ,
ForkID : opts . Base Repo . ID ,
}
}
oldRepoPath := old Repo . RepoPath ( )
oldRepoPath := opts . Base Repo . RepoPath ( )
err = models . WithTx ( func ( ctx models . DBContext ) error {
err = models . WithTx ( func ( ctx models . DBContext ) error {
if err = models . CreateRepository ( ctx , doer , owner , repo , false ) ; err != nil {
if err = models . CreateRepository ( ctx , doer , owner , repo , false ) ; err != nil {
@ -59,13 +59,13 @@ func ForkRepository(doer, owner *models.User, oldRepo *models.Repository, name,
}
}
}
}
if err = models . IncrementRepoForkNum ( ctx , old Repo . ID ) ; err != nil {
if err = models . IncrementRepoForkNum ( ctx , opts . Base Repo . ID ) ; err != nil {
rollbackRemoveFn ( )
rollbackRemoveFn ( )
return err
return err
}
}
// copy lfs files failure should not be ignored
// copy lfs files failure should not be ignored
if err := models . CopyLFS ( ctx , repo , old Repo ) ; err != nil {
if err := models . CopyLFS ( ctx , repo , opts . Base Repo ) ; err != nil {
rollbackRemoveFn ( )
rollbackRemoveFn ( )
return err
return err
}
}
@ -73,9 +73,9 @@ func ForkRepository(doer, owner *models.User, oldRepo *models.Repository, name,
repoPath := models . RepoPath ( owner . Name , repo . Name )
repoPath := models . RepoPath ( owner . Name , repo . Name )
if stdout , err := git . NewCommand (
if stdout , err := git . NewCommand (
"clone" , "--bare" , oldRepoPath , repoPath ) .
"clone" , "--bare" , oldRepoPath , repoPath ) .
SetDescription ( fmt . Sprintf ( "ForkRepository(git clone): %s to %s" , old Repo . FullName ( ) , repo . FullName ( ) ) ) .
SetDescription ( fmt . Sprintf ( "ForkRepository(git clone): %s to %s" , opts . Base Repo . FullName ( ) , repo . FullName ( ) ) ) .
RunInDirTimeout ( 10 * time . Minute , "" ) ; err != nil {
RunInDirTimeout ( 10 * time . Minute , "" ) ; err != nil {
log . Error ( "Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v" , repo , old Repo , stdout , err )
log . Error ( "Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v" , repo , opts . Base Repo , stdout , err )
rollbackRemoveFn ( )
rollbackRemoveFn ( )
return fmt . Errorf ( "git clone: %v" , err )
return fmt . Errorf ( "git clone: %v" , err )
}
}
@ -103,7 +103,7 @@ func ForkRepository(doer, owner *models.User, oldRepo *models.Repository, name,
if err = repo . UpdateSize ( ctx ) ; err != nil {
if err = repo . UpdateSize ( ctx ) ; err != nil {
log . Error ( "Failed to update size for repository: %v" , err )
log . Error ( "Failed to update size for repository: %v" , err )
}
}
if err := models . CopyLanguageStat ( old Repo , repo ) ; err != nil {
if err := models . CopyLanguageStat ( opts . Base Repo , repo ) ; err != nil {
log . Error ( "Copy language stat from oldRepo failed" )
log . Error ( "Copy language stat from oldRepo failed" )
}
}