<commit_before>package main

import (
	"encoding/xml"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"
)

func main() {
	convert(os.Stdin, os.Stdout)
}

func convert(in io.Reader, out io.Writer) {
	profiles, err := ParseProfiles(in)
	if err != nil {
		panic("Can't parse profiles")
	}

	srcDirs := build.Default.SrcDirs()
	sources := make([]*Source, len(srcDirs))
	for i, dir := range srcDirs {
		sources[i] = &Source{dir}
	}

	coverage := Coverage{Sources: sources, Packages: nil, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}
	coverage.parseProfiles(profiles)

	fmt.Fprintf(out, xml.Header)
	fmt.Fprintf(out, "<!DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/coverage-03.dtd\">\n")

	encoder := xml.NewEncoder(out)
	encoder.Indent("", "\t")
	err = encoder.Encode(coverage)
	if err != nil {
		panic(err)
	}

	fmt.Fprintln(out)
}

func (cov *Coverage) parseProfiles(profiles []*Profile) error {
	cov.Packages = []*Package{}
	for _, profile := range profiles {
		cov.parseProfile(profile)
	}
	return nil
}

func (cov *Coverage) parseProfile(profile *Profile) error {
	fileName := profile.FileName
	absFilePath, err := findFile(fileName)
	if err != nil {
		return err
	}
	fset := token.NewFileSet()
	parsed, err := parser.ParseFile(fset, absFilePath, nil, 0)
	if err != nil {
		return err
	}
	data, err := ioutil.ReadFile(absFilePath)
	if err != nil {
		return err
	}

	pkgPath, _ := filepath.Split(fileName)
	pkgPath = strings.TrimRight(pkgPath, string(os.PathSeparator))

	var pkg *Package
	for _, p := range cov.Packages {
		if p.Name == pkgPath {
			pkg = p
		}
	}
	if pkg == nil {
		pkg = &Package{Name: pkgPath, Classes: []*Class{}}
		cov.Packages = append(cov.Packages, pkg)
	}
	visitor := &fileVisitor{
		fset:     fset,
		fileName: fileName,
		fileData: data,
		classes:  make(map[string]*Class),
		pkg:      pkg,
		profile:  profile,
	}
	ast.Walk(visitor, parsed)
	return nil
}

type fileVisitor struct {
	fset     *token.FileSet
	fileName string
	fileData []byte
	pkg      *Package
	classes  map[string]*Class
	profile  *Profile
}

func (v *fileVisitor) Visit(node ast.Node) ast.Visitor {
	switch n := node.(type) {
	case *ast.FuncDecl:
		class := v.class(n)
		method := v.method(n)
		class.Methods = append(class.Methods, method)
		for _, line := range method.Lines {
			class.Lines = append(class.Lines, line)
		}
	}
	return v
}

func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
	method := &Method{Name: n.Name.Name}
	method.Lines = []*Line{}

	start := v.fset.Position(n.Pos())
	end := v.fset.Position(n.End())
	startLine := start.Line
	startCol := start.Column
	endLine := end.Line
	endCol := end.Column
	// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
	for _, b := range v.profile.Blocks {
		if b.StartLine > endLine || (b.StartLine == endLine && b.StartCol >= endCol) {
			// Past the end of the function.
			break
		}
		if b.EndLine < startLine || (b.EndLine == startLine && b.EndCol <= startCol) {
			// Before the beginning of the function
			continue
		}
		for i := b.StartLine; i <= b.EndLine; i++ {
			method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
		}
	}
	return method
}

func (v *fileVisitor) class(n *ast.FuncDecl) *Class {
	className := v.recvName(n)
	var class *Class = v.classes[className]
	if class == nil {
		class = &Class{Name: className, Filename: v.fileName, Methods: []*Method{}, Lines: []*Line{}}
		v.classes[className] = class
		v.pkg.Classes = append(v.pkg.Classes, class)
	}
	return class
}

func (v *fileVisitor) recvName(n *ast.FuncDecl) string {
	if n.Recv == nil {
		return "-"
	}
	recv := n.Recv.List[0].Type
	start := v.fset.Position(recv.Pos())
	end := v.fset.Position(recv.End())
	name := string(v.fileData[start.Offset:end.Offset])
	return strings.TrimSpace(strings.TrimLeft(name, "*"))
}
<commit_msg>Update XML DOCTYPE<commit_after>package main

import (
	"encoding/xml"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"
)

func main() {
	convert(os.Stdin, os.Stdout)
}

func convert(in io.Reader, out io.Writer) {
	profiles, err := ParseProfiles(in)
	if err != nil {
		panic("Can't parse profiles")
	}

	srcDirs := build.Default.SrcDirs()
	sources := make([]*Source, len(srcDirs))
	for i, dir := range srcDirs {
		sources[i] = &Source{dir}
	}

	coverage := Coverage{Sources: sources, Packages: nil, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}
	coverage.parseProfiles(profiles)

	fmt.Fprintf(out, xml.Header)
	fmt.Fprintf(out, "<!DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/coverage-04.dtd\">\n")

	encoder := xml.NewEncoder(out)
	encoder.Indent("", "\t")
	err = encoder.Encode(coverage)
	if err != nil {
		panic(err)
	}

	fmt.Fprintln(out)
}

func (cov *Coverage) parseProfiles(profiles []*Profile) error {
	cov.Packages = []*Package{}
	for _, profile := range profiles {
		cov.parseProfile(profile)
	}
	return nil
}

func (cov *Coverage) parseProfile(profile *Profile) error {
	fileName := profile.FileName
	absFilePath, err := findFile(fileName)
	if err != nil {
		return err
	}
	fset := token.NewFileSet()
	parsed, err := parser.ParseFile(fset, absFilePath, nil, 0)
	if err != nil {
		return err
	}
	data, err := ioutil.ReadFile(absFilePath)
	if err != nil {
		return err
	}

	pkgPath, _ := filepath.Split(fileName)
	pkgPath = strings.TrimRight(pkgPath, string(os.PathSeparator))

	var pkg *Package
	for _, p := range cov.Packages {
		if p.Name == pkgPath {
			pkg = p
		}
	}
	if pkg == nil {
		pkg = &Package{Name: pkgPath, Classes: []*Class{}}
		cov.Packages = append(cov.Packages, pkg)
	}
	visitor := &fileVisitor{
		fset:     fset,
		fileName: fileName,
		fileData: data,
		classes:  make(map[string]*Class),
		pkg:      pkg,
		profile:  profile,
	}
	ast.Walk(visitor, parsed)
	return nil
}

type fileVisitor struct {
	fset     *token.FileSet
	fileName string
	fileData []byte
	pkg      *Package
	classes  map[string]*Class
	profile  *Profile
}

func (v *fileVisitor) Visit(node ast.Node) ast.Visitor {
	switch n := node.(type) {
	case *ast.FuncDecl:
		class := v.class(n)
		method := v.method(n)
		class.Methods = append(class.Methods, method)
		for _, line := range method.Lines {
			class.Lines = append(class.Lines, line)
		}
	}
	return v
}

func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
	method := &Method{Name: n.Name.Name}
	method.Lines = []*Line{}

	start := v.fset.Position(n.Pos())
	end := v.fset.Position(n.End())
	startLine := start.Line
	startCol := start.Column
	endLine := end.Line
	endCol := end.Column
	// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
	for _, b := range v.profile.Blocks {
		if b.StartLine > endLine || (b.StartLine == endLine && b.StartCol >= endCol) {
			// Past the end of the function.
			break
		}
		if b.EndLine < startLine || (b.EndLine == startLine && b.EndCol <= startCol) {
			// Before the beginning of the function
			continue
		}
		for i := b.StartLine; i <= b.EndLine; i++ {
			method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
		}
	}
	return method
}

func (v *fileVisitor) class(n *ast.FuncDecl) *Class {
	className := v.recvName(n)
	var class *Class = v.classes[className]
	if class == nil {
		class = &Class{Name: className, Filename: v.fileName, Methods: []*Method{}, Lines: []*Line{}}
		v.classes[className] = class
		v.pkg.Classes = append(v.pkg.Classes, class)
	}
	return class
}

func (v *fileVisitor) recvName(n *ast.FuncDecl) string {
	if n.Recv == nil {
		return "-"
	}
	recv := n.Recv.List[0].Type
	start := v.fset.Position(recv.Pos())
	end := v.fset.Position(recv.End())
	name := string(v.fileData[start.Offset:end.Offset])
	return strings.TrimSpace(strings.TrimLeft(name, "*"))
}
<|endoftext|>


<commit_before>package ghooks

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

var count int
var some_string string

func Push(paylod interface{}) {
	count++
}

func Push2(payload interface{}) {
	some_string = payload.(map[string]interface{})["fuga"].(string)
}

func PullRequest(paylod interface{}) {
	count += 2
}

func TestEmmit(t *testing.T) {
	On("push", Push)
	On("pull_request", PullRequest)
	On("push2", Push2)

	var payload interface{}
	Emmit("push", payload)

	if count != 1 {
		t.Fatal("Not call push Event")
	}

	Emmit("pull_request", payload)
	if count != 3 {
		t.Fatal("Not call pull_request Event")

	}

	b := []byte(`{"fuga": "hoge"}`)
	dec := json.NewDecoder(bytes.NewReader(b))
	dec.Decode(&payload)
	Emmit("push2", payload)
	if !strings.EqualFold(some_string, "hoge") {
		t.Fatal("Cannot  access payload")
	}

}

func TestReciver(t *testing.T) {
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	w := httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Allowd only POST Method but expected status 200; received %d", w.Code)
	}

	req, _ = http.NewRequest("POST", "/", nil)
	req.Header.Add("X-GitHub-Event", "")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Event name is nil but return 200; received %d", w.Code)
	}

	req, _ = http.NewRequest("POST", "/", nil)
	req.Header.Set("X-GitHub-Event", "hoge")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Body is nil but return 200; received %d", w.Code)
	}

	json_string := `{"fuga": "hoge", "foo": { "bar": "boo" }}`
	req, _ = http.NewRequest("POST", "/", strings.NewReader(json_string))
	req.Header.Set("X-GitHub-Event", "hoge")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code != 200 {
		t.Fatalf("Not return 200; received %d", w.Code)
	}
}
<commit_msg>Fix test case<commit_after>package ghooks

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

var count int
var some_string string

func Push(paylod interface{}) {
	count++
}

func Push2(payload interface{}) {
	some_string = payload.(map[string]interface{})["fuga"].(string)
}

func PullRequest(paylod interface{}) {
	count += 2
}

func TestEmmit(t *testing.T) {
	hooks := NewServer(999999)
	hooks.On("push", Push)
	hooks.On("pull_request", PullRequest)
	hooks.On("push2", Push2)

	var payload interface{}
	Emmit("push", payload)

	if count != 1 {
		t.Fatal("Not call push Event")
	}

	Emmit("pull_request", payload)
	if count != 3 {
		t.Fatal("Not call pull_request Event")

	}

	b := []byte(`{"fuga": "hoge"}`)
	dec := json.NewDecoder(bytes.NewReader(b))
	dec.Decode(&payload)
	Emmit("push2", payload)
	if !strings.EqualFold(some_string, "hoge") {
		t.Fatal("Cannot  access payload")
	}

}

func TestReciver(t *testing.T) {
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	w := httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Allowd only POST Method but expected status 200; received %d", w.Code)
	}

	req, _ = http.NewRequest("POST", "/", nil)
	req.Header.Add("X-GitHub-Event", "")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Event name is nil but return 200; received %d", w.Code)
	}

	req, _ = http.NewRequest("POST", "/", nil)
	req.Header.Set("X-GitHub-Event", "hoge")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code == 200 {
		t.Fatalf("Body is nil but return 200; received %d", w.Code)
	}

	json_string := `{"fuga": "hoge", "foo": { "bar": "boo" }}`
	req, _ = http.NewRequest("POST", "/", strings.NewReader(json_string))
	req.Header.Set("X-GitHub-Event", "hoge")
	w = httptest.NewRecorder()
	Reciver(w, req)
	if w.Code != 200 {
		t.Fatalf("Not return 200; received %d", w.Code)
	}
}
<|endoftext|>


<commit_before>package git

import (
	"bufio"
	"bytes"
	"os"
	"path/filepath"
	"strings"

	"github.com/git-lfs/git-lfs/tools"
	"github.com/rubyist/tracerx"
)

const (
	LockableAttrib = "lockable"
)

// AttributePath is a path entry in a gitattributes file which has the LFS filter
type AttributePath struct {
	// Path entry in the attribute file
	Path string
	// The attribute file which was the source of this entry
	Source *AttributeSource
	// Path also has the 'lockable' attribute
	Lockable bool
}

type AttributeSource struct {
	Path       string
	LineEnding string
}

func (s *AttributeSource) String() string {
	return s.Path
}

