2 #include "repository.h"
5 #include "argv-array.h"
10 * Check if one of the prefixes is a prefix of the ref.
11 * If no prefixes were provided, all refs match.
13 static int ref_match(const struct argv_array
*prefixes
, const char *refname
)
18 return 1; /* no restriction */
20 for (i
= 0; i
< prefixes
->argc
; i
++) {
21 const char *prefix
= prefixes
->argv
[i
];
23 if (starts_with(refname
, prefix
))
33 struct argv_array prefixes
;
36 static int send_ref(const char *refname
, const struct object_id
*oid
,
37 int flag
, void *cb_data
)
39 struct ls_refs_data
*data
= cb_data
;
40 const char *refname_nons
= strip_namespace(refname
);
41 struct strbuf refline
= STRBUF_INIT
;
43 if (!ref_match(&data
->prefixes
, refname
))
46 strbuf_addf(&refline
, "%s %s", oid_to_hex(oid
), refname_nons
);
47 if (data
->symrefs
&& flag
& REF_ISSYMREF
) {
48 struct object_id unused
;
49 const char *symref_target
= resolve_ref_unsafe(refname
, 0,
54 die("'%s' is a symref but it is not?", refname
);
56 strbuf_addf(&refline
, " symref-target:%s", symref_target
);
60 struct object_id peeled
;
61 if (!peel_ref(refname
, &peeled
))
62 strbuf_addf(&refline
, " peeled:%s", oid_to_hex(&peeled
));
65 strbuf_addch(&refline
, '\n');
66 packet_write(1, refline
.buf
, refline
.len
);
68 strbuf_release(&refline
);
72 int ls_refs(struct repository
*r
, struct argv_array
*keys
,
73 struct packet_reader
*request
)
75 struct ls_refs_data data
;
77 memset(&data
, 0, sizeof(data
));
79 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
80 const char *arg
= request
->line
;
83 if (!strcmp("peel", arg
))
85 else if (!strcmp("symrefs", arg
))
87 else if (skip_prefix(arg
, "ref-prefix ", &out
))
88 argv_array_push(&data
.prefixes
, out
);
91 head_ref_namespaced(send_ref
, &data
);
92 for_each_namespaced_ref(send_ref
, &data
);
94 argv_array_clear(&data
.prefixes
);