repository

package
v0.0.0-...-413351d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2020 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Overview

Package repository contains helper methods for working with the Git repo.

Package repository contains helper methods for working with a Git repo.

Index

Constants

View Source
const (
	TestTargetRef          = "refs/heads/master"
	TestReviewRef          = "refs/heads/ojarjur/mychange"
	TestAlternateReviewRef = "refs/review/mychange"
	TestRequestsRef        = "refs/notes/devtools/reviews"
	TestCommentsRef        = "refs/notes/devtools/discuss"

	TestCommitA = "A"
	TestCommitB = "B"
	TestCommitC = "C"
	TestCommitD = "D"
	TestCommitE = "E"
	TestCommitF = "F"
	TestCommitG = "G"
	TestCommitH = "H"
	TestCommitI = "I"
	TestCommitJ = "J"

	TestRequestB = `` /* 175-byte string literal not displayed */
	TestRequestD = `` /* 175-byte string literal not displayed */
	TestRequestG = `` /* 573-byte string literal not displayed */

	TestDiscussB = `{"timestamp": "0000000001", "author": "ojarjur", "location": {"commit": "B"}, "resolved": true}`
	TestDiscussD = `{"timestamp": "0000000003", "author": "ojarjur", "location": {"commit": "E"}, "resolved": true}`
)

Constants used for testing. We initialize our mock repo with two branches (one of which holds a pending review), and commit history that looks like this:

Master Branch:    A--B--D--E--F--J
                   \   /    \  \
                     C       \  \
                              \  \
Review Branch:                 G--H--I

Where commits "B" and "D" represent reviews that have been submitted, and "G" is a pending review.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommitDetails

type CommitDetails struct {
	Author      string   `json:"author,omitempty"`
	AuthorEmail string   `json:"authorEmail,omitempty"`
	Tree        string   `json:"tree,omitempty"`
	Time        string   `json:"time,omitempty"`
	Parents     []string `json:"parents,omitempty"`
	Summary     string   `json:"summary,omitempty"`
}

CommitDetails represents the contents of a commit.

type GitRepo

type GitRepo struct {
	Path string
}

GitRepo represents an instance of a (local) git repository.

func NewGitRepo

func NewGitRepo(path string) (*GitRepo, error)

NewGitRepo determines if the given working directory is inside of a git repository, and returns the corresponding GitRepo instance if it is.

func (*GitRepo) AppendNote

func (repo *GitRepo) AppendNote(notesRef, revision string, note Note) error

AppendNote appends a note to a revision under the given ref.

func (*GitRepo) ArchiveRef

func (repo *GitRepo) ArchiveRef(ref, archive string) error

ArchiveRef adds the current commit pointed to by the 'ref' argument under the ref specified in the 'archive' argument.

Both the 'ref' and 'archive' arguments are expected to be the fully qualified names of git refs (e.g. 'refs/heads/my-change' or 'refs/devtools/archives/reviews').

If the ref pointed to by the 'archive' argument does not exist yet, then it will be created.

func (*GitRepo) Diff

func (repo *GitRepo) Diff(left, right string, diffArgs ...string) (string, error)

Diff computes the diff between two given commits.

func (*GitRepo) FetchAndReturnNewReviewHashes

func (repo *GitRepo) FetchAndReturnNewReviewHashes(remote, notesRefPattern,
	archiveRefPattern string) ([]string, error)

FetchAndReturnNewReviewHashes fetches the notes "branches" and then susses out the IDs (the revision the review points to) of any new reviews, then returns that list of IDs.

This is accomplished by determining which files in the notes tree have changed because the _names_ of these files correspond to the revisions they point to.

func (*GitRepo) GetAllNotes

func (repo *GitRepo) GetAllNotes(notesRef string) (map[string][]Note, error)

GetAllNotes reads the contents of the notes under the given ref for every commit.

The returned value is a mapping from commit hash to the list of notes for that commit.

This is the batch version of the corresponding GetNotes(...) method.

