Ejemplo n.º 1
0
Archivo: git.go Proyecto: akaspin/bar
// Run git command
func (g *Git) Run(sub string, arg ...string) (res string, err error) {
	logx.Tracef("executing `git %s`", strings.Join(
		append([]string{"-C", g.Root, "--no-pager", sub}, arg...), " "))

	c := execCommand("git",
		append([]string{"-C", g.Root, "--no-pager", sub}, arg...)...)

	var out, stderr bytes.Buffer
	c.Stdout = &out
	c.Stderr = &stderr
	err = c.Run()
	if err != nil {
		err = fmt.Errorf("%s %s", err, stderr.String())
	}
	res = out.String()
	logx.Tracef("git >>> %s", res)
	return
}
Ejemplo n.º 2
0
func (m *Mapper) remap(from, to string, arg ...string) (res []string, err error) {
	logx.Tracef("remapping from %s to %s %s", from, to, arg)
	if filepath.ToSlash(from) == filepath.ToSlash(to) {
		res = arg
		return
	}
	var one string
	for _, p := range arg {
		if one, err = filepath.Rel(OSFromSlash(to), OSFromSlash(OSJoin(from, p))); err != nil {
			logx.Errorf(">>> %s %s %s", err, OSJoin(from, p), to)
			return
		}
		res = append(res, one)
	}
	logx.Tracef("remapped %s", res)

	return
}
Ejemplo n.º 3
0
func (m *Model) SquashBlobs(blobs lists.BlobMap) (err error) {
	logx.Tracef("squashing blobs %s", blobs.IDMap())

	var req, res []interface{}
	for _, v := range blobs.ToSlice() {
		req = append(req, v)
	}

	err = m.BatchPool.Do(
		func(ctx context.Context, in interface{}) (out interface{}, err error) {
			r := in.(lists.BlobLink)

			lock, err := m.FdLocks.Take()
			if err != nil {
				return
			}
			defer lock.Release()

			absname := filepath.Join(m.WD, r.Name)
			backName := absname + ".bar-backup"
			os.Rename(absname, absname+".bar-backup")
			os.MkdirAll(filepath.Dir(absname), 0755)

			w, err := os.Create(absname)
			if err != nil {
				return
			}
			err = r.Manifest.Serialize(w)
			if err != nil {
				os.Remove(absname)
				os.Rename(backName, absname)
				return
			}
			defer os.Remove(backName)
			logx.Debugf("squashed %s", r.Name)
			return
		},
		&req, &res, concurrency.DefaultBatchOptions().AllowErrors(),
	)
	if err != nil {
		return
	}

	logx.Infof("blob %s squashed successfully", blobs.Names())
	return
}
Ejemplo n.º 4
0
// Assemble target files from stored chunks
func (a *Assembler) Done(what lists.BlobMap) (err error) {
	logx.Tracef("assembling %s", what.Names())

	var req, res []interface{}
	for k, v := range what {
		req = append(req, lists.BlobLink{v, k})
	}

	err = a.model.BatchPool.Do(
		func(ctx context.Context, in interface{}) (out interface{}, err error) {
			r := in.(lists.BlobLink)

			lock, err := a.model.FdLocks.Take()
			if err != nil {
				return
			}
			defer lock.Release()

			w, err := os.Create(filepath.Join(a.model.WD,
				r.Name+r.Manifest.ID.String()))
			if err != nil {
				return
			}
			defer w.Close()

			for _, chunk := range r.Manifest.Chunks {
				if err = a.writeChunkTo(w, chunk.ID); err != nil {
					return
				}
			}
			err = a.commitBlob(r.Name, r.Manifest.ID)

			return
		}, &req, &res, concurrency.DefaultBatchOptions().AllowErrors(),
	)
	if err != nil {
		return
	}
	defer a.Close()
	return
}
Ejemplo n.º 5
0
Archivo: model.go Proyecto: akaspin/bar
func (m *Model) getManifest(name string, blobs, manifests bool) (res *proto.Manifest, err error) {
	lock, err := m.FdLocks.Take()
	if err != nil {
		return
	}
	defer lock.Release()

	logx.Tracef("feeding manifest from %s",
		lists.OSFromSlash(lists.OSJoin(m.WD, name)))
	f, err := os.Open(lists.OSFromSlash(lists.OSJoin(m.WD, name)))
	if err != nil {
		logx.Errorf("!!! %s", err)
		return
	}
	defer f.Close()

	var r io.Reader
	var isManifest bool
	if r, isManifest, err = proto.PeekManifest(f); err != nil {
		return
	}
	if (isManifest && !manifests) || (!isManifest && !blobs) {
		return
	}

	if isManifest {
		res, err = proto.NewFromManifest(r)
		return
	}
	// Hard way. First - try git
	var sideR io.Reader
	if m.Git != nil {
		if sideR = m.getGitReader(name); sideR != nil {
			res, err = proto.NewFromAny(sideR, m.chunkSize)
			return
		}
	}
	// No git - make from blob
	res, err = proto.NewFromBLOB(r, m.chunkSize)
	return
}