2020-03-30 15:03:28 +03:00
# include "fetchers.hh"
2023-10-25 07:43:36 +03:00
# include "users.hh"
2020-03-30 15:03:28 +03:00
# include "cache.hh"
2020-01-21 17:27:53 +02:00
# include "globals.hh"
# include "tarfile.hh"
# include "store-api.hh"
2020-09-21 19:22:45 +03:00
# include "url-parts.hh"
2021-08-02 14:39:48 +03:00
# include "pathlocks.hh"
2023-10-25 07:43:36 +03:00
# include "processes.hh"
2022-05-04 15:32:21 +03:00
# include "git.hh"
2023-10-25 19:55:08 +03:00
# include "fs-input-accessor.hh"
2023-11-14 15:01:38 +02:00
# include "mounted-input-accessor.hh"
2023-10-25 19:55:08 +03:00
# include "git-utils.hh"
2020-01-21 17:27:53 +02:00
2022-03-01 03:29:34 +02:00
# include "fetch-settings.hh"
2021-03-03 03:13:20 +02:00
# include <regex>
2022-01-29 21:22:55 +02:00
# include <string.h>
2020-01-21 17:27:53 +02:00
# include <sys/time.h>
2021-04-19 21:31:58 +03:00
# include <sys/wait.h>
2020-01-21 17:27:53 +02:00
using namespace std : : string_literals ;
namespace nix : : fetchers {
2022-12-07 13:58:58 +02:00
2022-01-29 21:12:01 +02:00
namespace {
2020-01-21 17:27:53 +02:00
2021-09-24 17:01:27 +03:00
// Explicit initial branch of our bare repo to suppress warnings from new version of git.
// The value itself does not matter, since we always fetch a specific revision or branch.
// It is set with `-c init.defaultBranch=` instead of `--initial-branch=` to stay compatible with
// old version of git, which will ignore unrecognized `-c` options.
2022-01-29 21:12:01 +02:00
const std : : string gitInitialBranch = " __nix_dummy_branch " ;
2021-09-24 17:01:27 +03:00
2022-12-07 13:58:58 +02:00
bool isCacheFileWithinTtl ( time_t now , const struct stat & st )
2022-05-02 14:37:53 +03:00
{
2021-03-03 03:13:20 +02:00
return st . st_mtime + settings . tarballTtl > now ;
}
2022-12-07 13:58:58 +02:00
bool touchCacheFile ( const Path & path , time_t touch_time )
2022-01-29 21:22:55 +02:00
{
2022-12-07 13:58:58 +02:00
struct timeval times [ 2 ] ;
times [ 0 ] . tv_sec = touch_time ;
times [ 0 ] . tv_usec = 0 ;
times [ 1 ] . tv_sec = touch_time ;
times [ 1 ] . tv_usec = 0 ;
2022-01-29 21:22:55 +02:00
2022-12-07 13:58:58 +02:00
return lutimes ( path . c_str ( ) , times ) = = 0 ;
2022-01-29 21:22:55 +02:00
}
2022-12-07 13:58:58 +02:00
Path getCachePath ( std : : string_view key )
2022-01-29 21:22:55 +02:00
{
2021-03-03 03:13:20 +02:00
return getCacheDir ( ) + " /nix/gitv3/ " +
2023-10-13 04:48:15 +03:00
hashString ( htSHA256 , key ) . to_string ( HashFormat : : Base32 , false ) ;
2021-03-03 03:13:20 +02:00
}
// Returns the name of the HEAD branch.
//
// Returns the head branch name as reported by git ls-remote --symref, e.g., if
// ls-remote returns the output below, "main" is returned based on the ref line.
//
// ref: refs/heads/main HEAD
// ...
2022-01-29 21:12:01 +02:00
std : : optional < std : : string > readHead ( const Path & path )
2021-03-03 03:13:20 +02:00
{
2023-05-18 13:18:34 +03:00
auto [ status , output ] = runProgram ( RunOptions {
2021-03-03 03:13:20 +02:00
. program = " git " ,
2022-12-07 13:58:58 +02:00
// FIXME: use 'HEAD' to avoid returning all refs
2021-03-03 03:13:20 +02:00
. args = { " ls-remote " , " --symref " , path } ,
2023-05-18 13:18:34 +03:00
. isInteractive = true ,
2021-03-03 03:13:20 +02:00
} ) ;
2022-12-07 13:58:58 +02:00
if ( status ! = 0 ) return std : : nullopt ;
2021-03-03 03:13:20 +02:00
2022-04-30 01:30:00 +03:00
std : : string_view line = output ;
line = line . substr ( 0 , line . find ( " \n " ) ) ;
2022-05-04 15:32:21 +03:00
if ( const auto parseResult = git : : parseLsRemoteLine ( line ) ) {
switch ( parseResult - > kind ) {
case git : : LsRemoteRefLine : : Kind : : Symbolic :
debug ( " resolved HEAD ref '%s' for repo '%s' " , parseResult - > target , path ) ;
break ;
case git : : LsRemoteRefLine : : Kind : : Object :
debug ( " resolved HEAD rev '%s' for repo '%s' " , parseResult - > target , path ) ;
break ;
}
return parseResult - > target ;
2021-03-03 03:13:20 +02:00
}
return std : : nullopt ;
}
2022-01-29 21:22:55 +02:00
// Persist the HEAD ref from the remote repo in the local cached repo.
2022-12-07 13:58:58 +02:00
bool storeCachedHead ( const std : : string & actualUrl , const std : : string & headRef )
2022-01-29 21:22:55 +02:00
{
Path cacheDir = getCachePath ( actualUrl ) ;
try {
2022-12-07 13:58:58 +02:00
runProgram ( " git " , true , { " -C " , cacheDir , " --git-dir " , " . " , " symbolic-ref " , " -- " , " HEAD " , headRef } ) ;
2022-01-29 21:22:55 +02:00
} catch ( ExecError & e ) {
if ( ! WIFEXITED ( e . status ) ) throw ;
return false ;
}
/* No need to touch refs/HEAD, because `git symbolic-ref` updates the mtime. */
return true ;
}
2022-12-07 13:58:58 +02:00
std : : optional < std : : string > readHeadCached ( const std : : string & actualUrl )
2020-02-04 22:55:57 +02:00
{
2021-03-03 03:13:20 +02:00
// Create a cache path to store the branch of the HEAD ref. Append something
// in front of the URL to prevent collision with the repository itself.
2022-01-29 21:22:55 +02:00
Path cacheDir = getCachePath ( actualUrl ) ;
Path headRefFile = cacheDir + " /HEAD " ;
2021-03-03 03:13:20 +02:00
time_t now = time ( 0 ) ;
struct stat st ;
std : : optional < std : : string > cachedRef ;
2022-01-29 21:22:55 +02:00
if ( stat ( headRefFile . c_str ( ) , & st ) = = 0 ) {
cachedRef = readHead ( cacheDir ) ;
if ( cachedRef ! = std : : nullopt & &
* cachedRef ! = gitInitialBranch & &
2022-12-07 13:58:58 +02:00
isCacheFileWithinTtl ( now , st ) )
{
2021-03-03 03:13:20 +02:00
debug ( " using cached HEAD ref '%s' for repo '%s' " , * cachedRef , actualUrl ) ;
return cachedRef ;
}
}
auto ref = readHead ( actualUrl ) ;
2022-12-07 13:58:58 +02:00
if ( ref ) return ref ;
2021-03-03 03:13:20 +02:00
if ( cachedRef ) {
// If the cached git ref is expired in fetch() below, and the 'git fetch'
// fails, it falls back to continuing with the most recent version.
// This function must behave the same way, so we return the expired
// cached ref here.
warn ( " could not get HEAD ref for repository '%s'; using expired cached ref '%s' " , actualUrl , * cachedRef ) ;
return * cachedRef ;
}
return std : : nullopt ;
2020-02-04 22:55:57 +02:00
}
2023-11-09 17:48:41 +02:00
std : : vector < PublicKey > getPublicKeys ( const Attrs & attrs )
2020-04-07 14:45:17 +03:00
{
2023-10-20 22:16:56 +03:00
std : : vector < PublicKey > publicKeys ;
if ( attrs . contains ( " publicKeys " ) ) {
nlohmann : : json publicKeysJson = nlohmann : : json : : parse ( getStrAttr ( attrs , " publicKeys " ) ) ;
ensureType ( publicKeysJson , nlohmann : : json : : value_t : : array ) ;
publicKeys = publicKeysJson . get < std : : vector < PublicKey > > ( ) ;
}
if ( attrs . contains ( " publicKey " ) )
publicKeys . push_back ( PublicKey { maybeGetStrAttr ( attrs , " keytype " ) . value_or ( " ssh-ed25519 " ) , getStrAttr ( attrs , " publicKey " ) } ) ;
return publicKeys ;
}
2022-01-29 21:12:01 +02:00
} // end namespace
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
struct GitInputScheme : InputScheme
2020-01-21 17:27:53 +02:00
{
2023-08-01 17:07:20 +03:00
std : : optional < Input > inputFromURL ( const ParsedURL & url , bool requireTree ) const override
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
{
if ( url . scheme ! = " git " & &
url . scheme ! = " git+http " & &
url . scheme ! = " git+https " & &
url . scheme ! = " git+ssh " & &
url . scheme ! = " git+file " ) return { } ;
2020-01-21 17:27:53 +02:00
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
auto url2 ( url ) ;
if ( hasPrefix ( url2 . scheme , " git+ " ) ) url2 . scheme = std : : string ( url2 . scheme , 4 ) ;
url2 . query . clear ( ) ;
2020-01-31 20:16:40 +02:00
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
Attrs attrs ;
attrs . emplace ( " type " , " git " ) ;
2020-01-21 17:27:53 +02:00
2022-12-07 13:58:58 +02:00
for ( auto & [ name , value ] : url . query ) {
2023-10-20 22:16:56 +03:00
if ( name = = " rev " | | name = = " ref " | | name = = " keytype " | | name = = " publicKey " | | name = = " publicKeys " )
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
attrs . emplace ( name , value ) ;
2023-10-20 22:16:56 +03:00
else if ( name = = " shallow " | | name = = " submodules " | | name = = " allRefs " | | name = = " verifyCommit " )
2020-07-01 15:57:59 +03:00
attrs . emplace ( name , Explicit < bool > { value = = " 1 " } ) ;
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
else
url2 . query . emplace ( name , value ) ;
}
attrs . emplace ( " url " , url2 . to_string ( ) ) ;
return inputFromAttrs ( attrs ) ;
2020-01-21 17:27:53 +02:00
}
2023-10-30 16:14:27 +02:00
std : : string_view schemeName ( ) const override
2020-01-21 17:27:53 +02:00
{
2023-10-30 16:14:27 +02:00
return " git " ;
}
2020-01-21 17:27:53 +02:00
2023-10-30 16:14:27 +02:00
StringSet allowedAttrs ( ) const override
{
return {
" url " ,
" ref " ,
" rev " ,
" shallow " ,
" submodules " ,
" lastModified " ,
" revCount " ,
" narHash " ,
" allRefs " ,
" name " ,
" dirtyRev " ,
" dirtyShortRev " ,
2023-10-20 22:16:56 +03:00
" verifyCommit " ,
" keytype " ,
" publicKey " ,
" publicKeys " ,
2023-10-30 16:14:27 +02:00
} ;
}
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
2023-10-30 16:14:27 +02:00
std : : optional < Input > inputFromAttrs ( const Attrs & attrs ) const override
{
2023-10-20 22:16:56 +03:00
for ( auto & [ name , _ ] : attrs )
if ( name = = " verifyCommit "
| | name = = " keytype "
| | name = = " publicKey "
| | name = = " publicKeys " )
experimentalFeatureSettings . require ( Xp : : VerifiedFetches ) ;
maybeGetBoolAttr ( attrs , " verifyCommit " ) ;
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
if ( auto ref = maybeGetStrAttr ( attrs , " ref " ) ) {
2020-06-03 17:15:22 +03:00
if ( std : : regex_search ( * ref , badGitRefRegex ) )
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
throw BadURL ( " invalid Git branch/tag name '%s' " , * ref ) ;
}
2020-01-21 17:27:53 +02:00
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
Input input ;
input . attrs = attrs ;
2023-09-28 17:52:28 +03:00
auto url = fixGitURL ( getStrAttr ( attrs , " url " ) ) ;
parseURL ( url ) ;
input . attrs [ " url " ] = url ;
2023-11-15 15:43:30 +02:00
getShallowAttr ( input ) ;
getSubmodulesAttr ( input ) ;
getAllRefsAttr ( input ) ;
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
return input ;
}
2020-01-21 17:27:53 +02:00
2022-12-07 13:58:58 +02:00
ParsedURL toURL ( const Input & input ) const override
2020-01-21 17:27:53 +02:00
{
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
auto url = parseURL ( getStrAttr ( input . attrs , " url " ) ) ;
if ( url . scheme ! = " git " ) url . scheme = " git+ " + url . scheme ;
if ( auto rev = input . getRev ( ) ) url . query . insert_or_assign ( " rev " , rev - > gitRev ( ) ) ;
if ( auto ref = input . getRef ( ) ) url . query . insert_or_assign ( " ref " , * ref ) ;
2023-11-15 15:43:30 +02:00
if ( getShallowAttr ( input ) )
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
url . query . insert_or_assign ( " shallow " , " 1 " ) ;
2023-11-15 15:43:30 +02:00
if ( getSubmodulesAttr ( input ) )
url . query . insert_or_assign ( " submodules " , " 1 " ) ;
2023-10-20 22:16:56 +03:00
if ( maybeGetBoolAttr ( input . attrs , " verifyCommit " ) . value_or ( false ) )
url . query . insert_or_assign ( " verifyCommit " , " 1 " ) ;
auto publicKeys = getPublicKeys ( input . attrs ) ;
if ( publicKeys . size ( ) = = 1 ) {
url . query . insert_or_assign ( " keytype " , publicKeys . at ( 0 ) . type ) ;
url . query . insert_or_assign ( " publicKey " , publicKeys . at ( 0 ) . key ) ;
}
else if ( publicKeys . size ( ) > 1 )
url . query . insert_or_assign ( " publicKeys " , publicKeys_to_string ( publicKeys ) ) ;
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
return url ;
2020-01-21 17:27:53 +02:00
}
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
Input applyOverrides (
const Input & input ,
std : : optional < std : : string > ref ,
2022-12-07 13:58:58 +02:00
std : : optional < Hash > rev ) const override
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
{
auto res ( input ) ;
if ( rev ) res . attrs . insert_or_assign ( " rev " , rev - > gitRev ( ) ) ;
if ( ref ) res . attrs . insert_or_assign ( " ref " , * ref ) ;
if ( ! res . getRef ( ) & & res . getRev ( ) )
throw Error ( " Git input '%s' has a commit hash but no branch/tag name " , res . to_string ( ) ) ;
return res ;
}
2022-12-07 13:58:58 +02:00
void clone ( const Input & input , const Path & destDir ) const override
2020-01-21 17:27:53 +02:00
{
2023-10-25 19:55:08 +03:00
auto repoInfo = getRepoInfo ( input ) ;
2020-01-21 17:27:53 +02:00
Strings args = { " clone " } ;
2023-10-25 19:55:08 +03:00
args . push_back ( repoInfo . url ) ;
2020-01-21 17:27:53 +02:00
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
if ( auto ref = input . getRef ( ) ) {
2020-01-21 17:27:53 +02:00
args . push_back ( " --branch " ) ;
args . push_back ( * ref ) ;
}
2020-07-20 21:13:37 +03:00
if ( input . getRev ( ) ) throw UnimplementedError ( " cloning a specific revision is not implemented " ) ;
2020-01-21 17:27:53 +02:00
args . push_back ( destDir ) ;
2023-05-18 13:18:34 +03:00
runProgram ( " git " , true , args , { } , true ) ;
2020-01-21 17:27:53 +02:00
}
2023-10-25 19:18:15 +03:00
std : : optional < Path > getSourcePath ( const Input & input ) const override
2020-01-21 17:27:53 +02:00
{
2023-10-25 19:55:08 +03:00
auto repoInfo = getRepoInfo ( input ) ;
if ( repoInfo . isLocal ) return repoInfo . url ;
return std : : nullopt ;
2020-01-21 17:27:53 +02:00
}
2020-03-16 14:20:32 +02:00
2023-10-25 19:18:15 +03:00
void putFile (
const Input & input ,
const CanonPath & path ,
std : : string_view contents ,
std : : optional < std : : string > commitMsg ) const override
2020-02-02 17:32:46 +02:00
{
2023-10-25 19:55:08 +03:00
auto repoInfo = getRepoInfo ( input ) ;
if ( ! repoInfo . isLocal )
2023-10-25 19:18:15 +03:00
throw Error ( " cannot commit '%s' to Git repository '%s' because it's not a working tree " , path , input . to_string ( ) ) ;
2023-10-25 19:55:08 +03:00
writeFile ( ( CanonPath ( repoInfo . url ) + path ) . abs ( ) , contents ) ;
2020-02-05 15:48:49 +02:00
2020-02-02 17:32:46 +02:00
runProgram ( " git " , true ,
2023-10-25 19:55:08 +03:00
{ " -C " , repoInfo . url , " --git-dir " , repoInfo . gitDir , " add " , " --intent-to-add " , " -- " , std : : string ( path . rel ( ) ) } ) ;
2020-02-05 15:48:49 +02:00
if ( commitMsg )
runProgram ( " git " , true ,
2023-10-25 19:55:08 +03:00
{ " -C " , repoInfo . url , " --git-dir " , repoInfo . gitDir , " commit " , std : : string ( path . rel ( ) ) , " -m " , * commitMsg } ) ;
2020-02-02 17:32:46 +02:00
}
2023-10-25 19:55:08 +03:00
struct RepoInfo
2020-01-21 17:27:53 +02:00
{
2023-10-25 19:55:08 +03:00
/* Whether this is a local, non-bare repository. */
bool isLocal = false ;
/* Working directory info: the complete list of files, and
whether the working directory is dirty compared to HEAD . */
GitRepo : : WorkdirInfo workdirInfo ;
2023-11-14 14:38:03 +02:00
/* URL of the repo, or its path if isLocal. Never a `file` URL. */
2023-10-25 19:55:08 +03:00
std : : string url ;
void warnDirty ( ) const
{
if ( workdirInfo . isDirty ) {
if ( ! fetchSettings . allowDirty )
throw Error ( " Git tree '%s' is dirty " , url ) ;
if ( fetchSettings . warnDirty )
warn ( " Git tree '%s' is dirty " , url ) ;
}
}
std : : string gitDir = " .git " ;
} ;
2023-11-15 15:43:30 +02:00
bool getShallowAttr ( const Input & input ) const
{
return maybeGetBoolAttr ( input . attrs , " shallow " ) . value_or ( false ) ;
}
2023-10-25 19:55:08 +03:00
bool getSubmodulesAttr ( const Input & input ) const
{
return maybeGetBoolAttr ( input . attrs , " submodules " ) . value_or ( false ) ;
}
2023-11-15 15:43:30 +02:00
bool getAllRefsAttr ( const Input & input ) const
{
return maybeGetBoolAttr ( input . attrs , " allRefs " ) . value_or ( false ) ;
}
2023-10-25 19:55:08 +03:00
RepoInfo getRepoInfo ( const Input & input ) const
{
auto checkHashType = [ & ] ( const std : : optional < Hash > & hash )
{
if ( hash . has_value ( ) & & ! ( hash - > type = = htSHA1 | | hash - > type = = htSHA256 ) )
throw Error ( " Hash '%s' is not supported by Git. Supported types are sha1 and sha256. " , hash - > to_string ( HashFormat : : Base16 , true ) ) ;
} ;
if ( auto rev = input . getRev ( ) )
checkHashType ( rev ) ;
2023-11-15 15:43:30 +02:00
RepoInfo repoInfo ;
2023-10-25 19:55:08 +03:00
2021-02-21 18:08:28 +02:00
// file:// URIs are normally not cloned (but otherwise treated the
// same as remote URIs, i.e. we don't use the working tree or
// HEAD). Exception: If _NIX_FORCE_HTTP is set, or the repo is a bare git
// repo, treat as a remote URI to force a clone.
2020-01-21 17:27:53 +02:00
static bool forceHttp = getEnv ( " _NIX_FORCE_HTTP " ) = = " 1 " ; // for testing
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
auto url = parseURL ( getStrAttr ( input . attrs , " url " ) ) ;
2021-02-21 18:08:28 +02:00
bool isBareRepository = url . scheme = = " file " & & ! pathExists ( url . path + " /.git " ) ;
2023-10-25 19:55:08 +03:00
repoInfo . isLocal = url . scheme = = " file " & & ! forceHttp & & ! isBareRepository ;
repoInfo . url = repoInfo . isLocal ? url . path : url . base ;
// If this is a local directory and no ref or revision is
// given, then allow the use of an unclean working tree.
if ( ! input . getRef ( ) & & ! input . getRev ( ) & & repoInfo . isLocal )
repoInfo . workdirInfo = GitRepo : : openRepo ( CanonPath ( repoInfo . url ) ) - > getWorkdirInfo ( ) ;
return repoInfo ;
2020-01-21 17:27:53 +02:00
}
2023-10-25 19:55:08 +03:00
uint64_t getLastModified ( const RepoInfo & repoInfo , const std : : string & repoDir , const Hash & rev ) const
2020-01-21 17:27:53 +02:00
{
2023-10-25 19:55:08 +03:00
Attrs key { { " _what " , " gitLastModified " } , { " rev " , rev . gitRev ( ) } } ;
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
auto cache = getCache ( ) ;
2021-07-05 14:09:46 +03:00
2023-10-25 19:55:08 +03:00
if ( auto res = cache - > lookup ( key ) )
return getIntAttr ( * res , " lastModified " ) ;
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
auto lastModified = GitRepo : : openRepo ( CanonPath ( repoDir ) ) - > getLastModified ( rev ) ;
2020-03-17 23:32:26 +02:00
2023-10-25 19:55:08 +03:00
cache - > upsert ( key , Attrs { { " lastModified " , lastModified } } ) ;
return lastModified ;
}
uint64_t getRevCount ( const RepoInfo & repoInfo , const std : : string & repoDir , const Hash & rev ) const
{
Attrs key { { " _what " , " gitRevCount " } , { " rev " , rev . gitRev ( ) } } ;
auto cache = getCache ( ) ;
if ( auto revCountAttrs = cache - > lookup ( key ) )
return getIntAttr ( * revCountAttrs , " revCount " ) ;
Activity act ( * logger , lvlChatty , actUnknown , fmt ( " getting Git revision count of '%s' " , repoInfo . url ) ) ;
auto revCount = GitRepo : : openRepo ( CanonPath ( repoDir ) ) - > getRevCount ( rev ) ;
cache - > upsert ( key , Attrs { { " revCount " , revCount } } ) ;
return revCount ;
}
std : : string getDefaultRef ( const RepoInfo & repoInfo ) const
{
auto head = repoInfo . isLocal
? GitRepo : : openRepo ( CanonPath ( repoInfo . url ) ) - > getWorkdirRef ( )
: readHeadCached ( repoInfo . url ) ;
if ( ! head ) {
warn ( " could not read HEAD ref from repo at '%s', using 'master' " , repoInfo . url ) ;
return " master " ;
}
return * head ;
}
static MakeNotAllowedError makeNotAllowedError ( std : : string url )
{
return [ url { std : : move ( url ) } ] ( const CanonPath & path ) - > RestrictedPathError
2022-04-08 20:38:43 +03:00
{
2023-10-25 19:55:08 +03:00
if ( nix : : pathExists ( path . abs ( ) ) )
return RestrictedPathError ( " access to path '%s' is forbidden because it is not under Git control; maybe you should 'git add' it to the repository '%s'? " , path , url ) ;
else
return RestrictedPathError ( " path '%s' does not exist in Git repository '%s' " , path , url ) ;
2022-04-08 20:38:43 +03:00
} ;
2023-10-25 19:55:08 +03:00
}
2022-04-08 20:38:43 +03:00
2023-11-09 17:48:41 +02:00
void verifyCommit ( const Input & input , std : : shared_ptr < GitRepo > repo ) const
{
auto publicKeys = getPublicKeys ( input . attrs ) ;
auto verifyCommit = maybeGetBoolAttr ( input . attrs , " verifyCommit " ) . value_or ( ! publicKeys . empty ( ) ) ;
2020-01-21 17:27:53 +02:00
2023-11-09 17:48:41 +02:00
if ( verifyCommit ) {
if ( input . getRev ( ) & & repo )
repo - > verifyCommit ( * input . getRev ( ) , publicKeys ) ;
else
throw Error ( " commit verification is required for Git repository '%s', but it's dirty " , input . to_string ( ) ) ;
2020-01-21 17:27:53 +02:00
}
2023-11-09 17:48:41 +02:00
}
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
std : : pair < ref < InputAccessor > , Input > getAccessorFromCommit (
ref < Store > store ,
RepoInfo & repoInfo ,
Input & & input ) const
{
assert ( ! repoInfo . workdirInfo . isDirty ) ;
2022-04-08 20:38:43 +03:00
2023-10-25 19:55:08 +03:00
auto origRev = input . getRev ( ) ;
2020-03-17 23:32:26 +02:00
2023-10-25 19:55:08 +03:00
std : : string name = input . getName ( ) ;
auto originalRef = input . getRef ( ) ;
auto ref = originalRef ? * originalRef : getDefaultRef ( repoInfo ) ;
input . attrs . insert_or_assign ( " ref " , ref ) ;
2020-03-17 23:32:26 +02:00
2020-01-21 17:27:53 +02:00
Path repoDir ;
2023-10-25 19:55:08 +03:00
if ( repoInfo . isLocal ) {
repoDir = repoInfo . url ;
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
if ( ! input . getRev ( ) )
2023-10-25 19:55:08 +03:00
input . attrs . insert_or_assign ( " rev " , GitRepo : : openRepo ( CanonPath ( repoDir ) ) - > resolveRef ( ref ) . gitRev ( ) ) ;
2020-01-21 17:27:53 +02:00
} else {
2023-10-25 19:55:08 +03:00
Path cacheDir = getCachePath ( repoInfo . url ) ;
2020-01-21 17:27:53 +02:00
repoDir = cacheDir ;
2023-10-25 19:55:08 +03:00
repoInfo . gitDir = " . " ;
2020-01-21 17:27:53 +02:00
2021-08-02 14:39:48 +03:00
createDirs ( dirOf ( cacheDir ) ) ;
2023-10-25 19:55:08 +03:00
PathLocks cacheDirLock ( { cacheDir } ) ;
2021-08-02 14:39:48 +03:00
2023-10-25 19:55:08 +03:00
auto repo = GitRepo : : openRepo ( CanonPath ( cacheDir ) , true , true ) ;
2020-01-21 17:27:53 +02:00
Path localRefFile =
2023-10-25 19:55:08 +03:00
ref . compare ( 0 , 5 , " refs/ " ) = = 0
? cacheDir + " / " + ref
: cacheDir + " /refs/heads/ " + ref ;
2020-01-21 17:27:53 +02:00
bool doFetch ;
time_t now = time ( 0 ) ;
/* If a rev was specified, we need to fetch if it's not in the
repo . */
2023-10-25 19:55:08 +03:00
if ( auto rev = input . getRev ( ) ) {
doFetch = ! repo - > hasObject ( * rev ) ;
2020-01-21 17:27:53 +02:00
} else {
2023-11-15 15:43:30 +02:00
if ( getAllRefsAttr ( input ) ) {
2020-07-17 21:34:57 +03:00
doFetch = true ;
} else {
/* If the local ref is older than ‘ tarball-ttl’ seconds, do a
git fetch to update the local ref to the remote ref . */
struct stat st ;
doFetch = stat ( localRefFile . c_str ( ) , & st ) ! = 0 | |
2021-03-03 03:13:20 +02:00
! isCacheFileWithinTtl ( now , st ) ;
2020-07-17 21:34:57 +03:00
}
2020-01-21 17:27:53 +02:00
}
if ( doFetch ) {
try {
2023-11-15 15:43:30 +02:00
auto fetchRef = getAllRefsAttr ( input )
2020-07-17 21:34:57 +03:00
? " refs/* "
2023-10-25 19:55:08 +03:00
: ref . compare ( 0 , 5 , " refs/ " ) = = 0
? ref
: ref = = " HEAD "
? ref
: " refs/heads/ " + ref ;
2023-11-15 15:43:30 +02:00
repo - > fetch ( repoInfo . url , fmt ( " %s:%s " , fetchRef , fetchRef ) , getShallowAttr ( input ) ) ;
2020-01-21 17:27:53 +02:00
} catch ( Error & e ) {
if ( ! pathExists ( localRefFile ) ) throw ;
2023-10-25 19:55:08 +03:00
logError ( e . info ( ) ) ;
warn ( " could not update local clone of Git repository '%s'; continuing with the most recent version " , repoInfo . url ) ;
2020-01-21 17:27:53 +02:00
}
2022-01-29 21:22:55 +02:00
if ( ! touchCacheFile ( localRefFile , now ) )
warn ( " could not update mtime for file '%s': %s " , localRefFile , strerror ( errno ) ) ;
2023-10-25 19:55:08 +03:00
if ( ! originalRef & & ! storeCachedHead ( repoInfo . url , ref ) )
warn ( " could not update cached head '%s' for '%s' " , ref , repoInfo . url ) ;
2020-01-21 17:27:53 +02:00
}
2023-10-25 19:55:08 +03:00
if ( auto rev = input . getRev ( ) ) {
if ( ! repo - > hasObject ( * rev ) )
throw Error (
" Cannot find Git revision '%s' in ref '%s' of repository '%s'! "
" Please make sure that the " ANSI_BOLD " rev " ANSI_NORMAL " exists on the "
ANSI_BOLD " ref " ANSI_NORMAL " you've specified or add " ANSI_BOLD
" allRefs = true; " ANSI_NORMAL " to " ANSI_BOLD " fetchGit " ANSI_NORMAL " . " ,
rev - > gitRev ( ) ,
ref ,
repoInfo . url
) ;
} else
2020-07-16 20:28:52 +03:00
input . attrs . insert_or_assign ( " rev " , Hash : : parseAny ( chomp ( readFile ( localRefFile ) ) , htSHA1 ) . gitRev ( ) ) ;
2021-10-30 13:25:59 +03:00
// cache dir lock is removed at scope end; we will only use read-only operations on specific revisions in the remainder
2020-01-21 17:27:53 +02:00
}
2023-11-09 17:48:41 +02:00
auto repo = GitRepo : : openRepo ( CanonPath ( repoDir ) ) ;
2020-03-17 22:34:38 +02:00
2023-11-09 17:48:41 +02:00
auto isShallow = repo - > isShallow ( ) ;
2020-03-16 14:20:32 +02:00
2023-11-15 15:43:30 +02:00
if ( isShallow & & ! getShallowAttr ( input ) )
2023-10-25 19:55:08 +03:00
throw Error ( " '%s' is a shallow Git repository, but shallow repositories are only allowed when `shallow = true;` is specified " , repoInfo . url ) ;
2020-03-17 22:34:38 +02:00
2023-10-25 19:55:08 +03:00
// FIXME: check whether rev is an ancestor of ref?
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
auto rev = * input . getRev ( ) ;
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
Attrs infoAttrs ( {
{ " rev " , rev . gitRev ( ) } ,
{ " lastModified " , getLastModified ( repoInfo , repoDir , rev ) } ,
} ) ;
2020-03-17 23:32:26 +02:00
2023-11-15 15:43:30 +02:00
if ( ! getShallowAttr ( input ) )
2023-10-25 19:55:08 +03:00
infoAttrs . insert_or_assign ( " revCount " ,
getRevCount ( repoInfo , repoDir , rev ) ) ;
2020-04-07 14:45:17 +03:00
2023-10-25 19:55:08 +03:00
printTalkative ( " using revision %s of repo '%s' " , rev . gitRev ( ) , repoInfo . url ) ;
2023-11-09 17:48:41 +02:00
verifyCommit ( input , repo ) ;
2023-10-27 19:39:00 +03:00
auto accessor = repo - > getAccessor ( rev ) ;
2023-11-14 15:01:38 +02:00
/* If the repo has submodules, fetch them and return a mounted
2023-10-27 19:39:00 +03:00
input accessor consisting of the accessor for the top - level
repo and the accessors for the submodules . */
2023-11-15 15:43:30 +02:00
if ( getSubmodulesAttr ( input ) ) {
2023-10-27 19:39:00 +03:00
std : : map < CanonPath , nix : : ref < InputAccessor > > mounts ;
2023-10-31 16:59:25 +02:00
for ( auto & [ submodule , submoduleRev ] : repo - > getSubmodules ( rev ) ) {
2023-11-14 17:00:21 +02:00
auto resolved = repo - > resolveSubmoduleUrl ( submodule . url , repoInfo . url ) ;
2023-10-27 19:39:00 +03:00
debug ( " Git submodule %s: %s %s %s -> %s " ,
2023-10-31 16:59:25 +02:00
submodule . path , submodule . url , submodule . branch , submoduleRev . gitRev ( ) , resolved ) ;
2023-10-27 19:39:00 +03:00
fetchers : : Attrs attrs ;
attrs . insert_or_assign ( " type " , " git " ) ;
attrs . insert_or_assign ( " url " , resolved ) ;
if ( submodule . branch ! = " " )
attrs . insert_or_assign ( " ref " , submodule . branch ) ;
2023-10-31 16:59:25 +02:00
attrs . insert_or_assign ( " rev " , submoduleRev . gitRev ( ) ) ;
2023-10-27 19:39:00 +03:00
auto submoduleInput = fetchers : : Input : : fromAttrs ( std : : move ( attrs ) ) ;
auto [ submoduleAccessor , submoduleInput2 ] =
2023-11-14 16:52:18 +02:00
submoduleInput . getAccessor ( store ) ;
2023-10-27 19:39:00 +03:00
mounts . insert_or_assign ( submodule . path , submoduleAccessor ) ;
2023-02-03 22:42:42 +02:00
}
2020-01-21 17:27:53 +02:00
2023-10-27 19:39:00 +03:00
if ( ! mounts . empty ( ) ) {
mounts . insert_or_assign ( CanonPath : : root , accessor ) ;
2023-11-14 15:01:38 +02:00
accessor = makeMountedInputAccessor ( std : : move ( mounts ) ) ;
2023-02-07 16:50:35 +02:00
}
2023-10-25 19:55:08 +03:00
}
2023-10-27 19:39:00 +03:00
2023-11-09 17:48:41 +02:00
assert ( ! origRev | | origRev = = rev ) ;
2023-11-15 15:43:30 +02:00
if ( ! getShallowAttr ( input ) )
2023-10-28 17:16:20 +03:00
input . attrs . insert_or_assign ( " revCount " , getIntAttr ( infoAttrs , " revCount " ) ) ;
input . attrs . insert_or_assign ( " lastModified " , getIntAttr ( infoAttrs , " lastModified " ) ) ;
return { accessor , std : : move ( input ) } ;
2023-10-25 19:55:08 +03:00
}
std : : pair < ref < InputAccessor > , Input > getAccessorFromWorkdir (
2023-10-31 16:59:25 +02:00
ref < Store > store ,
2023-10-25 19:55:08 +03:00
RepoInfo & repoInfo ,
Input & & input ) const
{
2023-11-15 15:43:30 +02:00
if ( getSubmodulesAttr ( input ) )
2023-10-31 16:59:25 +02:00
/* Create mountpoints for the submodules. */
for ( auto & submodule : repoInfo . workdirInfo . submodules )
repoInfo . workdirInfo . files . insert ( submodule . path ) ;
ref < InputAccessor > accessor =
makeFSInputAccessor ( CanonPath ( repoInfo . url ) , repoInfo . workdirInfo . files , makeNotAllowedError ( repoInfo . url ) ) ;
2023-11-14 15:01:38 +02:00
/* If the repo has submodules, return a mounted input accessor
2023-10-31 16:59:25 +02:00
consisting of the accessor for the top - level repo and the
accessors for the submodule workdirs . */
2023-11-15 15:43:30 +02:00
if ( getSubmodulesAttr ( input ) & & ! repoInfo . workdirInfo . submodules . empty ( ) ) {
2023-10-31 16:59:25 +02:00
std : : map < CanonPath , nix : : ref < InputAccessor > > mounts ;
for ( auto & submodule : repoInfo . workdirInfo . submodules ) {
auto submodulePath = CanonPath ( repoInfo . url ) + submodule . path ;
fetchers : : Attrs attrs ;
attrs . insert_or_assign ( " type " , " git " ) ;
attrs . insert_or_assign ( " url " , submodulePath . abs ( ) ) ;
auto submoduleInput = fetchers : : Input : : fromAttrs ( std : : move ( attrs ) ) ;
auto [ submoduleAccessor , submoduleInput2 ] =
2023-11-14 16:52:18 +02:00
submoduleInput . getAccessor ( store ) ;
2023-10-31 16:59:25 +02:00
/* If the submodule is dirty, mark this repo dirty as
well . */
if ( ! submoduleInput2 . getRev ( ) )
repoInfo . workdirInfo . isDirty = true ;
mounts . insert_or_assign ( submodule . path , submoduleAccessor ) ;
}
mounts . insert_or_assign ( CanonPath : : root , accessor ) ;
2023-11-14 15:01:38 +02:00
accessor = makeMountedInputAccessor ( std : : move ( mounts ) ) ;
2023-10-31 16:59:25 +02:00
}
2023-10-25 19:55:08 +03:00
if ( ! repoInfo . workdirInfo . isDirty ) {
2023-11-09 17:48:41 +02:00
auto repo = GitRepo : : openRepo ( CanonPath ( repoInfo . url ) ) ;
2023-02-07 16:35:32 +02:00
2023-11-09 17:48:41 +02:00
if ( auto ref = repo - > getWorkdirRef ( ) )
2023-10-25 19:55:08 +03:00
input . attrs . insert_or_assign ( " ref " , * ref ) ;
auto rev = repoInfo . workdirInfo . headRev . value ( ) ;
input . attrs . insert_or_assign ( " rev " , rev . gitRev ( ) ) ;
input . attrs . insert_or_assign ( " revCount " , getRevCount ( repoInfo , repoInfo . url , rev ) ) ;
2020-04-07 14:45:17 +03:00
2023-11-09 17:48:41 +02:00
verifyCommit ( input , repo ) ;
2020-04-07 14:45:17 +03:00
} else {
2023-10-25 19:55:08 +03:00
repoInfo . warnDirty ( ) ;
if ( repoInfo . workdirInfo . headRev ) {
input . attrs . insert_or_assign ( " dirtyRev " ,
repoInfo . workdirInfo . headRev - > gitRev ( ) + " -dirty " ) ;
input . attrs . insert_or_assign ( " dirtyShortRev " ,
repoInfo . workdirInfo . headRev - > gitShortRev ( ) + " -dirty " ) ;
}
2023-11-09 17:48:41 +02:00
verifyCommit ( input , nullptr ) ;
2020-04-07 14:45:17 +03:00
}
2023-10-25 19:55:08 +03:00
input . attrs . insert_or_assign (
" lastModified " ,
repoInfo . workdirInfo . headRev
? getLastModified ( repoInfo , repoInfo . url , * repoInfo . workdirInfo . headRev )
: 0 ) ;
2020-03-16 14:20:32 +02:00
2023-10-25 19:55:08 +03:00
input . locked = true ; // FIXME
2020-01-21 17:27:53 +02:00
2023-10-31 16:59:25 +02:00
return { accessor , std : : move ( input ) } ;
2023-10-25 19:55:08 +03:00
}
2020-01-21 17:27:53 +02:00
2023-10-25 19:55:08 +03:00
std : : pair < ref < InputAccessor > , Input > getAccessor ( ref < Store > store , const Input & _input ) const override
{
Input input ( _input ) ;
auto repoInfo = getRepoInfo ( input ) ;
2023-11-09 17:48:41 +02:00
return
input . getRef ( ) | | input . getRev ( ) | | ! repoInfo . isLocal
? getAccessorFromCommit ( store , repoInfo , std : : move ( input ) )
: getAccessorFromWorkdir ( store , repoInfo , std : : move ( input ) ) ;
2020-01-21 17:27:53 +02:00
}
} ;
2020-10-06 14:36:55 +03:00
static auto rGitInputScheme = OnStartup ( [ ] { registerInputScheme ( std : : make_unique < GitInputScheme > ( ) ) ; } ) ;
2020-01-21 17:27:53 +02:00
}