func (GitRepo) GetCommitDetails

func (repo GitRepo) GetCommitDetails(ref string) (*CommitDetails, error)

GetCommitDetails returns the details of a commit's metadata.

func (*GitRepo) GetCommitHash

func (repo *GitRepo) GetCommitHash(ref string) (string, error)

GetCommitHash returns the hash of the commit pointed to by the given ref.

func (*GitRepo) GetCommitMessage

func (repo *GitRepo) GetCommitMessage(ref string) (string, error)

GetCommitMessage returns the message stored in the commit pointed to by the given ref.

func (*GitRepo) GetCommitTime

func (repo *GitRepo) GetCommitTime(ref string) (string, error)

GetCommitTime returns the commit time of the commit pointed to by the given ref.

func (*GitRepo) GetCoreEditor

func (repo *GitRepo) GetCoreEditor() (string, error)

GetCoreEditor returns the name of the editor that the user has used to configure git.

func (*GitRepo) GetHeadRef

func (repo *GitRepo) GetHeadRef() (string, error)

GetHeadRef returns the ref that is the current HEAD.

func (*GitRepo) GetLastParent

func (repo *GitRepo) GetLastParent(ref string) (string, error)

GetLastParent returns the last parent of the given commit (as ordered by git).

func (*GitRepo) GetNotes

func (repo *GitRepo) GetNotes(notesRef, revision string) []Note

GetNotes uses the "git" command-line tool to read the notes from the given ref for a given revision.

func (*GitRepo) GetPath

func (repo *GitRepo) GetPath() string

GetPath returns the path to the repo.

func (*GitRepo) GetRepoStateHash

func (repo *GitRepo) GetRepoStateHash() (string, error)

GetRepoStateHash returns a hash which embodies the entire current state of a repository.

func (*GitRepo) GetSubmitStrategy

func (repo *GitRepo) GetSubmitStrategy() (string, error)

GetSubmitStrategy returns the way in which a review is submitted

func (*GitRepo) GetUserEmail

func (repo *GitRepo) GetUserEmail() (string, error)

GetUserEmail returns the email address that the user has used to configure git.

func (*GitRepo) GetUserSigningKey

func (repo *GitRepo) GetUserSigningKey() (string, error)

GetUserSigningKey returns the key id the user has configured for sigining git artifacts.

func (*GitRepo) HasUncommittedChanges

func (repo *GitRepo) HasUncommittedChanges() (bool, error)

HasUncommittedChanges returns true if there are local, uncommitted changes.

func (*GitRepo) IsAncestor

func (repo *GitRepo) IsAncestor(ancestor, descendant string) (bool, error)

IsAncestor determines if the first argument points to a commit that is an ancestor of the second.

func (*GitRepo) ListCommits

func (repo *GitRepo) ListCommits(ref string) []string

ListCommits returns the list of commits reachable from the given ref.

The generated list is in chronological order (with the oldest commit first).

If the specified ref does not exist, then this method returns an empty result.

func (*GitRepo) ListCommitsBetween

func (repo *GitRepo) ListCommitsBetween(from, to string) ([]string, error)

ListCommitsBetween returns the list of commits between the two given revisions.

The "from" parameter is the starting point (exclusive), and the "to" parameter is the ending point (inclusive).

The "from" commit does not need to be an ancestor of the "to" commit. If it is not, then the merge base of the two is used as the starting point. Admittedly, this makes calling these the "between" commits is a bit of a misnomer, but it also makes the method easier to use when you want to generate the list of changes in a feature branch, as it eliminates the need to explicitly calculate the merge base. This also makes the semantics of the method compatible with git's built-in "rev-list" command.

The generated list is in chronological order (with the oldest commit first).

func (*GitRepo) ListNotedRevisions

func (repo *GitRepo) ListNotedRevisions(notesRef string) []string

ListNotedRevisions returns the collection of revisions that are annotated by notes in the given ref.

func (*GitRepo) MergeAndSignRef

func (repo *GitRepo) MergeAndSignRef(ref string, fastForward bool,
	messages ...string) error

MergeAndSignRef merges the given ref into the current one and signs the merge.

The ref argument is the ref to merge, and fastForward indicates that the current ref should only move forward, as opposed to creating a bubble merge. The messages argument(s) provide text that should be included in the default merge commit message (separated by blank lines).

func (*GitRepo) MergeArchives

func (repo *GitRepo) MergeArchives(remote, archiveRefPattern string) error

MergeArchives merges in the remote's state of the archives reference into the local repository's.

func (*GitRepo) MergeBase

func (repo *GitRepo) MergeBase(a, b string) (string, error)

MergeBase determines if the first commit that is an ancestor of the two arguments.

func (*GitRepo) MergeNotes

func (repo *GitRepo) MergeNotes(remote, notesRefPattern string) error

MergeNotes merges in the remote's state of the notes reference into the local repository's.

func (*GitRepo) MergeRef

func (repo *GitRepo) MergeRef(ref string, fastForward bool, messages ...string) error

MergeRef merges the given ref into the current one.

The ref argument is the ref to merge, and fastForward indicates that the current ref should only move forward, as opposed to creating a bubble merge. The messages argument(s) provide text that should be included in the default merge commit message (separated by blank lines).

func (*GitRepo) PullNotes

func (repo *GitRepo) PullNotes(remote, notesRefPattern string) error

PullNotes fetches the contents of the given notes ref from a remote repo, and then merges them with the corresponding local notes using the "cat_sort_uniq" strategy.

func (*GitRepo) PullNotesAndArchive

func (repo *GitRepo) PullNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error

PullNotesAndArchive fetches the contents of the notes and archives refs from a remote repo, and merges them with the corresponding local refs.

For notes refs, we assume that every note can be automatically merged using the 'cat_sort_uniq' strategy (the git-review schemas fit that requirement), so we automatically merge the remote notes into the local notes.

For "archive" refs, they are expected to be used solely for maintaining reachability of commits that are part of the history of any reviews, so we do not maintain any consistency with their tree objects. Instead, we merely ensure that their history graph includes every commit that we intend to keep.

func (*GitRepo) PushNotes

func (repo *GitRepo) PushNotes(remote, notesRefPattern string) error

PushNotes pushes git notes to a remote repo.

func (*GitRepo) PushNotesAndArchive

func (repo *GitRepo) PushNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error

PushNotesAndArchive pushes the given notes and archive refs to a remote repo.

func (*GitRepo) RebaseAndSignRef

func (repo *GitRepo) RebaseAndSignRef(ref string) error

RebaseAndSignRef rebases the current ref onto the given one and signs the result.

func (*GitRepo) RebaseRef

func (repo *GitRepo) RebaseRef(ref string) error

RebaseRef rebases the current ref onto the given one.

func (*GitRepo) ResolveRefCommit

func (repo *GitRepo) ResolveRefCommit(ref string) (string, error)

ResolveRefCommit returns the commit pointed to by the given ref, which may be a remote ref.

This differs from GetCommitHash which only works on exact matches, in that it will try to intelligently handle the scenario of a ref not existing locally, but being known to exist in a remote repo.

This method should be used when a command may be performed by either the reviewer or the reviewee, while GetCommitHash should be used when the encompassing command should only be performed by the reviewee.

func (*GitRepo) Show

func (repo *GitRepo) Show(commit, path string) (string, error)

Show returns the contents of the given file at the given commit.

func (*GitRepo) SwitchToRef

func (repo *GitRepo) SwitchToRef(ref string) error

SwitchToRef changes the currently-checked-out ref.

func (*GitRepo) VerifyCommit

func (repo *GitRepo) VerifyCommit(hash string) error

VerifyCommit verifies that the supplied hash points to a known commit.

func (*GitRepo) VerifyGitRef

func (repo *GitRepo) VerifyGitRef(ref string) error

VerifyGitRef verifies that the supplied ref points to a known commit.

type Note

type Note []byte

Note represents the contents of a git-note

type Repo

type Repo interface {
	// GetPath returns the path to the repo.
	GetPath() string

	// GetRepoStateHash returns a hash which embodies the entire current state of a repository.
	GetRepoStateHash() (string, error)

	// GetUserEmail returns the email address that the user has used to configure git.
	GetUserEmail() (string, error)

	// GetUserSigningKey returns the key id the user has configured for
	// sigining git artifacts.
	GetUserSigningKey() (string, error)

	// GetCoreEditor returns the name of the editor that the user has used to configure git.
	GetCoreEditor() (string, error)

	// GetSubmitStrategy returns the way in which a review is submitted
	GetSubmitStrategy() (string, error)

	// HasUncommittedChanges returns true if there are local, uncommitted changes.
	HasUncommittedChanges() (bool, error)

	// VerifyCommit verifies that the supplied hash points to a known commit.
	VerifyCommit(hash string) error

	// VerifyGitRef verifies that the supplied ref points to a known commit.
	VerifyGitRef(ref string) error

	// GetHeadRef returns the ref that is the current HEAD.
	GetHeadRef() (string, error)

	// GetCommitHash returns the hash of the commit pointed to by the given ref.
	GetCommitHash(ref string) (string, error)

	// ResolveRefCommit returns the commit pointed to by the given ref, which may be a remote ref.
	//
	// This differs from GetCommitHash which only works on exact matches, in that it will try to
	// intelligently handle the scenario of a ref not existing locally, but being known to exist
	// in a remote repo.
	//
	// This method should be used when a command may be performed by either the reviewer or the
	// reviewee, while GetCommitHash should be used when the encompassing command should only be
	// performed by the reviewee.
	ResolveRefCommit(ref string) (string, error)

	// GetCommitMessage returns the message stored in the commit pointed to by the given ref.
	GetCommitMessage(ref string) (string, error)

	// GetCommitTime returns the commit time of the commit pointed to by the given ref.
	GetCommitTime(ref string) (string, error)

	// GetLastParent returns the last parent of the given commit (as ordered by git).
	GetLastParent(ref string) (string, error)

	// GetCommitDetails returns the details of a commit's metadata.
	GetCommitDetails(ref string) (*CommitDetails, error)

	// MergeBase determines if the first commit that is an ancestor of the two arguments.
	MergeBase(a, b string) (string, error)

	// IsAncestor determines if the first argument points to a commit that is an ancestor of the second.
	IsAncestor(ancestor, descendant string) (bool, error)

	// Diff computes the diff between two given commits.
	Diff(left, right string, diffArgs ...string) (string, error)

	// Show returns the contents of the given file at the given commit.
	Show(commit, path string) (string, error)

	// SwitchToRef changes the currently-checked-out ref.
	SwitchToRef(ref string) error

	// ArchiveRef adds the current commit pointed to by the 'ref' argument
	// under the ref specified in the 'archive' argument.
	//
	// Both the 'ref' and 'archive' arguments are expected to be the fully
	// qualified names of git refs (e.g. 'refs/heads/my-change' or
	// 'refs/archive/devtools').
	//
	// If the ref pointed to by the 'archive' argument does not exist
	// yet, then it will be created.
	ArchiveRef(ref, archive string) error

	// MergeRef merges the given ref into the current one.
	//
	// The ref argument is the ref to merge, and fastForward indicates that the
	// current ref should only move forward, as opposed to creating a bubble merge.
	// The messages argument(s) provide text that should be included in the default
	// merge commit message (separated by blank lines).
	MergeRef(ref string, fastForward bool, messages ...string) error

	// MergeAndSignRef merges the given ref into the current one and signs the
	// merge.
	//
	// The ref argument is the ref to merge, and fastForward indicates that the
	// current ref should only move forward, as opposed to creating a bubble merge.
	// The messages argument(s) provide text that should be included in the default
	// merge commit message (separated by blank lines).
	MergeAndSignRef(ref string, fastForward bool, messages ...string) error

	// RebaseRef rebases the current ref onto the given one.
	RebaseRef(ref string) error

	// RebaseAndSignRef rebases the current ref onto the given one and signs
	// the result.
	RebaseAndSignRef(ref string) error

	// ListCommits returns the list of commits reachable from the given ref.
	//
	// The generated list is in chronological order (with the oldest commit first).
	//
	// If the specified ref does not exist, then this method returns an empty result.
	ListCommits(ref string) []string

	// ListCommitsBetween returns the list of commits between the two given revisions.
	//
	// The "from" parameter is the starting point (exclusive), and the "to"
	// parameter is the ending point (inclusive).
	//
	// The "from" commit does not need to be an ancestor of the "to" commit. If it
	// is not, then the merge base of the two is used as the starting point.
	// Admittedly, this makes calling these the "between" commits is a bit of a
	// misnomer, but it also makes the method easier to use when you want to
	// generate the list of changes in a feature branch, as it eliminates the need
	// to explicitly calculate the merge base. This also makes the semantics of the
	// method compatible with git's built-in "rev-list" command.
	//
	// The generated list is in chronological order (with the oldest commit first).
	ListCommitsBetween(from, to string) ([]string, error)

	// GetNotes reads the notes from the given ref that annotate the given revision.
	GetNotes(notesRef, revision string) []Note

	// GetAllNotes reads the contents of the notes under the given ref for every commit.
	//
	// The returned value is a mapping from commit hash to the list of notes for that commit.
	//
	// This is the batch version of the corresponding GetNotes(...) method.
	GetAllNotes(notesRef string) (map[string][]Note, error)

	// AppendNote appends a note to a revision under the given ref.
	AppendNote(ref, revision string, note Note) error

	// ListNotedRevisions returns the collection of revisions that are annotated by notes in the given ref.
	ListNotedRevisions(notesRef string) []string

	// PushNotes pushes git notes to a remote repo.
	PushNotes(remote, notesRefPattern string) error

	// PullNotes fetches the contents of the given notes ref from a remote repo,
	// and then merges them with the corresponding local notes using the
	// "cat_sort_uniq" strategy.
	PullNotes(remote, notesRefPattern string) error

	// PushNotesAndArchive pushes the given notes and archive refs to a remote repo.
	PushNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error

	// PullNotesAndArchive fetches the contents of the notes and archives refs from
	// a remote repo, and merges them with the corresponding local refs.
	//
	// For notes refs, we assume that every note can be automatically merged using
	// the 'cat_sort_uniq' strategy (the git-review schemas fit that requirement),
	// so we automatically merge the remote notes into the local notes.
	//
	// For "archive" refs, they are expected to be used solely for maintaining
	// reachability of commits that are part of the history of any reviews,
	// so we do not maintain any consistency with their tree objects. Instead,
	// we merely ensure that their history graph includes every commit that we
	// intend to keep.
	PullNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error

	// MergeNotes merges in the remote's state of the archives reference into
	// the local repository's.
	MergeNotes(remote, notesRefPattern string) error
	// MergeArchives merges in the remote's state of the archives reference
	// into the local repository's.
	MergeArchives(remote, archiveRefPattern string) error

	// FetchAndReturnNewReviewHashes fetches the notes "branches" and then
	// susses out the IDs (the revision the review points to) of any new
	// reviews, then returns that list of IDs.
	//
	// This is accomplished by determining which files in the notes tree have
	// changed because the _names_ of these files correspond to the revisions
	// they point to.
	FetchAndReturnNewReviewHashes(remote, notesRefPattern, archiveRefPattern string) ([]string, error)
}

Repo represents a source code repository.

func NewMockRepoForTest

func NewMockRepoForTest() Repo

NewMockRepoForTest returns a mocked-out instance of the Repo interface that has been pre-populated with test data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL