Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable git repository root #53

Merged
merged 5 commits into from
Jun 2, 2022
Merged
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
7 changes: 2 additions & 5 deletions lib/git_ops/commit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ defmodule GitOps.Commit do
Enum.map(results, fn {:commit, result} ->
remaining_lines =
result[:body]
|> Enum.map(&String.trim/1)
|> Enum.join("\n")
|> Enum.map_join("\n", &String.trim/1)
# Remove multiple newlines
|> String.split("\n")
|> Enum.map(&String.trim/1)
|> Enum.reject(&Kernel.==(&1, ""))
Expand All @@ -124,9 +124,6 @@ defmodule GitOps.Commit do
end)

{:ok, commits}

{:error, _message, _remaining, _state, _dunno, _also_dunno} ->
:error
end
rescue
_ ->
Expand Down
1 change: 1 addition & 0 deletions lib/git_ops/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule GitOps.Config do
def mix_project, do: Application.get_env(:git_ops, :mix_project)
def changelog_file, do: Application.get_env(:git_ops, :changelog_file) || "CHANGELOG.md"
def repository_url, do: Application.get_env(:git_ops, :repository_url)
def repository_path, do: Application.get_env(:git_ops, :repository_path) || File.cwd!()
def manage_mix_version?, do: truthy?(Application.get_env(:git_ops, :manage_mix_version?))

def manage_readme_version do
Expand Down
37 changes: 21 additions & 16 deletions lib/git_ops/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule GitOps.Git do

@default_githooks_path ".git/hooks"

@spec init!() :: Git.Repository.t()
def init! do
Git.init!(File.cwd!())
@spec init!(String.t()) :: Git.Repository.t()
def init!(repo_path) do
Git.init!(repo_path)
end

@spec add!(Git.Repositor.t(), [String.t()]) :: String.t()
@spec add!(Git.Repository.t(), [String.t()]) :: String.t()
def add!(repo, args) do
Git.add!(repo, args)
end
Expand Down Expand Up @@ -70,18 +70,8 @@ defmodule GitOps.Git do
@spec hooks_path(Git.Repository.t()) :: String.t() | no_return
def hooks_path(repo) do
case Git.config(repo, ["core.hookspath"]) do
{:error, %Git.Error{message: "", code: 1}} ->
# no custom config for core.hookspath
if File.dir?(@default_githooks_path) do
@default_githooks_path
else
raise """
Could not find the default git hooks path #{inspect(@default_githooks_path)}. Is this a git repo?
"""
end

{:error, %Git.Error{message: message}} ->
raise message
{:error, error} ->
handle_hooks_path_error(error)
zachdaniel marked this conversation as resolved.
Show resolved Hide resolved

{:ok, path} ->
hookspath = String.trim_trailing(path, "\n")
Expand All @@ -95,4 +85,19 @@ defmodule GitOps.Git do
end
end
end

defp handle_hooks_path_error(error) do
with "" <- error.error,
1 <- error.code do
if File.dir?(@default_githooks_path) do
@default_githooks_path
else
raise """
Could not find the default git hooks path #{inspect(@default_githooks_path)}. Is this a git repo?
"""
end
else
_ -> raise error.message
end
end
end
8 changes: 8 additions & 0 deletions lib/mix/tasks/git_ops.check_message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ defmodule Mix.Tasks.GitOps.CheckMessage do

@doc false
def run([path]) do
# Full paths do not need to be wrapped with repo root
path =
if path == Path.absname(path) do
path
else
Path.join(Config.repository_path(), path)
end

message = File.read!(path)

case Commit.parse(message) do
Expand Down
9 changes: 4 additions & 5 deletions lib/mix/tasks/git_ops.message_hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Mix.Tasks.GitOps.MessageHook do
* `--verbose|-v` - Be more verbose. Pass this option twice to be even more verbose.
"""

alias GitOps.Git
alias GitOps.{Config, Git}

@doc false
def run(args) do
Expand Down Expand Up @@ -175,7 +175,8 @@ defmodule Mix.Tasks.GitOps.MessageHook do
if commit_msg_hook_path_override && is_binary(commit_msg_hook_path_override) do
commit_msg_hook_path_override
else
Git.init!()
Config.repository_path()
|> Git.init!()
|> Git.hooks_path()
|> Path.join(@commit_msg_hook_name)
end
Expand All @@ -184,9 +185,7 @@ defmodule Mix.Tasks.GitOps.MessageHook do

if opts[:verbose] >= 2 do
Mix.shell().info("""
Git hooks path: #{commit_msg_hook_path} (#{
if commit_msg_hook_exists, do: "existing", else: "not existing"
})
Git hooks path: #{commit_msg_hook_path} (#{if commit_msg_hook_exists, do: "existing", else: "not existing"})
""")
end

Expand Down
16 changes: 9 additions & 7 deletions lib/mix/tasks/git_ops.release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ defmodule Mix.Tasks.GitOps.Release do

current_version = String.trim(mix_project[:version])

repo = Git.init!()
repo_path = Config.repository_path()
repo = Git.init!(repo_path)

if opts[:initial] do
Changelog.initialize(changelog_path, opts)
Expand Down Expand Up @@ -135,11 +136,11 @@ defmodule Mix.Tasks.GitOps.Release do
:ok

opts[:yes] ->
tag(repo, changelog_file, prefixed_new_version, changelog_changes)
zachdaniel marked this conversation as resolved.
Show resolved Hide resolved
tag(repo, changelog_path, prefixed_new_version, changelog_changes)
:ok

true ->
confirm_and_tag(repo, changelog_file, prefixed_new_version, changelog_changes)
confirm_and_tag(repo, changelog_path, prefixed_new_version, changelog_changes)
:ok
end
end
Expand Down Expand Up @@ -190,6 +191,7 @@ defmodule Mix.Tasks.GitOps.Release do
readme_changes =
readme
|> List.wrap()
|> Enum.reject(&(&1 == false))
|> Enum.map(fn readme ->
{readme, VersionReplace.update_readme(readme, current_version, new_version, opts)}
end)
Expand All @@ -209,8 +211,8 @@ defmodule Mix.Tasks.GitOps.Release do
end)
end

defp tag(repo, changelog_file, new_version, new_message) do
Git.add!(repo, "#{changelog_file}")
defp tag(repo, changelog_path, new_version, new_message) do
Git.add!(repo, [changelog_path])
Git.commit!(repo, ["-am", "chore: release version #{new_version}"])

new_message =
Expand All @@ -226,15 +228,15 @@ defmodule Mix.Tasks.GitOps.Release do
Mix.shell().info("Don't forget to push with tags:\n\n git push --follow-tags")
end

defp confirm_and_tag(repo, changelog_file, new_version, new_message) do
defp confirm_and_tag(repo, changelog_path, new_version, new_message) do
message = """
Shall we commit and tag?

Instructions will be printed for committing and tagging if you choose no.
"""

if Mix.shell().yes?(message) do
tag(repo, changelog_file, new_version, new_message)
tag(repo, changelog_path, new_version, new_message)
else
Mix.shell().info("""
If you want to do it on your own, make sure you tag the release with:
Expand Down
30 changes: 16 additions & 14 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"},
"credo": {:hex, :credo, "1.4.1", "16392f1edd2cdb1de9fe4004f5ab0ae612c92e230433968eab00aafd976282fc", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "155f8a2989ad77504de5d8291fa0d41320fdcaa6a1030472e9967f285f8c7692"},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"earmark_parser": {:hex, :earmark_parser, "1.4.10", "6603d7a603b9c18d3d20db69921527f82ef09990885ed7525003c7fe7dc86c56", [:mix], [], "hexpm", "8e2d5370b732385db2c9b22215c3f59c84ac7dda7ed7e544d7c459496ae519c0"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"},
"excoveralls": {:hex, :excoveralls, "0.13.3", "edc5f69218f84c2bf61b3609a22ddf1cec0fbf7d1ba79e59f4c16d42ea4347ed", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cc26f48d2f68666380b83d8aafda0fffc65dafcc8d8650358e0b61f6a99b1154"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"excoveralls": {:hex, :excoveralls, "0.14.5", "5c685449596e962c779adc8f4fb0b4de3a5b291c6121097572a3aa5400c386d3", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e9b4a9bf10e9a6e48b94159e13b4b8a1b05400f17ac16cc363ed8734f26e1f4e"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"git_cli": {:hex, :git_cli, "0.3.0", "a5422f9b95c99483385b976f5d43f7e8233283a47cda13533d7c16131cb14df5", [:mix], [], "hexpm", "78cb952f4c86a41f4d3511f1d3ecb28edb268e3a7df278de2faa1bd4672eaf9b"},
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
4 changes: 4 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ defmodule GitOps.Test.ConfigTest do
assert Config.repository_url() == "repo/url.git"
end

test "repository_path returns correctly" do
assert Config.repository_path() == File.cwd!()
end

test "manage_mix_version? returns correctly" do
assert Config.manage_mix_version?() == false
end
Expand Down