Added a script to dump refs.

master
Pacman Ghost 2 years ago
parent f83fc6aa5f
commit 86c956d783
  1. 14
      cli/Program.fs
  2. 66
      git-guts/Refs.fs
  3. 6
      git-guts/Utils.fs
  4. 1
      git-guts/git-guts.fsproj
  5. 1
      git-guts/tests/TestPacks.fs
  6. 48
      git-guts/tests/TestRefs.fs
  7. 6
      git-guts/tests/fixtures/empty.refs-packed.txt
  8. 6
      git-guts/tests/fixtures/empty.refs.txt
  9. 113
      git-guts/tests/fixtures/full2.pack-data.txt
  10. 43
      git-guts/tests/fixtures/full2.pack-index.txt
  11. 10
      git-guts/tests/fixtures/full2.refs-packed.txt
  12. 12
      git-guts/tests/fixtures/full2.refs.txt
  13. BIN
      git-guts/tests/fixtures/full2.zip
  14. 2
      git-guts/tests/tests.fsproj

@ -98,6 +98,17 @@ type DumpStagingIndexCommand() =
dumpStagingIndex settings.RepoDir settings.FullDump
0
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type DumpRefsSettings() =
inherit AppSettings()
type DumpRefsCommand() =
inherit Command<DumpRefsSettings>()
override this.Execute( ctx, settings ) =
dumpRefs settings.RepoDir
0
// --------------------------------------------------------------------
[<EntryPoint>]
@ -125,5 +136,8 @@ let main argv =
cfg.AddCommand<DumpStagingIndexCommand>( "dump-stagingindex" ).WithDescription(
"Dump the staging index."
) |> ignore
cfg.AddCommand<DumpRefsCommand>( "dump-refs" ).WithDescription(
"Dump references."
) |> ignore
)
app.Run( argv )

@ -0,0 +1,66 @@
namespace git_guts
open System.IO
open System.Text
open Spectre.Console
// --------------------------------------------------------------------
[<AutoOpen>]
module Refs =
let private _findLooseRefs repoDir refType = seq {
// find loose refs in the git repo
let refsDir = Path.Join( repoDir, ".git/refs", refType )
if Directory.Exists refsDir then
for fname in Directory.GetFiles( refsDir ) do
let objName = File.ReadAllText( fname, Encoding.ASCII ).Trim()
yield ( Path.GetFileName fname, objName )
}
let private _findPackedRefs repoDir = seq {
// find packed refs in the git repo
let fname = Path.Join( repoDir, ".git/packed-refs" )
if File.Exists fname then
let mutable currRef = None
for line in File.ReadLines( fname ) do
let line2 = line.Trim()
if line2.Length > 0 && line2.[0] <> '#' then
if line2.[0] = '^' then
// the previous line is an annotated tag, this line is the target commit
yield ( fst currRef.Value, snd currRef.Value, Some (line2.Substring 1) )
currRef <- None
else
// the previous line was a normal tag - we can now return it to the caller
if currRef.IsSome then
yield ( fst currRef.Value, snd currRef.Value, None )
// save the current line (to be yield'ed later)
// NOTE: We can't yield the tag now because it might be an annotated tag,
// in which case we need to wait for the next line to get the target commit.
currRef <- Some ( line2.Substring(41), line2.Substring(0,40) )
if currRef.IsSome then
yield ( fst currRef.Value, snd currRef.Value, None )
}
let dumpRefs repoDir =
// dump the loose refs
for refType in [| "heads"; "tags" |] do
AnsiConsole.MarkupLine( makeHeader refType "" )
let looseRefs = _findLooseRefs repoDir refType |> Seq.sortBy ( fun r -> fst r ) |> Seq.toList
if looseRefs.Length > 0 then
printfn ""
for ref, objName in looseRefs do
AnsiConsole.MarkupLine( "{0} -> {1}", refStr ref, objNameStr objName )
printfn ""
// dump the packed refs
AnsiConsole.MarkupLine( makeHeader "packed refs" "" )
printfn ""
let packedRefs = _findPackedRefs repoDir |> Seq.sortBy ( fun (r, _, _) -> r )
for ref, objName, target in packedRefs do
AnsiConsole.Markup( "{0} -> {1}", refStr ref, objNameStr objName )
if target.IsSome then
AnsiConsole.Markup( " -> {0}", objNameStr target.Value )
printfn ""

