Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Merge b4c8636 into a198dd4
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Sep 3, 2019
2 parents a198dd4 + b4c8636 commit dc675c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/xgit/core/file_mode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ defmodule Xgit.Core.FileMode do

@valid_file_modes [0o100644, 0o100755, 0o120000, 0o040000, 0o160000]

@doc ~S"""
Return a rendered version of this file mode as an octal charlist.
Optimized for the known file modes. Errors out for any other mode.
"""
@spec to_octal(file_mode :: t) :: charlist
def to_octal(file_mode)

def to_octal(0o040000), do: cover('040000')
def to_octal(0o120000), do: cover('120000')
def to_octal(0o100644), do: cover('100644')
def to_octal(0o100755), do: cover('100755')
def to_octal(0o160000), do: cover('160000')

@doc ~S"""
This guard requires the value to be one of the known git file mode values.
"""
Expand Down
12 changes: 12 additions & 0 deletions test/xgit/core/file_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ defmodule Xgit.Core.FileModeTest do
refute FileMode.valid?(FileMode.gitlink() + 1)
end

test "to_octal/1" do
assert FileMode.to_octal(FileMode.tree()) == '040000'
assert FileMode.to_octal(FileMode.symlink()) == '120000'
assert FileMode.to_octal(FileMode.regular_file()) == '100644'
assert FileMode.to_octal(FileMode.executable_file()) == '100755'
assert FileMode.to_octal(FileMode.gitlink()) == '160000'

assert_raise FunctionClauseError, fn ->
FileMode.to_octal(FileMode.gitlink() + 1)
end
end

@valid_file_modes [0o040000, 0o120000, 0o100644, 0o100755, 0o160000]

defp accepted_file_mode?(t) when is_file_mode(t), do: true
Expand Down

0 comments on commit dc675c7

Please sign in to comment.