]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/checkout_helpers.c
8256644db041270aaa80abf30a1764e80c1af100
[libgit2.git] / tests / checkout / checkout_helpers.c
1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3 #include "refs.h"
4 #include "fileops.h"
5 #include "index.h"
6
7 void assert_on_branch(git_repository *repo, const char *branch)
8 {
9 git_reference *head;
10 git_buf bname = GIT_BUF_INIT;
11
12 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
13 cl_assert_(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC, branch);
14
15 cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
16 cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
17
18 git_reference_free(head);
19 git_buf_dispose(&bname);
20 }
21
22 void reset_index_to_treeish(git_object *treeish)
23 {
24 git_object *tree;
25 git_index *index;
26 git_repository *repo = git_object_owner(treeish);
27
28 cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJECT_TREE));
29
30 cl_git_pass(git_repository_index(&index, repo));
31 cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
32 cl_git_pass(git_index_write(index));
33
34 git_object_free(tree);
35 git_index_free(index);
36 }
37
38 int checkout_count_callback(
39 git_checkout_notify_t why,
40 const char *path,
41 const git_diff_file *baseline,
42 const git_diff_file *target,
43 const git_diff_file *workdir,
44 void *payload)
45 {
46 checkout_counts *ct = payload;
47
48 GIT_UNUSED(baseline); GIT_UNUSED(target); GIT_UNUSED(workdir);
49
50 if (why & GIT_CHECKOUT_NOTIFY_CONFLICT) {
51 ct->n_conflicts++;
52
53 if (ct->debug) {
54 if (workdir) {
55 if (baseline) {
56 if (target)
57 fprintf(stderr, "M %s (conflicts with M %s)\n",
58 workdir->path, target->path);
59 else
60 fprintf(stderr, "M %s (conflicts with D %s)\n",
61 workdir->path, baseline->path);
62 } else {
63 if (target)
64 fprintf(stderr, "Existing %s (conflicts with A %s)\n",
65 workdir->path, target->path);
66 else
67 fprintf(stderr, "How can an untracked file be a conflict (%s)\n", workdir->path);
68 }
69 } else {
70 if (baseline) {
71 if (target)
72 fprintf(stderr, "D %s (conflicts with M %s)\n",
73 target->path, baseline->path);
74 else
75 fprintf(stderr, "D %s (conflicts with D %s)\n",
76 baseline->path, baseline->path);
77 } else {
78 if (target)
79 fprintf(stderr, "How can an added file with no workdir be a conflict (%s)\n", target->path);
80 else
81 fprintf(stderr, "How can a nonexistent file be a conflict (%s)\n", path);
82 }
83 }
84 }
85 }
86
87 if (why & GIT_CHECKOUT_NOTIFY_DIRTY) {
88 ct->n_dirty++;
89
90 if (ct->debug) {
91 if (workdir)
92 fprintf(stderr, "M %s\n", workdir->path);
93 else
94 fprintf(stderr, "D %s\n", baseline->path);
95 }
96 }
97
98 if (why & GIT_CHECKOUT_NOTIFY_UPDATED) {
99 ct->n_updates++;
100
101 if (ct->debug) {
102 if (baseline) {
103 if (target)
104 fprintf(stderr, "update: M %s\n", path);
105 else
106 fprintf(stderr, "update: D %s\n", path);
107 } else {
108 if (target)
109 fprintf(stderr, "update: A %s\n", path);
110 else
111 fprintf(stderr, "update: this makes no sense %s\n", path);
112 }
113 }
114 }
115
116 if (why & GIT_CHECKOUT_NOTIFY_UNTRACKED) {
117 ct->n_untracked++;
118
119 if (ct->debug)
120 fprintf(stderr, "? %s\n", path);
121 }
122
123 if (why & GIT_CHECKOUT_NOTIFY_IGNORED) {
124 ct->n_ignored++;
125
126 if (ct->debug)
127 fprintf(stderr, "I %s\n", path);
128 }
129
130 return 0;
131 }
132
133 void tick_index(git_index *index)
134 {
135 struct timespec ts;
136 struct p_timeval times[2];
137
138 cl_assert(index->on_disk);
139 cl_assert(git_index_path(index));
140
141 cl_git_pass(git_index_read(index, true));
142 ts = index->stamp.mtime;
143
144 times[0].tv_sec = ts.tv_sec;
145 times[0].tv_usec = ts.tv_nsec / 1000;
146 times[1].tv_sec = ts.tv_sec + 5;
147 times[1].tv_usec = ts.tv_nsec / 1000;
148
149 cl_git_pass(p_utimes(git_index_path(index), times));
150 cl_git_pass(git_index_read(index, true));
151 }