Skip to content

Commit

Permalink
fix(spm): install from annotated tag (#4120)
Browse files Browse the repository at this point in the history
See #4042

Installs using spm backend works with some repos, but fails for
other. For example
`mise install spm:nicklockwood/[email protected]` - ok
`mise install spm:realm/[email protected]` - fails:

```
ERROR git failed: Cmd(["git", "-C", "/home/vscode/.cache/mise/spm-realm-swift-lint/0.57.1/repo", "-c", "safe.directory=/home/vscode/.cache/mise/spm-realm-swift-lint/0.57.1/repo", "fetch", "--prune", "--update-head-ok", "origin", "0.57.1:0.57.1"]) error: cannot update ref 'refs/heads/0.57.1': trying to write non-commit object bf3f358f09ca74dc7f031ed44f185fd7a874efe4 to branch 'refs/heads/0.57.1'
```

Looks like it does not work with annotated tags:
```sh
swiftlint (main)> git cat-file -t 0.57.1
tag

SwiftFormat (main)> git cat-file -t 0.54.3
commit
```

The command:
```sh
swiftlint (main)> git fetch --prune --update-head-ok origin '0.57.1:0.57.1'
error: cannot update ref 'refs/heads/0.57.1': trying to write non-commit object bf3f358f09ca74dc7f031ed44f185fd7a874efe4 to branch 'refs/heads/0.57.1'
From github.com:realm/SwiftLint
 ! [new tag]             0.57.1     -> 0.57.1  (unable to update local ref)
```

is trying to create branch from annotated tag and fails.
For tags we could use:
```
git fetch --prune --update-head-ok origin 'refs/tags/0.57.1:refs/tags/0.57.1'
or
git fetch --prune --update-head-ok origin tag 0.57.1
```
  • Loading branch information
MontakOleg authored Jan 16, 2025
1 parent c36cba7 commit ca0fcee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/backend/spm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl SPMBackend {
repo.clone(package_repo.url.as_str(), Some(&ctx.pr))?;
}
debug!("Checking out revision: {revision}");
repo.update(Some(revision.to_string()))?;
repo.update_tag(revision.to_string())?;

Ok(repo.dir)
}
Expand Down
16 changes: 15 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ impl Git {

pub fn update(&self, gitref: Option<String>) -> Result<(String, String)> {
let gitref = gitref.map_or_else(|| self.current_branch(), Ok)?;
self.update_ref(gitref, false)
}

pub fn update_tag(&self, gitref: String) -> Result<(String, String)> {
self.update_ref(gitref, true)
}

fn update_ref(&self, gitref: String, is_tag_ref: bool) -> Result<(String, String)> {
debug!("updating {} to {}", self.dir.display(), gitref);
// if SETTINGS.libgit2 {
// if let Ok(repo) = self.repo() {
Expand All @@ -113,13 +121,19 @@ impl Git {
Err(err) => Err(eyre!("git failed: {cmd:?} {err:#}")),
};
debug!("updating {} to {} with git", self.dir.display(), gitref);

let refspec = if is_tag_ref {
format!("refs/tags/{}:refs/tags/{}", gitref, gitref)
} else {
format!("{}:{}", gitref, gitref)
};
exec(git_cmd!(
&self.dir,
"fetch",
"--prune",
"--update-head-ok",
"origin",
&format!("{}:{}", gitref, gitref),
&refspec
))?;
let prev_rev = self.current_sha()?;
exec(git_cmd!(
Expand Down

0 comments on commit ca0fcee

Please sign in to comment.