git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Daniel Barkalow <barkalow@iabervon.org>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: [PATCH 3/3] git-add --intent-to-add (-N)
Date: Thu, 21 Aug 2008 01:44:53 -0700	[thread overview]
Message-ID: <7v3akykb96.fsf_-_@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <7viqtukbec.fsf@gitster.siamese.dyndns.org> (Junio C. Hamano's message of "Thu, 21 Aug 2008 01:41:47 -0700")

This adds "--intent-to-add" option to "git add".  This is to let the
system know that you will tell it the final contents to be staged later,
iow, just be aware of the presense of the path with the type of the blob
for now.

With this sequence:

    $ git reset --hard
    $ edit newfile
    $ git add -N newfile
    $ edit newfile oldfile
    $ git diff

the diff will show all changes relative to the current commit.  Then you
can do:

    $ git commit -a ;# commit everything

or

    $ git commit oldfile ;# only oldfile, newfile not yet added

to pretend you are working with an index-free system like CVS.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-add.c         |    4 +++-
 cache.h               |    2 ++
 read-cache.c          |   30 ++++++++++++++++++++----------
 t/t2203-add-intent.sh |   36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 11 deletions(-)
 create mode 100755 t/t2203-add-intent.sh

diff --git a/builtin-add.c b/builtin-add.c
index fc3f96e..a08d50d 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -191,7 +191,7 @@ static const char ignore_error[] =
 "The following paths are ignored by one of your .gitignore files:\n";
 
 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
-static int ignore_add_errors, addremove;
+static int ignore_add_errors, addremove, intent_to_add;
 
 static struct option builtin_add_options[] = {
 	OPT__DRY_RUN(&show_only),
@@ -201,6 +201,7 @@ static struct option builtin_add_options[] = {
 	OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
 	OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
 	OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
+	OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
 	OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
 	OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
 	OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
@@ -271,6 +272,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
 	flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 		 (show_only ? ADD_CACHE_PRETEND : 0) |
+		 (intent_to_add ? ADD_CACHE_INTENT : 0) |
 		 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
 
 	if (require_pathspec && argc == 0) {
diff --git a/cache.h b/cache.h
index 68ce6e6..5948bcc 100644
--- a/cache.h
+++ b/cache.h
@@ -369,6 +369,7 @@ extern int index_name_pos(const struct index_state *, const char *name, int name
 #define ADD_CACHE_OK_TO_REPLACE 2	/* Ok to replace file/directory */
 #define ADD_CACHE_SKIP_DFCHECK 4	/* Ok to skip DF conflict checks */
 #define ADD_CACHE_JUST_APPEND 8		/* Append only; tree.c::read_tree() */
+#define ADD_CACHE_NEW_ONLY 16		/* Do not replace existing ones */
 extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
 extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
 extern void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
@@ -377,6 +378,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
 #define ADD_CACHE_VERBOSE 1
 #define ADD_CACHE_PRETEND 2
 #define ADD_CACHE_IGNORE_ERRORS	4
+#define ADD_CACHE_INTENT 8
 extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
 extern int add_file_to_index(struct index_state *, const char *path, int flags);
 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
diff --git a/read-cache.c b/read-cache.c
index 2c03ec3..1592045 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -154,13 +154,13 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
 	return 0;
 }
 
+static const unsigned char empty_blob_sha1[20] = {
+	0xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b,
+	0x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91
+};
+
 static int is_empty_blob_sha1(const unsigned char *sha1)
 {
-	static const unsigned char empty_blob_sha1[20] = {
-		0xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b,
-		0x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91
-	};
-
 	return !hashcmp(sha1, empty_blob_sha1);
 }
 
@@ -514,6 +514,9 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 	unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY;
 	int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
 	int pretend = flags & ADD_CACHE_PRETEND;
+	int intent_only = flags & ADD_CACHE_INTENT;
+	int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
+			  (intent_only ? ADD_CACHE_NEW_ONLY : 0));
 
 	if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
 		return error("%s: can only add regular files, symbolic links or git-directories", path);
@@ -527,7 +530,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 	ce = xcalloc(1, size);
 	memcpy(ce->name, path, namelen);
 	ce->ce_flags = namelen;
-	fill_stat_cache_info(ce, st);
+	if (!intent_only)
+		fill_stat_cache_info(ce, st);
 
 	if (trust_executable_bit && has_symlinks)
 		ce->ce_mode = create_ce_mode(st_mode);
@@ -550,8 +554,12 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 		alias->ce_flags |= CE_ADDED;
 		return 0;
 	}
-	if (index_path(ce->sha1, path, st, 1))
-		return error("unable to index file %s", path);
+	if (!intent_only) {
+		if (index_path(ce->sha1, path, st, 1))
+			return error("unable to index file %s", path);
+	} else
+		hashcpy(ce->sha1, empty_blob_sha1);
+
 	if (ignore_case && alias && different_name(ce, alias))
 		ce = create_alias_ce(ce, alias);
 	ce->ce_flags |= CE_ADDED;
@@ -564,7 +572,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 
 	if (pretend)
 		;
-	else if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
+	else if (add_index_entry(istate, ce, add_option))
 		return error("unable to add %s to index",path);
 	if (verbose && !was_same)
 		printf("add '%s'\n", path);
@@ -843,13 +851,15 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
 	int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
 	int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
 	int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
+	int new_only = option & ADD_CACHE_NEW_ONLY;
 
 	cache_tree_invalidate_path(istate->cache_tree, ce->name);
 	pos = index_name_pos(istate, ce->name, ce->ce_flags);
 
 	/* existing match? Just replace it. */
 	if (pos >= 0) {
-		replace_index_entry(istate, pos, ce);
+		if (!new_only)
+			replace_index_entry(istate, pos, ce);
 		return 0;
 	}
 	pos = -pos-1;
diff --git a/t/t2203-add-intent.sh b/t/t2203-add-intent.sh
new file mode 100755
index 0000000..d4de35e
--- /dev/null
+++ b/t/t2203-add-intent.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='Intent to add'
+
+. ./test-lib.sh
+
+test_expect_success 'intent to add' '
+	echo hello >file &&
+	echo hello >elif &&
+	git add -N file &&
+	git add elif
+'
+
+test_expect_success 'check result of "add -N"' '
+	git ls-files -s file >actual &&
+	empty=$(git hash-object --stdin </dev/null) &&
+	echo "100644 $empty 0	file" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'intent to add is just an ordinary empty blob' '
+	git add -u &&
+	git ls-files -s file >actual &&
+	git ls-files -s elif | sed -e "s/elif/file/" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'intent to add does not clobber existing paths' '
+	git add -N file elif &&
+	empty=$(git hash-object --stdin </dev/null) &&
+	git ls-files -s >actual &&
+	! grep "$empty" actual
+'
+
+test_done
+
-- 
1.6.0.51.g078ae

  parent reply	other threads:[~2008-08-21  8:46 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-18  0:02 Call Me Gitless Trans
2008-08-18  0:28 ` Benjamin Sergeant
2008-08-18  0:40 ` Martin Langhoff
2008-08-18  8:50 ` Pascal Obry
2008-08-18 16:43 ` Jon Loeliger
2008-08-18 19:22 ` Daniel Barkalow
2008-08-18 20:17   ` Marcus Griep
2008-08-18 20:20   ` Junio C Hamano
2008-08-18 21:31     ` Daniel Barkalow
2008-08-18 22:30       ` Junio C Hamano
2008-08-18 23:12         ` Daniel Barkalow
2008-08-19  3:22           ` Junio C Hamano
2008-08-19  3:55             ` Marcus Griep
2008-08-19  6:47               ` Junio C Hamano
2008-08-19  7:02                 ` Junio C Hamano
2008-08-20  8:00                   ` [PATCH v2] diff: vary default prefix depending on what are compared Junio C Hamano
2008-08-20  9:06                     ` Jakub Narebski
2008-08-19  6:28             ` Call Me Gitless Stephen R. van den Berg
2008-08-19 11:42             ` Jakub Narebski
2008-08-19 18:18               ` Junio C Hamano
2008-08-19 17:52             ` Jeff King
2008-08-19 18:39               ` Daniel Barkalow
2008-08-19 18:45                 ` Jeff King
2008-08-19 18:57                   ` Daniel Barkalow
2008-08-19 19:01                     ` Jeff King
2008-08-19 19:42                       ` Daniel Barkalow
2008-08-19 20:33                         ` Petr Baudis
2008-08-19 21:49                           ` Daniel Barkalow
2008-08-19 19:43               ` Junio C Hamano
2008-08-19  7:25       ` Junio C Hamano
2008-08-19 19:22         ` Daniel Barkalow
2008-08-21  3:40       ` Sverre Hvammen Johansen
2008-08-21  8:41       ` Junio C Hamano
2008-08-21  8:43         ` [PATCH 1/3] sha1_object_info(): pay attention to cached objects Junio C Hamano
2008-08-21  8:43         ` [PATCH 2/3] cached_object: learn empty blob Junio C Hamano
2008-08-21  8:44         ` Junio C Hamano [this message]
2008-08-21 14:23           ` [PATCH 3/3] git-add --intent-to-add (-N) Paolo Bonzini
2008-08-21 21:14           ` Jonathan Nieder
2008-08-22  4:10             ` Jonathan Nieder
2008-08-22  4:34               ` Daniel Barkalow
2008-08-22  4:59                 ` Junio C Hamano
2008-08-22  5:32                 ` Jonathan Nieder
2008-08-22  5:59                   ` Junio C Hamano
2008-08-22  6:38                     ` Jonathan Nieder
2008-08-22  7:52                       ` Jonathan Nieder
2008-08-21 13:58         ` Call Me Gitless Daniel Barkalow
2008-08-18 23:24     ` Tarmigan
2008-08-19  0:32       ` Daniel Barkalow
2008-08-19  0:45         ` Tarmigan
2008-08-19  7:53       ` "Peter Valdemar Mørch (Lists)"
2008-08-19  8:01         ` Junio C Hamano
2008-08-19  8:10           ` Imran M Yousuf
2008-08-19  8:26             ` "Peter Valdemar Mørch (Lists)"
2008-08-19  8:53               ` Imran M Yousuf
2008-08-19  8:57           ` Alexander E Genaud
2008-08-19  9:11             ` Matthieu Moy
2008-08-19  9:36               ` Mike Hommey
2008-08-19 10:09               ` Alexander E Genaud
2008-08-19 11:27                 ` Pascal Obry
2008-08-21 14:15                   ` Paolo Bonzini
2008-08-22 19:10                     ` Elijah Newren
2008-08-19 10:16               ` "Peter Valdemar Mørch (Lists)"
2008-08-19 11:31             ` Mark Struberg
2008-08-19 12:04               ` Alexander E Genaud
2008-08-19 18:15                 ` Junio C Hamano
2008-08-19  8:56         ` Teemu Likonen
2008-08-19 13:15 ` Jakub Narebski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7v3akykb96.fsf_-_@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=barkalow@iabervon.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).