Merge pull request #77473 from mayflower/worktrees
lib.commitIdFromGitRepo: support git-worktree
This commit is contained in:
commit
247c25d302
|
@ -100,7 +100,7 @@ let
|
||||||
inherit (sources) pathType pathIsDirectory cleanSourceFilter
|
inherit (sources) pathType pathIsDirectory cleanSourceFilter
|
||||||
cleanSource sourceByRegex sourceFilesBySuffices
|
cleanSource sourceByRegex sourceFilesBySuffices
|
||||||
commitIdFromGitRepo cleanSourceWith pathHasContext
|
commitIdFromGitRepo cleanSourceWith pathHasContext
|
||||||
canCleanSource;
|
canCleanSource pathIsRegularFile;
|
||||||
inherit (modules) evalModules unifyModuleSyntax
|
inherit (modules) evalModules unifyModuleSyntax
|
||||||
applyIfFunction mergeModules
|
applyIfFunction mergeModules
|
||||||
mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions
|
mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions
|
||||||
|
|
|
@ -9,6 +9,9 @@ rec {
|
||||||
# Returns true if the path exists and is a directory, false otherwise
|
# Returns true if the path exists and is a directory, false otherwise
|
||||||
pathIsDirectory = p: if builtins.pathExists p then (pathType p) == "directory" else false;
|
pathIsDirectory = p: if builtins.pathExists p then (pathType p) == "directory" else false;
|
||||||
|
|
||||||
|
# Returns true if the path exists and is a regular file, false otherwise
|
||||||
|
pathIsRegularFile = p: if builtins.pathExists p then (pathType p) == "regular" else false;
|
||||||
|
|
||||||
# Bring in a path as a source, filtering out all Subversion and CVS
|
# Bring in a path as a source, filtering out all Subversion and CVS
|
||||||
# directories, as well as backup files (*~).
|
# directories, as well as backup files (*~).
|
||||||
cleanSourceFilter = name: type: let baseName = baseNameOf (toString name); in ! (
|
cleanSourceFilter = name: type: let baseName = baseNameOf (toString name); in ! (
|
||||||
|
@ -110,24 +113,43 @@ rec {
|
||||||
with builtins;
|
with builtins;
|
||||||
let fileName = toString path + "/" + file;
|
let fileName = toString path + "/" + file;
|
||||||
packedRefsName = toString path + "/packed-refs";
|
packedRefsName = toString path + "/packed-refs";
|
||||||
in if lib.pathExists fileName
|
in if pathIsRegularFile path
|
||||||
|
# Resolve git worktrees. See gitrepository-layout(5)
|
||||||
|
then
|
||||||
|
let m = match "^gitdir: (.*)$" (lib.fileContents path);
|
||||||
|
in if m == null
|
||||||
|
then throw ("File contains no gitdir reference: " + path)
|
||||||
|
else
|
||||||
|
let gitDir = lib.head m;
|
||||||
|
commonDir' = if pathIsRegularFile "${gitDir}/commondir"
|
||||||
|
then lib.fileContents "${gitDir}/commondir"
|
||||||
|
else gitDir;
|
||||||
|
commonDir = if lib.hasPrefix "/" commonDir'
|
||||||
|
then commonDir'
|
||||||
|
else toString (/. + "${gitDir}/${commonDir'}");
|
||||||
|
refFile = lib.removePrefix "${commonDir}/" "${gitDir}/${file}";
|
||||||
|
in readCommitFromFile refFile commonDir
|
||||||
|
|
||||||
|
else if pathIsRegularFile fileName
|
||||||
|
# Sometimes git stores the commitId directly in the file but
|
||||||
|
# sometimes it stores something like: «ref: refs/heads/branch-name»
|
||||||
then
|
then
|
||||||
let fileContent = lib.fileContents fileName;
|
let fileContent = lib.fileContents fileName;
|
||||||
# Sometimes git stores the commitId directly in the file but
|
|
||||||
# sometimes it stores something like: «ref: refs/heads/branch-name»
|
|
||||||
matchRef = match "^ref: (.*)$" fileContent;
|
matchRef = match "^ref: (.*)$" fileContent;
|
||||||
in if matchRef == null
|
in if matchRef == null
|
||||||
then fileContent
|
then fileContent
|
||||||
else readCommitFromFile (lib.head matchRef) path
|
else readCommitFromFile (lib.head matchRef) path
|
||||||
|
|
||||||
|
else if pathIsRegularFile packedRefsName
|
||||||
# Sometimes, the file isn't there at all and has been packed away in the
|
# Sometimes, the file isn't there at all and has been packed away in the
|
||||||
# packed-refs file, so we have to grep through it:
|
# packed-refs file, so we have to grep through it:
|
||||||
else if lib.pathExists packedRefsName
|
|
||||||
then
|
then
|
||||||
let fileContent = readFile packedRefsName;
|
let fileContent = readFile packedRefsName;
|
||||||
matchRef = match (".*\n([^\n ]*) " + file + "\n.*") fileContent;
|
matchRef = match (".*\n([^\n ]*) " + file + "\n.*") fileContent;
|
||||||
in if matchRef == null
|
in if matchRef == null
|
||||||
then throw ("Could not find " + file + " in " + packedRefsName)
|
then throw ("Could not find " + file + " in " + packedRefsName)
|
||||||
else lib.head matchRef
|
else lib.head matchRef
|
||||||
|
|
||||||
else throw ("Not a .git directory: " + path);
|
else throw ("Not a .git directory: " + path);
|
||||||
in readCommitFromFile "HEAD";
|
in readCommitFromFile "HEAD";
|
||||||
|
|
||||||
|
|
|
@ -191,7 +191,7 @@ rec {
|
||||||
let
|
let
|
||||||
revisionFile = "${toString ./..}/.git-revision";
|
revisionFile = "${toString ./..}/.git-revision";
|
||||||
gitRepo = "${toString ./..}/.git";
|
gitRepo = "${toString ./..}/.git";
|
||||||
in if lib.pathIsDirectory gitRepo
|
in if builtins.pathExists gitRepo
|
||||||
then lib.commitIdFromGitRepo gitRepo
|
then lib.commitIdFromGitRepo gitRepo
|
||||||
else if lib.pathExists revisionFile then lib.fileContents revisionFile
|
else if lib.pathExists revisionFile then lib.fileContents revisionFile
|
||||||
else default;
|
else default;
|
||||||
|
|
|
@ -91,8 +91,8 @@ in
|
||||||
# These defaults are set here rather than up there so that
|
# These defaults are set here rather than up there so that
|
||||||
# changing them would not rebuild the manual
|
# changing them would not rebuild the manual
|
||||||
version = mkDefault (cfg.release + cfg.versionSuffix);
|
version = mkDefault (cfg.release + cfg.versionSuffix);
|
||||||
revision = mkIf (pathIsDirectory gitRepo) (mkDefault gitCommitId);
|
revision = mkIf (pathExists gitRepo) (mkDefault gitCommitId);
|
||||||
versionSuffix = mkIf (pathIsDirectory gitRepo) (mkDefault (".git." + gitCommitId));
|
versionSuffix = mkIf (pathExists gitRepo) (mkDefault (".git." + gitCommitId));
|
||||||
};
|
};
|
||||||
|
|
||||||
# Generate /etc/os-release. See
|
# Generate /etc/os-release. See
|
||||||
|
|
Loading…
Reference in New Issue