interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t0093-direct-index-write.pl

38 lines · 1146 bytes Raw Blame History
1#!/usr/bin/perl
2#
3# Build a v2 index file from entries listed on stdin.
4# Each line: "octalmode hex-oid name"
5# Output: binary index written to stdout.
6#
7# This bypasses all D/F safety checks in add_index_entry(), simulating
8# what happens when code uses ADD_CACHE_JUST_APPEND to bulk-load entries.
9use strict;
10use warnings;
11use Digest::SHA qw(sha1 sha256);
12
13my $hash_algo = $ENV{'GIT_DEFAULT_HASH'} || 'sha1';
14my $hash_func = $hash_algo eq 'sha256' ? \&sha256 : \&sha1;
15
16my @entries;
17while (my $line = <STDIN>) {
18 chomp $line;
19 my ($mode, $oid_hex, $name) = split(/ /, $line, 3);
20 push @entries, [$mode, $oid_hex, $name];
21}
22
23my $body = "DIRC" . pack("NN", 2, scalar @entries);
24
25for my $ent (@entries) {
26 my ($mode, $oid_hex, $name) = @{$ent};
27 # 10 x 32-bit stat fields (zeroed), with mode in position 7
28 my $stat = pack("N10", 0, 0, 0, 0, 0, 0, oct($mode), 0, 0, 0);
29 my $oid = pack("H*", $oid_hex);
30 my $flags = pack("n", length($name) & 0xFFF);
31 my $entry = $stat . $oid . $flags . $name . "\0";
32 # Pad to 8-byte boundary
33 while (length($entry) % 8) { $entry .= "\0"; }
34 $body .= $entry;
35}
36
37binmode STDOUT;
38print $body . $hash_func->($body);