英文:
Errors with git clone in perl script
问题
I am trying to write a Perl script that does git operations. I am getting errors in the script when running it and it seems to be with the git clone line. Any help is appreciated.
#!/usr/bin/perl
use strict;
use warnings;
my $url = "git@devbitbucket.ethos.com:7999";
print "What is the project asset code? ";
chomp(my $projectcode = <> );
print "$projectcode\n";
print "What is the repository name? ";
chomp(my $reponame = <> );
print "$reponame\n";
chdir "cd /export/appl/bbdev/scripts/files/";
chdir;
print "git clone ssh://$url/$projectcode/$reponame.git\n";
system( 'git clone ssh://$url/$projectcode/$reponame.git $reponame' );
#$reponame =~ s/\..*//s;
chdir "cd $reponame";
chdir;
system( 'cp -p /export/appl/bbdev/scripts/files/* .' );
system( 'git status' );
system( 'git add -A && git commit -m "Default Repository Initial Files" ' );
system( 'git push --all' );
system( 'git status' );
exit 0;
Error
git clone ssh://git@devbitbucket.ethos.com:7999/mikt/test2.git
fatal: No directory name could be guessed.
Please specify a directory on the command line
fatal: not a git repository (or any parent up to mount point /appl)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
英文:
I am trying to write a Perl script that does git operations. I am getting errors in the script when running it and it seems to be with the git clone line. Any help is appreciated.
#!/usr/bin/perl
use strict;
use warnings;
my $url = "git\@devbitbucket.ethos.com:7999";
print "What is the project asset code? ";
chomp(my $projectcode = <> );
print "$projectcode\n";
print "What is the repository name? ";
chomp(my $reponame = <> );
print "$reponame\n";
chdir "cd /export/appl/bbdev/scripts/files/";
chdir;
print "git clone ssh://$url/$projectcode/$reponame.git\n";
system( 'git clone ssh://$url/$projectcode/$reponame.git $reponame' );
#$reponame =~ s/\..*//s;
chdir "cd $reponame";
chdir;
system( 'cp -p /export/appl/bbdev/scripts/files/* .' );
system( 'git status' );
system( 'git add -A && git commit -m "Default Repository Initial Files" ');
system( 'git push --all' );
system( 'git status' );
exit 0;
Error
git clone ssh://git@devbitbucket.ethos.com:7999/mikt/test2.git
fatal: No directory name could be guessed.
Please specify a directory on the command line
fatal: not a git repository (or any parent up to mount point /appl)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
答案1
得分: 3
String Interpolation not happening:
这一行使用单引号括起的字符串。单引号字符串不会插值Perl变量 - 在这种情况下,$url
,$projectcode
等不会被插值。
system( 'git clone ssh://$url/$projectcode/$reponame.git $reponame' );
你需要使用双引号使其生效。
system( "git clone ssh://$url/$projectcode/$reponame.git $reponame" );
Incorrect use of chdir
:
有几处地方调用了chdir
并带有参数。
chdir "cd /export/appl/bbdev/scripts/files/";
...
chdir "cd $reponame";
这两者都会悄悄失败(如果你检查返回代码,就会显而易见)。当给chdir
函数传递参数时,它必须是一个目录的名称。你正在传递带有"cd
"`前缀的字符串。这些是不存在的目录。
你需要这样:
chdir "/export/appl/bbdev/scripts/files/"
or die "Cannot chdir: $!\n";
...
chdir $reponame
or die "Cannot chdir: $!\n";
在没有任何参数的情况下使用chdir
表示“进入我的主目录”(请参阅chdir以获取更多详情)。你根本不需要这些行。
英文:
A few issue immediately jump out
String Interpolation not happening
This line uses a string enclosed in single quotes. Single quoted string do not interpolate Perl variables -- $url
, $projectcode
, etc in this case.
system( 'git clone ssh://$url/$projectcode/$reponame.git $reponame' );
You need to use double quotes to make that happen
system( "git clone ssh://$url/$projectcode/$reponame.git $reponame" );
Incorrect use of chdir
There are a couple of places where chdir
is called with a parameter.
chdir "cd /export/appl/bbdev/scripts/files/";
...
chdir "cd $reponame";
Both are failing silently (if you checked the return code it would be apparent). When the chdir
function is given a parameter, it must be the name of a directory. You are passing strings with a "cd
" prefix in both instances. These are non-existent directories.
You want this this
chdir "/export/appl/bbdev/scripts/files/"
or die "Cannot chdir: $!\n";
...
chdir $reponame
or die "Cannot chdir: $!\n";
The use of chdir
without any parameter means "go to my home directory" (See chdir for more details). You don't need these lines at all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论