diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2014-03-26 14:38:14 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2014-03-26 14:38:14 +0800 |
commit | 9dbc808c7b71fc97015346704bb3d3db4918aba0 (patch) | |
tree | f0d76b189a0137bcb455e548e52805787d680fa7 /models | |
parent | f9024b3f43c700ae997c284458fcc1d0dfc2e9a7 (diff) | |
parent | 06cf878471af02376dfcd02b9781982a89c27a2a (diff) | |
download | gitea-9dbc808c7b71fc97015346704bb3d3db4918aba0.tar.gz gitea-9dbc808c7b71fc97015346704bb3d3db4918aba0.zip |
Merge branch 'master' of github.com:gogits/gogs
Conflicts:
models/repo.go
modules/base/tool.go
serve.go
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 33 | ||||
-rw-r--r-- | models/git.go | 274 | ||||
-rw-r--r-- | models/issue.go | 2 | ||||
-rw-r--r-- | models/repo.go | 295 |
4 files changed, 316 insertions, 288 deletions
diff --git a/models/action.go b/models/action.go index 44d7aea8ca..edf1bf58f9 100644 --- a/models/action.go +++ b/models/action.go @@ -19,6 +19,7 @@ const ( OP_STAR_REPO OP_FOLLOW_REPO OP_COMMIT_REPO + OP_CREATE_ISSUE OP_PULL_REQUEST ) @@ -59,7 +60,7 @@ func (a Action) GetContent() string { // CommitRepoAction records action for commit repository. func CommitRepoAction(userId int64, userName string, repoId int64, repoName string, refName string, commits *base.PushCommits) error { - log.Trace("action.CommitRepoAction: %d/%s", userId, repoName) + log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName) bs, err := json.Marshal(commits) if err != nil { @@ -67,32 +68,8 @@ func CommitRepoAction(userId int64, userName string, return err } - // Add feeds for user self and all watchers. - watches, err := GetWatches(repoId) - if err != nil { - log.Error("action.CommitRepoAction(get watches): %d/%s", userId, repoName) - return err - } - watches = append(watches, Watch{UserId: userId}) - - for i := range watches { - if userId == watches[i].UserId && i > 0 { - continue // Do not add twice in case author watches his/her repository. - } - - _, err = orm.InsertOne(&Action{ - UserId: watches[i].UserId, - ActUserId: userId, - ActUserName: userName, - OpType: OP_COMMIT_REPO, - Content: string(bs), - RepoId: repoId, - RepoName: repoName, - RefName: refName, - }) - if err != nil { - log.Error("action.CommitRepoAction(notify watches): %d/%s", userId, repoName) - } + if err = NotifyWatchers(userId, repoId, OP_COMMIT_REPO, userName, repoName, refName, string(bs)); err != nil { + log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName) return err } @@ -107,6 +84,8 @@ func CommitRepoAction(userId int64, userName string, log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName) return err } + + log.Trace("action.CommitRepoAction(end): %d/%s", userId, repoName) return nil } diff --git a/models/git.go b/models/git.go new file mode 100644 index 0000000000..b0b0dd2f84 --- /dev/null +++ b/models/git.go @@ -0,0 +1,274 @@ +// Copyright 2014 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 ( + "container/list" + "fmt" + "path" + "strings" + + "github.com/Unknwon/com" + + "github.com/gogits/git" +) + +// RepoFile represents a file object in git repository. +type RepoFile struct { + *git.TreeEntry + Path string + Size int64 + Repo *git.Repository + Commit *git.Commit +} + +// LookupBlob returns the content of an object. +func (file *RepoFile) LookupBlob() (*git.Blob, error) { + if file.Repo == nil { + return nil, ErrRepoFileNotLoaded + } + + return file.Repo.LookupBlob(file.Id) +} + +// GetBranches returns all branches of given repository. +func GetBranches(userName, reposName string) ([]string, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + refs, err := repo.AllReferences() + if err != nil { + return nil, err + } + + brs := make([]string, len(refs)) + for i, ref := range refs { + brs[i] = ref.Name + } + return brs, nil +} + +func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + commit, err := repo.GetCommit(branchName, commitId) + if err != nil { + return nil, err + } + + parts := strings.Split(path.Clean(rpath), "/") + + var entry *git.TreeEntry + tree := commit.Tree + for i, part := range parts { + if i == len(parts)-1 { + entry = tree.EntryByName(part) + if entry == nil { + return nil, ErrRepoFileNotExist + } + } else { + tree, err = repo.SubTree(tree, part) + if err != nil { + return nil, err + } + } + } + + size, err := repo.ObjectSize(entry.Id) + if err != nil { + return nil, err + } + + repoFile := &RepoFile{ + entry, + rpath, + size, + repo, + commit, + } + + return repoFile, nil +} + +// GetReposFiles returns a list of file object in given directory of repository. +func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + commit, err := repo.GetCommit(branchName, commitId) + if err != nil { + return nil, err + } + + var repodirs []*RepoFile + var repofiles []*RepoFile + commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int { + if dirname == rpath { + // TODO: size get method shoule be improved + size, err := repo.ObjectSize(entry.Id) + if err != nil { + return 0 + } + + var cm = commit + var i int + for { + i = i + 1 + //fmt.Println(".....", i, cm.Id(), cm.ParentCount()) + if cm.ParentCount() == 0 { + break + } else if cm.ParentCount() == 1 { + pt, _ := repo.SubTree(cm.Parent(0).Tree, dirname) + if pt == nil { + break + } + pEntry := pt.EntryByName(entry.Name) + if pEntry == nil || !pEntry.Id.Equal(entry.Id) { + break + } else { + cm = cm.Parent(0) + } + } else { + var emptyCnt = 0 + var sameIdcnt = 0 + var lastSameCm *git.Commit + //fmt.Println(".....", cm.ParentCount()) + for i := 0; i < cm.ParentCount(); i++ { + //fmt.Println("parent", i, cm.Parent(i).Id()) + p := cm.Parent(i) + pt, _ := repo.SubTree(p.Tree, dirname) + var pEntry *git.TreeEntry + if pt != nil { + pEntry = pt.EntryByName(entry.Name) + } + + //fmt.Println("pEntry", pEntry) + + if pEntry == nil { + emptyCnt = emptyCnt + 1 + if emptyCnt+sameIdcnt == cm.ParentCount() { + if lastSameCm == nil { + goto loop + } else { + cm = lastSameCm + break + } + } + } else { + //fmt.Println(i, "pEntry", pEntry.Id, "entry", entry.Id) + if !pEntry.Id.Equal(entry.Id) { + goto loop + } else { + lastSameCm = cm.Parent(i) + sameIdcnt = sameIdcnt + 1 + if emptyCnt+sameIdcnt == cm.ParentCount() { + // TODO: now follow the first parent commit? + cm = lastSameCm + //fmt.Println("sameId...") + break + } + } + } + } + } + } + + loop: + + rp := &RepoFile{ + entry, + path.Join(dirname, entry.Name), + size, + repo, + cm, + } + + if entry.IsFile() { + repofiles = append(repofiles, rp) + } else if entry.IsDir() { + repodirs = append(repodirs, rp) + } + } + return 0 + }) + + return append(repodirs, repofiles...), nil +} + +func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) + if err != nil { + return nil, err + } + + return repo.GetCommit(branchname, commitid) +} + +// GetCommits returns all commits of given branch of repository. +func GetCommits(userName, reposName, branchname string) (*list.List, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) + if err != nil { + return nil, err + } + return r.AllCommits() +} + +type DiffFile struct { + Name string + Addition, Deletion int + Type string + Content []string +} + +type Diff struct { + NumFiles int // Number of file has been changed. + TotalAddition, TotalDeletion int + Files []*DiffFile +} + +func GetDiff(repoPath, commitid string) (*Diff, error) { + stdout, _, err := com.ExecCmdDir(repoPath, "git", "show", commitid) + if err != nil { + return nil, err + } + + // Sperate parts by file. + startIndex := strings.Index(stdout, "diff --git ") + 12 + + // First part is commit information. + // Check if it's a merge. + mergeIndex := strings.Index(stdout[:startIndex], "merge") + if mergeIndex > -1 { + mergeCommit := strings.SplitN(strings.Split(stdout[:startIndex], "\n")[1], "", 3)[2] + return GetDiff(repoPath, mergeCommit) + } + + parts := strings.Split(stdout[startIndex:], "diff --git ") + diff := &Diff{NumFiles: len(parts)} + diff.Files = make([]*DiffFile, 0, diff.NumFiles) + for _, part := range parts { + infos := strings.SplitN(part, "\n", 6) + maxIndex := len(infos) - 1 + infos[maxIndex] = strings.TrimSuffix(strings.TrimSuffix(infos[maxIndex], "\n"), "\n\\ No newline at end of file") + + file := &DiffFile{ + Name: strings.TrimPrefix(strings.Split(infos[0], " ")[0], "a/"), + Content: strings.Split(infos[maxIndex], "\n"), + } + diff.Files = append(diff.Files, file) + } + return diff, nil +} diff --git a/models/issue.go b/models/issue.go index fe43a94b59..2de6568589 100644 --- a/models/issue.go +++ b/models/issue.go @@ -23,6 +23,7 @@ type Issue struct { Name string RepoId int64 `xorm:"index"` PosterId int64 + Poster *User `xorm:"-"` MilestoneId int64 AssigneeId int64 IsPull bool // Indicates whether is a pull request or not. @@ -58,7 +59,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co Content: content, } _, err = orm.Insert(issue) - // TODO: newIssueAction return issue, err } diff --git a/models/repo.go b/models/repo.go index 84c3283884..477f9472b4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -5,17 +5,14 @@ package models import ( - "container/list" "errors" "fmt" "io/ioutil" "os" "os/exec" - "path" "path/filepath" "regexp" "strings" - "sync" "time" "unicode/utf8" @@ -36,8 +33,6 @@ var ( ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded") ) -var gitInitLocker = sync.Mutex{} - var ( LanguageIgns, Licenses []string ) @@ -222,33 +217,21 @@ func extractGitBareZip(repoPath string) error { } // initRepoCommit temporarily changes with work directory. -func initRepoCommit(tmpPath string, sig *git.Signature) error { - gitInitLocker.Lock() - defer gitInitLocker.Unlock() - - // Change work directory. - curPath, err := os.Getwd() - if err != nil { - return err - } else if err = os.Chdir(tmpPath); err != nil { - return err - } - defer os.Chdir(curPath) - +func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { var stderr string - if _, stderr, err = com.ExecCmd("git", "add", "--all"); err != nil { + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { return err } - log.Info("stderr(1): %s", stderr) - if _, stderr, err = com.ExecCmd("git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + log.Trace("stderr(1): %s", stderr) + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { return err } - log.Info("stderr(2): %s", stderr) - if _, stderr, err = com.ExecCmd("git", "push", "origin", "master"); err != nil { + log.Trace("stderr(2): %s", stderr) + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { return err } - log.Info("stderr(3): %s", stderr) + log.Trace("stderr(3): %s", stderr) return nil } @@ -272,18 +255,6 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return err } - /*// hook/post-update - pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777) - if err != nil { - return err - } - defer pu2.Close() - // TODO: Windows .bat - if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil { - return err - } - */ - // Initialize repository according to user's choice. fileName := map[string]string{} if initReadme { @@ -505,239 +476,43 @@ func GetWatches(repoId int64) ([]Watch, error) { return watches, err } -// IsWatching checks if user has watched given repository. -func IsWatching(userId, repoId int64) bool { - has, _ := orm.Get(&Watch{0, repoId, userId}) - return has -} - -func StarReposiory(user *User, repoName string) error { - return nil -} - -func UnStarRepository() { - -} - -func WatchRepository() { - -} - -func UnWatchRepository() { - -} - -func ForkRepository(reposName string, userId int64) { - -} - -// RepoFile represents a file object in git repository. -type RepoFile struct { - *git.TreeEntry - Path string - Size int64 - Repo *git.Repository - Commit *git.Commit -} - -// LookupBlob returns the content of an object. -func (file *RepoFile) LookupBlob() (*git.Blob, error) { - if file.Repo == nil { - return nil, ErrRepoFileNotLoaded - } - - return file.Repo.LookupBlob(file.Id) -} - -// GetBranches returns all branches of given repository. -func GetBranches(userName, reposName string) ([]string, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - - refs, err := repo.AllReferences() - if err != nil { - return nil, err - } - - brs := make([]string, len(refs)) - for i, ref := range refs { - brs[i] = ref.BranchName() - } - return brs, nil -} - -func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) +// NotifyWatchers creates batch of actions for every watcher. +func NotifyWatchers(userId, repoId int64, opType int, userName, repoName, refName, content string) error { + // Add feeds for user self and all watchers. + watches, err := GetWatches(repoId) if err != nil { - return nil, err + return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) } + watches = append(watches, Watch{UserId: userId}) - commit, err := repo.GetCommit(branchName, commitId) - if err != nil { - return nil, err - } - - parts := strings.Split(path.Clean(rpath), "/") - - var entry *git.TreeEntry - tree := commit.Tree - for i, part := range parts { - if i == len(parts)-1 { - entry = tree.EntryByName(part) - if entry == nil { - return nil, ErrRepoFileNotExist - } - } else { - tree, err = repo.SubTree(tree, part) - if err != nil { - return nil, err - } + for i := range watches { + if userId == watches[i].UserId && i > 0 { + continue // Do not add twice in case author watches his/her repository. } - } - - size, err := repo.ObjectSize(entry.Id) - if err != nil { - return nil, err - } - repoFile := &RepoFile{ - entry, - rpath, - size, - repo, - commit, + _, err = orm.InsertOne(&Action{ + UserId: watches[i].UserId, + ActUserId: userId, + ActUserName: userName, + OpType: opType, + Content: content, + RepoId: repoId, + RepoName: repoName, + RefName: refName, + }) + if err != nil { + return errors.New("repo.NotifyWatchers(create action): " + err.Error()) + } } - - return repoFile, nil + return nil } -// GetReposFiles returns a list of file object in given directory of repository. -func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - - commit, err := repo.GetCommit(branchName, commitId) - if err != nil { - return nil, err - } - - var repodirs []*RepoFile - var repofiles []*RepoFile - commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int { - if dirname == rpath { - // TODO: size get method shoule be improved - size, err := repo.ObjectSize(entry.Id) - if err != nil { - return 0 - } - - var cm = commit - var i int - for { - i = i + 1 - //fmt.Println(".....", i, cm.Id(), cm.ParentCount()) - if cm.ParentCount() == 0 { - break - } else if cm.ParentCount() == 1 { - pt, _ := repo.SubTree(cm.Parent(0).Tree, dirname) - if pt == nil { - break - } - pEntry := pt.EntryByName(entry.Name) - if pEntry == nil || !pEntry.Id.Equal(entry.Id) { - break - } else { - cm = cm.Parent(0) - } - } else { - var emptyCnt = 0 - var sameIdcnt = 0 - var lastSameCm *git.Commit - //fmt.Println(".....", cm.ParentCount()) - for i := 0; i < cm.ParentCount(); i++ { - //fmt.Println("parent", i, cm.Parent(i).Id()) - p := cm.Parent(i) - pt, _ := repo.SubTree(p.Tree, dirname) - var pEntry *git.TreeEntry - if pt != nil { - pEntry = pt.EntryByName(entry.Name) - } - - //fmt.Println("pEntry", pEntry) - - if pEntry == nil { - emptyCnt = emptyCnt + 1 - if emptyCnt+sameIdcnt == cm.ParentCount() { - if lastSameCm == nil { - goto loop - } else { - cm = lastSameCm - break - } - } - } else { - //fmt.Println(i, "pEntry", pEntry.Id, "entry", entry.Id) - if !pEntry.Id.Equal(entry.Id) { - goto loop - } else { - lastSameCm = cm.Parent(i) - sameIdcnt = sameIdcnt + 1 - if emptyCnt+sameIdcnt == cm.ParentCount() { - // TODO: now follow the first parent commit? - cm = lastSameCm - //fmt.Println("sameId...") - break - } - } - } - } - } - } - - loop: - - rp := &RepoFile{ - entry, - path.Join(dirname, entry.Name), - size, - repo, - cm, - } - - if entry.IsFile() { - repofiles = append(repofiles, rp) - } else if entry.IsDir() { - repodirs = append(repodirs, rp) - } - } - return 0 - }) - - return append(repodirs, repofiles...), nil +// IsWatching checks if user has watched given repository. +func IsWatching(userId, repoId int64) bool { + has, _ := orm.Get(&Watch{0, repoId, userId}) + return has } -func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) { - repo, err := git.OpenRepository(RepoPath(userName, repoName)) - if err != nil { - return nil, err - } - - return repo.GetCommit(branchname, commitid) -} +func ForkRepository(reposName string, userId int64) { -// GetCommits returns all commits of given branch of repository. -func GetCommits(userName, reposName, branchname string) (*list.List, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) - if err != nil { - return nil, err - } - return r.AllCommits() } |