Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/checkout--worker.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "entry.h"
4
#include "gettext.h"
5
#include "parallel-checkout.h"
6
#include "parse-options.h"
7
#include "pkt-line.h"
8
#include "read-cache-ll.h"
9
10
static void packet_to_pc_item(const char *buffer, int len,
11
            struct parallel_checkout_item *pc_item)
12
0
{
13
0
  const struct pc_item_fixed_portion *fixed_portion;
14
0
  const char *variant;
15
0
  char *encoding;
16
17
0
  if (len < sizeof(struct pc_item_fixed_portion))
18
0
    BUG("checkout worker received too short item (got %dB, exp %dB)",
19
0
        len, (int)sizeof(struct pc_item_fixed_portion));
20
21
0
  fixed_portion = (struct pc_item_fixed_portion *)buffer;
22
23
0
  if (len - sizeof(struct pc_item_fixed_portion) !=
24
0
    fixed_portion->name_len + fixed_portion->working_tree_encoding_len)
25
0
    BUG("checkout worker received corrupted item");
26
27
0
  variant = buffer + sizeof(struct pc_item_fixed_portion);
28
29
  /*
30
   * Note: the main process uses zero length to communicate that the
31
   * encoding is NULL. There is no use case that requires sending an
32
   * actual empty string, since convert_attrs() never sets
33
   * ca.working_tree_enconding to "".
34
   */
35
0
  if (fixed_portion->working_tree_encoding_len) {
36
0
    encoding = xmemdupz(variant,
37
0
            fixed_portion->working_tree_encoding_len);
38
0
    variant += fixed_portion->working_tree_encoding_len;
39
0
  } else {
40
0
    encoding = NULL;
41
0
  }
42
43
0
  memset(pc_item, 0, sizeof(*pc_item));
44
0
  pc_item->ce = make_empty_transient_cache_entry(fixed_portion->name_len, NULL);
45
0
  pc_item->ce->ce_namelen = fixed_portion->name_len;
46
0
  pc_item->ce->ce_mode = fixed_portion->ce_mode;
47
0
  memcpy(pc_item->ce->name, variant, pc_item->ce->ce_namelen);
48
0
  oidcpy(&pc_item->ce->oid, &fixed_portion->oid);
49
50
0
  pc_item->id = fixed_portion->id;
51
0
  pc_item->ca.crlf_action = fixed_portion->crlf_action;
52
0
  pc_item->ca.ident = fixed_portion->ident;
53
0
  pc_item->ca.working_tree_encoding = encoding;
54
0
}
55
56
static void report_result(struct parallel_checkout_item *pc_item)
57
0
{
58
0
  struct pc_item_result res = { 0 };
59
0
  size_t size;
60
61
0
  res.id = pc_item->id;
62
0
  res.status = pc_item->status;
63
64
0
  if (pc_item->status == PC_ITEM_WRITTEN) {
65
0
    res.st = pc_item->st;
66
0
    size = sizeof(res);
67
0
  } else {
68
0
    size = PC_ITEM_RESULT_BASE_SIZE;
69
0
  }
70
71
0
  packet_write(1, (const char *)&res, size);
72
0
}
73
74
/* Free the worker-side malloced data, but not pc_item itself. */
75
static void release_pc_item_data(struct parallel_checkout_item *pc_item)
76
0
{
77
0
  free((char *)pc_item->ca.working_tree_encoding);
78
0
  discard_cache_entry(pc_item->ce);
79
0
}
80
81
static void worker_loop(struct checkout *state)
82
0
{
83
0
  struct parallel_checkout_item *items = NULL;
84
0
  size_t i, nr = 0, alloc = 0;
85
86
0
  while (1) {
87
0
    int len = packet_read(0, packet_buffer, sizeof(packet_buffer),
88
0
              0);
89
90
0
    if (len < 0)
91
0
      BUG("packet_read() returned negative value");
92
0
    else if (!len)
93
0
      break;
94
95
0
    ALLOC_GROW(items, nr + 1, alloc);
96
0
    packet_to_pc_item(packet_buffer, len, &items[nr++]);
97
0
  }
98
99
0
  for (i = 0; i < nr; i++) {
100
0
    struct parallel_checkout_item *pc_item = &items[i];
101
0
    write_pc_item(pc_item, state);
102
0
    report_result(pc_item);
103
0
    release_pc_item_data(pc_item);
104
0
  }
105
106
0
  packet_flush(1);
107
108
0
  free(items);
109
0
}
110
111
static const char * const checkout_worker_usage[] = {
112
  N_("git checkout--worker [<options>]"),
113
  NULL
114
};
115
116
int cmd_checkout__worker(int argc, const char **argv, const char *prefix)
117
0
{
118
0
  struct checkout state = CHECKOUT_INIT;
119
0
  struct option checkout_worker_options[] = {
120
0
    OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
121
0
      N_("when creating files, prepend <string>")),
122
0
    OPT_END()
123
0
  };
124
125
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
126
0
    usage_with_options(checkout_worker_usage,
127
0
           checkout_worker_options);
128
129
0
  git_config(git_default_config, NULL);
130
0
  argc = parse_options(argc, argv, prefix, checkout_worker_options,
131
0
           checkout_worker_usage, 0);
132
0
  if (argc > 0)
133
0
    usage_with_options(checkout_worker_usage, checkout_worker_options);
134
135
0
  if (state.base_dir)
136
0
    state.base_dir_len = strlen(state.base_dir);
137
138
  /*
139
   * Setting this on a worker won't actually update the index. We just
140
   * need to tell the checkout machinery to lstat() the written entries,
141
   * so that we can send this data back to the main process.
142
   */
143
0
  state.refresh_cache = 1;
144
145
0
  worker_loop(&state);
146
0
  return 0;
147
0
}