Coverage Report

Created: 2026-01-09 07:10

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, *at = strchr(name, '@');
161
0
  struct commit_reflog *commit_reflog;
162
0
  enum selector_type selector = SELECTOR_NONE;
163
164
0
  if (commit->object.flags & UNINTERESTING)
165
0
    die("cannot walk reflogs for %s", name);
166
167
0
  branch = xstrdup(name);
168
0
  if (at && at[1] == '{') {
169
0
    char *ep;
170
0
    branch[at - name] = '\0';
171
0
    recno = strtoul(at + 2, &ep, 10);
172
0
    if (*ep != '}') {
173
0
      recno = -1;
174
0
      timestamp = approxidate(at + 2);
175
0
      selector = SELECTOR_DATE;
176
0
    }
177
0
    else
178
0
      selector = SELECTOR_INDEX;
179
0
  } else
180
0
    recno = 0;
181
182
0
  item = string_list_lookup(&info->complete_reflogs, branch);
183
0
  if (item)
184
0
    reflogs = item->util;
185
0
  else {
186
0
    if (*branch == '\0') {
187
0
      free(branch);
188
0
      branch = refs_resolve_refdup(get_main_ref_store(the_repository),
189
0
                 "HEAD", 0, NULL, NULL);
190
0
      if (!branch)
191
0
        die("no current branch");
192
193
0
    }
194
0
    reflogs = read_complete_reflog(branch);
195
0
    if (!reflogs || reflogs->nr == 0) {
196
0
      char *b;
197
0
      int ret = repo_dwim_log(the_repository, branch, strlen(branch),
198
0
            NULL, &b);
199
0
      if (ret > 1)
200
0
        free(b);
201
0
      else if (ret == 1) {
202
0
        free_complete_reflog(reflogs);
203
0
        free(branch);
204
0
        branch = b;
205
0
        reflogs = read_complete_reflog(branch);
206
0
      }
207
0
    }
208
0
    if (!reflogs || reflogs->nr == 0) {
209
0
      free_complete_reflog(reflogs);
210
0
      free(branch);
211
0
      return -1;
212
0
    }
213
0
    string_list_insert(&info->complete_reflogs, branch)->util
214
0
      = reflogs;
215
0
  }
216
0
  free(branch);
217
218
0
  CALLOC_ARRAY(commit_reflog, 1);
219
0
  if (recno < 0) {
220
0
    commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
221
0
    if (commit_reflog->recno < 0) {
222
0
      free(commit_reflog);
223
0
      return -1;
224
0
    }
225
0
  } else
226
0
    commit_reflog->recno = reflogs->nr - recno - 1;
227
0
  commit_reflog->selector = selector;
228
0
  commit_reflog->reflogs = reflogs;
229
230
0
  ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
231
0
  info->logs[info->nr++] = commit_reflog;
232
233
0
  return 0;
234
0
}
235
236
void get_reflog_selector(struct strbuf *sb,
237
       struct reflog_walk_info *reflog_info,
238
       struct date_mode dmode, int force_date,
239
       int shorten)
240
0
{
241
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
242
0
  struct reflog_info *info;
243
0
  const char *printed_ref;
244
245
0
  if (!commit_reflog)
246
0
    return;
247
248
0
  if (shorten) {
249
0
    if (!commit_reflog->reflogs->short_ref)
250
0
      commit_reflog->reflogs->short_ref
251
0
        = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
252
0
                     commit_reflog->reflogs->ref,
253
0
                     0);
254
0
    printed_ref = commit_reflog->reflogs->short_ref;
255
0
  } else {
256
0
    printed_ref = commit_reflog->reflogs->ref;
257
0
  }
258
259
0
  strbuf_addf(sb, "%s@{", printed_ref);
260
0
  if (commit_reflog->selector == SELECTOR_DATE ||
261
0
      (commit_reflog->selector == SELECTOR_NONE && force_date)) {
262
0
    info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
263
0
    strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
264
0
  } else {
265
0
    strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
266
0
          - 2 - commit_reflog->recno);
267
0
  }
268
269
0
  strbuf_addch(sb, '}');
270
0
}
271
272
void get_reflog_message(struct strbuf *sb,
273
      struct reflog_walk_info *reflog_info)
274
0
{
275
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
276
0
  struct reflog_info *info;
277
0
  size_t len;
278
279
0
  if (!commit_reflog)
280
0
    return;
281
282
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
283
0
  len = strlen(info->message);
284
0
  if (len > 0)
285
0
    len--; /* strip away trailing newline */
286
0
  strbuf_add(sb, info->message, len);
287
0
}
288
289
const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
290
0
{
291
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
292
0
  struct reflog_info *info;
293
294
0
  if (!commit_reflog)
295
0
    return NULL;
296
297
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
298
0
  return info->email;
299
0
}
300
301
timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
302
0
{
303
0
  struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
304
0
  struct reflog_info *info;
305
306
0
  if (!commit_reflog)
307
0
    return 0;
308
309
0
  info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
310
0
  return info->timestamp;
311
0
}
312
313
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
314
       struct date_mode dmode, int force_date)
315
0
{
316
0
  if (reflog_info && reflog_info->last_commit_reflog) {
317
0
    struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
318
0
    struct reflog_info *info;
319
0
    struct strbuf selector = STRBUF_INIT;
320
321
0
    info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
322
0
    get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
323
0
    if (oneline) {
324
0
      printf("%s: %s", selector.buf, info->message);
325
0
    }
326
0
    else {
327
0
      printf("Reflog: %s (%s)\nReflog message: %s",
328
0
             selector.buf, info->email, info->message);
329
0
    }
330
331
0
    strbuf_release(&selector);
332
0
  }
333
0
}
334
335
int reflog_walk_empty(struct reflog_walk_info *info)
336
0
{
337
0
  return !info || !info->nr;
338
0
}
339
340
static struct commit *next_reflog_commit(struct commit_reflog *log)
341
0
{
342
0
  for (; log->recno >= 0; log->recno--) {
343
0
    struct reflog_info *entry = &log->reflogs->items[log->recno];
344
0
    struct object *obj = parse_object(the_repository,
345
0
              &entry->noid);
346
347
0
    if (obj && obj->type == OBJ_COMMIT)
348
0
      return (struct commit *)obj;
349
0
  }
350
0
  return NULL;
351
0
}
352
353
static timestamp_t log_timestamp(struct commit_reflog *log)
354
0
{
355
0
  return log->reflogs->items[log->recno].timestamp;
356
0
}
357
358
struct commit *next_reflog_entry(struct reflog_walk_info *walk)
359
0
{
360
0
  struct commit_reflog *best = NULL;
361
0
  struct commit *best_commit = NULL;
362
0
  size_t i;
363
364
0
  for (i = 0; i < walk->nr; i++) {
365
0
    struct commit_reflog *log = walk->logs[i];
366
0
    struct commit *commit = next_reflog_commit(log);
367
368
0
    if (!commit)
369
0
      continue;
370
371
0
    if (!best || log_timestamp(log) > log_timestamp(best)) {
372
0
      best = log;
373
0
      best_commit = commit;
374
0
    }
375
0
  }
376
377
0
  if (best) {
378
0
    best->recno--;
379
0
    walk->last_commit_reflog = best;
380
0
    return best_commit;
381
0
  }
382
383
0
  return NULL;
384
0
}