Skip to content

Commit

Permalink
feat: discard changes support for SourceControlToolbarBottom (#1480)
Browse files Browse the repository at this point in the history
* feat: `SourceControlToolbarBottom` discard changes dialog

* feat: discard all changes support for `GitClient`

* fix: swiftlint

* Update SourceControlToolbarBottom.swift

* fix: use `Discard All Changes` text
  • Loading branch information
EstebanBorai authored Nov 20, 2023
1 parent 72833bc commit da2350f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CodeEdit/Features/Git/Client/GitClient+Status.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ extension GitClient {
func discardChanges(for file: URL) async throws {
_ = try await run("restore \(file.relativePath)")
}

/// Discard unstaged changes
func discardAllChanges() async throws {
_ = try await run("restore .")
}
}
13 changes: 13 additions & 0 deletions CodeEdit/Features/Git/SourceControlManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ final class SourceControlManager: ObservableObject {
}
}

/// Discard changes for repository
func discardAllChanges() {
Task {
do {
try await gitClient.discardAllChanges()
// TODO: Refresh content of active and unmodified document,
// requires CodeEditTextView changes
} catch {
await showAlertForError(title: "Failed to discard changes", error: error)
}
}
}

/// Commit files selected by user
func commit(message: String) async throws {
var filesToCommit: [CEWorkspaceFile] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import SwiftUI

struct SourceControlToolbarBottom: View {
@EnvironmentObject private var workspace: WorkspaceDocument

var body: some View {
HStack(spacing: 0) {
sourceControlMenu
Expand All @@ -23,8 +25,11 @@ struct SourceControlToolbarBottom: View {

private var sourceControlMenu: some View {
Menu {
Button("Discard Changes...") {}
.disabled(true) // TODO: Implementation Needed
Button("Discard All Changes...") {
if discardChangesDialog() {
workspace.sourceControlManager?.discardAllChanges()
}
}
Button("Stash Changes...") {}
.disabled(true) // TODO: Implementation Needed
Button("Commit...") {}
Expand All @@ -38,4 +43,17 @@ struct SourceControlToolbarBottom: View {
.menuIndicator(.hidden)
.frame(maxWidth: 30)
}

/// Renders a Discard Changes Dialog
func discardChangesDialog() -> Bool {
let alert = NSAlert()

alert.messageText = "Do you want to discard all uncommitted, local changes?"
alert.informativeText = "This operation cannot be undone."
alert.alertStyle = .warning
alert.addButton(withTitle: "Discard")
alert.addButton(withTitle: "Cancel")

return alert.runModal() == .alertFirstButtonReturn
}
}

0 comments on commit da2350f

Please sign in to comment.