Skip to content

Setup for swift #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions compiled_starters/swift/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
49 changes: 49 additions & 0 deletions compiled_starters/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/
14 changes: 14 additions & 0 deletions compiled_starters/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OwnGit",
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "OwnGit"),
]
)
60 changes: 60 additions & 0 deletions compiled_starters/swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)

This is a starting point for Swift solutions to the
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).

In this challenge, you'll build a small Git implementation that's capable of
initializing a repository, creating commits and cloning a public repository.
Along the way we'll learn about the `.git` directory, Git objects (blobs,
commits, trees etc.), Git's transfer protocols and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Git implementation is in `Sources/main.swift`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `swift` installed locally
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
`Sources/main.swift`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.

# Testing locally

The `your_git.sh` script is expected to operate on the `.git` folder inside the
current working directory. If you're running this inside the root of this
repository, you might end up accidentally damaging your repository's `.git`
folder.

We suggest executing `your_git.sh` in a different folder when testing locally.
For example:

```sh
mkdir -p /tmp/testing && cd /tmp/testing
/path/to/your/repo/your_git.sh init
```

To make this easier to type out, you could add a
[shell alias](https://shapeshed.com/unix-alias/):

```sh
alias mygit=/path/to/your/repo/your_git.sh

mkdir -p /tmp/testing && cd /tmp/testing
mygit init
```
39 changes: 39 additions & 0 deletions compiled_starters/swift/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
import Foundation

// You can use print statements as follows for debugging, they'll be visible when running tests.
print("Logs from your program will appear here!")

// Uncomment this block to pass the first stage
//
//
//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)
//}
11 changes: 11 additions & 0 deletions compiled_starters/swift/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Swift version used to run your code
# on Codecrafters.
#
# Available versions: swift-5.10
language_pack: swift-5.10
8 changes: 8 additions & 0 deletions compiled_starters/swift/your_git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
swift run --package-path $(dirname $0) OwnGit "$@"
13 changes: 13 additions & 0 deletions dockerfiles/swift-5.10.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM swift:5.10

ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Package.swift"

COPY Package.swift /app/Package.swift
RUN mkdir -p /app/Sources
RUN echo "print(\"Hello, world!\")" > /app/Sources/main.swift

WORKDIR /app
RUN swift build

RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && swift build" > /codecrafters-precompile.sh
RUN chmod +x /codecrafters-precompile.sh
1 change: 1 addition & 0 deletions solutions/swift/01-init/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
49 changes: 49 additions & 0 deletions solutions/swift/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/
14 changes: 14 additions & 0 deletions solutions/swift/01-init/code/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OwnGit",
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "OwnGit"),
]
)
60 changes: 60 additions & 0 deletions solutions/swift/01-init/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)

This is a starting point for Swift solutions to the
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).

In this challenge, you'll build a small Git implementation that's capable of
initializing a repository, creating commits and cloning a public repository.
Along the way we'll learn about the `.git` directory, Git objects (blobs,
commits, trees etc.), Git's transfer protocols and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Git implementation is in `Sources/main.swift`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `swift` installed locally
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
`Sources/main.swift`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.

# Testing locally

The `your_git.sh` script is expected to operate on the `.git` folder inside the
current working directory. If you're running this inside the root of this
repository, you might end up accidentally damaging your repository's `.git`
folder.

We suggest executing `your_git.sh` in a different folder when testing locally.
For example:

```sh
mkdir -p /tmp/testing && cd /tmp/testing
/path/to/your/repo/your_git.sh init
```

To make this easier to type out, you could add a
[shell alias](https://shapeshed.com/unix-alias/):

```sh
alias mygit=/path/to/your/repo/your_git.sh

mkdir -p /tmp/testing && cd /tmp/testing
mygit init
```
34 changes: 34 additions & 0 deletions solutions/swift/01-init/code/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
11 changes: 11 additions & 0 deletions solutions/swift/01-init/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Swift version used to run your code
# on Codecrafters.
#
# Available versions: swift-5.10
language_pack: swift-5.10
8 changes: 8 additions & 0 deletions solutions/swift/01-init/code/your_git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
swift run --package-path $(dirname $0) OwnGit "$@"
Loading