@ -36,6 +36,8 @@ module Utils =
AnsiConsole.Profile.Capabilities.ColorSystem <- ColorSystem.NoColors
AnsiConsole.Profile.Capabilities.Ansi <- false
AnsiConsole.Profile.Capabilities.Links <- false
// FUDGE! Spectre.Console wraps output?!?!
AnsiConsole.Profile.Width <- 99999
let safeSpectreString (str: string) =
// escape characters that have meaning for Spectre
@ -117,6 +119,10 @@ module Utils =
// return the object name display string
"[yellow]" + objName + "[/]"
let refStr ref =
// return the ref display string
"[green]" + ref + "[/]"
let pathStr path =
// return the path display string
match path with

@ -14,6 +14,7 @@
<Compile Include="PackIndex.fs" />
<Compile Include="Pack.fs" />
<Compile Include="StagingIndex.fs" />
<Compile Include="Refs.fs" />
<Compile Include="GitRepo.fs" />
</ItemGroup>

@ -62,3 +62,4 @@ type TestPacks () =
doTest "simple.zip"
doTest "license.zip"
doTest "full.zip"
doTest "full2.zip"

@ -0,0 +1,48 @@
namespace tests
open System
open System.IO
open Microsoft.VisualStudio.TestTools.UnitTesting
open git_guts
// --------------------------------------------------------------------
[<TestClass>]
type TestRefs () =
[<TestInitialize>]
member this.init () =
// prepare to run a test
disableSpectreCapabilities
[<TestMethod>]
member this.TestDumpRefs () =
let doTest zipFname =
// set up the test repo
use gitTestRepo = new GitTestRepo( zipFname )
// dump the refs
using ( new CaptureStdout() ) ( fun cap ->
dumpRefs gitTestRepo.repoDir
let expectedFname =
let fname = Path.GetFileNameWithoutExtension( zipFname ) + ".refs.txt"
Path.Combine( __SOURCE_DIRECTORY__, "fixtures", fname )
cap.checkOutput expectedFname
)
// move the loose objects to a pack, and check again
runGitGc gitTestRepo.repoDir
using ( new CaptureStdout() ) ( fun cap ->
dumpRefs gitTestRepo.repoDir
let expectedFname =
let fname = Path.GetFileNameWithoutExtension( zipFname ) + ".refs-packed.txt"
Path.Combine( __SOURCE_DIRECTORY__, "fixtures", fname )
cap.checkOutput expectedFname
)
// run the tests
doTest "empty.zip"
doTest "full2.zip"

@ -0,0 +1,6 @@
--- heads ----------------------------------------------------------------------
--- tags -----------------------------------------------------------------------
--- packed refs ----------------------------------------------------------------

@ -0,0 +1,6 @@
--- heads ----------------------------------------------------------------------
--- tags -----------------------------------------------------------------------
--- packed refs ----------------------------------------------------------------

