worktree.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef WORKTREE_H
  2. #define WORKTREE_H
  3. #include "cache.h"
  4. #include "refs.h"
  5. struct strbuf;
  6. struct worktree {
  7. char *path;
  8. char *id;
  9. char *head_ref; /* NULL if HEAD is broken or detached */
  10. char *lock_reason; /* private - use worktree_lock_reason */
  11. struct object_id head_oid;
  12. int is_detached;
  13. int is_bare;
  14. int is_current;
  15. int lock_reason_valid; /* private */
  16. };
  17. /*
  18. * Get the worktrees. The primary worktree will always be the first returned,
  19. * and linked worktrees will follow in no particular order.
  20. *
  21. * The caller is responsible for freeing the memory from the returned
  22. * worktrees by calling free_worktrees().
  23. */
  24. struct worktree **get_worktrees(void);
  25. /*
  26. * Returns 1 if linked worktrees exist, 0 otherwise.
  27. */
  28. int submodule_uses_worktrees(const char *path);
  29. /*
  30. * Return git dir of the worktree. Note that the path may be relative.
  31. * If wt is NULL, git dir of current worktree is returned.
  32. */
  33. const char *get_worktree_git_dir(const struct worktree *wt);
  34. /*
  35. * Search for the worktree identified unambiguously by `arg` -- typically
  36. * supplied by the user via the command-line -- which may be a pathname or some
  37. * shorthand uniquely identifying a worktree, thus making it convenient for the
  38. * user to specify a worktree with minimal typing. For instance, if the last
  39. * component (say, "foo") of a worktree's pathname is unique among worktrees
  40. * (say, "work/foo" and "work/bar"), it can be used to identify the worktree
  41. * unambiguously.
  42. *
  43. * `prefix` should be the `prefix` handed to top-level Git commands along with
  44. * `argc` and `argv`.
  45. *
  46. * Return the worktree identified by `arg`, or NULL if not found.
  47. */
  48. struct worktree *find_worktree(struct worktree **list,
  49. const char *prefix,
  50. const char *arg);
  51. /*
  52. * Return the worktree corresponding to `path`, or NULL if no such worktree
  53. * exists.
  54. */
  55. struct worktree *find_worktree_by_path(struct worktree **, const char *path);
  56. /*
  57. * Return true if the given worktree is the main one.
  58. */
  59. int is_main_worktree(const struct worktree *wt);
  60. /*
  61. * Return the reason string if the given worktree is locked or NULL
  62. * otherwise.
  63. */
  64. const char *worktree_lock_reason(struct worktree *wt);
  65. #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
  66. /*
  67. * Return zero if the worktree is in good condition. Error message is
  68. * returned if "errmsg" is not NULL.
  69. */
  70. int validate_worktree(const struct worktree *wt,
  71. struct strbuf *errmsg,
  72. unsigned flags);
  73. /*
  74. * Update worktrees/xxx/gitdir with the new path.
  75. */
  76. void update_worktree_location(struct worktree *wt,
  77. const char *path_);
  78. typedef void (* worktree_repair_fn)(int iserr, const char *path,
  79. const char *msg, void *cb_data);
  80. /*
  81. * Visit each registered linked worktree and repair corruptions. For each
  82. * repair made or error encountered while attempting a repair, the callback
  83. * function, if non-NULL, is called with the path of the worktree and a
  84. * description of the repair or error, along with the callback user-data.
  85. */
  86. void repair_worktrees(worktree_repair_fn, void *cb_data);
  87. /*
  88. * Repair administrative files corresponding to the worktree at the given path.
  89. * The worktree's .git file pointing at the repository must be intact for the
  90. * repair to succeed. Useful for re-associating an orphaned worktree with the
  91. * repository if the worktree has been moved manually (without using "git
  92. * worktree move"). For each repair made or error encountered while attempting
  93. * a repair, the callback function, if non-NULL, is called with the path of the
  94. * worktree and a description of the repair or error, along with the callback
  95. * user-data.
  96. */
  97. void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
  98. /*
  99. * Free up the memory for worktree(s)
  100. */
  101. void free_worktrees(struct worktree **);
  102. /*
  103. * Check if a per-worktree symref points to a ref in the main worktree
  104. * or any linked worktree, and return the worktree that holds the ref,
  105. * or NULL otherwise. The result may be destroyed by the next call.
  106. */
  107. const struct worktree *find_shared_symref(const char *symref,
  108. const char *target);
  109. /*
  110. * Similar to head_ref() for all HEADs _except_ one from the current
  111. * worktree, which is covered by head_ref().
  112. */
  113. int other_head_refs(each_ref_fn fn, void *cb_data);
  114. int is_worktree_being_rebased(const struct worktree *wt, const char *target);
  115. int is_worktree_being_bisected(const struct worktree *wt, const char *target);
  116. /*
  117. * Similar to git_path() but can produce paths for a specified
  118. * worktree instead of current one
  119. */
  120. const char *worktree_git_path(const struct worktree *wt,
  121. const char *fmt, ...)
  122. __attribute__((format (printf, 2, 3)));
  123. /*
  124. * Parse a worktree ref (i.e. with prefix main-worktree/ or
  125. * worktrees/) and return the position of the worktree's name and
  126. * length (or NULL and zero if it's main worktree), and ref.
  127. *
  128. * All name, name_length and ref arguments could be NULL.
  129. */
  130. int parse_worktree_ref(const char *worktree_ref, const char **name,
  131. int *name_length, const char **ref);
  132. /*
  133. * Return a refname suitable for access from the current ref store.
  134. */
  135. void strbuf_worktree_ref(const struct worktree *wt,
  136. struct strbuf *sb,
  137. const char *refname);
  138. #endif