示例#1
0
// CheckPkg checks if versioned package is in records, and download it when needed.
func CheckPkg(importPath, rev string) (*Revision, error) {
	// Check package record.
	pkg, err := GetPakcageByPath(importPath)
	if err != nil {
		if err != ErrPackageNotExist {
			return nil, err
		}
		blocked, blockErr, err := IsPackageBlocked(importPath)
		if err != nil {
			return nil, err
		} else if blocked {
			return nil, blockErr
		}
	}

	n := archive.NewNode(importPath, rev)

	// Get and check revision record.
	if err = n.GetRevision(); err != nil {
		return nil, err
	}

	var r *Revision
	if pkg != nil {
		r, err = GetRevision(pkg.Id, n.Revision)
		if err != nil && err != ErrRevisionNotExist {
			return nil, err
		}
	}

	// return nil, fmt.Errorf("Revision: %s", n.Revision)

	if r == nil || (r.Storage == LOCAL && !com.IsFile(n.ArchivePath)) {
		if err := n.Download(); err != nil {
			return nil, err
		}
	}

	if pkg == nil {
		pkg, err = NewPackage(n.ImportPath)
		if err != nil {
			return nil, err
		}
	}

	if r == nil {
		r = &Revision{
			PkgId:    pkg.Id,
			Revision: n.Revision,
		}
		_, err = x.Insert(r)
	} else {
		_, err = x.Id(r.Id).Update(r)
	}
	return r, nil
}
示例#2
0
func GetRevision(ctx *middleware.Context) {
	importPath := archive.GetRootPath(ctx.Query("pkgname"))
	rev := ctx.Query("revision")
	n := archive.NewNode(importPath, rev)
	if err := n.GetRevision(); err != nil {
		ctx.JSON(422, map[string]interface{}{
			"error": err.Error(),
		})
		return
	}
	ctx.JSON(200, map[string]interface{}{
		"sha": n.Revision,
	})
}