Skip to content

Commit 98726ec

Browse files
committed
First setup for swift
1 parent 0f84985 commit 98726ec

30 files changed

+981
-173
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/swift/.gitignore

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## User settings
6+
xcuserdata/
7+
8+
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9+
*.xcscmblueprint
10+
*.xccheckout
11+
12+
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13+
build/
14+
DerivedData/
15+
*.moved-aside
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
25+
## Obj-C/Swift specific
26+
*.hmap
27+
28+
## App packaging
29+
*.ipa
30+
*.dSYM.zip
31+
*.dSYM
32+
33+
## Playgrounds
34+
timeline.xctimeline
35+
playground.xcworkspace
36+
37+
# Swift Package Manager
38+
#
39+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40+
# Packages/
41+
# Package.pins
42+
# Package.resolved
43+
# *.xcodeproj
44+
#
45+
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46+
# hence it is not needed unless you have added a package configuration file to your project
47+
# .swiftpm
48+
49+
.build/

compiled_starters/swift/Package.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "OwnGit",
8+
targets: [
9+
// Targets are the basic building blocks of a package, defining a module or a test suite.
10+
// Targets can depend on other targets in this package and products from dependencies.
11+
.executableTarget(
12+
name: "OwnGit"),
13+
]
14+
)

compiled_starters/swift/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)
2+
3+
This is a starting point for Swift solutions to the
4+
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).
5+
6+
In this challenge, you'll build a small Git implementation that's capable of
7+
initializing a repository, creating commits and cloning a public repository.
8+
Along the way we'll learn about the `.git` directory, Git objects (blobs,
9+
commits, trees etc.), Git's transfer protocols and more.
10+
11+
**Note**: If you're viewing this repo on GitHub, head over to
12+
[codecrafters.io](https://codecrafters.io) to try the challenge.
13+
14+
# Passing the first stage
15+
16+
The entry point for your Git implementation is in `Sources/main.swift`. Study
17+
and uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git add .
21+
git commit -m "pass 1st stage" # any msg
22+
git push origin master
23+
```
24+
25+
That's all!
26+
27+
# Stage 2 & beyond
28+
29+
Note: This section is for stages 2 and beyond.
30+
31+
1. Ensure you have `swift` installed locally
32+
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
33+
`Sources/main.swift`.
34+
1. Commit your changes and run `git push origin master` to submit your solution
35+
to CodeCrafters. Test output will be streamed to your terminal.
36+
37+
# Testing locally
38+
39+
The `your_git.sh` script is expected to operate on the `.git` folder inside the
40+
current working directory. If you're running this inside the root of this
41+
repository, you might end up accidentally damaging your repository's `.git`
42+
folder.
43+
44+
We suggest executing `your_git.sh` in a different folder when testing locally.
45+
For example:
46+
47+
```sh
48+
mkdir -p /tmp/testing && cd /tmp/testing
49+
/path/to/your/repo/your_git.sh init
50+
```
51+
52+
To make this easier to type out, you could add a
53+
[shell alias](https://shapeshed.com/unix-alias/):
54+
55+
```sh
56+
alias mygit=/path/to/your/repo/your_git.sh
57+
58+
mkdir -p /tmp/testing && cd /tmp/testing
59+
mygit init
60+
```
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
import Foundation
4+
5+
// You can use print statements as follows for debugging, they'll be visible when running tests.
6+
print("Logs from your program will appear here!")
7+
8+
// Uncomment this block to pass the first stage
9+
//
10+
//
11+
//guard CommandLine.arguments.count > 1 else {
12+
// throw ArgumentError.missingCommand
13+
//}
14+
//
15+
//switch CommandLine.arguments[1] {
16+
//case "init":
17+
// try createGitDirectory()
18+
//default:
19+
// throw ArgumentError.unknownCommand
20+
//}
21+
//
22+
//enum ArgumentError: Error {
23+
// case missingCommand
24+
// case unknownCommand
25+
//}
26+
//
27+
//func createGitDirectory() throws {
28+
// let currentPath = FileManager.default.currentDirectoryPath
29+
// let gitFolder = currentPath + "/.git"
30+
// try FileManager.default.createDirectory(atPath: gitFolder, withIntermediateDirectories: true)
31+
// let objectFolder = gitFolder + "/objects"
32+
// try FileManager.default.createDirectory(atPath: objectFolder, withIntermediateDirectories: true)
33+
// let refsFolder = gitFolder + "/refs"
34+
// try FileManager.default.createDirectory(atPath: refsFolder, withIntermediateDirectories: true)
35+
//
36+
// let head = "ref: refs/heads/master\n"
37+
// let headPath = gitFolder + "/HEAD"
38+
// try head.write(toFile: headPath, atomically: true, encoding: .utf8)
39+
//}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Swift version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: swift-5.10
11+
language_pack: swift-5.10

compiled_starters/swift/your_git.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
swift run --package-path $(dirname $0) OwnGit "$@"

dockerfiles/swift-5.10.Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM swift:5.10
2+
3+
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Package.swift"
4+
5+
COPY Package.swift /app/Package.swift
6+
RUN mkdir -p /app/Sources
7+
RUN echo "print(\"Hello, world!\")" > /app/Sources/main.swift
8+
9+
WORKDIR /app
10+
RUN swift build
11+
12+
RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && swift build" > /codecrafters-precompile.sh
13+
RUN chmod +x /codecrafters-precompile.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## User settings
6+
xcuserdata/
7+
8+
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9+
*.xcscmblueprint
10+
*.xccheckout
11+
12+
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13+
build/
14+
DerivedData/
15+
*.moved-aside
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
25+
## Obj-C/Swift specific
26+
*.hmap
27+
28+
## App packaging
29+
*.ipa
30+
*.dSYM.zip
31+
*.dSYM
32+
33+
## Playgrounds
34+
timeline.xctimeline
35+
playground.xcworkspace
36+
37+
# Swift Package Manager
38+
#
39+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40+
# Packages/
41+
# Package.pins
42+
# Package.resolved
43+
# *.xcodeproj
44+
#
45+
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46+
# hence it is not needed unless you have added a package configuration file to your project
47+
# .swiftpm
48+
49+
.build/
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "OwnGit",
8+
targets: [
9+
// Targets are the basic building blocks of a package, defining a module or a test suite.
10+
// Targets can depend on other targets in this package and products from dependencies.
11+
.executableTarget(
12+
name: "OwnGit"),
13+
]
14+
)
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)
2+
3+
This is a starting point for Swift solutions to the
4+
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).
5+
6+
In this challenge, you'll build a small Git implementation that's capable of
7+
initializing a repository, creating commits and cloning a public repository.
8+
Along the way we'll learn about the `.git` directory, Git objects (blobs,
9+
commits, trees etc.), Git's transfer protocols and more.
10+
11+
**Note**: If you're viewing this repo on GitHub, head over to
12+
[codecrafters.io](https://codecrafters.io) to try the challenge.
13+
14+
# Passing the first stage
15+
16+
The entry point for your Git implementation is in `Sources/main.swift`. Study
17+
and uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git add .
21+
git commit -m "pass 1st stage" # any msg
22+
git push origin master
23+
```
24+
25+
That's all!
26+
27+
# Stage 2 & beyond
28+
29+
Note: This section is for stages 2 and beyond.
30+
31+
1. Ensure you have `swift` installed locally
32+
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
33+
`Sources/main.swift`.
34+
1. Commit your changes and run `git push origin master` to submit your solution
35+
to CodeCrafters. Test output will be streamed to your terminal.
36+
37+
# Testing locally
38+
39+
The `your_git.sh` script is expected to operate on the `.git` folder inside the
40+
current working directory. If you're running this inside the root of this
41+
repository, you might end up accidentally damaging your repository's `.git`
42+
folder.
43+
44+
We suggest executing `your_git.sh` in a different folder when testing locally.
45+
For example:
46+
47+
```sh
48+
mkdir -p /tmp/testing && cd /tmp/testing
49+
/path/to/your/repo/your_git.sh init
50+
```
51+
52+
To make this easier to type out, you could add a
53+
[shell alias](https://shapeshed.com/unix-alias/):
54+
55+
```sh
56+
alias mygit=/path/to/your/repo/your_git.sh
57+
58+
mkdir -p /tmp/testing && cd /tmp/testing
59+
mygit init
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
import Foundation
4+
5+
6+
guard CommandLine.arguments.count > 1 else {
7+
throw ArgumentError.missingCommand
8+
}
9+
10+
switch CommandLine.arguments[1] {
11+
case "init":
12+
try createGitDirectory()
13+
default:
14+
throw ArgumentError.unknownCommand
15+
}
16+
17+
enum ArgumentError: Error {
18+
case missingCommand
19+
case unknownCommand
20+
}
21+
22+
func createGitDirectory() throws {
23+
let currentPath = FileManager.default.currentDirectoryPath
24+
let gitFolder = currentPath + "/.git"
25+
try FileManager.default.createDirectory(atPath: gitFolder, withIntermediateDirectories: true)
26+
let objectFolder = gitFolder + "/objects"
27+
try FileManager.default.createDirectory(atPath: objectFolder, withIntermediateDirectories: true)
28+
let refsFolder = gitFolder + "/refs"
29+
try FileManager.default.createDirectory(atPath: refsFolder, withIntermediateDirectories: true)
30+
31+
let head = "ref: refs/heads/master\n"
32+
let headPath = gitFolder + "/HEAD"
33+
try head.write(toFile: headPath, atomically: true, encoding: .utf8)
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Swift version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: swift-5.10
11+
language_pack: swift-5.10
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
swift run --package-path $(dirname $0) OwnGit "$@"

0 commit comments

Comments
 (0)