App-karr

 view release on metacpan or  search on metacpan

t/29-worktree.t  view on Meta::CPAN

# ABSTRACT: karr must work inside git worktrees, not only in the main work-tree
#
# A git worktree shares the object database and refs with the main repo but
# lives in a different directory and uses a `.git` *file* (not directory)
# pointing at `.git/worktrees/<name>` inside the main repo. karr stores its
# state in `refs/karr/*` which are shared refs, so all operations should work
# transparently from inside a worktree.
use strict;
use warnings;
use Test::More;
use lib 't/lib';
use TestGit qw( require_git_c );
require_git_c();
use File::Temp qw( tempdir );
use Cwd qw( abs_path getcwd );
use IPC::Open3 qw( open3 );
use Symbol qw( gensym );
use YAML::XS qw( Load );

use App::karr::Git;

my $ROOT = abs_path('.');
my $BIN  = "$ROOT/bin/karr";

sub _git_ok {
    my (@cmd) = @_;
    my $rc = system(@cmd);
    is( $rc, 0, "@cmd" );
}

sub _run_karr {
    my ( $cwd, @argv ) = @_;
    my $old = getcwd();
    chdir $cwd or die "chdir $cwd: $!";

    my $stderr = gensym;
    my $pid    = open3(
        undef,
        my $stdout_fh,
        $stderr,
        $^X,
        "-I$ROOT/lib",
        $BIN,
        @argv,
    );

    my $stdout      = do { local $/; <$stdout_fh> };
    my $stderr_text = do { local $/; <$stderr> };
    waitpid( $pid, 0 );
    my $exit = $? >> 8;

    chdir $old or die "chdir $old: $!";
    return {
        exit   => $exit,
        stdout => defined $stdout      ? $stdout      : '',
        stderr => defined $stderr_text ? $stderr_text : '',
    };
}

sub _setup_main_repo {
    my $repo = tempdir( CLEANUP => 1 );
    _git_ok( 'git', 'init', '-q', '-b', 'main', $repo );
    _git_ok( 'git', '-C', $repo, 'config', 'user.email', 'test@example.com' );
    _git_ok( 'git', '-C', $repo, 'config', 'user.name',  'Test User' );

    # Worktrees require at least one commit to branch from.
    open my $fh, '>', "$repo/README.md" or die "open: $!";



( run in 2.896 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )