<commit_before>/// Contains methods for the update process.
module Paket.UpdateProcess

open Paket
open System.IO
open Paket.Domain
open Paket.PackageResolver
open System.Collections.Generic
open Chessie.ErrorHandling
open Paket.Logging

let selectiveUpdate force getSha1 getSortedVersionsF getPackageDetailsF (lockFile:LockFile) (dependenciesFile:DependenciesFile) updateMode semVerUpdateMode =
    let allVersions = Dictionary<PackageName,SemVerInfo list>()
    let getSortedAndCachedVersionsF sources resolverStrategy groupName packageName =
        match allVersions.TryGetValue(packageName) with
        | false,_ ->
            let versions = 
                verbosefn "  - fetching versions for %O" packageName
                getSortedVersionsF sources resolverStrategy groupName packageName

            if Seq.isEmpty versions then
                failwithf "Couldn't retrieve versions for %O." packageName
            allVersions.Add(packageName,versions)
            versions
        | true,versions -> versions 
        |> List.toSeq
        

    let getPreferredVersionsF preferredVersions changedDependencies sources resolverStrategy groupName packageName = 
        seq { 
            match preferredVersions |> Map.tryFind (groupName, packageName), resolverStrategy, changedDependencies |> Set.exists ((=) (groupName, packageName)) with
            | Some v, ResolverStrategy.Min, _
            | Some v, _, false -> yield v
            | _ -> ()
            yield! getSortedAndCachedVersionsF sources resolverStrategy groupName packageName
        }

    let dependenciesFile =
        let processFile createRequirementF =
          lockFile.GetGroupedResolution()
          |> Map.fold (fun (dependenciesFile:DependenciesFile) (groupName,packageName) resolvedPackage -> 
                             dependenciesFile.AddFixedPackage(groupName,packageName,createRequirementF resolvedPackage.Version)) dependenciesFile
    
        let formatPrerelease (v:SemVerInfo) =
            match v.PreRelease with
            | Some p -> sprintf " %O" p
            | None -> ""

        match semVerUpdateMode with
        | SemVerUpdateMode.NoRestriction -> dependenciesFile
        | SemVerUpdateMode.KeepMajor -> processFile (fun v -> sprintf "~> %d" v.Major + formatPrerelease v)
        | SemVerUpdateMode.KeepMinor -> processFile (fun v -> sprintf "~> %d.%d" v.Major v.Minor + formatPrerelease v)
        | SemVerUpdateMode.KeepPatch -> processFile (fun v -> sprintf "~> %d.%d.%d" v.Major v.Minor v.Patch + formatPrerelease v)

    let getVersionsF,groupsToUpdate =
        match updateMode with
        | UpdateAll ->
            let preferredVersions = DependencyChangeDetection.GetPreferredNuGetVersions lockFile

            let changes =
                lockFile.GetGroupedResolution()
                |> Seq.map (fun k -> k.Key)
                |> Set.ofSeq

            (getPreferredVersionsF preferredVersions changes),dependenciesFile.Groups
        | UpdateGroup groupName ->
            let groups =
                dependenciesFile.Groups
                |> Map.filter (fun k _ -> k = groupName)

            getSortedAndCachedVersionsF,groups
        | Install ->
            let nuGetChanges = DependencyChangeDetection.findNuGetChangesInDependenciesFile(dependenciesFile,lockFile)
            let nuGetChangesPerGroup =
                nuGetChanges
                |> Seq.groupBy fst
                |> Map.ofSeq

            let remoteFileChanges = DependencyChangeDetection.findRemoteFileChangesInDependenciesFile(dependenciesFile,lockFile)
            let remoteFileChangesPerGroup =
                remoteFileChanges
                |> Seq.groupBy fst
                |> Map.ofSeq

            let preferredVersions = DependencyChangeDetection.GetPreferredNuGetVersions lockFile

            let hasNuGetChanges groupName =
                match nuGetChangesPerGroup |> Map.tryFind groupName with
                | None -> false
                | Some x -> Seq.isEmpty x |> not

            let hasRemoteFileChanges groupName =
                match remoteFileChangesPerGroup |> Map.tryFind groupName with
                | None -> false
                | Some x -> Seq.isEmpty x |> not
<commit_msg>Use preferred versions for update group<commit_after>/// Contains methods for the update process.
module Paket.UpdateProcess

open Paket
open System.IO
open Paket.Domain
open Paket.PackageResolver
open System.Collections.Generic
open Chessie.ErrorHandling
open Paket.Logging

let selectiveUpdate force getSha1 getSortedVersionsF getPackageDetailsF (lockFile:LockFile) (dependenciesFile:DependenciesFile) updateMode semVerUpdateMode =
    let allVersions = Dictionary<PackageName,SemVerInfo list>()
    let getSortedAndCachedVersionsF sources resolverStrategy groupName packageName =
        match allVersions.TryGetValue(packageName) with
        | false,_ ->
            let versions = 
                verbosefn "  - fetching versions for %O" packageName
                getSortedVersionsF sources resolverStrategy groupName packageName

            if Seq.isEmpty versions then
                failwithf "Couldn't retrieve versions for %O." packageName
            allVersions.Add(packageName,versions)
            versions
        | true,versions -> versions 
        |> List.toSeq
        

    let getPreferredVersionsF preferredVersions changedDependencies sources resolverStrategy groupName packageName = 
        seq { 
            match preferredVersions |> Map.tryFind (groupName, packageName), resolverStrategy, changedDependencies |> Set.exists ((=) (groupName, packageName)) with
            | Some v, ResolverStrategy.Min, _
            | Some v, _, false -> yield v
            | _ -> ()
            yield! getSortedAndCachedVersionsF sources resolverStrategy groupName packageName
        }

    let dependenciesFile =
        let processFile createRequirementF =
          lockFile.GetGroupedResolution()
          |> Map.fold (fun (dependenciesFile:DependenciesFile) (groupName,packageName) resolvedPackage -> 
                             dependenciesFile.AddFixedPackage(groupName,packageName,createRequirementF resolvedPackage.Version)) dependenciesFile
    
        let formatPrerelease (v:SemVerInfo) =
            match v.PreRelease with
            | Some p -> sprintf " %O" p
            | None -> ""

        match semVerUpdateMode with
        | SemVerUpdateMode.NoRestriction -> dependenciesFile
        | SemVerUpdateMode.KeepMajor -> processFile (fun v -> sprintf "~> %d" v.Major + formatPrerelease v)
        | SemVerUpdateMode.KeepMinor -> processFile (fun v -> sprintf "~> %d.%d" v.Major v.Minor + formatPrerelease v)
        | SemVerUpdateMode.KeepPatch -> processFile (fun v -> sprintf "~> %d.%d.%d" v.Major v.Minor v.Patch + formatPrerelease v)

    let getVersionsF,groupsToUpdate =
        match updateMode with
        | UpdateAll ->
            let preferredVersions = DependencyChangeDetection.GetPreferredNuGetVersions lockFile

            let changes =
                lockFile.GetGroupedResolution()
                |> Seq.map (fun k -> k.Key)
                |> Set.ofSeq

            (getPreferredVersionsF preferredVersions changes),dependenciesFile.Groups
        | UpdateGroup groupName ->
            let preferredVersions = DependencyChangeDetection.GetPreferredNuGetVersions lockFile

            let changes =
                lockFile.GetGroupedResolution()
                |> Seq.map (fun k -> k.Key)
                |> Seq.filter (fun (g,_) -> g = groupName)
                |> Set.ofSeq

            let groups =
                dependenciesFile.Groups
                |> Map.filter (fun k _ -> k = groupName)

            (getPreferredVersionsF preferredVersions changes),groups
        | Install ->
            let nuGetChanges = DependencyChangeDetection.findNuGetChangesInDependenciesFile(dependenciesFile,lockFile)
            let nuGetChangesPerGroup =
                nuGetChanges
                |> Seq.groupBy fst
                |> Map.ofSeq

            let remoteFileChanges = DependencyChangeDetection.findRemoteFileChangesInDependenciesFile(dependenciesFile,lockFile)
            let remoteFileChangesPerGroup =
                remoteFileChanges
                |> Seq.groupBy fst
                |> Map.ofSeq

            let preferredVersions = DependencyChangeDetection.GetPreferredNuGetVersions lockFile

            let hasNuGetChanges groupName =
                match nuGetChangesPerGroup |> Map.tryFind groupName with
                | None -> false
                | Some x -> Seq.isEmpty x |> not

            let hasRemoteFileChanges groupName =
                match remoteFileChangesPerGroup |> Map.tryFind groupName with
                | None -> false
                | Some x -> Seq.isEmpty x |> not

{'fraction_non_alphanumeric': 0.0686685552407932, 'fraction_numerical': 0.00022662889518413598, 'mean_word_length': 2.2847041310011167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6792198', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2160, 'n_words': 536}