Errors with git clone in perl script

huangapple go评论128阅读模式
英文:

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 = &quot;git\@devbitbucket.ethos.com:7999&quot;;

print &quot;What is the project asset code? &quot;;
chomp(my $projectcode = &lt;&gt; );
print &quot;$projectcode\n&quot;;

print &quot;What is the repository name? &quot;;
chomp(my $reponame = &lt;&gt; );
print &quot;$reponame\n&quot;;

chdir &quot;cd /export/appl/bbdev/scripts/files/&quot;;
chdir;
print &quot;git clone ssh://$url/$projectcode/$reponame.git\n&quot;;
system( &#39;git clone ssh://$url/$projectcode/$reponame.git $reponame&#39; );
#$reponame =~ s/\..*//s;

chdir &quot;cd $reponame&quot;;
chdir;
system( &#39;cp -p /export/appl/bbdev/scripts/files/* .&#39; );
system( &#39;git status&#39; );
system( &#39;git add -A &amp;&amp; git commit -m &quot;Default Repository Initial Files&quot; &#39;);
system( &#39;git push --all&#39; );
system( &#39;git status&#39; );

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( &#39;git clone ssh://$url/$projectcode/$reponame.git $reponame&#39; );

You need to use double quotes to make that happen

system( &quot;git clone ssh://$url/$projectcode/$reponame.git $reponame&quot; );

Incorrect use of chdir

There are a couple of places where chdir is called with a parameter.

chdir &quot;cd /export/appl/bbdev/scripts/files/&quot;;
...
chdir &quot;cd $reponame&quot;;

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 &quot;/export/appl/bbdev/scripts/files/&quot;
    or die &quot;Cannot chdir: $!\n&quot;;
...
chdir $reponame
    or die &quot;Cannot chdir: $!\n&quot;;

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.

huangapple
  • 本文由 发表于 2023年5月10日 22:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定