App-Gitc

 view release on metacpan or  search on metacpan

bin/gitc-open  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

#    Copyright 2012 Grant Street Group, All Rights Reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# PODNAME: gitc-open
# ABSTRACT: Open a new changeset
our $VERSION = '0.60'; # VERSION

use App::Gitc::Util qw(
    changeset_group
    changeset_merged_to
    confirm
    current_branch
    full_changeset_name
    git
    git_fetch_and_clean_up
    guarantee_a_clean_working_directory
    history
    history_owner
    history_status
    is_auto_fetch
    is_valid_ref
    its_for_changeset
    meta_data_add
    meta_data_rm
    project_config
    project_name
    state_blocked
);
use App::Gitc::Reversible;
use Getopt::Long;
use List::MoreUtils qw( none );

my $onto;
GetOptions( 'onto=s' => \$onto );

my $changeset_name = shift or die "You must specify a changeset name\n";

exec 'gitc-quickfix', if $changeset_name eq 'quickfix';

die "You may not create a changeset named '$changeset_name'\n"
    if $changeset_name =~ m/^(master|test|stage|prod)$/;

my $its = its_for_changeset($changeset_name);
if ($its) {
    my $issue = $its->get_issue($changeset_name);
    my $state = $its->issue_state($issue);
    if ($issue && state_blocked('open', $state)) {
        my $its_label = $its->label_issue;
        die "gitc-open aborting\n"
            unless confirm("$its_label is @{[ uc $state ]}.  Proceed?");
    }
}

my $history = history($changeset_name);
if (@$history) {
    my $owner = history_owner($history);
    my $status = history_status($history);
    die   "A changeset named '$changeset_name' already exists.\n"
        . "It was created by $owner and currently has status $status.\n"
        . "For more details, try 'gitc history $changeset_name'\n"
        ;
}


git_fetch_and_clean_up() if is_auto_fetch();
$onto = validate_onto_argument( $changeset_name, $onto );
my $branch = current_branch();

my $stash;
reversibly {
    failure_warning "\nCanceling gitc open\n";

    $stash = guarantee_a_clean_working_directory();
    to_undo { git "stash apply $stash" if $stash; $stash = undef };

    # create a new branch for this changeset
    git "checkout --no-track -b $changeset_name $onto";
    to_undo {
        git "checkout -f master";
        git "reset --hard HEAD";
        git "branch -D $changeset_name";
    };

    # make a note about opening the changeset
    my $id = meta_data_add({
        action    => 'open',
        changeset => $changeset_name,
    });
    to_undo { meta_data_rm(id => $id, changeset => $changeset_name) };
};

if ($its) {
    # update the ITS status (fail with a warning)
    my $its_name = $its->label_service;
    eval {
        print STDERR "Updating $its_name...";
        my $project = project_name();
        my $what_happened = $its->transition_state({
            command   => 'open',
            message   => "Opened a new changeset: $project#$changeset_name",
            changeset => $changeset_name,
        });
        print STDERR "done\n";
        warn $what_happened;
    };
    warn "Updating $its_name failed! $@\n" if $@;
}



( run in 0.559 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )