Skip to content

Commit 41e5b74

Browse files
authored
Merge pull request #5368 from gitbutlerapp/e-branch-1
Align gravatar images and refresh the branch listing on commit changes
2 parents 52fd4b1 + e688e35 commit 41e5b74

File tree

12 files changed

+80
-14
lines changed

12 files changed

+80
-14
lines changed

apps/desktop/src/lib/branches/branchListing.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ export class BranchListingService {
6363
return store;
6464
}
6565

66+
/**
67+
* Refresh the information for a particular branch.
68+
*
69+
* Will only fetch the information if the branch is already being tracked.
70+
*/
71+
refreshBranchListingDetails(branchName: string) {
72+
if (!this.branchListingDetails.has(branchName)) {
73+
return;
74+
}
75+
this.updateBranchListing(branchName);
76+
}
77+
6678
private accumulatedBranchListings: string[] = [];
6779
private updateBranchListingTimeout: ReturnType<typeof setTimeout> | undefined;
6880
// Accumulates multiple update calls
@@ -341,6 +353,8 @@ export class Author {
341353
name?: string | undefined;
342354
/** The email of the author as configured in the git config */
343355
email?: string | undefined;
356+
/** The gravatar id of the author */
357+
gravatarUrl?: string | undefined;
344358
}
345359

346360
/** Represents a fat struct with all the data associated with a branch */

apps/desktop/src/lib/commit/CommitCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
console.error('Unable to undo commit');
105105
return;
106106
}
107-
branchController.undoCommit(branch.id, commit.id);
107+
branchController.undoCommit(branch.id, branch.name, commit.id);
108108
}
109109
110110
let isUndoable = commit instanceof DetailedCommit && type !== 'remote';

apps/desktop/src/lib/commit/CommitDialog.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
try {
3434
await branchController.commitBranch(
3535
$branch.id,
36+
$branch.name,
3637
message.trim(),
3738
$selectedOwnership.toString(),
3839
$runCommitHooks

apps/desktop/src/lib/commit/StackingCommitCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
console.error('Unable to undo commit');
105105
return;
106106
}
107-
branchController.undoCommit(branch.id, commit.id);
107+
branchController.undoCommit(branch.id, branch.name, commit.id);
108108
}
109109
110110
let isUndoable = commit instanceof DetailedCommit && type !== 'remote';

apps/desktop/src/lib/navigation/BranchListingSidebarEntry.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
branchListingDetails.authors.map(async (author) => {
101101
return {
102102
name: author.name || unknownName,
103-
srcUrl: await gravatarUrlFromEmail(author.email || unknownEmail)
103+
srcUrl: author.gravatarUrl ?? (await gravatarUrlFromEmail(author.email || unknownEmail))
104104
};
105105
})
106106
);

apps/desktop/src/lib/vbranches/branchController.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { showError, showToast } from '$lib/notifications/toasts';
33
import * as toasts from '$lib/utils/toasts';
44
import posthog from 'posthog-js';
55
import type { BaseBranchService } from '$lib/baseBranch/baseBranchService';
6+
import type { BranchListingService } from '$lib/branches/branchListing';
67
import type { RemoteBranchService } from '$lib/stores/remoteBranches';
78
import type { BranchPushResult, ForgeIdentifier, Hunk, LocalFile, StackOrder } from './types';
89
import type { VirtualBranchService } from './virtualBranch';
@@ -14,7 +15,8 @@ export class BranchController {
1415
readonly projectId: string,
1516
readonly vbranchService: VirtualBranchService,
1617
readonly remoteBranchService: RemoteBranchService,
17-
readonly baseBranchService: BaseBranchService
18+
readonly baseBranchService: BaseBranchService,
19+
private readonly branchListingService: BranchListingService
1820
) {}
1921

