+sub rm_subdir_cached ($) {
+ my ($subdir) = @_;
+ runcmd qw(git rm --quiet -rf --cached --ignore-unmatch), $subdir;
+}
+
+sub read_tree_subdir ($$) {
+ my ($subdir, $new_tree_object) = @_;
+ # If $new_tree_object is '', the subtree is deleted.
+ confess unless defined $new_tree_object;
+ rm_subdir_cached $subdir;
+ runcmd qw(git read-tree), "--prefix=$subdir/", $new_tree_object
+ if length $new_tree_object;
+}
+
+sub read_tree_debian ($) {
+ my ($treeish) = @_;
+ read_tree_subdir 'debian', "$treeish:debian";
+ rm_subdir_cached 'debian/patches';
+}
+
+sub read_tree_upstream ($;$$) {
+ my ($treeish, $keep_patches, $tree_with_debian) = @_;
+ # if $tree_with_debian is supplied, will use that for debian/
+ # otherwise will save and restore it. If $tree_with_debian
+ # is '' then debian/ is deleted.
+ my $debian =
+ defined $tree_with_debian ? "$tree_with_debian:debian"
+ : cmdoutput qw(git write-tree --prefix=debian/);
+ runcmd qw(git read-tree), $treeish;
+ read_tree_subdir 'debian', $debian;
+ rm_subdir_cached 'debian/patches' unless $keep_patches;
+}
+