]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/describe/t6120.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / describe / t6120.c
CommitLineData
3a728fb5 1#include "clar_libgit2.h"
2#include "describe_helpers.h"
3#include "repository.h"
4
ac3d33df 5/* Ported from https://github.com/git/git/blob/adfc1857bdb090786fd9d22c1acec39371c76048/t/t6120-describe.sh */
3a728fb5 6
7static git_repository *repo;
8
9void test_describe_t6120__initialize(void)
10{
11 repo = cl_git_sandbox_init("describe");
12}
13
14void test_describe_t6120__cleanup(void)
15{
16 cl_git_sandbox_cleanup();
17}
18
19void test_describe_t6120__default(void)
20{
25345c0c 21 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8
CMN
22 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
23
5431c46a
CMN
24 assert_describe("A-*", "HEAD", repo, &opts, &fmt_opts);
25 assert_describe("A-*", "HEAD^", repo, &opts, &fmt_opts);
26 assert_describe("R-*", "HEAD^^", repo, &opts, &fmt_opts);
27 assert_describe("A-*", "HEAD^^2", repo, &opts, &fmt_opts);
28 assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
29 assert_describe("R-*", "HEAD^^^", repo, &opts, &fmt_opts);
3a728fb5 30}
31
32void test_describe_t6120__tags(void)
33{
25345c0c 34 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8 35 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
3a728fb5 36 opts.describe_strategy = GIT_DESCRIBE_TAGS;
37
5431c46a
CMN
38 assert_describe("c-*", "HEAD", repo, &opts, &fmt_opts);
39 assert_describe("c-*", "HEAD^", repo, &opts, &fmt_opts);
40 assert_describe("e-*", "HEAD^^", repo, &opts, &fmt_opts);
41 assert_describe("c-*", "HEAD^^2", repo, &opts, &fmt_opts);
42 assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
43 assert_describe("e", "HEAD^^^", repo, &opts, &fmt_opts);
3a728fb5 44}
45
46void test_describe_t6120__all(void)
47{
25345c0c 48 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8 49 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
3a728fb5 50 opts.describe_strategy = GIT_DESCRIBE_ALL;
51
5431c46a
CMN
52 assert_describe("heads/master", "HEAD", repo, &opts, &fmt_opts);
53 assert_describe("tags/c-*", "HEAD^", repo, &opts, &fmt_opts);
54 assert_describe("tags/e", "HEAD^^^", repo, &opts, &fmt_opts);
3a728fb5 55}
56
57void test_describe_t6120__longformat(void)
58{
25345c0c 59 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8
CMN
60 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
61
62 fmt_opts.always_use_long_format = 1;
3a728fb5 63
5431c46a
CMN
64 assert_describe("B-0-*", "HEAD^^2^", repo, &opts, &fmt_opts);
65 assert_describe("A-3-*", "HEAD^^2", repo, &opts, &fmt_opts);
3a728fb5 66}
67
68void test_describe_t6120__firstparent(void)
69{
25345c0c 70 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8 71 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
3a728fb5 72 opts.describe_strategy = GIT_DESCRIBE_TAGS;
73
5431c46a 74 assert_describe("c-7-*", "HEAD", repo, &opts, &fmt_opts);
3a728fb5 75
76 opts.only_follow_first_parent = 1;
5431c46a 77 assert_describe("e-3-*", "HEAD", repo, &opts, &fmt_opts);
3a728fb5 78}
79
fd8126e4
CMN
80void test_describe_t6120__workdir(void)
81{
25345c0c 82 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
fd8126e4
CMN
83 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
84
5431c46a 85 assert_describe_workdir("A-*[0-9a-f]", repo, &opts, &fmt_opts);
fd8126e4
CMN
86 cl_git_mkfile("describe/file", "something different");
87
88 fmt_opts.dirty_suffix = "-dirty";
5431c46a 89 assert_describe_workdir("A-*[0-9a-f]-dirty", repo, &opts, &fmt_opts);
fd8126e4 90 fmt_opts.dirty_suffix = ".mod";
5431c46a 91 assert_describe_workdir("A-*[0-9a-f].mod", repo, &opts, &fmt_opts);
fd8126e4
CMN
92}
93
3a728fb5 94static void commit_and_tag(
95 git_time_t *time,
96 const char *commit_msg,
97 const char *tag_name)
98{
99 git_index *index;
100 git_oid commit_id;
101 git_reference *ref;
102
103 cl_git_pass(git_repository_index__weakptr(&index, repo));
104
105 cl_git_append2file("describe/file", "\n");
106
ac3d33df
JK
107 cl_git_pass(git_index_add_bypath(index, "file"));
108 cl_git_pass(git_index_write(index));
3a728fb5 109
110 *time += 10;
111 cl_repo_commit_from_index(&commit_id, repo, NULL, *time, commit_msg);
112
113 if (tag_name == NULL)
114 return;
115
659cf202 116 cl_git_pass(git_reference_create(&ref, repo, tag_name, &commit_id, 0, NULL));
3a728fb5 117 git_reference_free(ref);
118}
119
120void test_describe_t6120__pattern(void)
121{
25345c0c 122 git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
3b6534b8 123 git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
3a728fb5 124 git_oid tag_id;
125 git_object *head;
126 git_signature *tagger;
127 git_time_t time;
128
129 /* set-up matching pattern tests */
130 cl_git_pass(git_revparse_single(&head, repo, "HEAD"));
131
132 time = 1380553019;
133 cl_git_pass(git_signature_new(&tagger, "tagger", "tagger@libgit2.org", time, 0));
134 cl_git_pass(git_tag_create(&tag_id, repo, "test-annotated", head, tagger, "test-annotated", 0));
135 git_signature_free(tagger);
136 git_object_free(head);
137
138 commit_and_tag(&time, "one more", "refs/tags/test1-lightweight");
139 commit_and_tag(&time, "yet another", "refs/tags/test2-lightweight");
140 commit_and_tag(&time, "even more", NULL);
141
142
143 /* Exercize */
144 opts.pattern = "test-*";
5431c46a 145 assert_describe("test-annotated-*", "HEAD", repo, &opts, &fmt_opts);
3a728fb5 146
147 opts.describe_strategy = GIT_DESCRIBE_TAGS;
148 opts.pattern = "test1-*";
5431c46a 149 assert_describe("test1-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
3a728fb5 150
151 opts.pattern = "test2-*";
5431c46a 152 assert_describe("test2-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
3a728fb5 153
3b6534b8 154 fmt_opts.always_use_long_format = 1;
5431c46a 155 assert_describe("test2-lightweight-*", "HEAD^", repo, &opts, &fmt_opts);
3a728fb5 156}