From 214d4fd321238a36baafcebd4028c1bd6366c7b4 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 1 Feb 2025 22:15:06 -0500 Subject: [PATCH] The `build dev` command warns about untracked and uncommitted files so there's a complete picture of what is being packaged that's not in git. --- lib/kamal/cli/build.rb | 19 ++++++++++++++++--- lib/kamal/docker.rb | 30 ++++++++++++++++++++++++++++++ lib/kamal/git.rb | 10 ++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 lib/kamal/docker.rb diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index 80cc6637..0187e686 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -116,9 +116,22 @@ class Kamal::Cli::Build < Kamal::Cli::Base 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.sort.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.sort.each { |f| say " #{f}", :yellow } + say end with_env(KAMAL.config.builder.secrets) do diff --git a/lib/kamal/docker.rb b/lib/kamal/docker.rb new file mode 100644 index 00000000..6ae7b7f3 --- /dev/null +++ b/lib/kamal/docker.rb @@ -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 diff --git a/lib/kamal/git.rb b/lib/kamal/git.rb index a286f735..e91983bb 100644 --- a/lib/kamal/git.rb +++ b/lib/kamal/git.rb @@ -24,4 +24,14 @@ module Kamal::Git 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