Skip to content

Commit af2a651

Browse files
committed
checkout_entry(): clarify the use of topath[] parameter
The said function has this signature: extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath); At first glance, it might appear that the caller of checkout_entry() can specify to which path the contents are written out by the last parameter, and it is tempting to add "const" in front of its type. In reality, however, topath[] is to point at a buffer to store the temporary path generated by the callchain originating from this function, and the temporary path is always short, much shorter than the buffer prepared by its only caller in builtin/checkout-index.c. Document the code a bit to clarify so that future callers know how to use the function better. Noticed-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd356f6 commit af2a651

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

builtin/checkout-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static int line_termination = '\n';
1515
static int checkout_stage; /* default to checkout stage0 */
1616
static int to_tempfile;
17-
static char topath[4][PATH_MAX + 1];
17+
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
1818

1919
static struct checkout state;
2020

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ struct checkout {
975975
refresh_cache:1;
976976
};
977977

978+
#define TEMPORARY_FILENAME_LENGTH 25
978979
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
979980

980981
struct cache_def {

entry.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
234234
return lstat(path, st);
235235
}
236236

237+
/*
238+
* Write the contents from ce out to the working tree.
239+
*
240+
* When topath[] is not NULL, instead of writing to the working tree
241+
* file named by ce, a temporary file is created by this function and
242+
* its name is returned in topath[], which must be able to hold at
243+
* least TEMPORARY_FILENAME_LENGTH bytes long.
244+
*/
237245
int checkout_entry(struct cache_entry *ce,
238246
const struct checkout *state, char *topath)
239247
{

0 commit comments

Comments
 (0)