From 3a81fdf092a39cc94f3bb896a42db8546bd5f39a Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 3 Nov 2015 22:49:06 -0500 Subject: rename fields --- modules/git/tree.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'modules/git/tree.go') diff --git a/modules/git/tree.go b/modules/git/tree.go index 27539f06..cc62a2d5 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -18,7 +18,7 @@ var ( // A tree is a flat directory listing. type Tree struct { - Id sha1 + ID sha1 repo *Repository // parent tree @@ -66,7 +66,7 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { if err != nil { return nil, err } - entry.Id = id + entry.ID = id pos += step + 1 // Skip half of sha1. step = bytes.IndexByte(data[pos:], '\n') @@ -100,7 +100,7 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) { return nil, err } - g, err = t.repo.getTree(te.Id) + g, err = t.repo.getTree(te.ID) if err != nil { return nil, err } @@ -117,7 +117,7 @@ func (t *Tree) ListEntries(relpath string) (Entries, error) { t.entriesParsed = true stdout, stderr, err := com.ExecCmdDirBytes(t.repo.Path, - "git", "ls-tree", t.Id.String()) + "git", "ls-tree", t.ID.String()) if err != nil { if strings.Contains(err.Error(), "exit status 128") { return nil, errors.New(strings.TrimSpace(string(stderr))) @@ -130,7 +130,7 @@ func (t *Tree) ListEntries(relpath string) (Entries, error) { func NewTree(repo *Repository, id sha1) *Tree { tree := new(Tree) - tree.Id = id + tree.ID = id tree.repo = repo return tree } -- cgit v1.2.3 From 3d14e73fd835f2a0ed4a22daa4c67df245be8103 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 20 Nov 2015 00:47:35 -0500 Subject: fix #1119 and data race in timming tasks --- models/repo.go | 63 ++++++++++++++++++++++++++++++++++++++++------------ modules/git/tree.go | 25 +++++++++++++++++++-- routers/user/home.go | 3 +++ 3 files changed, 75 insertions(+), 16 deletions(-) (limited to 'modules/git/tree.go') diff --git a/models/repo.go b/models/repo.go index 1521f1e0..80009409 100644 --- a/models/repo.go +++ b/models/repo.go @@ -17,6 +17,7 @@ import ( "regexp" "sort" "strings" + "sync" "time" "unicode/utf8" @@ -1377,20 +1378,54 @@ func RewriteRepositoryUpdateHook() error { }) } -var ( - // Prevent duplicate running tasks. - isMirrorUpdating = false - isGitFscking = false - isCheckingRepos = false +// statusPool represents a pool of status with true/false. +type statusPool struct { + lock sync.RWMutex + pool map[string]bool +} + +// Start sets value of given name to true in the pool. +func (p *statusPool) Start(name string) { + p.lock.Lock() + defer p.lock.Unlock() + + p.pool[name] = true +} + +// Stop sets value of given name to false in the pool. +func (p *statusPool) Stop(name string) { + p.lock.Lock() + defer p.lock.Unlock() + + p.pool[name] = false +} + +// IsRunning checks if value of given name is set to true in the pool. +func (p *statusPool) IsRunning(name string) bool { + p.lock.RLock() + defer p.lock.RUnlock() + + return p.pool[name] +} + +// Prevent duplicate running tasks. +var taskStatusPool = &statusPool{ + pool: make(map[string]bool), +} + +const ( + _MIRROR_UPDATE = "mirror_update" + _GIT_FSCK = "git_fsck" + _CHECK_REPOs = "check_repos" ) // MirrorUpdate checks and updates mirror repositories. func MirrorUpdate() { - if isMirrorUpdating { + if taskStatusPool.IsRunning(_MIRROR_UPDATE) { return } - isMirrorUpdating = true - defer func() { isMirrorUpdating = false }() + taskStatusPool.Start(_MIRROR_UPDATE) + defer taskStatusPool.Stop(_MIRROR_UPDATE) log.Trace("Doing: MirrorUpdate") @@ -1438,11 +1473,11 @@ func MirrorUpdate() { // GitFsck calls 'git fsck' to check repository health. func GitFsck() { - if isGitFscking { + if taskStatusPool.IsRunning(_GIT_FSCK) { return } - isGitFscking = true - defer func() { isGitFscking = false }() + taskStatusPool.Start(_GIT_FSCK) + defer taskStatusPool.Stop(_GIT_FSCK) log.Trace("Doing: GitFsck") @@ -1507,11 +1542,11 @@ func repoStatsCheck(checker *repoChecker) { } func CheckRepoStats() { - if isCheckingRepos { + if taskStatusPool.IsRunning(_CHECK_REPOs) { return } - isCheckingRepos = true - defer func() { isCheckingRepos = false }() + taskStatusPool.Start(_CHECK_REPOs) + defer taskStatusPool.Stop(_CHECK_REPOs) log.Trace("Doing: CheckRepoStats") diff --git a/modules/git/tree.go b/modules/git/tree.go index cc62a2d5..6cfdbf47 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -28,6 +28,28 @@ type Tree struct { entriesParsed bool } +var escapeChar = []byte("\\") + +func unescapeChars(in []byte) []byte { + if bytes.Index(in, escapeChar) == -1 { + return in + } + + endIdx := len(in) - 1 + isEscape := false + out := make([]byte, 0, endIdx+1) + for i := range in { + if in[i] == '\\' && i != endIdx { + isEscape = !isEscape + if isEscape { + continue + } + } + out = append(out, in[i]) + } + return out +} + // Parse tree information from the (uncompressed) raw // data from the tree object. func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { @@ -74,8 +96,7 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { // In case entry name is surrounded by double quotes(it happens only in git-shell). if entry.name[0] == '"' { - entry.name = string(data[pos+1 : pos+step-1]) - entry.name = strings.Replace(entry.name, `\"`, `"`, -1) + entry.name = string(unescapeChars(data[pos+1 : pos+step-1])) } pos += step + 1 diff --git a/routers/user/home.go b/routers/user/home.go index 98033fc1..96202ed0 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -319,6 +319,9 @@ func Profile(ctx *middleware.Context) { if uname == "favicon.ico" { ctx.Redirect(setting.AppSubUrl + "/img/favicon.png") return + } else if strings.HasSuffix(uname, ".png") { + ctx.Error(404) + return } isShowKeys := false -- cgit v1.2.3 From 902b5784659327a61ba7de56ef1885fcc6549b17 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 20 Nov 2015 01:18:50 -0500 Subject: better escape char handle --- .bra.toml | 1 - models/git_diff.go | 10 +++++----- modules/git/tree.go | 18 +++++++++--------- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'modules/git/tree.go') diff --git a/.bra.toml b/.bra.toml index f1da310a..7d49786d 100644 --- a/.bra.toml +++ b/.bra.toml @@ -13,7 +13,6 @@ watch_dirs = [ watch_exts = [".go"] build_delay = 1500 cmds = [ - ["go", "install"], ["go", "install", "-race"], # sqlite redis memcache cert pam tidb ["go", "build", "-race"], ["./gogs", "web"] diff --git a/models/git_diff.go b/models/git_diff.go index 4cc9ebf8..4b1ec09e 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -163,10 +163,10 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff if strings.HasPrefix(line, DIFF_HEAD) { middle := -1 - // Note: In case file name is surrounded by double quotes(it happens only in git-shell). - hasQuote := strings.Index(line, `\"`) > -1 + // Note: In case file name is surrounded by double quotes (it happens only in git-shell). + // e.g. diff --git "a/xxx" "b/xxx" + hasQuote := line[len(DIFF_HEAD)] == '"' if hasQuote { - line = strings.Replace(line, `\"`, `"`, -1) middle = strings.Index(line, ` "b/`) } else { middle = strings.Index(line, " b/") @@ -176,8 +176,8 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff a := line[beg+2 : middle] b := line[middle+3:] if hasQuote { - a = a[1 : len(a)-1] - b = b[1 : len(b)-1] + a = string(git.UnescapeChars([]byte(a[1 : len(a)-1]))) + b = string(git.UnescapeChars([]byte(b[1 : len(b)-1]))) } curFile = &DiffFile{ diff --git a/modules/git/tree.go b/modules/git/tree.go index 6cfdbf47..1a561cf5 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -30,7 +30,7 @@ type Tree struct { var escapeChar = []byte("\\") -func unescapeChars(in []byte) []byte { +func UnescapeChars(in []byte) []byte { if bytes.Index(in, escapeChar) == -1 { return in } @@ -39,12 +39,11 @@ func unescapeChars(in []byte) []byte { isEscape := false out := make([]byte, 0, endIdx+1) for i := range in { - if in[i] == '\\' && i != endIdx { - isEscape = !isEscape - if isEscape { - continue - } + if in[i] == '\\' && !isEscape { + isEscape = true + continue } + isEscape = false out = append(out, in[i]) } return out @@ -92,11 +91,12 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { pos += step + 1 // Skip half of sha1. step = bytes.IndexByte(data[pos:], '\n') - entry.name = string(data[pos : pos+step]) // In case entry name is surrounded by double quotes(it happens only in git-shell). - if entry.name[0] == '"' { - entry.name = string(unescapeChars(data[pos+1 : pos+step-1])) + if data[pos] == '"' { + entry.name = string(UnescapeChars(data[pos+1 : pos+step-1])) + } else { + entry.name = string(data[pos : pos+step]) } pos += step + 1 -- cgit v1.2.3