Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Version
Manage
Activity
Members
Plan
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Utilities
Version
Commits
aa4874d1
Verified
Commit
aa4874d1
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: add verbose mode to detect git transactions
parent
c6f1adc9
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
commandline.go
+52
-14
52 additions, 14 deletions
commandline.go
devenv.lock
+16
-16
16 additions, 16 deletions
devenv.lock
git.go
+72
-4
72 additions, 4 deletions
git.go
version.go
+10
-10
10 additions, 10 deletions
version.go
with
150 additions
and
44 deletions
commandline.go
+
52
−
14
View file @
aa4874d1
...
...
@@ -16,8 +16,9 @@ var gitRepo *git.Repository
type
commandLineOptions
struct
{
Path
string
`short:"p" long:"path" description:"path to the file"`
Selector
string
`short:"s" long:"selector" description:"selector of data"`
Null
bool
`short:"0" long:"null" description:"terminate with null byte"`
Git
bool
`short:"g" long:"git" description:"read from git"`
Verbose
bool
`short:"v" long:"verbose" description:"verbose output"`
Null
bool
`short:"0" long:"null" description:"terminate with null byte"`
Major
struct
{
}
`command:"major" description:"increase the major version"`
...
...
@@ -66,6 +67,9 @@ func increaseMajor() (string, error) {
return
next
.
String
(),
nil
}
var
verbosity
=
false
var
nullTerminated
=
false
func
executeCommand
()
{
arguments
=
new
(
commandLineOptions
)
...
...
@@ -75,7 +79,7 @@ func executeCommand() {
_
,
err
=
p
.
Parse
()
if
err
!=
nil
{
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
activeCommand
:=
p
.
Command
.
Active
...
...
@@ -87,28 +91,55 @@ func executeCommand() {
var
newVersion
string
command
:=
activeCommand
.
Name
if
arguments
.
Verbose
{
verbosity
=
true
}
if
arguments
.
Null
{
nullTerminated
=
true
}
if
arguments
.
Git
||
command
==
"auto"
{
gitRepo
,
err
=
git
.
PlainOpen
(
"."
)
path
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
_
,
err
:=
fmt
.
Fprintf
(
os
.
Stderr
,
"Could not get current working directory: %s
\n
"
,
err
)
if
err
!=
nil
{
fmt
.
Printf
(
"Could not get current working directory: %s
\n
"
,
err
)
}
os
.
Exit
(
1
)
}
if
verbosity
{
fmt
.
Println
(
"git path:"
,
path
)
}
gitRepo
,
err
=
git
.
PlainOpen
(
path
)
if
err
!=
nil
{
_
,
err
:=
fmt
.
Fprintf
(
os
.
Stderr
,
"Error: %s
\n
"
,
err
)
if
err
!=
nil
{
fmt
.
Printf
(
"Error: %s
\n
"
,
err
)
}
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
}
if
command
==
"auto"
{
updateType
,
err
:=
GetCommitType
()
commitType
,
err
:=
GetCommitType
()
if
arguments
.
Verbose
{
fmt
.
Printf
(
"commit type: %s
\n
"
,
commitType
)
}
if
err
!=
nil
{
_
,
err
:=
fmt
.
Fprintf
(
os
.
Stderr
,
"Error: %s
\n
"
,
err
)
if
err
!=
nil
{
fmt
.
Printf
(
"Error: %s
\n
"
,
err
)
}
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
switch
update
Type
{
switch
commit
Type
{
case
BreakingCommit
:
command
=
"major"
case
FeatCommit
:
...
...
@@ -133,12 +164,12 @@ func executeCommand() {
fmt
.
Printf
(
"Error: %s
\n
"
,
err
)
}
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
if
arguments
.
Null
{
if
nullTerminated
{
// terminate with null byte
fmt
.
Printf
(
"%s"
,
version
.
String
())
fmt
.
Printf
(
"%s
%s
"
,
version
.
String
()
,
string
(
rune
(
0
))
)
}
else
{
fmt
.
Printf
(
"%s
\n
"
,
version
.
String
())
}
...
...
@@ -148,7 +179,7 @@ func executeCommand() {
currentTime
:=
time
.
Now
()
build
=
currentTime
.
Format
(
"20060102150405"
)
if
arguments
.
Null
{
if
nullTerminated
{
build
+=
string
(
rune
(
0
))
}
...
...
@@ -163,7 +194,6 @@ func executeCommand() {
newVersion
,
err
=
increaseMinor
()
case
"patch"
:
newVersion
,
err
=
increasePatch
()
}
if
err
!=
nil
{
...
...
@@ -171,7 +201,11 @@ func executeCommand() {
if
err
!=
nil
{
fmt
.
Printf
(
"Error: %s
\n
"
,
err
)
}
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
if
verbosity
{
fmt
.
Printf
(
"new version: %s
\n
"
,
newVersion
)
}
err
=
writeVersion
(
newVersion
)
...
...
@@ -180,7 +214,11 @@ func executeCommand() {
if
err
!=
nil
{
fmt
.
Printf
(
"Error: %s
\n
"
,
err
)
}
os
.
Exit
(
-
1
)
os
.
Exit
(
1
)
}
if
verbosity
{
fmt
.
Printf
(
"wrote version: %s
\n
"
,
newVersion
)
}
}
This diff is collapsed.
Click to expand it.
devenv.lock
+
16
−
16
View file @
aa4874d1
...
...
@@ -3,11 +3,11 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1689
504341
,
"narHash": "sha256-
btK/nUaxB6HBCiuhnebiCTnohatHElVwWrBOWP+oWpI
=",
"lastModified": 1689
667485
,
"narHash": "sha256-
tLNoMRSPLlW1D4wgNpSIPUUHd6x1GdjAhETLpRTKnfo
=",
"owner": "cachix",
"repo": "devenv",
"rev": "
b9c9d83e89c0405ab02880132a0601911c477250
",
"rev": "
6f8add968bc12bf81d845eb7dc684a0733bb1518
",
"type": "github"
},
"original": {
...
...
@@ -74,11 +74,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1689
503327
,
"narHash": "sha256-
qVwzYLA8oT2oWNDXO0A3bZHOhoPOihIB9T677+Hor1
E=",
"lastModified": 1689
605451
,
"narHash": "sha256-
u2qp2k9V1smCfk6rdUcgMKvBj3G9jVvaPHyeXinjN9
E=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "
f64b9738da8e86195766147e9752c67fccee006c
",
"rev": "
53657afe29748b3e462f1f892287b7e254c26d77
",
"type": "github"
},
"original": {
...
...
@@ -106,11 +106,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1689
503327
,
"narHash": "sha256-
qVwzYLA8oT2oWNDXO0A3bZHOhoPOihIB9T677+Hor1
E=",
"lastModified": 1689
605451
,
"narHash": "sha256-
u2qp2k9V1smCfk6rdUcgMKvBj3G9jVvaPHyeXinjN9
E=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "
f64b9738da8e86195766147e9752c67fccee006c
",
"rev": "
53657afe29748b3e462f1f892287b7e254c26d77
",
"type": "github"
},
"original": {
...
...
@@ -130,11 +130,11 @@
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1689
553
10
6
,
"narHash": "sha256-
RFFf6BbpqQB0l1ehAbgri9g9MGZkAY9UdiNotD9fG8Y
=",
"lastModified": 1689
6682
10,
"narHash": "sha256-
XAATwDkaUxH958yXLs1lcEOmU6pSEIkatY3qjqk8X0E
=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "
87589fa438dd6d5b8c7c1c6ab2ad69e4663bb51f
",
"rev": "
eb433bff05b285258be76513add6f6c57b441775
",
"type": "github"
},
"original": {
...
...
@@ -171,11 +171,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 168958
4837
,
"narHash": "sha256-
fthvNvvzQlXBP2is6UanGY06nKmmBOU1m+NJoOtptpA
=",
"lastModified": 168958
5498
,
"narHash": "sha256-
oNgRghsOrPN7W+fcNL22LsetUvJS21v4Hm/Z1Hbcon4
=",
"ref": "refs/heads/master",
"rev": "
226cf5dc731ce42d842466257382e983a0bb2c4c
",
"revCount": 2
5
,
"rev": "
c6f1adc90800afdf3f6a42998fe602228615edae
",
"revCount": 2
7
,
"type": "git",
"url": "https://gitlab.schukai.com/oss/utilities/version.git"
},
...
...
This diff is collapsed.
Click to expand it.
git.go
+
72
−
4
View file @
aa4874d1
...
...
@@ -56,6 +56,19 @@ const (
FixCommit
)
func
(
c
CommitType
)
String
()
string
{
switch
c
{
case
FeatCommit
:
return
"feat"
case
BreakingCommit
:
return
"breaking"
case
FixCommit
:
return
"fix"
default
:
return
"other"
}
}
func
GetCommitType
()
(
CommitType
,
error
)
{
latestTag
,
err
:=
getLatestSemanticTag
()
...
...
@@ -63,16 +76,28 @@ func GetCommitType() (CommitType, error) {
return
OtherCommit
,
err
}
if
verbosity
{
fmt
.
Println
(
"latest found tag:"
,
latestTag
)
}
tagCommit
,
err
:=
getTagCommit
(
latestTag
.
Tag
)
if
err
!=
nil
{
return
OtherCommit
,
err
}
if
verbosity
{
fmt
.
Println
(
"tag commit:"
,
tagCommit
)
}
commitType
,
err
:=
getCommitTypeSinceTag
(
tagCommit
)
if
err
!=
nil
{
return
OtherCommit
,
err
}
if
verbosity
{
fmt
.
Println
(
"commit type:"
,
commitType
)
}
return
commitType
,
nil
}
...
...
@@ -135,9 +160,15 @@ func getSemanticTags() ([]SemanticVersion, error) {
var
tagList
[]
SemanticVersion
err
=
tags
.
ForEach
(
func
(
tag
*
plumbing
.
Reference
)
error
{
tagName
:=
tag
.
Name
()
.
Short
()
if
verbosity
{
fmt
.
Println
(
"found tag: "
,
tagName
)
}
if
versionRegex
.
MatchString
(
tagName
)
{
tagList
=
append
(
tagList
,
ParseSemanticVersion
(
tagName
))
}
return
nil
})
...
...
@@ -149,6 +180,10 @@ func getSemanticTags() ([]SemanticVersion, error) {
return
tagList
[
i
]
.
IsLessThan
(
tagList
[
j
])
})
if
verbosity
{
fmt
.
Println
(
"tag list: "
,
tagList
)
}
return
tagList
,
nil
}
...
...
@@ -164,21 +199,54 @@ func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) {
var
commitType
CommitType
found
:=
false
counter
:=
0
err
=
cIter
.
ForEach
(
func
(
commit
*
object
.
Commit
)
error
{
counter
++
if
commit
.
Hash
==
tagCommit
.
Hash
{
found
=
true
if
verbosity
{
fmt
.
Println
(
"found tag commit after"
,
counter
,
"commits"
)
}
return
storer
.
ErrStop
// stop iteration
}
if
strings
.
HasPrefix
(
commit
.
Message
,
"feat:"
)
{
message
:=
strings
.
TrimSpace
(
commit
.
Message
)
if
strings
.
HasPrefix
(
message
,
"feat"
)
{
commitType
=
FeatCommit
found
=
true
return
storer
.
ErrStop
// stop iteration
}
else
if
strings
.
HasPrefix
(
commit
.
Message
,
"fix:"
)
{
if
verbosity
{
fmt
.
Println
(
"found feat commit after"
,
counter
,
"commits"
)
}
}
else
if
strings
.
HasPrefix
(
message
,
"fix"
)
{
// if we already found a feat or breaking commit, we don't care about fix commits
if
commitType
<
FixCommit
&&
commitType
>
OtherCommit
{
return
nil
}
commitType
=
FixCommit
}
else
if
containsBreakingChangeFooter
(
commit
.
Message
)
{
if
verbosity
{
fmt
.
Println
(
"found fix commit after"
,
counter
,
"commits"
)
}
}
else
if
containsBreakingChangeFooter
(
message
)
{
commitType
=
BreakingCommit
found
=
true
if
verbosity
{
fmt
.
Println
(
"found breaking commit after"
,
counter
,
"commits"
)
}
return
storer
.
ErrStop
// stop iteration
}
...
...
This diff is collapsed.
Click to expand it.
version.go
+
10
−
10
View file @
aa4874d1
...
...
@@ -212,25 +212,25 @@ func writeVersionToYaml(version string) error {
func
writeVersionToGit
(
version
string
)
error
{
path
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
return
err
}
repo
:=
gitRepo
repo
,
err
:=
git
.
PlainOpen
(
path
)
h
,
err
:=
repo
.
Head
(
)
if
err
!=
nil
{
return
err
}
h
,
err
:=
repo
.
Head
()
if
err
!=
nil
{
return
err
if
verbosity
{
fmt
.
Println
(
"Writing to git HEAD:"
,
h
.
Hash
())
}
_
,
err
=
repo
.
CreateTag
(
version
,
h
.
Hash
(),
&
git
.
CreateTagOptions
{
Message
:
version
,
Message
:
"chore: bump version to "
+
version
,
})
if
err
!=
nil
{
return
err
}
return
nil
}
...
...
@@ -245,7 +245,7 @@ func writeVersion(version string) error {
}
if
arguments
.
Path
==
""
{
if
arguments
.
Null
{
if
nullTerminated
{
version
+=
string
(
rune
(
0
))
fmt
.
Printf
(
"%s"
,
version
)
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment