@matheusbsbs / matheus-bagliano / commits / 2bd452d3b9
Fork 0

git-update-index --unresolve

Retire git-unresolve and make it into "git-update-index --unresolve". It processes all paths that follow. During a merge, you would mark a path that is dealt with with: $ git update-index hello and you would "undo" it with: $ git update-index --unresolve hello Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 19, 2006 at 23:52 UTC 2bd452d3b9f62ecc3406307cd6a5553856d21ff2
4 files changed +128 -149
.gitignore
-1
@@ -111,7 +111,6 @@ git-tag
111 git-tar-tree
112 git-unpack-file
113 git-unpack-objects
114 -git-unresolve
114 git-update-index
115 git-update-ref
116 git-update-server-info
Makefile
+1 -2
@@ -165,8 +165,7 @@ PROGRAMS = \
165 git-upload-pack$X git-verify-pack$X git-write-tree$X \
166 git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
167 git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
168 - git-describe$X git-merge-tree$X git-blame$X git-imap-send$X \
169 - git-unresolve$X
168 + git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
169
170 BUILT_INS = git-log$X
171
unresolve.c deleted
-146
@@ -1,146 +0,0 @@
1 -#include "cache.h"
2 -#include "tree-walk.h"
3 -
4 -static const char unresolve_usage[] =
5 -"git-unresolve <paths>...";
6 -
7 -static struct cache_file cache_file;
8 -static unsigned char head_sha1[20];
9 -static unsigned char merge_head_sha1[20];
10 -
11 -static struct cache_entry *read_one_ent(const char *which,
12 - unsigned char *ent, const char *path,
13 - int namelen, int stage)
14 -{
15 - unsigned mode;
16 - unsigned char sha1[20];
17 - int size;
18 - struct cache_entry *ce;
19 -
20 - if (get_tree_entry(ent, path, sha1, &mode)) {
21 - error("%s: not in %s branch.", path, which);
22 - return NULL;
23 - }
24 - if (mode == S_IFDIR) {
25 - error("%s: not a blob in %s branch.", path, which);
26 - return NULL;
27 - }
28 - size = cache_entry_size(namelen);
29 - ce = xcalloc(1, size);
30 -
31 - memcpy(ce->sha1, sha1, 20);
32 - memcpy(ce->name, path, namelen);
33 - ce->ce_flags = create_ce_flags(namelen, stage);
34 - ce->ce_mode = create_ce_mode(mode);
35 - return ce;
36 -}
37 -
38 -static int unresolve_one(const char *path)
39 -{
40 - int namelen = strlen(path);
41 - int pos;
42 - int ret = 0;
43 - struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
44 -
45 - /* See if there is such entry in the index. */
46 - pos = cache_name_pos(path, namelen);
47 - if (pos < 0) {
48 - /* If there isn't, either it is unmerged, or
49 - * resolved as "removed" by mistake. We do not
50 - * want to do anything in the former case.
51 - */
52 - pos = -pos-1;
53 - if (pos < active_nr) {
54 - struct cache_entry *ce = active_cache[pos];
55 - if (ce_namelen(ce) == namelen &&
56 - !memcmp(ce->name, path, namelen)) {
57 - fprintf(stderr,
58 - "%s: skipping still unmerged path.\n",
59 - path);
60 - goto free_return;
61 - }
62 - }
63 - }
64 -
65 - /* Grab blobs from given path from HEAD and MERGE_HEAD,
66 - * stuff HEAD version in stage #2,
67 - * stuff MERGE_HEAD version in stage #3.
68 - */
69 - ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
70 - ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
71 -
72 - if (!ce_2 || !ce_3) {
73 - ret = -1;
74 - goto free_return;
75 - }
76 - if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
77 - ce_2->ce_mode == ce_3->ce_mode) {
78 - fprintf(stderr, "%s: identical in both, skipping.\n",
79 - path);
80 - goto free_return;
81 - }
82 -
83 - remove_file_from_cache(path);
84 - if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
85 - error("%s: cannot add our version to the index.", path);
86 - ret = -1;
87 - goto free_return;
88 - }
89 - if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
90 - return 0;
91 - error("%s: cannot add their version to the index.", path);
92 - ret = -1;
93 - free_return:
94 - free(ce_2);
95 - free(ce_3);
96 - return ret;
97 -}
98 -
99 -static void read_head_pointers(void)
100 -{
101 - if (read_ref(git_path("HEAD"), head_sha1))
102 - die("Cannot read HEAD -- no initial commit yet?");
103 - if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
104 - fprintf(stderr, "Not in the middle of a merge.\n");
105 - exit(0);
106 - }
107 -}
108 -
109 -int main(int ac, char **av)
110 -{
111 - int i;
112 - int err = 0;
113 - int newfd;
114 -
115 - if (ac < 2)
116 - usage(unresolve_usage);
117 -
118 - git_config(git_default_config);
119 -
120 - /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
121 - * are not doing a merge, so exit with success status.
122 - */
123 - read_head_pointers();
124 -
125 - /* Otherwise we would need to update the cache. */
126 - newfd= hold_index_file_for_update(&cache_file, get_index_file());
127 - if (newfd < 0)
128 - die("unable to create new cachefile");
129 -
130 - if (read_cache() < 0)
131 - die("cache corrupted");
132 -
133 - for (i = 1; i < ac; i++) {
134 - char *arg = av[i];
135 - err |= unresolve_one(arg);
136 - }
137 - if (err)
138 - die("Error encountered; index not updated.");
139 -
140 - if (active_cache_changed) {
141 - if (write_cache(newfd, active_cache, active_nr) ||
142 - commit_index_file(&cache_file))
143 - die("Unable to write new cachefile");
144 - }
145 - return 0;
146 -}
update-index.c
+127
@@ -6,6 +6,7 @@
6 #include "cache.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 +#include "tree-walk.h"
10
11 /*
12 * Default to not allowing changes to the list of files. The
@@ -471,6 +472,124 @@ static void read_index_info(int line_termination)
472 static const char update_index_usage[] =
473 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
474
475 +static unsigned char head_sha1[20];
476 +static unsigned char merge_head_sha1[20];
477 +
478 +static struct cache_entry *read_one_ent(const char *which,
479 + unsigned char *ent, const char *path,
480 + int namelen, int stage)
481 +{
482 + unsigned mode;
483 + unsigned char sha1[20];
484 + int size;
485 + struct cache_entry *ce;
486 +
487 + if (get_tree_entry(ent, path, sha1, &mode)) {
488 + error("%s: not in %s branch.", path, which);
489 + return NULL;
490 + }
491 + if (mode == S_IFDIR) {
492 + error("%s: not a blob in %s branch.", path, which);
493 + return NULL;
494 + }
495 + size = cache_entry_size(namelen);
496 + ce = xcalloc(1, size);
497 +
498 + memcpy(ce->sha1, sha1, 20);
499 + memcpy(ce->name, path, namelen);
500 + ce->ce_flags = create_ce_flags(namelen, stage);
501 + ce->ce_mode = create_ce_mode(mode);
502 + return ce;
503 +}
504 +
505 +static int unresolve_one(const char *path)
506 +{
507 + int namelen = strlen(path);
508 + int pos;
509 + int ret = 0;
510 + struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
511 +
512 + /* See if there is such entry in the index. */
513 + pos = cache_name_pos(path, namelen);
514 + if (pos < 0) {
515 + /* If there isn't, either it is unmerged, or
516 + * resolved as "removed" by mistake. We do not
517 + * want to do anything in the former case.
518 + */
519 + pos = -pos-1;
520 + if (pos < active_nr) {
521 + struct cache_entry *ce = active_cache[pos];
522 + if (ce_namelen(ce) == namelen &&
523 + !memcmp(ce->name, path, namelen)) {
524 + fprintf(stderr,
525 + "%s: skipping still unmerged path.\n",
526 + path);
527 + goto free_return;
528 + }
529 + }
530 + }
531 +
532 + /* Grab blobs from given path from HEAD and MERGE_HEAD,
533 + * stuff HEAD version in stage #2,
534 + * stuff MERGE_HEAD version in stage #3.
535 + */
536 + ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
537 + ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
538 +
539 + if (!ce_2 || !ce_3) {
540 + ret = -1;
541 + goto free_return;
542 + }
543 + if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
544 + ce_2->ce_mode == ce_3->ce_mode) {
545 + fprintf(stderr, "%s: identical in both, skipping.\n",
546 + path);
547 + goto free_return;
548 + }
549 +
550 + remove_file_from_cache(path);
551 + if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
552 + error("%s: cannot add our version to the index.", path);
553 + ret = -1;
554 + goto free_return;
555 + }
556 + if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
557 + return 0;
558 + error("%s: cannot add their version to the index.", path);
559 + ret = -1;
560 + free_return:
561 + free(ce_2);
562 + free(ce_3);
563 + return ret;
564 +}
565 +
566 +static void read_head_pointers(void)
567 +{
568 + if (read_ref(git_path("HEAD"), head_sha1))
569 + die("No HEAD -- no initial commit yet?\n");
570 + if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
571 + fprintf(stderr, "Not in the middle of a merge.\n");
572 + exit(0);
573 + }
574 +}
575 +
576 +static int do_unresolve(int ac, const char **av)
577 +{
578 + int i;
579 + int err = 0;
580 +
581 + /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
582 + * are not doing a merge, so exit with success status.
583 + */
584 + read_head_pointers();
585 +
586 + for (i = 1; i < ac; i++) {
587 + const char *arg = av[i];
588 + err |= unresolve_one(arg);
589 + }
590 + return err;
591 +}
592 +
593 int main(int argc, const char **argv)
594 {
595 int i, newfd, entries, has_errors = 0, line_termination = '\n';
@@ -581,6 +700,12 @@ int main(int argc, const char **argv)
700 read_index_info(line_termination);
701 break;
702 }
703 + if (!strcmp(path, "--unresolve")) {
704 + has_errors = do_unresolve(argc - i, argv + i);
705 + if (has_errors)
706 + active_cache_changed = 0;
707 + goto finish;
708 + }
709 if (!strcmp(path, "--ignore-missing")) {
710 not_new = 1;
711 continue;
@@ -612,6 +737,8 @@ int main(int argc, const char **argv)
737 free(path_name);
738 }
739 }
740 +
741 + finish:
742 if (active_cache_changed) {
743 if (write_cache(newfd, active_cache, active_nr) ||
744 commit_index_file(&cache_file))