Coverage Report

Created: 2024-09-08 06:24

/src/git/builtin/stripspace.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "environment.h"
4
#include "gettext.h"
5
#include "parse-options.h"
6
#include "setup.h"
7
#include "strbuf.h"
8
#include "write-or-die.h"
9
10
static void comment_lines(struct strbuf *buf)
11
0
{
12
0
  char *msg;
13
0
  size_t len;
14
15
0
  msg = strbuf_detach(buf, &len);
16
0
  strbuf_add_commented_lines(buf, msg, len, comment_line_str);
17
0
  free(msg);
18
0
}
19
20
static const char * const stripspace_usage[] = {
21
  "git stripspace [-s | --strip-comments]",
22
  "git stripspace [-c | --comment-lines]",
23
  NULL
24
};
25
26
enum stripspace_mode {
27
  STRIP_DEFAULT = 0,
28
  STRIP_COMMENTS,
29
  COMMENT_LINES
30
};
31
32
int cmd_stripspace(int argc, const char **argv, const char *prefix)
33
0
{
34
0
  struct strbuf buf = STRBUF_INIT;
35
0
  enum stripspace_mode mode = STRIP_DEFAULT;
36
0
  int nongit;
37
38
0
  const struct option options[] = {
39
0
    OPT_CMDMODE('s', "strip-comments", &mode,
40
0
          N_("skip and remove all lines starting with comment character"),
41
0
          STRIP_COMMENTS),
42
0
    OPT_CMDMODE('c', "comment-lines", &mode,
43
0
          N_("prepend comment character and space to each line"),
44
0
          COMMENT_LINES),
45
0
    OPT_END()
46
0
  };
47
48
0
  argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
49
0
  if (argc)
50
0
    usage_with_options(stripspace_usage, options);
51
52
0
  if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
53
0
    setup_git_directory_gently(&nongit);
54
0
    git_config(git_default_config, NULL);
55
0
  }
56
57
0
  if (strbuf_read(&buf, 0, 1024) < 0)
58
0
    die_errno("could not read the input");
59
60
0
  if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
61
0
    strbuf_stripspace(&buf,
62
0
        mode == STRIP_COMMENTS ? comment_line_str : NULL);
63
0
  else
64
0
    comment_lines(&buf);
65
66
0
  write_or_die(1, buf.buf, buf.len);
67
0
  strbuf_release(&buf);
68
0
  return 0;
69
0
}