@matheusbsbs / matheus-bagliano / commits / 83e77a25dc
Fork 0

update-index --again

After running 'git-update-index' for some paths, you may want to do the update on the same set of paths again. The new flag --again checks the paths whose index entries are are different from the HEAD commit and updates them from the working tree contents. This was brought up by Carl Worth on #git. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 5, 2006 at 17:40 UTC 83e77a25dc194933c0fb7908ab6d9fb84a5045e2
3 files changed +131 -4
Documentation/git-update-index.txt
+5 -1
@@ -15,7 +15,7 @@ SYNOPSIS
15 [--cacheinfo <mode> <object> <file>]\*
16 [--chmod=(+|-)x]
17 [--assume-unchanged | --no-assume-unchanged]
18 - [--really-refresh] [--unresolve]
18 + [--really-refresh] [--unresolve] [--again]
19 [--info-only] [--index-info]
20 [-z] [--stdin]
21 [--verbose]
@@ -80,6 +80,10 @@ OPTIONS
80 filesystem that has very slow lstat(2) system call
81 (e.g. cifs).
82
83 +--again::
84 + Runs `git-update-index` itself on the paths whose index
85 + entries are different from those from the `HEAD` commit.
86 +
87 --unresolve::
88 Restores the 'unmerged' or 'needs updating' state of a
89 file during a merge if it was cleared by accident.
t/t2101-update-index-reupdate.sh new
+73
@@ -0,0 +1,73 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Junio C Hamano
4 +#
5 +
6 +test_description='git-update-index --again test.
7 +'
8 +
9 +. ./test-lib.sh
10 +
11 +test_expect_success 'update-index --add' \
12 + 'echo hello world >file1 &&
13 + echo goodbye people >file2 &&
14 + git-update-index --add file1 file2 &&
15 + git-ls-files -s >current &&
16 + cmp current - <<\EOF
17 +100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0 file1
18 +100644 9db8893856a8a02eaa73470054b7c1c5a7c82e47 0 file2
19 +EOF'
20 +
21 +test_expect_success 'update-index --again' \
22 + 'rm -f file1 &&
23 + echo hello everybody >file2 &&
24 + if git-update-index --again
25 + then
26 + echo should have refused to remove file1
27 + exit 1
28 + else
29 + echo happy - failed as expected
30 + fi &&
31 + git-ls-files -s >current &&
32 + cmp current - <<\EOF
33 +100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0 file1
34 +100644 9db8893856a8a02eaa73470054b7c1c5a7c82e47 0 file2
35 +EOF'
36 +
37 +test_expect_success 'update-index --remove --again' \
38 + 'git-update-index --remove --again &&
39 + git-ls-files -s >current &&
40 + cmp current - <<\EOF
41 +100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0 file2
42 +EOF'
43 +
44 +test_expect_success 'first commit' 'git-commit -m initial'
45 +
46 +test_expect_success 'update-index again' \
47 + 'mkdir -p dir1 &&
48 + echo hello world >dir1/file3 &&
49 + echo goodbye people >file2 &&
50 + git-update-index --add file2 dir1/file3 &&
51 + echo hello everybody >file2
52 + echo happy >dir1/file3 &&
53 + git-update-index --again &&
54 + git-ls-files -s >current &&
55 + cmp current - <<\EOF
56 +100644 53ab446c3f4e42ce9bb728a0ccb283a101be4979 0 dir1/file3
57 +100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0 file2
58 +EOF'
59 +
60 +test_expect_success 'update-index --update from subdir' \
61 + 'echo not so happy >file2 &&
62 + cd dir1 &&
63 + cat ../file2 >file3 &&
64 + git-update-index --again &&
65 + cd .. &&
66 + git-ls-files -s >current &&
67 + cmp current - <<\EOF
68 +100644 d7fb3f695f06c759dbf3ab00046e7cc2da22d10f 0 dir1/file3
69 +100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0 file2
70 +EOF'
71 +
72 +test_done
73 +
update-index.c
+53 -3
@@ -476,7 +476,7 @@ static void read_index_info(int line_termination)
476 }
477
478 static const char update_index_usage[] =
479 -"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--ignore-missing] [-z] [--verbose] [--] <file>...";
479 +"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
480
481 static unsigned char head_sha1[20];
482 static unsigned char merge_head_sha1[20];
@@ -491,11 +491,13 @@ static struct cache_entry *read_one_ent(const char *which,
491 struct cache_entry *ce;
492
493 if (get_tree_entry(ent, path, sha1, &mode)) {
494 - error("%s: not in %s branch.", path, which);
494 + if (which)
495 + error("%s: not in %s branch.", path, which);
496 return NULL;
497 }
498 if (mode == S_IFDIR) {
498 - error("%s: not a blob in %s branch.", path, which);
499 + if (which)
500 + error("%s: not a blob in %s branch.", path, which);
501 return NULL;
502 }
503 size = cache_entry_size(namelen);
@@ -600,6 +602,47 @@ static int do_unresolve(int ac, const char **av,
602 return err;
603 }
604
605 +static int do_reupdate(int ac, const char **av,
606 + const char *prefix, int prefix_length)
607 +{
608 + /* Read HEAD and run update-index on paths that are
609 + * merged and already different between index and HEAD.
610 + */
611 + int pos;
612 + int has_head = 1;
613 +
614 + if (read_ref(git_path("HEAD"), head_sha1))
615 + /* If there is no HEAD, that means it is an initial
616 + * commit. Update everything in the index.
617 + */
618 + has_head = 0;
619 + redo:
620 + for (pos = 0; pos < active_nr; pos++) {
621 + struct cache_entry *ce = active_cache[pos];
622 + struct cache_entry *old = NULL;
623 + int save_nr;
624 + if (ce_stage(ce))
625 + continue;
626 + if (has_head)
627 + old = read_one_ent(NULL, head_sha1,
628 + ce->name, ce_namelen(ce), 0);
629 + if (old && ce->ce_mode == old->ce_mode &&
630 + !memcmp(ce->sha1, old->sha1, 20)) {
631 + free(old);
632 + continue; /* unchanged */
633 + }
634 + /* Be careful. The working tree may not have the
635 + * path anymore, in which case, under 'allow_remove',
636 + * or worse yet 'allow_replace', active_nr may decrease.
637 + */
638 + save_nr = active_nr;
639 + update_one(ce->name + prefix_length, prefix, prefix_length);
640 + if (save_nr != active_nr)
641 + goto redo;
642 + }
643 + return 0;
644 +}
645 +
646 int main(int argc, const char **argv)
647 {
648 int i, newfd, entries, has_errors = 0, line_termination = '\n';
@@ -717,6 +760,13 @@ int main(int argc, const char **argv)
760 active_cache_changed = 0;
761 goto finish;
762 }
763 + if (!strcmp(path, "--again")) {
764 + has_errors = do_reupdate(argc - i, argv + i,
765 + prefix, prefix_length);
766 + if (has_errors)
767 + active_cache_changed = 0;
768 + goto finish;
769 + }
770 if (!strcmp(path, "--ignore-missing")) {
771 not_new = 1;
772 continue;