Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ Agent Watch at the same path when updating.

**Protect Git commands** turns checks on or off for all connected profiles. Turning them off
requires confirmation and keeps the hooks and rules. Under **Branch rules**, select **Push**,
**Force Push**, or **Delete Remote Branch**. A repository can require a stricter check than
the shared rule. Use **Preview a Branch** to see which check applies.
**Force Push**, or **Delete Remote Branch**. In **All repositories**, edit **Default** or
**Protected branches**, then confirm the change. A repository can require a stricter check.
Use **Preview a Branch** to see which check applies.

In **Settings… → Working Copies**, scan a repository to see its working copies, largest first.
Expand a copy to see local changes, build folders and what Agent Watch can safely move to the
Expand Down
123 changes: 72 additions & 51 deletions Sources/AgentWatchApp/GitProtectionPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ struct GitProtectionPane: View {
let index: Int?
let count: Int
}
private struct DefaultSelection: Identifiable {
let rule: GitProtectionRules.DefaultRule
let operation: GitBranchOperation
let level: GitCheckLevel
var id: GitProtectionRules.DefaultRule { rule }
}

@State private var rules: GitProtectionRules?
@State private var settingsError: String?
Expand All @@ -39,6 +45,7 @@ struct GitProtectionPane: View {
@State private var readingPreview = false
@State private var confirmsLegacyRemoval = false
@State private var editor: EditorSelection?
@State private var defaultEditor: DefaultSelection?

private var enabled: Bool { rules?.enabled ?? true }
private var activeRepository: GitProtectionRepository? { scope == .repository ? rulesRepository : nil }
Expand All @@ -50,7 +57,7 @@ struct GitProtectionPane: View {
return rules?.global[operation] ?? []
}
private var canAddRule: Bool {
rules != nil && operation != .push && (scope == .all || activeRepository != nil)
rules != nil && (scope == .all || activeRepository != nil)
}

private var previewRequest: PreviewRequest { PreviewRequest(repository: previewRepository, branch: branch) }
Expand All @@ -66,7 +73,6 @@ struct GitProtectionPane: View {
}
.task { loadRules() }
.task(id: previewRequest) { await readPreview(previewRequest) }
.onChange(of: operation) { if operation == .push { scope = .all } }
.fileImporter(isPresented: $choosingFolder, allowedContentTypes: [.folder], onCompletion: acceptFolder)
.sheet(item: $editor) { selection in
GitProtectionRuleEditor(
Expand All @@ -78,6 +84,16 @@ struct GitProtectionPane: View {
change: { try edit(selection, change: $0) }
)
}
.sheet(item: $defaultEditor) { selection in
GitProtectionDefaultEditor(
rule: selection.rule, operation: selection.operation, currentLevel: selection.level
) {
level in
guard var updated = rules else { throw SettingsError.missingDirectory }
updated.setDefaultLevel(level, for: selection.rule, operation: selection.operation)
try save(updated)
}
}
.alert("Turn off Git Protection?", isPresented: $confirmsDisable) {
Button("Cancel", role: .cancel) {}.keyboardShortcut(.defaultAction)
Button("Turn Off Protection", role: .destructive) { setEnabled(false) }
Expand All @@ -90,7 +106,7 @@ struct GitProtectionPane: View {
Button("Cancel", role: .cancel) {}
Button("Remove Rule", role: .destructive) { removeLegacyRule() }
} message: {
Text("Other rules and the remote’s default branch can still require the repository name.")
Text("Other rules and the remote’s default branch can still require a check.")
}
}

Expand Down Expand Up @@ -132,31 +148,18 @@ struct GitProtectionPane: View {
.pickerStyle(.segmented)
.labelsHidden()

if operation != .push {
Picker("Rule scope", selection: $scope) {
Text("All repositories").tag(Scope.all)
Text("One repository…").tag(Scope.repository)
}
if scope == .repository {
LabeledContent("Repository") {
repositoryButton(rulesRepository, use: .rules)
}
if let folderError, folderUse == .rules { errorText(folderError) }
}
Picker("Rule scope", selection: $scope) {
Text("All repositories").tag(Scope.all)
Text("One repository…").tag(Scope.repository)
}
LabeledContent {
Text(operation.defaultLevel.title)
} label: {
Heading(title: scope == .all ? "Default" : "Inherited default", hint: "Branches with no matching rule")
if scope == .repository {
LabeledContent("Repository") {
repositoryButton(rulesRepository, use: .rules)
}
if let folderError, folderUse == .rules { errorText(folderError) }
}
LabeledContent {
Text(GitCheckLevel.name.title)
} label: {
Heading(
title: "Protected branches",
hint: operation == .push
? "main and master"
: "main, master, the remote’s default branch, and branches protected in Git")
ForEach(GitProtectionRules.DefaultRule.allCases) { rule in
defaultRow(rule)
}
ForEach(currentRules) { rule in
let patterns = rule.patterns.joined(separator: " ")
Expand All @@ -174,14 +177,12 @@ struct GitProtectionPane: View {
.disabled(rules == nil)
}
}
if operation != .push {
Button {
openEditor()
} label: {
Label("Add \(operation.ruleTitle)…", systemImage: "plus")
}
.disabled(!canAddRule)
Button {
openEditor()
} label: {
Label("Add \(operation.ruleTitle)…", systemImage: "plus")
}
.disabled(!canAddRule)
} header: {
HStack {
Text("Branch rules")
Expand All @@ -192,15 +193,37 @@ struct GitProtectionPane: View {
}
} footer: {
Footnote(
operation == .push
? "These built-in checks apply to ordinary pushes. Git can also require confirmation when it cannot determine the destination from the command."
: scope == .all
? "The first matching rule applies. Protected branches always require at least the check shown above."
: "The first matching repository rule can make the global check stricter. Linked working copies use the same rules."
scope == .all
? "The first matching pattern applies. Its check can strengthen the Protected branches rule."
: "The first matching repository rule can make the global check stricter. Linked working copies use the same rules."
)
}
}

private func defaultRow(_ rule: GitProtectionRules.DefaultRule) -> some View {
let level = rules?.defaultLevel(rule, for: operation) ?? rule.factoryLevel(for: operation)
return LabeledContent {
HStack {
Text(level.title)
if scope == .all {
Button("Edit…") {
defaultEditor = DefaultSelection(rule: rule, operation: operation, level: level)
}
.accessibilityLabel("Edit \(rule.title) for \(operation.title)")
.disabled(rules == nil)
}
}
} label: {
Heading(
title: rule == .unmatched && scope == .repository ? "Inherited default" : rule.title,
hint: rule == .unmatched
? "Other branches with no matching rule"
: operation == .push
? "main and master"
: "main, master, the remote’s default branch, and branches protected in Git")
}
}

private var branchPreview: some View {
Section("Preview a branch") {
LabeledContent("Repository") {
Expand Down Expand Up @@ -228,21 +251,19 @@ struct GitProtectionPane: View {
}

private func previewResult(_ evidence: GitDeletionPreview) -> (level: GitCheckLevel, reason: String) {
let choice = (rules ?? GitProtectionRules()).level(
for: operation, branch: branch, repository: previewRepository?.key)
let settings = rules ?? GitProtectionRules()
let protected = evidence == .builtIn || (operation != .push && evidence != .dependsOnRemote)
if protected && choice.level <= .name {
switch evidence {
case .builtIn: return (.name, "Built-in protection for \(branch).")
case .projectRule: return (.name, "This branch is protected in the repository’s Git configuration.")
case .includedRule: return (.name, "This branch is protected in an included Git configuration file.")
case .dependsOnRemote: break
let key = previewRepository?.key
let choice = settings.level(for: operation, branch: branch, repository: key, protected: protected)
var reason =
choice.source.map { "Matches rule “\($0)”." }
?? (protected ? "Uses the Protected branches rule." : "Uses the default for \(operation.title).")
if operation != .push && evidence == .dependsOnRemote {
let remoteLevel = settings.level(for: operation, branch: branch, repository: key, protected: true).level
if remoteLevel != choice.level {
reason += " If this is the remote’s default branch: \(remoteLevel.title)."
}
}
var reason = choice.source.map { "Matches rule “\($0)”." } ?? "Uses the default for \(operation.title)."
if operation != .push && choice.level < .name {
reason += " If this is the remote’s default branch, the repository name is also required."
}
return (choice.level, reason)
}

Expand Down Expand Up @@ -374,7 +395,7 @@ struct GitProtectionPane: View {
}
scoped[selection.operation] = list
if let key {
updated.repositories[key] = scoped.forcePush.isEmpty && scoped.delete.isEmpty ? nil : scoped
updated.repositories[key] = scoped.isEmpty ? nil : scoped
} else {
updated.global = scoped
}
Expand Down
73 changes: 73 additions & 0 deletions Sources/AgentWatchApp/GitProtectionRuleEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,79 @@ struct GitProtectionRuleEditor: View {
}
}

struct GitProtectionDefaultEditor: View {
let rule: GitProtectionRules.DefaultRule
let operation: GitBranchOperation
let currentLevel: GitCheckLevel
let save: (GitCheckLevel) throws -> Void
@Environment(\.dismiss) private var dismiss
@State private var level = GitCheckLevel.name
@State private var confirmsChange = false
@State private var errorMessage: String?

var body: some View {
VStack(alignment: .leading, spacing: 14) {
VStack(alignment: .leading, spacing: 4) {
Text("Edit \(rule.title)").font(.headline)
Text("All repositories · \(operation.title)").foregroundStyle(.secondary)
}
Picker("Agent action", selection: $level) {
ForEach(GitCheckLevel.allCases, id: \.self) { level in
Text(level.title).tag(level)
}
}
if let errorMessage { Text(errorMessage).font(.caption).foregroundStyle(.red) }
HStack {
Button("Use Original Default") { level = rule.factoryLevel(for: operation) }
.disabled(level == rule.factoryLevel(for: operation))
Spacer()
Button("Cancel", role: .cancel) { dismiss() }.keyboardShortcut(.cancelAction)
Button("Save Rule…") { confirmsChange = true }
.keyboardShortcut(.defaultAction)
.disabled(level == currentLevel)
}
}
.padding(20)
.frame(width: 460)
.onAppear { level = currentLevel }
.alert("Change \(rule.title) for \(operation.title)?", isPresented: $confirmsChange) {
Button("Cancel", role: .cancel) {}.keyboardShortcut(.defaultAction)
Button("Change Rule", role: level < currentLevel ? .destructive : nil) {
do {
try save(level)
dismiss()
} catch {
errorMessage = "Could not save the rule. \(error.localizedDescription)"
}
}
} message: {
Text(
"This changes Git Protection for all repositories and connected agent profiles. \(currentLevel.title) → \(level.title). \(consequence)"
)
}
}

private var consequence: String {
if level < currentLevel {
switch operation {
case .push: return "Agents may send changes to shared branches with fewer checks."
case .forcePush: return "Agents may overwrite shared branch history with fewer checks."
case .delete: return "Agents may delete remote branches with fewer checks."
}
}
return "Future matching commands may require an extra confirmation or be blocked."
}
}

extension GitProtectionRules.DefaultRule {
var title: String {
switch self {
case .unmatched: "Default"
case .protected: "Protected branches"
}
}
}

extension GitBranchOperation {
var title: String {
switch self {
Expand Down
Loading
Loading