Skip to content
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

Support specific event types from vcs fs #345

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/twelve-snails-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@atlaspack/rust': patch
'@atlaspack/fs': patch
---

Modify how VCS watcher change events are forwarded
5 changes: 3 additions & 2 deletions crates/atlaspack_vcs/examples/vcs_file_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ fn main() {
.unwrap();

for change in changes {
let change = change.strip_prefix(&repository_root).unwrap();
println!("{}", change.display());
let change_type = change.change_type_str();
let change = change.path().strip_prefix(&repository_root).unwrap();
println!("{} {}", change_type, change.display());
}
}
}
Expand Down
113 changes: 99 additions & 14 deletions crates/atlaspack_vcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,41 @@ pub enum FailureMode {
FailOnMissingNodeModules,
}

pub fn get_changed_files(
repo_path: &Path,
old_rev: &str,
new_rev: &str,
failure_mode: FailureMode,
) -> anyhow::Result<Vec<PathBuf>> {
let repo = Repository::open(repo_path)?;
let old_commit = repo.revparse_single(old_rev)?.peel_to_commit()?;
let new_commit = repo.revparse_single(new_rev)?.peel_to_commit()?;
pub struct FileChangeEvent {
path: PathBuf,
change_type: FileChangeType,
}

impl FileChangeEvent {
pub fn path(&self) -> &Path {
&self.path
}

pub fn change_type(&self) -> &FileChangeType {
&self.change_type
}

pub fn change_type_str(&self) -> &str {
match self.change_type() {
FileChangeType::Create => "create",
FileChangeType::Update => "update",
FileChangeType::Delete => "delete",
}
}
}

pub enum FileChangeType {
Create,
Update,
Delete,
}

pub fn get_changed_files_from_git(
repo_path: &Path,
repo: &Repository,
old_commit: &git2::Commit<'_>,
new_commit: &git2::Commit<'_>,
) -> anyhow::Result<Vec<FileChangeEvent>> {
tracing::debug!("Calculating git diff");
let mut diff_options = DiffOptions::new();
let diff = repo.diff_tree_to_tree(
Expand All @@ -288,8 +313,42 @@ pub fn get_changed_files(
let mut changed_files = Vec::new();
diff.foreach(
&mut |delta, _| {
if let Some(path) = delta.new_file().path() {
changed_files.push(repo_path.join(path));
if let Some(new_file_path) = delta.new_file().path() {
let new_file_path = repo_path.join(new_file_path);

let status = delta.status();
if status == git2::Delta::Renamed {
if let Some(old_file_path) = delta.old_file().path() {
let old_file_path = repo_path.join(old_file_path);
changed_files.push(FileChangeEvent {
path: old_file_path,
change_type: FileChangeType::Delete,
});
}

changed_files.push(FileChangeEvent {
path: new_file_path,
change_type: FileChangeType::Create,
});
return true;
}

changed_files.push(FileChangeEvent {
path: new_file_path,
change_type: match status {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all of these types for our solution? Do they affect how we bundle things?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do at the moment, but we might tweak the invalidation routine a bit to handle full node_modules sub-directory invalidation

git2::Delta::Added => FileChangeType::Create,
git2::Delta::Modified => FileChangeType::Update,
git2::Delta::Deleted => FileChangeType::Delete,
git2::Delta::Unmodified => FileChangeType::Update,
git2::Delta::Copied => FileChangeType::Create,
git2::Delta::Ignored => FileChangeType::Update,
git2::Delta::Untracked => FileChangeType::Update,
git2::Delta::Typechange => FileChangeType::Update,
git2::Delta::Unreadable => FileChangeType::Update,
git2::Delta::Conflicted => FileChangeType::Update,
git2::Delta::Renamed => panic!("Impossible branch"),
},
});
}
true
},
Expand All @@ -298,12 +357,28 @@ pub fn get_changed_files(
None,
)?;

Ok(changed_files)
}

pub fn get_changed_files(
repo_path: &Path,
old_rev: &str,
new_rev: &str,
failure_mode: FailureMode,
) -> anyhow::Result<Vec<FileChangeEvent>> {
let repo = Repository::open(repo_path)?;
let old_commit = repo.revparse_single(old_rev)?.peel_to_commit()?;
let new_commit = repo.revparse_single(new_rev)?.peel_to_commit()?;

let mut changed_files = get_changed_files_from_git(repo_path, &repo, &old_commit, &new_commit)?;

tracing::debug!("Reading yarn.lock from {} and {}", old_rev, new_rev);
let yarn_lock_changes = changed_files
.iter()
.filter(|file| file.file_name().unwrap() == "yarn.lock")
.cloned()
.filter(|file| file.path.file_name().unwrap() == "yarn.lock")
.map(|file| file.path.clone())
.collect::<Vec<_>>();

for yarn_lock_path in yarn_lock_changes {
tracing::debug!("Found yarn.lock in changed files");
let yarn_lock_path = yarn_lock_path.strip_prefix(repo_path)?;
Expand Down Expand Up @@ -338,7 +413,17 @@ pub fn get_changed_files(
&new_yarn_lock,
&yarn_state,
);
changed_files.extend(node_modules_changes);

for change in node_modules_changes {
changed_files.push(FileChangeEvent {
path: change.clone(),
change_type: FileChangeType::Delete,
});
changed_files.push(FileChangeEvent {
path: change,
change_type: FileChangeType::Create,
});
}
}

tracing::debug!("Done");
Expand Down
13 changes: 11 additions & 2 deletions crates/node-bindings/src/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ pub fn get_vcs_state_snapshot(
Ok(vcs_state)
}

#[napi(object)]
pub struct NodeChangeEvent {
pub path: String,
pub change_type: String,
}

#[napi]
pub fn get_events_since(
repo_path: String,
old_rev: String,
new_rev: Option<String>,
) -> napi::Result<Vec<String>> {
) -> napi::Result<Vec<NodeChangeEvent>> {
let repo_path = Path::new(&repo_path);
let files = atlaspack_vcs::get_changed_files(
repo_path,
Expand All @@ -39,7 +45,10 @@ pub fn get_events_since(

let files = files
.iter()
.map(|file| file.to_str().unwrap().to_string())
.map(|event| NodeChangeEvent {
path: event.path().to_str().unwrap().to_string(),
change_type: event.change_type_str().to_string(),
})
.collect();

Ok(files)
Expand Down
7 changes: 5 additions & 2 deletions packages/core/fs/src/NodeVCSAwareFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import packageJSON from '../package.json';
export interface NodeVCSAwareFSOptions {
gitRepoPath: FilePath;
excludePatterns: Array<string>;
logEventDiff: (watcherEvents: Event[], vcsEvents: string[]) => void;
logEventDiff: (watcherEvents: Event[], vcsEvents: Event[]) => void;
}

export class NodeVCSAwareFS extends NodeFS {
Expand Down Expand Up @@ -43,7 +43,10 @@ export class NodeVCSAwareFS extends NodeFS {
'NodeVCSAwareFS::rust.getEventsSince',
() => getEventsSince(this.#options.gitRepoPath, vcsState.gitHash),
);
this.#options.logEventDiff(watcherEventsSince, vcsEventsSince);
this.#options.logEventDiff(
watcherEventsSince,
vcsEventsSince.map((e) => ({path: e.path, type: e.changeType})),
);

return watcherEventsSince;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/rust/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,16 @@ declare export function getVcsStateSnapshot(
path: string,
excludePatterns: Array<string>,
): VCSState;
export interface NodeChangeEvent {
path: string;
changeType: 'update' | 'create' | 'delete';
}

declare export function getEventsSince(
repoPath: string,
oldRev: string,
newRev?: string | void | null,
): Array<string>;
): Array<NodeChangeEvent>;

declare export function createAssetId(params: mixed): string;
declare export function createDependencyId(params: mixed): string;
Expand Down