MCPcopy Create free account
hub / github.com/gogs/gogs / initRepository

Function initRepository

internal/database/repo.go:1052–1115  ·  view source on GitHub ↗

initRepository performs initial commit with chosen setup files on behave of doer.

(e Engine, repoPath string, doer *User, repo *Repository, opts CreateRepoOptionsLegacy)

Source from the content-addressed store, hash-verified

1050
1051// initRepository performs initial commit with chosen setup files on behave of doer.
1052func initRepository(e Engine, repoPath string, doer *User, repo *Repository, opts CreateRepoOptionsLegacy) (err error) {
1053 // Init bare new repository.
1054 if err = git.Init(repoPath, git.InitOptions{Bare: true}); err != nil {
1055 return errors.Newf("init repository: %v", err)
1056 } else if err = createDelegateHooks(repoPath); err != nil {
1057 return errors.Newf("createDelegateHooks: %v", err)
1058 }
1059
1060 // Set default branch
1061 _, err = git.SymbolicRef(
1062 repoPath,
1063 git.SymbolicRefOptions{
1064 Name: "HEAD",
1065 Ref: git.RefsHeads + conf.Repository.DefaultBranch,
1066 },
1067 )
1068 if err != nil {
1069 return errors.Wrap(err, "set default branch")
1070 }
1071
1072 tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond()))
1073
1074 // Initialize repository according to user's choice.
1075 if opts.AutoInit {
1076 if err = os.MkdirAll(tmpDir, os.ModePerm); err != nil {
1077 return err
1078 }
1079 defer RemoveAllWithNotice("Delete repository for auto-initialization", tmpDir)
1080
1081 if err = prepareRepoCommit(repo, tmpDir, repoPath, opts); err != nil {
1082 return errors.Newf("prepareRepoCommit: %v", err)
1083 }
1084
1085 // Apply changes and commit.
1086 err = initRepoCommit(
1087 tmpDir,
1088 &git.Signature{
1089 Name: doer.DisplayName(),
1090 Email: doer.Email,
1091 When: time.Now(),
1092 },
1093 )
1094 if err != nil {
1095 return errors.Newf("initRepoCommit: %v", err)
1096 }
1097 }
1098
1099 // Re-fetch the repository from database before updating it (else it would
1100 // override changes that were done earlier with sql)
1101 if repo, err = getRepositoryByID(e, repo.ID); err != nil {
1102 return errors.Newf("getRepositoryByID: %v", err)
1103 }
1104
1105 if !opts.AutoInit {
1106 repo.IsBare = true
1107 }
1108
1109 repo.DefaultBranch = conf.Repository.DefaultBranch

Callers 1

CreateRepositoryFunction · 0.70

Calls 7

createDelegateHooksFunction · 0.85
RemoveAllWithNoticeFunction · 0.85
prepareRepoCommitFunction · 0.85
initRepoCommitFunction · 0.85
getRepositoryByIDFunction · 0.85
updateRepositoryFunction · 0.85
DisplayNameMethod · 0.65

Tested by

no test coverage detected