[英]Powershell script to call git init in all subdirectories
I want to init repositories in all subdirectories in a given directory. 我想在给定目录中的所有子目录中初始化存储库。 Since there are about 200 of them (legacy projects), I want to automate it with Powershell. 由于其中大约有200个(遗留项目),我想用Powershell自动化它。 So far I have: 到目前为止,我有:
$items = Get-ChildItem -Path "."
foreach ($item in $items)
{
if ($item.Attributes -eq "Directory")
{
$git = "C:\Program Files (x86)\Git\cmd\git"
& $git "init"
}
}
Obviously I am missing a line where I make use of each subdirectory in the loop. 显然我错过了一个我在循环中使用每个子目录的行。 In its current form, I believe all this does it call "git init" over and over in the same directory ("."), so what is the way to call git init from inside of each of the subdirectories? 在目前的形式中,我相信所有这一切都在同一目录(“。”)中反复调用“git init”,那么从每个子目录中调用git init的方法是什么? Is there a way to temporarily "cd" into each subdirectory found? 有没有办法暂时“cd”到找到的每个子目录?
I tried something naive like 我尝试过天真的样子
& $item.Name + "\git init"
But it doesn't like that, for obvious reasons. 但由于显而易见的原因,它不喜欢这样。
The cmdlets you are looking for are Push-Location
and Pop-Location
. 您正在寻找的cmdlet是Push-Location
和Pop-Location
。 The former will change the current working directory to whatever argument you pass to it. 前者会将当前工作目录更改为您传递给它的任何参数。 The old location will be stored on a stack. 旧位置将存储在堆栈中。 Once you have changed the directory using Push-Location
, you can execute commands locally as if you manually cd'd into them. 使用Push-Location
更改目录后,您可以在本地执行命令,就像手动插入命令一样。 Afterwards, call Pop-Location
to clear the stack and go back to the original directory. 然后,调用Pop-Location
清除堆栈并返回原始目录。
Btw. 顺便说一句。 note that checking Attributes
equality for "Directory"
will not necessarily be good enough to identify directories. 请注意,检查"Directory"
Attributes
相等性不一定足以识别目录。 While you can be sure that you will only find directories using that, there may be other directories which have other attribute flags set as well. 虽然您可以确定只能使用它找到目录,但可能还有其他目录也设置了其他属性标志。 See also the FileAttributes enumeration. 另请参见FileAttributes枚举。
A better way to check for directories is to check the PSIsContainer
property. 检查目录的更好方法是检查PSIsContainer
属性。 It will simply be true for directories, and otherwise false. 它对于目录来说就是真的,否则就是假的。
Using PowerShell's piping, you can also write this all in a single line: 使用PowerShell的管道,您也可以将它们全部写在一行中:
Get-ChildItem | ? { $_.PSIsContainer } | % { Push-Location $_.FullName; git init; Pop-Location }
This will first get all items in the current directory and push them into the pipe. 这将首先获取当前目录中的所有项目并将它们推入管道。 Then they will be filtered for directories ( ?
is filter), and then commands will be executed for each item that is left in the pipe ( %
is for each). 然后将对目录进行过滤( ?
是过滤器),然后对管道中剩余的每个项目执行命令( %
为每个项目)。 So you call Push-Location
to get to the directory's location, call git init
there, and then go back using Pop-Location
. 所以你调用Push-Location
来到目录的位置,在那里调用git init
,然后使用Pop-Location
。
Note: If you are using PowerShell 3, you can also use Get-ChildItem -Directory
to filter for directories right away. 注意:如果您使用的是PowerShell 3,还可以使用Get-ChildItem -Directory
立即过滤目录。
All you have to do is call this command: 您所要做的就是调用此命令:
gci c:\parent\folder | ? { $_.PSIsContainer; } | % { cd $_.FullName; git init; };
Get-ChildItem
, which gets a list of subdirectories and files of the "parent folder." 第一位是Get-ChildItem
的别名,它获取“父文件夹”的子目录和文件列表。 It passes those items onto the next command via the pipe
character |
它通过传递这些项目到下一个命令pipe
字符|
PSIsContainer
bit filters the list of "child items" for folders only, so as to exclude files. PSIsContainer
位仅过滤文件夹的“子项”列表,以便排除文件。 It passes these folders onto the next command via the pipe |
它通过管道传送这些文件夹到下一个命令|
ForEach-Object
, which performs a series of tasks for each object in the PowerShell pipeline. 最后一位是ForEach-Object
的别名,它为PowerShell管道中的每个对象执行一系列任务。
git init
调用git init
如果您不想使用推拉位置,则可以使用更简单的poke脚本版本
Get-ChildItem | ? { $_.PSIsContainer } | % { git -C $_.FullName pull }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.