forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGit.swift
616 lines (530 loc) · 25.1 KB
/
Git.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//
// Git.swift
// Carthage
//
// Created by Alan Rogers on 14/10/2014.
// Copyright (c) 2014 Carthage. All rights reserved.
//
import Foundation
import Result
import ReactiveCocoa
import ReactiveTask
/// The git version Carthage requires at least.
public let CarthageRequiredGitVersion = "2.3.0"
/// Represents a URL for a Git remote.
public struct GitURL: Equatable {
/// The string representation of the URL.
public let URLString: String
/// A normalized URL string, without protocol, authentication, or port
/// information. This is mostly useful for comparison, and not for any
/// actual Git operations.
private var normalizedURLString: String {
let parsedURL: NSURL? = NSURL(string: URLString)
if let parsedURL = parsedURL, host = parsedURL.host {
// Normal, valid URL.
let path = stripGitSuffix(parsedURL.path ?? "")
return "\(host)\(path)"
} else if URLString.hasPrefix("/") {
// Local path.
return stripGitSuffix(URLString)
} else {
// scp syntax.
var strippedURLString = URLString
if let index = strippedURLString.characters.indexOf("@") {
strippedURLString.removeRange(strippedURLString.startIndex...index)
}
var host = ""
if let index = strippedURLString.characters.indexOf(":") {
host = strippedURLString[strippedURLString.startIndex..<index]
strippedURLString.removeRange(strippedURLString.startIndex...index)
}
var path = stripGitSuffix(strippedURLString)
if !path.hasPrefix("/") {
// This probably isn't strictly legit, but we'll have a forward
// slash for other URL types.
path.insert("/", atIndex: path.startIndex)
}
return "\(host)\(path)"
}
}
/// The name of the repository, if it can be inferred from the URL.
public var name: String? {
let components = URLString.characters.split(allowEmptySlices: false) { $0 == "/" }
return components
.last
.map(String.init)
.map(stripGitSuffix)
}
public init(_ URLString: String) {
self.URLString = URLString
}
}
/// Strips any trailing .git in the given name, if one exists.
public func stripGitSuffix(string: String) -> String {
if string.hasSuffix(".git") {
return string[string.startIndex..<string.endIndex.advancedBy(-4)]
} else {
return string
}
}
public func ==(lhs: GitURL, rhs: GitURL) -> Bool {
return lhs.normalizedURLString == rhs.normalizedURLString
}
extension GitURL: Hashable {
public var hashValue: Int {
return normalizedURLString.hashValue
}
}
extension GitURL: CustomStringConvertible {
public var description: String {
return URLString
}
}
/// A Git submodule.
public struct Submodule: Equatable {
/// The name of the submodule. Usually (but not always) the same as the
/// path.
public let name: String
/// The relative path at which the submodule is checked out.
public let path: String
/// The URL from which the submodule should be cloned, if present.
public var URL: GitURL
/// The SHA checked out in the submodule.
public var SHA: String
public init(name: String, path: String, URL: GitURL, SHA: String) {
self.name = name
self.path = path
self.URL = URL
self.SHA = SHA
}
}
public func ==(lhs: Submodule, rhs: Submodule) -> Bool {
return lhs.name == rhs.name && lhs.path == rhs.path && lhs.URL == rhs.URL && lhs.SHA == rhs.SHA
}
extension Submodule: Hashable {
public var hashValue: Int {
return name.hashValue
}
}
extension Submodule: CustomStringConvertible {
public var description: String {
return "\(name) @ \(SHA)"
}
}
/// Struct to encapsulate global fetch interval cache
public struct FetchCache {
/// Amount of time before a git repository is fetched again. Defaults to 1 minute
public static var fetchCacheInterval: NSTimeInterval = 60.0
private static var lastFetchTimes: [GitURL : NSTimeInterval] = [:]
internal static func clearFetchTimes() {
lastFetchTimes.removeAll()
}
internal static func needsFetch(forURL url: GitURL) -> Bool {
guard let lastFetch = lastFetchTimes[url] else {
return true
}
let difference = NSDate().timeIntervalSince1970 - lastFetch
return !(0...fetchCacheInterval).contains(difference)
}
private static func updateLastFetchTime(forURL url: GitURL?) {
if let url = url {
lastFetchTimes[url] = NSDate().timeIntervalSince1970
}
}
}
/// Shells out to `git` with the given arguments, optionally in the directory
/// of an existing repository.
public func launchGitTask(arguments: [String], repositoryFileURL: NSURL? = nil, standardInput: SignalProducer<NSData, NoError>? = nil, environment: [String: String]? = nil) -> SignalProducer<String, CarthageError> {
// See https://github.com/Carthage/Carthage/issues/219.
var updatedEnvironment = environment ?? NSProcessInfo.processInfo().environment
updatedEnvironment["GIT_TERMINAL_PROMPT"] = "0"
let taskDescription = Task("/usr/bin/env", arguments: [ "git" ] + arguments, workingDirectoryPath: repositoryFileURL?.path, environment: updatedEnvironment)
return launchTask(taskDescription, standardInput: standardInput)
.ignoreTaskData()
.mapError(CarthageError.TaskError)
.map { data in
return NSString(data: data, encoding: NSUTF8StringEncoding)! as String
}
}
/// Checks if the git version satisfies the given required version.
public func ensureGitVersion(requiredVersion: String = CarthageRequiredGitVersion) -> SignalProducer<Bool, CarthageError> {
return launchGitTask([ "--version" ])
.map { input -> Bool in
let scanner = NSScanner(string: input)
guard scanner.scanString("git version ", intoString: nil) else {
return false
}
var version: NSString?
if scanner.scanUpToString("", intoString: &version), let version = version {
return version.compare(requiredVersion, options: [ .NumericSearch ]) != .OrderedAscending
} else {
return false
}
}
}
/// Returns a signal that completes when cloning completes successfully.
public func cloneRepository(cloneURL: GitURL, _ destinationURL: NSURL, bare: Bool = true) -> SignalProducer<String, CarthageError> {
precondition(destinationURL.fileURL)
var arguments = [ "clone" ]
if bare {
arguments.append("--bare")
}
return launchGitTask(arguments + [ "--quiet", cloneURL.URLString, destinationURL.path! ])
.on(completed: { FetchCache.updateLastFetchTime(forURL: cloneURL) })
}
/// Returns a signal that completes when the fetch completes successfully.
public func fetchRepository(repositoryFileURL: NSURL, remoteURL: GitURL? = nil, refspec: String? = nil) -> SignalProducer<String, CarthageError> {
precondition(repositoryFileURL.fileURL)
var arguments = [ "fetch", "--prune", "--quiet" ]
if let remoteURL = remoteURL {
arguments.append(remoteURL.URLString)
}
// Specify an explict refspec that fetches tags for pruning.
// See https://github.com/Carthage/Carthage/issues/1027 and `man git-fetch`.
arguments.append("refs/tags/*:refs/tags/*")
if let refspec = refspec {
arguments.append(refspec)
}
return launchGitTask(arguments, repositoryFileURL: repositoryFileURL)
.on(completed: { FetchCache.updateLastFetchTime(forURL: remoteURL) })
}
/// Sends each tag found in the given Git repository.
public func listTags(repositoryFileURL: NSURL) -> SignalProducer<String, CarthageError> {
return launchGitTask([ "tag", "--column=never" ], repositoryFileURL: repositoryFileURL)
.flatMap(.Concat) { (allTags: String) -> SignalProducer<String, CarthageError> in
return SignalProducer { observer, disposable in
allTags.enumerateSubstringsInRange(allTags.characters.indices, options: [ .ByLines, .Reverse ]) { line, substringRange, enclosingRange, stop in
if disposable.disposed {
stop = true
}
if let line = line {
observer.sendNext(line)
}
}
observer.sendCompleted()
}
}
}
/// Returns the text contents of the path at the given revision, or an error if
/// the path could not be loaded.
public func contentsOfFileInRepository(repositoryFileURL: NSURL, _ path: String, revision: String = "HEAD") -> SignalProducer<String, CarthageError> {
let showObject = "\(revision):\(path)"
return launchGitTask([ "show", showObject ], repositoryFileURL: repositoryFileURL)
}
/// Checks out the working tree of the given (ideally bare) repository, at the
/// specified revision, to the given folder. If the folder does not exist, it
/// will be created.
public func checkoutRepositoryToDirectory(repositoryFileURL: NSURL, _ workingDirectoryURL: NSURL, revision: String = "HEAD", shouldCloneSubmodule: Submodule -> Bool = { _ in true }) -> SignalProducer<(), CarthageError> {
return SignalProducer.attempt { () -> Result<[String: String], CarthageError> in
do {
try NSFileManager.defaultManager().createDirectoryAtURL(workingDirectoryURL, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
return .Failure(CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: workingDirectoryURL, reason: "Could not create working directory", underlyingError: error))
}
var environment = NSProcessInfo.processInfo().environment
environment["GIT_WORK_TREE"] = workingDirectoryURL.path!
return .Success(environment)
}
.flatMap(.Concat) { environment in launchGitTask([ "checkout", "--quiet", "--force", revision ], repositoryFileURL: repositoryFileURL, environment: environment) }
.then(cloneSubmodulesForRepository(repositoryFileURL, workingDirectoryURL, revision: revision, shouldCloneSubmodule: shouldCloneSubmodule))
}
/// Clones matching submodules for the given repository at the specified
/// revision, into the given working directory.
public func cloneSubmodulesForRepository(repositoryFileURL: NSURL, _ workingDirectoryURL: NSURL, revision: String = "HEAD", shouldCloneSubmodule: Submodule -> Bool = { _ in true }) -> SignalProducer<(), CarthageError> {
return submodulesInRepository(repositoryFileURL, revision: revision)
.flatMap(.Concat) { submodule -> SignalProducer<(), CarthageError> in
if shouldCloneSubmodule(submodule) {
return cloneSubmoduleInWorkingDirectory(submodule, workingDirectoryURL)
} else {
return .empty
}
}
.filter { _ in false }
}
/// Clones the given submodule into the working directory of its parent
/// repository, but without any Git metadata.
public func cloneSubmoduleInWorkingDirectory(submodule: Submodule, _ workingDirectoryURL: NSURL) -> SignalProducer<(), CarthageError> {
let submoduleDirectoryURL = workingDirectoryURL.appendingPathComponent(submodule.path, isDirectory: true)
let purgeGitDirectories = NSFileManager.defaultManager().carthage_enumeratorAtURL(submoduleDirectoryURL, includingPropertiesForKeys: [ NSURLIsDirectoryKey, NSURLNameKey ], options: [], catchErrors: true)
.flatMap(.Merge) { enumerator, URL -> SignalProducer<(), CarthageError> in
var name: AnyObject?
do {
try URL.getResourceValue(&name, forKey: NSURLNameKey)
} catch let error as NSError {
return SignalProducer(error: CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: submoduleDirectoryURL, reason: "could not enumerate name of descendant at \(URL.path!)", underlyingError: error))
}
if (name as? String) != ".git" {
return .empty
}
var isDirectory: AnyObject?
do {
try URL.getResourceValue(&isDirectory, forKey: NSURLIsDirectoryKey)
if isDirectory == nil {
return SignalProducer(error: CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: submoduleDirectoryURL, reason: "could not determine whether \(URL.path!) is a directory", underlyingError: nil))
}
} catch let error as NSError {
return SignalProducer(error: CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: submoduleDirectoryURL, reason: "could not determine whether \(URL.path!) is a directory", underlyingError: error))
}
if let directory = isDirectory?.boolValue where directory {
enumerator.skipDescendants()
}
do {
try NSFileManager.defaultManager().removeItemAtURL(URL)
return .empty
} catch let error as NSError {
return SignalProducer(error: CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: submoduleDirectoryURL, reason: "could not remove \(URL.path!)", underlyingError: error))
}
}
return SignalProducer.attempt { () -> Result<NSURL, CarthageError> in
do {
try NSFileManager.defaultManager().removeItemAtURL(submoduleDirectoryURL)
} catch let error as NSError {
return .Failure(CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: submoduleDirectoryURL, reason: "could not remove submodule checkout", underlyingError: error))
}
return .Success(workingDirectoryURL.appendingPathComponent(submodule.path))
}
.flatMap(.Concat) { submoduleDirectoryURL in cloneRepository(submodule.URL, submoduleDirectoryURL, bare: false) }
.then(checkoutSubmodule(submodule, submoduleDirectoryURL))
.then(purgeGitDirectories)
}
/// Recursively checks out the given submodule's revision, in its working
/// directory.
private func checkoutSubmodule(submodule: Submodule, _ submoduleWorkingDirectoryURL: NSURL) -> SignalProducer<(), CarthageError> {
return launchGitTask([ "checkout", "--quiet", submodule.SHA ], repositoryFileURL: submoduleWorkingDirectoryURL)
.then(launchGitTask([ "submodule", "--quiet", "update", "--init", "--recursive" ], repositoryFileURL: submoduleWorkingDirectoryURL))
.then(.empty)
}
/// Parses each key/value entry from the given config file contents, optionally
/// stripping a known prefix/suffix off of each key.
private func parseConfigEntries(contents: String, keyPrefix: String = "", keySuffix: String = "") -> SignalProducer<(String, String), NoError> {
let entries = contents.characters.split(allowEmptySlices: false) { $0 == "\0" }
return SignalProducer { observer, disposable in
for entry in entries {
if disposable.disposed {
break
}
let components = entry.split(1, allowEmptySlices: true) { $0 == "\n" }.map(String.init)
if components.count != 2 {
continue
}
let value = components[1]
let scanner = NSScanner(string: components[0])
if !scanner.scanString(keyPrefix, intoString: nil) {
continue
}
var key: NSString?
if !scanner.scanUpToString(keySuffix, intoString: &key) {
continue
}
if let key = key as? String {
observer.sendNext((key, value))
}
}
observer.sendCompleted()
}
}
/// Determines the SHA that the submodule at the given path is pinned to, in the
/// revision of the parent repository specified.
public func submoduleSHAForPath(repositoryFileURL: NSURL, _ path: String, revision: String = "HEAD") -> SignalProducer<String, CarthageError> {
let task = [ "ls-tree", "-z", revision, path ]
return launchGitTask(task, repositoryFileURL: repositoryFileURL)
.attemptMap { string in
// Example:
// 160000 commit 083fd81ecf00124cbdaa8f86ef10377737f6325a External/ObjectiveGit
let components = string.characters.split(3, allowEmptySlices: false) { $0 == " " || $0 == "\t" }
if components.count >= 3 {
return .Success(String(components[2]))
} else {
return .Failure(CarthageError.ParseError(description: "expected submodule commit SHA in output of task (\(task.joinWithSeparator(" "))) but encountered: \(string)"))
}
}
}
/// Returns each entry of `.gitmodules` found in the given repository revision,
/// or an empty signal if none exist.
internal func gitmodulesEntriesInRepository(repositoryFileURL: NSURL, revision: String?) -> SignalProducer<(name: String, path: String, URL: GitURL), CarthageError> {
var baseArguments = [ "config", "-z" ]
let modulesFile = ".gitmodules"
if let revision = revision {
let modulesObject = "\(revision):\(modulesFile)"
baseArguments += [ "--blob", modulesObject ]
} else {
// This is required to support `--no-use-submodules` checkouts.
// See https://github.com/Carthage/Carthage/issues/1029.
baseArguments += [ "--file", modulesFile ]
}
return launchGitTask(baseArguments + [ "--get-regexp", "submodule\\..*\\.path" ], repositoryFileURL: repositoryFileURL)
.flatMapError { _ in SignalProducer<String, NoError>.empty }
.flatMap(.Concat) { value in parseConfigEntries(value, keyPrefix: "submodule.", keySuffix: ".path") }
.flatMap(.Concat) { name, path -> SignalProducer<(name: String, path: String, URL: GitURL), CarthageError> in
return launchGitTask(baseArguments + [ "--get", "submodule.\(name).url" ], repositoryFileURL: repositoryFileURL)
.map { URLString in (name: name, path: path, URL: GitURL(URLString)) }
}
}
/// Returns the root directory of the given repository
///
/// If in bare repository, return the passed repo path as the root
/// else, return the path given by "git rev-parse --show-toplevel"
public func gitRootDirectoryForRepository(repositoryFileURL: NSURL) -> SignalProducer<NSURL, CarthageError> {
return launchGitTask([ "rev-parse", "--is-bare-repository" ], repositoryFileURL: repositoryFileURL)
.map { $0.stringByTrimmingCharactersInSet(.newlineCharacterSet()) }
.flatMap(.Concat) { isBareRepository -> SignalProducer<NSURL, CarthageError> in
if isBareRepository == "true" {
return SignalProducer(value: repositoryFileURL)
} else {
return launchGitTask([ "rev-parse", "--show-toplevel" ], repositoryFileURL: repositoryFileURL)
.map { $0.stringByTrimmingCharactersInSet(.newlineCharacterSet()) }
.map(NSURL.init)
}
}
}
/// Returns each submodule found in the given repository revision, or an empty
/// signal if none exist.
public func submodulesInRepository(repositoryFileURL: NSURL, revision: String = "HEAD") -> SignalProducer<Submodule, CarthageError> {
return gitmodulesEntriesInRepository(repositoryFileURL, revision: revision)
.flatMap(.Concat) { name, path, URL in
return gitRootDirectoryForRepository(repositoryFileURL)
.flatMap(.Concat) { actualRepoURL in
return submoduleSHAForPath(actualRepoURL, path, revision: revision)
.map { SHA in Submodule(name: name, path: path, URL: URL, SHA: SHA) }
}
}
}
/// Determines whether a branch exists for the given pattern in the given
/// repository.
///
/// If the specified file URL does not represent a valid Git repository, `false`
/// will be sent.
internal func branchExistsInRepository(repositoryFileURL: NSURL, pattern: String) -> SignalProducer<Bool, NoError> {
return ensureDirectoryExistsAtURL(repositoryFileURL)
.succeeded()
.flatMap(.Concat) { exists -> SignalProducer<Bool, NoError> in
if !exists { return .init(value: false) }
return zip(
launchGitTask([ "show-ref", pattern ], repositoryFileURL: repositoryFileURL).succeeded(),
launchGitTask([ "show-ref", "--tags", pattern ], repositoryFileURL: repositoryFileURL).succeeded()
)
.map { branch, tag in
return branch && !tag
}
}
}
/// Determines whether the specified revision identifies a valid commit.
///
/// If the specified file URL does not represent a valid Git repository, `false`
/// will be sent.
public func commitExistsInRepository(repositoryFileURL: NSURL, revision: String = "HEAD") -> SignalProducer<Bool, NoError> {
return ensureDirectoryExistsAtURL(repositoryFileURL)
.then(launchGitTask([ "rev-parse", "\(revision)^{commit}" ], repositoryFileURL: repositoryFileURL))
.then(.init(value: true))
.flatMapError { _ in .init(value: false) }
}
/// NSTask throws a hissy fit (a.k.a. exception) if the working directory
/// doesn't exist, so pre-emptively check for that.
private func ensureDirectoryExistsAtURL(fileURL: NSURL) -> SignalProducer<(), CarthageError> {
return SignalProducer { observer, disposable in
var isDirectory: ObjCBool = false
if NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!, isDirectory: &isDirectory) && isDirectory {
observer.sendCompleted()
} else {
observer.sendFailed(.ReadFailed(fileURL, nil))
}
}
}
/// Attempts to resolve the given reference into an object SHA.
public func resolveReferenceInRepository(repositoryFileURL: NSURL, _ reference: String) -> SignalProducer<String, CarthageError> {
return ensureDirectoryExistsAtURL(repositoryFileURL)
.then(launchGitTask([ "rev-parse", "\(reference)^{object}" ], repositoryFileURL: repositoryFileURL))
.map { string in string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
.mapError { _ in CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: repositoryFileURL, reason: "No object named \"\(reference)\" exists", underlyingError: nil) }
}
/// Attempts to resolve the given tag into an object SHA.
internal func resolveTagInRepository(repositoryFileURL: NSURL, _ tag: String) -> SignalProducer<String, CarthageError> {
return launchGitTask([ "show-ref", "--tags", "--hash", tag ], repositoryFileURL: repositoryFileURL)
.map { string in string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
.mapError { _ in CarthageError.RepositoryCheckoutFailed(workingDirectoryURL: repositoryFileURL, reason: "No tag named \"\(tag)\" exists", underlyingError: nil) }
}
/// Attempts to determine whether the given directory represents a Git
/// repository.
public func isGitRepository(directoryURL: NSURL) -> SignalProducer<Bool, NoError> {
return ensureDirectoryExistsAtURL(directoryURL)
.then(launchGitTask([ "rev-parse", "--git-dir", ], repositoryFileURL: directoryURL))
.map { outputIncludingLineEndings in
let relativeOrAbsoluteGitDirectory = outputIncludingLineEndings.stringByTrimmingCharactersInSet(.newlineCharacterSet())
var absoluteGitDirectory: String?
if (relativeOrAbsoluteGitDirectory as NSString).absolutePath {
absoluteGitDirectory = relativeOrAbsoluteGitDirectory
} else {
absoluteGitDirectory = directoryURL.appendingPathComponent(relativeOrAbsoluteGitDirectory).path
}
var isDirectory: ObjCBool = false
let directoryExists = absoluteGitDirectory.map { NSFileManager.defaultManager().fileExistsAtPath($0, isDirectory: &isDirectory) } ?? false
return directoryExists && isDirectory
}
.flatMapError { _ in SignalProducer(value: false) }
}
/// Adds the given submodule to the given repository, cloning from `fetchURL` if
/// the desired revision does not exist or the submodule needs to be cloned.
public func addSubmoduleToRepository(repositoryFileURL: NSURL, _ submodule: Submodule, _ fetchURL: GitURL) -> SignalProducer<(), CarthageError> {
let submoduleDirectoryURL = repositoryFileURL.appendingPathComponent(submodule.path, isDirectory: true)
return isGitRepository(submoduleDirectoryURL)
.map { isRepository in
// Check if the submodule is initialized/updated already.
return isRepository && NSFileManager.defaultManager().fileExistsAtPath(submoduleDirectoryURL.appendingPathComponent(".git").path!)
}
.flatMap(.Merge) { submoduleExists -> SignalProducer<(), CarthageError> in
if submoduleExists {
// Just check out and stage the correct revision.
return fetchRepository(submoduleDirectoryURL, remoteURL: fetchURL, refspec: "+refs/heads/*:refs/remotes/origin/*")
.then(launchGitTask([ "config", "--file", ".gitmodules", "submodule.\(submodule.name).url", submodule.URL.URLString ], repositoryFileURL: repositoryFileURL))
.then(launchGitTask([ "submodule", "--quiet", "sync", "--recursive" ], repositoryFileURL: repositoryFileURL))
.then(checkoutSubmodule(submodule, submoduleDirectoryURL))
.then(launchGitTask([ "add", "--force", submodule.path ], repositoryFileURL: repositoryFileURL))
.then(.empty)
} else {
let addSubmodule = launchGitTask([ "submodule", "--quiet", "add", "--force", "--name", submodule.name, "--", submodule.URL.URLString, submodule.path ], repositoryFileURL: repositoryFileURL)
// A .failure to add usually means the folder was already added
// to the index. That's okay.
.flatMapError { _ in SignalProducer<String, CarthageError>.empty }
// If it doesn't exist, clone and initialize a submodule from our
// local bare repository.
return cloneRepository(fetchURL, submoduleDirectoryURL, bare: false)
.then(launchGitTask([ "remote", "set-url", "origin", submodule.URL.URLString ], repositoryFileURL: submoduleDirectoryURL))
.then(checkoutSubmodule(submodule, submoduleDirectoryURL))
.then(addSubmodule)
.then(launchGitTask([ "submodule", "--quiet", "init", "--", submodule.path ], repositoryFileURL: repositoryFileURL))
.then(.empty)
}
}
}
/// Moves an item within a Git repository, or within a simple directory if a Git
/// repository is not found.
///
/// Sends the new URL of the item after moving.
public func moveItemInPossibleRepository(repositoryFileURL: NSURL, fromPath: String, toPath: String) -> SignalProducer<NSURL, CarthageError> {
let toURL = repositoryFileURL.appendingPathComponent(toPath)
let parentDirectoryURL = toURL.URLByDeletingLastPathComponent!
return SignalProducer<(), CarthageError>.attempt {
do {
try NSFileManager.defaultManager().createDirectoryAtURL(parentDirectoryURL, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
return .Failure(CarthageError.WriteFailed(parentDirectoryURL, error))
}
return .Success(())
}
.then(isGitRepository(repositoryFileURL)
.promoteErrors(CarthageError.self))
.flatMap(.Merge) { isRepository -> SignalProducer<NSURL, CarthageError> in
if isRepository {
return launchGitTask([ "mv", "-k", fromPath, toPath ], repositoryFileURL: repositoryFileURL)
.then(SignalProducer(value: toURL))
} else {
let fromURL = repositoryFileURL.appendingPathComponent(fromPath)
do {
try NSFileManager.defaultManager().moveItemAtURL(fromURL, toURL: toURL)
return SignalProducer(value: toURL)
} catch let error as NSError {
return SignalProducer(error: .WriteFailed(toURL, error))
}
}
}
}