Skip to content

Commit

Permalink
The build dev command warns about untracked and uncommitted files
Browse files Browse the repository at this point in the history
so there's a complete picture of what is being packaged that's not in git.
  • Loading branch information
flavorjones committed Feb 2, 2025
1 parent 2127f17 commit 7c75aa5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/kamal/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,22 @@ def dev

ensure_docker_installed

uncommitted_changes = Kamal::Git.uncommitted_changes
if uncommitted_changes.present?
say "WARNING: building with uncommitted changes:\n #{uncommitted_changes}", :yellow
docker_included_files = Set.new(Kamal::Docker.included_files)
git_uncommitted_files = Set.new(Kamal::Git.uncommitted_files)
git_untracked_files = Set.new(Kamal::Git.untracked_files)

docker_uncommitted_files = docker_included_files & git_uncommitted_files
if docker_uncommitted_files.any?
say "WARNING: Files with uncommitted changes will be present in the dev container:", :yellow
docker_uncommitted_files.each { |f| say " #{f}", :yellow }
say
end

docker_untracked_files = docker_included_files & git_untracked_files
if docker_untracked_files.any?
say "WARNING: Untracked files will be present in the dev container:", :yellow
docker_untracked_files.each { |f| say " #{f}", :yellow }
say
end

with_env(KAMAL.config.builder.secrets) do
Expand Down
30 changes: 30 additions & 0 deletions lib/kamal/docker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "tempfile"
require "open3"

module Kamal::Docker
extend self
BUILD_CHECK_TAG = "kamal-local-build-check"

def included_files
Tempfile.create do |dockerfile|
dockerfile.write(<<~DOCKERFILE)
FROM busybox
COPY . app
WORKDIR app
CMD find . -type f | sed "s|^\./||"
DOCKERFILE
dockerfile.close

cmd = "docker buildx build -t=#{BUILD_CHECK_TAG} -f=#{dockerfile.path} ."
system(cmd) || raise("failed to build check image")
end

cmd = "docker run --rm #{BUILD_CHECK_TAG}"
out, err, status = Open3.capture3(cmd)
unless status
raise "failed to run check image:\n#{err}"
end

out.lines.map(&:strip)
end
end
10 changes: 10 additions & 0 deletions lib/kamal/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ def uncommitted_changes
def root
`git rev-parse --show-toplevel`.strip
end

# returns an array of relative path names of files with uncommitted changes
def uncommitted_files
`git ls-files --modified`.lines.map(&:strip)
end

# returns an array of relative path names of untracked files, including gitignored files
def untracked_files
`git ls-files --others`.lines.map(&:strip)
end
end

0 comments on commit 7c75aa5

Please sign in to comment.