Coverage Report

Created: 2024-09-08 06:24

/src/git/reflog-walk.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "commit.h"
5
#include "refs.h"
6
#include "diff.h"
7
#include "repository.h"
8
#include "revision.h"
9
#include "string-list.h"
10
#include "reflog-walk.h"
11
12
struct complete_reflogs {
13
  char *ref;
14
  char *short_ref;
15
  struct reflog_info {
16
    struct object_id ooid, noid;
17
    char *email;
18
    timestamp_t timestamp;
19
    int tz;
20
    char *message;
21
  } *items;
22
  int nr, alloc;
23
};
24
25
static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
26
    const char *email, timestamp_t timestamp, int tz,
27
    const char *message, void *cb_data)
28
0
{
29
0
  struct complete_reflogs *array = cb_data;
30
0
  struct reflog_info *item;
31
32
0
  ALLOC_GROW(array->items, array->nr + 1, array->alloc);
33
0
  item = array->items + array->nr;
34
0
  oidcpy(&item->ooid, ooid);
35
0
  oidcpy(&item->noid, noid);
36
0
  item->email = xstrdup(email);
37
0
  item->timestamp = timestamp;
38
0
  item->tz = tz;
39
0
  item->message = xstrdup(message);
40
0
  array->nr++;
41
0
  return 0;
42
0
}
43
44
static void free_complete_reflog(struct complete_reflogs *array)
45
0
{
46
0
  int i;
47
48
0
  if (!array)
49
0
    return;
50
51
0
  for (i = 0; i < array->nr; i++) {
52
0
    free(array->items[i].email);
53
0
    free(array->items[i].message);
54
0
  }
55
0
  free(array->items);
56
0
  free(array->ref);
57
0
  free(array->short_ref);
58
0
  free(array);
59
0
}
60
61
static void complete_reflogs_clear(void *util, const char *str UNUSED)
62
0
{
63
0
  struct complete_reflogs *array = util;
64
0
  free_complete_reflog(array);
65
0
}
66
67
static struct complete_reflogs *read_complete_reflog(const char *ref)
68
0
{
69
0
  struct complete_reflogs *reflogs =
70
0
    xcalloc(1, sizeof(struct complete_reflogs));
71
0
  reflogs->ref = xstrdup(ref);
72
0
  refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref,
73
0
         read_one_reflog, reflogs);
74
0
  if (reflogs->nr == 0) {
75
0
    const char *name;
76
0
    void *name_to_free;
77
0
    name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),
78
0
                ref,
79
0
                RESOLVE_REF_READING,
80
0
                NULL, NULL);
81
0
    if (name) {
82
0
      refs_for_each_reflog_ent(get_main_ref_store(the_repository),
83
0
             name, read_one_reflog,
84
0
             reflogs);
85
0
      free(name_to_free);
86
0
    }
87
0
  }
88
0
  if (reflogs->nr == 0) {
89
0
    char *refname = xstrfmt("refs/%s", ref);
90
0
    refs_for_each_reflog_ent(get_main_ref_store(the_repository),
91
0
           refname, read_one_reflog, reflogs);
92
0
    if (reflogs->nr == 0) {
93
0
      free(refname);
94
0
      refname = xstrfmt("refs/heads/%s", ref);
95
0
      refs_for_each_reflog_ent(get_main_ref_store(the_repository),
96
0
             refname, read_one_reflog,
97
0
             reflogs);
98
0
    }
99
0
    free(refname);
100
0
  }
101
0
  return reflogs;
102
0
}
103
104
static int get_reflog_recno_by_time(struct complete_reflogs *array,
105
  timestamp_t timestamp)
106
0
{
107
0
  int i;
108
0
  for (i = array->nr - 1; i >= 0; i--)
109
0
    if (timestamp >= array->items[i].timestamp)
110
0
      return i;
111
0
  return -1;
112
0
}
113
114
struct commit_reflog {
115
  int recno;
116
  enum selector_type {
117
    SELECTOR_NONE,
118
    SELECTOR_INDEX,
119
    SELECTOR_DATE
120
  } selector;
121
  struct complete_reflogs *reflogs;
122
};
123
124
struct reflog_walk_info {
125
  struct commit_reflog **logs;
126
  size_t nr, alloc;
127
  struct string_list complete_reflogs;
128
  struct commit_reflog *last_commit_reflog;
129
};
130
131
void init_reflog_walk(struct reflog_walk_info **info)
132
0
{
133
0
  CALLOC_ARRAY(*info, 1);
134
0
  (*info)->complete_reflogs.strdup_strings = 1;
135
0
}
136
137
void reflog_walk_info_release(struct reflog_walk_info *info)
138
46.8k
{
139
46.8k
  size_t i;
140
141
46.8k
  if (!info)
142
46.8k
    return;
143
144
0
  for (i = 0; i < info->nr; i++)
145
0
    free(info->logs[i]);
146
0
  string_list_clear_func(&info->complete_reflogs,
147
0
             complete_reflogs_clear);
148
0
  free(info->logs);
149
0
  free(info);
150
0
}
151
152
int add_reflog_for_walk(struct reflog_walk_info *info,
153
    struct commit *commit, const char *name)
154
0
{
155
0
  timestamp_t timestamp = 0;
156
0
  int recno = -1;
157
0
  struct string_list_item *item;
158
0
  struct complete_reflogs *reflogs;
159
0
  char *branch, *at = strchr(name, '@');
160
0
  struct commit_reflog *commit_reflog;
161
0
  enum selector_type selector = SELECTOR_NONE;
162
163
0
  if (commit->object.flags & UNINTERESTING)
164
0
    die("cannot walk reflogs for %s", name);
165
166
0
  branch = xstrdup(name);
167
0
  if (at && at[1] == '{') {
168
0
    char *ep;
169
0
    branch[at - name] = '\0';
170
0
    recno = strtoul(at + 2, &ep, 10);
171
0
    if (*ep != '}') {
172
0
      recno = -1;
173
0
      timestamp = approxidate(at + 2);
174
0
      selector = SELECTOR_DATE;
175
0
    }
176
0
    else
177
0
      selector = SELECTOR_INDEX;
178
0
  } else
179
0
    recno = 0;
180
181
0
  item = string_list_lookup(&info->complete_reflogs, branch);
182
0
  if (item)
183
0
    reflogs = item->util;
184
0
  else {
185
0
    if (*branch == '\0') {
186
0
      free(branch);
187
0
      branch = refs_resolve_refdup(get_main_ref_store(the_repository),
188
0
                 "HEAD", 0, NULL, NULL);
189
0
      if (!branch)
190
0
        die("no current branch");
191
192
0
    }
193
0
    reflogs = read_complete_reflog(branch);
194
0
    if (!reflogs || reflogs->nr == 0) {
195
0
      char *b;
196
0
      int ret = repo_dwim_log(the_repository, branch, strlen(branch),
197
0
            NULL, &b);
198
0
      if (ret > 1)
199
0
        free(b);
200
0
      else if (ret == 1) {
201
0
        free_complete_reflog(reflogs);
202
0
        free(branch);
203
0
        branch = b;
204
0
        reflogs = read_complete_reflog(branch);
205
0
      }
206
0
    }
207
0
    if (!reflogs || reflogs->nr == 0) {
208
0
      free_complete_reflog(reflogs);
209
0
      free(branch);
210
0
      return -1;
211
0
    }
212
0
    string_list_insert(&info->complete_reflogs, branch)->util
213
0
      = reflogs;
214
0
  }
215
0
  free(branch);
216
217
0
  CALLOC_ARRAY(commit_reflog, 1);
218
0
  if (recno < 0) {
219
0
    commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
220
0
    if (commit_reflog->recno < 0) {
221
0
      free(commit_reflog);
222
0
      return -1;
223
0
    }
224
0
  } else
225
0
    commit_reflog->recno = reflogs->nr - recno - 1;
226
0
  commit_reflog->selector = selector;
227
0
  commit_reflog->reflogs = reflogs;
228
229
0
  ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
230
0
  info->logs[info->nr++] = commit_reflog;
231
232
0
  return 0;
233
0
}
234
235
void get_reflog_selector(struct strbuf *sb,
236
       struct reflog_walk_info *reflog_info,
237
       struct date_mode dmode, int force_date,
238
       int shorten)
239
0
{
240
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
241
0
  struct reflog_info *info;
242
0
  const char *printed_ref;
243
244
0
  if (!commit_reflog)
245
0
    return;
246
247
0
  if (shorten) {
248
0
    if (!commit_reflog->reflogs->short_ref)
249
0
      commit_reflog->reflogs->short_ref
250
0
        = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
251
0
                     commit_reflog->reflogs->ref,
252
0
                     0);
253
0
    printed_ref = commit_reflog->reflogs->short_ref;
254
0
  } else {
255
0
    printed_ref = commit_reflog->reflogs->ref;
256
0
  }
257
258
0
  strbuf_addf(sb, "%s@{", printed_ref);
259
0
  if (commit_reflog->selector == SELECTOR_DATE ||
260
0
      (commit_reflog->selector == SELECTOR_NONE && force_date)) {
261
0
    info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
262
0
    strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
263
0
  } else {
264
0
    strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
265
0
          - 2 - commit_reflog->recno);
266
0
  }
267
268
0
  strbuf_addch(sb, '}');
269
0
}
270
271
void get_reflog_message(struct strbuf *sb,
272
      struct reflog_walk_info *reflog_info)
273
0
{
274
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
275
0
  struct reflog_info *info;
276
0
  size_t len;
277
278
0
  if (!commit_reflog)
279
0
    return;
280
281
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
282
0
  len = strlen(info->message);
283
0
  if (len > 0)
284
0
    len--; /* strip away trailing newline */
285
0
  strbuf_add(sb, info->message, len);
286
0
}
287
288
const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
289
0
{
290
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
291
0
  struct reflog_info *info;
292
293
0
  if (!commit_reflog)
294
0
    return NULL;
295
296
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
297
0
  return info->email;
298
0
}
299
300
timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
301
0
{
302
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
303
0
  struct reflog_info *info;
304
305
0
  if (!commit_reflog)
306
0
    return 0;
307
308
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
309
0
  return info->timestamp;
310
0
}
311
312
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
313
       struct date_mode dmode, int force_date)
314
0
{
315
0
  if (reflog_info && reflog_info->last_commit_reflog) {
316
0
    struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
317
0
    struct reflog_info *info;
318
0
    struct strbuf selector = STRBUF_INIT;
319
320
0
    info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
321
0
    get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
322
0
    if (oneline) {
323
0
      printf("%s: %s", selector.buf, info->message);
324
0
    }
325
0
    else {
326
0
      printf("Reflog: %s (%s)\nReflog message: %s",
327
0
             selector.buf, info->email, info->message);
328
0
    }
329
330
0
    strbuf_release(&selector);
331
0
  }
332
0
}
333
334
int reflog_walk_empty(struct reflog_walk_info *info)
335
0
{
336
0
  return !info || !info->nr;
337
0
}
338
339
static struct commit *next_reflog_commit(struct commit_reflog *log)
340
0
{
341
0
  for (; log->recno >= 0; log->recno--) {
342
0
    struct reflog_info *entry = &log->reflogs->items[log->recno];
343
0
    struct object *obj = parse_object(the_repository,
344
0
              &entry->noid);
345
346
0
    if (obj && obj->type == OBJ_COMMIT)
347
0
      return (struct commit *)obj;
348
0
  }
349
0
  return NULL;
350
0
}
351
352
static timestamp_t log_timestamp(struct commit_reflog *log)
353
0
{
354
0
  return log->reflogs->items[log->recno].timestamp;
355
0
}
356
357
struct commit *next_reflog_entry(struct reflog_walk_info *walk)
358
0
{
359
0
  struct commit_reflog *best = NULL;
360
0
  struct commit *best_commit = NULL;
361
0
  size_t i;
362
363
0
  for (i = 0; i < walk->nr; i++) {
364
0
    struct commit_reflog *log = walk->logs[i];
365
0
    struct commit *commit = next_reflog_commit(log);
366
367
0
    if (!commit)
368
0
      continue;
369
370
0
    if (!best || log_timestamp(log) > log_timestamp(best)) {
371
0
      best = log;
372
0
      best_commit = commit;
373
0
    }
374
0
  }
375
376
0
  if (best) {
377
0
    best->recno--;
378
0
    walk->last_commit_reflog = best;
379
0
    return best_commit;
380
0
  }
381
382
0
  return NULL;
383
0
}