Skip to content

Commit

Permalink
Merge pull request SwiftGit2#173 from spekke/support_status_options
Browse files Browse the repository at this point in the history
Add support for status options
  • Loading branch information
twa16 authored Feb 5, 2021
2 parents cbb2207 + b620e0c commit edbd643
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
6 changes: 6 additions & 0 deletions SwiftGit2.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
237731C71F46542B0020A3FE /* repository-with-status.zip in Resources */ = {isa = PBXBuildFile; fileRef = 237731C61F46542B0020A3FE /* repository-with-status.zip */; };
2549921B34FFC36AF8C9CD6D /* CommitIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25499A996CA7BD416620A397 /* CommitIterator.swift */; };
25499D325997CAB9BEFFCA4D /* CommitIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25499A996CA7BD416620A397 /* CommitIterator.swift */; };
3F5181B1258FE7D10048F1EF /* StatusOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F5181B0258FE7D10048F1EF /* StatusOptions.swift */; };
3F5181B2258FE7D10048F1EF /* StatusOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F5181B0258FE7D10048F1EF /* StatusOptions.swift */; };
621E66A01C72958800A0F352 /* OID.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE70B3E41A1ACB1A002C3F4E /* OID.swift */; };
621E66A11C72958800A0F352 /* Remotes.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6D1A57284700999413 /* Remotes.swift */; };
621E66A21C72958800A0F352 /* CheckoutStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE276B281ACCD3CF00D6DAD7 /* CheckoutStrategy.swift */; };
Expand Down Expand Up @@ -136,6 +138,7 @@
232861421F4A3A2E00276D65 /* Diffs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Diffs.swift; sourceTree = "<group>"; };
237731C61F46542B0020A3FE /* repository-with-status.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = "repository-with-status.zip"; sourceTree = "<group>"; };
25499A996CA7BD416620A397 /* CommitIterator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommitIterator.swift; sourceTree = "<group>"; };
3F5181B0258FE7D10048F1EF /* StatusOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusOptions.swift; sourceTree = "<group>"; };
621E66B41C72958800A0F352 /* SwiftGit2.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftGit2.framework; sourceTree = BUILT_PRODUCTS_DIR; };
621E66CE1C72958D00A0F352 /* SwiftGit2-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftGit2-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
621E66E11C729CE500A0F352 /* libgit2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libgit2.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -315,6 +318,7 @@
BECB5F691A56F19900999413 /* References.swift */,
BECB5F6D1A57284700999413 /* Remotes.swift */,
25499A996CA7BD416620A397 /* CommitIterator.swift */,
3F5181B0258FE7D10048F1EF /* StatusOptions.swift */,
);
path = SwiftGit2;
sourceTree = "<group>";
Expand Down Expand Up @@ -756,6 +760,7 @@
621E66A71C72958800A0F352 /* Pointers.swift in Sources */,
621E66A81C72958800A0F352 /* Errors.swift in Sources */,
621E66A91C72958800A0F352 /* SwiftGit2.m in Sources */,
3F5181B2258FE7D10048F1EF /* StatusOptions.swift in Sources */,
2549921B34FFC36AF8C9CD6D /* CommitIterator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -791,6 +796,7 @@
BE7A753F1A4A2BCC002DA7E3 /* Pointers.swift in Sources */,
DA5914761A94579000AED74C /* Errors.swift in Sources */,
BE14AA501A1974010015B439 /* SwiftGit2.m in Sources */,
3F5181B1258FE7D10048F1EF /* StatusOptions.swift in Sources */,
25499D325997CAB9BEFFCA4D /* CommitIterator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
7 changes: 4 additions & 3 deletions SwiftGit2/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ public final class Repository {

// MARK: - Status

public func status() -> Result<[StatusEntry], NSError> {
public func status(options: StatusOptions = [.includeUntracked]) -> Result<[StatusEntry], NSError> {
var returnArray = [StatusEntry]()

// Do this because GIT_STATUS_OPTIONS_INIT is unavailable in swift
Expand All @@ -897,12 +897,13 @@ public final class Repository {
guard optionsResult == GIT_OK.rawValue else {
return .failure(NSError(gitError: optionsResult, pointOfFailure: "git_status_init_options"))
}
var options = pointer.move()
var listOptions = pointer.move()
listOptions.flags = options.rawValue
pointer.deallocate()

var unsafeStatus: OpaquePointer? = nil
defer { git_status_list_free(unsafeStatus) }
let statusResult = git_status_list_new(&unsafeStatus, self.pointer, &options)
let statusResult = git_status_list_new(&unsafeStatus, self.pointer, &listOptions)
guard statusResult == GIT_OK.rawValue, let unwrapStatusResult = unsafeStatus else {
return .failure(NSError(gitError: statusResult, pointOfFailure: "git_status_list_new"))
}
Expand Down
63 changes: 63 additions & 0 deletions SwiftGit2/StatusOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright © 2020 GitHub, Inc. All rights reserved.
//

import Clibgit2
import Foundation

public struct StatusOptions: OptionSet {
public let rawValue: UInt32

public init(rawValue: UInt32) {
self.rawValue = rawValue
}

public static let includeUntracked = StatusOptions(
rawValue: GIT_STATUS_OPT_INCLUDE_UNTRACKED.rawValue
)
public static let includeIgnored = StatusOptions(
rawValue: GIT_STATUS_OPT_INCLUDE_IGNORED.rawValue
)
public static let includeUnmodified = StatusOptions(
rawValue: GIT_STATUS_OPT_INCLUDE_UNMODIFIED.rawValue
)
public static let excludeSubmodules = StatusOptions(
rawValue: GIT_STATUS_OPT_EXCLUDE_SUBMODULES.rawValue
)
public static let recurseUntrackedDirs = StatusOptions(
rawValue: GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS.rawValue
)
public static let disablePathSpecMatch = StatusOptions(
rawValue: GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH.rawValue
)
public static let recurseIgnoredDirs = StatusOptions(
rawValue: GIT_STATUS_OPT_RECURSE_IGNORED_DIRS.rawValue
)
public static let renamesHeadToIndex = StatusOptions(
rawValue: GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX.rawValue
)
public static let renamesIndexToWorkDir = StatusOptions(
rawValue: GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR.rawValue
)
public static let sortCasesSensitively = StatusOptions(
rawValue: GIT_STATUS_OPT_SORT_CASE_SENSITIVELY.rawValue
)
public static let sortCasesInSensitively = StatusOptions(
rawValue: GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY.rawValue
)
public static let renamesFromRewrites = StatusOptions(
rawValue: GIT_STATUS_OPT_RENAMES_FROM_REWRITES.rawValue
)
public static let noRefresh = StatusOptions(
rawValue: GIT_STATUS_OPT_NO_REFRESH.rawValue
)
public static let updateIndex = StatusOptions(
rawValue: GIT_STATUS_OPT_UPDATE_INDEX.rawValue
)
public static let includeUnreadable = StatusOptions(
rawValue: GIT_STATUS_OPT_INCLUDE_UNREADABLE.rawValue
)
public static let includeUnreadableAsUntracked = StatusOptions(
rawValue: GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED.rawValue
)
}
21 changes: 18 additions & 3 deletions SwiftGit2Tests/RepositorySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ class RepositorySpec: FixturesSpec {
}

it("Should accurately report status for repositories with status") {
let expectedCount = 5
let expectedCount = 6
let expectedNewFilePaths = [
"stage-file-1",
"stage-file-2",
Expand All @@ -791,6 +791,9 @@ class RepositorySpec: FixturesSpec {
"stage-file-4",
"stage-file-5",
]
let expectedUntrackedFiles = [
"unstaged-file",
]

let repoWithStatus = Fixtures.sharedInstance.repository(named: "repository-with-status")
let branchWithStatus = repoWithStatus.localBranch(named: "master").value!
Expand All @@ -800,16 +803,28 @@ class RepositorySpec: FixturesSpec {

var newFilePaths: [String] = []
for status in statuses {
newFilePaths.append((status.headToIndex?.newFile?.path)!)
if let path = status.headToIndex?.newFile?.path {
newFilePaths.append(path)
}
}
var oldFilePaths: [String] = []
for status in statuses {
oldFilePaths.append((status.headToIndex?.oldFile?.path)!)
if let path = status.headToIndex?.oldFile?.path {
oldFilePaths.append(path)
}
}

var newUntrackedFilePaths: [String] = []
for status in statuses {
if let path = status.indexToWorkDir?.newFile?.path {
newUntrackedFilePaths.append(path)
}
}

expect(statuses.count).to(equal(expectedCount))
expect(newFilePaths).to(equal(expectedNewFilePaths))
expect(oldFilePaths).to(equal(expectedOldFilePaths))
expect(newUntrackedFilePaths).to(equal(expectedUntrackedFiles))
}
}

Expand Down

0 comments on commit edbd643

Please sign in to comment.