Skip to content

Commit baf2bb1

Browse files
committed
Extract git preparation to a function
1 parent d00e4ec commit baf2bb1

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

src/cmd/shelf.rs

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -112,72 +112,23 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
112112
writeln!(summary, "- [Index](./{INDEX_MD_FILE})")?;
113113

114114
for sb in &shelf_config.books {
115-
if let Some(url) = &sb.git_url {
116-
println!("{:?}", sb);
117-
let path = sb.path.clone().unwrap_or("root".to_owned());
118-
let repo_raw_name = url.split('/').last().unwrap_or(&path);
119-
let repo_name = format!("{repo_raw_name}-{path}");
120-
let mut checkout_path = PathBuf::from(REPOS_DIR);
121-
checkout_path.push(repo_name);
122-
123-
let book_path = if let Some(path) = &sb.path {
124-
let mut bp = checkout_path.clone();
125-
bp.push(path);
126-
bp
127-
} else {
128-
checkout_path.clone()
129-
};
130-
131-
let repo = match git2::Repository::open(&checkout_path) {
132-
Ok(repo) => repo,
133-
Err(_) => match git2::Repository::clone(&url, &checkout_path) {
134-
Ok(repo) => repo,
135-
Err(e) => panic!("failed to clone: {}", e),
136-
},
137-
};
138-
139-
if let Some(refname) = &sb.git_ref {
140-
// branch or a tag (v0.1.1) or a commit (8e8128)
141-
let (object, reference) =
142-
if let Ok((object, reference)) = repo.revparse_ext(refname) {
143-
(object, reference)
144-
} else if let Ok((object, reference)) =
145-
repo.revparse_ext(&format!("origin/{refname}"))
146-
{
147-
(object, reference)
148-
} else {
149-
panic!("Could not checkout {refname}");
150-
};
151-
152-
repo.checkout_tree(&object, None)
153-
.expect("Failed to checkout");
154-
155-
match reference {
156-
// gref is an actual reference like branches or tags
157-
Some(gref) => repo.set_head(gref.name().unwrap()),
158-
// this is a commit, not a reference
159-
None => repo.set_head_detached(object.id()),
160-
}
161-
.expect("Failed to set HEAD");
162-
}
163-
164-
process_book(
165-
&book_path.to_str().unwrap(),
166-
&mut index_file,
167-
&mut summary,
168-
&shelf_source,
169-
&shelf_url,
170-
)?
115+
let book_path = if let Some(url) = &sb.git_url {
116+
prepare_git(sb, url)
171117
} else if let Some(path) = &sb.path {
118+
Some(path.to_owned())
119+
} else {
120+
warn!("Neither path or git specified. Invalid book");
121+
None
122+
};
123+
124+
if let Some(path) = book_path {
172125
process_book(
173-
path,
126+
&path,
174127
&mut index_file,
175128
&mut summary,
176129
&shelf_source,
177130
&shelf_url,
178131
)?
179-
} else {
180-
warn!("Neither path or git specified. Invalid book");
181132
}
182133
}
183134

@@ -187,6 +138,57 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
187138
Ok(())
188139
}
189140

141+
fn prepare_git(sb: &mdbook::config::ShelfBook, url: &String) -> Option<String> {
142+
println!("{:?}", sb);
143+
144+
// Prepare checkout directory name
145+
let path = sb.path.clone().unwrap_or("root".to_owned());
146+
let repo_raw_name = url.split('/').last().unwrap_or(&path);
147+
let repo_name = format!("{repo_raw_name}-{path}");
148+
let mut checkout_path = PathBuf::from(REPOS_DIR);
149+
checkout_path.push(repo_name);
150+
151+
let book_path = if let Some(path) = &sb.path {
152+
let mut bp = checkout_path.clone();
153+
bp.push(path);
154+
bp
155+
} else {
156+
checkout_path.clone()
157+
};
158+
159+
let repo = match git2::Repository::open(&checkout_path) {
160+
Ok(repo) => repo,
161+
Err(_) => match git2::Repository::clone(&url, &checkout_path) {
162+
Ok(repo) => repo,
163+
Err(e) => panic!("failed to clone: {}", e),
164+
},
165+
};
166+
167+
if let Some(refname) = &sb.git_ref {
168+
// branch or a tag (v0.1.1) or a commit (8e8128)
169+
let (object, reference) = if let Ok((object, reference)) = repo.revparse_ext(refname) {
170+
(object, reference)
171+
} else if let Ok((object, reference)) = repo.revparse_ext(&format!("origin/{refname}")) {
172+
(object, reference)
173+
} else {
174+
panic!("Could not checkout {refname}");
175+
};
176+
177+
repo.checkout_tree(&object, None)
178+
.expect("Failed to checkout");
179+
180+
match reference {
181+
// gref is an actual reference like branches or tags
182+
Some(gref) => repo.set_head(gref.name().unwrap()),
183+
// this is a commit, not a reference
184+
None => repo.set_head_detached(object.id()),
185+
}
186+
.expect("Failed to set HEAD");
187+
}
188+
189+
Some(book_path.to_str().unwrap().to_owned())
190+
}
191+
190192
#[test]
191193
fn test_parse_toml() {
192194
let toml = r#"

0 commit comments

Comments
 (0)