Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/bundles/git-branch-freshness.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add an opt-in Git branch freshness check before bundle deployment.
16 changes: 16 additions & 0 deletions acceptance/bundle/deploy/git-freshness/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bundle:
name: git-freshness

experimental:
check_git_branch_freshness: true

sync:
exclude:
- remote.git/**
- writer/**
- tmp.plan.json

resources:
jobs:
job:
name: test-job
2 changes: 2 additions & 0 deletions acceptance/bundle/deploy/git-freshness/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
24 changes: 24 additions & 0 deletions acceptance/bundle/deploy/git-freshness/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

>>> [CLI] bundle plan -o json

=== A stale branch blocks non-interactive deployments

>>> [CLI] bundle deploy
Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main"

Error: Git branch "main" is 1 commit(s) behind its upstream "origin/main"; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions)

>>> [CLI] bundle deploy --plan tmp.plan.json
Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main"

Error: Git branch "main" is 1 commit(s) behind its upstream "origin/main"; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions)

=== Auto-approve allows a stale branch

>>> [CLI] bundle deploy --plan tmp.plan.json --auto-approve
Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main"

Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/git-freshness/default/files...
Created jobs.job
Files: 2 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
19 changes: 19 additions & 0 deletions acceptance/bundle/deploy/git-freshness/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
git-repo-init
git clone --bare . remote.git &> LOG.git
git remote add origin remote.git
git fetch origin >> LOG.git 2>&1
git branch --set-upstream-to=origin/main main >> LOG.git 2>&1
git clone remote.git writer >> LOG.git 2>&1
git -C writer config user.name "Tester"
git -C writer config user.email "tester@databricks.com"

trace $CLI bundle plan -o json > tmp.plan.json
git -C writer commit --allow-empty -m "Update upstream" >> LOG.git 2>&1
git -C writer push >> LOG.git 2>&1

title "A stale branch blocks non-interactive deployments"
musterr trace $CLI bundle deploy
musterr trace $CLI bundle deploy --plan tmp.plan.json

title "Auto-approve allows a stale branch"
trace $CLI bundle deploy --plan tmp.plan.json --auto-approve
1 change: 1 addition & 0 deletions acceptance/bundle/deploy/git-freshness/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ignore = [".git", "remote.git", "writer", "tmp.plan.json"]
3 changes: 3 additions & 0 deletions bundle/config/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package config
type Experimental struct {
Scripts map[ScriptHook]Command `json:"scripts,omitempty"`

// CheckGitBranchFreshness checks the current branch against its upstream before deployment.
CheckGitBranchFreshness *bool `json:"check_git_branch_freshness,omitempty"`

// ImmutableFolder specifies that bundle files and artifacts are uploaded as a
// single immutable snapshot rather than being synced individually. When true,
// the deployment calls /api/2.0/repos/snapshots with a zip of all files and sets
Expand Down
8 changes: 8 additions & 0 deletions bundle/internal/schema/annotations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ experimental:
"description": |-
Defines attributes for experimental features.
"$fields":
"check_git_branch_freshness":
"description": |-
Before deployment, fetch the current Git branch's upstream and ask for confirmation if the branch is behind. This also applies when deploying a saved plan. If the check cannot run, a warning is emitted and deployment continues. Use --auto-approve to proceed without prompting, including in CI; this also approves other deployment actions.
"markdown_examples": |-
```yaml
experimental:
check_git_branch_freshness: true
```
"deployment_history":
"description": |-
Whether to record deployment history using the deployment metadata service (DMS), which tracks what changed across deployments.
Expand Down
120 changes: 120 additions & 0 deletions bundle/phases/git_freshness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package phases

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"slices"
"strconv"
"strings"
"time"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/logdiag"
)

const gitFreshnessTimeout = 30 * time.Second

// CheckGitBranchFreshness asks for approval when the current branch is behind its upstream.
func CheckGitBranchFreshness(ctx context.Context, b *bundle.Bundle) error {
if b.Config.Experimental == nil || !config.IsExplicitlyEnabled(b.Config.Experimental.CheckGitBranchFreshness) {
return nil
}

checkCtx, cancel := context.WithTimeout(ctx, gitFreshnessTimeout)
defer cancel()

branch, upstream, behind, err := gitBranchFreshness(checkCtx, b.BundleRoot.Native())
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
if checkCtx.Err() != nil {
err = checkCtx.Err()
}
logdiag.LogDiag(ctx, diag.Warningf("could not check Git branch freshness: %s; run git fetch and verify the branch's upstream", err)[0])
return nil
}
if behind == 0 {
return nil
}

message := fmt.Sprintf("Git branch %q is %d commit(s) behind its upstream %q", branch, behind, upstream)
logdiag.LogDiag(ctx, diag.Warningf("%s", message)[0])
if b.AutoApprove {
return nil
}
if !cmdio.IsPromptSupported(ctx) {
return fmt.Errorf("%s; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions)", message)
}
approved, err := cmdio.AskYesOrNo(ctx, "Deploy from this branch anyway?")
if err != nil {
return fmt.Errorf("confirming deployment from a stale Git branch: %w", err)
}
if !approved {
return errors.New("deployment cancelled")
}
return nil
}

func gitBranchFreshness(ctx context.Context, dir string) (string, string, int, error) {
branch, err := gitFreshnessCommand(ctx, dir, "symbolic-ref", "--quiet", "--short", "HEAD")
if err != nil {
return "", "", 0, fmt.Errorf("cannot determine the current branch: %w", err)
}
// https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtmerge
remote, err := gitFreshnessCommand(ctx, dir, "config", "--get", "branch."+branch+".remote")
if err != nil {
return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q: %w", branch, err)
}
refs, err := gitFreshnessCommand(ctx, dir, "config", "--get-all", "branch."+branch+".merge")
if err != nil {
return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q: %w", branch, err)
}
ref, _, _ := strings.Cut(refs, "\n")
if remote == "" || ref == "" {
return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q", branch)
}
upstream := strings.TrimPrefix(ref, "refs/heads/")
if remote != "." {
remotes, err := gitFreshnessCommand(ctx, dir, "remote")
if err != nil {
return "", "", 0, fmt.Errorf("cannot list Git remotes: %w", err)
}
// branch.<name>.remote can be a URL containing credentials, not just a remote name.
if slices.Contains(strings.Split(remotes, "\n"), remote) {
upstream = remote + "/" + upstream
}
}
if _, err := gitFreshnessCommand(ctx, dir, "fetch", "--quiet", "--", remote, ref); err != nil {
return "", "", 0, fmt.Errorf("git fetch failed: %w", err)
}
// The tracking ref may not be updated if the remote's fetch refspec excludes it.
count, err := gitFreshnessCommand(ctx, dir, "rev-list", "--count", "HEAD..FETCH_HEAD")
if err != nil {
return "", "", 0, fmt.Errorf("cannot compare branch %q with its upstream: %w", branch, err)
}
behind, err := strconv.Atoi(count)
if err != nil || behind < 0 {
return "", "", 0, fmt.Errorf("invalid Git commit count %q", count)
}
return branch, upstream, behind, nil
}

func gitFreshnessCommand(ctx context.Context, dir string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
cmd.WaitDelay = time.Second
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITTERMINALPROMPTcode
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
174 changes: 174 additions & 0 deletions bundle/phases/git_freshness_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package phases

import (
"bytes"
"context"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/logdiag"
"github.com/databricks/cli/libs/vfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func runGit(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.CommandContext(t.Context(), "git", args...)
cmd.Dir = dir
output, err := cmd.CombinedOutput()
require.NoError(t, err, "git %v: %s", args, output)
}

func freshnessRepositories(t *testing.T) (string, string) {
t.Helper()
dir := t.TempDir()
remote := filepath.Join(dir, "remote.git")
local := filepath.Join(dir, "local")
writer := filepath.Join(dir, "writer")
runGit(t, dir, "init", "--bare", "--initial-branch=main", remote)
runGit(t, dir, "clone", remote, writer)
runGit(t, writer, "config", "user.name", "Test")
runGit(t, writer, "config", "user.email", "test@example.test")
runGit(t, writer, "commit", "--allow-empty", "-m", "initial")
runGit(t, writer, "push", "-u", "origin", "main")
runGit(t, dir, "clone", remote, local)
return local, writer
}

func freshnessContext(ctx context.Context, input string, interactive bool) (context.Context, *bytes.Buffer) {
var stderr bytes.Buffer
if interactive {
ctx = cmdio.InContext(ctx, cmdio.NewTestIO(strings.NewReader(input), &bytes.Buffer{}, &stderr))
} else {
ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, strings.NewReader(input), &bytes.Buffer{}, &stderr, "", ""))
}
return logdiag.InitContext(ctx), &stderr
}

func TestCheckGitBranchFreshness(t *testing.T) {
local, writer := freshnessRepositories(t)
b := &bundle.Bundle{
BundleRoot: vfs.MustNew(local),
Config: config.Root{
Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)},
},
}

ctx, output := freshnessContext(t.Context(), "", false)
require.NoError(t, CheckGitBranchFreshness(ctx, b))
assert.Empty(t, output.String())

runGit(t, local, "config", "--replace-all", "remote.origin.fetch", "+refs/heads/other:refs/remotes/origin/other")
runGit(t, writer, "commit", "--allow-empty", "-m", "one")
runGit(t, writer, "commit", "--allow-empty", "-m", "two")
runGit(t, writer, "push")

tests := []struct {
name string
input string
interactive bool
autoApprove bool
wantErr string
}{
{name: "noninteractive", wantErr: "--auto-approve"},
{name: "approved", input: "yes\n", interactive: true},
{name: "declined", input: "no\n", interactive: true, wantErr: "deployment cancelled"},
{name: "auto approved", autoApprove: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, output := freshnessContext(t.Context(), tt.input, tt.interactive)
b.AutoApprove = tt.autoApprove
err := CheckGitBranchFreshness(ctx, b)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
} else {
require.NoError(t, err)
}
assert.Contains(t, output.String(), `Git branch "main" is 2 commit(s) behind its upstream "origin/main"`)
})
}

b.Config.Experimental.CheckGitBranchFreshness = new(false)
ctx, output = freshnessContext(t.Context(), "", false)
require.NoError(t, CheckGitBranchFreshness(ctx, b))
assert.Empty(t, output.String())
}

func TestCheckGitBranchFreshnessUnavailable(t *testing.T) {
local, _ := freshnessRepositories(t)
b := &bundle.Bundle{
BundleRoot: vfs.MustNew(local),
Config: config.Root{
Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)},
},
}

runGit(t, local, "branch", "--unset-upstream")
ctx, output := freshnessContext(t.Context(), "", false)
require.NoError(t, CheckGitBranchFreshness(ctx, b))
assert.Contains(t, output.String(), `cannot determine the upstream of branch "main"`)

runGit(t, local, "branch", "--set-upstream-to=origin/main")
runGit(t, local, "remote", "set-url", "origin", filepath.Join(t.TempDir(), "missing.git"))
ctx, output = freshnessContext(t.Context(), "", false)
require.NoError(t, CheckGitBranchFreshness(ctx, b))
assert.Contains(t, output.String(), "git fetch failed")

ctx, cancel := context.WithCancel(t.Context())
cancel()
ctx, output = freshnessContext(ctx, "", false)
require.ErrorIs(t, CheckGitBranchFreshness(ctx, b), context.Canceled)
assert.Empty(t, output.String())

runGit(t, local, "checkout", "--detach")
ctx, output = freshnessContext(t.Context(), "", false)
require.NoError(t, CheckGitBranchFreshness(ctx, b))
assert.Contains(t, output.String(), "cannot determine the current branch")
}

func TestCheckGitBranchFreshnessLocalUpstream(t *testing.T) {
local, _ := freshnessRepositories(t)
runGit(t, local, "config", "user.name", "Test")
runGit(t, local, "config", "user.email", "test@example.test")
runGit(t, local, "checkout", "-b", "other")
runGit(t, local, "commit", "--allow-empty", "-m", "advance local branch")
runGit(t, local, "checkout", "main")
runGit(t, local, "branch", "--set-upstream-to=other")

b := &bundle.Bundle{
BundleRoot: vfs.MustNew(local),
Config: config.Root{
Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)},
},
}
ctx, output := freshnessContext(t.Context(), "", false)
require.ErrorContains(t, CheckGitBranchFreshness(ctx, b), "--auto-approve")
assert.Contains(t, output.String(), `Git branch "main" is 1 commit(s) behind its upstream "other"`)
}

func TestCheckGitBranchFreshnessRemoteURL(t *testing.T) {
local, writer := freshnessRepositories(t)
runGit(t, writer, "commit", "--allow-empty", "-m", "advance upstream")
runGit(t, writer, "push")
remote := filepath.Join(filepath.Dir(local), "remote.git")
runGit(t, local, "config", "branch.main.remote", remote)

b := &bundle.Bundle{
BundleRoot: vfs.MustNew(local),
Config: config.Root{
Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)},
},
}
ctx, output := freshnessContext(t.Context(), "", false)
require.ErrorContains(t, CheckGitBranchFreshness(ctx, b), "--auto-approve")
assert.Contains(t, output.String(), `Git branch "main" is 1 commit(s) behind its upstream "main"`)
assert.NotContains(t, output.String(), remote)
}
Loading