Skip to content

Commit 3400c22

Browse files
committed
Merge branch 'nd/decorate-grafts'
* nd/decorate-grafts: log: Do not decorate replacements with --no-replace-objects log: decorate "replaced" on to replaced commits log: decorate grafted commits with "grafted" Move write_shallow_commits to fetch-pack.c Add for_each_commit_graft() to iterate all grafts decoration: do not mis-decorate refs with same prefix
2 parents 2730f55 + b9ad500 commit 3400c22

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

builtin/fetch-pack.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ static void consume_shallow_list(int fd)
185185
}
186186
}
187187

188+
struct write_shallow_data {
189+
struct strbuf *out;
190+
int use_pack_protocol;
191+
int count;
192+
};
193+
194+
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
195+
{
196+
struct write_shallow_data *data = cb_data;
197+
const char *hex = sha1_to_hex(graft->sha1);
198+
data->count++;
199+
if (data->use_pack_protocol)
200+
packet_buf_write(data->out, "shallow %s", hex);
201+
else {
202+
strbuf_addstr(data->out, hex);
203+
strbuf_addch(data->out, '\n');
204+
}
205+
return 0;
206+
}
207+
208+
static int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
209+
{
210+
struct write_shallow_data data;
211+
data.out = out;
212+
data.use_pack_protocol = use_pack_protocol;
213+
data.count = 0;
214+
for_each_commit_graft(write_one_shallow, &data);
215+
return data.count;
216+
}
217+
188218
static enum ack_type get_ack(int fd, unsigned char *result_sha1)
189219
{
190220
static char line[1000];

commit.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,12 @@ struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
214214
return commit_graft[pos];
215215
}
216216

217-
int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
218-
{
219-
int i, count = 0;
220-
for (i = 0; i < commit_graft_nr; i++)
221-
if (commit_graft[i]->nr_parent < 0) {
222-
const char *hex =
223-
sha1_to_hex(commit_graft[i]->sha1);
224-
count++;
225-
if (use_pack_protocol)
226-
packet_buf_write(out, "shallow %s", hex);
227-
else {
228-
strbuf_addstr(out, hex);
229-
strbuf_addch(out, '\n');
230-
}
231-
}
232-
return count;
217+
int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
218+
{
219+
int i, ret;
220+
for (i = ret = 0; i < commit_graft_nr && !ret; i++)
221+
ret = fn(commit_graft[i], cb_data);
222+
return ret;
233223
}
234224

235225
int unregister_shallow(const unsigned char *sha1)

commit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct commit_graft {
142142
int nr_parent; /* < 0 if shallow commit */
143143
unsigned char parent[FLEX_ARRAY][20]; /* more */
144144
};
145+
typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
145146

146147
struct commit_graft *read_graft_line(char *buf, int len);
147148
int register_commit_graft(struct commit_graft *, int);
@@ -153,7 +154,7 @@ extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
153154

154155
extern int register_shallow(const unsigned char *sha1);
155156
extern int unregister_shallow(const unsigned char *sha1);
156-
extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol);
157+
extern int for_each_commit_graft(each_commit_graft_fn, void *);
157158
extern int is_repository_shallow(void);
158159
extern struct commit_list *get_shallow_commits(struct object_array *heads,
159160
int depth, int shallow_flag, int not_shallow_flag);

log-tree.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum decoration_type {
1818
DECORATION_REF_TAG,
1919
DECORATION_REF_STASH,
2020
DECORATION_REF_HEAD,
21+
DECORATION_GRAFTED,
2122
};
2223

2324
static char decoration_colors[][COLOR_MAXLEN] = {
@@ -27,6 +28,7 @@ static char decoration_colors[][COLOR_MAXLEN] = {
2728
GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
2829
GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
2930
GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
31+
GIT_COLOR_BOLD_BLUE, /* GRAFTED */
3032
};
3133

3234
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
@@ -90,16 +92,32 @@ static void add_name_decoration(enum decoration_type type, const char *name, str
9092

9193
static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
9294
{
93-
struct object *obj = parse_object(sha1);
95+
struct object *obj;
9496
enum decoration_type type = DECORATION_NONE;
97+
98+
if (!prefixcmp(refname, "refs/replace/")) {
99+
unsigned char original_sha1[20];
100+
if (!read_replace_refs)
101+
return 0;
102+
if (get_sha1_hex(refname + 13, original_sha1)) {
103+
warning("invalid replace ref %s", refname);
104+
return 0;
105+
}
106+
obj = parse_object(original_sha1);
107+
if (obj)
108+
add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
109+
return 0;
110+
}
111+
112+
obj = parse_object(sha1);
95113
if (!obj)
96114
return 0;
97115

98-
if (!prefixcmp(refname, "refs/heads"))
116+
if (!prefixcmp(refname, "refs/heads/"))
99117
type = DECORATION_REF_LOCAL;
100-
else if (!prefixcmp(refname, "refs/remotes"))
118+
else if (!prefixcmp(refname, "refs/remotes/"))
101119
type = DECORATION_REF_REMOTE;
102-
else if (!prefixcmp(refname, "refs/tags"))
120+
else if (!prefixcmp(refname, "refs/tags/"))
103121
type = DECORATION_REF_TAG;
104122
else if (!prefixcmp(refname, "refs/stash"))
105123
type = DECORATION_REF_STASH;
@@ -118,13 +136,23 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
118136
return 0;
119137
}
120138

139+
static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
140+
{
141+
struct commit *commit = lookup_commit(graft->sha1);
142+
if (!commit)
143+
return 0;
144+
add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
145+
return 0;
146+
}
147+
121148
void load_ref_decorations(int flags)
122149
{
123150
static int loaded;
124151
if (!loaded) {
125152
loaded = 1;
126153
for_each_ref(add_ref_decoration, &flags);
127154
head_ref(add_ref_decoration, &flags);
155+
for_each_commit_graft(add_graft_decoration, NULL);
128156
}
129157
}
130158

0 commit comments

Comments
 (0)