コード例 #1
0
ファイル: spec_test.go プロジェクト: pombredanne/bar
func Test_Proto_Spec1(t *testing.T) {
	m1, err := proto.NewFromManifest(fixtures.CleanInput(`BAR:MANIFEST

		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234


		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234
		offset 0
	`))
	assert.NoError(t, err)
	m2, err := proto.NewFromManifest(fixtures.CleanInput(`BAR:MANIFEST

		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234


		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234
		offset 0
	`))
	assert.NoError(t, err)

	spec, err := proto.NewSpec(time.Now().UnixNano(), map[string]proto.ID{
		"file/1": m1.ID,
		"file/2": m2.ID,
	}, []string{})

	// hand-made fixture
	var sorted sort.StringSlice
	sorted = append(sorted, "file/2")
	sorted = append(sorted, "file/1")
	sorted.Sort()

	hasher := sha3.New256()
	var id []byte

	err = m1.ID.Decode(id)
	assert.NoError(t, err)

	_, err = hasher.Write([]byte(sorted[0]))
	_, err = hasher.Write(id)

	_, err = hasher.Write([]byte(sorted[1]))
	_, err = hasher.Write(id)

	assert.Equal(t, spec.ID, proto.ID(hex.EncodeToString(hasher.Sum(nil))))
}
コード例 #2
0
ファイル: model.go プロジェクト: pombredanne/bar
// Get manifest by filename or given reader
func (m *Model) GetManifest(name string, in io.Reader) (res *proto.Manifest, err error) {
	r, isManifest, err := proto.PeekManifest(in)
	if err != nil {
		return
	}

	if isManifest {
		// ok - just read
		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
}
コード例 #3
0
ファイル: manifest_test.go プロジェクト: pombredanne/bar
func Test_Shadow_New1(t *testing.T) {
	in := `BAR:MANIFEST

		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234


		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234
		offset 0
		`
	m, err := proto.NewFromManifest(fixtures.CleanInput(in))
	assert.NoError(t, err)
	assert.Equal(t, fixtures.FixStream(in), (*m).String())
}
コード例 #4
0
ファイル: marshal_test.go プロジェクト: pombredanne/bar
func Test_Manifest_MarshalThrift(t *testing.T) {
	in := `BAR:MANIFEST

		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234


		id ac934d9a88b42aa3b40ef7c81a9dee1aad5a2cddccb00ae6abab9c38095fc15c
		size 1234
		offset 0
		`
	m, err := proto.NewFromManifest(fixtures.CleanInput(in))
	assert.NoError(t, err)

	_, err = (*m).MarshalThrift()
	assert.NoError(t, err)
}
コード例 #5
0
ファイル: model.go プロジェクト: 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
}
コード例 #6
0
ファイル: git.go プロジェクト: akaspin/bar
/*
Extract manifests from diff

	$ git diff --staged --cached -U99999999999999 --no-prefix
	diff --git fix/aa.txt fix/aa.txt
	index 5afd5028d71cfadf73c0e3abd70f852d67357909..63c202a03152c8635eb78dcfef35859f0c68f5cf 100644
	--- fix/aa.txt
	+++ fix/aa.txt
	@@ -1,10 +1,10 @@
	 BAR:MANIFEST

	-id aba7aeb8a7948dd0cdb8eeb9239e5d1dab2bd840f13930f86f6e67ba40ea5350
	-size 4
	+id f627c8f9355399ef45e1a6b6e5a9e6a3abcb3e1b6255603357bffa9f2211ba7e
	+size 6


	-id aba7aeb8a7948dd0cdb8eeb9239e5d1dab2bd840f13930f86f6e67ba40ea5350
	-size 4
	+id f627c8f9355399ef45e1a6b6e5a9e6a3abcb3e1b6255603357bffa9f2211ba7e
	+size 6
	 offset 0

		...

	diff --git fix/aa2.txt fix/aa2.txt
	new file mode 100644
	index 0000000000000000000000000000000000000000..63c202a03152c8635eb78dcfef35859f0c68f5cf
	--- /dev/null
	+++ fix/aa2.txt         <--- Start consuming
	@@ -0,0 +1,10 @@        <--- if consuming - next line
	+BAR:MANIFEST           <--- Start consuming manifest by "" and "+"
	+
	+id f627c8f9355399ef45e1a6b6e5a9e6a3abcb3e1b6255603357bffa9f2211ba7e
	+size 6
	+
	+
	+id f627c8f9355399ef45e1a6b6e5a9e6a3abcb3e1b6255603357bffa9f2211ba7e
	+size 6
	+offset 0
	+
*/
func (g *Git) ManifestsFromDiff(r io.Reader) (res lists.BlobMap, err error) {
	res = lists.BlobMap{}

	var data []byte
	var line string
	buf := bufio.NewReader(r)

	var filename string
	var w *bytes.Buffer
	var hunt, eof bool

	for {
		data, _, err = buf.ReadLine()
		if err == io.EOF {
			err = nil
			eof = true
		} else if err != nil {
			return
		} else {
			line = strings.TrimSpace(string(data))
		}

		if eof || strings.HasPrefix(line, "diff --git ") {
			if hunt {
				mn, err := proto.NewFromManifest(bytes.NewReader(w.Bytes()))
				if err != nil {
					return res, err
				}
				// need to lift filenames from root
				f1, err := g.FromRoot(filename)
				if err != nil {
					return res, err
				}
				res[f1[0]] = *mn
			}
			filename = ""
			w = nil
			hunt = false
			if eof {
				return
			}
			continue
		}

		if strings.HasPrefix(line, "+++ ") {
			// new file - hunt
			filename = strings.TrimPrefix(line, "+++ ")
			continue
		}

		if strings.TrimPrefix(line, "+") == "BAR:MANIFEST" {
			// HUNT
			hunt = true
			w = new(bytes.Buffer)
			if _, err = fmt.Fprintln(w, "BAR:MANIFEST"); err != nil {
				return
			}
			continue
		}

		if hunt {
			line := strings.TrimPrefix(line, "+")
			if !strings.HasPrefix(line, "-") {
				fmt.Fprintln(w, line)
			}
		}
	}

	return
}