refs.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. #ifndef REFS_H
  2. #define REFS_H
  3. #include "cache.h"
  4. struct object_id;
  5. struct ref_store;
  6. struct repository;
  7. struct strbuf;
  8. struct string_list;
  9. struct string_list_item;
  10. struct worktree;
  11. /*
  12. * Resolve a reference, recursively following symbolic refererences.
  13. *
  14. * Return the name of the non-symbolic reference that ultimately pointed
  15. * at the resolved object name. The return value, if not NULL, is a
  16. * pointer into either a static buffer or the input ref.
  17. *
  18. * If oid is non-NULL, store the referred-to object's name in it.
  19. *
  20. * If the reference cannot be resolved to an object, the behavior
  21. * depends on the RESOLVE_REF_READING flag:
  22. *
  23. * - If RESOLVE_REF_READING is set, return NULL.
  24. *
  25. * - If RESOLVE_REF_READING is not set, clear oid and return the name of
  26. * the last reference name in the chain, which will either be a non-symbolic
  27. * reference or an undefined reference. If this is a prelude to
  28. * "writing" to the ref, the return value is the name of the ref
  29. * that will actually be created or changed.
  30. *
  31. * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
  32. * level of symbolic reference. The value stored in oid for a symbolic
  33. * reference will always be null_oid in this case, and the return
  34. * value is the reference that the symref refers to directly.
  35. *
  36. * If flags is non-NULL, set the value that it points to the
  37. * combination of REF_ISPACKED (if the reference was found among the
  38. * packed references), REF_ISSYMREF (if the initial reference was a
  39. * symbolic reference), REF_BAD_NAME (if the reference name is ill
  40. * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
  41. * (if the ref is malformed or has a bad name). See refs.h for more detail
  42. * on each flag.
  43. *
  44. * If ref is not a properly-formatted, normalized reference, return
  45. * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
  46. * give up and return NULL.
  47. *
  48. * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
  49. * name is invalid according to git-check-ref-format(1). If the name
  50. * is bad then the value stored in oid will be null_oid and the two
  51. * flags REF_ISBROKEN and REF_BAD_NAME will be set.
  52. *
  53. * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
  54. * directory and do not consist of all caps and underscores cannot be
  55. * resolved. The function returns NULL for such ref names.
  56. * Caps and underscores refers to the special refs, such as HEAD,
  57. * FETCH_HEAD and friends, that all live outside of the refs/ directory.
  58. */
  59. #define RESOLVE_REF_READING 0x01
  60. #define RESOLVE_REF_NO_RECURSE 0x02
  61. #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
  62. const char *refs_resolve_ref_unsafe(struct ref_store *refs,
  63. const char *refname,
  64. int resolve_flags,
  65. struct object_id *oid,
  66. int *flags);
  67. const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
  68. struct object_id *oid, int *flags);
  69. char *refs_resolve_refdup(struct ref_store *refs,
  70. const char *refname, int resolve_flags,
  71. struct object_id *oid, int *flags);
  72. char *resolve_refdup(const char *refname, int resolve_flags,
  73. struct object_id *oid, int *flags);
  74. int refs_read_ref_full(struct ref_store *refs, const char *refname,
  75. int resolve_flags, struct object_id *oid, int *flags);
  76. int read_ref_full(const char *refname, int resolve_flags,
  77. struct object_id *oid, int *flags);
  78. int read_ref(const char *refname, struct object_id *oid);
  79. /*
  80. * Return 0 if a reference named refname could be created without
  81. * conflicting with the name of an existing reference. Otherwise,
  82. * return a negative value and write an explanation to err. If extras
  83. * is non-NULL, it is a list of additional refnames with which refname
  84. * is not allowed to conflict. If skip is non-NULL, ignore potential
  85. * conflicts with refs in skip (e.g., because they are scheduled for
  86. * deletion in the same operation). Behavior is undefined if the same
  87. * name is listed in both extras and skip.
  88. *
  89. * Two reference names conflict if one of them exactly matches the
  90. * leading components of the other; e.g., "foo/bar" conflicts with
  91. * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
  92. * "foo/barbados".
  93. *
  94. * extras and skip must be sorted.
  95. */
  96. int refs_verify_refname_available(struct ref_store *refs,
  97. const char *refname,
  98. const struct string_list *extras,
  99. const struct string_list *skip,
  100. struct strbuf *err);
  101. int refs_ref_exists(struct ref_store *refs, const char *refname);
  102. int ref_exists(const char *refname);
  103. int should_autocreate_reflog(const char *refname);
  104. int is_branch(const char *refname);
  105. int refs_init_db(struct strbuf *err);
  106. /*
  107. * If refname is a non-symbolic reference that refers to a tag object,
  108. * and the tag can be (recursively) dereferenced to a non-tag object,
  109. * store the object ID of the referred-to object to oid and return 0.
  110. * If any of these conditions are not met, return a non-zero value.
  111. * Symbolic references are considered unpeelable, even if they
  112. * ultimately resolve to a peelable tag.
  113. */
  114. int refs_peel_ref(struct ref_store *refs, const char *refname,
  115. struct object_id *oid);
  116. int peel_ref(const char *refname, struct object_id *oid);
  117. /**
  118. * Resolve refname in the nested "gitlink" repository in the specified
  119. * submodule (which must be non-NULL). If the resolution is
  120. * successful, return 0 and set oid to the name of the object;
  121. * otherwise, return a non-zero value.
  122. */
  123. int resolve_gitlink_ref(const char *submodule, const char *refname,
  124. struct object_id *oid);
  125. /*
  126. * Return true iff abbrev_name is a possible abbreviation for
  127. * full_name according to the rules defined by ref_rev_parse_rules in
  128. * refs.c.
  129. */
  130. int refname_match(const char *abbrev_name, const char *full_name);
  131. /*
  132. * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
  133. * the results to 'prefixes'
  134. */
  135. struct strvec;
  136. void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
  137. int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
  138. int repo_dwim_ref(struct repository *r, const char *str, int len,
  139. struct object_id *oid, char **ref, int nonfatal_dangling_mark);
  140. int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
  141. static inline int dwim_ref(const char *str, int len, struct object_id *oid,
  142. char **ref, int nonfatal_dangling_mark)
  143. {
  144. return repo_dwim_ref(the_repository, str, len, oid, ref,
  145. nonfatal_dangling_mark);
  146. }
  147. int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
  148. /*
  149. * Retrieves the default branch name for newly-initialized repositories.
  150. *
  151. * The return value of `repo_default_branch_name()` is an allocated string. The
  152. * return value of `git_default_branch_name()` is a singleton.
  153. */
  154. const char *git_default_branch_name(void);
  155. char *repo_default_branch_name(struct repository *r);
  156. /*
  157. * A ref_transaction represents a collection of reference updates that
  158. * should succeed or fail together.
  159. *
  160. * Calling sequence
  161. * ----------------
  162. *
  163. * - Allocate and initialize a `struct ref_transaction` by calling
  164. * `ref_transaction_begin()`.
  165. *
  166. * - Specify the intended ref updates by calling one or more of the
  167. * following functions:
  168. * - `ref_transaction_update()`
  169. * - `ref_transaction_create()`
  170. * - `ref_transaction_delete()`
  171. * - `ref_transaction_verify()`
  172. *
  173. * - Then either:
  174. *
  175. * - Optionally call `ref_transaction_prepare()` to prepare the
  176. * transaction. This locks all references, checks preconditions,
  177. * etc. but doesn't finalize anything. If this step fails, the
  178. * transaction has been closed and can only be freed. If this step
  179. * succeeds, then `ref_transaction_commit()` is almost certain to
  180. * succeed. However, you can still call `ref_transaction_abort()`
  181. * if you decide not to commit the transaction after all.
  182. *
  183. * - Call `ref_transaction_commit()` to execute the transaction,
  184. * make the changes permanent, and release all locks. If you
  185. * haven't already called `ref_transaction_prepare()`, then
  186. * `ref_transaction_commit()` calls it for you.
  187. *
  188. * Or
  189. *
  190. * - Call `initial_ref_transaction_commit()` if the ref database is
  191. * known to be empty and have no other writers (e.g. during
  192. * clone). This is likely to be much faster than
  193. * `ref_transaction_commit()`. `ref_transaction_prepare()` should
  194. * *not* be called before `initial_ref_transaction_commit()`.
  195. *
  196. * - Then finally, call `ref_transaction_free()` to free the
  197. * `ref_transaction` data structure.
  198. *
  199. * At any time before calling `ref_transaction_commit()`, you can call
  200. * `ref_transaction_abort()` to abort the transaction, rollback any
  201. * locks, and free any associated resources (including the
  202. * `ref_transaction` data structure).
  203. *
  204. * Putting it all together, a complete reference update looks like
  205. *
  206. * struct ref_transaction *transaction;
  207. * struct strbuf err = STRBUF_INIT;
  208. * int ret = 0;
  209. *
  210. * transaction = ref_store_transaction_begin(refs, &err);
  211. * if (!transaction ||
  212. * ref_transaction_update(...) ||
  213. * ref_transaction_create(...) ||
  214. * ...etc... ||
  215. * ref_transaction_commit(transaction, &err)) {
  216. * error("%s", err.buf);
  217. * ret = -1;
  218. * }
  219. * ref_transaction_free(transaction);
  220. * strbuf_release(&err);
  221. * return ret;
  222. *
  223. * Error handling
  224. * --------------
  225. *
  226. * On error, transaction functions append a message about what
  227. * went wrong to the 'err' argument. The message mentions what
  228. * ref was being updated (if any) when the error occurred so it
  229. * can be passed to 'die' or 'error' as-is.
  230. *
  231. * The message is appended to err without first clearing err.
  232. * err will not be '\n' terminated.
  233. *
  234. * Caveats
  235. * -------
  236. *
  237. * Note that no locks are taken, and no refs are read, until
  238. * `ref_transaction_prepare()` or `ref_transaction_commit()` is
  239. * called. So, for example, `ref_transaction_verify()` won't report a
  240. * verification failure until the commit is attempted.
  241. */
  242. struct ref_transaction;
  243. /*
  244. * Bit values set in the flags argument passed to each_ref_fn() and
  245. * stored in ref_iterator::flags. Other bits are for internal use
  246. * only:
  247. */
  248. /* Reference is a symbolic reference. */
  249. #define REF_ISSYMREF 0x01
  250. /* Reference is a packed reference. */
  251. #define REF_ISPACKED 0x02
  252. /*
  253. * Reference cannot be resolved to an object name: dangling symbolic
  254. * reference (directly or indirectly), corrupt reference file,
  255. * reference exists but name is bad, or symbolic reference refers to
  256. * ill-formatted reference name.
  257. */
  258. #define REF_ISBROKEN 0x04
  259. /*
  260. * Reference name is not well formed.
  261. *
  262. * See git-check-ref-format(1) for the definition of well formed ref names.
  263. */
  264. #define REF_BAD_NAME 0x08
  265. /*
  266. * The signature for the callback function for the for_each_*()
  267. * functions below. The memory pointed to by the refname and oid
  268. * arguments is only guaranteed to be valid for the duration of a
  269. * single callback invocation.
  270. */
  271. typedef int each_ref_fn(const char *refname,
  272. const struct object_id *oid, int flags, void *cb_data);
  273. /*
  274. * The same as each_ref_fn, but also with a repository argument that
  275. * contains the repository associated with the callback.
  276. */
  277. typedef int each_repo_ref_fn(struct repository *r,
  278. const char *refname,
  279. const struct object_id *oid,
  280. int flags,
  281. void *cb_data);
  282. /*
  283. * The following functions invoke the specified callback function for
  284. * each reference indicated. If the function ever returns a nonzero
  285. * value, stop the iteration and return that value. Please note that
  286. * it is not safe to modify references while an iteration is in
  287. * progress, unless the same callback function invocation that
  288. * modifies the reference also returns a nonzero value to immediately
  289. * stop the iteration. Returned references are sorted.
  290. */
  291. int refs_head_ref(struct ref_store *refs,
  292. each_ref_fn fn, void *cb_data);
  293. int refs_for_each_ref(struct ref_store *refs,
  294. each_ref_fn fn, void *cb_data);
  295. int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
  296. each_ref_fn fn, void *cb_data);
  297. int refs_for_each_tag_ref(struct ref_store *refs,
  298. each_ref_fn fn, void *cb_data);
  299. int refs_for_each_branch_ref(struct ref_store *refs,
  300. each_ref_fn fn, void *cb_data);
  301. int refs_for_each_remote_ref(struct ref_store *refs,
  302. each_ref_fn fn, void *cb_data);
  303. /* just iterates the head ref. */
  304. int head_ref(each_ref_fn fn, void *cb_data);
  305. /* iterates all refs. */
  306. int for_each_ref(each_ref_fn fn, void *cb_data);
  307. /**
  308. * iterates all refs which have a defined prefix and strips that prefix from
  309. * the passed variable refname.
  310. */
  311. int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
  312. int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
  313. each_ref_fn fn, void *cb_data,
  314. unsigned int broken);
  315. int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,
  316. unsigned int broken);
  317. /**
  318. * iterate refs from the respective area.
  319. */
  320. int for_each_tag_ref(each_ref_fn fn, void *cb_data);
  321. int for_each_branch_ref(each_ref_fn fn, void *cb_data);
  322. int for_each_remote_ref(each_ref_fn fn, void *cb_data);
  323. int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
  324. /* iterates all refs that match the specified glob pattern. */
  325. int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
  326. int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
  327. const char *prefix, void *cb_data);
  328. int head_ref_namespaced(each_ref_fn fn, void *cb_data);
  329. int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
  330. /* can be used to learn about broken ref and symref */
  331. int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
  332. int for_each_rawref(each_ref_fn fn, void *cb_data);
  333. /*
  334. * Normalizes partial refs to their fully qualified form.
  335. * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
  336. * <prefix> will default to 'refs/' if NULL.
  337. *
  338. * item.string will be set to the result.
  339. * item.util will be set to NULL if <pattern> contains glob characters, or
  340. * non-NULL if it doesn't.
  341. */
  342. void normalize_glob_ref(struct string_list_item *item, const char *prefix,
  343. const char *pattern);
  344. static inline const char *has_glob_specials(const char *pattern)
  345. {
  346. return strpbrk(pattern, "?*[");
  347. }
  348. void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
  349. void warn_dangling_symrefs(FILE *fp, const char *msg_fmt,
  350. const struct string_list *refnames);
  351. /*
  352. * Flags for controlling behaviour of pack_refs()
  353. * PACK_REFS_PRUNE: Prune loose refs after packing
  354. * PACK_REFS_ALL: Pack _all_ refs, not just tags and already packed refs
  355. */
  356. #define PACK_REFS_PRUNE 0x0001
  357. #define PACK_REFS_ALL 0x0002
  358. /*
  359. * Write a packed-refs file for the current repository.
  360. * flags: Combination of the above PACK_REFS_* flags.
  361. */
  362. int refs_pack_refs(struct ref_store *refs, unsigned int flags);
  363. /*
  364. * Setup reflog before using. Fill in err and return -1 on failure.
  365. */
  366. int refs_create_reflog(struct ref_store *refs, const char *refname,
  367. int force_create, struct strbuf *err);
  368. int safe_create_reflog(const char *refname, int force_create, struct strbuf *err);
  369. /** Reads log for the value of ref during at_time. **/
  370. int read_ref_at(struct ref_store *refs,
  371. const char *refname, unsigned int flags,
  372. timestamp_t at_time, int cnt,
  373. struct object_id *oid, char **msg,
  374. timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
  375. /** Check if a particular reflog exists */
  376. int refs_reflog_exists(struct ref_store *refs, const char *refname);
  377. int reflog_exists(const char *refname);
  378. /*
  379. * Delete the specified reference. If old_oid is non-NULL, then
  380. * verify that the current value of the reference is old_oid before
  381. * deleting it. If old_oid is NULL, delete the reference if it
  382. * exists, regardless of its old value. It is an error for old_oid to
  383. * be null_oid. msg and flags are passed through to
  384. * ref_transaction_delete().
  385. */
  386. int refs_delete_ref(struct ref_store *refs, const char *msg,
  387. const char *refname,
  388. const struct object_id *old_oid,
  389. unsigned int flags);
  390. int delete_ref(const char *msg, const char *refname,
  391. const struct object_id *old_oid, unsigned int flags);
  392. /*
  393. * Delete the specified references. If there are any problems, emit
  394. * errors but attempt to keep going (i.e., the deletes are not done in
  395. * an all-or-nothing transaction). msg and flags are passed through to
  396. * ref_transaction_delete().
  397. */
  398. int refs_delete_refs(struct ref_store *refs, const char *msg,
  399. struct string_list *refnames, unsigned int flags);
  400. int delete_refs(const char *msg, struct string_list *refnames,
  401. unsigned int flags);
  402. /** Delete a reflog */
  403. int refs_delete_reflog(struct ref_store *refs, const char *refname);
  404. int delete_reflog(const char *refname);
  405. /*
  406. * Callback to process a reflog entry found by the iteration functions (see
  407. * below)
  408. */
  409. typedef int each_reflog_ent_fn(
  410. struct object_id *old_oid, struct object_id *new_oid,
  411. const char *committer, timestamp_t timestamp,
  412. int tz, const char *msg, void *cb_data);
  413. /* Iterate over reflog entries in the log for `refname`. */
  414. /* oldest entry first */
  415. int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
  416. each_reflog_ent_fn fn, void *cb_data);
  417. /* youngest entry first */
  418. int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
  419. const char *refname,
  420. each_reflog_ent_fn fn,
  421. void *cb_data);
  422. /*
  423. * Iterate over reflog entries in the log for `refname` in the main ref store.
  424. */
  425. /* oldest entry first */
  426. int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);
  427. /* youngest entry first */
  428. int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);
  429. /*
  430. * Calls the specified function for each reflog file until it returns nonzero,
  431. * and returns the value. Reflog file order is unspecified.
  432. */
  433. int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data);
  434. int for_each_reflog(each_ref_fn fn, void *cb_data);
  435. #define REFNAME_ALLOW_ONELEVEL 1
  436. #define REFNAME_REFSPEC_PATTERN 2
  437. /*
  438. * Return 0 iff refname has the correct format for a refname according
  439. * to the rules described in Documentation/git-check-ref-format.txt.
  440. * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
  441. * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
  442. * allow a single "*" wildcard character in the refspec. No leading or
  443. * repeated slashes are accepted.
  444. */
  445. int check_refname_format(const char *refname, int flags);
  446. /*
  447. * Apply the rules from check_refname_format, but mutate the result until it
  448. * is acceptable, and place the result in "out".
  449. */
  450. void sanitize_refname_component(const char *refname, struct strbuf *out);
  451. const char *prettify_refname(const char *refname);
  452. char *refs_shorten_unambiguous_ref(struct ref_store *refs,
  453. const char *refname, int strict);
  454. char *shorten_unambiguous_ref(const char *refname, int strict);
  455. /** rename ref, return 0 on success **/
  456. int refs_rename_ref(struct ref_store *refs, const char *oldref,
  457. const char *newref, const char *logmsg);
  458. int rename_ref(const char *oldref, const char *newref,
  459. const char *logmsg);
  460. /** copy ref, return 0 on success **/
  461. int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
  462. const char *newref, const char *logmsg);
  463. int copy_existing_ref(const char *oldref, const char *newref,
  464. const char *logmsg);
  465. int refs_create_symref(struct ref_store *refs, const char *refname,
  466. const char *target, const char *logmsg);
  467. int create_symref(const char *refname, const char *target, const char *logmsg);
  468. enum action_on_err {
  469. UPDATE_REFS_MSG_ON_ERR,
  470. UPDATE_REFS_DIE_ON_ERR,
  471. UPDATE_REFS_QUIET_ON_ERR
  472. };
  473. /*
  474. * Begin a reference transaction. The reference transaction must
  475. * be freed by calling ref_transaction_free().
  476. */
  477. struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
  478. struct strbuf *err);
  479. struct ref_transaction *ref_transaction_begin(struct strbuf *err);
  480. /*
  481. * Reference transaction updates
  482. *
  483. * The following four functions add a reference check or update to a
  484. * ref_transaction. They have some common similar parameters:
  485. *
  486. * transaction -- a pointer to an open ref_transaction, obtained
  487. * from ref_transaction_begin().
  488. *
  489. * refname -- the name of the reference to be affected.
  490. *
  491. * new_oid -- the object ID that should be set to be the new value
  492. * of the reference. Some functions allow this parameter to be
  493. * NULL, meaning that the reference is not changed, or
  494. * null_oid, meaning that the reference should be deleted. A
  495. * copy of this value is made in the transaction.
  496. *
  497. * old_oid -- the object ID that the reference must have before
  498. * the update. Some functions allow this parameter to be NULL,
  499. * meaning that the old value of the reference is not checked,
  500. * or null_oid, meaning that the reference must not exist
  501. * before the update. A copy of this value is made in the
  502. * transaction.
  503. *
  504. * flags -- flags affecting the update, passed to
  505. * update_ref_lock(). Possible flags: REF_NO_DEREF,
  506. * REF_FORCE_CREATE_REFLOG. See those constants for more
  507. * information.
  508. *
  509. * msg -- a message describing the change (for the reflog).
  510. *
  511. * err -- a strbuf for receiving a description of any error that
  512. * might have occurred.
  513. *
  514. * The functions make internal copies of refname and msg, so the
  515. * caller retains ownership of these parameters.
  516. *
  517. * The functions return 0 on success and non-zero on failure. A
  518. * failure means that the transaction as a whole has failed and needs
  519. * to be rolled back.
  520. */
  521. /*
  522. * The following flags can be passed to ref_transaction_update() etc.
  523. * Internally, they are stored in `ref_update::flags`, along with some
  524. * internal flags.
  525. */
  526. /*
  527. * Act on the ref directly; i.e., without dereferencing symbolic refs.
  528. * If this flag is not specified, then symbolic references are
  529. * dereferenced and the update is applied to the referent.
  530. */
  531. #define REF_NO_DEREF (1 << 0)
  532. /*
  533. * Force the creation of a reflog for this reference, even if it
  534. * didn't previously have a reflog.
  535. */
  536. #define REF_FORCE_CREATE_REFLOG (1 << 1)
  537. /*
  538. * Bitmask of all of the flags that are allowed to be passed in to
  539. * ref_transaction_update() and friends:
  540. */
  541. #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
  542. (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG)
  543. /*
  544. * Add a reference update to transaction. `new_oid` is the value that
  545. * the reference should have after the update, or `null_oid` if it
  546. * should be deleted. If `new_oid` is NULL, then the reference is not
  547. * changed at all. `old_oid` is the value that the reference must have
  548. * before the update, or `null_oid` if it must not have existed
  549. * beforehand. The old value is checked after the lock is taken to
  550. * prevent races. If the old value doesn't agree with old_oid, the
  551. * whole transaction fails. If old_oid is NULL, then the previous
  552. * value is not checked.
  553. *
  554. * See the above comment "Reference transaction updates" for more
  555. * information.
  556. */
  557. int ref_transaction_update(struct ref_transaction *transaction,
  558. const char *refname,
  559. const struct object_id *new_oid,
  560. const struct object_id *old_oid,
  561. unsigned int flags, const char *msg,
  562. struct strbuf *err);
  563. /*
  564. * Add a reference creation to transaction. new_oid is the value that
  565. * the reference should have after the update; it must not be
  566. * null_oid. It is verified that the reference does not exist
  567. * already.
  568. *
  569. * See the above comment "Reference transaction updates" for more
  570. * information.
  571. */
  572. int ref_transaction_create(struct ref_transaction *transaction,
  573. const char *refname,
  574. const struct object_id *new_oid,
  575. unsigned int flags, const char *msg,
  576. struct strbuf *err);
  577. /*
  578. * Add a reference deletion to transaction. If old_oid is non-NULL,
  579. * then it holds the value that the reference should have had before
  580. * the update (which must not be null_oid).
  581. *
  582. * See the above comment "Reference transaction updates" for more
  583. * information.
  584. */
  585. int ref_transaction_delete(struct ref_transaction *transaction,
  586. const char *refname,
  587. const struct object_id *old_oid,
  588. unsigned int flags, const char *msg,
  589. struct strbuf *err);
  590. /*
  591. * Verify, within a transaction, that refname has the value old_oid,
  592. * or, if old_oid is null_oid, then verify that the reference
  593. * doesn't exist. old_oid must be non-NULL.
  594. *
  595. * See the above comment "Reference transaction updates" for more
  596. * information.
  597. */
  598. int ref_transaction_verify(struct ref_transaction *transaction,
  599. const char *refname,
  600. const struct object_id *old_oid,
  601. unsigned int flags,
  602. struct strbuf *err);
  603. /* Naming conflict (for example, the ref names A and A/B conflict). */
  604. #define TRANSACTION_NAME_CONFLICT -1
  605. /* All other errors. */
  606. #define TRANSACTION_GENERIC_ERROR -2
  607. /*
  608. * Perform the preparatory stages of committing `transaction`. Acquire
  609. * any needed locks, check preconditions, etc.; basically, do as much
  610. * as possible to ensure that the transaction will be able to go
  611. * through, stopping just short of making any irrevocable or
  612. * user-visible changes. The updates that this function prepares can
  613. * be finished up by calling `ref_transaction_commit()` or rolled back
  614. * by calling `ref_transaction_abort()`.
  615. *
  616. * On success, return 0 and leave the transaction in "prepared" state.
  617. * On failure, abort the transaction, write an error message to `err`,
  618. * and return one of the `TRANSACTION_*` constants.
  619. *
  620. * Callers who don't need such fine-grained control over committing
  621. * reference transactions should just call `ref_transaction_commit()`.
  622. */
  623. int ref_transaction_prepare(struct ref_transaction *transaction,
  624. struct strbuf *err);
  625. /*
  626. * Commit all of the changes that have been queued in transaction, as
  627. * atomically as possible. On success, return 0 and leave the
  628. * transaction in "closed" state. On failure, roll back the
  629. * transaction, write an error message to `err`, and return one of the
  630. * `TRANSACTION_*` constants
  631. */
  632. int ref_transaction_commit(struct ref_transaction *transaction,
  633. struct strbuf *err);
  634. /*
  635. * Abort `transaction`, which has been begun and possibly prepared,
  636. * but not yet committed.
  637. */
  638. int ref_transaction_abort(struct ref_transaction *transaction,
  639. struct strbuf *err);
  640. /*
  641. * Like ref_transaction_commit(), but optimized for creating
  642. * references when originally initializing a repository (e.g., by "git
  643. * clone"). It writes the new references directly to packed-refs
  644. * without locking the individual references.
  645. *
  646. * It is a bug to call this function when there might be other
  647. * processes accessing the repository or if there are existing
  648. * references that might conflict with the ones being created. All
  649. * old_oid values must either be absent or null_oid.
  650. */
  651. int initial_ref_transaction_commit(struct ref_transaction *transaction,
  652. struct strbuf *err);
  653. /*
  654. * Free `*transaction` and all associated data.
  655. */
  656. void ref_transaction_free(struct ref_transaction *transaction);
  657. /**
  658. * Lock, update, and unlock a single reference. This function
  659. * basically does a transaction containing a single call to
  660. * ref_transaction_update(). The parameters to this function have the
  661. * same meaning as the corresponding parameters to
  662. * ref_transaction_update(). Handle errors as requested by the `onerr`
  663. * argument.
  664. */
  665. int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
  666. const struct object_id *new_oid, const struct object_id *old_oid,
  667. unsigned int flags, enum action_on_err onerr);
  668. int update_ref(const char *msg, const char *refname,
  669. const struct object_id *new_oid, const struct object_id *old_oid,
  670. unsigned int flags, enum action_on_err onerr);
  671. int parse_hide_refs_config(const char *var, const char *value, const char *);
  672. /*
  673. * Check whether a ref is hidden. If no namespace is set, both the first and
  674. * the second parameter point to the full ref name. If a namespace is set and
  675. * the ref is inside that namespace, the first parameter is a pointer to the
  676. * name of the ref with the namespace prefix removed. If a namespace is set and
  677. * the ref is outside that namespace, the first parameter is NULL. The second
  678. * parameter always points to the full ref name.
  679. */
  680. int ref_is_hidden(const char *, const char *);
  681. enum ref_type {
  682. REF_TYPE_PER_WORKTREE, /* refs inside refs/ but not shared */
  683. REF_TYPE_PSEUDOREF, /* refs outside refs/ in current worktree */
  684. REF_TYPE_MAIN_PSEUDOREF, /* pseudo refs from the main worktree */
  685. REF_TYPE_OTHER_PSEUDOREF, /* pseudo refs from other worktrees */
  686. REF_TYPE_NORMAL, /* normal/shared refs inside refs/ */
  687. };
  688. enum ref_type ref_type(const char *refname);
  689. enum expire_reflog_flags {
  690. EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
  691. EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
  692. EXPIRE_REFLOGS_VERBOSE = 1 << 2,
  693. EXPIRE_REFLOGS_REWRITE = 1 << 3
  694. };
  695. /*
  696. * The following interface is used for reflog expiration. The caller
  697. * calls reflog_expire(), supplying it with three callback functions,
  698. * of the following types. The callback functions define the
  699. * expiration policy that is desired.
  700. *
  701. * reflog_expiry_prepare_fn -- Called once after the reference is
  702. * locked.
  703. *
  704. * reflog_expiry_should_prune_fn -- Called once for each entry in the
  705. * existing reflog. It should return true iff that entry should be
  706. * pruned.
  707. *
  708. * reflog_expiry_cleanup_fn -- Called once before the reference is
  709. * unlocked again.
  710. */
  711. typedef void reflog_expiry_prepare_fn(const char *refname,
  712. const struct object_id *oid,
  713. void *cb_data);
  714. typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
  715. struct object_id *noid,
  716. const char *email,
  717. timestamp_t timestamp, int tz,
  718. const char *message, void *cb_data);
  719. typedef void reflog_expiry_cleanup_fn(void *cb_data);
  720. /*
  721. * Expire reflog entries for the specified reference. oid is the old
  722. * value of the reference. flags is a combination of the constants in
  723. * enum expire_reflog_flags. The three function pointers are described
  724. * above. On success, return zero.
  725. */
  726. int refs_reflog_expire(struct ref_store *refs,
  727. const char *refname,
  728. const struct object_id *oid,
  729. unsigned int flags,
  730. reflog_expiry_prepare_fn prepare_fn,
  731. reflog_expiry_should_prune_fn should_prune_fn,
  732. reflog_expiry_cleanup_fn cleanup_fn,
  733. void *policy_cb_data);
  734. int reflog_expire(const char *refname, const struct object_id *oid,
  735. unsigned int flags,
  736. reflog_expiry_prepare_fn prepare_fn,
  737. reflog_expiry_should_prune_fn should_prune_fn,
  738. reflog_expiry_cleanup_fn cleanup_fn,
  739. void *policy_cb_data);
  740. int ref_storage_backend_exists(const char *name);
  741. struct ref_store *get_main_ref_store(struct repository *r);
  742. /**
  743. * Submodules
  744. * ----------
  745. *
  746. * If you want to iterate the refs of a submodule you first need to add the
  747. * submodules object database. You can do this by a code-snippet like
  748. * this:
  749. *
  750. * const char *path = "path/to/submodule"
  751. * if (add_submodule_odb(path))
  752. * die("Error submodule '%s' not populated.", path);
  753. *
  754. * `add_submodule_odb()` will return zero on success. If you
  755. * do not do this you will get an error for each ref that it does not point
  756. * to a valid object.
  757. *
  758. * Note: As a side-effect of this you cannot safely assume that all
  759. * objects you lookup are available in superproject. All submodule objects
  760. * will be available the same way as the superprojects objects.
  761. *
  762. * Example:
  763. * --------
  764. *
  765. * ----
  766. * static int handle_remote_ref(const char *refname,
  767. * const unsigned char *sha1, int flags, void *cb_data)
  768. * {
  769. * struct strbuf *output = cb_data;
  770. * strbuf_addf(output, "%s\n", refname);
  771. * return 0;
  772. * }
  773. *
  774. */
  775. /*
  776. * Return the ref_store instance for the specified submodule. For the
  777. * main repository, use submodule==NULL; such a call cannot fail. For
  778. * a submodule, the submodule must exist and be a nonbare repository,
  779. * otherwise return NULL. If the requested reference store has not yet
  780. * been initialized, initialize it first.
  781. *
  782. * For backwards compatibility, submodule=="" is treated the same as
  783. * submodule==NULL.
  784. */
  785. struct ref_store *get_submodule_ref_store(const char *submodule);
  786. struct ref_store *get_worktree_ref_store(const struct worktree *wt);
  787. #endif /* REFS_H */