// GetAttributePaths returns a list of entries in .gitattributes which are
// configured with the filter=lfs attribute
// workingDIr is the root of the working copy
// gitDir is the root of the git repo
func GetAttributePaths(workingDir, gitDir string) []AttributePath {
	paths := make([]AttributePath, 0)

	for _, path := range findAttributeFiles(workingDir, gitDir) {
		attributes, err := os.Open(path)
		if err != nil {
			continue
		}

		relfile, _ := filepath.Rel(workingDir, path)
		reldir := filepath.Dir(relfile)
		source := &AttributeSource{Path: relfile}

		le := &lineEndingSplitter{}
		scanner := bufio.NewScanner(attributes)
		scanner.Split(le.ScanLines)

		for scanner.Scan() {
			line := scanner.Text()
			if strings.Contains(line, "filter=lfs") {
				fields := strings.Fields(line)
				pattern := fields[0]
				if len(reldir) > 0 {
					pattern = filepath.Join(reldir, pattern)
				}
				// Find lockable flag in any position after pattern to avoid
				// edge case of matching "lockable" to a file pattern
				lockable := false
				for _, f := range fields[1:] {
					if f == LockableAttrib {
						lockable = true
						break
					}
				}
				paths = append(paths, AttributePath{
					Path:     pattern,
					Source:   source,
					Lockable: lockable,
				})
			}
		}

		source.LineEnding = le.LineEnding()
	}

	return paths
}

// copies bufio.ScanLines(), counting LF vs CRLF in a file
type lineEndingSplitter struct {
	LFCount   int
	CRLFCount int
}

func (s *lineEndingSplitter) LineEnding() string {
	if s.CRLFCount > s.LFCount {
		return "\r\n"
	} else if s.LFCount == 0 {
		return ""
	}
	return "\n"
}

func (s *lineEndingSplitter) ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, s.dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), data, nil
	}
	// Request more data.
	return 0, nil, nil
}

// dropCR drops a terminal \r from the data.
func (s *lineEndingSplitter) dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		s.CRLFCount++
		return data[0 : len(data)-1]
	}
	s.LFCount++
	return data
}

func findAttributeFiles(workingDir, gitDir string) []string {
	var paths []string

	repoAttributes := filepath.Join(gitDir, "info", "attributes")
	if info, err := os.Stat(repoAttributes); err == nil && !info.IsDir() {
		paths = append(paths, repoAttributes)
	}

	tools.FastWalkGitRepo(workingDir, func(parentDir string, info os.FileInfo, err error) {
		if err != nil {
			tracerx.Printf("Error finding .gitattributes: %v", err)
			return
		}

		if info.IsDir() || info.Name() != ".gitattributes" {
			return
		}
		paths = append(paths, filepath.Join(parentDir, info.Name()))
	})

	// reverse the order of the files so more specific entries are found first
	// when iterating from the front (respects precedence)
	for i, j := 0, len(paths)-1; i < j; i, j = i+1, j-1 {
		paths[i], paths[j] = paths[j], paths[i]
	}

	return paths
}
<commit_msg>git: expand 'GetAttributePaths' check to include non-LFS lockables<commit_after>package git

import (
	"bufio"
	"bytes"
	"os"
	"path/filepath"
	"strings"

	"github.com/git-lfs/git-lfs/tools"
	"github.com/rubyist/tracerx"
)

const (
	LockableAttrib = "lockable"
)

// AttributePath is a path entry in a gitattributes file which has the LFS filter
type AttributePath struct {
	// Path entry in the attribute file
	Path string
	// The attribute file which was the source of this entry
	Source *AttributeSource
	// Path also has the 'lockable' attribute
	Lockable bool
}

type AttributeSource struct {
	Path       string
	LineEnding string
}

func (s *AttributeSource) String() string {
	return s.Path
}

// GetAttributePaths returns a list of entries in .gitattributes which are
// configured with the filter=lfs attribute
// workingDIr is the root of the working copy
// gitDir is the root of the git repo
func GetAttributePaths(workingDir, gitDir string) []AttributePath {
	paths := make([]AttributePath, 0)

	for _, path := range findAttributeFiles(workingDir, gitDir) {
		attributes, err := os.Open(path)
		if err != nil {
			continue
		}

		relfile, _ := filepath.Rel(workingDir, path)
		reldir := filepath.Dir(relfile)
		source := &AttributeSource{Path: relfile}

		le := &lineEndingSplitter{}
		scanner := bufio.NewScanner(attributes)
		scanner.Split(le.ScanLines)

		for scanner.Scan() {
			line := scanner.Text()

			// Check for filter=lfs (signifying that LFS is tracking
			// this file) or "lockable", which indicates that the
			// file is lockable (and may or may not be tracked by
			// Git LFS).
			if strings.Contains(line, "filter=lfs") ||
				strings.HasSuffix(line, "lockable") {

				fields := strings.Fields(line)
				pattern := fields[0]
				if len(reldir) > 0 {
					pattern = filepath.Join(reldir, pattern)
				}
				// Find lockable flag in any position after pattern to avoid
				// edge case of matching "lockable" to a file pattern
				lockable := false
				for _, f := range fields[1:] {
					if f == LockableAttrib {
						lockable = true
						break
					}
				}
				paths = append(paths, AttributePath{
					Path:     pattern,
					Source:   source,
					Lockable: lockable,
				})
			}
		}

		source.LineEnding = le.LineEnding()
	}

	return paths
}

// copies bufio.ScanLines(), counting LF vs CRLF in a file
type lineEndingSplitter struct {
	LFCount   int
	CRLFCount int
}

func (s *lineEndingSplitter) LineEnding() string {
	if s.CRLFCount > s.LFCount {
		return "\r\n"
	} else if s.LFCount == 0 {
		return ""
	}
	return "\n"
}

func (s *lineEndingSplitter) ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, s.dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), data, nil
	}
	// Request more data.
	return 0, nil, nil
}

// dropCR drops a terminal \r from the data.
func (s *lineEndingSplitter) dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		s.CRLFCount++
		return data[0 : len(data)-1]
	}
	s.LFCount++
	return data
}

func findAttributeFiles(workingDir, gitDir string) []string {
	var paths []string

	repoAttributes := filepath.Join(gitDir, "info", "attributes")
	if info, err := os.Stat(repoAttributes); err == nil && !info.IsDir() {
		paths = append(paths, repoAttributes)
	}

	tools.FastWalkGitRepo(workingDir, func(parentDir string, info os.FileInfo, err error) {
		if err != nil {
			tracerx.Printf("Error finding .gitattributes: %v", err)
			return
		}

		if info.IsDir() || info.Name() != ".gitattributes" {
			return
		}
		paths = append(paths, filepath.Join(parentDir, info.Name()))
	})

	// reverse the order of the files so more specific entries are found first
	// when iterating from the front (respects precedence)
	for i, j := 0, len(paths)-1; i < j; i, j = i+1, j-1 {
		paths[i], paths[j] = paths[j], paths[i]
	}

	return paths
}
<|endoftext|>


<commit_before>package git

import (
	"context"
	"errors"
	"os"
	"path/filepath"
)

var (
	ErrReadOnly = errors.New("cannot make a working clone of a read-only git repo")
)

// Config holds some values we use when working in the working clone of
// a repo.
type Config struct {
	Branch      string   // branch we're syncing to
	Paths       []string // paths within the repo containing files we care about
	SyncTag     string
	NotesRef    string
	UserName    string
	UserEmail   string
	SigningKey  string
	SetAuthor   bool
	SkipMessage string
}

// Checkout is a local working clone of the remote repo. It is
// intended to be used for one-off "transactions", e.g,. committing
// changes then pushing upstream. It has no locking.
type Checkout struct {
	dir          string
	config       Config
	upstream     Remote
	realNotesRef string // cache the notes ref, since we use it to push as well
}

type Commit struct {
	Signature Signature
	Revision  string
	Message   string
}

// CommitAction - struct holding commit information
type CommitAction struct {
	Author     string
	Message    string
	SigningKey string
}

// TagAction - struct holding tag information
type TagAction struct {
	Revision   string
	Message    string
	SigningKey string
}

// Clone returns a local working clone of the sync'ed `*Repo`, using
// the config given.
func (r *Repo) Clone(ctx context.Context, conf Config) (*Checkout, error) {
	if r.readonly {
		return nil, ErrReadOnly
	}

	upstream := r.Origin()
	repoDir, err := r.workingClone(ctx, conf.Branch)
	if err != nil {
		return nil, err
	}

	if err := config(ctx, repoDir, conf.UserName, conf.UserEmail); err != nil {
		os.RemoveAll(repoDir)
		return nil, err
	}

	// We'll need the notes ref for pushing it, so make sure we have
	// it. This assumes we're syncing it (otherwise we'll likely get conflicts)
	realNotesRef, err := getNotesRef(ctx, repoDir, conf.NotesRef)
	if err != nil {
		os.RemoveAll(repoDir)
		return nil, err
	}

	r.mu.RLock()
	if err := fetch(ctx, repoDir, r.dir, realNotesRef+":"+realNotesRef); err != nil {
		os.RemoveAll(repoDir)
		r.mu.RUnlock()
		return nil, err
	}
	r.mu.RUnlock()

	return &Checkout{
		dir:          repoDir,
		upstream:     upstream,
		realNotesRef: realNotesRef,
		config:       conf,
	}, nil
}

// Clean a Checkout up (remove the clone)
func (c *Checkout) Clean() {
	if c.dir != "" {
		os.RemoveAll(c.dir)
	}
}

// Dir returns the path to the repo
func (c *Checkout) Dir() string {
	return c.dir
}

// ManifestDirs returns the paths to the manifests files. It ensures
// that at least one path is returned, so that it can be used with
// `Manifest.LoadManifests`.
func (c *Checkout) ManifestDirs() []string {
	if len(c.config.Paths) == 0 {
		return []string{c.dir}
	}

	paths := make([]string, len(c.config.Paths), len(c.config.Paths))
	for i, p := range c.config.Paths {
		paths[i] = filepath.Join(c.dir, p)
	}
	return paths
}

// CommitAndPush commits changes made in this checkout, along with any
// extra data as a note, and pushes the commit and note to the remote repo.
func (c *Checkout) CommitAndPush(ctx context.Context, commitAction CommitAction, note interface{}, addUntracked bool) error {
	if addUntracked {
		if err := add(ctx, c.dir, "."); err != nil {
			return err
		}
	}

	if !check(ctx, c.dir, c.config.Paths, addUntracked) {
		return ErrNoChanges
	}

	commitAction.Message += c.config.SkipMessage
	if commitAction.SigningKey == "" {
		commitAction.SigningKey = c.config.SigningKey
	}

	if err := commit(ctx, c.dir, commitAction); err != nil {
		return err
	}

	if note != nil {
		rev, err := c.HeadRevision(ctx)
		if err != nil {
			return err
		}
		if err := addNote(ctx, c.dir, rev, c.config.NotesRef, note); err != nil {
			return err
		}
	}

	refs := []string{c.config.Branch}
	ok, err := refExists(ctx, c.dir, c.realNotesRef)
	if ok {
		refs = append(refs, c.realNotesRef)
	} else if err != nil {
		return err
	}

	if err := push(ctx, c.dir, c.upstream.URL, refs); err != nil {
		return PushError(c.upstream.URL, err)
	}
	return nil
}

// GetNote gets a note for the revision specified, or nil if there is no such note.
func (c *Checkout) GetNote(ctx context.Context, rev string, note interface{}) (bool, error) {
	return getNote(ctx, c.dir, c.realNotesRef, rev, note)
}

func (c *Checkout) HeadRevision(ctx context.Context) (string, error) {
	return refRevision(ctx, c.dir, "HEAD")
}

func (c *Checkout) SyncRevision(ctx context.Context) (string, error) {
	return refRevision(ctx, c.dir, "tags/"+c.config.SyncTag)
}

func (c *Checkout) MoveSyncTagAndPush(ctx context.Context, tagAction TagAction) error {
	if tagAction.SigningKey == "" {
		tagAction.SigningKey = c.config.SigningKey
	}
	return moveTagAndPush(ctx, c.dir, c.config.SyncTag, c.upstream.URL, tagAction)
}

func (c *Checkout) VerifySyncTag(ctx context.Context) (string, error) {
	return verifyTag(ctx, c.dir, c.config.SyncTag)
}

// ChangedFiles does a git diff listing changed files
func (c *Checkout) ChangedFiles(ctx context.Context, ref string) ([]string, error) {
	list, err := changed(ctx, c.dir, ref, c.config.Paths)
	if err == nil {
		for i, file := range list {
			list[i] = filepath.Join(c.dir, file)
		}
	}
	return list, err
}

func (c *Checkout) NoteRevList(ctx context.Context) (map[string]struct{}, error) {
	return noteRevList(ctx, c.dir, c.realNotesRef)
}

