Blame


1 718b3ab0 2018-03-17 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 718b3ab0 2018-03-17 stsp *
4 718b3ab0 2018-03-17 stsp * Permission to use, copy, modify, and distribute this software for any
5 718b3ab0 2018-03-17 stsp * purpose with or without fee is hereby granted, provided that the above
6 718b3ab0 2018-03-17 stsp * copyright notice and this permission notice appear in all copies.
7 718b3ab0 2018-03-17 stsp *
8 718b3ab0 2018-03-17 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 718b3ab0 2018-03-17 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 718b3ab0 2018-03-17 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 718b3ab0 2018-03-17 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 718b3ab0 2018-03-17 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 718b3ab0 2018-03-17 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 718b3ab0 2018-03-17 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 718b3ab0 2018-03-17 stsp */
16 718b3ab0 2018-03-17 stsp
17 718b3ab0 2018-03-17 stsp /*
18 718b3ab0 2018-03-17 stsp * State information for a tracked file in a work tree.
19 718b3ab0 2018-03-17 stsp * When written to disk, multi-byte fields are written in big-endian.
20 718b3ab0 2018-03-17 stsp * Some fields are based on results from stat(2). These are only used in
21 718b3ab0 2018-03-17 stsp * order to detect modifications made to on-disk files, they are never
22 718b3ab0 2018-03-17 stsp * applied back to the filesystem.
23 718b3ab0 2018-03-17 stsp */
24 718b3ab0 2018-03-17 stsp struct got_fileindex_entry {
25 133d2798 2019-01-08 stsp RB_ENTRY(got_fileindex_entry) entry;
26 718b3ab0 2018-03-17 stsp uint64_t ctime_sec;
27 718b3ab0 2018-03-17 stsp uint64_t ctime_nsec;
28 718b3ab0 2018-03-17 stsp uint64_t mtime_sec;
29 718b3ab0 2018-03-17 stsp uint64_t mtime_nsec;
30 718b3ab0 2018-03-17 stsp uint32_t uid;
31 718b3ab0 2018-03-17 stsp uint32_t gid;
32 718b3ab0 2018-03-17 stsp /*
33 718b3ab0 2018-03-17 stsp * On-disk size is truncated to the lower 32 bits.
34 718b3ab0 2018-03-17 stsp * The value is only used to check for modifications anyway.
35 718b3ab0 2018-03-17 stsp */
36 718b3ab0 2018-03-17 stsp uint32_t size;
37 718b3ab0 2018-03-17 stsp
38 718b3ab0 2018-03-17 stsp uint16_t mode;
39 a7f9d64d 2019-01-13 stsp #define GOT_FILEIDX_MODE_FILE_TYPE 0x000f
40 1e0a3718 2020-07-23 stsp #define GOT_FILEIDX_MODE_FILE_TYPE_ONDISK 0x0003
41 1e0a3718 2020-07-23 stsp #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED 0x000c
42 1e0a3718 2020-07-23 stsp #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT 2
43 a7f9d64d 2019-01-13 stsp #define GOT_FILEIDX_MODE_REGULAR_FILE 1
44 a7f9d64d 2019-01-13 stsp #define GOT_FILEIDX_MODE_SYMLINK 2
45 2e1fa222 2020-07-23 stsp #define GOT_FILEIDX_MODE_BAD_SYMLINK 3
46 ecd6d584 2020-06-27 stsp #define GOT_FILEIDX_MODE_PERMS 0xfff0
47 a7f9d64d 2019-01-13 stsp #define GOT_FILEIDX_MODE_PERMS_SHIFT 4
48 718b3ab0 2018-03-17 stsp
49 718b3ab0 2018-03-17 stsp /* SHA1 of corresponding blob in repository. */
50 718b3ab0 2018-03-17 stsp uint8_t blob_sha1[SHA1_DIGEST_LENGTH];
51 718b3ab0 2018-03-17 stsp
52 fc76cabb 2018-12-25 stsp /* SHA1 of corresponding base commit in repository. */
53 fc76cabb 2018-12-25 stsp uint8_t commit_sha1[SHA1_DIGEST_LENGTH];
54 fc76cabb 2018-12-25 stsp
55 718b3ab0 2018-03-17 stsp uint32_t flags;
56 718b3ab0 2018-03-17 stsp
57 718b3ab0 2018-03-17 stsp /*
58 718b3ab0 2018-03-17 stsp * UNIX-style path, relative to work tree root.
59 718b3ab0 2018-03-17 stsp * Variable length, and NUL-padded to a multiple of 8 on disk.
60 718b3ab0 2018-03-17 stsp */
61 718b3ab0 2018-03-17 stsp char *path;
62 df335242 2019-08-03 stsp
63 df335242 2019-08-03 stsp /*
64 df335242 2019-08-03 stsp * (since GOT_FILE_INDEX_VERSION 2)
65 df335242 2019-08-03 stsp * SHA1 of staged blob in repository if stage equals either
66 df335242 2019-08-03 stsp * GOT_FILEIDX_STAGE_MODIFY or GOT_FILEIDX_STAGE_ADD.
67 df335242 2019-08-03 stsp * Otherwise, this field is not written to disk.
68 df335242 2019-08-03 stsp */
69 df335242 2019-08-03 stsp uint8_t staged_blob_sha1[SHA1_DIGEST_LENGTH];
70 718b3ab0 2018-03-17 stsp };
71 718b3ab0 2018-03-17 stsp
72 83718700 2019-08-03 stsp /* Modifications explicitly staged for commit. */
73 83718700 2019-08-03 stsp #define GOT_FILEIDX_STAGE_NONE 0
74 83718700 2019-08-03 stsp #define GOT_FILEIDX_STAGE_MODIFY 1
75 83718700 2019-08-03 stsp #define GOT_FILEIDX_STAGE_ADD 2
76 83718700 2019-08-03 stsp #define GOT_FILEIDX_STAGE_DELETE 3
77 83718700 2019-08-03 stsp
78 133d2798 2019-01-08 stsp struct got_fileindex;
79 718b3ab0 2018-03-17 stsp
80 7a9df742 2019-01-08 stsp RB_HEAD(got_fileindex_tree, got_fileindex_entry);
81 7a9df742 2019-01-08 stsp
82 4d555405 2019-08-03 stsp size_t got_fileindex_entry_path_len(const struct got_fileindex_entry *);
83 4d555405 2019-08-03 stsp
84 7a9df742 2019-01-08 stsp static inline int
85 7a9df742 2019-01-08 stsp got_fileindex_cmp(const struct got_fileindex_entry *e1,
86 7a9df742 2019-01-08 stsp const struct got_fileindex_entry *e2)
87 7a9df742 2019-01-08 stsp {
88 4d555405 2019-08-03 stsp return got_path_cmp(e1->path, e2->path,
89 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(e1),
90 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(e2));
91 7a9df742 2019-01-08 stsp }
92 7a9df742 2019-01-08 stsp
93 7a9df742 2019-01-08 stsp RB_PROTOTYPE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);
94 7a9df742 2019-01-08 stsp
95 718b3ab0 2018-03-17 stsp /* On-disk file index header structure. */
96 718b3ab0 2018-03-17 stsp struct got_fileindex_hdr {
97 718b3ab0 2018-03-17 stsp uint32_t signature; /* big-endian */
98 718b3ab0 2018-03-17 stsp #define GOT_FILE_INDEX_SIGNATURE 0x676f7449 /* 'g', 'o', 't', 'I' */
99 718b3ab0 2018-03-17 stsp uint32_t version; /* big-endian */
100 df335242 2019-08-03 stsp #define GOT_FILE_INDEX_VERSION 2
101 718b3ab0 2018-03-17 stsp uint32_t nentries; /* big-endian */
102 718b3ab0 2018-03-17 stsp /* list of concatenated fileindex entries */
103 718b3ab0 2018-03-17 stsp uint8_t sha1[SHA1_DIGEST_LENGTH]; /* checksum of above on-disk data */
104 718b3ab0 2018-03-17 stsp };
105 718b3ab0 2018-03-17 stsp
106 0aeb8099 2020-07-23 stsp mode_t got_fileindex_entry_perms_get(struct got_fileindex_entry *);
107 26a7fe28 2019-07-27 stsp uint16_t got_fileindex_perms_from_st(struct stat *);
108 26a7fe28 2019-07-27 stsp mode_t got_fileindex_perms_to_st(struct got_fileindex_entry *);
109 26a7fe28 2019-07-27 stsp
110 51514078 2018-12-25 stsp const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *,
111 437adc9d 2020-12-10 yzhong int, const char *, uint8_t *, uint8_t *, int);
112 a769b60b 2021-06-27 stsp void got_fileindex_entry_mark_skipped(struct got_fileindex_entry *);
113 7426bbfd 2018-12-24 stsp const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
114 3969253a 2020-03-07 stsp const char *);
115 7426bbfd 2018-12-24 stsp void got_fileindex_entry_free(struct got_fileindex_entry *);
116 4d555405 2019-08-03 stsp
117 133d2798 2019-01-08 stsp struct got_fileindex *got_fileindex_alloc(void);
118 7426bbfd 2018-12-24 stsp void got_fileindex_free(struct got_fileindex *);
119 718b3ab0 2018-03-17 stsp const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
120 718b3ab0 2018-03-17 stsp const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
121 718b3ab0 2018-03-17 stsp struct got_fileindex_entry *);
122 133d2798 2019-01-08 stsp void got_fileindex_entry_remove(struct got_fileindex *,
123 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *);
124 51514078 2018-12-25 stsp struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
125 d6c87207 2019-08-02 stsp const char *, size_t);
126 52a74475 2018-12-24 stsp const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *);
127 b504a804 2019-01-08 stsp typedef const struct got_error *(*got_fileindex_cb)(void *,
128 b504a804 2019-01-08 stsp struct got_fileindex_entry *);
129 e1ed7f77 2019-01-06 stsp const struct got_error *got_fileindex_for_each_entry_safe(
130 b504a804 2019-01-08 stsp struct got_fileindex *, got_fileindex_cb cb, void *);
131 8da9e5f4 2019-01-12 stsp
132 f44ffd20 2019-02-04 stsp typedef const struct got_error *(*got_fileindex_diff_tree_old_new_cb)(void *,
133 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *, struct got_tree_entry *, const char *);
134 f44ffd20 2019-02-04 stsp typedef const struct got_error *(*got_fileindex_diff_tree_old_cb)(void *,
135 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *, const char *);
136 f44ffd20 2019-02-04 stsp typedef const struct got_error *(*got_fileindex_diff_tree_new_cb)(void *,
137 8da9e5f4 2019-01-12 stsp struct got_tree_entry *, const char *);
138 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb {
139 f44ffd20 2019-02-04 stsp got_fileindex_diff_tree_old_new_cb diff_old_new;
140 f44ffd20 2019-02-04 stsp got_fileindex_diff_tree_old_cb diff_old;
141 f44ffd20 2019-02-04 stsp got_fileindex_diff_tree_new_cb diff_new;
142 8da9e5f4 2019-01-12 stsp };
143 8da9e5f4 2019-01-12 stsp const struct got_error *got_fileindex_diff_tree(struct got_fileindex *,
144 c4cdcb68 2019-04-03 stsp struct got_tree_object *, const char *, const char *,
145 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
146 d1f6d47b 2019-02-04 stsp
147 d1f6d47b 2019-02-04 stsp typedef const struct got_error *(*got_fileindex_diff_dir_old_new_cb)(void *,
148 7f91a133 2019-12-13 stsp struct got_fileindex_entry *, struct dirent *, const char *, int);
149 d1f6d47b 2019-02-04 stsp typedef const struct got_error *(*got_fileindex_diff_dir_old_cb)(void *,
150 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *, const char *);
151 62da3196 2021-10-01 stsp typedef const struct got_error *(*got_fileindex_diff_dir_new_cb)(int *, void *,
152 7f91a133 2019-12-13 stsp struct dirent *, const char *, int);
153 3143d852 2020-06-25 stsp typedef const struct got_error *(*got_fileindex_diff_dir_traverse)(void *,
154 3143d852 2020-06-25 stsp const char *, int);
155 d1f6d47b 2019-02-04 stsp struct got_fileindex_diff_dir_cb {
156 d1f6d47b 2019-02-04 stsp got_fileindex_diff_dir_old_new_cb diff_old_new;
157 d1f6d47b 2019-02-04 stsp got_fileindex_diff_dir_old_cb diff_old;
158 d1f6d47b 2019-02-04 stsp got_fileindex_diff_dir_new_cb diff_new;
159 3143d852 2020-06-25 stsp got_fileindex_diff_dir_traverse diff_traverse;
160 d1f6d47b 2019-02-04 stsp };
161 6fc93f37 2019-12-13 stsp const struct got_error *got_fileindex_diff_dir(struct got_fileindex *, int,
162 927df6b7 2019-02-10 stsp const char *, const char *, struct got_repository *,
163 927df6b7 2019-02-10 stsp struct got_fileindex_diff_dir_cb *, void *);
164 d00136be 2019-03-26 stsp
165 d00136be 2019-03-26 stsp int got_fileindex_entry_has_blob(struct got_fileindex_entry *);
166 d00136be 2019-03-26 stsp int got_fileindex_entry_has_commit(struct got_fileindex_entry *);
167 2ec1f75b 2019-03-26 stsp int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *);
168 a769b60b 2021-06-27 stsp int got_fileindex_entry_was_skipped(struct got_fileindex_entry *);
169 0cb83759 2019-08-03 stsp uint32_t got_fileindex_entry_stage_get(const struct got_fileindex_entry *);
170 0cb83759 2019-08-03 stsp void got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t);
171 2e1fa222 2020-07-23 stsp int got_fileindex_entry_filetype_get(struct got_fileindex_entry *);
172 6131ab45 2020-07-23 stsp void got_fileindex_entry_filetype_set(struct got_fileindex_entry *, int);
173 984c073d 2020-07-23 stsp void got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *, int);
174 984c073d 2020-07-23 stsp int got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *);
175 2ec1f75b 2019-03-26 stsp
176 2ec1f75b 2019-03-26 stsp void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *);
177 b4b2adf5 2023-02-09 op
178 b4b2adf5 2023-02-09 op /*
179 b4b2adf5 2023-02-09 op * Retrieve staged, blob or commit id from a fileindex entry, and return
180 b4b2adf5 2023-02-09 op * the given object id.
181 b4b2adf5 2023-02-09 op */
182 b4b2adf5 2023-02-09 op struct got_object_id *got_fileindex_entry_get_staged_blob_id(
183 b4b2adf5 2023-02-09 op struct got_object_id *, struct got_fileindex_entry *);
184 b4b2adf5 2023-02-09 op struct got_object_id *got_fileindex_entry_get_blob_id(struct got_object_id *,
185 b4b2adf5 2023-02-09 op struct got_fileindex_entry *);
186 b4b2adf5 2023-02-09 op struct got_object_id *got_fileindex_entry_get_commit_id(struct got_object_id *,
187 b4b2adf5 2023-02-09 op struct got_fileindex_entry *);