summaryrefslogtreecommitdiffstats
path: root/git.py
diff options
context:
space:
mode:
authorAudun Sutterud <audun.sutterud@qt.io>2024-08-06 13:43:14 +0200
committerAudun Sutterud <audun.sutterud@qt.io>2024-08-06 13:43:14 +0200
commit1128fd91e1426461e1870a73142b69e50f459abc (patch)
treeefe6ab25ea6e9a82a4149ba794a0d0cc59f730fd /git.py
Initial commit
Diffstat (limited to 'git.py')
-rw-r--r--git.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/git.py b/git.py
new file mode 100644
index 0000000..fd827f9
--- /dev/null
+++ b/git.py
@@ -0,0 +1,77 @@
+# Copyright (C) 2023 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+import os
+from typing import Optional, Union
+
+import common
+
+COMMAND_TIMEOUT = 5 * 60
+
+
+class Remote:
+ """A remote repository."""
+
+ def __init__(self, url: str) -> None:
+ self.url = url
+
+
+class Repository:
+ """
+ A local repository cloned from a remote repository.
+ """
+
+ def __init__(self, directory: str) -> None:
+ self.directory = directory
+
+ @staticmethod
+ async def clone(
+ remote: Remote,
+ parent_directory: str,
+ log_directory: str,
+ ) -> Union["Repository", common.Error]:
+ try:
+ name = remote.url.rsplit("/", maxsplit=1)[1]
+ except IndexError:
+ return common.Error("Failed to extract repository name from remote URL")
+
+ directory = os.path.join(parent_directory, name)
+ error = await common.Command.run(
+ arguments=["git", "clone", "--", remote.url, directory],
+ output_file=os.path.join(log_directory, "clone.log"),
+ timeout=COMMAND_TIMEOUT,
+ )
+ match error:
+ case common.Error() as error:
+ return error
+
+ return Repository(directory)
+
+ async def reset(self, revision: str, log_directory: str) -> Optional[common.Error]:
+ error = await common.Command.run(
+ arguments=["git", "fetch", "origin", revision],
+ output_file=os.path.join(log_directory, "fetch.log"),
+ timeout=COMMAND_TIMEOUT,
+ cwd=self.directory,
+ )
+ if error:
+ return error
+
+ error = await common.Command.run(
+ arguments=["git", "clean", "-dfx"],
+ output_file=os.path.join(log_directory, "clean.log"),
+ timeout=COMMAND_TIMEOUT,
+ cwd=self.directory,
+ )
+ if error:
+ return error
+
+ error = await common.Command.run(
+ arguments=["git", "reset", "--hard", revision],
+ output_file=os.path.join(log_directory, "reset.log"),
+ timeout=COMMAND_TIMEOUT,
+ cwd=self.directory,
+ )
+ if error:
+ return error
+
+ return None