Beispiel #1
0
// GitCommitHash gets the head commit of the master branch.
func GitCommitHash(path string) (string, error) {
	srcPath, err := repoSrcPath(path)
	if err != nil {
		return "", err
	}

	return git.CommitHash(srcPath)
}
Beispiel #2
0
// Build builds a repo into a repodb build.
// It performs static analysis on the repository.
func Build(src, path string) (*repodb.Build, []error) {
	buildHash, err := git.CommitHash(src)
	if err != nil {
		return nil, []error{err}
	}

	lang := pl.LangGoLike()
	input := builds.NewDirHome(src, lang)
	input.AddLang("asm", asm.Lang())

	output := builds.NewMemHome(lang)
	pkgs := input.Pkgs(".")
	for _, p := range pkgs {
		output.NewPkg(p)
	}
	deps := new(repodb.Deps)
	fileToks := make(map[string][]*lexing.Token)

	b := builds.NewBuilder(input, output)
	b.Verbose = true
	b.InitPC = arch.InitPC
	b.RunTests = false
	b.StaticOnly = true
	b.SaveDeps = func(m *dagvis.Map) {
		deps.Pkgs = dagvis.JSONMap(m)
	}
	b.SaveFileTokens = func(p string, toks []*lexing.Token) {
		fileToks[p] = toks
	}

	es := b.BuildPkgs(pkgs)
	if es != nil {
		return nil, lexErrors(es)
	}

	deps.Files = make(map[string]interface{})
	for _, pkg := range pkgs {
		bs := output.OutputBytes(pkg, "depmap")
		if bs == nil {
			continue
		}
		var m map[string]interface{}
		err := json.Unmarshal(bs, &m)
		if err != nil {
			return nil, []error{err}
		}
		deps.Files[pkg] = m
	}

	files := make(map[string]*repodb.File)
	for _, pkg := range pkgs {
		srcFiles := input.Src(pkg)
		for name, f := range srcFiles {
			p := filepath.Join(pkg, name)
			toks := fileToks[p]
			if toks == nil {
				continue
			}

			res, err := renderFile(f, toks)
			if err != nil {
				return nil, []error{err}
			}

			p = filepath.Join(path, p)
			files[p] = res
		}
	}

	return &repodb.Build{
		Name:  path,
		Build: buildHash,
		Lang:  "g",
		Deps:  deps,
		Files: files,
	}, nil
}