From 150eef93b2340f665c070158ade1863339829e05 Mon Sep 17 00:00:00 2001 From: lunnyxiao Date: Mon, 22 Sep 2014 10:43:16 +0800 Subject: [PATCH] add submodule basic support & buf fixed #478 --- modules/git/commit.go | 50 ++++++++++++++++++++++++++++++++++- modules/git/submodule.go | 6 +++++ modules/git/tree.go | 2 ++ modules/git/tree_entry.go | 4 +++ routers/repo/http.go | 5 ++++ routers/repo/view.go | 40 +++++++++++++++++++++++----- templates/repo/view_list.tmpl | 17 ++++++++++-- 7 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 modules/git/submodule.go diff --git a/modules/git/commit.go b/modules/git/commit.go index 52348fefed..d2d373da12 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -5,6 +5,7 @@ package git import ( + "bufio" "container/list" "strings" ) @@ -17,7 +18,8 @@ type Commit struct { Committer *Signature CommitMessage string - parents []sha1 // sha1 strings + parents []sha1 // sha1 strings + submodules map[string]*SubModule } // Return the commit message. Same as retrieving CommitMessage directly. @@ -84,3 +86,49 @@ func (c *Commit) CommitsByRange(page int) (*list.List, error) { func (c *Commit) GetCommitOfRelPath(relPath string) (*Commit, error) { return c.repo.getCommitOfRelPath(c.Id, relPath) } + +func (c *Commit) GetSubModule(entryname string) (*SubModule, error) { + moduels, err := c.GetSubModules() + if err != nil { + return nil, err + } + return moduels[entryname], nil +} + +func (c *Commit) GetSubModules() (map[string]*SubModule, error) { + if c.submodules != nil { + return c.submodules, nil + } + + entry, err := c.GetTreeEntryByPath(".gitmodules") + if err != nil { + return nil, err + } + rd, err := entry.Blob().Data() + if err != nil { + return nil, err + } + + scanner := bufio.NewScanner(rd) + c.submodules = make(map[string]*SubModule) + var ismodule bool + var path string + for scanner.Scan() { + if strings.HasPrefix(scanner.Text(), "[submodule") { + ismodule = true + continue + } + if ismodule { + fields := strings.Split(scanner.Text(), "=") + k := strings.TrimSpace(fields[0]) + if k == "path" { + path = strings.TrimSpace(fields[1]) + } else if k == "url" { + c.submodules[path] = &SubModule{path, strings.TrimSpace(fields[1])} + ismodule = false + } + } + } + + return c.submodules, nil +} diff --git a/modules/git/submodule.go b/modules/git/submodule.go new file mode 100644 index 0000000000..28b5b9f375 --- /dev/null +++ b/modules/git/submodule.go @@ -0,0 +1,6 @@ +package git + +type SubModule struct { + Name string + Url string +} diff --git a/modules/git/tree.go b/modules/git/tree.go index 03152c34ac..a3012443d7 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -51,6 +51,8 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { case "160000": entry.mode = ModeCommit entry.Type = COMMIT + + step = 8 case "040000": entry.mode = ModeTree entry.Type = TREE diff --git a/modules/git/tree_entry.go b/modules/git/tree_entry.go index e842f2332a..f65f332666 100644 --- a/modules/git/tree_entry.go +++ b/modules/git/tree_entry.go @@ -61,6 +61,10 @@ func (te *TreeEntry) Size() int64 { return te.size } +func (te *TreeEntry) IsSubModule() bool { + return te.mode == ModeCommit +} + func (te *TreeEntry) IsDir() bool { return te.mode == ModeTree } diff --git a/routers/repo/http.go b/routers/repo/http.go index 56c85bf59a..a98478c9f5 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -103,6 +103,7 @@ func Http(ctx *middleware.Context) { // check access if askAuth { baHead := ctx.Req.Header.Get("Authorization") + fmt.Println("auth:", baHead) if baHead == "" { authRequired(ctx) return @@ -121,6 +122,8 @@ func Http(ctx *middleware.Context) { return } + fmt.Println("auth2:", authUsername, passwd) + authUser, err = models.GetUserByName(authUsername) if err != nil { ctx.Handle(401, "no basic auth and digit auth", nil) @@ -134,6 +137,8 @@ func Http(ctx *middleware.Context) { return } + fmt.Println("passwd is right") + if !isPublicPull { var tp = models.WRITABLE if isPull { diff --git a/routers/repo/view.go b/routers/repo/view.go index e42894ae73..41fdaba08c 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" "strings" + "time" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/git" @@ -21,6 +22,15 @@ const ( HOME base.TplName = "repo/home" ) +type fakeCommit struct { + Id string + Summary string + Url string + Committer struct { + When time.Time + } +} + func Home(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Repo.Repository.Name @@ -127,13 +137,31 @@ func Home(ctx *middleware.Context) { files := make([][]interface{}, 0, len(entries)) for _, te := range entries { - c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name())) - if err != nil { - ctx.Handle(404, "GetCommitOfRelPath", err) - return - } + if te.Type != git.COMMIT { + c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name())) + if err != nil { + ctx.Handle(404, "GetCommitOfRelPath", err) + return + } + files = append(files, []interface{}{te, c}) + } else { + sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name())) + if err != nil { + ctx.Handle(404, "GetSubModule", err) + return + } - files = append(files, []interface{}{te, c}) + commit := git.Commit{ + Tree: *tree, + Id: te.Id, + Committer: &git.Signature{ + When: time.Now(), + }, + CommitMessage: sm.Url, + } + + files = append(files, []interface{}{te, &commit}) + } } ctx.Data["Files"] = files diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index ce46cfb275..e8628306c9 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -27,8 +27,20 @@ {{$entry := index $item 0}} {{$commit := index $item 1}} - - + {{if $entry.IsSubModule}} + + + + + {{$entry.Name}} @ {{ShortSha $commit.Id.String}} + + + {{$commit.Summary}} + + {{TimeSince $commit.Committer.When $.i18n.Lang}} + {{else}} + + {{$entry.Name}} @@ -37,6 +49,7 @@ {{$commit.Summary}} {{TimeSince $commit.Committer.When $.i18n.Lang}} + {{end}} {{end}}