···3131 | true -> String.sub url 4 (String.length url - 4)
3232 | false -> url
3333 in
3434- Uri.of_string url
3434+ let uri = Uri.of_string url in
3535+ (* Strip fragment from dev-repo URL - branch comes from url field *)
3636+ Uri.with_fragment uri None
3737+3838+(** Extract branch from a URL string with optional #branch fragment *)
3939+let extract_branch_from_url url =
4040+ let url =
4141+ match String.starts_with ~prefix:"git+" url with
4242+ | true -> String.sub url 4 (String.length url - 4)
4343+ | false -> url
4444+ in
4545+ Uri.fragment (Uri.of_string url)
35463647module OP = OpamParserTypes.FullPos
3748···4455 match item.pelem with
4556 | OP.Variable (name, value) when name.pelem = "dev-repo" ->
4657 extract_string_value value
5858+ | _ -> None)
5959+ items
6060+6161+(** Find the 'src' field inside a 'url' section *)
6262+let find_url_src (items : OP.opamfile_item list) : string option =
6363+ List.find_map
6464+ (fun (item : OP.opamfile_item) ->
6565+ match item.pelem with
6666+ | OP.Section sec when sec.section_kind.pelem = "url" ->
6767+ (* Look for src field inside the section *)
6868+ List.find_map
6969+ (fun (inner : OP.opamfile_item) ->
7070+ match inner.pelem with
7171+ | OP.Variable (name, value) when name.pelem = "src" ->
7272+ extract_string_value value
7373+ | _ -> None)
7474+ sec.section_items.pelem
4775 | _ -> None)
4876 items
4977···116144 if not (is_git_url url) then Error (Not_git_remote (name, url))
117145 else
118146 let dev_repo = normalize_git_url url in
147147+ (* Extract branch from url field's src, not from dev-repo *)
148148+ let branch = Option.bind (find_url_src opamfile.file_contents) extract_branch_from_url in
119149 let depends = find_depends opamfile.file_contents in
120150 let synopsis = find_synopsis opamfile.file_contents in
121121- Ok (Package.create ~name ~version ~dev_repo ~depends ?synopsis ())
151151+ Ok (Package.create ~name ~version ~dev_repo ?branch ~depends ?synopsis ())
122152 with
123153 | Eio.Io _ as e -> Error (Io_error (Printexc.to_string e))
124154 | exn -> Error (Parse_error (path_str, Printexc.to_string exn)))
+7-2
lib/opam_repo.mli
···71717272val normalize_git_url : string -> Uri.t
7373(** [normalize_git_url url] normalizes a git URL by removing the "git+" prefix
7474- if present.
7474+ and any fragment (branch) if present.
75757676- For example, "git+https://example.com/repo.git" becomes
7676+ For example, "git+https://example.com/repo.git#main" becomes
7777 "https://example.com/repo.git". *)
7878+7979+val extract_branch_from_url : string -> string option
8080+(** [extract_branch_from_url url] extracts the branch from a URL fragment.
8181+8282+ For example, "git+https://example.com/repo.git#main" returns [Some "main"]. *)
78837984val scan_opam_files_for_deps : fs:_ Eio.Path.t -> Fpath.t -> string list
8085(** [scan_opam_files_for_deps ~fs dir_path] scans a directory for .opam files