NAME

CPAN::InGit::MutableTree - Utility object that represents a Git Tree and pending changes

SYNOPSIS

my $t= CPAN::InGit::MutableTree->new(
  parent   => $cpan_repo,
  tree     => $git_tree_obj,
  branch   => $name,
);

$t->set_path('path/to/file',  \$file_data);
$t->set_path('path/to/file2', \$file_data);
$t->set_path('other/path' => \$path, ( symlink => 1 ));
$t->commit("Message");

DESCRIPTION

This object wraps a Git::Raw::Tree, optionally tied to a Git::Raw::Branch. It can store changes to the tree which have not been committed yet, but which are seen when querying the paths of the tree. The changes can be serialized in e.g. a web framework's session, and then you can commit them when ready.

If use_workdir is set, this will instead write changes to the working directory and add them to the Git index, where the user can commit them.

ATTRIBUTES

parent

An instance of CPAN::InGit

tree

An instance of Git::Raw::Tree which is the starting state for this MutableTree instance. This attribute gets updated to point to the new tree by calls to "update_tree" or "commit".

branch

An instance of Git::Raw::Branch, which may be coerced from a branch name to the constructor. This may be undef if the tree is not the head of any branch.

has_changes

True if any "set_path" calls caused a change that needs written back to the tree, and hasn't been written yet.

use_workdir

True if changes written to this MutableTree should use the Git index (working directory) instead of directly updating the branch in Git's storage.

git_repo

Convenient accessor for ->parent->git_repo.

METHODS

get_path

my ($git_obj, $mode)= @{ $tree->get_path($path) };

Return the Git object and mode at $path, or undef if nothing exists there. The result reflects pending "set_path" changes (or the Git index, with "use_workdir"). Directories are returned as a Git::Raw::Tree with mode 0040000. The empty path refers to the root.

list_dir

my $entries= $tree->list_dir($path);
# { $name => [ $git_obj, $mode ], ... }

Return the entries of the directory at $path (default: the root) in the same form as "get_path", or undef if $path is not a directory.

set_path

$tree->set_path('path/within/repo', undef); # remove file or directory
$tree->set_path('path/within/repo', \$bytes, %opts);
$tree->set_path('path/within/repo', $blob, %opts);
$tree->set_path('path/within/repo', \%directory);
$tree->set_path('path/within/repo', $git_tree);

Add, replace, or remove the file or directory at a path within the tree.

File content can be a scalar-ref of bytes or a Git::Raw::Blob. The only option is mode, which defaults to 0100644. Use 0100755 for executables and 0120000 for a symlink (whose content is the link target).

A hashref or Git::Raw::Tree creates a directory, replacing any directory already at that path. Each key of the hashref is a relative name (which may contain / to reach into subdirectories) and each value is file content as above, [ $content, $mode ], or another hashref or Tree for a subdirectory.

check_new_paths

$tree->check_new_paths(@repo_paths);

Check that paths can be added without replacing existing entries. Directories used as parents must be real directories. Checks the index and working directory when using the checked-out branch.

diff

my $deltas= $tree->diff($other_tree, %options);
# [
#   { path => 'lib/Foo.pm', status => 'modified',
#     old_id => $blob_id, new_id => $blob_id, old_mode => 0100644, new_mode => 0100644,
#     additions => 3, deletions => 1, binary => 0, patch => "diff --git ...",
#   },
#   ...
# ]

Compare this tree (including pending changes) to another MutableTree or Git::Raw::Tree, describing what it takes to get from this tree to $other_tree. undef means an empty tree. Each delta has a status of added, deleted, modified, renamed, or type_change. Renames also have an old_path. The id and mode of a side where the file does not exist are undef.

Options:

paths

Arrayref of paths (or git pathspecs) to limit the comparison

patch

Boolean, whether to include the unified diff text of each file as patch

context_lines

Number of unchanged lines around each change in patch (default 3)

renames

Boolean, whether to detect renamed files (default true)

repo

A Git::Raw::Repository able to read the objects of both trees. Defaults to "git_repo".

update_tree

$tree->update_tree;

Store any pending changes from "set_path" into Git object storage and update the "tree" attribute to point to the new Git::Raw::Tree. This does not commit the tree to a branch.

commit

$commit= $tree->commit($message, %options);

# Options:
#   author        => Git::Raw::Signature
#   committer     => Git::Raw::Signature,
#   create_branch => $branch_name

Commit any pending changes from "set_path" and write a commit message for the change. IN order to make a commit, you must either be on a "branch", using the HEAD in the working directory, or specify the 'create_branch' option.

If this tree is using the working directory, it updates the index and HEAD as if a user had run 'git commit' in the working directory.

If you specify 'create_branch', it creates a new branch and updates the "branch" attribute to refer to it.

VERSION

version 0.007

AUTHOR

Michael Conrad <mike@nrdvana.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Michael Conrad, and IntelliTree Solutions.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.