merge-recursive: honor diff.algorithm
[git/gitster.git] / oidset.c
blob8d36aef8dca4fca8aa979692a536687a21f2b90d
1 #include "git-compat-util.h"
2 #include "oidset.h"
3 #include "hex.h"
4 #include "strbuf.h"
6 void oidset_init(struct oidset *set, size_t initial_size)
8 memset(&set->set, 0, sizeof(set->set));
9 if (initial_size)
10 kh_resize_oid_set(&set->set, initial_size);
13 int oidset_contains(const struct oidset *set, const struct object_id *oid)
15 khiter_t pos = kh_get_oid_set(&set->set, *oid);
16 return pos != kh_end(&set->set);
19 int oidset_insert(struct oidset *set, const struct object_id *oid)
21 int added;
22 kh_put_oid_set(&set->set, *oid, &added);
23 return !added;
26 void oidset_insert_from_set(struct oidset *dest, struct oidset *src)
28 struct oidset_iter iter;
29 struct object_id *src_oid;
31 oidset_iter_init(src, &iter);
32 while ((src_oid = oidset_iter_next(&iter)))
33 oidset_insert(dest, src_oid);
36 int oidset_remove(struct oidset *set, const struct object_id *oid)
38 khiter_t pos = kh_get_oid_set(&set->set, *oid);
39 if (pos == kh_end(&set->set))
40 return 0;
41 kh_del_oid_set(&set->set, pos);
42 return 1;
45 void oidset_clear(struct oidset *set)
47 kh_release_oid_set(&set->set);
48 oidset_init(set, 0);
51 void oidset_parse_file(struct oidset *set, const char *path,
52 const struct git_hash_algo *algop)
54 oidset_parse_file_carefully(set, path, algop, NULL, NULL);
57 void oidset_parse_file_carefully(struct oidset *set, const char *path,
58 const struct git_hash_algo *algop,
59 oidset_parse_tweak_fn fn, void *cbdata)
61 FILE *fp;
62 struct strbuf sb = STRBUF_INIT;
63 struct object_id oid;
65 fp = fopen(path, "r");
66 if (!fp)
67 die("could not open object name list: %s", path);
68 while (!strbuf_getline(&sb, fp)) {
69 const char *p;
70 const char *name;
73 * Allow trailing comments, leading whitespace
74 * (including before commits), and empty or whitespace
75 * only lines.
77 name = strchr(sb.buf, '#');
78 if (name)
79 strbuf_setlen(&sb, name - sb.buf);
80 strbuf_trim(&sb);
81 if (!sb.len)
82 continue;
84 if (parse_oid_hex_algop(sb.buf, &oid, &p, algop) || *p != '\0')
85 die("invalid object name: %s", sb.buf);
86 if (fn && fn(&oid, cbdata))
87 continue;
88 oidset_insert(set, &oid);
90 if (ferror(fp))
91 die_errno("Could not read '%s'", path);
92 fclose(fp);
93 strbuf_release(&sb);