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