Skip to content

Commit 74f2e16

Browse files
committed
make use of :commits instead of recomputing the changes
1 parent 5352007 commit 74f2e16

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

packages/kmono-git/src/k16/kmono/git/files.clj

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
(:import
66
[java.io ByteArrayOutputStream]
77
[org.eclipse.jgit.diff DiffEntry DiffFormatter]
8-
[org.eclipse.jgit.lib Constants Repository]))
8+
[org.eclipse.jgit.lib Constants Repository]
9+
[org.eclipse.jgit.revwalk RevCommit RevWalk]))
910

1011
(set! *warn-on-reflection* true)
1112

@@ -41,3 +42,30 @@
4142
(or (nil? subdir)
4243
(str/starts-with? entry subdir)))))
4344
results))))))
45+
46+
(defn find-commit-changed-files
47+
"Return file paths changed by a specific commit. If `subdir` is provided, only
48+
include files within that subdir."
49+
[^String repo-path {:keys [sha subdir]}]
50+
(git/with-repo [repo repo-path]
51+
(let [commit-id (Repository/.resolve repo sha)
52+
subdir-prefix (when subdir (str subdir "/"))]
53+
(with-open [walk (RevWalk. repo)]
54+
(let [commit (RevWalk/.parseCommit walk commit-id)
55+
parent-tree (when (pos? (RevCommit/.getParentCount commit))
56+
(let [parent (RevWalk/.parseCommit walk
57+
(aget (RevCommit/.getParents commit) 0))]
58+
(RevCommit/.getTree parent)))
59+
commit-tree (RevCommit/.getTree commit)]
60+
(with-open [out (ByteArrayOutputStream.)
61+
df (DiffFormatter. out)]
62+
(DiffFormatter/.setRepository df repo)
63+
(let [diffs (DiffFormatter/.scan df parent-tree commit-tree)]
64+
(into []
65+
(comp
66+
(map #(DiffEntry/.getNewPath %))
67+
(remove #(= % DiffEntry/DEV_NULL))
68+
(filter (fn [path]
69+
(or (nil? subdir-prefix)
70+
(str/starts-with? path subdir-prefix)))))
71+
diffs))))))))

packages/kmono-version/src/k16/kmono/version.clj

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@
7474
[patterns file-path]
7575
(boolean (some #(re-find (re-pattern %) file-path) patterns)))
7676

77-
(defn- has-meaningful-changes?
78-
"Returns true if the package has changed files that don't all match ignore
79-
patterns. Files are matched against patterns using paths relative to the
80-
package root."
81-
[project-root pkg ref ignore-changes]
82-
(let [files (git.files/find-changed-files-since
83-
project-root {:ref ref
77+
(defn- commit-has-meaningful-changes?
78+
"Returns true if the commit has changed files that don't all match ignore
79+
patterns. Files are matched relative to the package root."
80+
[project-root pkg ignore-changes commit]
81+
(let [files (git.files/find-commit-changed-files
82+
project-root {:sha (:sha commit)
8483
:subdir (:relative-path pkg)})
8584
pkg-prefix (str (:relative-path pkg) "/")
8685
relative-files (map #(str/replace-first % pkg-prefix "") files)]
@@ -98,9 +97,9 @@
9897
:subdir (:relative-path pkg)})
9998
pkg-ignore-changes (or (:ignore-changes pkg) ignore-changes)
10099
commits (if (and (seq pkg-ignore-changes) (seq commits) ref)
101-
(if (has-meaningful-changes? project-root pkg ref pkg-ignore-changes)
102-
commits
103-
[])
100+
(filterv #(commit-has-meaningful-changes?
101+
project-root pkg pkg-ignore-changes %)
102+
commits)
104103
commits)
105104
pkg (assoc pkg :commits (vec commits))]
106105
[pkg-name pkg]))
@@ -118,8 +117,8 @@
118117
Any commits found will be appended to the packages `:commits` key.
119118
120119
An optional `opts` map may be provided with `:ignore-changes` - a sequence of
121-
regexp patterns. When all changed files in a package match these patterns,
122-
the package is treated as unchanged."
120+
regexp patterns. Commits where all changed files match these patterns are
121+
filtered out."
123122
{:malli/schema [:-> :string core.schema/?PackageMap core.schema/?PackageMap]}
124123
([project-root packages]
125124
(resolve-package-changes project-root nil packages))
@@ -138,8 +137,8 @@
138137
Any commits found will be appended to the packages `:commits` key.
139138
140139
An optional `opts` map may be provided with `:ignore-changes` - a sequence of
141-
regexp patterns. When all changed files in a package match these patterns,
142-
the package is treated as unchanged."
140+
regexp patterns. Commits where all changed files match these patterns are
141+
filtered out."
143142
{:malli/schema [:-> :string :string core.schema/?PackageMap core.schema/?PackageMap]}
144143
([project-root rev packages]
145144
(resolve-package-changes-since project-root rev nil packages))

packages/kmono-version/test/k16/kmono/changes_test.clj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,26 @@
158158
(is (match? {'com.kepler16/b {:version "1.1.0"
159159
:commits []}}
160160
packages))))
161+
162+
(deftest ignore-changes-per-commit-filtering-test
163+
(git.tags/create-tags *repo* {:tags ["com.kepler16/a@1.0.0"
164+
"com.kepler16/b@1.1.0"]})
165+
166+
;; Commit 1: only a .md file (should be filtered out)
167+
(fs/create-file (fs/file *repo* "packages/a/README.md"))
168+
(commit *repo* "docs: add readme")
169+
170+
;; Commit 2: a .clj file (should be kept)
171+
(fs/create-dirs (fs/file *repo* "packages/a/src"))
172+
(fs/create-file (fs/file *repo* "packages/a/src/core.clj"))
173+
(commit *repo* "feat: add core module")
174+
175+
(let [config (core.config/resolve-workspace-config *repo*)
176+
packages (->> (core.packages/resolve-packages *repo* config)
177+
(kmono.version/resolve-package-versions *repo*)
178+
(kmono.version/resolve-package-changes *repo* {:ignore-changes [".*\\.md$"]}))]
179+
;; Only the second commit should remain
180+
(is (match? {'com.kepler16/a {:version "1.0.0"
181+
:commits [{:message "feat: add core module"
182+
:body ""}]}}
183+
packages))))

0 commit comments

Comments
 (0)