Skip to content

Commit 200da1d

Browse files
committed
Quickly implement filtering with a half-baked test
The isn't complete, but let's trust we can implement the filter correctly, once more.
1 parent 863e157 commit 200da1d

File tree

4 files changed

+298
-3
lines changed

4 files changed

+298
-3
lines changed

crates/but-api/src/commands/workspace.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ pub fn head_info(project_id: ProjectId) -> Result<but_workspace::ui::RefInfo, Er
4242
},
4343
)
4444
.map_err(Into::into)
45-
.and_then(|info| but_workspace::ui::RefInfo::for_ui(info, &repo).map_err(Into::into))
45+
.and_then(|info| {
46+
but_workspace::ui::RefInfo::for_ui(info, &repo)
47+
.map(|ref_info| ref_info.pruned_to_entrypoint())
48+
.map_err(Into::into)
49+
})
4650
}
4751

4852
#[api_cmd]

crates/but-server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ async fn handle_command(
165165
"assign_hunk" => diff::assign_hunk_cmd(request.params),
166166
// Workspace commands
167167
"stacks" => workspace::stacks_cmd(request.params),
168+
"head_info" => workspace::head_info_cmd(request.params),
168169
#[cfg(unix)]
169170
"show_graph_svg" => workspace::show_graph_svg_cmd(request.params),
170171
"stack_details" => workspace::stack_details_cmd(request.params),

crates/but-workspace/src/ui/ref_info.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ pub(crate) mod inner {
148148
/// The workspace represents what `HEAD` is pointing to.
149149
pub is_entrypoint: bool,
150150
}
151+
152+
impl RefInfo {
153+
/// Make sure only the stack and segment that is the entrypoint remains.
154+
pub fn pruned_to_entrypoint(mut self) -> Self {
155+
if self.is_entrypoint {
156+
return self;
157+
}
158+
self.stacks
159+
.retain(|s| s.segments.iter().any(|s| s.is_entrypoint));
160+
if let Some(only_stack) = self.stacks.first_mut() {
161+
let mut found_entrypoint = false;
162+
only_stack.segments.retain(|s| {
163+
found_entrypoint |= s.is_entrypoint;
164+
found_entrypoint
165+
})
166+
}
167+
self
168+
}
169+
}
151170
}
152171

