|
| 1 | +// |
| 2 | +// This file was sourced from: |
| 3 | +// https://github.com/apple/swift-testing/blob/7f39433a0a78ccc92b541597c542b70f68de75e6/Sources/GitStatus/main.swift |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// |
| 9 | + |
| 10 | +import Foundation |
| 11 | +import SwiftSyntax |
| 12 | +import SwiftSyntaxBuilder |
| 13 | + |
| 14 | +// Resolve arguments to the tool. |
| 15 | +let repoPath = CommandLine.arguments[1] |
| 16 | +let generatedSourceURL = URL(fileURLWithPath: CommandLine.arguments[2], isDirectory: false) |
| 17 | + |
| 18 | +/// Run the `git` tool and process the output it writes to standard output. |
| 19 | +/// |
| 20 | +/// - Parameters: |
| 21 | +/// - arguments: The arguments to pass to `git`. |
| 22 | +/// - maxOutputCount: The maximum amount of output to read and return. |
| 23 | +/// |
| 24 | +/// - Returns: A string containing the `git` command's output, up to |
| 25 | +/// `maxOutputCount` UTF-8-encoded bytes, or `nil` if the command failed or |
| 26 | +/// the output could not be read. |
| 27 | +func _runGit(passing arguments: String..., readingUpToCount maxOutputCount: Int) -> String? { |
| 28 | + #if os(macOS) || os(Linux) || os(Windows) |
| 29 | + let path: String |
| 30 | + var arguments = ["-C", repoPath] + arguments |
| 31 | + #if os(Windows) |
| 32 | + path = "C:\\Program Files\\Git\\cmd\\git.exe" |
| 33 | + #else |
| 34 | + path = "/usr/bin/env" |
| 35 | + arguments = CollectionOfOne("git") + arguments |
| 36 | + #endif |
| 37 | + |
| 38 | + let process = Process() |
| 39 | + process.executableURL = URL(fileURLWithPath: path, isDirectory: false) |
| 40 | + process.arguments = arguments |
| 41 | + |
| 42 | + let stdoutPipe = Pipe() |
| 43 | + process.standardOutput = stdoutPipe |
| 44 | + process.standardError = nil |
| 45 | + do { |
| 46 | + try process.run() |
| 47 | + } catch { |
| 48 | + return nil |
| 49 | + } |
| 50 | + defer { |
| 51 | + process.terminate() |
| 52 | + } |
| 53 | + guard let output = try? stdoutPipe.fileHandleForReading.read(upToCount: maxOutputCount) else { |
| 54 | + return nil |
| 55 | + } |
| 56 | + return String(data: output, encoding: .utf8) |
| 57 | + #else |
| 58 | + return nil |
| 59 | + #endif |
| 60 | +} |
| 61 | + |
| 62 | +// The current Git tag, if available. |
| 63 | +let currentGitTag = _runGit(passing: "describe", "--exact-match", "--tags", readingUpToCount: 40)? |
| 64 | + .split(whereSeparator: \.isNewline) |
| 65 | + .first |
| 66 | + .map(String.init) |
| 67 | + |
| 68 | +// The current Git commit hash, if available. |
| 69 | +let currentGitCommitHash = _runGit(passing: "rev-parse", "HEAD", readingUpToCount: 40)? |
| 70 | + .split(whereSeparator: \.isNewline) |
| 71 | + .first |
| 72 | + .map(String.init) |
| 73 | + |
| 74 | +// Whether or not the Git repository has uncommitted changes, if available. |
| 75 | +let gitHasUncommittedChanges = _runGit(passing: "status", "-s", readingUpToCount: 1) |
| 76 | + .map { !$0.isEmpty } ?? false |
| 77 | + |
| 78 | +// Figure out what value to emit for the version: |
| 79 | +// - If the repository is sitting at a tag with no uncommitted changes, use the tag. |
| 80 | +// - Otherwise, use the commit hash (with a "there are changes" marker if needed.) |
| 81 | +// - Finally, fall back to nil if nothing else is available. |
| 82 | +let sourceCode: DeclSyntax = if !gitHasUncommittedChanges, let currentGitTag { |
| 83 | + """ |
| 84 | + var _toolVersion: String? { |
| 85 | + \(literal: currentGitTag) |
| 86 | + } |
| 87 | + """ |
| 88 | +} else if let currentGitCommitHash { |
| 89 | + if gitHasUncommittedChanges { |
| 90 | + """ |
| 91 | + var _toolVersion: String? { |
| 92 | + \(literal: currentGitCommitHash) + " (modified)" |
| 93 | + } |
| 94 | + """ |
| 95 | + } else { |
| 96 | + """ |
| 97 | + var _toolVersion: String? { |
| 98 | + \(literal: currentGitCommitHash) |
| 99 | + } |
| 100 | + """ |
| 101 | + } |
| 102 | +} else { |
| 103 | + """ |
| 104 | + var _toolVersion: String? { |
| 105 | + nil |
| 106 | + } |
| 107 | + """ |
| 108 | +} |
| 109 | + |
| 110 | +// Write the generated Swift file to the specified destination path. |
| 111 | +try String(describing: sourceCode).write(to: generatedSourceURL, atomically: false, encoding: .utf8) |
0 commit comments