#!/usr/bin/perl
use strict;
use warnings;
use SVK::Test;
plan('no_plan');

our $output;

# I'll turn it into a proper test, later; turn the lights on for now
$ENV{DEBUG_INTERACTIVE} = 1;

# create a repository (simulating upstream http://dev.catalyst.perl.org/repos)
my ( $xd_remote, $svk_remote ) = build_test('remote');
my ( $copath_remote, $coapath_remote ) = get_copath('remote');
my ( $repospath_remote, undef, $repos_remote ) = $xd_remote->find_repos('/remote/', 1);
$svk_remote->checkout('/remote/', $copath_remote);

# create tree structure
mkdir "$copath_remote/Project";     # equiv to bast/DBIx-Class
mkdir "$copath_remote/Project/A";   # equiv to bast/DBIx-Class/0.08
overwrite_file("$copath_remote/Project/A/foo", "foobar\n");
$svk_remote->add("$copath_remote/Project");
$svk_remote->commit('-m', 'project branch A', $copath_remote);

# create new branch, equiv to bast/DBIx-Class/0.09
$svk_remote->copy('-m', 'new branch', '/remote/Project/A', '/remote/Project/B');

# add a file in branch A, pull it into B
overwrite_file("$copath_remote/Project/A/bar", "created in A\n");
$svk_remote->add("$copath_remote/Project/A/bar");
$svk_remote->commit('-m', 'bar created in remote, A', $copath_remote);
$svk_remote->update($copath_remote);
$svk_remote->smerge('-It', "$copath_remote/Project/B");
$svk_remote->commit('-m', 'commit after merge', "$copath_remote/Project/B");



# now create a local repository equiv to *my* //mirror
my ( $xd_local, $svk_local ) = build_test();
my ( $copath_mirror, $coapath_mirror ) = get_copath('mirror');
my ( $repospath_local, undef, $repos_local ) = $xd_local->find_repos('//', 1);

$svk_local->mkdir('-m', 'mirrored repos', '//mirror');
$svk_local->mkdir('-m', 'local branches repos', '//local');

# mirror the remote
$svk_local->mirror("file://$repospath_remote/Project", '//mirror/Project');
$svk_local->sync('//mirror/Project');


# make local copy equiv to *my* //local
# as I understand it, this is the key to the trouble; I should be copying
# //mirror/Project/A -> //local/project-a (or some such)
# but copying the entire tree to //local, including both branches is the source of trouble.
my ( $copath_local, $coapath_local ) = get_copath('local');
$svk_local->copy('-m', 'local copy of entire Project', '//mirror/Project', '//local/Project');
$svk_local->checkout('//local', $copath_local);

### pull
$svk_local->pull("$copath_local/Project/A");

# make a change to branch A and push it (one or both of these pushes breaks the repos??)
append_file("$copath_local/Project/A/foo", "modified in local branch A\n");
$svk_local->commit('-m', 'change in local copy, branch A', $copath_local);
$svk_local->push("$copath_local/Project/A");

=for comment

# uncomment this block to generate a merge conflict and empty merge

# make a change to branch B and push it
append_file("$copath_local/Project/B/foo", "modified in local branch B\n");
$svk_local->commit('-m', 'change in local copy, branch B', $copath_local);
###$svk_local->push("$copath_local/Project/B");
$svk_local->push("$copath_local/Project"); # no different than pushing Project/B

=cut

$svk_local->checkout('//mirror/Project/B', "$copath_mirror/Project/B");
$svk_local->pull("$copath_mirror/Project/B");

exit;  # bail out here... I used to have:

$svk_remote->pull("$copath_remote/Project/B");
    # pull won't work here, not copied from a mirror

$svk_remote->smerge('-It', "$copath_remote/Project/B");
    # this works (merge conflict); I expected an error, or an empty merge??
