Coverage Report

Created: 2023-11-19 07:08

/src/git/prompt.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "parse.h"
3
#include "environment.h"
4
#include "run-command.h"
5
#include "strbuf.h"
6
#include "prompt.h"
7
#include "compat/terminal.h"
8
9
static char *do_askpass(const char *cmd, const char *prompt)
10
0
{
11
0
  struct child_process pass = CHILD_PROCESS_INIT;
12
0
  static struct strbuf buffer = STRBUF_INIT;
13
0
  int err = 0;
14
15
0
  strvec_push(&pass.args, cmd);
16
0
  strvec_push(&pass.args, prompt);
17
18
0
  pass.out = -1;
19
20
0
  if (start_command(&pass))
21
0
    return NULL;
22
23
0
  strbuf_reset(&buffer);
24
0
  if (strbuf_read(&buffer, pass.out, 20) < 0)
25
0
    err = 1;
26
27
0
  close(pass.out);
28
29
0
  if (finish_command(&pass))
30
0
    err = 1;
31
32
0
  if (err) {
33
0
    error("unable to read askpass response from '%s'", cmd);
34
0
    strbuf_release(&buffer);
35
0
    return NULL;
36
0
  }
37
38
0
  strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
39
40
0
  return buffer.buf;
41
0
}
42
43
char *git_prompt(const char *prompt, int flags)
44
0
{
45
0
  char *r = NULL;
46
47
0
  if (flags & PROMPT_ASKPASS) {
48
0
    const char *askpass;
49
50
0
    askpass = getenv("GIT_ASKPASS");
51
0
    if (!askpass)
52
0
      askpass = askpass_program;
53
0
    if (!askpass)
54
0
      askpass = getenv("SSH_ASKPASS");
55
0
    if (askpass && *askpass)
56
0
      r = do_askpass(askpass, prompt);
57
0
  }
58
59
0
  if (!r) {
60
0
    const char *err;
61
62
0
    if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
63
0
      r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
64
0
      err = strerror(errno);
65
0
    } else {
66
0
      err = "terminal prompts disabled";
67
0
    }
68
0
    if (!r) {
69
      /* prompts already contain ": " at the end */
70
0
      die("could not read %s%s", prompt, err);
71
0
    }
72
0
  }
73
0
  return r;
74
0
}
75
76
int git_read_line_interactively(struct strbuf *line)
77
0
{
78
0
  int ret;
79
80
0
  fflush(stdout);
81
0
  ret = strbuf_getline_lf(line, stdin);
82
0
  if (ret != EOF)
83
0
    strbuf_trim_trailing_newline(line);
84
85
0
  return ret;
86
0
}