Coverage Report

Created: 2024-09-16 06:10

/src/git/builtin/count-objects.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Builtin "git count-objects".
3
 *
4
 * Copyright (c) 2006 Junio C Hamano
5
 */
6
7
#include "builtin.h"
8
#include "config.h"
9
#include "dir.h"
10
#include "environment.h"
11
#include "gettext.h"
12
#include "path.h"
13
#include "repository.h"
14
#include "parse-options.h"
15
#include "quote.h"
16
#include "packfile.h"
17
#include "object-store-ll.h"
18
19
static unsigned long garbage;
20
static off_t size_garbage;
21
static int verbose;
22
static unsigned long loose, packed, packed_loose;
23
static off_t loose_size;
24
25
static const char *bits_to_msg(unsigned seen_bits)
26
0
{
27
0
  switch (seen_bits) {
28
0
  case 0:
29
0
    return "no corresponding .idx or .pack";
30
0
  case PACKDIR_FILE_GARBAGE:
31
0
    return "garbage found";
32
0
  case PACKDIR_FILE_PACK:
33
0
    return "no corresponding .idx";
34
0
  case PACKDIR_FILE_IDX:
35
0
    return "no corresponding .pack";
36
0
  case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
37
0
  default:
38
0
    return NULL;
39
0
  }
40
0
}
41
42
static void real_report_garbage(unsigned seen_bits, const char *path)
43
0
{
44
0
  struct stat st;
45
0
  const char *desc = bits_to_msg(seen_bits);
46
47
0
  if (!desc)
48
0
    return;
49
50
0
  if (!stat(path, &st))
51
0
    size_garbage += st.st_size;
52
0
  warning("%s: %s", desc, path);
53
0
  garbage++;
54
0
}
55
56
static void loose_garbage(const char *path)
57
0
{
58
0
  if (verbose)
59
0
    report_garbage(PACKDIR_FILE_GARBAGE, path);
60
0
}
61
62
static int count_loose(const struct object_id *oid, const char *path,
63
           void *data UNUSED)
64
0
{
65
0
  struct stat st;
66
67
0
  if (lstat(path, &st) || !S_ISREG(st.st_mode))
68
0
    loose_garbage(path);
69
0
  else {
70
0
    loose_size += on_disk_bytes(st);
71
0
    loose++;
72
0
    if (verbose && has_object_pack(oid))
73
0
      packed_loose++;
74
0
  }
75
0
  return 0;
76
0
}
77
78
static int count_cruft(const char *basename UNUSED, const char *path,
79
           void *data UNUSED)
80
0
{
81
0
  loose_garbage(path);
82
0
  return 0;
83
0
}
84
85
static int print_alternate(struct object_directory *odb, void *data UNUSED)
86
0
{
87
0
  printf("alternate: ");
88
0
  quote_c_style(odb->path, NULL, stdout, 0);
89
0
  putchar('\n');
90
0
  return 0;
91
0
}
92
93
static char const * const count_objects_usage[] = {
94
  "git count-objects [-v] [-H | --human-readable]",
95
  NULL
96
};
97
98
int cmd_count_objects(int argc, const char **argv, const char *prefix)
99
0
{
100
0
  int human_readable = 0;
101
0
  struct option opts[] = {
102
0
    OPT__VERBOSE(&verbose, N_("be verbose")),
103
0
    OPT_BOOL('H', "human-readable", &human_readable,
104
0
       N_("print sizes in human readable format")),
105
0
    OPT_END(),
106
0
  };
107
108
0
  git_config(git_default_config, NULL);
109
110
0
  argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
111
  /* we do not take arguments other than flags for now */
112
0
  if (argc)
113
0
    usage_with_options(count_objects_usage, opts);
114
0
  if (verbose) {
115
0
    report_garbage = real_report_garbage;
116
0
    report_linked_checkout_garbage(the_repository);
117
0
  }
118
119
0
  for_each_loose_file_in_objdir(get_object_directory(),
120
0
              count_loose, count_cruft, NULL, NULL);
121
122
0
  if (verbose) {
123
0
    struct packed_git *p;
124
0
    unsigned long num_pack = 0;
125
0
    off_t size_pack = 0;
126
0
    struct strbuf loose_buf = STRBUF_INIT;
127
0
    struct strbuf pack_buf = STRBUF_INIT;
128
0
    struct strbuf garbage_buf = STRBUF_INIT;
129
130
0
    for (p = get_all_packs(the_repository); p; p = p->next) {
131
0
      if (!p->pack_local)
132
0
        continue;
133
0
      if (open_pack_index(p))
134
0
        continue;
135
0
      packed += p->num_objects;
136
0
      size_pack += p->pack_size + p->index_size;
137
0
      num_pack++;
138
0
    }
139
140
0
    if (human_readable) {
141
0
      strbuf_humanise_bytes(&loose_buf, loose_size);
142
0
      strbuf_humanise_bytes(&pack_buf, size_pack);
143
0
      strbuf_humanise_bytes(&garbage_buf, size_garbage);
144
0
    } else {
145
0
      strbuf_addf(&loose_buf, "%lu",
146
0
            (unsigned long)(loose_size / 1024));
147
0
      strbuf_addf(&pack_buf, "%lu",
148
0
            (unsigned long)(size_pack / 1024));
149
0
      strbuf_addf(&garbage_buf, "%lu",
150
0
            (unsigned long)(size_garbage / 1024));
151
0
    }
152
153
0
    printf("count: %lu\n", loose);
154
0
    printf("size: %s\n", loose_buf.buf);
155
0
    printf("in-pack: %lu\n", packed);
156
0
    printf("packs: %lu\n", num_pack);
157
0
    printf("size-pack: %s\n", pack_buf.buf);
158
0
    printf("prune-packable: %lu\n", packed_loose);
159
0
    printf("garbage: %lu\n", garbage);
160
0
    printf("size-garbage: %s\n", garbage_buf.buf);
161
0
    foreach_alt_odb(print_alternate, NULL);
162
0
    strbuf_release(&loose_buf);
163
0
    strbuf_release(&pack_buf);
164
0
    strbuf_release(&garbage_buf);
165
0
  } else {
166
0
    struct strbuf buf = STRBUF_INIT;
167
0
    if (human_readable)
168
0
      strbuf_humanise_bytes(&buf, loose_size);
169
0
    else
170
0
      strbuf_addf(&buf, "%lu kilobytes",
171
0
            (unsigned long)(loose_size / 1024));
172
0
    printf("%lu objects, %s\n", loose, buf.buf);
173
0
    strbuf_release(&buf);
174
0
  }
175
0
  return 0;
176
0
}