Skip to content

Commit fd7972e

Browse files
authored
Add stash user command variable (#160)
* Add stash user command variable * Document stash user command variable * Fix refs user command documentation
1 parent 3590c67 commit fd7972e

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

docs/src/features/user-command.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ They will be replaced with their respective values command is executed.
6363
- The hashes of all parents of the selected commit, separated by a space.
6464
- example: `c103d9744df8ebf100773a11345f011152ec5581 a1b2c3d4e5f67890123456789abcdef0123456789`
6565
- `{{refs}}`
66-
- The names of all refs (branches, tags, stashes) pointing to the selected commit, separated by a space.
66+
- The names of all refs (branches, remote branches, tags) pointing to the selected commit, separated by a space.
6767
- example: `master v1.0.0`
6868
- `{{branches}}`
6969
- The names of all branches pointing to the selected commit, separated by a space.
@@ -74,6 +74,9 @@ They will be replaced with their respective values command is executed.
7474
- `{{tags}}`
7575
- The names of all tags pointing to the selected commit, separated by a space.
7676
- example: `v1.0.0 v1.0.1`
77+
- `{{stash}}`
78+
- The name of the stash when the selected commit is a stash commit. Otherwise, this is an empty string.
79+
- example: `stash@{0}`
7780
- `{{area_width}}`
7881
- Width of the user command display area (number of cells).
7982
- example: `80`

src/app.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,12 +786,16 @@ fn build_external_command_parameters<'a>(
786786
let mut branches = vec![];
787787
let mut remote_branches = vec![];
788788
let mut tags = vec![];
789+
let mut stash = None;
789790
for r in refs {
790791
match r {
791792
Ref::Tag { .. } => tags.push(r.name()),
792793
Ref::Branch { .. } => branches.push(r.name()),
793794
Ref::RemoteBranch { .. } => remote_branches.push(r.name()),
794-
Ref::Stash { .. } => continue, // skip stashes
795+
Ref::Stash { .. } => {
796+
stash = Some(r.name());
797+
continue; // skip stashes from {{refs}}
798+
}
795799
}
796800
all_refs.push(r.name());
797801
}
@@ -808,6 +812,7 @@ fn build_external_command_parameters<'a>(
808812
branches,
809813
remote_branches,
810814
tags,
815+
stash,
811816
area_width,
812817
area_height,
813818
})

src/external.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const USER_COMMAND_REFS_MARKER: &str = "{{refs}}";
1212
const USER_COMMAND_BRANCHES_MARKER: &str = "{{branches}}";
1313
const USER_COMMAND_REMOTE_BRANCHES_MARKER: &str = "{{remote_branches}}";
1414
const USER_COMMAND_TAGS_MARKER: &str = "{{tags}}";
15+
const USER_COMMAND_STASH_MARKER: &str = "{{stash}}";
1516
const USER_COMMAND_AREA_WIDTH_MARKER: &str = "{{area_width}}";
1617
const USER_COMMAND_AREA_HEIGHT_MARKER: &str = "{{area_height}}";
1718

@@ -78,6 +79,7 @@ pub struct ExternalCommandParameters<'a> {
7879
pub branches: Vec<&'a str>,
7980
pub remote_branches: Vec<&'a str>,
8081
pub tags: Vec<&'a str>,
82+
pub stash: Option<&'a str>,
8183
pub area_width: u16,
8284
pub area_height: u16,
8385
}
@@ -152,6 +154,7 @@ fn replace_command_arg(s: &str, params: &ExternalCommandParameters) -> String {
152154
let branches = &params.branches.join(sep);
153155
let remote_branches = &params.remote_branches.join(sep);
154156
let tags = &params.tags.join(sep);
157+
let stash = params.stash.unwrap_or_default();
155158
let area_width = &params.area_width.to_string();
156159
let area_height = &params.area_height.to_string();
157160

@@ -162,6 +165,7 @@ fn replace_command_arg(s: &str, params: &ExternalCommandParameters) -> String {
162165
.replace(USER_COMMAND_BRANCHES_MARKER, branches)
163166
.replace(USER_COMMAND_REMOTE_BRANCHES_MARKER, remote_branches)
164167
.replace(USER_COMMAND_TAGS_MARKER, tags)
168+
.replace(USER_COMMAND_STASH_MARKER, stash)
165169
.replace(USER_COMMAND_AREA_WIDTH_MARKER, area_width)
166170
.replace(USER_COMMAND_AREA_HEIGHT_MARKER, area_height)
167171
}

0 commit comments

Comments
 (0)