Repository navigation
chore: conflicted bookmarks 棚卸し + push 前 bookmark 自動前進 #49
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
Merged
Merged
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 hidden or 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 hidden or 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,5 +1,6 @@ | ||
| mod diff; | ||
| mod push; | ||
| mod push_jj_bookmark; | ||
| mod quality_gate; | ||
| mod takt; | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,196 @@ | ||
| use std::process::Command; | ||
|
|
||
| use crate::log::{log_info, log_stage}; | ||
|
|
||
| pub(super) fn advance_jj_bookmarks() -> Result<(), String> { | ||
| let target = match determine_target_revision()? { | ||
| Some(rev) => rev, | ||
| None => return Ok(()), // root commit 等で有効な target がない | ||
| }; | ||
| let bookmarks = get_bookmarks_in_range(&target)?; | ||
|
|
||
| if bookmarks.is_empty() { | ||
| return Ok(()); // 前進対象なし (新規ブランチ等) | ||
| } | ||
|
|
||
| for bookmark in &bookmarks { | ||
| match set_bookmark(bookmark, &target) { | ||
| Ok(()) => log_stage( | ||
| "push", | ||
| &format!("bookmark '{}' を {} に自動更新", bookmark, target), | ||
| ), | ||
| Err(e) => { | ||
| log_info(&format!("bookmark '{}' の更新失敗 (続行): {}", bookmark, e)); | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn determine_target_revision() -> Result<Option<String>, String> { | ||
| let output = run_jj_log("@", "if(empty, \"empty\", \"content\")")?; | ||
| if output.trim() == "empty" { | ||
| match run_jj_log("@-", "commit_id") { | ||
| Ok(_) => Ok(Some("@-".to_string())), | ||
| Err(_) => { | ||
| log_info("@ が root commit のため bookmark 自動更新をスキップします"); | ||
| Ok(None) | ||
| } | ||
| } | ||
| } else { | ||
| Ok(Some("@".to_string())) | ||
| } | ||
| } | ||
|
|
||
| fn get_bookmarks_in_range(target: &str) -> Result<Vec<String>, String> { | ||
| let revsets = [ | ||
| format!("(trunk()..{}) & bookmarks()", target), | ||
| format!("(main..{}) & bookmarks()", target), | ||
| format!("(master..{}) & bookmarks()", target), | ||
| ]; | ||
|
|
||
| for revset in &revsets { | ||
| let template = "local_bookmarks.map(|b| b.name()).join(\",\") ++ \"\\n\""; | ||
| match run_jj_log(revset, template) { | ||
| Ok(output) => { | ||
| let bookmarks = parse_bookmarks_from_template(&output); | ||
| if !bookmarks.is_empty() { | ||
| return Ok(dedup(bookmarks)); | ||
| } | ||
| } | ||
| Err(_) => continue, | ||
| } | ||
| } | ||
|
|
||
| // (push 自体は続行するので Err を返さず警告に留める) | ||
| log_info("trunk/main/master bookmark が見つからず、bookmark 自動更新をスキップします"); | ||
| Ok(Vec::new()) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| fn parse_bookmarks_from_template(raw: &str) -> Vec<String> { | ||
| raw.lines() | ||
| .flat_map(|line| line.split(',')) | ||
| .map(|s| s.trim().to_string()) | ||
| .filter(|s| !s.is_empty()) | ||
| .collect() | ||
| } | ||
|
|
||
| fn run_jj(args: &[&str], error_prefix: &str) -> Result<String, String> { | ||
| let output = Command::new("jj") | ||
| .args(args) | ||
| .output() | ||
| .map_err(|e| format!("{}: {}", error_prefix, e))?; | ||
| if output.status.success() { | ||
| Ok(String::from_utf8_lossy(&output.stdout).to_string()) | ||
| } else { | ||
| Err(String::from_utf8_lossy(&output.stderr).trim().to_string()) | ||
| } | ||
| } | ||
|
|
||
| fn set_bookmark(name: &str, target: &str) -> Result<(), String> { | ||
| run_jj( | ||
| &["bookmark", "set", "-r", target, "--", name], | ||
| "jj bookmark set 実行失敗", | ||
| )?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn run_jj_log(revset: &str, template: &str) -> Result<String, String> { | ||
| run_jj( | ||
| &["log", "-r", revset, "--no-graph", "-T", template], | ||
| "jj log 実行失敗", | ||
| ) | ||
| } | ||
|
|
||
| fn dedup(items: Vec<String>) -> Vec<String> { | ||
| let mut seen = std::collections::HashSet::new(); | ||
| items | ||
| .into_iter() | ||
| .filter(|s| seen.insert(s.clone())) | ||
| .collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| // --- dedup --- | ||
|
|
||
| #[test] | ||
| fn dedup_preserves_order_and_removes_duplicates() { | ||
| let input = vec![ | ||
| "a".to_string(), | ||
| "b".to_string(), | ||
| "a".to_string(), | ||
| "c".to_string(), | ||
| "b".to_string(), | ||
| ]; | ||
| assert_eq!(dedup(input), vec!["a", "b", "c"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dedup_empty_returns_empty() { | ||
| assert_eq!(dedup(Vec::new()), Vec::<String>::new()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dedup_single_unchanged() { | ||
| assert_eq!(dedup(vec!["x".to_string()]), vec!["x"]); | ||
| } | ||
|
|
||
| // --- parse_bookmarks_from_template --- | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_empty_string_returns_empty() { | ||
| assert_eq!(parse_bookmarks_from_template(""), Vec::<String>::new()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_single_name() { | ||
| assert_eq!( | ||
| parse_bookmarks_from_template("main\n"), | ||
| vec!["main".to_string()] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_comma_separated_single_line() { | ||
| assert_eq!( | ||
| parse_bookmarks_from_template("feat/foo,feat/bar,fix/baz\n"), | ||
| vec!["feat/foo", "feat/bar", "fix/baz"] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_multi_line_output() { | ||
| let raw = "feat/a,feat/b\nfeat/c\n"; | ||
| assert_eq!( | ||
| parse_bookmarks_from_template(raw), | ||
| vec!["feat/a", "feat/b", "feat/c"] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_strips_leading_trailing_whitespace() { | ||
| assert_eq!( | ||
| parse_bookmarks_from_template(" main , dev \n"), | ||
| vec!["main", "dev"] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_filters_whitespace_only_entries() { | ||
| assert_eq!( | ||
| parse_bookmarks_from_template(", ,feat/x,\n"), | ||
| vec!["feat/x".to_string()] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bookmarks_with_duplicates_returned_as_is() { | ||
| assert_eq!( | ||
| parse_bookmarks_from_template("a,a,b\n"), | ||
| vec!["a", "a", "b"] | ||
| ); | ||
| } | ||
| } | ||
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.
🧩 Analysis chain
🌐 Web query:
jj revset functions: ismain()ormaster()a built-in revset function, or onlytrunk()?💡 Result:
Only trunk is a built-in revset function (specifically, a built-in alias) in Jujutsu (jj). Neither main nor master is a built-in revset function or alias. The trunk alias is defined in the default revsets.toml configuration as latest(remote_bookmarks(exact:"main", exact:"origin") | remote_bookmarks(exact:"master", exact:"origin") | ... | root), which tries remote bookmarks named "main", "master", or "trunk" on "origin" or "upstream" remotes, falling back to root. It is explicitly listed under "Built-in Aliases" in the official documentation, alongside others like mutable and visible. The comprehensive lists of functions and built-in aliases in the revset documentation do not include main or master.
Citations:
🏁 Script executed:
fd -t f "push_jj_bookmark.rs" --exec cat -n {}Repository: aloekun/claude-code-hook-test
Length of output: 6402
main()/master()は jj の revset 関数ではありません。修正が必要です。Web 検索で確認したところ、jj に組み込みの revset 関数は
trunk()のみで、main()やmaster()という関数は存在しません。したがって(main()..{target}) & bookmarks()と(master()..{target}) & bookmarks()は常に revset エラーで失敗し、trunk()が未設定な環境では自動更新が常にスキップされます。bookmark 名を直接参照する構文に修正してください:
修正パッチ
let revsets = [ format!("(trunk()..{}) & bookmarks()", target), - format!("(main()..{}) & bookmarks()", target), - format!("(master()..{}) & bookmarks()", target), + format!("(main..{}) & bookmarks()", target), + format!("(master..{}) & bookmarks()", target), ];🤖 Prompt for AI Agents