Exemplo n.º 1
0
func (s *BlameCommon) mockBlame(t blameTest, c *C) (blame *Blame) {
	repo, ok := s.repos[t.repo]
	c.Assert(ok, Equals, true)

	commit, err := repo.Commit(core.NewHash(t.rev))
	c.Assert(err, IsNil, Commentf("%v: repo=%s, rev=%s", err, repo, t.rev))

	file, err := commit.File(t.path)
	c.Assert(err, IsNil)
	lines := file.Lines()
	c.Assert(len(t.blames), Equals, len(lines), Commentf(
		"repo=%s, path=%s, rev=%s: the number of lines in the file and the number of expected blames differ (len(blames)=%d, len(lines)=%d)\nblames=%#q\nlines=%#q", t.repo, t.path, t.rev, len(t.blames), len(lines), t.blames, lines))

	blamedLines := make([]*line, 0, len(t.blames))
	for i := range t.blames {
		commit, err := repo.Commit(core.NewHash(t.blames[i]))
		c.Assert(err, IsNil)
		l := &line{
			author: commit.Author.Email,
			text:   lines[i],
		}
		blamedLines = append(blamedLines, l)
	}

	return &Blame{
		Path:  t.path,
		Rev:   core.NewHash(t.rev),
		Lines: blamedLines,
	}
}
Exemplo n.º 2
0
func (s *ReferencesSuite) TestRevList(c *C) {
	for _, t := range referencesTests {
		repo, ok := s.repos[t.repo]
		c.Assert(ok, Equals, true)

		commit, err := repo.Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil)

		revs, err := commit.References(t.path)
		c.Assert(err, IsNil)
		c.Assert(len(revs), Equals, len(t.revs))

		for i := range revs {
			if revs[i].Hash.String() != t.revs[i] {
				commit, err := repo.Commit(core.NewHash(t.revs[i]))
				c.Assert(err, IsNil)
				equiv, err := equivalent(t.path, revs[i], commit)
				c.Assert(err, IsNil)
				if equiv {
					fmt.Printf("cherry-pick detected: %s    %s\n", revs[i].Hash.String(), t.revs[i])
				} else {
					c.Fatalf("\nrepo=%s, commit=%s, path=%s, \n%s",
						t.repo, t.commit, t.path, compareSideBySide(t.revs, revs))
				}
			}
		}
	}
}
Exemplo n.º 3
0
func (s *SuiteCommon) TestGitUploadPackRequest(c *C) {
	r := &GitUploadPackRequest{}
	r.Want(core.NewHash("d82f291cde9987322c8a0c81a325e1ba6159684c"))
	r.Want(core.NewHash("2b41ef280fdb67a9b250678686a0c3e03b0a9989"))
	r.Have(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))

	c.Assert(r.String(), Equals,
		"0032want d82f291cde9987322c8a0c81a325e1ba6159684c\n"+
			"0032want 2b41ef280fdb67a9b250678686a0c3e03b0a9989\n"+
			"0032have 6ecf0ef2c2dffb796033e5a02219af86ec6584e5\n0000"+
			"0009done\n",
	)
}
Exemplo n.º 4
0
func (s *SuiteRemote) TestFetchNotConnected(c *C) {
	r := NewGitUploadPackService()
	pr := &common.GitUploadPackRequest{}
	pr.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
	_, err := r.Fetch(pr)
	c.Assert(err, Equals, ErrNotConnected)
}
Exemplo n.º 5
0
func makeHashSlice(hashes []string) []core.Hash {
	series := make([]core.Hash, 0, len(hashes))
	for _, member := range hashes {
		series = append(series, core.NewHash(member))
	}
	return series
}
Exemplo n.º 6
0
func AssertObjects(c *C, s *memory.ObjectStorage, expects []string) {
	c.Assert(len(expects), Equals, len(s.Objects))
	for _, expected := range expects {
		obtained, err := s.Get(core.NewHash(expected))
		c.Assert(err, IsNil)
		c.Assert(obtained.Hash().String(), Equals, expected)
	}
}
Exemplo n.º 7
0
// Ref returns the Hash pointing the given refName
func (r *Remote) Ref(refName string) (core.Hash, error) {
	ref, ok := r.upInfo.Refs[refName]
	if !ok {
		return core.NewHash(""), fmt.Errorf("unable to find ref %q", refName)
	}

	return ref, nil
}
Exemplo n.º 8
0
func (r *GitUploadPackInfo) readLine(line string) {
	parts := strings.Split(strings.Trim(line, " \n"), " ")
	if len(parts) != 2 {
		return
	}

	r.Refs[parts[1]] = core.NewHash(parts[0])
}
Exemplo n.º 9
0
func makeObjectSlice(hashes []string, storage core.ObjectStorage) []core.Object {
	series := make([]core.Object, 0, len(hashes))
	for _, member := range hashes {
		obj, err := storage.Get(core.NewHash(member))
		if err == nil {
			series = append(series, obj)
		}
	}
	return series
}
Exemplo n.º 10
0
// Decode transform an core.Object into a Blob struct
func (c *Commit) Decode(o core.Object) error {
	if o.Type() != core.CommitObject {
		return ErrUnsupportedObject
	}

	c.Hash = o.Hash()
	r := bufio.NewReader(o.Reader())

	var message bool
	for {
		line, err := r.ReadSlice('\n')
		if err != nil && err != io.EOF {
			return err
		}

		line = bytes.TrimSpace(line)
		if !message {
			if len(line) == 0 {
				message = true
				continue
			}

			split := bytes.SplitN(line, []byte{' '}, 2)
			switch string(split[0]) {
			case "tree":
				c.tree = core.NewHash(string(split[1]))
			case "parent":
				c.parents = append(c.parents, core.NewHash(string(split[1])))
			case "author":
				c.Author.Decode(split[1])
			case "committer":
				c.Committer.Decode(split[1])
			}
		} else {
			c.Message += string(line) + "\n"
		}

		if err == io.EOF {
			return nil
		}
	}
}
Exemplo n.º 11
0
// returns the commits from a slice of hashes
func (s *ReferencesSuite) commits(cc *C, repo string, hs ...string) []*Commit {
	r, ok := s.repos[repo]
	cc.Assert(ok, Equals, true)
	result := make([]*Commit, 0, len(hs))
	for _, h := range hs {
		c, err := r.Commit(core.NewHash(h))
		cc.Assert(err, IsNil)
		result = append(result, c)
	}
	return result
}
Exemplo n.º 12
0
func (s *SuiteFile) TestContents(c *C) {
	for i, t := range contentsTests {
		commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

		file, err := commit.File(t.path)
		c.Assert(err, IsNil)
		c.Assert(file.Contents(), Equals, t.contents, Commentf(
			"subtest %d: commit=%s, path=%s", i, t.commit, t.path))
	}
}
Exemplo n.º 13
0
// It is difficult to assert that we are ignoring an (empty) dir as even
// if we don't, no files will be found in it.
//
// At least this test has a high chance of panicking if
// we don't ignore empty dirs.
func (s *SuiteFile) TestIgnoreEmptyDirEntries(c *C) {
	for i, t := range ignoreEmptyDirEntriesTests {
		commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

		for file := range commit.Tree().Files() {
			_ = file.Contents()
			// this would probably panic if we are not ignoring empty dirs
		}
	}
}
Exemplo n.º 14
0
func (s *MockGitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
	hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")

	cap := common.NewCapabilities()
	cap.Decode("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEADmulti_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed no-done symref=HEAD:refs/heads/master agent=git/2:2.4.8~dbussink-fix-enterprise-tokens-compilation-1167-gc7006cf")

	return &common.GitUploadPackInfo{
		Capabilities: cap,
		Head:         hash,
		Refs:         map[string]core.Hash{"refs/heads/master": hash},
	}, nil
}
Exemplo n.º 15
0
func (s *SuiteCommit) TestFile(c *C) {
	for i, t := range fileTests {
		commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

		file, err := commit.File(t.path)
		found := err == nil
		c.Assert(found, Equals, t.found, Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit))
		if found {
			c.Assert(file.Hash.String(), Equals, t.blobHash, Commentf("subtest %d, commit=%s, path=%s", i, t.commit, t.path))
		}
	}
}
Exemplo n.º 16
0
func (s *SuiteRemote) TestFetch(c *C) {
	r := NewGitUploadPackService()
	c.Assert(r.Connect(RepositoryFixture), IsNil)

	req := &common.GitUploadPackRequest{}
	req.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))

	reader, err := r.Fetch(req)
	c.Assert(err, IsNil)

	b, err := ioutil.ReadAll(reader)
	c.Assert(err, IsNil)
	c.Assert(b, HasLen, 85374)
}
Exemplo n.º 17
0
func (s *SuiteRepository) TestCommit(c *C) {
	r, err := NewRepository(RepositoryFixture, nil)
	r.Remotes["origin"].upSrv = &MockGitUploadPackService{}

	c.Assert(err, IsNil)
	c.Assert(r.Pull("origin", "refs/heads/master"), IsNil)

	hash := core.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47")
	commit, err := r.Commit(hash)
	c.Assert(err, IsNil)

	c.Assert(commit.Hash.IsZero(), Equals, false)
	c.Assert(commit.Tree().Hash.IsZero(), Equals, false)
	c.Assert(commit.Author.Email, Equals, "*****@*****.**")
}
Exemplo n.º 18
0
// run a blame on all the suite's tests
func (s *BlameCommon) TestBlame(c *C) {
	for _, t := range blameTests {
		expected := s.mockBlame(t, c)

		repo, ok := s.repos[t.repo]
		c.Assert(ok, Equals, true)

		commit, err := repo.Commit(core.NewHash(t.rev))
		c.Assert(err, IsNil)

		obtained, err := commit.Blame(t.path)
		c.Assert(err, IsNil)
		c.Assert(obtained, DeepEquals, expected)
	}
}
Exemplo n.º 19
0
func (s *SuiteCommon) TestGitUploadPackEncode(c *C) {
	info := NewGitUploadPackInfo()
	info.Capabilities.Add("symref", "HEAD:refs/heads/master")

	info.Head = core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
	info.Refs = map[string]core.Hash{
		"refs/heads/master": info.Head,
	}

	c.Assert(info.String(), Equals,
		"001e# service=git-upload-pack\n"+
			"000000506ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:refs/heads/master\n"+
			"003f6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master\n"+
			"0000",
	)
}
Exemplo n.º 20
0
func (s *SuiteRemote) TestFetch(c *C) {
	agent, err := newSSHAgentConn()
	c.Assert(err, IsNil)
	defer func() { c.Assert(agent.close(), IsNil) }()

	r := NewGitUploadPackService()
	c.Assert(r.ConnectWithAuth(fixRepo, agent.auth), IsNil)
	defer func() { c.Assert(r.Disconnect(), IsNil) }()

	pr := &common.GitUploadPackRequest{}
	pr.Want(core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
	reader, err := r.Fetch(pr)
	c.Assert(err, IsNil)

	b, err := ioutil.ReadAll(reader)
	c.Assert(err, IsNil)
	c.Assert(b, HasLen, 85374)
}
Exemplo n.º 21
0
func (s *ObjectsSuite) TestParseTree(c *C) {
	hash := core.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")
	tree, err := s.r.Tree(hash)
	c.Assert(err, IsNil)

	c.Assert(tree.Entries, HasLen, 8)
	c.Assert(tree.Entries[".gitignore"].Name, Equals, ".gitignore")
	c.Assert(tree.Entries[".gitignore"].Mode.String(), Equals, "-rw-r--r--")
	c.Assert(tree.Entries[".gitignore"].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")

	count := 0
	ch := tree.Files()
	for f := range ch {
		count++
		if f.Name == "go/example.go" {
			content, _ := ioutil.ReadAll(f)
			c.Assert(content, HasLen, 2780)
		}
	}

	c.Assert(count, Equals, 9)
}
Exemplo n.º 22
0
func (s *ObjectsSuite) TestNewCommit(c *C) {
	hash := core.NewHash("a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69")
	commit, err := s.r.Commit(hash)
	c.Assert(err, IsNil)

	c.Assert(commit.Hash.String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69")
	c.Assert(commit.Tree().Hash.String(), Equals, "c2d30fa8ef288618f65f6eed6e168e0d514886f4")

	parents := commit.Parents()
	parentCommit, err := parents.Next()
	c.Assert(err, IsNil)
	c.Assert(parentCommit.Hash.String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d")

	parentCommit, err = parents.Next()
	c.Assert(err, IsNil)
	c.Assert(parentCommit.Hash.String(), Equals, "b8e471f58bcbca63b07bda20e428190409c2db47")

	c.Assert(commit.Author.Email, Equals, "*****@*****.**")
	c.Assert(commit.Author.Name, Equals, "Máximo Cuadros")
	c.Assert(commit.Author.When.Format(time.RFC3339), Equals, "2015-03-31T13:47:14+02:00")
	c.Assert(commit.Committer.Email, Equals, "*****@*****.**")
	c.Assert(commit.Message, Equals, "Merge pull request #1 from dripolles/feature\n\nCreating changelog\n")
}
Exemplo n.º 23
0
func (s *SuiteTree) TestFiles(c *C) {
	for i, t := range []struct {
		repo   string   // the repo name as in localRepos
		commit string   // the commit to search for the file
		files  []string // the expected files in the commit
	}{
		{"https://github.com/alcortesm/binary-relations.git", "b373f85fa2594d7dcd9989f4a5858a81647fb8ea", []string{
			"binary-relations.tex",
			".gitignore",
			"imgs-gen/simple-graph/fig.fig",
			"imgs-gen/simple-graph/Makefile",
			"Makefile",
			"src/map-slice/map-slice.go",
			"src/simple-arrays/simple-arrays.go",
		}},
		{"https://github.com/Tribler/dispersy.git", "f5a1fca709f760bf75a7adaa480bf0f0e1a547ee", []string{
			"authentication.py",
			"bloomfilter.py",
			"bootstrap.py",
			"cache.py",
			"callback.py",
			"candidate.py",
			"community.py",
			"conversion.py",
			"crypto.py",
			"database.py",
			"debugcommunity.py",
			"debug.py",
			"decorator.py",
			"destination.py",
			"dispersydatabase.py",
			"dispersy.py",
			"distribution.py",
			"dprint.py",
			"encoding.py",
			"endpoint.py",
			"__init__.py",
			"member.py",
			"message.py",
			"meta.py",
			"payload.py",
			"requestcache.py",
			"resolution.py",
			"script.py",
			"singleton.py",
			"timeline.py",
			"tool/callbackscript.py",
			"tool/__init__.py",
			"tool/scenarioscript.py",
		}},
		{"https://github.com/Tribler/dispersy.git", "9d38ff85ca03adcf68dc14f5b68b8994f15229f4", []string(nil)},
	} {
		commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

		tree := commit.Tree()
		var output []string
		for file := range tree.Files() {
			output = append(output, file.Name)
		}
		sort.Strings(output)
		sort.Strings(t.files)
		c.Assert(output, DeepEquals, t.files, Commentf("subtest %d, repo=%s, commit=%s", i, t.repo, t.commit))
	}
}
Exemplo n.º 24
0
func (r *GitUploadPackInfo) decodeHeaderLine(line string) {
	parts := strings.SplitN(line, " HEAD", 2)
	r.Head = core.NewHash(parts[0])
	r.Capabilities.Decode(line)
}
Exemplo n.º 25
0
func (s *SuiteTree) TestFile(c *C) {
	for i, t := range []struct {
		repo     string // the repo name as in localRepos
		commit   string // the commit to search for the file
		path     string // the path of the file to find
		blobHash string // expected hash of the returned file
		found    bool   // expected found value
	}{
		// use git ls-tree commit to get the hash of the blobs
		{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found",
			"", false},
		{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore",
			"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
		{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE",
			"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},

		{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found",
			"", false},
		{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore",
			"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
		{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg",
			"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
		{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE",
			"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},

		{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg",
			"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
		{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg",
			"", false},

		{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
		{"https://github.com/tyba/git-fixture.git", "1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
		{"https://github.com/tyba/git-fixture.git", "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
		{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},
		{"https://github.com/tyba/git-fixture.git", "b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
		{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG",
			"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},

		// git submodule
		{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final",
			"", false},
		{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final/not-found",
			"", false},

		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "LICENSE",
			"49c45e6cc893d6f5ebd5c9343fe4492360f339bf", true},
		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples",
			"", false},
		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/desk.sh",
			"d9c7751138824cd2d539c23d5afe3f9d29836854", true},
		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/not-found",
			"", false},
		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/bashrc",
			"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", true},
		{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/not-found",
			"", false},

		{"https://github.com/spinnaker/spinnaker.git", "b32b2aecae2cfca4840dd480f8082da206a538da", "etc/apache2/sites-available/spinnaker.conf",
			"1d452c616be4fb16d2cc6b8a7e7a2208a6e64d2d", true},

		{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "Makefile",
			"2dd2ad8c14de6612ed15813679a6554bad99330b", true},
		{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/binrels",
			"", false},
		{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice",
			"", false},
		{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go",
			"12431e98381dd5097e1a19fe53429c72ef1f328e", true},
		{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go/not-found",
			"", false},
	} {
		commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
		c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

		tree := commit.Tree()
		file, err := tree.File(t.path)
		found := err == nil
		c.Assert(found, Equals, t.found, Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit))
		if found {
			c.Assert(file.Hash.String(), Equals, t.blobHash, Commentf("subtest %d, commit=%s, path=%s", i, t.commit, t.path))
		}
	}
}