Merge pull request #1508 from gitbutlerapp/add-test-for-dirty-function

GB-626
This commit is contained in:
Nikita Galaiko 2023-11-03 14:47:38 +01:00 committed by GitHub
commit f00aa38677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 7 deletions

View File

@ -89,8 +89,40 @@ impl App {
project_id: &ProjectId,
earliest_timestamp_ms: Option<u128>,
) -> Result<Vec<sessions::Session>> {
let sessions = self
.sessions_database
.list_by_project_id(project_id, earliest_timestamp_ms)?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,
user.as_ref(),
)?;
// this is a hack to account for a case when we have a session created, but fs was never
// touched, so the wathcer never picked up the session
let current_session = gb_repository.get_current_session()?;
let have_to_index = match (current_session.as_ref(), sessions.first()) {
(Some(real), Some(from_db)) => !real.eq(from_db),
(Some(_), None) => true,
_ => false,
};
if !have_to_index {
return Ok(sessions);
}
let sessions_iter = gb_repository.get_sessions_iterator()?;
let mut sessions = sessions_iter.collect::<Result<Vec<_>, _>>()?;
self.sessions_database
.list_by_project_id(project_id, earliest_timestamp_ms)
.insert(project_id, &sessions.iter().collect::<Vec<_>>())?;
if let Some(session) = current_session {
self.sessions_database.insert(project_id, &[&session])?;
sessions.insert(0, session);
}
Ok(sessions)
}
pub fn list_session_files(

View File

@ -2001,3 +2001,31 @@ mod amend {
}
}
}
mod init {
use super::*;
#[tokio::test]
async fn dirty() {
let Test {
repository,
project_id,
controller,
..
} = Test::default();
fs::write(repository.path().join("file.txt"), "content").unwrap();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(branches[0].files[0].hunks.len(), 1);
}
}

View File

@ -1,12 +1,13 @@
import { Session, listSessions, subscribeToSessions } from '$lib/backend/sessions';
import { asyncWritable, get, type Loadable, type WritableLoadable } from '@square/svelte-store';
import type { CustomStore } from '$lib/vbranches/types';
import { asyncWritable, get } from '@square/svelte-store';
export function getSessionStore(projectId: string): Loadable<Session[]> {
export function getSessionStore(projectId: string): CustomStore<Session[]> {
const store = asyncWritable(
[],
async () => await listSessions(projectId),
async (data) => data,
{ trackState: true },
{ reloadable: true, trackState: true },
(set) => {
const unsubscribe = subscribeToSessions(projectId, (session) => {
const oldValue = get(store)?.filter((b) => b.id != session.id);
@ -21,6 +22,6 @@ export function getSessionStore(projectId: string): Loadable<Session[]> {
});
return () => unsubscribe();
}
) as WritableLoadable<Session[]>;
) as CustomStore<Session[]>;
return store;
}

View File

@ -8,22 +8,26 @@ import type {
} from './types';
import * as toasts from '$lib/utils/toasts';
import { invoke } from '$lib/backend/ipc';
import type { Session } from '$lib/backend/sessions';
export class BranchController {
constructor(
readonly projectId: string,
readonly virtualBranchStore: VirtualBranchStore<Branch>,
readonly remoteBranchStore: CustomStore<RemoteBranch[] | undefined>,
readonly targetBranchStore: CustomStore<BaseBranch | undefined>
readonly targetBranchStore: CustomStore<BaseBranch | undefined>,
readonly sessionsStore: CustomStore<Session[] | undefined>
) {}
async setTarget(branch: string) {
try {
await invoke<BaseBranch>('set_base_branch', { projectId: this.projectId, branch });
await this.sessionsStore.reload();
await this.targetBranchStore.reload();
// TODO: Reloading seems to trigger 4 invocations of `list_virtual_branches`
await this.virtualBranchStore.reload();
} catch (err) {
console.error(err);
toasts.error('Failed to set base branch');
}
}

View File

@ -41,7 +41,8 @@ export const load: LayoutLoad = async ({ params }) => {
projectId,
vbranchStore,
remoteBranchStore,
baseBranchStore
baseBranchStore,
sessionsStore
);
const githubContextStore = getGitHubContextStore(userStore, baseBranchStore);