contentsync 1.0.1
dotnet tool install --global contentsync --version 1.0.1
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local contentsync --version 1.0.1
#tool dotnet:?package=contentsync&version=1.0.1
nuke :add-package contentsync --version 1.0.1
ContentSync
基于文件内容(而非时间戳)进行目录复制/同步/镜像的工具,以避免触及相同的文件。
下载
https://github.com/KirillOsenkov/ContentSync/releases/latest/download/ContentSync.exe
也可在 Chocolatey 上找到:https://chocolatey.org/packages/ContentSync/
cinst ContentSync
用法
Usage: ContentSync.exe <source> <destination> [<pattern>] [-c] [-u] [-d]
[-dc] [-ds]
[-whatif] [-q]
[-h]
Copy/mirror/sync the destination folder to look exactly like the source.
Only modifies the destination folder, never touches the source folder.
Copies missing files from source to destination. Overwrites files in
destination that have different contents than in source. Deletes files from
destination that are not in source.
Doesn't take into account file and date timestamps, works off content only.
-c Copy files from source that don't exist in destination (left-only).
-u Update files that have changed between source and destination. This
only overwrites the destination file if the contents are different.
-d Delete right-only files (that are in destination but not in
source).
-ds Delete same (identical) files from destination (that exist in
source and destination and have identical contents).
-dc Delete changed files from destination (can't be used with -u).
-whatif Print what would have been done (without changing anything).
-q Quiet mode. Do not output anything to the console.
-h Display this help and exit.
Default is: -c -u -d (and if the pattern is not specified, it also syncs
empty directories (creates empty directories that are in the source and
deletes empty directories that are in the destination).
Explicit mode: If any of -c -u -d -ds or -dc are specified explicitly,
all the defaults for other arguments are reset to false. For instance
if you specify -u then -c and -d default to false (and you have to
specify them explicitly). But if no arguments are specified, -c -u -d
default to true.
You can combine -c and -u to -cu, -c and -d to -cd, -u and -d to -ud
and -c -u -d to -cud.
Common usage examples:
-c to copy files missing from the destination (useful when restarting a previous failed copy operation)
-cu -whatif to see which files have changed or are new
-ds to delete duplicate (identical files) from destination
-whatif to diff directories
重要! 自行承担风险。
概述
假设您需要同步两个大型文件夹“源”和“目标”的内容。通常情况下,使用 robocopy *.* Source Destination /MIR
就可以完成任务。然而,即使文件的字节内容没有改变,但时间戳改变了,robocopy 也会复制文件并将目标的时间戳更改为与源匹配。
这是安全且预期的行为,然而有许多工具只根据时间戳追踪文件修改。例如,如果文件的时间戳有所改变,MSBuild 将触发所有依赖文件的重构,即使实际文件内容根本没有变化。这称为过度构建。一个可扩展的构建系统应该检测到文件内容没有变化,并在此情况下避免进行任何工作。
或者,假设您需要使用 MSDeploy 将数十万个文件上传到 Azure。如果这些文件上的时间戳有所改变,即使实际内容相同,MSDeploy 也会上传这些文件。
总的来说,如果您根据时间戳决定一个文件是否已被修改,您肯定会在本可以避免的不必要工作中安排工作。
此工具根据文件内容同步目录,并完全忽略时间戳。
工作原理
它相当简单。首先,它比较源和目标目录,并构建一个仅存在于左侧、仅存在于右侧、在左右两侧之间发生更改以及完全相同的文件的扁平列表
- (仅存在于左侧),
- (仅存在于右侧),
- (在左右两侧之间发生更改),
- (完全相同,具有相同的字节)。
该算法是非递归的(我通过调用 Directory.GetFiles()
隐藏递归,以在左侧和右侧建立初始的扁平文件列表)。
此工具还会同步空文件夹,尽管这有点像附加功能。由于我只为文件创建了清单,所以我不知道左右两侧的空目录信息。因此(是啊,真糟糕)我又遍历了左右两侧,这次收集了左侧和右侧的所有文件夹,然后将它们进行比较。
最后,在我准备好了差异后,我开始第二个阶段,这实际上会对您的文件系统进行操作。
- 复制所有仅左侧文件
- 复制所有已更改的文件,
- 删除所有仅右侧文件。然后我
- 创建所有只存在于左侧的空目录,
- 删除所有仅存在于右侧的空目录。
查看测试
我利用机会以声明式、数据驱动的风格编写了单元测试,其中没有显式的arrange-act-assert。相反,Act和Assert阶段都是隐式的(测试是自我验证的)。您只需要指定左右文件夹的初始状态,然后测试就会完成剩下的工作(它执行动作,然后比较左右文件夹以确保它们相同)。
更多信息请参阅此处
https://github.com/KirillOsenkov/ContentSync/blob/master/src/ContentSync.Tests/Tests.cs https://github.com/KirillOsenkov/ContentSync/blob/master/src/ContentSync.Tests/Folder.cs
产品 | 版本 兼容和附加计算的目标框架版本。 |
---|---|
.NET | net6.0兼容。 net6.0-android已计算。 net6.0-ios已计算。 net6.0-maccatalyst已计算。 net6.0-macos已计算。 net6.0-tvos已计算。 net6.0-windows已计算。 net7.0已计算。 net7.0-android已计算。 net7.0-ios已计算。 net7.0-maccatalyst已计算。 net7.0-macos已计算。 net7.0-tvos已计算。 net7.0-windows已计算。 net8.0已计算。 net8.0-android已计算。 net8.0-browser已计算。 net8.0-ios已计算。 net8.0-maccatalyst已计算。 net8.0-macos已计算。 net8.0-tvos已计算。 net8.0-windows已计算。 |
此包无依赖项。