Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
git-scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Hugo Hörnquist
git-scripts
Commits
27362112
Commit
27362112
authored
2 years ago
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
open: Docstrings.
parent
c07d33f6
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
git-open
+48
-28
48 additions, 28 deletions
git-open
with
48 additions
and
28 deletions
git-open
+
48
−
28
View file @
27362112
#!/usr/bin/env python3
#!/usr/bin/env python3
"""
Figure out the HTTP source of a remote, and open it in a browser.
"""
import
sys
import
sys
import
subprocess
import
subprocess
import
argparse
import
argparse
import
re
import
re
import
os.path
import
os.path
def
err
(
s
):
"""
Print error message.
"""
print
(
"
\x1b
[0;31mError
\x1b
[m
"
+
s
,
file
=
sys
.
stderr
)
def
warn
(
s
):
"""
Print warning message.
"""
print
(
"
\x1b
[0;33mWarn
\x1b
[m
"
+
s
,
file
=
sys
.
stderr
)
def
info
(
s
):
"""
Print info message.
"""
print
(
"
\x1b
[0;32mInfo
\x1b
[m
"
+
s
,
file
=
sys
.
stderr
)
try
:
try
:
# package python-pyxdg on Arch
# package python-pyxdg on Arch
from
xdg.BaseDirectory
import
xdg_config_dirs
from
xdg.BaseDirectory
import
xdg_config_dirs
except
ModuleNotFoundError
:
except
ModuleNotFoundError
:
print
(
"
xdg module not found, defaulting to $HOME/.config
"
,
file
=
sys
.
stderr
)
warn
(
"
xdg module not found, defaulting to $HOME/.config
"
)
import
os
import
os
home
=
os
.
getenv
(
"
HOME
"
)
home
=
os
.
getenv
(
"
HOME
"
)
xdg_config_dirs
=
[
os
.
path
.
join
(
home
,
"
.config
"
)]
xdg_config_dirs
=
[
os
.
path
.
join
(
home
,
"
.config
"
)]
...
@@ -20,8 +39,7 @@ try:
...
@@ -20,8 +39,7 @@ try:
import
yaml
import
yaml
can_load_configuration
=
True
can_load_configuration
=
True
except
ModuleNotFoundError
:
except
ModuleNotFoundError
:
print
(
"
yaml module not found, configuration will NOT be loaded
"
,
warn
(
"
yaml module not found, configuration will NOT be loaded
"
)
file
=
sys
.
stderr
)
configuration
=
{
configuration
=
{
...
@@ -31,6 +49,7 @@ configuration = {
...
@@ -31,6 +49,7 @@ configuration = {
def
popen
(
str
):
def
popen
(
str
):
"""
Exec str, and return its stdout.
"""
p
=
subprocess
.
Popen
(
str
.
split
(
"
"
),
p
=
subprocess
.
Popen
(
str
.
split
(
"
"
),
universal_newlines
=
True
,
universal_newlines
=
True
,
stdout
=
subprocess
.
PIPE
)
stdout
=
subprocess
.
PIPE
)
...
@@ -42,15 +61,24 @@ def popen(str):
...
@@ -42,15 +61,24 @@ def popen(str):
def
gitconf
(
field
):
def
gitconf
(
field
):
"""
Get git config field, or die.
"""
return
popen
(
"
git config
"
+
field
)
return
popen
(
"
git config
"
+
field
)
def
remote_url
(
remote_name
):
def
remote_url
(
remote_name
):
"""
Get url for git remote by name.
"""
# return gitconf("remote.{}.url".format(remote_name))
# return gitconf("remote.{}.url".format(remote_name))
return
popen
(
f
'
git remote get-url
{
remote_name
}
'
).
strip
()
return
popen
(
f
'
git remote get-url
{
remote_name
}
'
).
strip
()
def
to_http
(
url
):
def
to_http
(
url
):
"""
Convert url into matching HTTP url.
Uses configuration[
'
patterns
'
] to check for special cases,
otherwise anything starting with http is returned verbatim, and
git@ is replaced with https://.
"""
for
pattern
in
configuration
[
'
patterns
'
]:
for
pattern
in
configuration
[
'
patterns
'
]:
if
match
:
=
re
.
match
(
pattern
[
'
i
'
],
url
):
if
match
:
=
re
.
match
(
pattern
[
'
i
'
],
url
):
rx
=
re
.
compile
(
'
[$]([0-9]+)
'
)
rx
=
re
.
compile
(
'
[$]([0-9]+)
'
)
...
@@ -74,28 +102,33 @@ def to_http(url):
...
@@ -74,28 +102,33 @@ def to_http(url):
def
xdg_open
(
item
):
def
xdg_open
(
item
):
"""
Run xdg-open on argument.
"""
subprocess
.
run
([
"
xdg-open
"
,
item
])
subprocess
.
run
([
"
xdg-open
"
,
item
])
def
err
(
s
):
print
(
"
\x1b
[0;31mError
\x1b
[m
"
+
s
)
def
warn
(
s
):
print
(
"
\x1b
[0;33mWarn
\x1b
[m
"
+
s
)
def
info
(
s
):
print
(
"
\x1b
[0;32mInfo
\x1b
[m
"
+
s
)
def
open_remote
(
remote
):
def
open_remote
(
remote
):
"""
Open url for the named remote.
"""
url
=
to_http
(
remote_url
(
remote
))
url
=
to_http
(
remote_url
(
remote
))
xdg_open
(
url
)
xdg_open
(
url
)
info
(
f
'
opening
{
url
}
'
)
info
(
f
'
opening
{
url
}
'
)
def
load_configuration
():
"""
Load configuration file, updates `configuration`.
"""
global
configuration
# TODO possibly add ~/.config/git/open.yaml to list
for
dir
in
xdg_config_dirs
:
try
:
with
open
(
os
.
path
.
join
(
dir
,
'
git-open.yaml
'
))
as
f
:
conf
=
yaml
.
unsafe_load
(
f
)
configuration
|=
conf
break
except
FileNotFoundError
:
pass
def
main
(
args
):
def
main
(
args
):
"""
Entry point, args should come from ArgumentParser.parse_args.
"""
out
=
popen
(
"
git remote
"
)
out
=
popen
(
"
git remote
"
)
remotes
=
out
.
strip
().
split
(
"
\n
"
)
remotes
=
out
.
strip
().
split
(
"
\n
"
)
...
@@ -127,19 +160,6 @@ def main(args):
...
@@ -127,19 +160,6 @@ def main(args):
err
(
"
All remotes failed
"
)
err
(
"
All remotes failed
"
)
def
load_configuration
():
global
configuration
# TODO possibly add ~/.config/git/open.yaml to list
for
dir
in
xdg_config_dirs
:
try
:
with
open
(
os
.
path
.
join
(
dir
,
'
git-open.yaml
'
))
as
f
:
conf
=
yaml
.
unsafe_load
(
f
)
configuration
|=
conf
break
except
FileNotFoundError
:
pass
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
# Note that argparse gives `--help' and `-h', but git "eats"
# Note that argparse gives `--help' and `-h', but git "eats"
# `--help'. `-h' does however work.
# `--help'. `-h' does however work.
...
...
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