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

Commit

Permalink
Implement Xgit.Core.ObjectId.to_binary_iodata/1. (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Sep 3, 2019
1 parent ca739f6 commit 6fad325
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/xgit/core/object_id.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ defmodule Xgit.Core.ObjectId do
end
end

@doc ~S"""
Convert an object ID to raw binary representation.
## Return Value
A 20-byte binary encoding the object ID.
"""
@spec to_binary_iodata(id :: t) :: binary
def to_binary_iodata(id), do: Base.decode16!(id, case: :lower)

@doc ~S"""
Assign an object ID for a given data blob.
Expand Down
5 changes: 2 additions & 3 deletions lib/xgit/repository/working_tree/write_index_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Xgit.Repository.WorkingTree.WriteIndexFile do

alias Xgit.Core.DirCache
alias Xgit.Core.DirCache.Entry, as: DirCacheEntry
alias Xgit.Core.ObjectId
alias Xgit.Util.NB
alias Xgit.Util.TrailingHashDevice

Expand Down Expand Up @@ -108,15 +109,13 @@ defmodule Xgit.Repository.WorkingTree.WriteIndexFile do
NB.encode_uint32(uid),
NB.encode_uint32(gid),
NB.encode_uint32(size),
encode_object_id(object_id),
ObjectId.to_binary_iodata(object_id),
encode_v2_flags(stage, assume_valid?, extended?, name_length),
name,
padding(name_length)
])
end

defp encode_object_id(object_id), do: Base.decode16!(object_id, case: :lower)

defp encode_v2_flags(stage, assume_valid?, extended?, name_length) do
value =
value_if_boolean(assume_valid?, 0x8000) +
Expand Down
6 changes: 6 additions & 0 deletions test/xgit/core/object_id_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ defmodule Xgit.Core.ObjectIdTest do
assert ObjectId.from_hex_charlist('1234567890abXdef12341234567890abcdef1234') == false
end

test "to_binary_iodata/1" do
assert ObjectId.to_binary_iodata("1234567890abcdef12341234567890abcdef1234") ==
<<18, 52, 86, 120, 144, 171, 205, 239, 18, 52, 18, 52, 86, 120, 144, 171, 205, 239,
18, 52>>
end

describe "calculate_id/3" do
test "happy path: SHA hash with string content" do
assert ObjectId.calculate_id("test content\n", :blob) ==
Expand Down

0 comments on commit 6fad325

Please sign in to comment.