handle merge commits

This commit is contained in:
Kiril Videlov 2024-05-30 16:23:40 +02:00
parent 7c06114ab2
commit 3af46b3cae
3 changed files with 41 additions and 2 deletions

View File

@ -1,6 +1,8 @@
<script lang="ts">
import BranchFilesHeader from './BranchFilesHeader.svelte';
import Button from './Button.svelte';
import FileListItem from './FileListItem.svelte';
import { copyToClipboard } from '$lib/utils/clipboard';
import { getContext } from '$lib/utils/context';
import { selectFilesInList } from '$lib/utils/selectFilesInList';
import { maybeMoveSelection } from '$lib/utils/selection';
@ -46,9 +48,28 @@
currentDisplayIndex += 1;
displayedFiles = [...displayedFiles, ...chunkedFiles[currentDisplayIndex]];
}
let mergeDiffCommand = 'git diff-tree --cc ';
</script>
<BranchFilesHeader {title} {files} {showCheckboxes} />
{#if !$commit?.isMergeCommit()}
<BranchFilesHeader {title} {files} {showCheckboxes} />
{:else}
<div
class="text-base-11"
style="padding-left: 1rem; padding-right: 1rem; color: var(--clr-scale-ntrl-50); "
>
<span style="font-style: italic;">Merge commit diff:</span>
<br />
<span style="font-family: monospace; user-select: text;"
>{mergeDiffCommand + $commit.id.slice(0, 7)}</span
>
<Button
size="tag"
icon="copy"
on:mousedown={() => copyToClipboard(mergeDiffCommand + $commit.id.slice(0, 7))}
></Button>
</div>
{/if}
{#each displayedFiles as file (file.id)}
<FileListItem
{file}

View File

@ -194,6 +194,10 @@ export class Commit {
isParentOf(possibleChild: Commit) {
return possibleChild.parentIds.includes(this.id);
}
isMergeCommit() {
return this.parentIds.length > 1;
}
}
export function isLocalCommit(obj: any): obj is Commit {
@ -208,6 +212,7 @@ export class RemoteCommit {
createdAt!: Date;
changeId!: string;
isSigned!: boolean;
parentIds!: string[];
parent?: RemoteCommit;
children?: RemoteCommit[];
@ -228,6 +233,10 @@ export class RemoteCommit {
get status(): CommitStatus {
return 'upstream';
}
isMergeCommit() {
return this.parentIds.length > 1;
}
}
export function isRemoteCommit(obj: any): obj is RemoteCommit {

View File

@ -1,4 +1,4 @@
use std::path::Path;
use std::{os::unix::process::parent_id, path::Path};
use anyhow::{Context, Result};
use bstr::BString;
@ -48,6 +48,7 @@ pub struct RemoteCommit {
pub created_at: u128,
pub author: Author,
pub change_id: Option<String>,
pub parent_ids: Vec<git::Oid>,
}
// for legacy purposes, this is still named "remote" branches, but it's actually
@ -187,12 +188,20 @@ pub fn branch_to_remote_branch_data(
}
pub fn commit_to_remote_commit(commit: &git2::Commit) -> RemoteCommit {
let parent_ids: Vec<git::Oid> = commit
.parents()
.map(|c| {
let c: git::Oid = c.id().into();
c
})
.collect::<Vec<_>>();
RemoteCommit {
id: commit.id().to_string(),
description: commit.message_bstr().to_owned(),
created_at: commit.time().seconds().try_into().unwrap(),
author: commit.author().into(),
change_id: commit.change_id(),
parent_ids,
}
}