From 89b581c6fa663d8a9db728d98e7f56c83c991444 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 11 Feb 2020 14:34:08 -0700 Subject: rework script into API::GitForge generic interface Signed-off-by: Sean Whitton --- lib/App/git/clean_forge_fork.pm | 84 ++++++++++++++++++++++++++++++++++++++++ lib/App/git/clean_forge_repo.pm | 78 +++++++++++++++++++++++++++++++++++++ lib/App/git/nuke_forge_fork.pm | 85 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 lib/App/git/clean_forge_fork.pm create mode 100644 lib/App/git/clean_forge_repo.pm create mode 100644 lib/App/git/nuke_forge_fork.pm (limited to 'lib/App') diff --git a/lib/App/git/clean_forge_fork.pm b/lib/App/git/clean_forge_fork.pm new file mode 100644 index 0000000..bffcbab --- /dev/null +++ b/lib/App/git/clean_forge_fork.pm @@ -0,0 +1,84 @@ +package App::git::clean_forge_fork; +# ABSTRACT: create tidy forks for pull requests +# +# Copyright (C) 2017, 2020 Sean Whitton +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use 5.028; +use strict; +use warnings; + +use subs 'main'; +use Term::UI; +use Getopt::Long; +use Git::Wrapper; +use API::GitForge qw(new_from_domain forge_access_token remote_forge_info); +use Try::Tiny; +use Cwd; + +my $exit_main = 0; + +CORE::exit main unless caller; + +=func main + +Implementation of git-clean-forge-fork(1). Please see documentation +for that command. + +Normally takes no arguments and responds to C<@ARGV>. If you want to +override that you can pass an arrayref of arguments, and those will be +used instead of the contents of C<@ARGV>. + +=cut + +sub main { + shift if $_[0] and ref $_[0] eq ""; + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + + my $term = Term::ReadLine->new("brand"); + my $upstream = "origin"; + my $git = Git::Wrapper->new(getcwd); + #<<< + try { + $git->rev_parse({ git_dir => 1 }); + } catch { + die "pwd doesn't look like a git repository ..\n"; + }; + #>>> + GetOptions "upstream=s" => \$upstream; + + my ($forge_domain, $upstream_repo) = remote_forge_info $upstream; + exit + unless $term->ask_yn( + prompt => "Do you want to submit changes against $upstream_repo?"); + + my $forge = new_from_domain + domain => $forge_domain, + access_token => forge_access_token $forge_domain; + my $fork_uri = $forge->clean_fork($upstream_repo); + if (grep /\Afork\z/, $git->remote) { + $fork_uri eq ($git->remote(qw(get-url fork)))[0] + or die "fork remote exists but has wrong URI\n"; + } else { + $git->remote(qw(add fork), $fork_uri); + } + + EXIT_MAIN: + return $exit_main; +} + +sub exit { $exit_main = shift // 0; goto EXIT_MAIN } + +1; diff --git a/lib/App/git/clean_forge_repo.pm b/lib/App/git/clean_forge_repo.pm new file mode 100644 index 0000000..6ecb790 --- /dev/null +++ b/lib/App/git/clean_forge_repo.pm @@ -0,0 +1,78 @@ +package App::git::clean_forge_repo; +# ABSTRACT: create repos on git forges with optional features disabled +# +# Copyright (C) 2020 Sean Whitton +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use 5.028; +use strict; +use warnings; + +use subs 'main'; +use Cwd; +use Term::UI; +use Getopt::Long; +use Git::Wrapper; +use API::GitForge qw(new_from_domain forge_access_token remote_forge_info); +use Try::Tiny; + +my $exit_main = 0; + +CORE::exit main unless caller; + +=func main + +Implementation of git-clean-forge-repo(1). Please see documentation +for that command. + +Normally takes no arguments and responds to C<@ARGV>. If you want to +override that you can pass an arrayref of arguments, and those will be +used instead of the contents of C<@ARGV>. + +=cut + +sub main { + shift if $_[0] and ref $_[0] eq ""; + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + + my $term = Term::ReadLine->new("brand"); + my $remote = "origin"; + my $git = Git::Wrapper->new(getcwd); + #<<< + try { + $git->rev_parse({ git_dir => 1 }); + } catch { + die "pwd doesn't look like a git repository ..\n"; + }; + #>>> + GetOptions "remote=s" => \$remote; + + my ($forge_domain, $forge_repo) = remote_forge_info $remote; + exit + unless $term->ask_yn( + prompt => "Do you want to create repo $forge_repo?"); + + my $forge = new_from_domain + domain => $forge_domain, + access_token => forge_access_token $forge_domain; + $forge->clean_repo($forge_repo); + + EXIT_MAIN: + return $exit_main; +} + +sub exit { $exit_main = shift // 0; goto EXIT_MAIN } + +1; diff --git a/lib/App/git/nuke_forge_fork.pm b/lib/App/git/nuke_forge_fork.pm new file mode 100644 index 0000000..6e6997a --- /dev/null +++ b/lib/App/git/nuke_forge_fork.pm @@ -0,0 +1,85 @@ +package App::git::nuke_forge_fork; +# ABSTRACT: delete forks created by git-clean-forge-fork(1) +# +# Copyright (C) 2020 Sean Whitton +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use 5.028; +use strict; +use warnings; + +use subs 'main'; +use Getopt::Long; +use Git::Wrapper; +use API::GitForge qw(new_from_domain forge_access_token remote_forge_info); +use Try::Tiny; +use Cwd; +use Term::UI; + +my $exit_main = 0; + +CORE::exit main unless caller; + +=func main + +Implementation of git-nuke-forge-fork(1). Please see documentation +for that command. + +Normally takes no arguments and responds to C<@ARGV>. If you want to +override that you can pass an arrayref of arguments, and those will be +used instead of the contents of C<@ARGV>. + +=cut + +sub main { + shift if $_[0] and ref $_[0] eq ""; + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + + my $term = Term::ReadLine->new("brand"); + my $upstream = "origin"; + my $git = Git::Wrapper->new(getcwd); + #<<< + try { + $git->rev_parse({ git_dir => 1 }); + } catch { + die "pwd doesn't look like a git repository ..\n"; + }; + #>>> + GetOptions "upstream=s" => \$upstream; + + my @fork_branches + = grep !/\Agitforge\z/, + map { m#refs/heads/#; $' } $git->ls_remote(qw(--heads fork)); + if (@fork_branches) { + say "Would delete the following branches:"; + say " $_" for @fork_branches; + print "\n"; + exit unless $term->ask_yn(prompt => "Are you sure?"); + } + + my ($forge_domain, $upstream_repo) = remote_forge_info $upstream ; + my $forge = new_from_domain + domain => $forge_domain, + access_token => forge_access_token $forge_domain; + $forge->nuke_fork($upstream_repo); + $git->remote(qw(rm fork)); + + EXIT_MAIN: + return $exit_main; +} + +sub exit { $exit_main = shift // 0; goto EXIT_MAIN } + +1; -- cgit v1.2.3