common.py
Python script, ASCII text executable
1
"""
2
This module provides some essential utilities for Roundabout.
3
4
Roundabout - git hosting for everyone <https://roundabout-host.com>
5
Copyright (C) 2023-2025 Roundabout developers <root@roundabout-host.com>
6
7
This program is free software: you can redistribute it and/or modify
8
it under the terms of the GNU Affero General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU Affero General Public License for more details.
16
17
You should have received a copy of the GNU Affero General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""
20
21
import os
22
import subprocess
23
24
25
def git_command(repo, data, *args, return_err=False, return_exit=False):
26
if not os.path.isdir(repo):
27
raise FileNotFoundError(f"Repo {repo} not found")
28
env = os.environ.copy()
29
30
command = ["git", *args]
31
32
proc = subprocess.Popen(" ".join(command), cwd=repo, env=env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
33
stdin=subprocess.PIPE)
34
35
if data:
36
proc.stdin.write(data)
37
38
out, err = proc.communicate()
39
exit_code = proc.returncode
40
41
result = (out,)
42
43
if return_err:
44
result = result + (err,)
45
if return_exit:
46
result = result + (exit_code,)
47
48
return result[0] if len(result) == 1 else result
49
50