Skip to content

Commit

Permalink
Changed how to retrieve git tags (#64)
Browse files Browse the repository at this point in the history
* Calculating latest matching tag by executing 'git log' instead of 'git tag'.
* Updated the documentation with how latest tag is retrieved.
* Updated version to alpha9

Co-authored-by: Furkan Bayraktar <[email protected]>
  • Loading branch information
tengstrand and furkan3ayraktar authored Dec 23, 2020
1 parent 4ec8422 commit c4c9490
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 39 deletions.
27 changes: 8 additions & 19 deletions components/git/src/polylith/clj/core/git/core.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns polylith.clj.core.git.core
(:require [clojure.string :as str]
[polylith.clj.core.git.tag :as tag]
[polylith.clj.core.shell.interface :as shell]))

(defn is-git-repo? [ws-dir]
Expand Down Expand Up @@ -35,30 +36,18 @@
[:dir ws-dir]))]
(str/split-lines files)))

(defn list-tags [ws-dir pattern]
(let [sort "--sort=committerdate"]
(filterv #(-> % str/blank? not)
(str/split-lines (shell/sh "git" "tag" sort "-l" pattern :dir ws-dir)))))

(defn sha-of-tag [ws-dir tag-name]
(first (str/split-lines (shell/sh "git" "rev-list" "-1" tag-name :dir ws-dir))))

(defn first-committed-sha [ws-dir]
(last (str/split-lines (shell/sh "git" "log" "--format=%H" :dir ws-dir))))

(defn drop-or-keep [lst drop?]
(if drop? (drop-last lst) lst))
(defn drop-or-keep [elements drop?]
(when elements (if drop? (rest elements) elements)))

(defn release [ws-dir pattern previous?]
(if-let [tag-name (-> (list-tags ws-dir pattern)
(drop-or-keep previous?)
last)]
{:tag tag-name
:sha (sha-of-tag ws-dir tag-name)}
(or (-> (tag/matching-tags ws-dir pattern)
(drop-or-keep previous?)
first)
{:sha (first-committed-sha ws-dir)}))

(defn latest-stable [ws-dir pattern]
(if-let [tag-name (last (list-tags ws-dir pattern))]
{:tag tag-name
:sha (sha-of-tag ws-dir tag-name)}
{:sha (first-committed-sha ws-dir)}))
(or (first (tag/matching-tags ws-dir pattern))
{:sha (first-committed-sha ws-dir)}))
29 changes: 29 additions & 0 deletions components/git/src/polylith/clj/core/git/tag.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns polylith.clj.core.git.tag
(:require [clojure.string :as str]
[polylith.clj.core.shell.interface :as shell]
[polylith.clj.core.util.interface.str :as str-util]))

(defn log-lines [ws-dir]
(str/split-lines (shell/sh "git" "log" (str "--pretty=format:%H %d") :dir ws-dir)))

(defn skip-tag [string]
(if (str/starts-with? string "tag: ")
(subs string 5)
string))

(defn matching-tag [tag pattern]
(when (re-find (re-pattern pattern) tag)
tag))

(defn tags [line pattern]
(when-let [string (str-util/take-until
(str-util/skip-until line "tag: ") ")")]
(when-let [tag (first (filter identity
(map #(matching-tag (skip-tag %) pattern)
(str/split string #", "))))]
{:tag tag
:sha (subs line 0 40)})))

(defn matching-tags [ws-dir pattern]
(filterv identity (map #(tags % pattern)
(log-lines ws-dir))))
5 changes: 3 additions & 2 deletions components/help/src/polylith/clj/core/help/diff.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"\n"
" The pattern can be changed in " (color/purple color-mode ":stable-tag-pattern") " in ./deps.edn.\n"
"\n"
" The way the latest tag is found is by taking the last line of the output from:\n"
" git tag --sort=committerdate -l 'stable-*'\n"
" The way the latest tag is found is by taking the first line that matches the 'stable-*'\n"
" regular expression, or if no match was found, the first commit in the repository.\n"
" git log --pretty=format:'%H %d'\n"
"\n"
" Here is a compact way of listing all the commits including tags:\n"
" git log --pretty=oneline"))
Expand Down
9 changes: 5 additions & 4 deletions components/util/src/polylith/clj/core/util/interface/str.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
[polylith.clj.core.util.str :as str-util]))

(defn skip-if-ends-with [string ends-with]
(if (str/ends-with? string ends-with)
(let [chars (- (count string) (count ends-with))]
(subs string 0 chars))
string))
(when string
(if (str/ends-with? string ends-with)
(let [chars (- (count string) (count ends-with))]
(subs string 0 chars))
string)))