func (c *Checkout) Checkout(ctx context.Context, rev string) error {
	return checkout(ctx, c.dir, rev)
}

func (c *Checkout) Add(ctx context.Context, path string) error {
	return add(ctx, c.dir, path)
}
<commit_msg>Force fetch tags on checkout local working clone (#2184)<commit_after>package git

import (
	"context"
	"errors"
	"os"
	"path/filepath"
)

var (
	ErrReadOnly = errors.New("cannot make a working clone of a read-only git repo")
)

// Config holds some values we use when working in the working clone of
// a repo.
type Config struct {
	Branch      string   // branch we're syncing to
	Paths       []string // paths within the repo containing files we care about
	SyncTag     string
	NotesRef    string
	UserName    string
	UserEmail   string
	SigningKey  string
	SetAuthor   bool
	SkipMessage string
}

// Checkout is a local working clone of the remote repo. It is
// intended to be used for one-off "transactions", e.g,. committing
// changes then pushing upstream. It has no locking.
type Checkout struct {
	dir          string
	config       Config
	upstream     Remote
	realNotesRef string // cache the notes ref, since we use it to push as well
}

type Commit struct {
	Signature Signature
	Revision  string
	Message   string
}

// CommitAction - struct holding commit information
type CommitAction struct {
	Author     string
	Message    string
	SigningKey string
}

// TagAction - struct holding tag information
type TagAction struct {
	Revision   string
	Message    string
	SigningKey string
}

// Clone returns a local working clone of the sync'ed `*Repo`, using
// the config given.
func (r *Repo) Clone(ctx context.Context, conf Config) (*Checkout, error) {
	if r.readonly {
		return nil, ErrReadOnly
	}

	upstream := r.Origin()
	repoDir, err := r.workingClone(ctx, conf.Branch)
	if err != nil {
		return nil, err
	}

	if err := config(ctx, repoDir, conf.UserName, conf.UserEmail); err != nil {
		os.RemoveAll(repoDir)
		return nil, err
	}

	// We'll need the notes ref for pushing it, so make sure we have
	// it. This assumes we're syncing it (otherwise we'll likely get conflicts)
	realNotesRef, err := getNotesRef(ctx, repoDir, conf.NotesRef)
	if err != nil {
		os.RemoveAll(repoDir)
		return nil, err
	}

	r.mu.RLock()
	// Here is where we mimic `git fetch --tags --force`, but
	// _without_ overwriting head refs. This is only required for a
	// `Checkout` and _not_ for `Repo` as (bare) mirrors will happily
	// accept any ref changes to tags.
	//
	// NB: do this before any other fetch actions, as otherwise we may
	// get an 'existing tag clobber' error back.
	if err := fetch(ctx, repoDir, r.dir, `'+refs/tags/*:refs/tags/*'`); err != nil {
		os.RemoveAll(repoDir)
		r.mu.RUnlock()
		return nil, err
	}
	if err := fetch(ctx, repoDir, r.dir, realNotesRef+":"+realNotesRef); err != nil {
		os.RemoveAll(repoDir)
		r.mu.RUnlock()
		return nil, err
	}
	r.mu.RUnlock()

	return &Checkout{
		dir:          repoDir,
		upstream:     upstream,
		realNotesRef: realNotesRef,
		config:       conf,
	}, nil
}

// Clean a Checkout up (remove the clone)
func (c *Checkout) Clean() {
	if c.dir != "" {
		os.RemoveAll(c.dir)
	}
}

// Dir returns the path to the repo
func (c *Checkout) Dir() string {
	return c.dir
}

// ManifestDirs returns the paths to the manifests files. It ensures
// that at least one path is returned, so that it can be used with
// `Manifest.LoadManifests`.
func (c *Checkout) ManifestDirs() []string {
	if len(c.config.Paths) == 0 {
		return []string{c.dir}
	}

	paths := make([]string, len(c.config.Paths), len(c.config.Paths))
	for i, p := range c.config.Paths {
		paths[i] = filepath.Join(c.dir, p)
	}
	return paths
}

// CommitAndPush commits changes made in this checkout, along with any
// extra data as a note, and pushes the commit and note to the remote repo.
func (c *Checkout) CommitAndPush(ctx context.Context, commitAction CommitAction, note interface{}, addUntracked bool) error {
	if addUntracked {
		if err := add(ctx, c.dir, "."); err != nil {
			return err
		}
	}

	if !check(ctx, c.dir, c.config.Paths, addUntracked) {
		return ErrNoChanges
	}

	commitAction.Message += c.config.SkipMessage
	if commitAction.SigningKey == "" {
		commitAction.SigningKey = c.config.SigningKey
	}

	if err := commit(ctx, c.dir, commitAction); err != nil {
		return err
	}

	if note != nil {
		rev, err := c.HeadRevision(ctx)
		if err != nil {
			return err
		}
		if err := addNote(ctx, c.dir, rev, c.config.NotesRef, note); err != nil {
			return err
		}
	}

	refs := []string{c.config.Branch}
	ok, err := refExists(ctx, c.dir, c.realNotesRef)
	if ok {
		refs = append(refs, c.realNotesRef)
	} else if err != nil {
		return err
	}

	if err := push(ctx, c.dir, c.upstream.URL, refs); err != nil {
		return PushError(c.upstream.URL, err)
	}
	return nil
}

// GetNote gets a note for the revision specified, or nil if there is no such note.
func (c *Checkout) GetNote(ctx context.Context, rev string, note interface{}) (bool, error) {
	return getNote(ctx, c.dir, c.realNotesRef, rev, note)
}

func (c *Checkout) HeadRevision(ctx context.Context) (string, error) {
	return refRevision(ctx, c.dir, "HEAD")
}

func (c *Checkout) SyncRevision(ctx context.Context) (string, error) {
	return refRevision(ctx, c.dir, "tags/"+c.config.SyncTag)
}

func (c *Checkout) MoveSyncTagAndPush(ctx context.Context, tagAction TagAction) error {
	if tagAction.SigningKey == "" {
		tagAction.SigningKey = c.config.SigningKey
	}
	return moveTagAndPush(ctx, c.dir, c.config.SyncTag, c.upstream.URL, tagAction)
}

func (c *Checkout) VerifySyncTag(ctx context.Context) (string, error) {
	return verifyTag(ctx, c.dir, c.config.SyncTag)
}

// ChangedFiles does a git diff listing changed files
func (c *Checkout) ChangedFiles(ctx context.Context, ref string) ([]string, error) {
	list, err := changed(ctx, c.dir, ref, c.config.Paths)
	if err == nil {
		for i, file := range list {
			list[i] = filepath.Join(c.dir, file)
		}
	}
	return list, err
}

func (c *Checkout) NoteRevList(ctx context.Context) (map[string]struct{}, error) {
	return noteRevList(ctx, c.dir, c.realNotesRef)
}

func (c *Checkout) Checkout(ctx context.Context, rev string) error {
	return checkout(ctx, c.dir, rev)
}

func (c *Checkout) Add(ctx context.Context, path string) error {
	return add(ctx, c.dir, path)
}
<|endoftext|>


<commit_before>package main

import "fmt"

var x int = 123
var inc int = 312
func main()  {
	fmt.Println(x)
	foo()

	// block
	y := 42
	fmt.Println(y)
	{
		fmt.Println(y) // Y can be accessed in inner scope
		z := "Lords of the Ring : The Return of the King"
		fmt.Println(z)
	}
	// fmt.Println(z)  - Not available outside of the block

	fmt.Println(increment())
	fmt.Println(increment())
}

func foo()  {
	fmt.Println(x)
}

func increment()  int {
	inc++
	return inc
}<commit_msg>Did go fmt<commit_after>package main

import "fmt"

var x int = 123
var inc int = 312

func main() {
	fmt.Println(x)
	foo()

	// block
	y := 42
	fmt.Println(y)
	{
		fmt.Println(y) // Y can be accessed in inner scope or you can initialize new one also.
		z := "Lords of the Ring : The Return of the King"
		fmt.Println(z)
	}
	// fmt.Println(z)  - Not available outside of the block

	fmt.Println(increment())
	fmt.Println(increment())
}

func foo() {
	fmt.Println(x)
}

func increment() int {
	inc++
	return inc
}
<|endoftext|>


<commit_before>// Copyright 2015 Tamás Gulácsi. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package olc

import (
	"bytes"
	"io/ioutil"
	"math"
	"path/filepath"
	"strconv"
	"sync"
	"testing"
)

var (
	validity []validityTest
	encoding []encodingTest
	shorten  []shortenTest
)

type (
	validityTest struct {
		code                     string
		isValid, isShort, isFull bool
	}

	encodingTest struct {
		code                                 string
		lat, lng, latLo, lngLo, latHi, lngHi float64
	}

	shortenTest struct {
		code     string
		lat, lng float64
		short    string
		tType    string
	}
)

func init() {
	var wg sync.WaitGroup
	wg.Add(3)

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("validity") {
			validity = append(validity, validityTest{
				code:    string(cols[0]),
				isValid: cols[1][0] == 't',
				isShort: cols[2][0] == 't',
				isFull:  cols[3][0] == 't',
			})
		}
	}()

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("encoding") {
			encoding = append(encoding, encodingTest{
				code: string(cols[0]),
				lat:  mustFloat(cols[1]), lng: mustFloat(cols[2]),
				latLo: mustFloat(cols[3]), lngLo: mustFloat(cols[4]),
				latHi: mustFloat(cols[5]), lngHi: mustFloat(cols[6]),
			})
		}
	}()

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("shortCode") {
			shorten = append(shorten, shortenTest{
				code: string(cols[0]),
				lat:  mustFloat(cols[1]), lng: mustFloat(cols[2]),
				short: string(cols[3]),
			})
		}
	}()
	wg.Wait()
}

func TestCheck(t *testing.T) {
	for i, elt := range validity {
		err := Check(elt.code)
		got := err == nil
		if got != elt.isValid {
			t.Errorf("%d. %q validity is %t (err=%v), awaited %t.", i, elt.code, got, err, elt.isValid)
		}
	}
}

func TestEncode(t *testing.T) {
	for i, elt := range encoding {
		n := len(stripCode(elt.code))
		code := Encode(elt.lat, elt.lng, n)
		if code != elt.code {
			t.Errorf("%d. got %q for (%v,%v,%d), awaited %q.", i, code, elt.lat, elt.lng, n, elt.code)
			t.FailNow()
		}
	}
}

func TestDecode(t *testing.T) {
	check := func(i int, code, name string, got, want float64) {
		if !closeEnough(got, want) {
			t.Errorf("%d. %q want %s=%f, got %f", i, code, name, want, got)
			t.FailNow()
		}
	}
	for i, elt := range encoding {
		area, err := Decode(elt.code)
		if err != nil {
			t.Errorf("%d. %q: %v", i, elt.code, err)
			continue
		}
		code := Encode(elt.lat, elt.lng, area.Len)
		if code != elt.code {
			t.Errorf("%d. encode (%f,%f) got %q, awaited %q", i, elt.lat, elt.lng, code, elt.code)
		}
		C := func(name string, got, want float64) {
			check(i, elt.code, name, got, want)
		}
		C("latLo", area.LatLo, elt.latLo)
		C("latHi", area.LatHi, elt.latHi)
		C("lngLo", area.LngLo, elt.lngLo)
		C("lngHi", area.LngHi, elt.lngHi)
	}
}

func TestShorten(t *testing.T) {
	for i, elt := range shorten {
		if tType == "B" || tType == "S" {
			got, err := Shorten(elt.code, elt.lat, elt.lng)
			if err != nil {
				t.Errorf("%d. shorten %q: %v", i, elt.code, err)
				t.FailNow()
			}
			if got != elt.short {
				t.Errorf("%d. shorten got %q, awaited %q.", i, got, elt.short)
				t.FailNow()
			}
		}

		if tType == "B" || tType == "R" {
			got, err = RecoverNearest(elt.short, elt.lat, elt.lng)
			if err != nil {
				t.Errorf("%d. nearest %q: %v", i, got, err)
				t.FailNow()
			}
			if got != elt.code {
				t.Errorf("%d. nearest got %q, awaited %q.", i, got, elt.code)
				t.FailNow()
			}
		}
	}
}

func closeEnough(a, b float64) bool {
	return a == b || math.Abs(a-b) <= 0.0000000001
}

func mustReadLines(name string) [][][]byte {
	rows, err := readLines(filepath.Join("..", "test_data", name+"Tests.csv"))
	if err != nil {
		panic(err)
	}
	return rows
}

func readLines(path string) (rows [][][]byte, err error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	for _, row := range bytes.Split(data, []byte{'\n'}) {
		if j := bytes.IndexByte(row, '#'); j >= 0 {
			row = row[:j]
		}
		row = bytes.TrimSpace(row)
		if len(row) == 0 {
			continue
		}
		rows = append(rows, bytes.Split(row, []byte{','}))
	}
	return rows, nil
}

