1
1
import os
2
+ import shutil
2
3
import subprocess
4
+ import tempfile
3
5
import typing
4
6
5
7
8
+ def add_safe_git_dir (path : str ) -> None :
9
+ """add_safe_git_dir marks the directory safe for git
10
+
11
+ Args:
12
+ path (str): The path to directory
13
+ """
14
+ try :
15
+ subprocess .run (["git" , "config" , "--add" , "safe.directory" , path ], cwd = path )
16
+ except Exception :
17
+ return
18
+
6
19
def is_file (filename : str ) -> bool :
7
20
"""is_file checks if a given value is valid a file.
8
21
@@ -16,10 +29,11 @@ def is_file(filename: str) -> bool:
16
29
return True
17
30
return False
18
31
19
- def commit_staged_files (message : str = "Dummy commit by semgrep pre-commit hook" ) -> bool :
32
+ def commit_staged_files (path : str , message : str = "Dummy commit by semgrep pre-commit hook" ) -> bool :
20
33
"""commit_staged_files commits all staged files with a given message.
21
34
22
35
Args:
36
+ path (str): The path to the git repository.
23
37
message (str, optional): A commit message.
24
38
Defaults to "Dummy commit by semgrep pre-commit hook".
25
39
@@ -28,30 +42,34 @@ def commit_staged_files(message: str = "Dummy commit by semgrep pre-commit hook"
28
42
"""
29
43
try :
30
44
# We use -n to avoid running the pre-commit hook again
31
- subprocess .run (["git" , "commit" , "-n" , "-m" , message ], stdout = subprocess .DEVNULL )
45
+ subprocess .run (["git" , "commit" , "-n" , "-m" , message ], stdout = subprocess .DEVNULL ,
46
+ cwd = path )
32
47
except Exception :
33
48
return False
34
49
else :
35
50
return True
36
51
37
- def get_last_two_commit_ids () -> typing .Tuple [str | None , str | None ]:
52
+ def get_last_two_commit_ids (path : str ) -> typing .Tuple [str | None , str | None ]:
38
53
"""get_last_two_commit_ids returns the last two commit IDs of the current branch
39
54
55
+ Args:
56
+ path (str): The path to the git repository.
57
+
40
58
Returns:
41
59
typing.Tuple[str | None, str | None]: The last two commit IDs
42
60
"""
43
61
commit_ids = None , None
44
62
try :
45
63
proc = subprocess .run (["git" , "log" , "--pretty=format:%H" , "-n" , "2" ],
46
- capture_output = True )
64
+ capture_output = True , cwd = path )
47
65
if proc .returncode == 0 :
48
66
commit_ids = proc .stdout .decode ("utf-8" ).strip ("\n " ).split ("\n " )
49
67
except Exception :
50
68
return None , None
51
69
else :
52
70
return tuple (commit_ids )
53
71
54
- def soft_reset_to_commit (commit_id : str ) -> bool :
72
+ def soft_reset_to_commit (path : str , commit_id : str ) -> bool :
55
73
"""git_soft_reset_to_commit performs a soft reset to the previous commit
56
74
57
75
Args:
@@ -61,8 +79,39 @@ def soft_reset_to_commit(commit_id: str) -> bool:
61
79
bool: True if the soft reset was successful, False otherwise
62
80
"""
63
81
try :
64
- subprocess .run (["git" , "reset" , "--soft" , commit_id ], stdout = subprocess .DEVNULL )
82
+ subprocess .run (["git" , "reset" , "--soft" , commit_id ], stdout = subprocess .DEVNULL , cwd = path )
65
83
except Exception :
66
84
return False
67
85
else :
68
86
return True
87
+
88
+ def make_tmp_dir () -> tempfile .TemporaryDirectory :
89
+ """
90
+ make_tmp_dir: Creates a temporary directory
91
+
92
+ Returns:
93
+ tempfile.TemporaryDirectory: The tempfile.TemporaryDirectory object
94
+ """
95
+ temp_dir = tempfile .TemporaryDirectory ()
96
+ return temp_dir
97
+
98
+ def copy_to_tmp (dest : str | tempfile .TemporaryDirectory , src : str = "/src" ) -> bool :
99
+ """
100
+ copy_to_tmp: Copies the contents of the src directory to the dest directory
101
+
102
+ Args:
103
+ dest (str | tempfile.TemporaryDirectory): A tempfile.TemporaryDirectory object or a string
104
+ src (str, optional): The source path to be copied. Defaults to "/src".
105
+
106
+ Returns:
107
+ bool: True if the copy was successful, False otherwise
108
+ """
109
+ dest_name = dest
110
+ if isinstance (dest_name , tempfile .TemporaryDirectory ):
111
+ dest_name = dest .name
112
+
113
+ try :
114
+ shutil .copytree (src , dest_name , dirs_exist_ok = True )
115
+ return True
116
+ except Exception :
117
+ return False
0 commit comments