diff options
author | Unknwon <u@gogs.io> | 2015-11-30 20:46:19 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-11-30 20:46:19 -0500 |
commit | dcb391d3415a84ec893d11b788f776ea89c301c3 (patch) | |
tree | d83263075dbbe416cc79691fd87cc1255226ddf5 /models | |
parent | 5a14c3cf9854b2762a82ef6bdd781810380ab666 (diff) | |
parent | 830d00066785d131413d1de11ce301bf1f0b818a (diff) |
Merge branch 'feature/wiki' into develop
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 20 | ||||
-rw-r--r-- | models/pull.go | 9 | ||||
-rw-r--r-- | models/release.go | 12 | ||||
-rw-r--r-- | models/repo.go | 254 | ||||
-rw-r--r-- | models/user.go | 12 | ||||
-rw-r--r-- | models/wiki.go | 178 |
6 files changed, 328 insertions, 157 deletions
diff --git a/models/error.go b/models/error.go index 069346be..d005b9af 100644 --- a/models/error.go +++ b/models/error.go @@ -107,6 +107,26 @@ func (err ErrUserHasOrgs) Error() string { return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID) } +// __ __.__ __ .__ +// / \ / \__| | _|__| +// \ \/\/ / | |/ / | +// \ /| | <| | +// \__/\ / |__|__|_ \__| +// \/ \/ + +type ErrWikiAlreadyExist struct { + Title string +} + +func IsErrWikiAlreadyExist(err error) bool { + _, ok := err.(ErrWikiAlreadyExist) + return ok +} + +func (err ErrWikiAlreadyExist) Error() string { + return fmt.Sprintf("wiki page already exists [title: %s]", err.Title) +} + // __________ ___. .__ .__ ____ __. // \______ \__ _\_ |__ | | |__| ____ | |/ _|____ ___.__. // | ___/ | \ __ \| | | |/ ___\ | <_/ __ < | | diff --git a/models/pull.go b/models/pull.go index dfd80635..8020a1e1 100644 --- a/models/pull.go +++ b/models/pull.go @@ -252,7 +252,7 @@ func (pr *PullRequest) testPatch() (err error) { // Checkout base branch. _, stderr, err := process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(), - fmt.Sprintf("PullRequest.Merge(git checkout): %s", pr.BaseRepo.ID), + fmt.Sprintf("PullRequest.Merge(git checkout): %v", pr.BaseRepo.ID), "git", "checkout", pr.BaseBranch) if err != nil { return fmt.Errorf("git checkout: %s", stderr) @@ -415,12 +415,7 @@ func (pr *PullRequest) UpdatePatch() (err error) { return fmt.Errorf("GetOwner: %v", err) } - headRepoPath, err := pr.HeadRepo.RepoPath() - if err != nil { - return fmt.Errorf("HeadRepo.RepoPath: %v", err) - } - - headGitRepo, err := git.OpenRepository(headRepoPath) + headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } diff --git a/models/release.go b/models/release.go index ef1d640d..08e00609 100644 --- a/models/release.go +++ b/models/release.go @@ -51,6 +51,10 @@ func IsReleaseExist(repoID int64, tagName string) (bool, error) { return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}) } +func init() { + git.GetVersion() +} + func createTag(gitRepo *git.Repository, rel *Release) error { // Only actual create when publish. if !rel.IsDraft { @@ -175,12 +179,8 @@ func DeleteReleaseByID(id int64) error { return fmt.Errorf("GetRepositoryByID: %v", err) } - repoPath, err := repo.RepoPath() - if err != nil { - return fmt.Errorf("RepoPath: %v", err) - } - - _, stderr, err := process.ExecDir(-1, repoPath, fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID), + _, stderr, err := process.ExecDir(-1, repo.RepoPath(), + fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID), "git", "tag", "-d", rel.TagName) if err != nil && !strings.Contains(stderr, "not found") { return fmt.Errorf("git tag -d: %v - %s", err, stderr) diff --git a/models/repo.go b/models/repo.go index b628e752..bc8d4cd4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -24,11 +24,13 @@ import ( "github.com/Unknwon/cae/zip" "github.com/Unknwon/com" "github.com/go-xorm/xorm" + "github.com/mcuadros/go-version" "gopkg.in/ini.v1" + "github.com/gogits/git-shell" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/bindata" - "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" @@ -95,19 +97,15 @@ func NewRepoContext() { } // Check Git version. - ver, err := git.GetVersion() + gitVer, err := git.Version() if err != nil { log.Fatal(4, "Fail to get Git version: %v", err) } - reqVer, err := git.ParseVersion("1.7.1") - if err != nil { - log.Fatal(4, "Fail to parse required Git version: %v", err) - } - if ver.LessThan(reqVer) { + log.Info("Git Version: %s", gitVer) + if version.Compare("1.7.1", gitVer, ">") { log.Fatal(4, "Gogs requires Git version greater or equal to 1.7.1") } - log.Info("Git Version: %s", ver.String()) // Git requires setting user.name and user.email in order to commit changes. for configKey, defaultValue := range map[string]string{"user.name": "Gogs", "user.email": "gogs@fake.local"} { @@ -197,6 +195,25 @@ func (repo *Repository) GetOwner() error { return repo.getOwner(x) } +func (repo *Repository) mustOwner(e Engine) *User { + if err := repo.getOwner(e); err != nil { + return &User{ + Name: "error", + FullName: err.Error(), + } + } + + return repo.Owner +} + +// MustOwner always returns a valid *User object to avoid +// conceptually impossible error handling. +// It creates a fake object that contains error deftail +// when error occurs. +func (repo *Repository) MustOwner() *User { + return repo.mustOwner(x) +} + // GetAssignees returns all users that have write access of repository. func (repo *Repository) GetAssignees() (_ []*User, err error) { if err = repo.GetOwner(); err != nil { @@ -253,22 +270,16 @@ func (repo *Repository) GetBaseRepo() (err error) { return err } -func (repo *Repository) repoPath(e Engine) (string, error) { - if err := repo.getOwner(e); err != nil { - return "", err - } - return RepoPath(repo.Owner.Name, repo.Name), nil +func (repo *Repository) repoPath(e Engine) string { + return RepoPath(repo.mustOwner(e).Name, repo.Name) } -func (repo *Repository) RepoPath() (string, error) { +func (repo *Repository) RepoPath() string { return repo.repoPath(x) } -func (repo *Repository) RepoLink() (string, error) { - if err := repo.GetOwner(); err != nil { - return "", err - } - return setting.AppSubUrl + "/" + repo.Owner.Name + "/" + repo.Name, nil +func (repo *Repository) RepoLink() string { + return setting.AppSubUrl + "/" + repo.MustOwner().Name + "/" + repo.Name } func (repo *Repository) HasAccess(u *User) bool { @@ -305,31 +316,24 @@ func (repo *Repository) LocalCopyPath() string { return path.Join(setting.AppDataPath, "tmp/local", com.ToStr(repo.ID)) } -// UpdateLocalCopy makes sure the local copy of repository is up-to-date. -func (repo *Repository) UpdateLocalCopy() error { - repoPath, err := repo.RepoPath() - if err != nil { - return err - } - - localPath := repo.LocalCopyPath() +func updateLocalCopy(repoPath, localPath string) error { if !com.IsExist(localPath) { - _, stderr, err := process.Exec( - fmt.Sprintf("UpdateLocalCopy(git clone): %s", repoPath), "git", "clone", repoPath, localPath) - if err != nil { - return fmt.Errorf("git clone: %v - %s", err, stderr) + if err := git.Clone(repoPath, localPath); err != nil { + return fmt.Errorf("Clone: %v", err) } } else { - _, stderr, err := process.ExecDir(-1, localPath, - fmt.Sprintf("UpdateLocalCopy(git pull --all): %s", repoPath), "git", "pull", "--all") - if err != nil { - return fmt.Errorf("git pull: %v - %s", err, stderr) + if err := git.Pull(localPath, true); err != nil { + return fmt.Errorf("Pull: %v", err) } } - return nil } +// UpdateLocalCopy makes sure the local copy of repository is up-to-date. +func (repo *Repository) UpdateLocalCopy() error { + return updateLocalCopy(repo.RepoPath(), repo.LocalCopyPath()) +} + // PatchPath returns corresponding patch file path of repository by given issue ID. func (repo *Repository) PatchPath(index int64) (string, error) { if err := repo.GetOwner(); err != nil { @@ -374,24 +378,31 @@ type CloneLink struct { Git string } -// CloneLink returns clone URLs of repository. -func (repo *Repository) CloneLink() (cl CloneLink, err error) { - if err = repo.GetOwner(); err != nil { - return cl, err +func (repo *Repository) cloneLink(isWiki bool) *CloneLink { + repoName := repo.Name + if isWiki { + repoName += ".wiki" } + repo.Owner = repo.MustOwner() + cl := new(CloneLink) if setting.SSHPort != 22 { - cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.SSHDomain, setting.SSHPort, repo.Owner.Name, repo.Name) + cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.SSHDomain, setting.SSHPort, repo.Owner.Name, repoName) } else { - cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSHDomain, repo.Owner.Name, repo.Name) + cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSHDomain, repo.Owner.Name, repoName) } - cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.Name, repo.Name) - return cl, nil + cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.Name, repoName) + return cl +} + +// CloneLink returns clone URLs of repository. +func (repo *Repository) CloneLink() (cl *CloneLink) { + return repo.cloneLink(false) } var ( reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"} - reservedPatterns = []string{"*.git", "*.keys"} + reservedPatterns = []string{"*.git", "*.keys", "*.wiki"} ) // IsUsableName checks if name is reserved or pattern of name is not allowed. @@ -463,6 +474,11 @@ func UpdateMirror(m *Mirror) error { return updateMirror(x, m) } +func createUpdateHook(repoPath string) error { + return git.SetUpdateHook(repoPath, + fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf)) +} + // MirrorRepository creates a mirror repository from source. func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error { _, stderr, err := process.ExecTimeout(10*time.Minute, @@ -560,13 +576,19 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) { } } - // Check if repository has master branch, if so set it to default branch. + // Try to get HEAD branch and set it as default branch. gitRepo, err := git.OpenRepository(repoPath) if err != nil { - return repo, fmt.Errorf("open git repository: %v", err) + log.Error(4, "OpenRepository: %v", err) + return repo, nil } - if gitRepo.IsBranchExist("master") { - repo.DefaultBranch = "master" + headBranch, err := gitRepo.GetHEADBranch() + if err != nil { + log.Error(4, "GetHEADBranch: %v", err) + return repo, nil + } + if headBranch != nil { + repo.DefaultBranch = headBranch.Name } return repo, UpdateRepository(repo, false) @@ -596,13 +618,6 @@ func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { return nil } -func createUpdateHook(repoPath string) error { - hookPath := path.Join(repoPath, "hooks/update") - os.MkdirAll(path.Dir(hookPath), os.ModePerm) - return ioutil.WriteFile(hookPath, - []byte(fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf)), 0777) -} - type CreateRepoOptions struct { Name string Description string @@ -639,10 +654,7 @@ func prepareRepoCommit(repo *Repository, tmpDir, repoPath string, opts CreateRep return fmt.Errorf("getRepoInitFile[%s]: %v", opts.Readme, err) } - cloneLink, err := repo.CloneLink() - if err != nil { - return fmt.Errorf("CloneLink: %v", err) - } + cloneLink := repo.CloneLink() match := map[string]string{ "Name": repo.Name, "Description": repo.Description, @@ -691,22 +703,17 @@ func prepareRepoCommit(repo *Repository, tmpDir, repoPath string, opts CreateRep } // InitRepository initializes README and .gitignore if needed. -func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts CreateRepoOptions) error { +func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts CreateRepoOptions) (err error) { // Somehow the directory could exist. if com.IsExist(repoPath) { return fmt.Errorf("initRepository: path already exists: %s", repoPath) } // Init bare new repository. - os.MkdirAll(repoPath, os.ModePerm) - _, stderr, err := process.ExecDir(-1, repoPath, - fmt.Sprintf("initRepository (git init --bare): %s", repoPath), "git", "init", "--bare") - if err != nil { - return fmt.Errorf("git init --bare: %v - %s", err, stderr) - } - - if err := createUpdateHook(repoPath); err != nil { - return err + if err = git.InitRepository(repoPath, true); err != nil { + return fmt.Errorf("InitRepository: %v", err) + } else if err = createUpdateHook(repoPath); err != nil { + return fmt.Errorf("createUpdateHook: %v", err) } tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) @@ -977,7 +984,9 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error { // Change repository directory name. if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil { - return fmt.Errorf("rename directory: %v", err) + return fmt.Errorf("rename repository directory: %v", err) + } else if err = os.Rename(WikiPath(owner.Name, repo.Name), WikiPath(newOwner.Name, repo.Name)); err != nil { + return fmt.Errorf("rename repository wiki: %v", err) } return sess.Commit() @@ -999,7 +1008,10 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) } // Change repository directory name. - return os.Rename(RepoPath(u.LowerName, oldRepoName), RepoPath(u.LowerName, newRepoName)) + if err = os.Rename(RepoPath(u.Name, oldRepoName), RepoPath(u.Name, newRepoName)); err != nil { + return fmt.Errorf("rename repository directory: %v", err) + } + return os.Rename(WikiPath(u.Name, oldRepoName), WikiPath(u.Name, newRepoName)) } func getRepositoriesByForkID(e Engine, forkID int64) ([]*Repository, error) { @@ -1103,26 +1115,19 @@ func DeleteRepository(uid, repoID int64) error { } } - if _, err = sess.Delete(&Repository{ID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&Access{RepoID: repo.ID}); err != nil { - return err - } else if _, err = sess.Delete(&Action{RepoID: repo.ID}); err != nil { - return err - } else if _, err = sess.Delete(&Watch{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&Mirror{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&IssueUser{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&Milestone{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&Release{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&Collaboration{RepoID: repoID}); err != nil { - return err - } else if _, err = sess.Delete(&PullRequest{BaseRepoID: repoID}); err != nil { - return err + if err = deleteBeans(sess, + &Repository{ID: repoID}, + &Access{RepoID: repo.ID}, + &Action{RepoID: repo.ID}, + &Watch{RepoID: repoID}, + &Mirror{RepoID: repoID}, + &IssueUser{RepoID: repoID}, + &Milestone{RepoID: repoID}, + &Release{RepoID: repoID}, + &Collaboration{RepoID: repoID}, + &PullRequest{BaseRepoID: repoID}, + ); err != nil { + return fmt.Errorf("deleteBeans: %v", err) } // Delete comments and attachments. @@ -1164,12 +1169,18 @@ func DeleteRepository(uid, repoID int64) error { } // Remove repository files. - repoPath, err := repo.repoPath(sess) - if err != nil { - return fmt.Errorf("RepoPath: %v", err) - } + repoPath := repo.repoPath(sess) if err = os.RemoveAll(repoPath); err != nil { - desc := fmt.Sprintf("delete repository files[%s]: %v", repoPath, err) + desc := fmt.Sprintf("delete repository files [%s]: %v", repoPath, err) + log.Warn(desc) + if err = CreateRepositoryNotice(desc); err != nil { + log.Error(4, "CreateRepositoryNotice: %v", err) + } + } + + wikiPath := repo.WikiPath() + if err = os.RemoveAll(wikiPath); err != nil { + desc := fmt.Sprintf("delete repository wiki [%s]: %v", wikiPath, err) log.Warn(desc) if err = CreateRepositoryNotice(desc); err != nil { log.Error(4, "CreateRepositoryNotice: %v", err) @@ -1315,14 +1326,7 @@ func DeleteRepositoryArchives() error { return x.Where("id > 0").Iterate(new(Repository), func(idx int, bean interface{}) error { repo := bean.(*Repository) - repoPath, err := repo.RepoPath() - if err != nil { - if err2 := CreateRepositoryNotice(fmt.Sprintf("DeleteRepositoryArchives.RepoPath [%d]: %v", repo.ID, err)); err2 != nil { - log.Error(4, "CreateRepositoryNotice: %v", err2) - } - return nil - } - return os.RemoveAll(filepath.Join(repoPath, "archives")) + return os.RemoveAll(filepath.Join(repo.RepoPath(), "archives")) }) } @@ -1332,15 +1336,7 @@ func DeleteMissingRepositories() error { if err := x.Where("id > 0").Iterate(new(Repository), func(idx int, bean interface{}) error { repo := bean.(*Repository) - repoPath, err := repo.RepoPath() - if err != nil { - if err2 := CreateRepositoryNotice(fmt.Sprintf("DeleteRepositoryArchives.RepoPath [%d]: %v", repo.ID, err)); err2 != nil { - log.Error(4, "CreateRepositoryNotice: %v", err2) - } - return nil - } - - if !com.IsDir(repoPath) { + if !com.IsDir(repo.RepoPath()) { repos = append(repos, repo) } return nil @@ -1371,14 +1367,7 @@ func RewriteRepositoryUpdateHook() error { return x.Where("id > 0").Iterate(new(Repository), func(idx int, bean interface{}) error { repo := bean.(*Repository) - repoPath, err := repo.RepoPath() - if err != nil { - if err2 := CreateRepositoryNotice(fmt.Sprintf("RewriteRepositoryUpdateHook[%d]: %v", repo.ID, err)); err2 != nil { - log.Error(4, "CreateRepositoryNotice: %v", err2) - } - return nil - } - return createUpdateHook(repoPath) + return createUpdateHook(repo.RepoPath()) }) } @@ -1445,11 +1434,7 @@ func MirrorUpdate() { return nil } - repoPath, err := m.Repo.RepoPath() - if err != nil { - return fmt.Errorf("Repo.RepoPath: %v", err) - } - + repoPath := m.Repo.RepoPath() if _, stderr, err := process.ExecDir(10*time.Minute, repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath), "git", "remote", "update", "--prune"); err != nil { @@ -1489,12 +1474,8 @@ func GitFsck() { if err := x.Where("id>0").Iterate(new(Repository), func(idx int, bean interface{}) error { repo := bean.(*Repository) - repoPath, err := repo.RepoPath() - if err != nil { - return fmt.Errorf("RepoPath: %v", err) - } - - _, _, err = process.ExecDir(-1, repoPath, "Repository health check", "git", args...) + repoPath := repo.RepoPath() + _, _, err := process.ExecDir(-1, repoPath, "Repository health check", "git", args...) if err != nil { desc := fmt.Sprintf("Fail to health check repository(%s)", repoPath) log.Warn(desc) @@ -1912,15 +1893,10 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit return nil, err } - oldRepoPath, err := oldRepo.RepoPath() - if err != nil { - return nil, fmt.Errorf("get old repository path: %v", err) - } - repoPath := RepoPath(u.Name, repo.Name) _, stderr, err := process.ExecTimeout(10*time.Minute, fmt.Sprintf("ForkRepository(git clone): %s/%s", u.Name, repo.Name), - "git", "clone", "--bare", oldRepoPath, repoPath) + "git", "clone", "--bare", oldRepo.RepoPath(), repoPath) if err != nil { return nil, fmt.Errorf("git clone: %v", stderr) } diff --git a/models/user.go b/models/user.go index fa19e0c4..885111ad 100644 --- a/models/user.go +++ b/models/user.go @@ -25,9 +25,11 @@ import ( "github.com/go-xorm/xorm" "github.com/nfnt/resize" + "github.com/gogits/git-shell" + "github.com/gogits/gogs/modules/avatar" "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/git" + oldgit "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" ) @@ -664,7 +666,7 @@ func deleteUser(e *xorm.Session, u *User) error { &IssueUser{UID: u.Id}, &EmailAddress{UID: u.Id}, ); err != nil { - return fmt.Errorf("deleteUser: %v", err) + return fmt.Errorf("deleteBeans: %v", err) } // ***** START: PublicKey ***** @@ -938,11 +940,11 @@ func MakeEmailPrimary(email *EmailAddress) error { // UserCommit represents a commit with validation of user. type UserCommit struct { User *User - *git.Commit + *oldgit.Commit } // ValidateCommitWithEmail chceck if author's e-mail of commit is corresponsind to a user. -func ValidateCommitWithEmail(c *git.Commit) *User { +func ValidateCommitWithEmail(c *oldgit.Commit) *User { u, err := GetUserByEmail(c.Author.Email) if err != nil { return nil @@ -959,7 +961,7 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List { e = oldCommits.Front() ) for e != nil { - c := e.Value.(*git.Commit) + c := e.Value.(*oldgit.Commit) if v, ok := emails[c.Author.Email]; !ok { u, _ = GetUserByEmail(c.Author.Email) diff --git a/models/wiki.go b/models/wiki.go new file mode 100644 index 00000000..7baf9f94 --- /dev/null +++ b/models/wiki.go @@ -0,0 +1,178 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + "strings" + "sync" + + "github.com/Unknwon/com" + + "github.com/gogits/git-shell" + + "github.com/gogits/gogs/modules/setting" +) + +// workingPool represents a pool of working status which makes sure +// that only one instance of same task is performing at a time. +// However, different type of tasks can performing at the same time. +type workingPool struct { + lock sync.Mutex + pool map[string]*sync.Mutex + count map[string]int +} + +// CheckIn checks in a task and waits if others are running. +func (p *workingPool) CheckIn(name string) { + p.lock.Lock() + + lock, has := p.pool[name] + if !has { + lock = &sync.Mutex{} + p.pool[name] = lock + } + p.count[name]++ + + p.lock.Unlock() + lock.Lock() +} + +// CheckOut checks out a task to let other tasks run. +func (p *workingPool) CheckOut(name string) { + p.lock.Lock() + defer p.lock.Unlock() + + p.pool[name].Unlock() + if p.count[name] == 1 { + delete(p.pool, name) + delete(p.count, name) + } else { + p.count[name]-- + } +} + +var wikiWorkingPool = &workingPool{ + pool: make(map[string]*sync.Mutex), + count: make(map[string]int), +} + +// ToWikiPageURL formats a string to corresponding wiki URL name. +func ToWikiPageURL(name string) string { + return strings.Replace(name, " ", "-", -1) +} + +// ToWikiPageName formats a URL back to corresponding wiki page name. +func ToWikiPageName(name string) string { + return strings.Replace(name, "-", " ", -1) +} + +// WikiCloneLink returns clone URLs of repository wiki. +func (repo *Repository) WikiCloneLink() (cl *CloneLink) { + return repo.cloneLink(true) +} + +// WikiPath returns wiki data path by given user and repository name. +func WikiPath(userName, repoName string) string { + return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git") +} + +func (repo *Repository) WikiPath() string { + return WikiPath(repo.MustOwner().Name, repo.Name) +} + +// HasWiki returns true if repository has wiki. +func (repo *Repository) HasWiki() bool { + return com.IsDir(repo.WikiPath()) +} + +// InitWiki initializes a wiki for repository, +// it does nothing when repository already has wiki. +func (repo *Repository) InitWiki() error { + if repo.HasWiki() { + return nil + } + + if err := git.InitRepository(repo.WikiPath(), true); err != nil { + return fmt.Errorf("InitRepository: %v", err) + } + return nil +} + +func (repo *Repository) LocalWikiPath() string { + return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID)) +} + +// UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date. +func (repo *Repository) UpdateLocalWiki() error { + return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath()) +} + +// updateWikiPage adds new page to repository wiki. +func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) { + wikiWorkingPool.CheckIn(com.ToStr(repo.ID)) + defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID)) + + if err = repo.InitWiki(); err != nil { + return fmt.Errorf("InitWiki: %v", err) + } + + localPath := repo.LocalWikiPath() + + // Discard local commits make sure even to remote when local copy exists. + if com.IsExist(localPath) { + // No need to check if nothing in the repository. + if git.IsBranchExist(localPath, "master") { + if err = git.Reset(localPath, true, "origin/master"); err != nil { + return fmt.Errorf("Reset: %v", err) + } + } + } + + if err = repo.UpdateLocalWiki(); err != nil { + return fmt.Errorf("UpdateLocalWiki: %v", err) + } + + title = ToWikiPageName(strings.Replace(title, "/", " ", -1)) + filename := path.Join(localPath, title+".md") + + // If not a new file, show perform update not create. + if isNew { + if com.IsExist(filename) { + return ErrWikiAlreadyExist{filename} + } + } else { + os.Remove(path.Join(localPath, oldTitle+".md")) + } + + if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil { + return fmt.Errorf("WriteFile: %v", err) + } + + if len(message) == 0 { + message = "Update page '" + title + "'" + } + if err = git.AddChanges(localPath, true); err != nil { + return fmt.Errorf("AddChanges: %v", err) + } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil { + return fmt.Errorf("CommitChanges: %v", err) + } else if err = git.Push(localPath, "origin", "master"); err != nil { + return fmt.Errorf("Push: %v", err) + } + + return nil +} + +func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error { + return repo.updateWikiPage(doer, "", title, content, message, true) +} + +func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error { + return repo.updateWikiPage(doer, oldTitle, title, content, message, false) +} |