(defn skip-until [string separator]
(str-util/skip-until string separator))
Expand Down
2 changes: 1 addition & 1 deletion components/util/src/polylith/clj/core/util/str.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(when string
(let [index (str/index-of string separator)]
(when index
(subs string (inc index))))))
(subs string (+ (count separator) index))))))

(defn skip-prefix [string prefix]
(when string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
(def ws-schema-version {:breaking 0
:non-breaking 0})

(def version "0.1.0-alpha8")
(def date "2020-12-02")
(def version "0.1.0-alpha9")
(def date "2020-12-23")
2 changes: 1 addition & 1 deletion doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ poly help
```

```
Poly 0.1.0-alpha8 (2020-12-02) - https://github.com/polyfy/polylith
Poly 0.1.0-alpha9 (2020-12-23) - https://github.com/polyfy/polylith
poly CMD [ARGS] - where CMD [ARGS] are:
Expand Down
21 changes: 11 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ name and email.
To install the `poly` command on Linux:

- Download the [latest release](https://github.com/polyfy/polylith/releases/latest) of the `poly` jar,
e.g. `poly-0.1.0-alpha8.jar`.
e.g. `poly-0.1.0-alpha9.jar`.
- Create a directory, e.g. `/usr/local/polylith` and copy the jar file to that directory.
- Create a file with the name `poly` and put it in e.g. `/usr/local/bin` with this content:
```
Expand All @@ -149,7 +149,7 @@ while [ "$1" != "" ] ; do
shift
done
exec "/usr/bin/java" "-jar" "/usr/local/polylith/poly-0.1.0-alpha8.jar" $ARGS
exec "/usr/bin/java" "-jar" "/usr/local/polylith/poly-0.1.0-alpha9.jar" $ARGS
```
- Make sure that:
- you point to the correct jar file.
Expand Down Expand Up @@ -193,13 +193,13 @@ Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
To install the `poly` command on Windows:

- Download the [latest release](https://github.com/polyfy/polylith/releases/latest) of the `poly` jar,
e.g. `poly-0.1.0-alpha8.jar`.
e.g. `poly-0.1.0-alpha9.jar`.
- Create the `Polylith` directory somewhere on your machine, e.g.
`C:\Program Files\Polylith` and copy the jar file to that directory.
- Create the file `poly.bat` with this content (make sure you point to the jar):
```sh
@echo off
start /wait /b java -jar "C:\Program Files\Polylith\poly-0.1.0-alpha8.jar" %*
start /wait /b java -jar "C:\Program Files\Polylith\poly-0.1.0-alpha9.jar" %*
```
- Add `C:\Program Files\Polylith` to the Windows `PATH` variable.

Expand All @@ -218,7 +218,7 @@ To use it this way, add one of the following aliases to the `:aliases` section i
{
...
:aliases {:poly {:extra-deps {polylith/clj-poly
{:mvn/version "0.1.0-alpha8"}}
{:mvn/version "0.1.0-alpha9"}}
:main-opts ["-m" "polylith.clj.core.poly-cli.core"]}}
...
}
Expand Down Expand Up @@ -250,7 +250,7 @@ clj -M:poly info
Similarly, you can use other artifacts from this repository, `clj-api` or `clj-poly-migrator` as dependencies. For example, in order to add `clj-api` as a dependency, add one of the following to your `:deps` section in your `deps.edn` file:

```clojure
polylith/clj-api {:mvn/version "0.1.0-alpha8"}
polylith/clj-api {:mvn/version "0.1.0-alpha9"}
```
or
```clojure
Expand Down Expand Up @@ -1153,10 +1153,11 @@ git tag -f stable-lisa c91fdad

The way the tool finds the latest tag is to execute this command internally:
```
git tag --sort=committerdate -l 'stable-*'
```
git log --pretty=format:'%H %d'
```

Then it uses the last line of the output, or if no match was found, the first commit in the repository.
Then it uses the first line of the output that matches the `stable-*` regular expression,
or if no match was found, the first commit in the repository.

### Release

Expand Down Expand Up @@ -2428,7 +2429,7 @@ poly ws get:settings
:user-config-file "/Users/tengstrand/.polylith/config.edn",
:user-home "/Users/tengstrand",
:vcs "git",
:version "0.1.0-alpha8",
:version "0.1.0-alpha9",
:ws-schema-version {:breaking 0, :non-breaking 0}}
```
Expand Down

0 comments on commit c4c9490

Please sign in to comment.