From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 2/2] reset: support "--mixed --intent-to-add" mode
Date: Tue, 4 Feb 2014 09:20:09 +0700 [thread overview]
Message-ID: <1391480409-25727-2-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1391480409-25727-1-git-send-email-pclouds@gmail.com>
When --mixed is used, entries could be removed from index if the
target ref does not have them. When "reset" is used in preparation for
commit spliting (in a dirty worktree), it could be hard to track what
files to be added back. The new option --intent-to-add simplifies it
by marking all removed files intent-to-add.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Documentation/git-reset.txt | 5 ++++-
builtin/reset.c | 19 +++++++++++++++++--
cache.h | 1 +
read-cache.c | 9 +++++++++
t/t7102-reset.sh | 9 +++++++++
5 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-reset.txt b/Documentation/git-reset.txt
index f445cb3..a077ba0 100644
--- a/Documentation/git-reset.txt
+++ b/Documentation/git-reset.txt
@@ -10,7 +10,7 @@ SYNOPSIS
[verse]
'git reset' [-q] [<tree-ish>] [--] <paths>...
'git reset' (--patch | -p) [<tree-ish>] [--] [<paths>...]
-'git reset' [--soft | --mixed | --hard | --merge | --keep] [-q] [<commit>]
+'git reset' [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
DESCRIPTION
-----------
@@ -60,6 +60,9 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
Resets the index but not the working tree (i.e., the changed files
are preserved but not marked for commit) and reports what has not
been updated. This is the default action.
++
+If `-N` is specified, removed paths are marked as intent-to-add (see
+linkgit:git-add[1]).
--hard::
Resets the index and working tree. Any changes to tracked files in the
diff --git a/builtin/reset.c b/builtin/reset.c
index 6004803..f34eab4 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -116,6 +116,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
struct diff_options *opt, void *data)
{
int i;
+ int *intent_to_add = data;
for (i = 0; i < q->nr; i++) {
struct diff_filespec *one = q->queue[i]->one;
@@ -128,13 +129,20 @@ static void update_index_from_diff(struct diff_queue_struct *q,
one->path);
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
ADD_CACHE_OK_TO_REPLACE);
+ } else if (*intent_to_add) {
+ int pos = cache_name_pos(one->path, strlen(one->path));
+ if (pos < 0)
+ die(_("%s does not exist in index"),
+ one->path);
+ set_intent_to_add(&the_index, active_cache[pos]);
} else
remove_file_from_cache(one->path);
}
}
static int read_from_tree(const struct pathspec *pathspec,
- unsigned char *tree_sha1)
+ unsigned char *tree_sha1,
+ int intent_to_add)
{
struct diff_options opt;
@@ -142,6 +150,7 @@ static int read_from_tree(const struct pathspec *pathspec,
copy_pathspec(&opt.pathspec, pathspec);
opt.output_format = DIFF_FORMAT_CALLBACK;
opt.format_callback = update_index_from_diff;
+ opt.format_callback_data = &intent_to_add;
if (do_diff_cache(tree_sha1, &opt))
return 1;
@@ -258,6 +267,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
const char *rev;
unsigned char sha1[20];
struct pathspec pathspec;
+ int intent_to_add = 0;
const struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_SET_INT(0, "mixed", &reset_type,
@@ -270,6 +280,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
OPT_SET_INT(0, "keep", &reset_type,
N_("reset HEAD but keep local changes"), KEEP),
OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
+ OPT_BOOL('N', "intent-to-add", &intent_to_add,
+ N_("record only the fact that removed paths will be added later")),
OPT_END()
};
@@ -327,6 +339,9 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
die(_("%s reset is not allowed in a bare repository"),
_(reset_type_names[reset_type]));
+ if (intent_to_add && reset_type != MIXED)
+ die(_("-N can only be used with --mixed"));
+
/* Soft reset does not touch the index file nor the working tree
* at all, but requires them in a good order. Other resets reset
* the index file to the tree object we are switching to. */
@@ -338,7 +353,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
int newfd = hold_locked_index(lock, 1);
if (reset_type == MIXED) {
int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
- if (read_from_tree(&pathspec, sha1))
+ if (read_from_tree(&pathspec, sha1, intent_to_add))
return 1;
refresh_index(&the_index, flags, NULL, NULL,
_("Unstaged changes after reset:"));
diff --git a/cache.h b/cache.h
index dc040fb..9d5e09e 100644
--- a/cache.h
+++ b/cache.h
@@ -479,6 +479,7 @@ extern void rename_index_entry_at(struct index_state *, int pos, const char *new
extern int remove_index_entry_at(struct index_state *, int pos);
extern void remove_marked_cache_entries(struct index_state *istate);
extern int remove_file_from_index(struct index_state *, const char *path);
+extern int set_intent_to_add(struct index_state *, struct cache_entry *);
#define ADD_CACHE_VERBOSE 1
#define ADD_CACHE_PRETEND 2
#define ADD_CACHE_IGNORE_ERRORS 4
diff --git a/read-cache.c b/read-cache.c
index 33dd676..dc160a3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -587,6 +587,15 @@ static void record_intent_to_add(struct cache_entry *ce)
hashcpy(ce->sha1, sha1);
}
+int set_intent_to_add(struct index_state *istate, struct cache_entry *ce)
+{
+ record_intent_to_add(ce);
+ ce->ce_flags |= CE_INTENT_TO_ADD;
+ cache_tree_invalidate_path(istate->cache_tree, ce->name);
+ istate->cache_changed = 1;
+ return 0;
+}
+
int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
{
int size, namelen, was_same;
diff --git a/t/t7102-reset.sh b/t/t7102-reset.sh
index 8d4b50d..642920a 100755
--- a/t/t7102-reset.sh
+++ b/t/t7102-reset.sh
@@ -535,4 +535,13 @@ test_expect_success 'reset with paths accepts tree' '
git diff HEAD --exit-code
'
+test_expect_success 'reset -N keeps removed files as intent-to-add' '
+ echo new-file >new-file &&
+ git add new-file &&
+ git reset -N HEAD &&
+ git diff --name-only >actual &&
+ echo new-file >expect &&
+ test_cmp expect actual
+'
+
test_done
--
1.8.5.2.240.g8478abd
next prev parent reply other threads:[~2014-02-04 2:20 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-01 10:48 splitting a commit that adds new files Duy Nguyen
2014-02-02 18:15 ` Junio C Hamano
2014-02-02 23:11 ` Jeff King
2014-02-03 18:11 ` Junio C Hamano
2014-02-04 0:54 ` Duy Nguyen
2014-02-04 2:20 ` [PATCH 1/2] t7101, t7014: rename test files to indicate what that file is for Nguyễn Thái Ngọc Duy
2014-02-04 2:20 ` Nguyễn Thái Ngọc Duy [this message]
2014-02-04 19:09 ` [PATCH 2/2] reset: support "--mixed --intent-to-add" mode Junio C Hamano
2014-02-04 22:25 ` Junio C Hamano
2014-02-05 0:27 ` Duy Nguyen
2014-02-05 17:16 ` Junio C Hamano
2014-02-05 18:25 ` Junio C Hamano
2014-02-05 23:48 ` Duy Nguyen
2014-02-06 0:08 ` Junio C Hamano
2014-02-06 0:43 ` Junio C Hamano
2014-02-04 16:05 ` [PATCH 1/2] t7101, t7014: rename test files to indicate what that file is for Jonathan Nieder
2014-02-06 1:58 ` Duy Nguyen
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=1391480409-25727-2-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--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).