-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Introduce .kraftignore
#1032
Open
MdSahil-oss
wants to merge
2
commits into
unikraft:staging
Choose a base branch
from
MdSahil-oss:kraftignore
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
package initrd | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"kraftkit.sh/log" | ||
) | ||
|
||
// kraftignore filename | ||
const KraftignoreFileName = ".kraftignore" | ||
|
||
type IgnoringFileType string | ||
|
||
const ( | ||
Exist = IgnoringFileType("Exist") | ||
NotExist = IgnoringFileType("NotExist") | ||
SkipDir = IgnoringFileType("SkipDir") | ||
) | ||
|
||
// GetKraftIgnoreItems returns file and directory names specified in .kraftignore | ||
func GetKraftIgnoreItems(ctx context.Context, dir string) ([]string, error) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
kraftignorePath := filepath.Join(cwd, KraftignoreFileName) | ||
|
||
if _, err := os.Stat(kraftignorePath); errors.Is(err, os.ErrNotExist) { | ||
return []string{}, nil | ||
} else if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
kraftignoreFile, err := os.Open(kraftignorePath) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
defer func() { | ||
kraftIgnoreErr := kraftignoreFile.Close() | ||
if kraftIgnoreErr != nil { | ||
if err != nil { | ||
err = fmt.Errorf("%w: %w", err, kraftIgnoreErr) | ||
} else { | ||
err = kraftIgnoreErr | ||
} | ||
} | ||
}() | ||
|
||
kraftignoreScanner := bufio.NewScanner(kraftignoreFile) | ||
kraftignoreScanner.Split(bufio.ScanLines) | ||
var kraftignoreFileLines, ignoringItems []string | ||
|
||
for kraftignoreScanner.Scan() { | ||
kraftignoreFileLines = append(kraftignoreFileLines, kraftignoreScanner.Text()) | ||
} | ||
|
||
for lineNum, line := range kraftignoreFileLines { | ||
line = strings.TrimSpace(line) | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
items := findLineItems(line) | ||
for _, item := range items { | ||
if item == "" || item == "#" { | ||
continue | ||
} | ||
|
||
if hasGlobPatterns(item) { | ||
log.G(ctx). | ||
WithField("file", kraftignorePath). | ||
Warn("contains a glob pattern ", item, | ||
" at line ", lineNum, | ||
" which is not supported by Kraftkit") | ||
continue | ||
} | ||
|
||
if _, err := os.Stat(filepath.Join(dir, item)); os.IsNotExist(err) { | ||
log.G(ctx). | ||
WithField("file", kraftignorePath). | ||
Warn("contains ", item, | ||
" at line ", lineNum, | ||
" which does not exist in the provided rootfs directory") | ||
continue | ||
} | ||
|
||
ignoringItems = append(ignoringItems, item) | ||
} | ||
} | ||
|
||
return ignoringItems, err | ||
} | ||
|
||
// isExistInKraftignoreFile checks if the path exist in .kraftignore | ||
func isExistInKraftignoreFile(internal string, pathInfo fs.DirEntry, kraftignoreItems []string) IgnoringFileType { | ||
for _, ignoringItem := range kraftignoreItems { | ||
if internal == ignoringItem { | ||
if pathInfo.IsDir() { | ||
return SkipDir | ||
} | ||
return Exist | ||
} | ||
} | ||
return NotExist | ||
} | ||
|
||
// hasGlobPatterns checks if the item contains glob pattern | ||
func hasGlobPatterns(item string) bool { | ||
return strings.ContainsAny(item, "*?![{") | ||
} | ||
|
||
// findLineItems finds items in a line of .kraftignore | ||
func findLineItems(line string) []string { | ||
items := strings.Split(line, " ") | ||
for index := 0; index < len(items); index++ { | ||
charToFind := "" | ||
if strings.HasPrefix(items[index], `"`) && !strings.HasSuffix(items[index], `"`) { | ||
charToFind = `"` | ||
} else if strings.HasPrefix(items[index], `'`) && !strings.HasSuffix(items[index], `'`) { | ||
charToFind = `'` | ||
} | ||
|
||
if len(charToFind) > 0 { | ||
i := index + 1 | ||
for ; i < len(items) && !strings.HasSuffix(items[i], charToFind); i++ { | ||
items[index] += " " + items[i] | ||
items = append(items[:i], items[i+1:]...) | ||
i-- | ||
} | ||
items[index] += " " + items[i] | ||
items = append(items[:i], items[i+1:]...) | ||
} | ||
items[index] = strings.Trim(items[index], `"`) | ||
items[index] = strings.Trim(items[index], `'`) | ||
items[index] = strings.TrimSpace(items[index]) | ||
items[index] = strings.TrimPrefix(items[index], "../") | ||
if !strings.HasPrefix(items[index], "./") { | ||
if !strings.HasPrefix(items[index], "/") { | ||
items[index] = "/" + items[index] | ||
} | ||
items[index] = "." + items[index] | ||
} | ||
items[index] = strings.TrimSuffix(items[index], string(filepath.Separator)) | ||
} | ||
return items | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, we can declare these as errors:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can then do comparisons via:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely, It can be done. But
kraftiignore.go
not uses these vars as errors, These vars are being used as notifiers forfilepath.WalkDir()
function indirectory.go
.Please have an eye on
filepath.WalkDir()
function then lemme know if you really want me to change const vars as error vars :)