ค้นหาเว็บไซต์

ค้นหาที่เก็บ Git ด้วย PowerShell


เนื่องจากเป็นส่วนหนึ่งของกระบวนการปรับปรุงอย่างต่อเนื่องของฉันในปีนี้ ฉันจึงเริ่มใช้ Git มากขึ้น เมื่อวานฉันโพสต์บทความด้วยสคริปต์ PowerShell ของฉันเพื่อสร้างโฟลเดอร์โครงการใหม่ซึ่งรวมถึงการสร้างที่เก็บ Git ความท้าทายของฉันคือฉันจำไม่ได้เสมอไปว่าฉันได้ตั้งค่าอะไรกับ Git และสิ่งที่ฉันไม่มี ดังนั้นฉันจึงรวบรวมฟังก์ชัน PowerShell เล็กๆ น้อยๆ เพื่อระบุโฟลเดอร์ที่มีที่เก็บ Git

ฟังก์ชัน Find-GitRepository ใช้เส้นทางระดับบนสุดเป็นพารามิเตอร์ จากนั้นฟังก์ชันจะค้นหาโฟลเดอร์ย่อยทั้งหมดเพื่อค้นหาไดเร็กทอรีที่ซ่อนอยู่ซึ่งเรียกว่า .git สำหรับแต่ละไดเร็กทอรี ฟังก์ชันจะสร้างออบเจ็กต์แบบกำหนดเองที่มีเส้นทางและรายละเอียดเกี่ยวกับที่เก็บ Git ในการรับค่าเหล่านั้น คำสั่งจะข้ามไปยังแต่ละโฟลเดอร์ที่ฉันแยกวิเคราะห์ผลลัพธ์ของ สาขา git และ บันทึก git

อาจมีข้อมูลอีกเล็กน้อยที่ฉันสามารถดึงมาจาก Git ได้ แต่สำหรับตอนนี้ดูเหมือนว่าจะเพียงพอสำหรับฉัน คุณสามารถค้นหาฟังก์ชันเป็นส่วนสำคัญได้ในที่เก็บ Github ของฉัน

Find-GitRepository.ps1:


Function Find-GitRepository {

<#
.SYNOPSIS
Find Git repositories
.DESCRIPTION
Use this command to find Git repositories in the specified folder. It is assumed that you have the Git command line tools already installed.
.PARAMETER Path
The top level path to search.
.EXAMPLE
PS C:\> Find-GitRepository -path D:\Projects

Repository             Branch LastAuthor     LastLog             
----------             ------ ----------     -------             
D:\Projects\Excalibur  master jdhitsolutions 4/17/2016 9:59:02 AM
D:\Projects\PiedPiper  bug    jdhitsolutions 5/16/2016 1:15:03 PM
D:\Projects\PSMagic    master jdhitsolutions 5/11/2016 1:27:35 PM
D:\Projects\ProjectX   dev    jdhitsolutions 4/12/2016 4:49:30 PM

.NOTES
NAME        :  Find-GitRepository
VERSION     :  1.0.0   
LAST UPDATED:  5/17/2016
AUTHOR      :  Jeff Hicks (@JeffHicks)

Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
.LINK
https://gist.github.com/jdhitsolutions/ba3295167ccc997b7714e1f2a2777405
.INPUTS
[string]
.OUTPUTS
[pscustomobject]
#>

[cmdletbinding()]
Param(
[Parameter(
Position = 0,
HelpMessage = "The top level path to search"
)]
[ValidateScript({
if (Test-Path $_) {
   $True
}
else {
   Throw "Cannot validate path $_"
}
})]    
[string]$Path = "."
)

Write-Verbose "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"
Write-Verbose "[PROCESS] Searching $(Convert-Path -path $path) for Git repositories"

dir -path $Path -Hidden -filter .git -Recurse | 
select @{Name="Repository";Expression={Convert-Path $_.PSParentPath}},
@{Name ="Branch";Expression = {
 #save current location
 Push-Location
 #change location to the repository
 Set-Location -Path (Convert-Path -path ($_.psparentPath))
 #get current branch with out the leading asterisk
 (git branch).where({$_ -match "\*"}).Substring(2)
 
}},
@{Name="LastAuthor";Expression = { git log --date=local --format=%an -1 }},
 @{Name="LastLog";Expression = {
  (git log --date=iso --format=%ad -1 ) -as [datetime]
 #change back to original location
 Pop-Location
 }}

Write-Verbose "[END    ] Ending: $($MyInvocation.Mycommand)"

} #end function

เช่นเดียวกับสคริปต์ก่อนหน้าของฉัน ฉันคิดว่าคุณมีเครื่องมือบรรทัดคำสั่ง git อยู่ในเส้นทางของคุณ หากคุณใช้ Git ฉันหวังว่าคุณจะลองใช้และแจ้งให้เราทราบว่าคุณคิดอย่างไร

บทความที่เกี่ยวข้อง