From 557ef577085017887cfbc8bcc8445b719a78cf95 Mon Sep 17 00:00:00 2001 From: kballou Date: Wed, 8 Feb 2017 19:48:24 -0700 Subject: WIP: initial packfile parser --- lib/exgit/packfile.ex | 13 +++++++++++++ test/exgit/packfile_test.exs | 24 ++++++++++++++++++++++++ test/fixtures/packfiles/pack-simple.idx | Bin 0 -> 1156 bytes test/fixtures/packfiles/pack-simple.pack | Bin 0 -> 210 bytes test/fixtures/packfiles/pack-simple3.pack | Bin 0 -> 210 bytes 5 files changed, 37 insertions(+) create mode 100644 test/fixtures/packfiles/pack-simple.idx create mode 100644 test/fixtures/packfiles/pack-simple.pack create mode 100644 test/fixtures/packfiles/pack-simple3.pack diff --git a/lib/exgit/packfile.ex b/lib/exgit/packfile.ex index 4fda345..3823259 100644 --- a/lib/exgit/packfile.ex +++ b/lib/exgit/packfile.ex @@ -2,4 +2,17 @@ defmodule Exgit.Packfile do @moduledoc """ Module for parsing Git Packfiles """ + + @spec parse_header(binary) :: {:pack, 2|3, integer} + def parse_header(packfile) do + <<"PACK" :: utf8, 2 :: size(32), n :: size(32)>> <> rest = packfile + {:pack, 2, n, rest} + end + + @spec parse_objects(integer, binary) :: [{:commit|:blob|:tree, + binary, + binary}] + def parse_objects(n, pack) do + nil + end end diff --git a/test/exgit/packfile_test.exs b/test/exgit/packfile_test.exs index fd49787..a3c5d4c 100644 --- a/test/exgit/packfile_test.exs +++ b/test/exgit/packfile_test.exs @@ -1,4 +1,28 @@ defmodule Exgit.Packfile.Test do use ExUnit.Case + import Exgit.Packfile + doctest Exgit.Packfile + + @fixtures_dir "test/fixtures/packfiles/" + + describe "can parse header information" do + test "header with version 2" do + filename = Path.join(@fixtures_dir, "pack-simple.pack") + packfile = File.read!(filename) + assert {:pack, 2, 3, _} = parse_header(packfile) + end + end + + describe "parse objects from pack" do + test "can return all objects from pack" do + filename = Path.join(@fixtures_dir, "pack-simple.pack") + packfile = File.read!(filename) + {:pack, 2, 3, rest} = parse_header(packfile) + assert [{:commit, "ca5bae75ae512b3e3a3d86f6009452d24cc3873f", }, + {:blob, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", _}, + {:tree, "4d5fcadc293a348e88f777dc0920f11e7d71441c", _}] = + parse_objects(3, rest) + end + end end diff --git a/test/fixtures/packfiles/pack-simple.idx b/test/fixtures/packfiles/pack-simple.idx new file mode 100644 index 0000000..0bd43fd Binary files /dev/null and b/test/fixtures/packfiles/pack-simple.idx differ diff --git a/test/fixtures/packfiles/pack-simple.pack b/test/fixtures/packfiles/pack-simple.pack new file mode 100644 index 0000000..58a2362 Binary files /dev/null and b/test/fixtures/packfiles/pack-simple.pack differ diff --git a/test/fixtures/packfiles/pack-simple3.pack b/test/fixtures/packfiles/pack-simple3.pack new file mode 100644 index 0000000..58a2362 Binary files /dev/null and b/test/fixtures/packfiles/pack-simple3.pack differ -- cgit v1.2.1