MCPcopy Create free account
hub / github.com/github/gh-aw / cleanupOrphanedIncludes

Function cleanupOrphanedIncludes

pkg/cli/remove_command.go:175–262  ·  view source on GitHub ↗

cleanupOrphanedIncludes removes include files that are no longer used by any workflow

(verbose bool)

Source from the content-addressed store, hash-verified

173
174// cleanupOrphanedIncludes removes include files that are no longer used by any workflow
175func cleanupOrphanedIncludes(verbose bool) error {
176 removeLog.Print("Cleaning up orphaned include files")
177 // Get all remaining markdown files
178 mdFiles, err := getMarkdownWorkflowFiles("")
179 if err != nil {
180 // No markdown files means we can clean up all includes
181 removeLog.Print("No markdown files found, cleaning up all includes")
182 if verbose {
183 fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No markdown files found, cleaning up all includes"))
184 }
185 return cleanupAllIncludes(verbose)
186 }
187
188 // Collect all include dependencies from remaining workflows
189 usedIncludes := make(map[string]struct {
190 })
191
192 for _, mdFile := range mdFiles {
193 content, err := os.ReadFile(mdFile)
194 if err != nil {
195 if verbose {
196 fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not read %s for include analysis: %v", mdFile, err)))
197 }
198 continue
199 }
200
201 // Find includes used by this workflow
202 includes, err := findIncludesInContent(string(content))
203 if err != nil {
204 if verbose {
205 fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not analyze includes in %s: %v", mdFile, err)))
206 }
207 continue
208 }
209
210 for _, include := range includes {
211 usedIncludes[include] = struct {
212 }{}
213 }
214 }
215
216 // Find all include files in the workflows directory
217 // Only consider files in subdirectories (like shared/) as potential include files
218 // Root-level .md files are workflow files, not include files
219 workflowsDir := constants.GetWorkflowDir()
220 var allIncludes []string
221
222 err = filepath.Walk(workflowsDir, func(path string, info os.FileInfo, err error) error {
223 if err != nil {
224 return err
225 }
226
227 if !info.IsDir() && strings.HasSuffix(info.Name(), ".md") {
228 relPath, err := filepath.Rel(workflowsDir, path)
229 if err != nil {
230 return err
231 }
232

Callers 2

RemoveWorkflowsFunction · 0.85

Calls 10

FormatInfoMessageFunction · 0.92
FormatWarningMessageFunction · 0.92
GetWorkflowDirFunction · 0.92
ContainsFunction · 0.92
FormatSuccessMessageFunction · 0.92
getMarkdownWorkflowFilesFunction · 0.85
cleanupAllIncludesFunction · 0.85
findIncludesInContentFunction · 0.85
PrintMethod · 0.80
ErrorfMethod · 0.45

Tested by 1