Skip to content
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

Add fetch-interval to outdated command to skip fetching the dependencies #2714

Open
wants to merge 3 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 48 additions & 3 deletions Source/CarthageKit/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ public struct FetchCache {
lastFetchTimes.removeAll()
}

internal static func needsFetch(forURL url: GitURL) -> Bool {
guard let lastFetch = lastFetchTimes[url] else {
internal static func needsFetch(forURL url: GitURL) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carthage uses tabs instead of spaces for indentation -- could you please reformat your changes to do the same?

guard let lastFetch = lastFetchTimes[url] ?? repositoriesLastFetchTime() else {
return true
}

let difference = Date().timeIntervalSince1970 - lastFetch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert this change?

return !(0...fetchCacheInterval).contains(difference)
}

Expand All @@ -37,6 +36,52 @@ public struct FetchCache {
lastFetchTimes[url] = Date().timeIntervalSince1970
}
}

fileprivate static func repositoriesLastFetchTime() -> TimeInterval? {
guard let data = try? Constants.Dependency.repositoriesURL.extendedAttribute(for: "\(Constants.bundleIdentifier).repositories.last.fetch.time") else {
return nil
}
return data.withUnsafeBytes { $0.pointee as TimeInterval }
}

public static func updateRepositoriesLastFetchTime() {
var now = Date().timeIntervalSince1970
let data = Data(bytes: &now, count: MemoryLayout<TimeInterval>.size)
try? Constants.Dependency.repositoriesURL.setExtendedAttribute(for: "\(Constants.bundleIdentifier).repositories.last.fetch.time", data: data)
}
}

/// URL extension to support operations on extended attributes of a file
fileprivate extension URL {

enum ExtendedAttributesError: Error {
case cannotReadAttribute
case cannotSetAttribute
}

func extendedAttribute(for name: String) throws -> Data {
let data = try withUnsafeFileSystemRepresentation { path -> Data in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious -- what are your thoughts on using this approach rather than using the methods provided by NSFileManager? I've never used either personally.

https://developer.apple.com/documentation/foundation/filemanager/1413667-setattributes

https://developer.apple.com/documentation/foundation/filemanager/1410452-attributesofitem

let length = getxattr(path, name, nil, 0, 0, 0)
guard length >= 0 else { throw ExtendedAttributesError.cannotReadAttribute }

var data = Data(count: length)
let result = data.withUnsafeMutableBytes { [count = data.count] in
getxattr(path, name, $0.baseAddress, count, 0, 0)
}
guard result >= 0 else { throw ExtendedAttributesError.cannotReadAttribute }
return data
}
return data
}

func setExtendedAttribute(for name: String, data: Data) throws {
try withUnsafeFileSystemRepresentation { path in
let result = data.withUnsafeBytes {
setxattr(path, name, $0, data.count, 0, 0)
}
guard result >= 0 else { throw ExtendedAttributesError.cannotSetAttribute }
}
}
}

/// Shells out to `git` with the given arguments, optionally in the directory
Expand Down
12 changes: 9 additions & 3 deletions Source/carthage/Outdated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public struct OutdatedCommand: CommandProtocol {
public let useSSH: Bool
public let isVerbose: Bool
public let outputXcodeWarnings: Bool
public let fetchInterval: Int
mollyIV marked this conversation as resolved.
Show resolved Hide resolved
public let colorOptions: ColorOptions
public let directoryPath: String

Expand All @@ -65,7 +66,8 @@ public struct OutdatedCommand: CommandProtocol {
<*> mode <| Option(key: "use-ssh", defaultValue: false, usage: "use SSH for downloading GitHub repositories")
<*> mode <| Option(key: "verbose", defaultValue: false, usage: "include nested dependencies")
<*> mode <| Option(key: "xcode-warnings", defaultValue: false, usage: "output Xcode compatible warning messages")
<*> ColorOptions.evaluate(mode, additionalUsage: UpdateType.legend)
<*> mode <| Option(key: "fetch-interval", defaultValue: 0, usage: "the minimum amount of time that must elapse between dependencies fetch operation")
<*> ColorOptions.evaluate(mode, additionalUsage: UpdateType.legend)
<*> mode <| projectDirectoryOption
}

Expand All @@ -87,9 +89,13 @@ public struct OutdatedCommand: CommandProtocol {
public let function = "Check for compatible updates to the project's dependencies"

public func run(_ options: Options) -> Result<(), CarthageError> {
return options.loadProject()
.flatMap(.merge) { $0.outdatedDependencies(options.isVerbose) }
FetchCache.fetchCacheInterval = TimeInterval(options.fetchInterval)

return options.loadProject()
.flatMap(.merge) { $0.outdatedDependencies(options.isVerbose) }
.on(value: { outdatedDependencies in
FetchCache.updateRepositoriesLastFetchTime()

let formatting = options.colorOptions.formatting

if !outdatedDependencies.isEmpty {
Expand Down