Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
adds constructors, ability to delete, ability to verify GPG signature
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Mar 16, 2019
1 parent bce5c7e commit 19a7b8e
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 55 deletions.
19 changes: 9 additions & 10 deletions cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ func main() {
gitConfig := git.Config{
Paths: *gitPath,
Branch: *gitBranch,
SyncTag: *gitSyncTag,
NotesRef: *gitNotesRef,
UserName: *gitUser,
UserEmail: *gitEmail,
Expand Down Expand Up @@ -505,17 +504,17 @@ func main() {
switch *fluxStateMode {
case fluxsync.NativeStateMode:
// reminder: this code assumes that if we are in readonly mode the *fluxStateMode has already been forced to NativeStateMode
syncProvider = nativestate.NativeSyncProvider{}
syncProvider = nativestate.NewNativeSyncProvider()

case fluxsync.GitTagStateMode:
syncProvider = git.GitTagSyncProvider{
WorkingDir: repo.Dir(),
SyncTag: *gitSyncTag,
UpstreamURL: *gitURL,
SigningKey: *gitSigningKey,
UserName: *gitUser,
UserEmail: *gitEmail,
}
syncProvider = git.NewGitTagSyncProvider(
repo.Dir(),
*gitSyncTag,
*gitURL,
*gitSigningKey,
*gitUser,
*gitEmail,
)
}

daemon := &daemon.Daemon{
Expand Down
1 change: 0 additions & 1 deletion daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *cluster.Mock, *mockEven
Branch: "master",
UserName: "example",
UserEmail: "example@example.com",
SyncTag: "flux-test",
NotesRef: "fluxtest",
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncMarkerRev *string, warne
Revision: headRev,
Message: "Sync pointer",
}
err := d.SyncProvider.SetRevision(ctx, syncMarkerAction)
err := d.SyncProvider.UpdateMarker(ctx, syncMarkerAction)
cancel()
if err != nil {
return err
Expand Down
13 changes: 6 additions & 7 deletions daemon/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func daemon(t *testing.T) (*Daemon, func()) {

gitConfig := git.Config{
Branch: "master",
SyncTag: gitSyncTag,
NotesRef: gitNotesRef,
UserName: gitUser,
UserEmail: gitEmail,
Expand Down Expand Up @@ -133,7 +132,7 @@ func TestPullAndSync_InitialSync(t *testing.T) {
// It creates the tag at HEAD
if err := d.Repo.Refresh(context.Background()); err != nil {
t.Errorf("pulling sync tag: %v", err)
} else if revs, err := d.Repo.CommitsBefore(context.Background(), gitSyncTag); err != nil {
} else if revs, err := d.Repo.CommitsBefore(context.Background(), gitSyncTag); err != nil { // READONLY-NOTE: this needs to be fixed - direct access to Git Tag is no longer permitted, must go through SyncProvider
t.Errorf("finding revisions before sync tag: %v", err)
} else if len(revs) <= 0 {
t.Errorf("Found no revisions before the sync tag")
Expand All @@ -152,7 +151,7 @@ func TestDoSync_NoNewCommits(t *testing.T) {
Revision: "HEAD",
Message: "Sync pointer",
}
return d.SyncProvider.SetRevision(ctx, syncMarkerAction)
return d.SyncProvider.UpdateMarker(ctx, syncMarkerAction)
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -201,12 +200,12 @@ func TestDoSync_NoNewCommits(t *testing.T) {
}

// It doesn't move the tag
oldRevs, err := d.Repo.CommitsBefore(ctx, gitSyncTag)
oldRevs, err := d.Repo.CommitsBefore(ctx, gitSyncTag) // READONLY-NOTE: this needs to be fixed - direct access to Git Tag is no longer permitted, must go through SyncProvider
if err != nil {
t.Fatal(err)
}

if revs, err := d.Repo.CommitsBefore(ctx, gitSyncTag); err != nil {
if revs, err := d.Repo.CommitsBefore(ctx, gitSyncTag); err != nil { // READONLY-NOTE: this needs to be fixed - direct access to Git Tag is no longer permitted, must go through SyncProvider
t.Errorf("finding revisions before sync tag: %v", err)
} else if !reflect.DeepEqual(revs, oldRevs) {
t.Errorf("Should have kept the sync tag at HEAD")
Expand All @@ -229,7 +228,7 @@ func TestDoSync_WithNewCommit(t *testing.T) {
Revision: "HEAD",
Message: "Sync pointer",
}
err = d.SyncProvider.SetRevision(ctx, syncMarkerAction)
err = d.SyncProvider.UpdateMarker(ctx, syncMarkerAction)
if err != nil {
return err
}
Expand Down Expand Up @@ -311,7 +310,7 @@ func TestDoSync_WithNewCommit(t *testing.T) {
defer cancel()
if err := d.Repo.Refresh(ctx); err != nil {
t.Errorf("pulling sync tag: %v", err)
} else if revs, err := d.Repo.CommitsBetween(ctx, oldRevision, gitSyncTag); err != nil {
} else if revs, err := d.Repo.CommitsBetween(ctx, oldRevision, gitSyncTag); err != nil { // READONLY-NOTE: this needs to be fixed - direct access to Git Tag is no longer permitted, must go through SyncProvider
t.Errorf("finding revisions before sync tag: %v", err)
} else if len(revs) <= 0 {
t.Errorf("Should have moved sync tag forward")
Expand Down
5 changes: 2 additions & 3 deletions git/gittest/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CheckoutWithConfig(t *testing.T, config git.Config) (*git.Checkout, *git.Re
// Add files to the repo with the same name as the git branch and the sync tag.
// This is to make sure that git commands don't have ambiguity problems between revisions and files.
testfiles.Files[config.Branch] = "Filename doctored to create a conflict with the git branch name"
testfiles.Files[config.SyncTag] = "Filename doctored to create a conflict with the git sync tag"
// testfiles.Files[config.SyncTag] = "Filename doctored to create a conflict with the git sync tag" // READONLY-NOTE: need to figure out now that the config itself doesn't have access to the SyncTag
repo, cleanup := Repo(t)
if err := repo.Ready(context.Background()); err != nil {
cleanup()
Expand All @@ -94,15 +94,14 @@ func CheckoutWithConfig(t *testing.T, config git.Config) (*git.Checkout, *git.Re
co.Clean()
cleanup()
delete(testfiles.Files, config.Branch)
delete(testfiles.Files, config.SyncTag)
// delete(testfiles.Files, config.SyncTag) // READONLY-NOTE: need to figure out now that the config itself doesn't have access to the SyncTag
}
}

var TestConfig git.Config = git.Config{
Branch: "master",
UserName: "example",
UserEmail: "example@example.com",
SyncTag: "flux-test",
NotesRef: "fluxtest",
}

Expand Down
18 changes: 10 additions & 8 deletions git/gittest/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,25 @@ func TestSignedTag(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

syncProvider := git.GitTagSyncProvider{
WorkingDir: checkout.Dir(),
SyncTag: "flux-test",
SigningKey: signingKey,
}
syncProvider := git.NewGitTagSyncProvider(
checkout.Dir(),
"flux-test",
"",
signingKey,
"User Name",
"email@test.com",
)

syncMarkerAction := fluxsync.SyncMarkerAction{
Revision: "HEAD",
Message: "Sync pointer",
}

if err := syncProvider.SetRevision(ctx, syncMarkerAction); err != nil {
if err := syncProvider.UpdateMarker(ctx, syncMarkerAction); err != nil {
t.Fatal(err)
}

err := checkout.VerifySyncTag(ctx)
err := syncProvider.VerifySyncTag(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -177,7 +180,6 @@ func TestCheckout(t *testing.T) {
Branch: "master",
UserName: "example",
UserEmail: "example@example.com",
SyncTag: "flux-test",
NotesRef: "fluxtest",
}
checkout, err := repo.Clone(ctx, params)
Expand Down
8 changes: 7 additions & 1 deletion git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ func checkPush(ctx context.Context, workingDir, upstream string) error {
if err := execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}); err != nil {
return errors.Wrap(err, "attempt to push tag")
}
args = []string{"push", "--delete", upstream, "tag", checkPushTag}
return deleteTag(ctx, workingDir, upstream, checkPushTag)
}

// deleteTag deletes the given git tag
// See https://git-scm.com/docs/git-tag and https://git-scm.com/docs/git-push for more info.
func deleteTag(ctx context.Context, workingDir string, upstream string, tag string) error {
args := []string{"push", "--delete", upstream, "tag", tag}
return execGitCmd(ctx, args, gitCmdConfig{dir: workingDir})
}

Expand Down
57 changes: 43 additions & 14 deletions git/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,49 @@ import (
)

type GitTagSyncProvider struct {
WorkingDir string
SyncTag string
UpstreamURL string
SigningKey string
UserName string
UserEmail string
workingDir string
syncTag string
upstreamURL string
signingKey string
userName string
userEmail string
}

// NewGitTagSyncProvider creates a new git tag sync provider
func NewGitTagSyncProvider(
workingDir string,
syncTag string,
upstreamURL string,
signingKey string,
userName string,
userEmail string,
) GitTagSyncProvider {
return GitTagSyncProvider{
workingDir: workingDir,
syncTag: syncTag,
upstreamURL: upstreamURL,
signingKey: signingKey,
userName: userName,
userEmail: userEmail,
}
}

// GetRevision returns the revision of the git commit where the flux sync tag is currently positioned
func (p GitTagSyncProvider) GetRevision(ctx context.Context) (string, error) {
return refRevision(ctx, p.WorkingDir, p.SyncTag)
return refRevision(ctx, p.workingDir, p.syncTag)
}

// SetRevision updates the state that Flux relies on for keeping track of the so-called "High Water Mark" for which commits Flux has reconciled.
func (p GitTagSyncProvider) SetRevision(ctx context.Context, syncMarkerAction fluxsync.SyncMarkerAction) error {
workingDir := p.WorkingDir
tag := p.SyncTag
upstream := p.UpstreamURL
// UpdateMarker updates the state that Flux relies on for keeping track of the so-called "High Water Mark" for which commits Flux has reconciled.
func (p GitTagSyncProvider) UpdateMarker(ctx context.Context, syncMarkerAction fluxsync.SyncMarkerAction) error {
workingDir := p.workingDir
tag := p.syncTag
upstream := p.upstreamURL

if syncMarkerAction.SigningKey == "" {
syncMarkerAction.SigningKey = p.SigningKey
syncMarkerAction.SigningKey = p.signingKey
}

config(ctx, p.WorkingDir, p.UserName, p.UserEmail)
config(ctx, p.workingDir, p.userName, p.userEmail)

args := []string{"tag", "--force", "--annotate", "--message", syncMarkerAction.Message}
var env []string
Expand All @@ -50,3 +69,13 @@ func (p GitTagSyncProvider) SetRevision(ctx context.Context, syncMarkerAction fl

return nil
}

// DeleteMarker removes the Git Tag used for syncing
func (p GitTagSyncProvider) DeleteMarker(ctx context.Context) error {
return deleteTag(ctx, p.workingDir, p.upstreamURL, p.syncTag)
}

// VerifySyncTag validates the gpg signature created by git tag.
func (p GitTagSyncProvider) VerifySyncTag(ctx context.Context) error {
return verifyTag(ctx, p.workingDir, p.syncTag)
}
6 changes: 0 additions & 6 deletions git/working.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
type Config struct {
Branch string // branch we're syncing to
Paths []string // paths within the repo containing files we care about
SyncTag string
NotesRef string
UserName string
UserEmail string
Expand Down Expand Up @@ -121,11 +120,6 @@ func (c *Checkout) HeadRevision(ctx context.Context) (string, error) {
return refRevision(ctx, c.dir, "HEAD")
}

// VerifySyncTag validates the gpg signature created by git tag.
func (c *Checkout) VerifySyncTag(ctx context.Context) error {
return verifyTag(ctx, c.dir, c.config.SyncTag)
}

// ChangedFiles does a git diff listing changed files
func (c *Checkout) ChangedFiles(ctx context.Context, ref string) ([]string, error) {
list, err := changed(ctx, c.dir, ref, c.config.Paths)
Expand Down
16 changes: 13 additions & 3 deletions nativestate/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ type NativeSyncProvider struct {
revision string
}

func NewNativeSyncProvider() NativeSyncProvider {
return NativeSyncProvider{}
}

// GetRevision gets the revision of the current sync marker (representing the place flux has synced to)
func (n NativeSyncProvider) GetRevision(ctx context.Context) (string, error) {
func (p NativeSyncProvider) GetRevision(ctx context.Context) (string, error) {
return nativeShim.data.FluxSync.Revision, nil
}

// SetRevision updates the revision the sync marker points to
func (n NativeSyncProvider) SetRevision(ctx context.Context, syncMarkerAction fluxsync.SyncMarkerAction) error {
// UpdateMarker updates the revision the sync marker points to
func (p NativeSyncProvider) UpdateMarker(ctx context.Context, syncMarkerAction fluxsync.SyncMarkerAction) error {
nativeShim.data.FluxSync.Revision = syncMarkerAction.Revision
nativeShim.data.FluxSync.Message = syncMarkerAction.Message
return nil
}

// DeleteMarker resets the state of the object
func (p NativeSyncProvider) DeleteMarker(ctx context.Context) error {
nativeShim = NativeShim{}
return nil
}
3 changes: 2 additions & 1 deletion sync/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ type SyncMarkerAction struct {

type SyncProvider interface {
GetRevision(ctx context.Context) (string, error)
SetRevision(ctx context.Context, syncMarkerAction SyncMarkerAction) error
UpdateMarker(ctx context.Context, syncMarkerAction SyncMarkerAction) error
DeleteMarker(ctx context.Context) error
}

0 comments on commit 19a7b8e

Please sign in to comment.