-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add more hooks #27
Open
chaaz
wants to merge
1
commit into
main
Choose a base branch
from
19-more-hooks
base: main
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
Add more hooks #27
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -190,38 +190,50 @@ impl StateWrite { | |
Ok(()) | ||
} | ||
|
||
pub fn commit(&mut self, repo: &Repo, data: CommitArgs) -> Result<()> { | ||
pub fn commit(&mut self, repo: &Repo, args: CommitArgs) -> Result<()> { | ||
for proj_id in &self.proj_writes { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_pre_begin(root)?; | ||
} | ||
} | ||
|
||
for proj_id in &self.proj_writes { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_pre_write(root)?; | ||
} | ||
} | ||
|
||
for write in &self.writes { | ||
write.write()?; | ||
} | ||
let did_write = !self.writes.is_empty(); | ||
self.writes.clear(); | ||
|
||
for proj_id in &self.proj_writes { | ||
if let Some((root, hooks)) = data.hooks.get(proj_id) { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_post_write(root)?; | ||
} | ||
} | ||
|
||
let me = take(self); | ||
let prev_tag = data.prev_tag.to_string(); | ||
let last_commits = data.last_commits.clone(); | ||
let old_tags = data.old_tags.clone(); | ||
let prev_tag = args.prev_tag.to_string(); | ||
let last_commits = args.last_commits.clone(); | ||
let old_tags = args.old_tags.clone(); | ||
let mut commit_state = CommitState::new( | ||
me, | ||
did_write, | ||
prev_tag, | ||
last_commits, | ||
old_tags, | ||
data.advance_prev, | ||
args.advance_prev, | ||
repo.commit_config().clone() | ||
); | ||
|
||
if data.pause { | ||
if args.pause { | ||
let file = OpenOptions::new().create(true).write(true).truncate(true).open(".versio-paused")?; | ||
Ok(serde_json::to_writer(file, &commit_state)?) | ||
} else { | ||
commit_state.resume(repo) | ||
commit_state.resume(repo, args.into_resume_args()) | ||
} | ||
} | ||
} | ||
|
@@ -242,6 +254,18 @@ impl<'a> CommitArgs<'a> { | |
) -> CommitArgs<'a> { | ||
CommitArgs { prev_tag, last_commits, old_tags, advance_prev, hooks, pause } | ||
} | ||
|
||
pub fn into_resume_args(self) -> ResumeArgs<'a> { ResumeArgs { hooks: self.hooks } } | ||
} | ||
|
||
pub struct ResumeArgs<'a> { | ||
hooks: &'a HashMap<ProjectId, (Option<&'a String>, &'a HookSet)> | ||
} | ||
|
||
impl<'a> ResumeArgs<'a> { | ||
pub fn new(hooks: &'a HashMap<ProjectId, (Option<&'a String>, &'a HookSet)>) -> ResumeArgs<'a> { | ||
ResumeArgs { hooks } | ||
} | ||
} | ||
|
||
fn fill_from_old(old: &HashMap<ProjectId, String>, new_tags: &mut HashMap<ProjectId, String>) { | ||
|
@@ -274,14 +298,20 @@ impl CommitState { | |
|
||
pub fn commit_config(&self) -> &CommitConfig { &self.commit_config } | ||
|
||
pub fn resume(&mut self, repo: &Repo) -> Result<()> { | ||
pub fn resume(&mut self, repo: &Repo, args: ResumeArgs) -> Result<()> { | ||
if self.did_write { | ||
trace!("Wrote files, so committing."); | ||
repo.commit()?; | ||
} else { | ||
trace!("No files written, so not committing."); | ||
} | ||
|
||
for proj_id in &self.write.proj_writes { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_post_commit(root)?; | ||
} | ||
} | ||
|
||
for tag in &self.write.tag_head { | ||
repo.update_tag_head(tag)?; | ||
} | ||
|
@@ -298,7 +328,6 @@ impl CommitState { | |
} | ||
} | ||
self.write.tag_head_or_last.clear(); | ||
self.write.proj_writes.clear(); | ||
|
||
for (tag, oid) in &self.write.tag_commit { | ||
repo.update_tag(tag, oid)?; | ||
|
@@ -311,6 +340,20 @@ impl CommitState { | |
repo.update_tag_head_anno(&self.prev_tag, &msg)?; | ||
} | ||
|
||
for proj_id in &self.write.proj_writes { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_post_tag(root)?; | ||
} | ||
} | ||
|
||
for proj_id in &self.write.proj_writes { | ||
if let Some((root, hooks)) = args.hooks.get(proj_id) { | ||
hooks.execute_post_end(root)?; | ||
} | ||
} | ||
Comment on lines
+343
to
+353
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (see above) |
||
|
||
self.write.proj_writes.clear(); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
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.
Do we really need two hooks that are in the same place but different name?
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.
It does seem a little weird, but the idea is that the hooks are semantically different. Long term goal would be that the pre-begin hook runs on all projects on every release, whereas the pre-write hook runs only on projects that actually will write something. Also, if there later exists some hook-able action that occurs before write, the pre-write hook will come after that, but pre-begin will still come before it. Finally, because hooks are all run together, this will give a user to have some control over when their hooks will run in relation to each other.