@ -0,0 +1,113 @@
--- OBJECT 0: commit (fpos=0xc, size=218) --------------------------------------
tree 36c4c87ea568a6728a769586ecaac54a0cbee508
parent de483a0a2160dc698967033f2c351208e6da9066
author Taka <tutorial@git-guts> 1640749994 +0000
committer Taka <tutorial@git-guts> 1640749994 +0000
Changed the greeting.
--- OBJECT 1: commit (fpos=0xa7, size=256) -------------------------------------
tree b2029d7668b35e4bd3d9f661b6b89028943124f3
parent fb407a730e3730eb07698f7ced84b596fe7fb7bd
author Taka <tutorial@git-guts> 1640749030 +0000
committer Taka <tutorial@git-guts> 1640749030 +0000
Added a file with Unicode (日本) in its name and content.
--- OBJECT 2: tag (fpos=0x16a, size=154) ---------------------------------------
object 06d4e9c2beb2c23a955bdb213d02ac63e15e3318
type commit
tag annotated-tag
tagger Taka <tutorial@git-guts> 1640750013 +0000
This is an annotated tag.
--- OBJECT 3: commit (fpos=0x1f2, size=217) ------------------------------------
tree dda83875e2c2e9d5f31615a53280bb7ecca3c3f9
parent 89bac892dbe3bc028ec2c361d836791f446ffc11
author Taka <tutorial@git-guts> 1640749006 +0000
committer Taka <tutorial@git-guts> 1640749006 +0000
Added an empty file.
--- OBJECT 4: commit (fpos=0x28c, size=217) ------------------------------------
tree 4794689f37a386016b4c5bd6e6ba757410c31e2f
parent de483a0a2160dc698967033f2c351208e6da9066
author Taka <tutorial@git-guts> 1640748991 +0000
committer Taka <tutorial@git-guts> 1640748991 +0000
Added a binary file.
--- OBJECT 5: commit (fpos=0x326, size=186) ------------------------------------
tree 92dfd7467382cbb75e4e808fe62e7e2bca44f9f2
author Taka <tutorial@git-guts> 1640748967 +0000
committer Taka <tutorial@git-guts> 1640748967 +0000
Added a file with spaces in its path.
--- OBJECT 6: tree (fpos=0x3ac, size=141) --------------------------------------
100644 818c71d03f435db011069584cda25c1f66af1a85 1x1.png
040000 7d9872b3f8a2d8f1eca570e34203e1a530242e06 a subdir
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 empty
100644 1072e024f685d47432c4d30abdbf4564eb42ba36 日本.txt
--- OBJECT 7: tree (fpos=0x43e, size=43) ---------------------------------------
100644 f75ba05f340c51065cbea2e1fdbfe5fe13144c97 hello world.txt
--- OBJECT 8: tree (fpos=0x474, size=35) ---------------------------------------
040000 f6fa0c3be2a987cc010b467aced34fdb7841e729 a subdir
--- OBJECT 9: tree (fpos=0x4a0, size=43) ---------------------------------------
100644 f967a9db1b6145568be7176f411f02dd174cc56c hello world.txt
--- OBJECT 10: tree (fpos=0x4d5, size=103) -------------------------------------
100644 818c71d03f435db011069584cda25c1f66af1a85 1x1.png
040000 7d9872b3f8a2d8f1eca570e34203e1a530242e06 a subdir
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 empty
--- OBJECT 11: tree (fpos=0x4e5, size=70) --------------------------------------
100644 818c71d03f435db011069584cda25c1f66af1a85 1x1.png
040000 7d9872b3f8a2d8f1eca570e34203e1a530242e06 a subdir
--- OBJECT 12: tree (fpos=0x4f3, size=35) --------------------------------------
040000 7d9872b3f8a2d8f1eca570e34203e1a530242e06 a subdir
--- OBJECT 13: blob (fpos=0x520, size=119) -------------------------------------
00000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 | .PNG........IHDR
00010 | 00 00 00 01 00 00 00 01 08 02 00 00 00 90 77 53 | ..............wS
00020 | de 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 | .....sRGB.......
00030 | 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 00 | ..gAMA......a...
00040 | 00 09 70 48 59 73 00 00 0e c3 00 00 0e c3 01 c7 | ..pHYs..........
00050 | 6f a8 64 00 00 00 0c 49 44 41 54 18 57 63 f8 ff | o.d....IDAT.Wc..
00060 | ff 3f 00 05 fe 02 fe a7 35 81 84 00 00 00 00 49 | .?......5......I
00070 | 45 4e 44 ae 42 60 82 .. .. .. .. .. .. .. .. .. | END.B`.
--- OBJECT 14: blob (fpos=0x594, size=14) --------------------------------------
Hello, world.
--- OBJECT 15: blob (fpos=0x5ab, size=0) ---------------------------------------
--- OBJECT 16: blob (fpos=0x5b4, size=15) --------------------------------------
japan = 日本
--- OBJECT 17: blob (fpos=0x5cc, size=4) ---------------------------------------
Yo!

@ -0,0 +1,43 @@
--- FANOUT ---------------------------------------------------------------------
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
00: 0 0 1 1 1 1 2 2 2 2 2 2 2 2 2 2
10: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
20: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
30: 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
40: 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6
50: 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
60: 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
70: 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7
80: 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9
90: 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10
a0: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
b0: 10 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11
c0: 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
d0: 11 11 11 11 11 11 11 11 11 11 11 11 11 12 13 13
e0: 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14
f0: 14 14 14 14 14 14 15 16 16 17 17 18 18 18 18 18
--- OBJECTS (18) ---------------------------------------------------------------
name crc offset
---------------------------------------- -------- --------
0: 0214f2698f786600f169378e3001348b345fa3d8 084da0ff 0x16a
1: 06d4e9c2beb2c23a955bdb213d02ac63e15e3318 d68e100e 0xc
2: 1072e024f685d47432c4d30abdbf4564eb42ba36 4a9f022f 0x5b4
3: 30c84cc603759f82470a119c5b5348001ab13500 2e1dc193 0xa7
4: 36c4c87ea568a6728a769586ecaac54a0cbee508 c35e0867 0x474
5: 4794689f37a386016b4c5bd6e6ba757410c31e2f 36ec494e 0x4e5
6: 7d9872b3f8a2d8f1eca570e34203e1a530242e06 c78df9a1 0x43e
7: 818c71d03f435db011069584cda25c1f66af1a85 cddd5436 0x520
8: 89bac892dbe3bc028ec2c361d836791f446ffc11 e28588c0 0x28c
9: 92dfd7467382cbb75e4e808fe62e7e2bca44f9f2 e42ccf40 0x4f3
10: b2029d7668b35e4bd3d9f661b6b89028943124f3 7d1e7053 0x3ac
11: dda83875e2c2e9d5f31615a53280bb7ecca3c3f9 8f17452a 0x4d5
12: de483a0a2160dc698967033f2c351208e6da9066 62e6c92f 0x326
13: e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 6e760029 0x5ab
14: f6fa0c3be2a987cc010b467aced34fdb7841e729 0f842725 0x4a0
15: f75ba05f340c51065cbea2e1fdbfe5fe13144c97 cd6524d6 0x594
16: f967a9db1b6145568be7176f411f02dd174cc56c 358a7d04 0x5cc
17: fb407a730e3730eb07698f7ced84b596fe7fb7bd 3785ebe1 0x1f2

@ -0,0 +1,10 @@
--- heads ----------------------------------------------------------------------
--- tags -----------------------------------------------------------------------
--- packed refs ----------------------------------------------------------------
refs/heads/a-branch -> 06d4e9c2beb2c23a955bdb213d02ac63e15e3318
refs/heads/master -> 30c84cc603759f82470a119c5b5348001ab13500
refs/tags/annotated-tag -> 0214f2698f786600f169378e3001348b345fa3d8 -> 06d4e9c2beb2c23a955bdb213d02ac63e15e3318
refs/tags/lightweight-tag -> 30c84cc603759f82470a119c5b5348001ab13500

@ -0,0 +1,12 @@
--- heads ----------------------------------------------------------------------
a-branch -> 06d4e9c2beb2c23a955bdb213d02ac63e15e3318
master -> 30c84cc603759f82470a119c5b5348001ab13500
--- tags -----------------------------------------------------------------------
annotated-tag -> 0214f2698f786600f169378e3001348b345fa3d8
lightweight-tag -> 30c84cc603759f82470a119c5b5348001ab13500
--- packed refs ----------------------------------------------------------------

Binary file not shown.

@ -10,7 +10,7 @@
<ItemGroup>
<Compile Include="Utils.fs" />
<Compile Include="TestPacks.fs" />
<Compile Include="TestIndex.fs" />
<Compile Include="TestRefs.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Loading…
Cancel
Save