8000 crates/minimal: make `minimal update` update sideloads as well by twitchyliquid64 · Pull Request #172 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
46 changes: 45 additions & 1 deletion crates/minimal/src/cmd_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,35 @@ pub async fn cmd_update(_args: UpdateArgs, ctx: &mut Context) -> Result<(), Erro
None
};

// Update all the sideloads, if present
let sideload_updates = if let Some(sideloads) = mfile.upstream.as_ref().map(|u| u.sideloads()) {
sideloads
.iter()
.map(|s| match s.link() {
mfile::LinkConfig::Git {
repo,
branch: Some(branch),
locked_commit,
} => {
let new_git_ref = vcs
.checkout_of(repo, checkouts::GitRef::Branch(branch.clone()))?
.1;
if Some(&new_git_ref) != locked_commit.as_ref() {
Ok(Some(new_git_ref))
} else {
Ok(None)
}
}
_ => Ok(None),
})
.collect::<Result<Vec<_>, Error>>()?
} else {
vec![]
};

// If theres a minimal file on disk, and theres at least one new rev, write the revs to the minimal file.
if let Some(p) = mfile_path
&& new_up_rev.is_some()
&& (new_up_rev.is_some() || sideload_updates.iter().any(|u| u.is_some()))
{
use toml_edit::{DocumentMut, value};
let toml = std::fs::read_to_string(&p)
Expand All @@ -66,6 +92,24 @@ pub async fn cmd_update(_args: UpdateArgs, ctx: &mut Context) -> Result<(), Erro
)
}

// Update sideloads
for (i, rev) in sideload_updates.into_iter().enumerate() {
if let Some(new_rev) = rev {
println!(
"Sideload {}:{} updated from {} to {}",
doc["upstream"]["sideload"][i]["repo"].as_str().unwrap(),
doc["upstream"]["sideload"][i]["branch"].as_str().unwrap(),
doc["upstream"]["sideload"][i]
.get("locked_commit")
.and_then(|s| s.as_str())
.unwrap_or("<unpinned>"),
new_rev
);

doc["upstream"]["sideload"][i]["locked_commit"] = value(new_rev.clone());
Comment on lines +95 to +109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Honor both sideloads and legacy sideload keys when writing sideload pins.

mfile::Upstream accepts both TOML spellings, but Lines 100-109 always read and mutate doc["upstream"]["sideload"]. If a minimal.toml uses the canonical sideloads key, this block will update the wrong node and can panic on the unwrap()s even though the sideload was parsed successfully.

Suggested change
+        let sideload_key = if doc["upstream"].get("sideloads").is_some() {
+            "sideloads"
+        } else {
+            "sideload"
+        };
+
         // Update sideloads
         for (i, rev) in sideload_updates.into_iter().enumerate() {
             if let Some(new_rev) = rev {
                 println!(
                     "Sideload {}:{} updated from {} to {}",
-                    doc["upstream"]["sideload"][i]["repo"].as_str().unwrap(),
-                    doc["upstream"]["sideload"][i]["branch"].as_str().unwrap(),
-                    doc["upstream"]["sideload"][i]
+                    doc["upstream"][sideload_key][i]["repo"].as_str().unwrap(),
+                    doc["upstream"][sideload_key][i]["branch"].as_str().unwrap(),
+                    doc["upstream"][sideload_key][i]
                         .get("locked_commit")
                         .and_then(|s| s.as_str())
                         .unwrap_or("<unpinned>"),
                     new_rev
                 );
 
-                doc["upstream"]["sideload"][i]["locked_commit"] = value(new_rev.clone());
+                doc["upstream"][sideload_key][i]["locked_commit"] = value(new_rev.clone());
             }
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Update sideloads
for (i, rev) in sideload_updates.into_iter().enumerate() {
if let Some(new_rev) = rev {
println!(
"Sideload {}:{} updated from {} to {}",
doc["upstream"]["sideload"][i]["repo"].as_str().unwrap(),
doc["upstream"]["sideload"][i]["branch"].as_str().unwrap(),
doc["upstream"]["sideload"][i]
.get("locked_commit")
.and_then(|s| s.as_str())
.unwrap_or("<unpinned>"),
new_rev
);
doc["upstream"]["sideload"][i]["locked_commit"] = value(new_rev.clone());
let sideload_key = if doc["upstream"].get("sideloads").is_some() {
"sideloads"
} else {
"sideload"
};
// Update sideloads
for (i, rev) in sideload_updates.into_iter().enumerate() {
if let Some(new_rev) = rev {
println!(
"Sideload {}:{} updated from {} to {}",
doc["upstream"][sideload_key][i]["repo"].as_str().unwrap(),
doc["upstream"][sideload_key][i]["branch"].as_str().unwrap(),
doc["upstream"][sideload_key][i]
.get("locked_commit")
.and_then(|s| s.as_str())
.unwrap_or("<unpinned>"),
new_rev
);
doc["upstream"][sideload_key][i]["locked_commit"] = value(new_rev.clone());
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/minimal/src/cmd_update.rs` around lines 95 - 109, The loop that
updates sideload pins currently always reads/writes doc["upstream"]["sideload"],
which misses the canonical "sideloads" key and can panic; update the code in the
sideload_updates handling (the for (i, rev) in sideload_updates loop and the
place where doc["upstream"]["sideload"][i]["locked_commit"] is set) to first
choose the correct node: let key = if doc["upstream"].get("sideloads").is_some()
{ "sideloads" } else { "sideload" }; then use doc["upstream"][key][i] for
reads/prints and set the locked_commit on that selected node (and avoid direct
unwraps by using get()/and_then(|v| v.as_str()).unwrap_or("<unpinned>") as
already used).

}
}

std::fs::write(p, doc.to_string()).map_err(|e| Error::Other(anyhow::Error::from(e)))?;
}

Expand Down
0