153172
impl inner::RefInfo {

crates/but-workspace/tests/workspace/ui.rs

Lines changed: 273 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ mod changes_in_branch {
137137
"passing strange ref-names still causes an error - they must exist"
138138
);
139139

140-
let ref_info = ui::RefInfo::for_ui(
140+
let mut ref_info = ui::RefInfo::for_ui(
141141
but_workspace::head_info(&repo, &*meta, Default::default())?,
142142
&repo,
143-
)?;
143+
)?
144+
.pruned_to_entrypoint();
144145
insta::assert_json_snapshot!(&ref_info, @r#"
145146
{
146147
"workspaceRef": {
@@ -307,6 +308,276 @@ mod changes_in_branch {
307308
"isEntrypoint": true
308309
}
309310
"#);
311+
312+
// Forcefully set another entrypoint to simulate the real deal.
313+
ref_info.is_entrypoint = false;
314+
ref_info
315+
.stacks
316+
.push(ref_info.stacks.first().unwrap().clone());
317+
ref_info
318+
.stacks
319+
.first_mut()
320+
.unwrap()
321+
.segments
322+
.first_mut()
323+
.unwrap()
324+
.is_entrypoint = true;
325+
ref_info = ref_info.pruned_to_entrypoint();
326+
// only one entrypoint, despite having two.
327+
insta::assert_json_snapshot!(&ref_info, @r#"
328+
{
329+
"workspaceRef": {
330+
"fullNameBytes": [
331+
114,
332+
101,
333+
102,
334+
115,
335+
47,
336+
104,
337+
101,
338+
97,
339+
100,
340+
115,
341+
47,
342+
103,
343+
105,
344+
116,
345+
98,
346+
117,
347+
116,
348+
108,
349+
101,
350+
114,
351+
47,
352+
119,
353+
111,
354+
114,
355+
107,
356+
115,
357+
112,
358+
97,
359+
99,
360+
101
361+
],
362+
"displayName": "gitbutler/workspace"
363+
},
364+
"stacks": [
365+
{
366+
"id": null,
367+
"base": "c166d42d4ef2e5e742d33554d03805cfb0b24d11",
368+
"segments": [
369+
{
370+
"refName": {
371+
"fullNameBytes": [
372+
114,
373+
101,
374+
102,
375+
115,
376+
47,
377+
104,
378+
101,
379+
97,
380+
100,
381+
115,
382+
47,
383+
65
384+
],
385+
"displayName": "A"
386+
},
387+
"remoteTrackingRefName": {
388+
"fullNameBytes": [
389+
114,
390+
101,
391+
102,
392+
115,
393+
47,
394+
114,
395+
101,
396+
109,
397+
111,
398+
116,
399+
101,
400+
115,
401+
47,
402+
111,
403+
114,
404+
105,
405+
103,
406+
105,
407+
110,
408+
47,
409+
65
410+
],
411+
"displayName": "A",
412+
"remoteName": "origin"
413+
},
414+
"commits": [
415+
{
416+
"id": "d79bba960b112dbd25d45921c47eeda22288022b",
417+
"parentIds": [
418+
"c166d42d4ef2e5e742d33554d03805cfb0b24d11"
419+
],
420+
"message": "new file in A\n",
421+
"hasConflicts": false,
422+
"state": {
423+
"type": "LocalAndRemote",
424+
"subject": "d79bba960b112dbd25d45921c47eeda22288022b"
425+
},
426+
"createdAt": 946684800000,
427+
"author": {
428+
"name": "author",
429+
"email": "[email protected]",
430+
"gravatarUrl": "https://www.gravatar.com/avatar/5c1e6d6e64e12aca17657581a48005d1?s=100&r=g&d=retro"
431+
}
432+
}
433+
],
434+
"commitsOnRemote": [
435+
{
436+
"id": "89cc2d303514654e9cab2d05b9af08b420a740c1",
437+
"message": "change in A\n",
438+
"createdAt": 946684800000,
439+
"author": {
440+
"name": "author",
441+
"email": "[email protected]",
442+
"gravatarUrl": "https://www.gravatar.com/avatar/5c1e6d6e64e12aca17657581a48005d1?s=100&r=g&d=retro"
443+
}
444+
}
445+
],
446+
"commitsOutside": null,
447+
"metadata": null,
448+
"isEntrypoint": true,
449+
"pushStatus": "unpushedCommitsRequiringForce",
450+
"base": "c166d42d4ef2e5e742d33554d03805cfb0b24d11"
451+
}
452+
]
453+
}
454+
],
455+
"target": {
456+
"remoteTrackingRef": {
457+
"fullNameBytes": [
458+
114,
459+
101,
460+
102,
461+
115,
462+
47,
463+
114,
464+
101,
465+
109,
466+
111,
467+
116,
468+
101,
469+
115,
470+
47,
471+
111,
472+
114,
473+
105,
474+
103,
475+
105,
476+
110,
477+
47,
478+
109,
479+
97,
480+
105,
481+
110
482+
],
483+
"displayName": "main",
484+
"remoteName": "origin"
485+
},
486+
"commitsAhead": 0
487+
},
488+
"isManagedRef": true,
489+
"isManagedCommit": true,
490+
"isEntrypoint": false
491+
}
492+
"#);
493+
494+
ref_info
495+
.stacks
496+
.first_mut()
497+
.unwrap()
498+
.segments
499+
.first_mut()
500+
.unwrap()
501+
.is_entrypoint = false;
502+
503+
// it's Ok to have no entrypoint (even though it's not happening in practice)
504+
ref_info = ref_info.pruned_to_entrypoint();
505+
insta::assert_json_snapshot!(&ref_info, @r#"
506+
{
507+
"workspaceRef": {
508+
"fullNameBytes": [
509+
114,
510+
101,
511+
102,
512+
115,
513+
47,
514+
104,
515+
101,
516+
97,
517+
100,
518+
115,
519+
47,
520+
103,
521+
105,
522+
116,
523+
98,
524+
117,
525+
116,
526+
108,
527+
101,
528+
114,
529+
47,
530+
119,
531+
111,
532+
114,
533+
107,
534+
115,
535+
112,
536+
97,
537+
99,
538+
101
539+
],
540+
"displayName": "gitbutler/workspace"
541+
},
542+
"stacks": [],
543+
"target": {
544+
"remoteTrackingRef": {
545+
"fullNameBytes": [
546+
114,
547+
101,
548+
102,
549+
115,
550+
47,
551+
114,
552+
101,
553+
109,
554+
111,
555+
116,
556+
101,
557+
115,
558+
47,
559+
111,
560+
114,
561+
105,
562+
103,
563+
105,
564+
110,
565+
47,
566+
109,
567+
97,
568+
105,
569+
110
570+
],
571+
"displayName": "main",
572+
"remoteName": "origin"
573+
},
574+
"commitsAhead": 0
575+
},
576+
"isManagedRef": true,
577+
"isManagedCommit": true,
578+
"isEntrypoint": false
579+
}
580+
"#);
310581
Ok(())
311582
}
312583
}

0 commit comments

Comments
 (0)