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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pom.xml
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
.lein-plugins
.idea
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ current branch.
## Usage

To be able to access the plugin from any project, put
`[lein-sha-version "0.1.1"]` into the `:plugins` vector of your `:sha`, or other
`[coreagile/lein-sha-version "0.1.2"]` into the `:plugins` vector of your `:sha`, or other
non-standard, profile. Invoke `lein` with `lein with-profile sha` to use the sha
version number for the project, e.g. `lein with-profile sha deploy clojars` to
deploy the sha based version to clojars.

Put `[lein-sha-version "0.1.1"]` into the `:plugins` vector of your `:dev`
Put `[coreagile/lein-sha-version "0.1.2"]` into the `:plugins` vector of your `:dev`
profile in `project.clj` if you always want to use a sha version with a specific
project.

Expand All @@ -22,6 +22,14 @@ To control the length of the generated SHA, you can set the `:length` key under
:sha {:length 8}
```

To add the generated SHA to your artifacts' `MANIFEST.MF`, you can set the
`:manifest-header` to your project or profile. The SHA will be written to that
header in your jar and/or uberjar.

```clj
:sha {:manifest-header "Implementation-Version"}
```

## License

Copyright © 2012 Hugo Duncan
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject lein-sha-version "0.1.2-SNAPSHOT"
(defproject coreagile/lein-sha-version "0.1.2"
:description "A Leiningen plugin to set the project version based on the git SHA."
:url "https://github.com/pallet/lein-sha-version"
:license {:name "Eclipse Public License"
Expand Down
42 changes: 28 additions & 14 deletions src/lein_sha_version/plugin.clj
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
(ns lein-sha-version.plugin
(:use
[clojure.java.io :only [file]]
[leiningen.core.main :only [debug]])
[clojure.java.io :only [file]]
[leiningen.core.main :only [warn debug]])
(:import
org.eclipse.jgit.storage.file.FileRepositoryBuilder
[org.eclipse.jgit.lib ObjectId Repository]))
org.eclipse.jgit.storage.file.FileRepositoryBuilder
[org.eclipse.jgit.lib ObjectId Repository]))

(defn git-sha [{:keys [root version sha]}]
(debug "Finding SHA for" root)
(let [^Repository repository (.. (FileRepositoryBuilder.)
(readEnvironment)
(findGitDir (file root))
(build))
^ObjectId head (.resolve repository "HEAD")]
(if head
(let [abbr (.abbreviate head (:length sha 7))]
(debug "Found SHA" (.name head) "using" (.name abbr))
(.name abbr))
(try
(let [^Repository repository (.. (FileRepositoryBuilder.)
(readEnvironment)
(findGitDir (file root))
(build))
^ObjectId head (.resolve repository "HEAD")]
(if head
(let [abbr (.abbreviate head (:length sha 7))]
(debug "Found SHA" (.name head) "using" (.name abbr))
(.name abbr))
version))
(catch Throwable e
(warn (format "Unable to get SHA: %s" (.getMessage e)))
version)))

(defn update-manifest [git-sha {manifest :manifest
{:keys [manifest-header]} :sha}]
(if manifest-header
(merge {manifest-header git-sha} manifest)
manifest))

(defn middleware
[project]
(assoc project :version (git-sha project)))
(let [version (git-sha project)
manifest (update-manifest version project)]
(-> project
(assoc :version version)
(assoc :manifest manifest))))