我想编写一个 POSIX shell (
/bin/sh
),它将输入分支名称。这些分支是发布分支,其格式为 release/yymmDD-HHMMSS
,例如 release/240321-170138
、/release/240405-095914
。 A git branch -A | grep release
为我找到了所有这些:
origin/release/230823-184353
origin/release/230825-154255
origin/release/230829-112434
origin/release/230905-101013
origin/release/230913-114212
origin/release/230915-105323
origin/release/230920-115230
origin/release/230927-115028
origin/release/230927-125843
origin/release/231016-145729
origin/release/231019-110314
origin/release/231030-150735
origin/release/231102-125814
origin/release/231109-114532
origin/release/231122-120112
origin/release/231123-140553
origin/release/231204-143206
origin/release/231213-153646
origin/release/231229-123237
origin/release/240112-160116
origin/release/240123-093125
origin/release/240201-171058
origin/release/240209-140214
origin/release/240216-121012
origin/release/240222-134817
origin/release/240307-115055
origin/release/240320-111003
origin/release/240321-170138
origin/release/240405-095914
现在假设我的脚本已被赋予分支名称
release/240216-121012
。我想以列表或数组的形式(不确定 POSIX shell 中正确的术语是什么)接收按时间顺序跟随此分支名称的所有分支,在本例中这也意味着按字典顺序,至少到 2099 年:
origin/release/240222-134817
origin/release/240307-115055
origin/release/240320-111003
origin/release/240321-170138
origin/release/240405-095914
什么是快速而优雅的方法来做到这一点?我目前设想将所有这些结果放入一个数组中,然后从末尾向后循环,直到遇到我想要的分支。也许带有
awk
或 sed
和 xargs
的东西可以在这里解决问题?
一种方法,使用
sort
确保正确的顺序,使用 sed
标记入口点,使用 awk
打印标记后的行(此处为 <=
)。
% set="release/240216-121012"
% sort -t / -Vk 3 files |
sed "s|${set}|PP<=|" |
awk 'set==1{print} /PP<=/{set=1}'
origin/release/240222-134817
origin/release/240307-115055
origin/release/240320-111003
origin/release/240321-170138
origin/release/240405-095914
更改顺序以确保排序按预期进行
% cat files
origin/release/231204-143206
origin/release/231213-153646
origin/release/231229-123237
origin/release/240112-160116
origin/release/240123-093125
origin/release/240201-171058
origin/release/240209-140214
origin/release/240216-121012
origin/release/240222-134817
origin/release/240307-115055
origin/release/240320-111003
origin/release/240321-170138
origin/release/240405-095914
origin/release/230823-184353
origin/release/230825-154255
origin/release/230829-112434
origin/release/230905-101013
origin/release/230913-114212
origin/release/230915-105323
origin/release/230920-115230
origin/release/230927-115028
origin/release/230927-125843
origin/release/231016-145729
origin/release/231019-110314
origin/release/231030-150735
origin/release/231102-125814
origin/release/231109-114532
origin/release/231122-120112
origin/release/231123-140553