Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit 103ff58

Browse files
authored
Refactor revision.tag to have filter, order, select logic (#25)
revision.tag key is now a dict with sub keys filter, order, select with more advance and complex logic that can be used to extract out exactly the data and select exactly what you want from the list of tags in a git repo
1 parent bca2353 commit 103ff58

8 files changed

Lines changed: 721 additions & 24 deletions

File tree

docs/revision-branch.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Revision: branch
2+
3+
If you want to specify the cloning and updating of a git repo based on a git branch you can define it with the following syntax.
4+
5+
```yaml
6+
repos:
7+
pykwalify:
8+
clone-url: [email protected]:Grokzen/pykwalify.git
9+
revision:
10+
branch: master
11+
```
12+
13+
This will clone the git repo and for each `sgit update` you make after the initial clone, it will do a git pull on the given branch to move your clone to the latest commit on this branch.
14+
15+
In the case you change the branch to a new one, it will fetch the latest commits for that other branch and do a git checkout of it locally.
16+
17+
18+
## CLI
19+
20+
There is a subcommand `sgit repo set` that is used to update the branch for a given repo.
21+
22+
Example syntax
23+
24+
```
25+
# Update to branch unstable
26+
sgit repo set pykwalify branch unstable
27+
28+
# Update cloned git repo to this new branch
29+
sgit update
30+
```
31+
32+
In the case you have defined a branch that do not exists on the `origin remote` then you will get an error when running `sgit update`. No checks is done at the time you set your branch.

docs/revision-tag.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Revision: tag
2+
3+
If you want to use the more specialized and advanced option for parsing and selecting a specific tag from a git repo then you should use the tag revision option.
4+
5+
This is the following syntax and extra options. More detailed examples is further down in this document.
6+
7+
```yaml
8+
repos:
9+
pykwalify:
10+
clone-url: [email protected]:Grokzen/pykwalify.git
11+
revision:
12+
tag:
13+
# One regex string or a list of regex strings is supported here
14+
# Filter is the first step and the purpose is two fold where one
15+
# is that you want to filter out a specific set of tags based on a pattern matching.
16+
# The second feature is that if you make a regex match group you can extract out
17+
# any value from within the string in case you need to clean away unwanted data.
18+
# A common case is that semver tags is named v1.0.0 where we only want 1.0.0 to perform
19+
# a semver comparison against. For a good example where this is very usefull please check
20+
# the azure-python-sdk git mono repo.
21+
# If filter key is not present or is an empty string or empty list all values
22+
# will be preserved in the output.
23+
filter: "my-sdk-[0-9].[0-9].[0-9]"
24+
25+
# Or in list form with multiple values. With multiple values, any value that wants to be preserved must match
26+
# at least one of them, but not all of them. This is considred a whitelist & extract operation.
27+
filter:
28+
- "([0-9].[0-9].[0-9])"
29+
- "v([0-9].[0-9].[0-9])"
30+
31+
# Second step is to order the output from the filter step. In most cases the default semver
32+
# ordering is what you want by default. How to think about it is that if you order something
33+
# you will order the "top" or latest item to the right and the lower items to the
34+
# left or begining of a list.
35+
# If you order ["1.0.0", "1.1.0", "0.9.0"] it will semver sort to ["0.9.0", "1.0.0", "1.1.0"]
36+
# This means that if you later on in the selection stage choose "last" or "first" special keywords,
37+
# you will get the first or last item in the output list from this step.
38+
# If you order it by time, for tags it will look only at the timestamp of when the tag
39+
# was created and ignores the semver version or the content of the tag string.
40+
# Alphabetical ordering will simply sort all items based on their string content only.
41+
# Order defaults to semver
42+
order: "semver|time|alphabetical"
43+
44+
# Last step is to select one item out from the output from the order step.
45+
# Two special keywords exists, "first" and "last". They will pick the first or last item from the list
46+
# of items from the order step. In the most common case for simple git repos, you want to select the last
47+
# tag from any tag in the git repo as they only make tags on their master branch and only sequential version increases.
48+
# This will not work in cases like big shared mono repos with tags for different components where if you do not
49+
# filter out what you want properly, you will get the latest tag for a random component which might be wrong for
50+
# your needs
51+
# By default the selection method will be semver comparison defined by PEP440. This mean you can do selections
52+
# with >, <, >=, <=, == etc to select out the version you want. Note here that if your selection matches multiple
53+
# versions, it will take the last item in the list by default.
54+
select: ">=1.0.0"
55+
56+
# Select supports child arguments in the following format. This makes it possible to change some of the parameters
57+
# in the selection method to be either SEMVER or EXACT. EXACT means that you make a plain string comparison
58+
# between what you specify in the "value" field and what you have in your input list. This is equal to
59+
# "if str_a == str_b" and the first match will be the returned item. If you have no match it will fail out
60+
# as the selection you want do not exists. EXACT option should be chosen if you care less about semver and want
61+
# to ignore filters and order and do more plain string selection.
62+
select:
63+
value: "abc-1.0.0"
64+
method: "exact"
65+
```
66+
67+
68+
## Examples
69+
70+
Basic example. This example will not filter out any items, it will order them by semver and the selection want the latest tag based on semver comparison. This will result in `1.0.0` as the output when doing `sgit update`
71+
72+
```yaml
73+
# .sgit.yml
74+
repos:
75+
pykwalify:
76+
clone-url: [email protected]:Grokzen/pykwalify.git
77+
revision:
78+
tag:
79+
select: last
80+
81+
# Tags list
82+
1.0.0
83+
0.9.0
84+
0.8.0
85+
86+
# Selected tag
87+
1.0.0
88+
```
89+
90+
This example will ignore the semver and order all tags by time and pick the last one. Note this example is not super realistic as the ordering based on the semver looks wrong. But in this case we are working with git-flow model and we have support branches where old minor version tags can be created after new:er releases have been done. Time ordering is more suited for single release branch repos and not git-flow model git repos.
91+
92+
```yaml
93+
# .sgit.yml
94+
repos:
95+
pykwalify:
96+
clone-url: [email protected]:Grokzen/pykwalify.git
97+
revision:
98+
tag:
99+
order: time
100+
select: last
101+
102+
# Tags list
103+
1.0.0 (created 2021-01-01)
104+
0.8.1 (created 2022-12-01)
105+
0.8.0 (created 2020-01-01)
106+
107+
# Selected tag
108+
0.8.1
109+
```
110+
111+
In this example we will filter out v1.0.0 as that is an unwanted tag format and we do not desire in our
112+
parsing for the latest tag. It will default to semver comparison and select the latest version 0.9.0.
113+
114+
```yaml
115+
# .sgit.yml
116+
repos:
117+
pykwalify:
118+
clone-url: [email protected]:Grokzen/pykwalify.git
119+
revision:
120+
tag:
121+
# Filter out unwanted tags based on regex match and grouping.
122+
# This feature works as a whitelist for what tags we want to keep
123+
# Multiple list values is supported
124+
filter: "([0-9].[0-9].[0-9])"
125+
select: "last"
126+
127+
# Tags list
128+
v1.0.0
129+
0.9.0
130+
0.8.0
131+
132+
# Selected tag
133+
0.9.0
134+
```
135+
136+
In this example we want to have the latest semver version. It will extract out the semver version from all tags based on the regex and then do a semver comparison on the result list of items.
137+
138+
```yaml
139+
# .sgit.yml
140+
repos:
141+
pykwalify:
142+
clone-url: [email protected]:Grokzen/pykwalify.git
143+
revision:
144+
tag:
145+
# Filter out the "v" from some tags, and keep the regular semver tags
146+
filter:
147+
- "[0-9].[0-9].[0-9]"
148+
- "v([0-9].[0-9].[0-9])"
149+
select: "last"
150+
151+
# Tags list
152+
v1.0.0
153+
0.9.0
154+
v0.8.0
155+
156+
# Selected tag
157+
1.0.0
158+
```
159+
160+
In this example we can limit the version we want to select to a older tag based on semver comparison.
161+
162+
```yaml
163+
# .sgit.yml
164+
repos:
165+
pykwalify:
166+
clone-url: [email protected]:Grokzen/pykwalify.git
167+
revision:
168+
tag:
169+
select: "<1.0.0"
170+
171+
# Tags list
172+
1.0.0
173+
0.9.0
174+
0.8.0
175+
176+
# Selected tag
177+
0.9.0
178+
```

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"docopt>=0.6.2",
2727
"ruamel.yaml>=0.16.0",
2828
"gitpython>=3.1.0",
29+
"packaging>=21.3",
2930
],
3031
python_requires=">=3.6",
3132
extras_require={
@@ -36,7 +37,12 @@
3637
],
3738
"dev": [
3839
"pylint",
40+
"ptpython",
41+
"ptpdb",
3942
],
43+
"validation": [
44+
"pykwalify",
45+
]
4046
},
4147
classifiers=[
4248
# "Development Status :: 1 - Planning",

sgit/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
3+
DEFAULT_REPO_CONTENT = "repos: { }\n"
4+
5+
__all__ = [
6+
"DEFAULT_REPO_CONTENT",
7+
]

0 commit comments

Comments
 (0)