|
27 | 27 | #include "branch.h" |
28 | 28 | #include "sequencer.h" |
29 | 29 | #include "rebase-interactive.h" |
| 30 | +#include "reset.h" |
30 | 31 |
|
31 | 32 | #define DEFAULT_REFLOG_ACTION "rebase" |
32 | 33 |
|
@@ -766,144 +767,6 @@ static void add_var(struct strbuf *buf, const char *name, const char *value) |
766 | 767 | } |
767 | 768 | } |
768 | 769 |
|
769 | | -#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" |
770 | | - |
771 | | -#define RESET_HEAD_DETACH (1<<0) |
772 | | -#define RESET_HEAD_HARD (1<<1) |
773 | | -#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2) |
774 | | -#define RESET_HEAD_REFS_ONLY (1<<3) |
775 | | -#define RESET_ORIG_HEAD (1<<4) |
776 | | - |
777 | | -static int reset_head(struct repository *r, struct object_id *oid, const char *action, |
778 | | - const char *switch_to_branch, unsigned flags, |
779 | | - const char *reflog_orig_head, const char *reflog_head, |
780 | | - const char *default_reflog_action) |
781 | | -{ |
782 | | - unsigned detach_head = flags & RESET_HEAD_DETACH; |
783 | | - unsigned reset_hard = flags & RESET_HEAD_HARD; |
784 | | - unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; |
785 | | - unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; |
786 | | - unsigned update_orig_head = flags & RESET_ORIG_HEAD; |
787 | | - struct object_id head_oid; |
788 | | - struct tree_desc desc[2] = { { NULL }, { NULL } }; |
789 | | - struct lock_file lock = LOCK_INIT; |
790 | | - struct unpack_trees_options unpack_tree_opts; |
791 | | - struct tree *tree; |
792 | | - const char *reflog_action; |
793 | | - struct strbuf msg = STRBUF_INIT; |
794 | | - size_t prefix_len; |
795 | | - struct object_id *orig = NULL, oid_orig, |
796 | | - *old_orig = NULL, oid_old_orig; |
797 | | - int ret = 0, nr = 0; |
798 | | - |
799 | | - if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
800 | | - BUG("Not a fully qualified branch: '%s'", switch_to_branch); |
801 | | - |
802 | | - if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
803 | | - ret = -1; |
804 | | - goto leave_reset_head; |
805 | | - } |
806 | | - |
807 | | - if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { |
808 | | - ret = error(_("could not determine HEAD revision")); |
809 | | - goto leave_reset_head; |
810 | | - } |
811 | | - |
812 | | - if (!oid) |
813 | | - oid = &head_oid; |
814 | | - |
815 | | - if (refs_only) |
816 | | - goto reset_head_refs; |
817 | | - |
818 | | - memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); |
819 | | - setup_unpack_trees_porcelain(&unpack_tree_opts, action); |
820 | | - unpack_tree_opts.head_idx = 1; |
821 | | - unpack_tree_opts.src_index = r->index; |
822 | | - unpack_tree_opts.dst_index = r->index; |
823 | | - unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; |
824 | | - unpack_tree_opts.update = 1; |
825 | | - unpack_tree_opts.merge = 1; |
826 | | - if (!detach_head) |
827 | | - unpack_tree_opts.reset = 1; |
828 | | - |
829 | | - if (repo_read_index_unmerged(r) < 0) { |
830 | | - ret = error(_("could not read index")); |
831 | | - goto leave_reset_head; |
832 | | - } |
833 | | - |
834 | | - if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) { |
835 | | - ret = error(_("failed to find tree of %s"), |
836 | | - oid_to_hex(&head_oid)); |
837 | | - goto leave_reset_head; |
838 | | - } |
839 | | - |
840 | | - if (!fill_tree_descriptor(r, &desc[nr++], oid)) { |
841 | | - ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); |
842 | | - goto leave_reset_head; |
843 | | - } |
844 | | - |
845 | | - if (unpack_trees(nr, desc, &unpack_tree_opts)) { |
846 | | - ret = -1; |
847 | | - goto leave_reset_head; |
848 | | - } |
849 | | - |
850 | | - tree = parse_tree_indirect(oid); |
851 | | - prime_cache_tree(r, r->index, tree); |
852 | | - |
853 | | - if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) { |
854 | | - ret = error(_("could not write index")); |
855 | | - goto leave_reset_head; |
856 | | - } |
857 | | - |
858 | | -reset_head_refs: |
859 | | - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); |
860 | | - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); |
861 | | - prefix_len = msg.len; |
862 | | - |
863 | | - if (update_orig_head) { |
864 | | - if (!get_oid("ORIG_HEAD", &oid_old_orig)) |
865 | | - old_orig = &oid_old_orig; |
866 | | - if (!get_oid("HEAD", &oid_orig)) { |
867 | | - orig = &oid_orig; |
868 | | - if (!reflog_orig_head) { |
869 | | - strbuf_addstr(&msg, "updating ORIG_HEAD"); |
870 | | - reflog_orig_head = msg.buf; |
871 | | - } |
872 | | - update_ref(reflog_orig_head, "ORIG_HEAD", orig, |
873 | | - old_orig, 0, UPDATE_REFS_MSG_ON_ERR); |
874 | | - } else if (old_orig) |
875 | | - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); |
876 | | - } |
877 | | - |
878 | | - if (!reflog_head) { |
879 | | - strbuf_setlen(&msg, prefix_len); |
880 | | - strbuf_addstr(&msg, "updating HEAD"); |
881 | | - reflog_head = msg.buf; |
882 | | - } |
883 | | - if (!switch_to_branch) |
884 | | - ret = update_ref(reflog_head, "HEAD", oid, orig, |
885 | | - detach_head ? REF_NO_DEREF : 0, |
886 | | - UPDATE_REFS_MSG_ON_ERR); |
887 | | - else { |
888 | | - ret = update_ref(reflog_head, switch_to_branch, oid, |
889 | | - NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
890 | | - if (!ret) |
891 | | - ret = create_symref("HEAD", switch_to_branch, |
892 | | - reflog_head); |
893 | | - } |
894 | | - if (run_hook) |
895 | | - run_hook_le(NULL, "post-checkout", |
896 | | - oid_to_hex(orig ? orig : &null_oid), |
897 | | - oid_to_hex(oid), "1", NULL); |
898 | | - |
899 | | -leave_reset_head: |
900 | | - strbuf_release(&msg); |
901 | | - rollback_lock_file(&lock); |
902 | | - while (nr) |
903 | | - free((void *)desc[--nr].buffer); |
904 | | - return ret; |
905 | | -} |
906 | | - |
907 | 770 | static int move_to_original_branch(struct rebase_options *opts) |
908 | 771 | { |
909 | 772 | struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; |
|
0 commit comments