-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 154568fb541c55c79690af73bda0dd1f95652ebd Author: Whirlwind <[email protected]> Date: Tue Aug 24 22:23:01 2021 +0800 [Feature] Add `mbox git hooks` CLI
- Loading branch information
liyao.ryan
committed
Aug 25, 2021
1 parent
a7d5e20
commit 2844098
Showing
6 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Git+Config.swift | ||
// MBoxWorkspace | ||
// | ||
// Created by 詹迟晶 on 2021/8/20. | ||
// Copyright © 2021 bytedance. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MBoxCore | ||
import MBoxGit | ||
import MBoxWorkspaceCore | ||
|
||
extension MBCommander.Git { | ||
@objc(MBCommanderGitConfig) | ||
open class Config: Git { | ||
open override class var flags: [Flag] { | ||
var flags = super.flags | ||
flags << Flag("workspace", description: "use the workspace config file") | ||
return flags | ||
} | ||
|
||
open override func setup() throws { | ||
if self.shiftFlag("workspace") { | ||
self.level = Self.WorkspaceLevel | ||
} | ||
try super.setup() | ||
} | ||
|
||
open var level: String? | ||
public static let WorkspaceLevel = "Workspace" | ||
|
||
open override var cmd: MBCMD { | ||
let cmd = super.cmd as! GitCMD | ||
cmd.pager = false | ||
return cmd | ||
} | ||
|
||
open override var args: [String] { | ||
var args = super.args | ||
args.insert(contentsOf: ["config"], at: 0) | ||
if self.level == Self.WorkspaceLevel { | ||
args.insert(contentsOf: ["-f", self.workspace.gitConfigPath], at: 1) | ||
} | ||
return args | ||
} | ||
|
||
open override var onlyRunInWorkspace: Bool { | ||
if self.level == Self.WorkspaceLevel { | ||
return true | ||
} | ||
for flag in ["global", "system"] { | ||
if (try? Flag(flag).parse(self.argv, shift: false)) == true { | ||
return true | ||
} | ||
} | ||
return super.onlyRunInWorkspace | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// Git+Hooks.swift | ||
// MBoxWorkspace | ||
// | ||
// Created by 詹迟晶 on 2021/8/20. | ||
// Copyright © 2021 bytedance. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MBoxCore | ||
import MBoxGit | ||
import MBoxWorkspaceCore | ||
|
||
extension MBCommander.Git { | ||
open class Hooks: MBCommander { | ||
open override class var description: String? { | ||
return "Show/Set the git hooks for workspace" | ||
} | ||
|
||
open override class var flags: [Flag] { | ||
var flags = super.flags | ||
flags << Flag("enable", description: "Enable workspace hooks") | ||
flags << Flag("disable", description: "Enable workspace hooks") | ||
return flags | ||
} | ||
|
||
open override func setup() throws { | ||
if self.shiftFlag("disable") { | ||
self.enable = false | ||
} | ||
if self.shiftFlag("enable") { | ||
self.enable = true | ||
} | ||
try super.setup() | ||
} | ||
|
||
open var enable: Bool? | ||
|
||
open override func run() throws { | ||
try super.run() | ||
if let enable = self.enable { | ||
try UI.with(verbose: true) { | ||
try self.workspace.setupGitHooks(enable: enable) | ||
} | ||
} else { | ||
if let path = try self.workspace.gitHooks() { | ||
UI.log(info: "Workspace Git Config File: \(self.workspace.gitConfigPath)\n Workspace Hooks Directory: \(path)") | ||
} else { | ||
UI.log(info: "No Workspace Hooks.") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// Git.swift | ||
// MBoxWorkspace | ||
// | ||
// Created by Whirlwind on 2019/6/12. | ||
// Copyright © 2019 bytedance. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MBoxCore | ||
import MBoxGit | ||
import MBoxWorkspaceCore | ||
|
||
extension MBCommander { | ||
open class Git: Exec { | ||
open class override var description: String? { | ||
return "Execute git command for every repo" | ||
} | ||
|
||
open class override var arguments: [Argument] { | ||
return [Argument("command", description: "The command will be executed", required: true)] | ||
} | ||
|
||
open override var cmd: MBCMD { | ||
let cmd = GitCMD() | ||
cmd.showOutput = true | ||
cmd.workingDirectory = UI.workspace?.rootPath | ||
return cmd | ||
} | ||
|
||
open class override var onlyRunInWorkspace: Bool { | ||
return false | ||
} | ||
|
||
open override var onlyRunInWorkspace: Bool { | ||
if self.argv.remainder.count == 0 { | ||
return true | ||
} | ||
return super.onlyRunInWorkspace | ||
} | ||
|
||
open override func runInRepo(repo: MBWorkRepo, cmd: MBCMD, args: String) -> Int32 { | ||
UI.with(verbose: false) { | ||
if let git = repo.git, let branch = git.currentBranch { | ||
if git.trackBranch(autoMatch: false) == nil, let remoteBranch = git.remoteBranch(named: branch) { | ||
if git.setTrackBranch(local: branch, remote: remoteBranch.name) { | ||
UI.log(info: "Set the current track branch to `\(remoteBranch.name)`.", pip: .ERR) | ||
} | ||
} | ||
} | ||
} | ||
return super.runInRepo(repo: repo, cmd: cmd, args: args) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters