Coverage Report

Created: 2026-02-14 06:27

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