-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgit-pushdir
executable file
·103 lines (74 loc) · 2.04 KB
/
git-pushdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/perl
use strict;
use warnings;
use File::Temp qw(tempdir);
use Getopt::Long qw(GetOptions);
use Pod::Usage;
# read the args
my $branch = "master";
my ($message, $help);
GetOptions(
"branch=s" => \$branch,
"message=s" => \$message,
"help" => \$help,
) or exit 1;
if ($help) {
pod2usage(0);
}
die "aborting; no commit message given\n"
unless defined $message;
die "aborting; no dir or repo given\n"
if @ARGV < 1;
my $repo = shift;
# create temporary dir
my $tempdir = tempdir(CLEANUP => 1);
# set all vars in %ENV (so that they can be used without the fear of shell escape errors)
local $ENV{WORKDIR} = $tempdir;
local $ENV{BRANCH} = $branch;
local $ENV{REPO} = $repo;
local $ENV{MESSAGE} = $message;
# git clone
print "# cloning the repo using git\n";
system('cd "$WORKDIR" && git clone --depth=1 --branch="$BRANCH" "$REPO" .') == 0
or exit 1;
# unlink all files in the repo, and copy from source
print "# updating the files using tar\n";
for my $file (glob("$tempdir/* $tempdir/.??*")) {
if ($file ne "$tempdir/.git") {
local $ENV{FN} = $file;
system('rm -rf "$FN"') == 0
or die "failed to remove file $file:$!";
}
}
system('tar cvf - . | (cd "$WORKDIR" && tar xf -)') == 0
or exit 1;
# add & commit
print "# committing the changes using git\n";
system('cd "$WORKDIR" && git add -A .') == 0
or exit 1;
system('cd "$WORKDIR" && git commit -m "$MESSAGE"') == 0
or exit 0; # means that the workdir is clean
# push
print "# pushing the changes using git\n";
system('cd $WORKDIR && git push origin "$BRANCH"') == 0
or exit 1;
exit 0;
__END__
=head1 NAME
git-pushdir - push files in current directory to a Git repository
=head1 SYNOPSIS
% git-pushdir -m 'commit message' <git-repo-url>
=head1 OPTIONS
=over 4
=item -m <message>
commit message (mandatory)
=item -b <branch>
name of the branch to which the files should be pushed (default: master)
=back
=head1 AUTHOR
Kazuho Oku
=head1 SOURCE REPOSITORY
https://github.com/kazuho/kaztools
=head1 LICENSE
MIT
=cut