| 1 | package App::git::nuke_forge_fork; | ||
| 2 | # ABSTRACT: delete forks created by git-clean-forge-fork(1) | ||
| 3 | # | ||
| 4 | # Copyright (C) 2020 Sean Whitton <spwhitton@spwhitton.name> | ||
| 5 | # | ||
| 6 | # This program is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation, either version 3 of the License, or (at | ||
| 9 | # your option) any later version. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, but | ||
| 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | # General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | $App::git::nuke_forge_fork::VERSION = '0.005'; | ||
| 19 | use 5.028; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use subs 'main'; | ||
| 24 | use Getopt::Long; | ||
| 25 | use Git::Wrapper; | ||
| 26 | use API::GitForge qw(new_from_domain forge_access_token remote_forge_info); | ||
| 27 | use Try::Tiny; | ||
| 28 | use Cwd; | ||
| 29 | use Term::UI; | ||
| 30 | |||
| 31 | my $exit_main = 0; | ||
| 32 | |||
| 33 | CORE::exit main unless caller; | ||
| 34 | |||
| 35 | |||
| 36 | sub main { | ||
| 37 | shift if $_[0] and ref $_[0] eq ""; | ||
| 38 | local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; | ||
| 39 | |||
| 40 | my $term = Term::ReadLine->new("brand"); | ||
| 41 | my $upstream = "origin"; | ||
| 42 | my $git = Git::Wrapper->new(getcwd); | ||
| 43 | #<<< | ||
| 44 | try { | ||
| 45 | $git->rev_parse({ git_dir => 1 }); | ||
| 46 | } catch { | ||
| 47 | die "pwd doesn't look like a git repository ..\n"; | ||
| 48 | }; | ||
| 49 | #>>> | ||
| 50 | GetOptions "upstream=s" => \$upstream; | ||
| 51 | |||
| 52 | my @fork_branches | ||
| 53 | = grep !/\Agitforge\z/, | ||
| 54 | map { m#refs/heads/#; $' } $git->ls_remote(qw(--heads fork)); | ||
| 55 | if (@fork_branches) { | ||
| 56 | say "Would delete the following branches:"; | ||
| 57 | say " $_" for @fork_branches; | ||
| 58 | print "\n"; | ||
| 59 | exit unless $term->ask_yn(prompt => "Are you sure?"); | ||
| 60 | } | ||
| 61 | |||
| 62 | my ($forge_domain, $upstream_repo) = remote_forge_info $upstream; | ||
| 63 | my $forge = new_from_domain | ||
| 64 | domain => $forge_domain, | ||
| 65 | access_token => forge_access_token $forge_domain; | ||
| 66 | $forge->nuke_fork($upstream_repo); | ||
| 67 | $git->remote(qw(rm fork)); | ||
| 68 | |||
| 69 | EXIT_MAIN: | ||
| 70 | return $exit_main; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub exit { $exit_main = shift // 0; goto EXIT_MAIN } | ||
| 74 | |||
| 75 | __END__ |