func mustFloat(a []byte) float64 {
	f, err := strconv.ParseFloat(string(a), 64)
	if err != nil {
		panic(err)
	}
	return f
}

func TestFuzzCrashers(t *testing.T) {
	for i, code := range []string{
		"+975722X988X29qqX297" +
			"5722X888X2975722X888" +
			"X2975722X988X29qqX29" +
			"75722X888X2975722X88" +
			"8X2975722X988X29qqX2" +
			"975722X88qqX2975722X" +
			"888X2975722X888X2975" +
			"722X988X29qqX2975722" +
			"X888X2975722X888X297" +
			"5722X988X29qqX297572" +
			"2X888X2975722X888X29" +
			"75722X988X29qqX29757" +
			"22X88qqX2975722X888X" +
			"2975722X888X2975722X" +
			"988X29qqX2975722X888" +
			"X2975722X888X2975722" +
			"X988X29qqX2975722X88" +
			"8X2975722X888X297572" +
			"2X988X29qqX2975722X8" +
			"8qqX2975722X888X2975" +
			"722X888X2975722X988X" +
			"29qqX2975722X888X297" +
			"5722X888X2975722X988" +
			"X20",

		"+qqX2975722X888X2975" +
			"722X888X2975722X988X" +
			"29qqX2975722X888X297" +
			"5722X888X2975722X988" +
			"X29qqX2975722X888X29" +
			"75722X888X2975722X98" +
			"8X29qqX2975722X88qqX" +
			"2975722X888X2975722X" +
			"888X2975722X988X29qq" +
			"X2975722X888X2975722" +
			"X888X2975722X988X29q" +
			"qX2975722X888X297572" +
			"2X888X2975722X988X29" +
			"qqX2975722X88qqX2975" +
			"722X888X2975722X888X" +
			"2975722X988X29qqX297" +
			"5722X888X2975722X888" +
			"X2975722X988X29qqX29" +
			"75722X888X2975722X88" +
			"8X2975722X988X29qqX2" +
			"975722X88qqX2975722X" +
			"888X2975722X888X2975" +
			"722X988X29qqX2975722" +
			"X888X2975722X888X297" +
			"5722X988X29qqX297572" +
			"2X888X2975722X888X29" +
			"75722X988X29qqX29757" +
			"2",
	} {
		if err := Check(code); err != nil {
			t.Logf("%d. %q Check: %v", i, code, err)
		}
		area, err := Decode(code)
		if err != nil {
			t.Logf("%d. %q Decode: %v", i, code, err)
		}
		if _, err = Decode(Encode(area.LatLo, area.LngLo, len(code))); err != nil {
			t.Logf("%d. Lo Decode(Encode(%q, %f, %f, %d))): %v", i, code, area.LatLo, area.LngLo, len(code), err)
		}
		if _, err = Decode(Encode(area.LatHi, area.LngHi, len(code))); err != nil {
			t.Logf("%d. Hi Decode(Encode(%q, %f, %f, %d))): %v", i, code, area.LatHi, area.LngHi, len(code), err)
		}

	}
}
<commit_msg>Update olc_test.go<commit_after>// Copyright 2015 Tamás Gulácsi. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package olc

import (
	"bytes"
	"io/ioutil"
	"math"
	"path/filepath"
	"strconv"
	"sync"
	"testing"
)

var (
	validity []validityTest
	encoding []encodingTest
	shorten  []shortenTest
)

type (
	validityTest struct {
		code                     string
		isValid, isShort, isFull bool
	}

	encodingTest struct {
		code                                 string
		lat, lng, latLo, lngLo, latHi, lngHi float64
	}

	shortenTest struct {
		code     string
		lat, lng float64
		short    string
		tType    string
	}
)

func init() {
	var wg sync.WaitGroup
	wg.Add(3)

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("validity") {
			validity = append(validity, validityTest{
				code:    string(cols[0]),
				isValid: cols[1][0] == 't',
				isShort: cols[2][0] == 't',
				isFull:  cols[3][0] == 't',
			})
		}
	}()

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("encoding") {
			encoding = append(encoding, encodingTest{
				code: string(cols[0]),
				lat:  mustFloat(cols[1]), lng: mustFloat(cols[2]),
				latLo: mustFloat(cols[3]), lngLo: mustFloat(cols[4]),
				latHi: mustFloat(cols[5]), lngHi: mustFloat(cols[6]),
			})
		}
	}()

	go func() {
		defer wg.Done()
		for _, cols := range mustReadLines("shortCode") {
			shorten = append(shorten, shortenTest{
				code: string(cols[0]),
				lat:  mustFloat(cols[1]), lng: mustFloat(cols[2]),
				short: string(cols[3]),
			})
		}
	}()
	wg.Wait()
}

func TestCheck(t *testing.T) {
	for i, elt := range validity {
		err := Check(elt.code)
		got := err == nil
		if got != elt.isValid {
			t.Errorf("%d. %q validity is %t (err=%v), awaited %t.", i, elt.code, got, err, elt.isValid)
		}
	}
}

func TestEncode(t *testing.T) {
	for i, elt := range encoding {
		n := len(stripCode(elt.code))
		code := Encode(elt.lat, elt.lng, n)
		if code != elt.code {
			t.Errorf("%d. got %q for (%v,%v,%d), awaited %q.", i, code, elt.lat, elt.lng, n, elt.code)
			t.FailNow()
		}
	}
}

func TestDecode(t *testing.T) {
	check := func(i int, code, name string, got, want float64) {
		if !closeEnough(got, want) {
			t.Errorf("%d. %q want %s=%f, got %f", i, code, name, want, got)
			t.FailNow()
		}
	}
	for i, elt := range encoding {
		area, err := Decode(elt.code)
		if err != nil {
			t.Errorf("%d. %q: %v", i, elt.code, err)
			continue
		}
		code := Encode(elt.lat, elt.lng, area.Len)
		if code != elt.code {
			t.Errorf("%d. encode (%f,%f) got %q, awaited %q", i, elt.lat, elt.lng, code, elt.code)
		}
		C := func(name string, got, want float64) {
			check(i, elt.code, name, got, want)
		}
		C("latLo", area.LatLo, elt.latLo)
		C("latHi", area.LatHi, elt.latHi)
		C("lngLo", area.LngLo, elt.lngLo)
		C("lngHi", area.LngHi, elt.lngHi)
	}
}

func TestShorten(t *testing.T) {
	for i, elt := range shorten {
		if elt.tType == "B" || elt.tType == "S" {
			got, err := Shorten(elt.code, elt.lat, elt.lng)
			if err != nil {
				t.Errorf("%d. shorten %q: %v", i, elt.code, err)
				t.FailNow()
			}
			if got != elt.short {
				t.Errorf("%d. shorten got %q, awaited %q.", i, got, elt.short)
				t.FailNow()
			}
		}

		if elt.tType == "B" || elt.tType == "R" {
			got, err = RecoverNearest(elt.short, elt.lat, elt.lng)
			if err != nil {
				t.Errorf("%d. nearest %q: %v", i, got, err)
				t.FailNow()
			}
			if got != elt.code {
				t.Errorf("%d. nearest got %q, awaited %q.", i, got, elt.code)
				t.FailNow()
			}
		}
	}
}

func closeEnough(a, b float64) bool {
	return a == b || math.Abs(a-b) <= 0.0000000001
}

func mustReadLines(name string) [][][]byte {
	rows, err := readLines(filepath.Join("..", "test_data", name+"Tests.csv"))
	if err != nil {
		panic(err)
	}
	return rows
}

func readLines(path string) (rows [][][]byte, err error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	for _, row := range bytes.Split(data, []byte{'\n'}) {
		if j := bytes.IndexByte(row, '#'); j >= 0 {
			row = row[:j]
		}
		row = bytes.TrimSpace(row)
		if len(row) == 0 {
			continue
		}
		rows = append(rows, bytes.Split(row, []byte{','}))
	}
	return rows, nil
}

func mustFloat(a []byte) float64 {
	f, err := strconv.ParseFloat(string(a), 64)
	if err != nil {
		panic(err)
	}
	return f
}

func TestFuzzCrashers(t *testing.T) {
	for i, code := range []string{
		"+975722X988X29qqX297" +
			"5722X888X2975722X888" +
			"X2975722X988X29qqX29" +
			"75722X888X2975722X88" +
			"8X2975722X988X29qqX2" +
			"975722X88qqX2975722X" +
			"888X2975722X888X2975" +
			"722X988X29qqX2975722" +
			"X888X2975722X888X297" +
			"5722X988X29qqX297572" +
			"2X888X2975722X888X29" +
			"75722X988X29qqX29757" +
			"22X88qqX2975722X888X" +
			"2975722X888X2975722X" +
			"988X29qqX2975722X888" +
			"X2975722X888X2975722" +
			"X988X29qqX2975722X88" +
			"8X2975722X888X297572" +
			"2X988X29qqX2975722X8" +
			"8qqX2975722X888X2975" +
			"722X888X2975722X988X" +
			"29qqX2975722X888X297" +
			"5722X888X2975722X988" +
			"X20",

		"+qqX2975722X888X2975" +
			"722X888X2975722X988X" +
			"29qqX2975722X888X297" +
			"5722X888X2975722X988" +
			"X29qqX2975722X888X29" +
			"75722X888X2975722X98" +
			"8X29qqX2975722X88qqX" +
			"2975722X888X2975722X" +
			"888X2975722X988X29qq" +
			"X2975722X888X2975722" +
			"X888X2975722X988X29q" +
			"qX2975722X888X297572" +
			"2X888X2975722X988X29" +
			"qqX2975722X88qqX2975" +
			"722X888X2975722X888X" +
			"2975722X988X29qqX297" +
			"5722X888X2975722X888" +
			"X2975722X988X29qqX29" +
			"75722X888X2975722X88" +
			"8X2975722X988X29qqX2" +
			"975722X88qqX2975722X" +
			"888X2975722X888X2975" +
			"722X988X29qqX2975722" +
			"X888X2975722X888X297" +
			"5722X988X29qqX297572" +
			"2X888X2975722X888X29" +
			"75722X988X29qqX29757" +
			"2",
	} {
		if err := Check(code); err != nil {
			t.Logf("%d. %q Check: %v", i, code, err)
		}
		area, err := Decode(code)
		if err != nil {
			t.Logf("%d. %q Decode: %v", i, code, err)
		}
		if _, err = Decode(Encode(area.LatLo, area.LngLo, len(code))); err != nil {
			t.Logf("%d. Lo Decode(Encode(%q, %f, %f, %d))): %v", i, code, area.LatLo, area.LngLo, len(code), err)
		}
		if _, err = Decode(Encode(area.LatHi, area.LngHi, len(code))); err != nil {
			t.Logf("%d. Hi Decode(Encode(%q, %f, %f, %d))): %v", i, code, area.LatHi, area.LngHi, len(code), err)
		}

	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"math"
	"welcome"
	"log"
	"os"
)

var myNumber = 1.23

func main() {
	roundedUp := math.Ceil(myNumber)
	roundedDown := math.Floor(myNumber)
	fmt.Println(roundedUp, roundedDown)
	fmt.Println(welcome.English)

	var a int
	a = 1
	var b, c int
	b, c = 2, 3
	var d = 5
	e, f := 6, 7
	fmt.Println(a, b, c, d, e, f)

	var anInt int = 1
	var aFloat float64 = float64(anInt)
	fmt.Println(anInt, aFloat)

	myFunction()

	fmt.Println(add(1,2))
	fmt.Println(subtract(1,2))

	squareRoot, err := squareRoot(9)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(squareRoot)

	fileInfo, _ := os.Stat("existent.txt")
	fmt.Println("size of existent.txt = ",fileInfo.Size())

	fileInfo, error := os.Stat("nonexistent.txt")
	if error != nil {
		fmt.Println(error)
	} else {
		fmt.Println("size of existent.txt = ",fileInfo.Size())
	}

	for i := 1; i <= 3; i++ {
		fmt.Println(i)
	}

	if true {
		fmt.Println("true")
	}

	if false {
		fmt.Println("false")
	}

	if 1 < 2 {
		fmt.Println("1 < 2")
	}

	if 1 > 2 {
		fmt.Println("1 > 2")
	}

	if true && true {
		fmt.Println("true && true")
	}

	if true || false {
		fmt.Println("true || false")
	}

	if true {
		fmt.Println("true")
	} else if false {
		fmt.Println("false")
	} else {
		fmt.Println("else")
	}

}

func myFunction() {
	fmt.Println("Running myFunction")
}

func ExportedFunction() {

}

func unexportedFunction() {

}

func add(a float64, b float64) (sum float64) {
	return a + b
}

func subtract(a, b float64) (difference float64) {
	difference = a - b
	return
}

func squareRoot(x float64) (float64, error) {
	if x < 0 {
		return 0, fmt.Errorf("can't take square root of a negative number")
	}
	return math.Sqrt(x), nil
}<commit_msg>go: switch<commit_after>package main

import (
	"fmt"
	"math"
	"welcome"
	"log"
	"os"
)

var myNumber = 1.23

func main() {
	roundedUp := math.Ceil(myNumber)
	roundedDown := math.Floor(myNumber)
	fmt.Println(roundedUp, roundedDown)
	fmt.Println(welcome.English)

	var a int
	a = 1
	var b, c int
	b, c = 2, 3
	var d = 5
	e, f := 6, 7
	fmt.Println(a, b, c, d, e, f)

	var anInt int = 1
	var aFloat float64 = float64(anInt)
	fmt.Println(anInt, aFloat)

	myFunction()

	fmt.Println(add(1,2))
	fmt.Println(subtract(1,2))

	squareRoot, err := squareRoot(9)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(squareRoot)

	fileInfo, _ := os.Stat("existent.txt")
	fmt.Println("size of existent.txt = ",fileInfo.Size())

	fileInfo, error := os.Stat("nonexistent.txt")
	if error != nil {
		fmt.Println(error)
	} else {
		fmt.Println("size of existent.txt = ",fileInfo.Size())
	}

	for i := 1; i <= 3; i++ {
		fmt.Println(i)
	}

	if true {
		fmt.Println("true")
	}

	if false {
		fmt.Println("false")
	}

	if 1 < 2 {
		fmt.Println("1 < 2")
	}

	if 1 > 2 {
		fmt.Println("1 > 2")
	}

	if true && true {
		fmt.Println("true && true")
	}

	if true || false {
		fmt.Println("true || false")
	}

	if true {
		fmt.Println("true")
	} else if false {
		fmt.Println("false")
	} else {
		fmt.Println("else")
	}

	doorNumber := 1
	switch doorNumber {
	case 1:
		fmt.Println("a new car!")
		fallthrough
	case 2:
		fmt.Println("a llama!")
	default:
		fmt.Println("a goat!")
	}

}

func myFunction() {
	fmt.Println("Running myFunction")
}

func ExportedFunction() {

}

func unexportedFunction() {

}

func add(a float64, b float64) (sum float64) {
	return a + b
}

func subtract(a, b float64) (difference float64) {
	difference = a - b
	return
}

func squareRoot(x float64) (float64, error) {
	if x < 0 {
		return 0, fmt.Errorf("can't take square root of a negative number")
	}
	return math.Sqrt(x), nil
}<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	// "github.com/cheggaaa/pb"
	"os"
)

var Repos []GHRepo

func main() {
	Username := flag.String("username", "", "The username that you are targetting")
	flag.Parse()

	if os.Getenv("GOPATH") == "" {
		fmt.Println("You don't have a GOPATH set, I don't know where to clone to! Please set one.")
		os.Exit(1)
	}

	if *Username == "" {
		fmt.Println("Please give a username to auto clone all their go stars to the gopath to.")
		os.Exit(1)
	}

	fmt.Print("Grabbing all stars off that user")
	GHUrl := fmt.Sprintf("https://api.github.com/users/%s/starred", *Username)
	s, e := ExpectGithubToBreak(GHUrl)
	if e != nil {
		fmt.Println("Cannot get the first set, Not going to attempt to get others.")
	}

	Repos = make([]GHRepo, 0)
	CastData := make([]GHRepo, 0)
	e = json.Unmarshal([]byte(s), &CastData)
	Repos = FilterForGoRepo(CastData, Repos)
	if e != nil {
		fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
	}
	var StillData bool = true
	var PageCount int = 2
	var TripCount int = 0
	for StillData {
		GHUrl = fmt.Sprintf("https://api.github.com/users/%s/starred?page=%d", *Username, PageCount)
		s, e = ExpectGithubToBreak(GHUrl)
		if e == nil && TripCount < 2 {
			CastData = make([]GHRepo, 0)
			e = json.Unmarshal([]byte(s), &CastData)
			if len(CastData) == 0 {
				// We have found it all!
				break
			}
			Repos = FilterForGoRepo(CastData, Repos)
			if e != nil {
				fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
			}
			PageCount++
		} else {
			TripCount++
			if TripCount < 2 {
				fmt.Println("API errors stopped the program from running.", e.Error())
			}
		}
	}

	// b, _ := json.Marshal(Repos)
	// fmt.Println(string(b))
}
<commit_msg>Finishing up the tool, Looks like it works now!<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/cheggaaa/pb"
	"log"
	"os"
	"os/exec"
	"strings"
)

var Repos []GHRepo

func main() {
	Username := flag.String("username", "", "The username that you are targetting")
	flag.Parse()

	if os.Getenv("GOPATH") == "" {
		fmt.Println("You don't have a GOPATH set, I don't know where to clone to! Please set one.")
		os.Exit(1)
	}

	if *Username == "" {
		fmt.Println("Please give a username to auto clone all their go stars to the gopath to.")
		os.Exit(1)
	}

	fmt.Print("Grabbing all stars off that user")
	GHUrl := fmt.Sprintf("https://api.github.com/users/%s/starred", *Username)
	s, e := ExpectGithubToBreak(GHUrl)
	if e != nil {
		fmt.Println("Cannot get the first set, Not going to attempt to get others.")
	}

	Repos = make([]GHRepo, 0)
	CastData := make([]GHRepo, 0)
	e = json.Unmarshal([]byte(s), &CastData)
	Repos = FilterForGoRepo(CastData, Repos)
	if e != nil {
		fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
	}
	var PageCount int = 2
	var TripCount int = 0
	for {
		GHUrl = fmt.Sprintf("https://api.github.com/users/%s/starred?page=%d", *Username, PageCount)
		s, e = ExpectGithubToBreak(GHUrl)
		if e == nil && TripCount < 2 {
			CastData = make([]GHRepo, 0)
			e = json.Unmarshal([]byte(s), &CastData)
			if len(CastData) == 0 {
				// We have found it all!
				break
			}
			Repos = FilterForGoRepo(CastData, Repos)
			if e != nil {
				fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
			}
			PageCount++
			fmt.Print(".")
		} else {
			TripCount++
			if TripCount < 2 {
				fmt.Println("API errors stopped the program from running.", e.Error())
			}
		}
	}

	bar := pb.StartNew(len(Repos))
	for _, repo := range Repos {
		GoGet(strings.Replace(repo.HtmlURL, "https://", "", -1))
		bar.Increment()
	}
	// b, _ := json.Marshal(Repos)
	// fmt.Println(string(b))
}

func GoGet(url string) {
	buf := make([]byte, 1024)
	cmd := exec.Command("go", "get", url)
	stdout, _ := cmd.StdoutPipe()
	if err := cmd.Start(); err != nil {
		log.Fatal(err)
	}
	for {
		_, e := stdout.Read(buf)
		if e != nil {
			break
		}
	}
}
<|endoftext|>


<commit_before>package goheap

import (
	"testing"
	"os"
)

func devConfig() (config Config) {
	url   := os.Getenv("RH_URL")
	user  := os.Getenv("RH_USER")
	token := os.Getenv("RH_TOKEN")
	if url == "" {
		config.URL = RefheapURL
	}
	config.User = user
	config.Key  = token
	return
}

func cError(t *testing.T, config *Config, expected interface{}, err *error, call string) {
	t.Errorf("%v failed! Returned config %#v and err %#v; Wanted %#v",
		call, config, err, expected)
}

// This function is by nature pretty fickle because of the magic
// that it does with variadic arguments. As such, we're going to
// very thoroughly test it!
func TestNewConfig(t *testing.T) {
	zero := Config{RefheapURL, "", ""}
	one := Config{"foo", "", ""}
	two := Config{RefheapURL, "raynes", "123"}
	three := Config{"foo", "raynes", "123"}
	error := ConfigError{[]string{"", "", "", ""}}

	if config, err := NewConfig(); err != nil || config != zero {
		cError(t, &config, &zero, &err, "NewConfig()")
	}

	if config, err := NewConfig("foo"); err != nil || config != one {
		cError(t, &config, &one, &err, "NewConfig(\"foo\")")
	}

	if config, err := NewConfig("raynes", "123"); err != nil || config != two {
		cError(t, &config, &two, &err, "NewConfig(\"raynes\", \"123\")")
	}

	if config, err := NewConfig("foo", "raynes", "123"); err != nil || config != three {
		cError(t, &config, &three, &err, "NewConfig(\"foo\", \"raynes\", \"123\", )")
	}

	if config, err := NewConfig("", "", "", ""); err == nil {
		cError(t, &config, &error, &err, "NewConfig(\"\", \"\", \"\", \"\")")
	}
}

// This will be set to whatever the current expression is for
// gpError() messages. It is a convenience because validating
// individual paste fields manually is already tedious and
// passing the current expression each time would be a massive
// pain in the rear. It pokes at my FP nerves, but these are
// merely tests after all. We're allowed a bit of leeway. When
// changing this variable we should always document what we're
// doing with a comment.
var expression string

func gpError(t *testing.T, missing string, missingValue interface{}, expected interface{}) {
	err := `
		Expression %v failing.
		Paste field %v was not as expected.
		Got %#v; Expected %v.
		`
	t.Errorf(err, expression, missing, missingValue, expected)
}

func TestGet(t *testing.T) {
	// Set what the current expression is for error messages.
	expression = "paste.Get(&config)"
	config := devConfig()
	paste := Paste{ID: "1"}
	err := paste.Get(&config)
	if err != nil {
		t.Errorf("%v failed because of error %v", expression, err)
		return
	}

	// Unfortunately we cannot just create a dummy object to
	// compare against because views is dynamic. Technically
	// all of this is dynamic, but views is the only thing
	// a person other than me (Raynes) can change. Anyways,
	// because of this we have to validate each field one by
	// one manually. At least we get nice failure messages.
	if lines := paste.Lines; lines != 1 {
		gpError(t, "Lines", lines, 1)
	}

	if views := paste.Views; views <= 0 {
		gpError(t, "Views", views, "a number greater than zero")
	}

	const dateValue = "2012-01-04T01:44:22.964Z"
	if date := paste.Date; date != dateValue {
		gpError(t, "Date", date, dateValue)
	}

	if ID := paste.ID; ID != "1" {
		gpError(t, "ID", ID, "1")
	}

	if language := paste.Language; language != "Clojure" {
		gpError(t, "Language", language, "Clojure")
	}

	if private := paste.Private; private {
		gpError(t, "Private", private, !private)
	}

	const expectedUrl = "https://www.refheap.com/1"
	if url := paste.URL; url != expectedUrl {
		gpError(t, "Url", url, expectedUrl)
	}

	if user := paste.User; user != "raynes" {
		gpError(t, "User", user, "raynes")
	}

	if contents := paste.Contents; contents != "(begin)" {
		gpError(t, "Contents", contents, "(begin)")
	}

	expectedErr := RefheapError{"Paste does not exist."}
	paste = Paste{ID: "@D("}
	err = paste.Get(&config)
	if err != expectedErr {
		msg := `
		Expression %v did not fail as expected.
		err was %#v.
		Expected err to be %#v.
		`
		t.Errorf(msg, expression, err, expectedErr)
	}
}

// Sadly, TestCreate and TestDelete are rather interleaved, since we
// can't delete a paste without creating it (and thus TestCreate must
// pass) and you don't want to create a paste without deleting it after
// because nobody likes a litterbug. As such, these tests depend on one
// another.

func TestCreate(t *testing.T) {
	config := devConfig()
	expression = "paste.Create(&config)"
	paste := Paste{Private: true, Contents: "hi", Language: "Go"}
	defer paste.Delete(&config)
	err := paste.Create(&config)
	if err != nil {
		t.Errorf("Error creating paste with expression %v: %v", expression, err)
	}

	if pUser, cUser := paste.User, config.User; pUser != cUser {
		t.Errorf("Expected creating user to be %v. It was %v.", cUser, pUser)
	}

	if lang := paste.Language; lang != "Go" {
		t.Errorf("Expected language to be Go. It was %v.", lang)
	}

	if priv := paste.Private; !priv {
		t.Error("Expected paste to be private!")
	}
}

func TestDelete(t *testing.T) {
	config := devConfig()
	expression = "paste.Delete(&config)"
	paste := Paste{Contents: "foo", Private: true}
	if err := paste.Create(&config); err != nil {
		t.Errorf("Something went wrong creating a paste: %v", err)
	}

	if err := paste.Delete(&config); err != nil {
		t.Errorf("Something went wrong deleting a paste: %v", err)
	}

	err := paste.Get(&config)
	if _, ok := err.(RefheapError); !ok {
		t.Errorf("Paste %v still exists after trying to delete!", paste.ID)
	}
}
<commit_msg>Overhauled the tests a bit.<commit_after>package goheap

import (
	"testing"
	"os"
)

func devConfig() (config Config) {
	url   := os.Getenv("RH_URL")
	user  := os.Getenv("RH_USER")
	token := os.Getenv("RH_TOKEN")
	if url == "" {
		config.URL = RefheapURL
	}
	config.User = user
	config.Key  = token
	return
}

func cError(t *testing.T, config *Config, expected interface{}, err *error, call string) {
	msg := `
	%v failed!
	Expected %#v.
	Got err %#v and config %#v
	`
	t.Errorf(msg, call, config, err, expected)
}

// This function is by nature pretty fickle because of the magic
// that it does with variadic arguments. As such, we're going to
// very thoroughly test it!
func TestNewConfig(t *testing.T) {
	zero := Config{RefheapURL, "", ""}
	one := Config{"foo", "", ""}
	two := Config{RefheapURL, "raynes", "123"}
	three := Config{"foo", "raynes", "123"}
	error := ConfigError{[]string{"", "", "", ""}}

	if config, err := NewConfig(); err != nil || config != zero {
		cError(t, &config, &zero, &err, "NewConfig()")
	}

	if config, err := NewConfig("foo"); err != nil || config != one {
		cError(t, &config, &one, &err, "NewConfig(\"foo\")")
	}

	if config, err := NewConfig("raynes", "123"); err != nil || config != two {
		cError(t, &config, &two, &err, "NewConfig(\"raynes\", \"123\")")
	}

	if config, err := NewConfig("foo", "raynes", "123"); err != nil || config != three {
		cError(t, &config, &three, &err, "NewConfig(\"foo\", \"raynes\", \"123\", )")
	}

	if config, err := NewConfig("", "", "", ""); err == nil {
		cError(t, &config, &error, &err, "NewConfig(\"\", \"\", \"\", \"\")")
	}
}

func gpError(t *testing.T, missing string, missingValue interface{}, expected interface{}) {
	err := `
		Paste field %v was not as expected.
		Got %#v; Expected %v.
		`
	t.Errorf(err, missing, missingValue, expected)
}

func TestGet(t *testing.T) {
	config := devConfig()
	testPaste := Paste{Private: true, Contents: "hi", Language: "Go"}
	defer testPaste.Delete(&config)
	if err := testPaste.Create(&config); err != nil {
		t.Errorf("Something went wrong creating a paste: %v", err)
	}

	paste := Paste{ID: testPaste.ID}
	if err := paste.Get(&config); err != nil {
		t.Errorf("Something went wrong getting a paste: %v", err)
	}

	if lines := paste.Lines; lines != 1 {
		gpError(t, "Lines", lines, 1)
	}

	if date := paste.Date; date == "" {
		gpError(t, "Date", "a date", "no date")
	}

	if id := paste.ID; id == "" {
		gpError(t, "ID", "no id", "an id")
	}

	if language := paste.Language; language != "Go" {
		gpError(t, "Language", language, "Go")
	}

	if private := paste.Private; !private {
		gpError(t, "Private", !private, private)
	}

	if url := paste.URL; url == "" {
		gpError(t, "Url", url, "no url")
	}

	if user := paste.User; user != config.User {
		gpError(t, "User", user, config.User)
	}

	if contents := paste.Contents; contents != "hi" {
		gpError(t, "Contents", contents, "hi")
	}

	expectedErr := RefheapError{"Paste does not exist."}
	paste = Paste{ID: "@D("}
	err := paste.Get(&config)
	if err != expectedErr {
		msg := `
		err was %#v.
		Expected err to be %#v.
		`
		t.Errorf(msg, err, expectedErr)
	}
}

func TestCreate(t *testing.T) {
	config := devConfig()
	paste := Paste{Private: true, Contents: "hi", Language: "Go"}
	err := paste.Create(&config)
	if err != nil {
		t.Errorf("Error creating paste: %v", err)
	}

	defer paste.Delete(&config)

	if pUser, cUser := paste.User, config.User; pUser != cUser {
		t.Errorf("Expected creating user to be %v. It was %v.", cUser, pUser)
	}

	if lang := paste.Language; lang != "Go" {
		t.Errorf("Expected language to be Go. It was %v.", lang)
	}

	if priv := paste.Private; !priv {
		t.Error("Expected paste to be private!")
	}
}

func TestDelete(t *testing.T) {
	config := devConfig()
	paste := Paste{Contents: "foo", Private: true}
	if err := paste.Create(&config); err != nil {
		t.Errorf("Something went wrong creating a paste: %v", err)
	}

	if err := paste.Delete(&config); err != nil {
		t.Errorf("Something went wrong deleting a paste: %v", err)
	}

	err := paste.Get(&config)
	if _, ok := err.(RefheapError); !ok {
		t.Errorf("Paste %v still exists after trying to delete!", paste.ID)
	}
}
<|endoftext|>


<commit_before>package colonycore

import (
	"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"
)

func (ds *WebGrabber) TableName() string {
	return "webgrabbers"
}

func (ds *WebGrabber) RecordID() interface{} {
	return ds.ID
}

type WebGrabber struct {
	orm.ModelBase
	ID           string          `json:"_id",bson:"_id"`
	SourceType   string          `json:"sourcetype",bson:"sourcetype"`
	GrabConf     toolkit.M       `json:"grabconf",bson:"grabconf"`
	IntervalConf *IntervalConf   `json:"intervalconf",bson:"intervalconf"`
	LogConf      *LogConf        `json:"logconf",bson:"logconf"`
	HistConf     *HistConf       `json:"histconf",bson:"histconf"`
	DataSettings []*DataSettings `json:"datasettings",bson:"datasettings"`
	Running      bool            `json:"running",bson:"running"`
}

type IntervalConf struct {
	StartTime       string    `json:"starttime",bson:"starttime"`
	ExpiredTime     string    `json:"expiredtime",bson:"expiredtime"`
	IntervalType    string    `json:"intervaltype",bson:"intervaltype"`
	GrabInterval    int       `json:"grabinterval",bson:"grabinterval"`
	TimeoutInterval int       `json:"timeoutinterval",bson:"timeoutinterval"`
	CronConf        toolkit.M `json:"cronconf",bson:"cronconf"`
}

type LogConf struct {
	LogPath     string `json:"logpath",bson:"logpath"`
	FileName    string `json:"filename",bson:"filename"`
	FilePattern string `json:"filepattern",bson:"filepattern"`
}

type HistConf struct {
	Histpath    string `json:"histpath",bson:"histpath"`
	RecPath     string `json:"recpath",bson:"recpath"`
	FileName    string `json:"filename",bson:"filename"`
	FilePattern string `json:"filepattern",bson:"filepattern"`
}

type DataSettings struct {
	id             string            `json:"id",bson:"id"`
	RowSelector    string            `json:"rowselector",bson:"rowselector"`
	ColumnSettings []*ColumnSettings `json:"columnsettings",bson:"columnsettings"`
	FilterCond     toolkit.M         `json:"filtercond",bson:"filtercond"`
	DestOutputType string            `json:"destoutputtype",bson:"destoutputtype"`
	DestType       string            `json:"desttype",bson:"desttype"`
	ConnectionInfo *ConnectionInfo   `json:"connectioninfo",bson:"connectioninfo"`
}

type ColumnSettings struct {
	Index     int    `json:"index",bson:"index"`
	Alias     string `json:"alias",bson:"alias"`
	Selector  string `json:"selector",bson:"selector"`
	ValueType string `json:"valuetype",bson:"valuetype"`
	AttrName  string `json:"attrname",bson:"attrname"`
}

type ConnectionInfo struct {
	Host       string    `json:"host",bson:"host"`
	UserName   string    `json:"username",bson:"username"`
	Password   string    `json:"password",bson:"password"`
	Database   string    `json:"database",bson:"database"`
	Collection string    `json:"collection",bson:"collection"`
	Settings   toolkit.M `json:"settings",bson:"settings"`
}
<commit_msg>no message<commit_after>package colonycore

import (
	"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"
)

func (ds *WebGrabber) TableName() string {
	return "webgrabbers"
}

func (ds *WebGrabber) RecordID() interface{} {
	return ds.ID
}

type WebGrabber struct {
	orm.ModelBase
	ID           string          `json:"_id",bson:"_id"`
	SourceType   string          `json:"sourcetype",bson:"sourcetype"`
	GrabConf     toolkit.M       `json:"grabconf",bson:"grabconf"`
	IntervalConf *IntervalConf   `json:"intervalconf",bson:"intervalconf"`
	LogConf      *LogConf        `json:"logconf",bson:"logconf"`
	HistConf     *HistConf       `json:"histconf",bson:"histconf"`
	DataSettings []*DataSettings `json:"datasettings",bson:"datasettings"`
	Running      bool            `json:"running",bson:"running"`
}

type IntervalConf struct {
	StartTime       string    `json:"starttime",bson:"starttime"`
	ExpiredTime     string    `json:"expiredtime",bson:"expiredtime"`
	IntervalType    string    `json:"intervaltype",bson:"intervaltype"`
	GrabInterval    int       `json:"grabinterval",bson:"grabinterval"`
	TimeoutInterval int       `json:"timeoutinterval",bson:"timeoutinterval"`
	CronConf        toolkit.M `json:"cronconf",bson:"cronconf"`
}

type LogConf struct {
	LogPath     string `json:"logpath",bson:"logpath"`
	FileName    string `json:"filename",bson:"filename"`
	FilePattern string `json:"filepattern",bson:"filepattern"`
}

type HistConf struct {
	Histpath    string `json:"histpath",bson:"histpath"`
	RecPath     string `json:"recpath",bson:"recpath"`
	FileName    string `json:"filename",bson:"filename"`
	FilePattern string `json:"filepattern",bson:"filepattern"`
}

type DataSettings struct {
	ID             string            `json:"_id",bson:"_id"`
	RowSelector    string            `json:"rowselector",bson:"rowselector"`
	ColumnSettings []*ColumnSettings `json:"columnsettings",bson:"columnsettings"`
	FilterCond     toolkit.M         `json:"filtercond",bson:"filtercond"`
	DestOutputType string            `json:"destoutputtype",bson:"destoutputtype"`
	DestType       string            `json:"desttype",bson:"desttype"`
	ConnectionInfo *ConnectionInfo   `json:"connectioninfo",bson:"connectioninfo"`
}

type ColumnSettings struct {
	Index     int    `json:"index",bson:"index"`
	Alias     string `json:"alias",bson:"alias"`
	Selector  string `json:"selector",bson:"selector"`
	ValueType string `json:"valuetype",bson:"valuetype"`
	AttrName  string `json:"attrname",bson:"attrname"`
}

type ConnectionInfo struct {
	Host       string    `json:"host",bson:"host"`
	UserName   string    `json:"username",bson:"username"`
	Password   string    `json:"password",bson:"password"`
	Database   string    `json:"database",bson:"database"`
	Collection string    `json:"collection",bson:"collection"`
	Settings   toolkit.M `json:"settings",bson:"settings"`
}
<|endoftext|>


<commit_before>// Copyright 2016 The Govisor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ui

import (
	"github.com/gdamore/tcell"
	"github.com/gdamore/tcell/views"
)

type HelpPanel struct {
	text *views.TextArea
	Panel
}

func (h *HelpPanel) HandleEvent(ev tcell.Event) bool {
	switch ev := ev.(type) {
	case *tcell.EventKey:
		switch ev.Key() {
		case tcell.KeyEsc:
			h.App().ShowMain()
			return true
		case tcell.KeyRune:
			switch ev.Rune() {
			case 'Q', 'q':
				h.app.ShowMain()
				return true
			}
		}
	}
	return h.Panel.HandleEvent(ev)
}

func (h *HelpPanel) Draw() {
	h.SetKeys([]string{"[ESC] Main"})
	h.SetTitle("Help")
	h.Panel.Draw()
}

func (h *HelpPanel) Init(app *App) {

	h.Panel.Init(app)

	// No, we don't have context-sensitive help.
	h.text = views.NewTextArea()
	h.text.SetLines([]string{
		"Supported keys (not all keys available in all contexts)",
		"",
		"  <ESC>          : return to main screen",
		"  <CTRL-C>       : quit",
		"  <CTRL-L>       : refresh the screeen",
		"  <H>            : show this help",
		"  <UP>, <DOWN>   : navigation",
		"  <E>            : enable selected service",
		"  <D>            : disable selected service",
		"  <I>            : view detailed information for service",
		"  <R>            : restart selected service",
		"  <C>            : clear faults on selected service",
		"  <L>            : view log for selected service",
		"",
		"This program is distributed under the Apache 2.0 License",
		"Copyright 2016 The Govisor Authors",
	})
	h.SetContent(h.text)
}

func NewHelpPanel(app *App) *HelpPanel {

	h := &HelpPanel{}

	h.Init(app)
	return h
}
<commit_msg>fixes #46 shortcut to go to end in log view<commit_after>// Copyright 2016 The Govisor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ui

