-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up do-not-land and if-change-then-change (#5)
do-not-land asserts that if you have a 'DO NOT LAND' somewhere in the code then it can't get checked in; with hold-the-line this works out to a 'DO NOT LAND' in the diff if-change-then-change keeps different code blocks in sync. has to go through libgit2 and eventually rely on `${upstream}` from trunk, because it needs to know the exact set of modified lines, not just the set of modified files. for some frustrating reason `diff_tree_to_workdir_with_index` doesn't recognize untracked files, even though i explicitly set it to true; apparently in trunk we go thru a `git status` API instead. (i'm not married to the current syntax, but it's the best that we have right now; it also suffers from a number of structural deficiencies, e.g. doesn't support multiple other files nor block labels) there are still a number of antipatterns in here (e.g. the unwrap in main.rs) but for now i think i'm reasonably happy with how the impl looks. tests are certainly terrible and need to be written, but that's a separate problem.
- Loading branch information
Showing
13 changed files
with
393 additions
and
61 deletions.
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,3 @@ | ||
[toolchain] | ||
channel = "1.64.0" | ||
components = ["cargo-watch"] |
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,68 @@ | ||
use git2::{Delta, DiffOptions, Repository}; | ||
use std::collections::HashSet; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug)] | ||
pub struct Hunk { | ||
pub path: PathBuf, | ||
pub begin: i64, | ||
pub end: i64, | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct NewOrModified { | ||
/// Set of modified line ranges in new/existing files | ||
pub hunks: Vec<Hunk>, | ||
|
||
/// Set of new/modified files | ||
pub paths: HashSet<PathBuf>, | ||
} | ||
|
||
pub fn modified_since(upstream: &str) -> anyhow::Result<NewOrModified> { | ||
let repo = Repository::open(".")?; | ||
|
||
let upstream_tree = repo.find_reference(upstream)?.peel_to_tree()?; | ||
|
||
let diff = { | ||
let mut diff_opts = DiffOptions::new(); | ||
diff_opts.include_untracked(true); | ||
|
||
repo.diff_tree_to_workdir_with_index(Some(&upstream_tree), Some(&mut diff_opts))? | ||
}; | ||
|
||
let mut ret = NewOrModified::default(); | ||
diff.foreach( | ||
&mut |_, _| true, | ||
None, | ||
Some(&mut |delta, hunk| { | ||
match delta.status() { | ||
Delta::Unmodified | ||
| Delta::Added | ||
| Delta::Modified | ||
| Delta::Renamed | ||
| Delta::Copied | ||
| Delta::Untracked => { | ||
if let Some(path) = delta.new_file().path() { | ||
let path = path.to_path_buf(); | ||
|
||
ret.paths.insert(path.clone()); | ||
ret.hunks.push(Hunk { | ||
path, | ||
begin: hunk.new_start() as i64, | ||
end: (hunk.new_start() + hunk.new_lines()) as i64, | ||
}); | ||
} else { | ||
// TODO(sam): accumulate errors and return them | ||
// See https://doc.rust-lang.org/rust-by-example/error/iter_result.html | ||
log::error!("Found git delta where new_file had no path"); | ||
} | ||
} | ||
_ => (), | ||
} | ||
true | ||
}), | ||
None, | ||
)?; | ||
|
||
Ok(ret) | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod lsp_json; | ||
pub mod diagnostic; | ||
pub mod git; | ||
pub mod rules; |
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
Empty file.
Oops, something went wrong.