1010 " // Devcontainer context hash (do not manually edit this, it's managed by a pre-commit hook): "
1111)
1212
13+ DEVCONTAINER_COMMENT_LINE_SUFFIX = (
14+ " # spellchecker:disable-line" # the typos hook can sometimes mess with the hash without this
15+ )
16+
1317
1418def get_tracked_files (repo_path : Path ) -> list [str ]:
1519 """Return a list of files tracked by Git in the given repository folder, using the 'git ls-files' command."""
@@ -76,9 +80,13 @@ def find_devcontainer_hash_line(lines: list[str]) -> tuple[int, str | None]:
7680 for i in range (len (lines ) - 1 , - 1 , - 1 ):
7781 if lines [i ].strip () == "}" :
7882 # Check the line above it
79- if i > 0 and lines [i - 1 ].startswith (DEVCONTAINER_COMMENT_LINE_PREFIX ):
80- current_hash = lines [i - 1 ].split (": " , 1 )[1 ].strip ()
81- return i - 1 , current_hash
83+ if i > 0 :
84+ above_line = lines [i - 1 ]
85+ if above_line .startswith (DEVCONTAINER_COMMENT_LINE_PREFIX ):
86+ part_after_prefix = above_line .split (": " , 1 )[1 ]
87+ part_before_suffix = part_after_prefix .split ("#" )[0 ]
88+ current_hash = part_before_suffix .strip ()
89+ return i - 1 , current_hash
8290 return i , None
8391 return - 1 , None
8492
@@ -102,12 +110,13 @@ def update_devcontainer_context_hash(devcontainer_json_file: Path, new_hash: str
102110 lines = file .readlines ()
103111
104112 line_index , current_hash = find_devcontainer_hash_line (lines )
113+ new_hash_line = f"{ DEVCONTAINER_COMMENT_LINE_PREFIX } { new_hash } { DEVCONTAINER_COMMENT_LINE_SUFFIX } \n "
105114 if current_hash is not None :
106115 # Replace the old hash with the new hash
107- lines [line_index ] = f" { DEVCONTAINER_COMMENT_LINE_PREFIX } { new_hash } \n "
116+ lines [line_index ] = new_hash_line
108117 else :
109118 # Insert the new hash line above the closing `}`
110- lines .insert (line_index , f" { DEVCONTAINER_COMMENT_LINE_PREFIX } { new_hash } \n " )
119+ lines .insert (line_index , new_hash_line )
111120
112121 # Write the updated lines back to the file
113122 with devcontainer_json_file .open ("w" , encoding = "utf-8" ) as file :
0 commit comments