2022
async setTarget(branch: string, pushRemote: string | undefined = undefined) {
@@ -51,19 +53,21 @@ export class BranchController {
5153
}
5254

5355
async commitBranch(
54-
branch: string,
56+
branchId: string,
57+
branchName: string,
5558
message: string,
5659
ownership: string | undefined = undefined,
5760
runHooks = false
5861
) {
5962
try {
6063
await invoke<void>('commit_virtual_branch', {
6164
projectId: this.projectId,
62-
branch,
65+
branch: branchId,
6366
message,
6467
ownership,
6568
runHooks: runHooks
6669
});
70+
this.branchListingService.refreshBranchListingDetails(branchName);
6771
posthog.capture('Commit Successful');
6872
} catch (err: any) {
6973
if (err.code === 'errors.commit.signing_failed') {
@@ -543,13 +547,14 @@ export class BranchController {
543547
}
544548
}
545549

546-
async undoCommit(branchId: string, commitOid: string) {
550+
async undoCommit(branchId: string, branchName: string, commitOid: string) {
547551
try {
548552
await invoke<void>('undo_commit', {
549553
projectId: this.projectId,
550554
branchId,
551555
commitOid
552556
});
557+
this.branchListingService.refreshBranchListingDetails(branchName);
553558
} catch (err: any) {
554559
showError('Failed to amend commit', err);
555560
}

apps/desktop/src/routes/[projectId]/+layout.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export const load: LayoutLoad = async ({ params, parent }) => {
7979
projectId,
8080
vbranchService,
8181
remoteBranchService,
82-
baseBranchService
82+
baseBranchService,
83+
branchListingService
8384
);
8485

8586
const branchDragActionsFactory = new BranchDragActionsFactory(branchController);

crates/gitbutler-branch-actions/src/author.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use bstr::ByteSlice;
12
use serde::Serialize;
23

4+
use crate::gravatar::gravatar_url_from_email;
5+
36
#[derive(Debug, Serialize, Hash, Clone, PartialEq, Eq)]
47
#[serde(rename_all = "camelCase")]
58
pub struct Author {
@@ -13,11 +16,7 @@ impl From<git2::Signature<'_>> for Author {
1316
let name = value.name().unwrap_or_default().to_string();
1417
let email = value.email().unwrap_or_default().to_string();
1518

16-
let gravatar_url = url::Url::parse(&format!(
17-
"https://www.gravatar.com/avatar/{:x}?s=100&r=g&d=retro",
18-
md5::compute(email.to_lowercase())
19-
))
20-
.unwrap();
19+
let gravatar_url = gravatar_url_from_email(email.as_str()).unwrap();
2120

2221
Author {
2322
name,
@@ -26,3 +25,15 @@ impl From<git2::Signature<'_>> for Author {
2625
}
2726
}
2827
}
28+
29+
impl From<gix::actor::SignatureRef<'_>> for Author {
30+
fn from(value: gix::actor::SignatureRef<'_>) -> Self {
31+
let gravatar_url = gravatar_url_from_email(&value.email.to_str_lossy()).unwrap();
32+
33+
Author {
34+
name: value.name.to_owned().to_string(),
35+
email: value.email.to_owned().to_string(),
36+
gravatar_url,
37+
}
38+
}
39+
}

crates/gitbutler-branch-actions/src/branch.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::gravatar::gravatar_url_from_email;
12
use crate::{RemoteBranchFile, VirtualBranchesExt};
23
use anyhow::{bail, Context, Result};
34
use bstr::{BStr, ByteSlice};
@@ -421,26 +422,48 @@ pub struct BranchListing {
421422

422423
/// Represents a "commit author" or "signature", based on the data from the git history
423424
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
425+
#[serde(rename_all = "camelCase")]
424426
pub struct Author {
425427
/// The name of the author as configured in the git config
426428
pub name: Option<BStringForFrontend>,
427429
/// The email of the author as configured in the git config
428430
pub email: Option<BStringForFrontend>,
431+
432+
pub gravatar_url: Option<BStringForFrontend>,
429433
}
430434

431435
impl From<git2::Signature<'_>> for Author {
432436
fn from(value: git2::Signature) -> Self {
433437
let name = value.name().map(str::to_string).map(Into::into);
434438
let email = value.email().map(str::to_string).map(Into::into);
435-
Author { name, email }
439+
440+
let gravatar_url = match value.email() {
441+
Some(email) => gravatar_url_from_email(email)
442+
.map(|url| url.as_ref().into())
443+
.ok(),
444+
None => None,
445+
};
446+
447+
Author {
448+
name,
449+
email,
450+
gravatar_url,
451+
}
436452
}
437453
}
438454

439455
impl From<gix::actor::SignatureRef<'_>> for Author {
440456
fn from(value: gix::actor::SignatureRef<'_>) -> Self {
457+
let gravatar_url = {
458+
gravatar_url_from_email(&value.email.to_str_lossy())
459+
.map(|url| url.as_ref().into())
460+
.ok()
461+
};
462+
441463
Author {
442464
name: Some(value.name.to_owned().into()),
443465
email: Some(value.email.to_owned().into()),
466+
gravatar_url,
444467
}
445468
}
446469
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use anyhow::Result;
2+
3+
pub fn gravatar_url_from_email(email: &str) -> Result<url::Url> {
4+
let gravatar_url = format!(
5+
"https://www.gravatar.com/avatar/{:x}?s=100&r=g&d=retro",
6+
md5::compute(email.to_lowercase())
7+
);
8+
url::Url::parse(gravatar_url.as_str()).map_err(Into::into)
9+
}

0 commit comments

Comments
 (0)