Merge branch 'nd/diff-stat-with-summary' into jch
[git] / ls-refs.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "refs.h"
4 #include "remote.h"
5 #include "argv-array.h"
6 #include "ls-refs.h"
7 #include "pkt-line.h"
8
9 struct ls_refs_data {
10         unsigned peel;
11         unsigned symrefs;
12         struct argv_array patterns;
13 };
14
15 /*
16  * Check if one of the patterns matches the tail part of the ref.
17  * If no patterns were provided, all refs match.
18  */
19 static int ref_match(const struct argv_array *patterns, const char *refname)
20 {
21         char *pathbuf;
22         int i;
23
24         if (!patterns->argc)
25                 return 1; /* no restriction */
26
27         pathbuf = xstrfmt("/%s", refname);
28         for (i = 0; i < patterns->argc; i++) {
29                 if (!wildmatch(patterns->argv[i], pathbuf, 0)) {
30                         free(pathbuf);
31                         return 1;
32                 }
33         }
34         free(pathbuf);
35         return 0;
36 }
37
38 static int send_ref(const char *refname, const struct object_id *oid,
39                     int flag, void *cb_data)
40 {
41         struct ls_refs_data *data = cb_data;
42         const char *refname_nons = strip_namespace(refname);
43         struct strbuf refline = STRBUF_INIT;
44
45         if (!ref_match(&data->patterns, refname))
46                 return 0;
47
48         strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
49         if (data->symrefs && flag & REF_ISSYMREF) {
50                 struct object_id unused;
51                 const char *symref_target = resolve_ref_unsafe(refname, 0,
52                                                                &unused,
53                                                                &flag);
54
55                 if (!symref_target)
56                         die("'%s' is a symref but it is not?", refname);
57
58                 strbuf_addf(&refline, " symref-target:%s", symref_target);
59         }
60
61         if (data->peel) {
62                 struct object_id peeled;
63                 if (!peel_ref(refname, &peeled))
64                         strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
65         }
66
67         strbuf_addch(&refline, '\n');
68         packet_write(1, refline.buf, refline.len);
69
70         strbuf_release(&refline);
71         return 0;
72 }
73
74 int ls_refs(struct repository *r, struct argv_array *keys, struct argv_array *args)
75 {
76         int i;
77         struct ls_refs_data data = { 0, 0, ARGV_ARRAY_INIT };
78
79         for (i = 0; i < args->argc; i++) {
80                 const char *arg = args->argv[i];
81                 const char *out;
82
83                 if (!strcmp("peel", arg))
84                         data.peel = 1;
85                 else if (!strcmp("symrefs", arg))
86                         data.symrefs = 1;
87                 else if (skip_prefix(arg, "ref-pattern ", &out))
88                         argv_array_pushf(&data.patterns, "*/%s", out);
89         }
90
91         head_ref_namespaced(send_ref, &data);
92         for_each_namespaced_ref(send_ref, &data);
93         packet_flush(1);
94         argv_array_clear(&data.patterns);
95         return 0;
96 }