Skip to content

Commit

Permalink
Bug fixes and edge case handling
Browse files Browse the repository at this point in the history
Bug fixes and edge case handling
- better handle when 'raw blob' content is not available (due to rename, etc.)
- add token files for additional modification types
- if specified branch is not found locally, check the remote equivalent
- detect if 'current directory' is a git root and prompt to use it if so
  • Loading branch information
JanusMael authored Jul 21, 2023
1 parent 2cf0584 commit 21516bb
Showing 1 changed file with 92 additions and 16 deletions.
108 changes: 92 additions & 16 deletions Git-ArchiveBranchDiffs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,55 @@ class GitBranch {
Write-Fail "branchName should not be null"
}
$this.BranchName = $branchName
$this.CommitHash = [GitTool]::GetCommitHash($branchName)
$this.RemoteName = [GitTool]::GetRemoteName()
$this.ResolveLocalOrRemoteCommit($this.RemoteName, $this.BranchName)
$this.RemoteUrl = [GitTool]::GetRemoteUrl()
$this.CommitDate = [GitTool]::GetCommitDate($branchName)
$this.CommitDate = [GitTool]::GetCommitDate($this.BranchName)
}
[string]$BranchName
[string]$CommitHash
[System.DateTimeOffset]$CommitDate
[string]$RemoteName
[string]$RemoteUrl

hidden [void] ResolveLocalOrRemoteCommit([string]$remoteName, [string]$branchName)
{
$this.CommitHash = [GitTool]::GetCommitHash($branchName)

if([string]::IsNullOrEmpty($this.CommitHash))
{
if($this.IsLocalBranch())
{
[string]$remoteBranch = $remoteName + "/" + $branchName
$this.CommitHash = git rev-parse $remoteBranch
if([string]::Equals($this.CommitHash, $remoteBranch))
{
$this.CommitHash = $null
}
else
{
Write-Warn "Did not find local branch '$branchName' but will use remote branch '$remoteBranch'"
$this.BranchName = $remoteBranch
}
}

if([string]::IsNullOrEmpty($this.CommitHash))
{
Write-Fail branch $this.BranchName not found, defaulting to 'HEAD'
$this.CommitHash = git rev-parse "HEAD"
}
}
}

[bool] IsLocalBranch()
{
if($this.BranchName.StartsWith($this.RemoteName + "/"))
{
return $false
}
return $true
}

[string] GetDirectorySafeName()
{
return $this.BranchName.Replace("\\","_").Replace("/", "_")
Expand Down Expand Up @@ -1125,7 +1165,7 @@ class GitTool {

if([string]::IsNullOrEmpty($commitHash))
{
$commitHash = "HEAD~1"
$commitHash = "HEAD~1" # previous commit
}

Write-Info Diffing $commitHash...
Expand Down Expand Up @@ -1209,13 +1249,29 @@ class GitTool {
}

[string]$remoteUrl = git config --get remote.origin.url
if([string]::IsNullOrEmpty($remoteUrl))
{
return $remoteUrl
}
if([string]::IsNullOrEmpty($remoteUrl))
{
return $remoteUrl
}
return $remoteUrl.Trim()
}

static [string] GetRemoteName()
{
if(-not (Get-Command -CommandType Application git -ErrorAction SilentlyContinue))
{
Write-Fail git not found
return ""
}

[string]$remoteName = git remote
if([string]::IsNullOrEmpty($remoteName))
{
return $remoteName
}
return $remoteName.Trim()
}

static [string] GetCommitHash([string]$branchName)
{
if(-not (Get-Command -CommandType Application git -ErrorAction SilentlyContinue))
Expand All @@ -1230,14 +1286,8 @@ class GitTool {
[string]$commitHash = git rev-parse $branchName
if([string]::Equals($commitHash, $branchName))
{
Write-Fail branch $branchName not found, defaulting to 'HEAD~1'
#branchName needs become HEAD~1
$commitHash = $null
}
if([string]::IsNullOrEmpty($commitHash))
{
$commitHash = git rev-parse "HEAD~1"
}
return $commitHash
}

Expand Down Expand Up @@ -1308,6 +1358,11 @@ class GitTool {
return $false
}

static [bool] IsGitRoot([string]$directoryPath)
{
return [System.IO.Directory]::Exists([System.IO.Path]::Combine($directoryPath, ".git"))
}

static [Void] Clean()
{
if(-not (Get-Command -CommandType Application git -ErrorAction SilentlyContinue))
Expand All @@ -1329,8 +1384,29 @@ Write-Info ""

if([string]::IsNullOrEmpty($repositoryPath))
{
$repositoryPath = Read-Prompt "Enter the path to the root of a `git` repository"
$repositoryPath = [System.IO.Path]::GetFullPath($repositoryPath)
[System.IO.DirectoryInfo]$currentDirectory = [System.IO.DirectoryInfo]::new($(Get-Location))
if([GitTool]::IsGitRoot($currentDirectory.FullName))
{
$useCurrentDirectory = Read-Prompt "Use '$currentDirectory' as the `git` repository root? (Y/N)"

if([string]::Equals($useCurrentDirectory, "Y", [System.StringComparison]::InvariantCultureIgnoreCase) -or
[string]::Equals($useCurrentDirectory, "YES", [System.StringComparison]::InvariantCultureIgnoreCase))
{
$repositoryPath = [System.IO.Path]::GetFullPath($currentDirectory)
}
}

if([string]::IsNullOrEmpty($repositoryPath))
{
$repositoryPath = Read-Prompt "Enter the path to the root of a `git` repository"
$repositoryPath = [System.IO.Path]::GetFullPath($repositoryPath)
}
}

if(-not [GitTool]::IsGitRoot($repositoryPath))
{
Write-Fail "'$repositoryPath' does not appear to be a `git` repository root."
Exit
}

if([string]::IsNullOrEmpty($leftBranch))
Expand Down Expand Up @@ -1364,4 +1440,4 @@ Push-Location -Path $repositoryPath

[GitTool]::ArchiveBranchDiffs($leftBranch, $rightBranch, $outputDirectory, $archiveFileName)

Pop-Location
Pop-Location

0 comments on commit 21516bb

Please sign in to comment.