Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
regit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Software Systems Engineering
regit
Commits
522f86be
Commit
522f86be
authored
May 8, 2023
by
chrg
Browse files
Options
Downloads
Patches
Plain Diff
Add --batch option
parent
8b64ce1f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/regit/__main__.py
+74
-23
74 additions, 23 deletions
src/regit/__main__.py
with
74 additions
and
23 deletions
src/regit/__main__.py
+
74
−
23
View file @
522f86be
from
dataclasses
import
dataclass
,
field
from
dataclasses
import
dataclass
,
field
from
pathlib
import
Path
from
pathlib
import
Path
from
typing
import
Callable
from
typing
import
Callable
,
cast
import
subprocess
import
csv
import
csv
import
logging
import
logging
import
tempfile
import
tempfile
from
contextlib
import
contextmanager
import
click
import
click
import
git
import
git
...
@@ -99,6 +101,7 @@ class BlobHandler:
...
@@ -99,6 +101,7 @@ class BlobHandler:
help
=
"
The file to output the csv mapping to
"
,
help
=
"
The file to output the csv mapping to
"
,
type
=
click
.
File
(
"
w
"
,
lazy
=
False
),
type
=
click
.
File
(
"
w
"
,
lazy
=
False
),
)
)
@click.option
(
"
--batch/--no-batch
"
,
default
=
False
)
@click.option
(
@click.option
(
"
-v
"
,
"
-v
"
,
"
--verbose
"
,
"
--verbose
"
,
...
@@ -115,6 +118,7 @@ def regit(
...
@@ -115,6 +118,7 @@ def regit(
program
:
Path
,
program
:
Path
,
args
:
tuple
[
str
],
args
:
tuple
[
str
],
verbose
:
int
,
verbose
:
int
,
batch
:
bool
,
):
):
"""
A simple program that runs a command on every commit on a repo.
"""
"""
A simple program that runs a command on every commit on a repo.
"""
...
@@ -132,22 +136,6 @@ def regit(
...
@@ -132,22 +136,6 @@ def regit(
repo
=
git
.
Repo
.
clone_from
(
url
=
repo
,
to_path
=
output
,
no_local
=
True
)
repo
=
git
.
Repo
.
clone_from
(
url
=
repo
,
to_path
=
output
,
no_local
=
True
)
log
.
debug
(
"
Cloned repo to %s
"
,
output
)
log
.
debug
(
"
Cloned repo to %s
"
,
output
)
with
tempfile
.
TemporaryDirectory
()
as
folder
:
folder
=
Path
(
folder
)
def
transformer
(
file
:
Path
,
content
:
bytes
)
->
bytes
:
pargs
=
list
(
args
)
try
:
ix
=
pargs
.
index
(
"
{}
"
)
except
ValueError
:
return
utils
.
run_stdout
([
program
]
+
pargs
,
input
=
content
)
else
:
with
utils
.
tfile
(
folder
,
file
.
name
,
content
)
as
tmp_file
:
pargs
[
ix
]
=
str
(
tmp_file
)
utils
.
run
([
program
]
+
pargs
)
with
open
(
tmp_file
,
"
rb
"
)
as
f
:
return
f
.
read
()
def
is_relevant
(
file
:
Path
):
def
is_relevant
(
file
:
Path
):
if
pattern
is
None
:
if
pattern
is
None
:
return
True
return
True
...
@@ -155,6 +143,7 @@ def regit(
...
@@ -155,6 +143,7 @@ def regit(
log
.
debug
(
"
Check if %s matched pattern %s
"
,
file
,
match
)
log
.
debug
(
"
Check if %s matched pattern %s
"
,
file
,
match
)
return
match
return
match
with
mktransformer
(
program
,
args
,
batch
)
as
transformer
:
handler
=
BlobHandler
(
repo
,
is_relevant
=
is_relevant
,
transform
=
transformer
)
handler
=
BlobHandler
(
repo
,
is_relevant
=
is_relevant
,
transform
=
transformer
)
log
.
debug
(
"
Starting handler
"
)
log
.
debug
(
"
Starting handler
"
)
with
handler
:
with
handler
:
...
@@ -176,5 +165,67 @@ def regit(
...
@@ -176,5 +165,67 @@ def regit(
print
(
repo
.
working_dir
)
print
(
repo
.
working_dir
)
@contextmanager
def
mktransformer
(
program
,
args
,
batch
):
with
tempfile
.
TemporaryDirectory
()
as
folder
:
folder
=
Path
(
folder
)
pargs
=
list
(
args
)
if
batch
:
formatproc
=
utils
.
popen
(
[
program
]
+
pargs
,
stdin
=
subprocess
.
PIPE
,
universal_newlines
=
False
,
)
if
formatproc
.
stdin
is
None
:
raise
RuntimeError
(
"
Could not start the formatting program
"
)
fin
=
formatproc
.
stdin
def
transformer
(
file
:
Path
,
content
:
bytes
)
->
bytes
:
with
utils
.
tfile
(
folder
,
file
.
name
,
content
)
as
tmp_file
:
try
:
fin
.
write
(
str
(
tmp_file
).
encode
(
"
utf-8
"
)
+
b
"
\n
"
)
# type: ignore
fin
.
flush
()
except
subprocess
.
CalledProcessError
:
file
=
Path
(
file
.
name
).
with_suffix
(
"
input
"
).
absolute
()
log
.
error
(
"
Writing argument to %s
"
,
file
)
with
open
(
file
,
"
wb
"
)
as
f
:
f
.
write
(
content
)
raise
with
open
(
tmp_file
,
"
rb
"
)
as
f
:
return
f
.
read
()
yield
transformer
formatproc
.
stdin
.
close
()
formatproc
.
wait
()
elif
"
{}
"
in
pargs
:
ix
=
pargs
.
index
(
"
{}
"
)
def
transformer
(
file
:
Path
,
content
:
bytes
)
->
bytes
:
with
utils
.
tfile
(
folder
,
file
.
name
,
content
)
as
tmp_file
:
pargs
[
ix
]
=
str
(
tmp_file
)
try
:
utils
.
run
([
program
]
+
pargs
)
except
subprocess
.
CalledProcessError
:
file
=
Path
(
file
.
name
).
with_suffix
(
"
input
"
).
absolute
()
log
.
error
(
"
Writing argument to %s
"
,
file
)
with
open
(
file
,
"
wb
"
)
as
f
:
f
.
write
(
content
)
raise
with
open
(
tmp_file
,
"
rb
"
)
as
f
:
return
f
.
read
()
yield
transformer
else
:
def
transformer
(
file
:
Path
,
content
:
bytes
)
->
bytes
:
return
utils
.
run_stdout
([
program
]
+
pargs
,
input
=
content
)
yield
transformer
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
regit
()
regit
()
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