123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158(*********************************************************************************)(* Git_pager - Show diffs in the terminal with the user's configured git pager *)(* Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com> *)(* *)(* This file is part of Git_pager. *)(* *)(* Git_pager is free software; you can redistribute it and/or modify it *)(* under the terms of the GNU Lesser General Public License as published by *)(* the Free Software Foundation either version 3 of the License, or any later *)(* version, with the LGPL-3.0 Linking Exception. *)(* *)(* Git_pager is distributed in the hope that it will be useful, but WITHOUT *)(* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *)(* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *)(* and the file `NOTICE.md` at the root of this repository for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* and the LGPL-3.0 Linking Exception along with this library. If not, see *)(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *)(*********************************************************************************)moduleUnix=UnixLabelstypet={output_kind:[`Tty|`Pager|`Other];color:[`Auto|`Always|`Never];write_end:Out_channel.t}letshould_force_color{output_kind;color;write_end=_}=matchoutput_kind,colorwith|`Pager,`Auto->(* This is the only case in which Git on its own would make the wrong
decision if we did nothing to help. If we ran the command in a terminal,
Git would normally color it, but because we are sending its output to a
pipe, the [auto] strategy is confused and Git disables the colors. In all
other cases we return [false]. It doesn't mean we'll get no color, it
just means we let Git decide on its own. *)true|`Pager,(`Always|`Never)|(`Other|`Tty),(`Always|`Auto|`Never)->false;;letwrite_endt=t.write_endmoduleProcess_status=structtypet=Unix.process_status=|WEXITEDofint|WSIGNALEDofint|WSTOPPEDofint[@@derivingsexp_of]endletget_git_pager()=let((in_ch,_)asprocess)=Unix.open_process_args"git"[|"git";"var";"GIT_PAGER"|]inletoutput=In_channel.input_allin_chinmatchUnix.close_processprocesswith|WEXITED0->output|>String.strip|(WEXITED_|WSIGNALED_|WSTOPPED_)asprocess_status->Err.raisePp.O.[Pp.text"Failed to get the value of "++Pp_tty.kwd(moduleString)"GIT_PAGER"++Pp.text".";Err.pp_of_sexp(Process_status.sexp_of_tprocess_status)];;letget_git_color()=let((in_ch,_)asprocess)=Unix.open_process_args"git"[|"git";"config";"--get";"color.ui"|]inletoutput=In_channel.input_allin_chinmatchUnix.close_processprocesswith|WEXITED(0|1)->(matchoutput|>String.stripwith|""|"auto"->`Auto|"always"->`Always|"never"->`Never|other->Err.raisePp.O.[Pp.text"Unexpected "++Pp_tty.kwd(moduleString)"git color.ui"++Pp.text" value "++Pp_tty.id(moduleString)other++Pp.text"."])|(WEXITED_|WSIGNALED_|WSTOPPED_)asprocess_status->Err.raisePp.O.[Pp.text"Failed to get the value of "++Pp_tty.kwd(moduleString)"color.ui"++Pp.text".";Err.pp_of_sexp(Process_status.sexp_of_tprocess_status)];;letgit_pager=lazy(get_git_pager())letget_git_color=lazy(get_git_color())letrecwaitpid_non_intrpid=tryUnix.waitpid~mode:[]pidwith|Unix.Unix_error(EINTR,_,_)->waitpid_non_intrpid;;letrun~f=letgit_pager=forcegit_pagerinletcolor=forceget_git_colorinletstdout_isatty=Unix.isattyUnix.stdoutinletpager_is_disabled=(notstdout_isatty)||String.equalgit_pager"cat"inletoutput_kind=matchpager_is_disabled,stdout_isattywith|true,true->`Tty|true,false->`Other|false,true->`Pager|false,false->assertfalseinmatchoutput_kindwith|`Tty|`Other->f{output_kind;color;write_end=stdout}|`Pager->letprocess_env=letenv=Unix.environment()inifArray.existsenv~f:(funs->String.is_prefixs~prefix:"LESS=")thenenvelseArray.appendenv[|"LESS=FRX"|]inletpager_in,pager_out=Unix.pipe~cloexec:true()inletprocess=Unix.create_process_env~prog:git_pager~args:[|git_pager|]~env:process_env~stdin:pager_in~stdout:Unix.stdout~stderr:Unix.stderrinletout_ch=Unix.out_channel_of_descrpager_outinExn.protect~f:(fun()->letres=f{output_kind;color;write_end=out_ch}inOut_channel.flushout_ch;res)~finally:(fun()->Out_channel.closeout_ch;matchwaitpid_non_intrprocess|>sndwith|WEXITED0->()|WSIGNALEDsignalwhenInt.equalsignalStdlib.Sys.sigpipe->()|(WEXITED_|WSIGNALED_|WSTOPPED_)asprocess_status->Err.raisePp.O.[Pp.text"Call to "++Pp_tty.kwd(moduleString)"GIT_PAGER"++Pp.text"failed.";Err.pp_of_sexp(Process_status.sexp_of_tprocess_status)]);;