- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图找到一种方法来确定 git 存储库中的文件是否由 git-lfs 正确管理。
到目前为止,我发现比较 git lfs ls-files
和 git ls-files ':(attr:filter=lfs)'
的结果似乎给出了我想要什么。
Add-Type -AssemblyName 'System.Linq';
[string[]] $actualLfsFilePaths = git lfs ls-files | `
ForEach-Object `
{
#'12345678 * my.dll' - not all lfs versions support -n flag, so it is better to be conservative
$_.Split(' ', 3)[2]
};
[string[]] $shouldBeUnderLfsFilePaths = git ls-files ':(attr:filter=lfs)';
$filePathsNotUnderLfs = [System.Linq.Enumerable]::ToArray(
[System.Linq.Enumerable]::Except($shouldBeUnderLfsFilePaths, $actualLfsFilePaths)
);
$count = $filePathsNotUnderLfs.Count;
Write-Host "There are $($count) lfs files that are not under lfs";
foreach ($filePathNotUnderLfs in $filePathsNotUnderLfs)
{
Write-Host "`t'$filePathNotUnderLfs'";
}
if ($count -gt 0)
{
Write-Error "There are $count lfs files that are not under lfs";
return 1;
}
return 0;
我仍然不确定这是一种可靠的方法。那么,这是一种可靠/正确的方法吗?或者也许有其他可用的替代方法?
P.S.:我想要实现的是创建一个 pull 请求验证构建(用于 TFS 存储库),它将检测 lfs 违规(提交的文件未添加到 lfs),这些违规经常因一个或另一个原因发生并导致不必要的最好的情况是警告和 repo 膨胀,最坏的情况是 major nuisances。
编辑:嗯,发现当前代码有一些问题:
但它们相对容易修复并且不会改变问题的 Gist 。
最佳答案
我还没有设法确定它是否是一种真正可靠的方法,但到目前为止我的测试似乎证实了这一点。
我们最终使用以下脚本作为构建管道的一部分:
#See https://stackoverflow.com/questions/54804333/is-comparing-git-lfs-ls-files-with-git-ls-files-attrfilter-lfs-a-reliable
[CmdletBinding()]
[OutputType([int])]
Param (
[Parameter(Mandatory = $true)]
[string]
$RepositoryLocalPath
)
$ErrorActionPreference = "Stop";
function Assert-LastExitCodeIs0
{
if ($LASTEXITCODE -ne 0)
{
throw (New-Object System.InvalidOpertationException("LASTEXITCODE is $LASTEXITCODE"));
}
}
function Get-StringFromOctal
{
[CmdLetBinding()]
[OutputType([string])]
Param (
[Parameter(Mandatory = $true)]
[string]
$String
)
if ([String]::IsNullOrEmpty($String))
{
return $String;
}
if (($String[0] -ne '"') -or
($String[$String.Length - 1] -ne '"'))
{
return $String;
}
if ($String.Length -lt 2)
{
return $String;
}
$String = $String.Substring(1, $String.Length -2);
[regex] $regex = '(\\[0-9]+)+';
$encoding = [System.Text.Encoding]::UTF8;
return $regex.Replace(
$String,
{
Param ($Capture)
$bytes = $Capture.Groups[1].Captures |
ForEach-Object { [System.Convert]::ToByte($_.Value.Substring(1), 8) };
return $encoding.GetString(@($bytes));
});
}
function Normalize-GitLsFilesPath
{
[CmdLetBinding()]
[OutputType([string])]
Param (
[Parameter(Mandatory = $true)]
[string]
$Path
)
# Normalize octets paths
# Technically we could try ls-files -z, but there is no such option for lfs ls-files.
# Or we could try to set core.quotePath to 0, but it brings a slew of problems with encodings and etc.
# And by doing so we would change git config settings, that is not very desirable, even if we will always(?) succeed in reverting it.
# So it is seems simpler just to parse octal paths.
return Get-StringFromOctal -String ($Path.Trim());
}
function Is-FileEmpty
{
[CmdLetBinding()]
[OutputType([bool])]
Param (
[Parameter(Mandatory = $true)]
[string]
$Path
)
return ((Get-Item -LiteralPath $Path).Length -eq 0);
}
function Is-NotEmptyLfsFile
{
[CmdLetBinding()]
[OutputType([bool])]
Param (
[Parameter(Mandatory = $true)]
[string]
$Path
)
# Empty files will be empty files whether they are under lfs or not - https://github.com/git-lfs/git-lfs/issues/2863
# But they seem not to get the lfs attributes, so we have no other choice but to filter them.
return !(Is-FileEmpty -Path $Path);
}
function Get-ActualLfsFilePaths
{
[CmdletBinding()]
[OutputType([string[]])]
Param()
Write-Host 'Getting actual lfs file paths.';
$lfsLsFilesOutput = git lfs ls-files;
Assert-LastExitCodeIs0;
$actualLfsFilePaths = ($lfsLsFilesOutput | `
ForEach-Object `
{
#'12345678 * my.dll' - not all lfs versions support -n flag, so it is better to be conservative
$lsFilePath = $_.Split(' ', 3)[2];
return Normalize-GitLsFilesPath -Path $lsFilePath;
});
Write-Host "There are $($actualLfsFilePaths.Count) files that are actually under lfs";
return , $actualLfsFilePaths;
}
function Get-ShouldBeLfsFilePaths
{
[CmdletBinding()]
[OutputType([string[]])]
Param()
Write-Host 'Getting should be under lfs file paths.';
[string[]] $shouldBeUnderLfsFilePaths = git ls-files -s ':(attr:filter=lfs)';
Assert-LastExitCodeIs0;
$shouldBeUnderLfsFilePaths = ($shouldBeUnderLfsFilePaths | `
ForEach-Object `
{
#120000 1234567890abcdef 0 mylib.dylib
, $_.Split($null, 4);
} | `
Where `
{
$symlinkBitMaskString = '120000';
$fileChmodString = $_[0];
return ($fileChmodString -ne $symlinkBitMaskString); # Perhaps we should check actual bitmask?
} | `
ForEach-Object `
{
Normalize-GitLsFilesPath -Path $_[3];
});
Write-Host "There are $($shouldBeUnderLfsFilePaths.Count) files that should be under lfs (may include flukes - empty files)";
return , $shouldBeUnderLfsFilePaths;
}
function Get-LfsFilePathsNotUnderLfs
{
[CmdletBinding()]
[OutputType([string[]])]
Param()
[string[]] $actualLfsFilePaths = Get-ActualLfsFilePaths;
[string[]] $shouldBeUnderLfsFilePaths = Get-ShouldBeLfsFilePaths;
Add-Type -AssemblyName 'System.Linq';
Write-Host 'Comparing actual lfs and should be under lfs files.'
return , [System.Linq.Enumerable]::ToArray(
[System.Linq.Enumerable]::Where(
[System.Linq.Enumerable]::Except($shouldBeUnderLfsFilePaths, $actualLfsFilePaths),
[Func[String, Boolean]] ${function:Is-NotEmptyLfsFile}
)
);
}
function Main-WithRepositoryAsWorkingFolder
{
$filePathsNotUnderLfs = Get-LfsFilePathsNotUnderLfs;
$count = $filePathsNotUnderLfs.Count;
Write-Host "There are $($count) lfs files that are not under lfs";
foreach ($filePathNotUnderLfs in $filePathsNotUnderLfs)
{
Write-Host "`t'$filePathNotUnderLfs'";
}
if ($count -eq 0)
{
return 0;
}
return 1;
}
function Main
{
[CmdletBinding()]
[OutputType([int])]
Param (
[Parameter(Mandatory = $true)]
[string]
$RepositoryLocalPath
)
Write-Host "RepositoryLocalPath = $RepositoryLocalPath";
# It is simpler to set working dir rather than use Process.Start with WorkingDirectory everywhere, and more reliable than Start-Process (that one may in some cases have issues with return exit code).
$originalGetLocation = (Get-Location).Path;
Set-Location -LiteralPath $RepositoryLocalPath;
$originalCurrentDirectory = [System.IO.Directory]::GetCurrentDirectory();
[System.IO.Directory]::SetCurrentDirectory($RepositoryLocalPath);
# Otherwise we won't get proper file paths from git lfs ls-files, as it doesn't use octal encoding
# And using output redirection may lead us to even bigger problems|difficulties.
$originalOutputEncoding = $OutputEncoding;
$OutputEncoding = [System.Text.Encoding]::UTF8;
$originalConsoleOutputEncoding = [Console]::OutputEncoding;
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
$originalConsoleInputEncoding = [Console]::InputEncoding;
[Console]::InputEncoding = [System.Text.Encoding]::UTF8;
try
{
return Main-WithRepositoryAsWorkingFolder;
}
catch
{
Write-Host "$_ $($_.ScriptStackTrace)";
return 2;
}
finally
{
Set-Location -LiteralPath $originalGetLocation;
[System.IO.Directory]::SetCurrentDirectory($originalCurrentDirectory);
$OutputEncoding = $originalOutputEncoding;
[Console]::OutputEncoding = $originalConsoleOutputEncoding;
[Console]::InputEncoding = $originalConsoleInputEncoding;
}
}
exit (Main -RepositoryLocalPath $RepositoryLocalPath);
关于git - 将 git lfs ls-files 与 git ls-files ':(attr:filter=lfs)' 进行比较是一种检测不受 lfs 管理的 lfs 文件的可靠方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804333/
我有一个使用 git-lfs 存储一些大文件的存储库的工作副本。我安装了 git-lfs 二进制文件,但可能没有在工作副本中运行“git lfs install”。当我想在添加 lfs 文件后更新我的
我试图找到一种方法来确定 git 存储库中的文件是否由 git-lfs 正确管理。 到目前为止,我发现比较 git lfs ls-files 和 git ls-files ':(attr:filter
我正在尝试通过以下方式从 git 克隆一个项目: git clone link 并收到了这条消息 remote: Enumerating objects: 24533, done. remote: C
我有一个本地存储库,其中跟踪了一堆 git-lfs 文件。它是使用 git-lfs 版本 2.1.0 创建的。 如果我尝试使用 git-lfs 版本 1.5.6 对该存储库进行结帐,则会收到错误:
我的小组正在与另一个小组合作开展一个项目,我们为合作建立了一个 GitHub 组织。现在协作结束了,我们即将从 GitHub 中删除该组织,我需要为我的团队工作的存储库创建一个本地副本。 我的理解是g
我们在 gitlab 中有一个启用了 git-lfs 的存储库,它工作得很好。它很好地分离了 pdf、电影和其他我们不想进行版本管理的内容。 出现用户错误,有人在未启用 git-lfs 支持的系统上克
我刚刚从 https://github.com/git-lfs/git-lfs/releases 更新了我安装的 Git LFS到最新版本(2.3.0)。但是,当我运行 git lfs version
我刚刚从 https://github.com/git-lfs/git-lfs/releases 更新了我安装的 Git LFS到最新版本(2.3.0)。但是,当我运行 git lfs version
对于 GitHub 上的存储库,GitKraken 显示: This repository requires LFS but you do not have LFS installed. Modify
事情是这样的: 不小心提交了很多本不该提交的文件。 是否执行了 git reset --soft HEAD~2 以返回到事故发生前的提交 修改 gitignore 以忽略文件 再次提交并推送到原点。
在 Bitbucket 服务器上托管了 LFS git 存储库。现在我们需要创建新的 LFS 存储库并使用 LFS 存储将存储库移动到那里。 如何做到这一点? 例如对于普通的 repo 我们可以这样做
fatal: 'lfs' appears to be a git command, but we were not able to execute it. Maybe git-lfs is broke
我尝试了很多命令,但都不起作用: java -jar bfg-1.13.0.jar --convert-to-git-lfs 'lib/**' --no-blob-protection java -j
我有一个相当大的存储库,需要很长时间才能从远程克隆。我决定我可能会从 Git LFS 中受益,所以我去迁移了 repo。但是,在迁移、推送和重新克隆之后,存储库占用的磁盘空间比原始存储库多。 FS 上
我们希望将特定目录从一个 git 存储库移动到另一个。我找到了 these instructions ,直到从远程分支中 pull 出为止,一切顺利;似乎让 git-lfs 跟踪两边的文件会使过程稍微
我们希望将特定目录从一个 git 存储库移动到另一个。我找到了 these instructions ,直到从远程分支中 pull 出为止,一切顺利;似乎让 git-lfs 跟踪两边的文件会使过程稍微
当使用 Git LFS 将大文件推送到 git 存储库时,系统上没有安装 git-lfs 的用户是否可以在不进行任何额外设置的情况下克隆它? 最佳答案 我只是尝试按照您的建议去做。我使用 Git LF
请帮助我理解以下错误消息的含义以及如何遵守其要求。 我想复制一个现有的本地 git 仓库;然后将该副本推送到新创建的远程。 但是我得到以下错误: This repository is configur
故事是这样的,我们在 GitHub 上工作,并且正在使用 GitDesktop 1.0.10。 我们还使用来自 https://github.com/blog/2432-git-lfs-2-3-0-r
这个问题的另一个标题可以是“如何 check out 一个 Git-LFS 管理文件的多个版本?” 我想检查存储在 Git-LFS 中的文件的多个版本。因此,我想在我的工作目录中并排放置此文件的多个版
我是一名优秀的程序员,十分优秀!