-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
74 lines (60 loc) · 2.51 KB
/
git.py
File metadata and controls
74 lines (60 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import filecmp
def do_command(command, pathSpecs, filePaths, pathsToShowLogFor, versions, message):
"""
Executes the specified Git command with the given arguments.
Args:
command (str): The Git command to execute.
pathSpecs (list): A list of file path patterns to include in the command.
filePaths (list): A list of file paths to include in the command.
pathsToShowLogFor (list): A list of file paths to show logs for.
versions (list): A list of versions to compare for the "diff" command.
message (str): The commit message to use for the "commit" command.
Returns:
str: The result of the Git command.
Raises:
TypeError: If any of the input arguments are not lists.
ValueError: If the "diff" command is used with a number of versions other than 2,
or if any of the specified file paths do not exist for the "commit" command.
"""
if not isinstance(pathSpecs, list):
raise TypeError("pathSpecs should be a list")
if not isinstance(filePaths, list):
raise TypeError("filePaths should be a list")
if not isinstance(pathsToShowLogFor, list):
raise TypeError("pathsToShowLogFor should be a list")
if not isinstance(versions, list):
raise TypeError("versions should be a list")
if command == "status":
return status(pathSpecs)
elif command == "commit":
return commit(filePaths, message)
elif command == "log":
return log(pathsToShowLogFor)
elif command == "diff":
if len(versions) != 2:
raise ValueError("diff command requires exactly 2 versions")
return diff(versions[0], versions[1])
else:
return f"{command} is not supported by git"
def status(pathSpecs):
return f"Status for: {', '.join(pathSpecs)}"
def commit(filePaths, message):
if not message:
return "Please enter a commit message"
for path in filePaths:
if not os.path.exists(path):
raise ValueError(f"{path} is not a valid file path")
return f"Committed: {', '.join(filePaths)}"
def log(pathsToShowLogFor):
return f"Log for: {', '.join(pathsToShowLogFor)}"
def diff(file1, file2):
if not os.path.exists(file1):
return "file is not a valid file path"
if not os.path.exists(file2):
return "file is not a valid file path"
# Compare two files using filecmp
if filecmp.cmp(file1, file2):
return "Files are identical"
else:
return "Files are different"