import (
	"github.com/gdamore/tcell"
	"github.com/gdamore/tcell/views"
)

type HelpPanel struct {
	text *views.TextArea
	Panel
}

func (h *HelpPanel) HandleEvent(ev tcell.Event) bool {
	switch ev := ev.(type) {
	case *tcell.EventKey:
		switch ev.Key() {
		case tcell.KeyEsc:
			h.App().ShowMain()
			return true
		case tcell.KeyRune:
			switch ev.Rune() {
			case 'Q', 'q':
				h.app.ShowMain()
				return true
			}
		}
	}
	return h.Panel.HandleEvent(ev)
}

func (h *HelpPanel) Draw() {
	h.SetKeys([]string{"[ESC] Main"})
	h.SetTitle("Help")
	h.Panel.Draw()
}

func (h *HelpPanel) Init(app *App) {

	h.Panel.Init(app)

	// No, we don't have context-sensitive help.
	h.text = views.NewTextArea()
	h.text.SetLines([]string{
		"Supported keys (not all keys available in all contexts)",
		"",
		"  <ESC>          : return to main screen",
		"  <CTRL-C>       : quit",
		"  <CTRL-L>       : refresh the screeen",
		"  <H>            : show this help",
		"  <UP>,   <DOWN> : navigation",
		"  <PGUP>, <PGDN>",
		"  <HOME>, <END>",
		"  <E>            : enable selected service",
		"  <D>            : disable selected service",
		"  <I>            : view detailed information for service",
		"  <R>            : restart selected service",
		"  <C>            : clear faults on selected service",
		"  <L>            : view log for selected service",
		"",
		"This program is distributed under the Apache 2.0 License",
		"Copyright 2016 The Govisor Authors",
	})
	h.SetContent(h.text)
}

func NewHelpPanel(app *App) *HelpPanel {

	h := &HelpPanel{}

	h.Init(app)
	return h
}
<|endoftext|>


<commit_before>package config

import (
	"errors"
	"fmt"
	"os"
	"syscall"
)

type closedError struct {
	flockErr error
	fileErr  error
}

func (ce closedError) Error() string {
	return fmt.Sprintf("%s, %s", ce.fileErr.Error(), ce.flockErr.Error())
}

func newClosedError(flockErr, fileErr error) error {
	if fileErr == nil {
		fileErr = errors.New("no file errors")
	}

	if flockErr == nil {
		flockErr = errors.New("no lock errors")
	}

	return closedError{flockErr, fileErr}
}

func createOrOpenLockedFile(name string) (file *os.File, err error) {
	if _, err := os.Stat(name); os.IsNotExist(err) {
		file, err = os.Create(name)
	} else {
		file, err = os.OpenFile(name, os.O_RDWR, 0644)
	}

	if err != nil {
		return
	}

	if flockErr := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); flockErr != nil {
		err = flockErr
	}

	return
}

func closeLockedFile(file *os.File) error {
	flockErr := syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
	fileErr := file.Close()

	if flockErr != nil || fileErr != nil {
		return newClosedError(flockErr, fileErr)
	}

	return nil
}
<commit_msg>Return the multierror as a pointer<commit_after>package config

import (
	"errors"
	"fmt"
	"os"
	"syscall"
)

type closedError struct {
	flockErr error
	fileErr  error
}

func (ce closedError) Error() string {
	return fmt.Sprintf("%s, %s", ce.fileErr.Error(), ce.flockErr.Error())
}

func newClosedError(flockErr, fileErr error) error {
	if fileErr == nil {
		fileErr = errors.New("no file errors")
	}

	if flockErr == nil {
		flockErr = errors.New("no lock errors")
	}

	return &closedError{flockErr, fileErr}
}

func createOrOpenLockedFile(name string) (file *os.File, err error) {
	if _, err := os.Stat(name); os.IsNotExist(err) {
		file, err = os.Create(name)
	} else {
		file, err = os.OpenFile(name, os.O_RDWR, 0644)
	}

	if err != nil {
		return
	}

	if flockErr := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); flockErr != nil {
		err = flockErr
	}

	return
}

func closeLockedFile(file *os.File) error {
	flockErr := syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
	fileErr := file.Close()

	if flockErr != nil || fileErr != nil {
		return newClosedError(flockErr, fileErr)
	}

	return nil
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/iam"
	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSRolePolicyAttachment_basic(t *testing.T) {
	var out iam.ListAttachedRolePoliciesOutput
	rInt := acctest.RandInt()
	testPolicy := fmt.Sprintf("test-policy-%d", rInt)
	testPolicy2 := fmt.Sprintf("test-policy2-%d", rInt)
	testPolicy3 := fmt.Sprintf("test-policy3-%d", rInt)

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSRolePolicyAttachmentDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccAWSRolePolicyAttachConfig(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 1, &out),
					testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy}, &out),
				),
			},
			{
				Config: testAccAWSRolePolicyAttachConfigUpdate(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 2, &out),
					testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy2, testPolicy3}, &out),
				),
			},
		},
	})
}
func testAccCheckAWSRolePolicyAttachmentDestroy(s *terraform.State) error {
	return nil
}

func testAccCheckAWSRolePolicyAttachmentExists(n string, c int, out *iam.ListAttachedRolePoliciesOutput) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No policy name is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).iamconn
		role := rs.Primary.Attributes["role"]

		attachedPolicies, err := conn.ListAttachedRolePolicies(&iam.ListAttachedRolePoliciesInput{
			RoleName: aws.String(role),
		})
		if err != nil {
			return fmt.Errorf("Error: Failed to get attached policies for role %s (%s)", role, n)
		}
		if c != len(attachedPolicies.AttachedPolicies) {
			return fmt.Errorf("Error: Role (%s) has wrong number of policies attached on initial creation", n)
		}

		*out = *attachedPolicies
		return nil
	}
}
func testAccCheckAWSRolePolicyAttachmentAttributes(policies []string, out *iam.ListAttachedRolePoliciesOutput) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		matched := 0

		for _, p := range policies {
			for _, ap := range out.AttachedPolicies {
				// *ap.PolicyArn like arn:aws:iam::111111111111:policy/test-policy
				parts := strings.Split(*ap.PolicyArn, "/")
				if len(parts) == 2 && p == parts[1] {
					matched++
				}
			}
		}
		if matched != len(policies) || matched != len(out.AttachedPolicies) {
			return fmt.Errorf("Error: Number of attached policies was incorrect: expected %d matched policies, matched %d of %d", len(policies), matched, len(out.AttachedPolicies))
		}
		return nil
	}
}

func testAccAWSRolePolicyAttachConfig(rInt int) string {
	return fmt.Sprintf(`
	resource "aws_iam_role" "role" {
			name = "test-role-%d"
			assume_role_policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": "sts:AssumeRole",
			"Principal": {
				"Service": "ec2.amazonaws.com"
			},
			"Effect": "Allow",
			"Sid": ""
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy" {
			name = "test-policy-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_role_policy_attachment" "test-attach" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy.arn}"
	}`, rInt, rInt)
}

func testAccAWSRolePolicyAttachConfigUpdate(rInt int) string {
	return fmt.Sprintf(`
	resource "aws_iam_role" "role" {
			name = "test-role-%d"
			assume_role_policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": "sts:AssumeRole",
			"Principal": {
				"Service": "ec2.amazonaws.com"
			},
			"Effect": "Allow",
			"Sid": ""
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy" {
			name = "test-policy-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy2" {
			name = "test-policy2-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy3" {
			name = "test-policy3-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_role_policy_attachment" "test-attach" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy2.arn}"
	}

	resource "aws_iam_role_policy_attachment" "test-attach2" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy3.arn}"
	}`, rInt, rInt, rInt, rInt)
}
<commit_msg>update test-resource names<commit_after>package aws

import (
	"fmt"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/iam"
	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSRolePolicyAttachment_basic(t *testing.T) {
	var out iam.ListAttachedRolePoliciesOutput
	rInt := acctest.RandInt()
	testPolicy := fmt.Sprintf("tf-acctest-%d", rInt)
	testPolicy2 := fmt.Sprintf("tf-acctest2-%d", rInt)
	testPolicy3 := fmt.Sprintf("tf-acctest3-%d", rInt)

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSRolePolicyAttachmentDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccAWSRolePolicyAttachConfig(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 1, &out),
					testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy}, &out),
				),
			},
			{
				Config: testAccAWSRolePolicyAttachConfigUpdate(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 2, &out),
					testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy2, testPolicy3}, &out),
				),
			},
		},
	})
}
func testAccCheckAWSRolePolicyAttachmentDestroy(s *terraform.State) error {
	return nil
}

func testAccCheckAWSRolePolicyAttachmentExists(n string, c int, out *iam.ListAttachedRolePoliciesOutput) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No policy name is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).iamconn
		role := rs.Primary.Attributes["role"]

		attachedPolicies, err := conn.ListAttachedRolePolicies(&iam.ListAttachedRolePoliciesInput{
			RoleName: aws.String(role),
		})
		if err != nil {
			return fmt.Errorf("Error: Failed to get attached policies for role %s (%s)", role, n)
		}
		if c != len(attachedPolicies.AttachedPolicies) {
			return fmt.Errorf("Error: Role (%s) has wrong number of policies attached on initial creation", n)
		}

		*out = *attachedPolicies
		return nil
	}
}
func testAccCheckAWSRolePolicyAttachmentAttributes(policies []string, out *iam.ListAttachedRolePoliciesOutput) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		matched := 0

		for _, p := range policies {
			for _, ap := range out.AttachedPolicies {
				// *ap.PolicyArn like arn:aws:iam::111111111111:policy/test-policy
				parts := strings.Split(*ap.PolicyArn, "/")
				if len(parts) == 2 && p == parts[1] {
					matched++
				}
			}
		}
		if matched != len(policies) || matched != len(out.AttachedPolicies) {
			return fmt.Errorf("Error: Number of attached policies was incorrect: expected %d matched policies, matched %d of %d", len(policies), matched, len(out.AttachedPolicies))
		}
		return nil
	}
}

func testAccAWSRolePolicyAttachConfig(rInt int) string {
	return fmt.Sprintf(`
	resource "aws_iam_role" "role" {
			name = "test-role-%d"
			assume_role_policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": "sts:AssumeRole",
			"Principal": {
				"Service": "ec2.amazonaws.com"
			},
			"Effect": "Allow",
			"Sid": ""
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy" {
			name = "tf-acctest-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_role_policy_attachment" "test-attach" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy.arn}"
	}`, rInt, rInt)
}

func testAccAWSRolePolicyAttachConfigUpdate(rInt int) string {
	return fmt.Sprintf(`
	resource "aws_iam_role" "role" {
			name = "test-role-%d"
			assume_role_policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": "sts:AssumeRole",
			"Principal": {
				"Service": "ec2.amazonaws.com"
			},
			"Effect": "Allow",
			"Sid": ""
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy" {
			name = "tf-acctest-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy2" {
			name = "tf-acctest2-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_policy" "policy3" {
			name = "tf-acctest3-%d"
			description = "A test policy"
			policy = <<EOF
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Action": [
				"iam:ChangePassword"
			],
			"Resource": "*",
			"Effect": "Allow"
		}
	]
}
EOF
	}

	resource "aws_iam_role_policy_attachment" "test-attach" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy2.arn}"
	}

	resource "aws_iam_role_policy_attachment" "test-attach2" {
			role = "${aws_iam_role.role.name}"
			policy_arn = "${aws_iam_policy.policy3.arn}"
	}`, rInt, rInt, rInt, rInt)
}
<|endoftext|>


<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bytes

import "os"

// Efficient construction of large strings and byte arrays.
// Implements io.Reader and io.Writer.

// A Buffer provides efficient construction of large strings
// and slices of bytes.  It implements io.Reader and io.Writer.
// Appends (writes) are efficient.
// The zero value for Buffer is an empty buffer ready to use.
type Buffer struct {
	blk	[]block;
	len	int;
	oneByte	[1]byte;
}

// There are two kinds of block: a string or a []byte.
// When the user writes big strings, we add string blocks;
// when the user writes big byte slices, we add []byte blocks.
// Small writes are coalesced onto the end of the last block,
// whatever it is.
// This strategy is intended to reduce unnecessary allocation.
type block interface {
	Len()	int;
	String()	string;
	appendBytes(s []byte);
	appendString(s string);
	setSlice(m, n int);
}

// stringBlocks represent strings. We use pointer receivers
// so append and setSlice can overwrite the receiver.
type stringBlock string

func (b *stringBlock) Len() int {
	return len(*b)
}

func (b *stringBlock) String() string {
	return string(*b)
}

func (b *stringBlock) appendBytes(s []byte) {
	*b += stringBlock(s)
}

func (b *stringBlock) appendString(s string) {
	*b = stringBlock(s)
}

func (b *stringBlock) setSlice(m, n int) {
	*b = (*b)[m:n]
}

// byteBlock represent slices of bytes.  We use pointer receivers
// so append and setSlice can overwrite the receiver.
type byteBlock []byte

func (b *byteBlock) Len() int {
	return len(*b)
}

func (b *byteBlock) String() string {
	return string(*b)
}

func (b *byteBlock) resize(max int) {
	by := []byte(*b);
	if cap(by) >= max {
		by = by[0:max];
	} else {
		nby := make([]byte, max, 3*(max+10)/2);
		copyBytes(nby, 0, by);
		by = nby;
	}
	*b = by;
}

func (b *byteBlock) appendBytes(s []byte) {
	curLen := b.Len();
	b.resize(curLen + len(s));
	copyBytes([]byte(*b), curLen, s);
}

func (b *byteBlock) appendString(s string) {
	curLen := b.Len();
	b.resize(curLen + len(s));
	copyString([]byte(*b), curLen, s);
}

func (b *byteBlock) setSlice(m, n int) {
	*b = (*b)[m:n]
}

// Because the user may overwrite the contents of byte slices, we need
// to make a copy.  Allocation strategy: leave some space on the end so
// small subsequent writes can avoid another allocation.  The input
// is known to be non-empty.
func newByteBlock(s []byte) *byteBlock {
	l := len(s);
	// Capacity with room to grow.  If small, allocate a mininum.  If medium,
	// double the size.  If huge, use the size plus epsilon (room for a newline,
	// at least).
	c := l;
	switch {
	case l < 32:
		c = 64
	case l < 1<<18:
		c *= 2;
	default:
		c += 8
	}
	b := make([]byte, l, c);
	copyBytes(b, 0, s);
	return &b;
}

// Copy from block to byte array at offset doff.  Assume there's room.
func copy(dst []byte, doff int, src block) {
	switch s := src.(type) {
	case *stringBlock:
		copyString(dst, doff, string(*s));
	case *byteBlock:
		copyBytes(dst, doff, []byte(*s));
	}
}

// Copy from string to byte array at offset doff.  Assume there's room.
func copyString(dst []byte, doff int, str string) {
	for soff := 0; soff < len(str); soff++ {
		dst[doff] = str[soff];
		doff++;
	}
}

// Copy from bytes to byte array at offset doff.  Assume there's room.
func copyBytes(dst []byte, doff int, src []byte) {
	for soff := 0; soff < len(src); soff++ {
		dst[doff] = src[soff];
		doff++;
	}
}

// Bytes returns the contents of the unread portion of the buffer
// as a byte array.
func (b *Buffer) Bytes() []byte {
	n := b.len;
	bytes := make([]byte, n);
	nbytes := 0;
	for _, s := range b.blk {
		copy(bytes, nbytes, s);
		nbytes += s.Len();
	}
	return bytes;
}

// String returns the contents of the unread portion of the buffer
// as a string.
func (b *Buffer) String() string {
	if len(b.blk) == 1 {	// important special case
		return b.blk[0].String()
	}
	return string(b.Bytes())
}

// Len returns the number of bytes in the unread portion of the buffer;
// b.Len() == len(b.Bytes()) == len(b.String()).
func (b *Buffer) Len() int {
	return b.len
}

// Truncate discards all but the first n unread bytes from the buffer.
func (b *Buffer) Truncate(n int) {
	b.len = 0;	// recompute during scan.
	for i, s := range b.blk {
		if n <= 0 {
			b.blk = b.blk[0:i];
			break;
		}
		if l := s.Len(); n < l {
			b.blk[i].setSlice(0, n);
			b.len += n;
			n = 0;
		} else {
			b.len += l;
			n -= l;
		}
	}
}

// Reset resets the buffer so it has no content.
// b.Reset() is the same as b.Truncate(0).
func (b *Buffer) Reset() {
	b.blk = b.blk[0:0];
	b.len = 0;
}

// Can n bytes be appended efficiently to the end of the final string?
func (b *Buffer) canCombine(n int) bool {
	return len(b.blk) > 0 && n+b.blk[len(b.blk)-1].Len() <= 64
}

// WriteString appends string s to the buffer.  The return
// value n is the length of s; err is always nil.
func (b *Buffer) WriteString(s string) (n int, err os.Error) {
	n = len(s);
	if n == 0 {
		return
	}
	b.len += n;
	numStr := len(b.blk);
	// Special case: If the last piece is short and this one is short,
	// combine them and avoid growing the list.
	if b.canCombine(n) {
		b.blk[numStr-1].appendString(s);
		return
	}
	if cap(b.blk) == numStr {
		nstr := make([]block, numStr, 3*(numStr+10)/2);
		for i, s := range b.blk {
			nstr[i] = s;
		}
		b.blk = nstr;
	}
	b.blk = b.blk[0:numStr+1];
	// The string is immutable; no need to make a copy.
	b.blk[numStr] = (*stringBlock)(&s);
	return
}

// Write appends the contents of p to the buffer.  The return
// value n is the length of p; err is always nil.
func (b *Buffer) Write(p []byte) (n int, err os.Error) {
	n = len(p);
	if n == 0 {
		return
	}
	b.len += n;
	numStr := len(b.blk);
	// Special case: If the last piece is short and this one is short,
	// combine them and avoid growing the list.
	if b.canCombine(n) {
		b.blk[numStr-1].appendBytes(p);
		return
	}
	if cap(b.blk) == numStr {
		nstr := make([]block, numStr, 3*(numStr+10)/2);
		for i, s := range b.blk {
			nstr[i] = s;
		}
		b.blk = nstr;
	}
	b.blk = b.blk[0:numStr+1];
	// Need to copy the data - user might overwrite the data.
	b.blk[numStr] = newByteBlock(p);
	return
}

// WriteByte appends the byte c to the buffer.
// The returned error is always nil, but is included
// to match bufio.Writer's WriteByte.
func (b *Buffer) WriteByte(c byte) os.Error {
	b.oneByte[0] = c;
	// For WriteByte, canCombine is almost always true so it's worth
	// doing here.
	if b.canCombine(1) {
		b.blk[len(b.blk)-1].appendBytes(&b.oneByte);
		b.len++;
		return nil
	}
	b.Write(&b.oneByte);
	return nil;
}

// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained.  The return value n is the number of bytes read.  If the
// buffer has no data to return, err is os.EOF even if len(p) is zero;
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int, err os.Error) {
	if len(b.blk) == 0 {
		return 0, os.EOF
	}
	for len(b.blk) > 0 {
		blk := b.blk[0];
		m := len(p) - n;
		if l := blk.Len(); m >= l {
			// consume all of this string.
			copy(p, n, blk);
			n += l;
			b.blk = b.blk[1:len(b.blk)];
		} else {
			// consume some of this block; it's the last piece.
			switch b := blk.(type) {
			case *stringBlock:
				copyString(p, n, string(*b)[0:m]);
			case *byteBlock:
				copyBytes(p, n, []byte(*b)[0:m]);
			}
			n += m;
			b.blk[0].setSlice(m, l);
			break;
		}
	}
	b.len -= n;
	return
}

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error os.EOF.
func (b *Buffer) ReadByte() (c byte, err os.Error) {
	if _, err := b.Read(&b.oneByte); err != nil {
		return 0, err
	}
	return b.oneByte[0], nil
}

// NewBufferString creates and initializes a new Buffer
// using a string as its initial contents.
func NewBufferString(str string) *Buffer {
	b := new(Buffer);
	if len(str) > 0 {
		b.blk = make([]block, 1, 10);	// room to grow
		b.blk[0] = (*stringBlock)(&str);
	}
	b.len = len(str);
	return b;
}

// NewBuffer creates and initializes a new Buffer
// using a byte slice as its initial contents.
func NewBuffer(by []byte) *Buffer {
	b := new(Buffer);
	if len(by) > 0 {
		b.blk = make([]block, 1, 10);	// room to grow
		b.blk[0] = (*byteBlock)(&by);
	}
	b.len = len(by);
	return b;
}
<commit_msg>restore the old algorithm. the new one is more memory efficient in large cases but too slow across the board.<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bytes

// Simple byte buffer for marshaling data.

import (
	"os";
)

// Copy from string to byte array at offset doff.  Assume there's room.
func copyString(dst []byte, doff int, str string) {
	for soff := 0; soff < len(str); soff++ {
		dst[doff] = str[soff];
		doff++;
	}
}

// Copy from bytes to byte array at offset doff.  Assume there's room.
func copyBytes(dst []byte, doff int, src []byte) {
	for soff := 0; soff < len(src); soff++ {
		dst[doff] = src[soff];
		doff++;
	}
}

// A Buffer is a variable-sized buffer of bytes
// with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
type Buffer struct {
	buf	[]byte;	// contents are the bytes buf[off : len(buf)]
	off	int;	// read at &buf[off], write at &buf[len(buf)]
	oneByte	[]byte;	// avoid allocation of slice on each WriteByte
}

// Bytes returns the contents of the unread portion of the buffer;
// len(b.Bytes()) == b.Len().
func (b *Buffer) Bytes() []byte {
	return b.buf[b.off : len(b.buf)]
}

// String returns the contents of the unread portion of the buffer
// as a string.
func (b *Buffer) String() string {
	return string(b.buf[b.off : len(b.buf)])
}

// Len returns the number of bytes of the unread portion of the buffer;
// b.Len() == len(b.Bytes()).
func (b *Buffer) Len() int {
	return len(b.buf) - b.off
}

// Truncate discards all but the first n unread bytes from the buffer.
// It is an error to call b.Truncate(n) with n > b.Len().
func (b *Buffer) Truncate(n int) {
	if n == 0 {
		// Reuse buffer space.
		b.off = 0;
	}
	b.buf = b.buf[0 : b.off + n];
}

// Reset resets the buffer so it has no content.
// b.Reset() is the same as b.Truncate(0).
func (b *Buffer) Reset() {
	b.Truncate(0);
}

// Write appends the contents of p to the buffer.  The return
// value n is the length of p; err is always nil.
func (b *Buffer) Write(p []byte) (n int, err os.Error) {
	m := b.Len();
	n = len(p);

	if len(b.buf) + n > cap(b.buf) {
		// not enough space at end
		buf := b.buf;
		if m + n > cap(b.buf) {
			// not enough space anywhere
			buf = make([]byte, 2*cap(b.buf) + n)
		}
		copyBytes(buf, 0, b.buf[b.off:b.off+m]);
		b.buf = buf;
		b.off = 0
	}

	b.buf = b.buf[0 : b.off + m + n];
	copyBytes(b.buf, b.off + m, p);
	return n, nil
}

// WriteString appends the contents of s to the buffer.  The return
// value n is the length of s; err is always nil.
func (b *Buffer) WriteString(s string) (n int, err os.Error) {
	m := b.Len();
	n = len(s);

	if len(b.buf) + n > cap(b.buf) {
		// not enough space at end
		buf := b.buf;
		if m + n > cap(b.buf) {
			// not enough space anywhere
			buf = make([]byte, 2*cap(b.buf) + n)
		}
		copyBytes(buf, 0, b.buf[b.off:b.off+m]);
		b.buf = buf;
		b.off = 0
	}

	b.buf = b.buf[0 : b.off + m + n];
	copyString(b.buf, b.off+m, s);
	return n, nil
}

// WriteByte appends the byte c to the buffer.
// The returned error is always nil, but is included
// to match bufio.Writer's WriteByte.
func (b *Buffer) WriteByte(c byte) os.Error {
	if b.oneByte == nil {
		// Only happens once per Buffer, and then we have a slice.
		b.oneByte = make([]byte, 1);
	}
	b.oneByte[0] = c;
	b.Write(b.oneByte);
	return nil;
}

// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained.  The return value n is the number of bytes read.  If the
// buffer has no data to return, err is os.EOF even if len(p) is zero;
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int, err os.Error) {
	if b.off >= len(b.buf) {
		return 0, os.EOF
	}
	m := b.Len();
	n = len(p);

	if n > m {
		// more bytes requested than available
		n = m
	}

	copyBytes(p, 0, b.buf[b.off:b.off+n]);
	b.off += n;
	return n, err
}

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error os.EOF.
func (b *Buffer) ReadByte() (c byte, err os.Error) {
	if b.off >= len(b.buf) {
		return 0, os.EOF;
	}
	c = b.buf[b.off];
	b.off++;
	return c, nil;
}

// NewBuffer creates and initializes a new Buffer
// using buf as its initial contents.
func NewBuffer(buf []byte) *Buffer {
	return &Buffer{buf: buf};
}

// NewBufferString creates and initializes a new Buffer
// using string s as its initial contents.
func NewBufferString(s string) *Buffer {
	buf := make([]byte, len(s));
	copyString(buf, 0, s);
	return &Buffer{buf: buf};
}
<|endoftext|>