-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.swift
34 lines (28 loc) · 1.03 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// The Swift Programming Language
// https://docs.swift.org/swift-book
import Foundation
guard CommandLine.arguments.count > 1 else {
throw ArgumentError.missingCommand
}
switch CommandLine.arguments[1] {
case "init":
try createGitDirectory()
default:
throw ArgumentError.unknownCommand
}
enum ArgumentError: Error {
case missingCommand
case unknownCommand
}
func createGitDirectory() throws {
let currentPath = FileManager.default.currentDirectoryPath
let gitFolder = currentPath + "/.git"
try FileManager.default.createDirectory(atPath: gitFolder, withIntermediateDirectories: true)
let objectFolder = gitFolder + "/objects"
try FileManager.default.createDirectory(atPath: objectFolder, withIntermediateDirectories: true)
let refsFolder = gitFolder + "/refs"
try FileManager.default.createDirectory(atPath: refsFolder, withIntermediateDirectories: true)
let head = "ref: refs/heads/master\n"
let headPath = gitFolder + "/HEAD"
try head.write(toFile: headPath, atomically: true, encoding: .utf8)
}