6 #include "unpack-trees.h"
8 struct tree_entry_list
{
9 struct tree_entry_list
*next
;
10 unsigned directory
: 1;
11 unsigned executable
: 1;
15 const unsigned char *sha1
;
18 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
20 struct tree_desc desc
;
21 struct name_entry one
;
22 struct tree_entry_list
*ret
= NULL
;
23 struct tree_entry_list
**list_p
= &ret
;
25 desc
.buf
= tree
->buffer
;
26 desc
.size
= tree
->size
;
28 while (tree_entry(&desc
, &one
)) {
29 struct tree_entry_list
*entry
;
31 entry
= xmalloc(sizeof(struct tree_entry_list
));
32 entry
->name
= one
.path
;
33 entry
->sha1
= one
.sha1
;
34 entry
->mode
= one
.mode
;
35 entry
->directory
= S_ISDIR(one
.mode
) != 0;
36 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
37 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
41 list_p
= &entry
->next
;
46 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
48 int len1
= strlen(name1
);
49 int len2
= strlen(name2
);
50 int len
= len1
< len2
? len1
: len2
;
51 int ret
= memcmp(name1
, name2
, len
);
61 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
67 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
68 const char *base
, struct unpack_trees_options
*o
,
70 struct tree_entry_list
*df_conflict_list
)
72 int baselen
= strlen(base
);
73 int src_size
= len
+ 1;
80 struct tree_entry_list
**subposns
;
81 struct cache_entry
**src
;
87 /* Find the first name in the input. */
93 if (o
->merge
&& *indpos
< active_nr
) {
94 /* This is a bit tricky: */
95 /* If the index has a subdirectory (with
96 * contents) as the first name, it'll get a
97 * filename like "foo/bar". But that's after
98 * "foo", so the entry in trees will get
99 * handled first, at which point we'll go into
100 * "foo", and deal with "bar" from the index,
101 * because the base will be "foo/". The only
102 * way we can actually have "foo/bar" first of
103 * all the things is if the trees don't
104 * contain "foo" at all, in which case we'll
105 * handle "foo/bar" without going into the
106 * directory, but that's fine (and will return
107 * an error anyway, with the added unknown
111 cache_name
= active_cache
[*indpos
]->name
;
112 if (strlen(cache_name
) > baselen
&&
113 !memcmp(cache_name
, base
, baselen
)) {
114 cache_name
+= baselen
;
123 printf("index %s\n", first
);
125 for (i
= 0; i
< len
; i
++) {
126 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
129 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
131 if (!first
|| entcmp(first
, firstdir
,
133 posns
[i
]->directory
) > 0) {
134 first
= posns
[i
]->name
;
135 firstdir
= posns
[i
]->directory
;
138 /* No name means we're done */
142 pathlen
= strlen(first
);
143 ce_size
= cache_entry_size(baselen
+ pathlen
);
145 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
147 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
149 if (cache_name
&& !strcmp(cache_name
, first
)) {
151 src
[0] = active_cache
[*indpos
];
152 remove_cache_entry_at(*indpos
);
155 for (i
= 0; i
< len
; i
++) {
156 struct cache_entry
*ce
;
159 (posns
[i
] != df_conflict_list
&&
160 strcmp(first
, posns
[i
]->name
))) {
164 if (posns
[i
] == df_conflict_list
) {
165 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
169 if (posns
[i
]->directory
) {
170 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
173 subposns
[i
] = create_tree_entry_list(tree
);
174 posns
[i
] = posns
[i
]->next
;
175 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
181 else if (i
+ 1 < o
->head_idx
)
183 else if (i
+ 1 > o
->head_idx
)
188 ce
= xcalloc(1, ce_size
);
189 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
190 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
192 memcpy(ce
->name
, base
, baselen
);
193 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
197 memcpy(ce
->sha1
, posns
[i
]->sha1
, 20);
198 src
[i
+ o
->merge
] = ce
;
199 subposns
[i
] = df_conflict_list
;
200 posns
[i
] = posns
[i
]->next
;
207 printf("%s:\n", first
);
208 for (i
= 0; i
< src_size
; i
++) {
211 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
219 printf("Added %d entries\n", ret
);
223 for (i
= 0; i
< src_size
; i
++) {
225 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
231 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
232 memcpy(newbase
, base
, baselen
);
233 memcpy(newbase
+ baselen
, first
, pathlen
);
234 newbase
[baselen
+ pathlen
] = '/';
235 newbase
[baselen
+ pathlen
+ 1] = '\0';
236 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
237 indpos
, df_conflict_list
))
246 /* Unlink the last component and attempt to remove leading
247 * directories, in case this unlink is the removal of the
248 * last entry in the directory -- empty directories are removed.
250 static void unlink_entry(char *name
)
259 cp
= strrchr(name
, '/');
266 status
= rmdir(name
);
275 static volatile int progress_update
= 0;
277 static void progress_interval(int signum
)
282 static void setup_progress_signal(void)
287 memset(&sa
, 0, sizeof(sa
));
288 sa
.sa_handler
= progress_interval
;
289 sigemptyset(&sa
.sa_mask
);
290 sa
.sa_flags
= SA_RESTART
;
291 sigaction(SIGALRM
, &sa
, NULL
);
293 v
.it_interval
.tv_sec
= 1;
294 v
.it_interval
.tv_usec
= 0;
295 v
.it_value
= v
.it_interval
;
296 setitimer(ITIMER_REAL
, &v
, NULL
);
299 static struct checkout state
;
300 static void check_updates(struct cache_entry
**src
, int nr
,
301 struct unpack_trees_options
*o
)
303 unsigned short mask
= htons(CE_UPDATE
);
304 unsigned last_percent
= 200, cnt
= 0, total
= 0;
306 if (o
->update
&& o
->verbose_update
) {
307 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
308 struct cache_entry
*ce
= src
[cnt
];
309 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
313 /* Don't bother doing this for very small updates */
318 fprintf(stderr
, "Checking files out...\n");
319 setup_progress_signal();
326 struct cache_entry
*ce
= *src
++;
329 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
332 percent
= (cnt
* 100) / total
;
333 if (percent
!= last_percent
||
335 fprintf(stderr
, "%4u%% (%u/%u) done\r",
336 percent
, cnt
, total
);
337 last_percent
= percent
;
344 unlink_entry(ce
->name
);
347 if (ce
->ce_flags
& mask
) {
348 ce
->ce_flags
&= ~mask
;
350 checkout_entry(ce
, &state
, NULL
);
354 signal(SIGALRM
, SIG_IGN
);
359 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
362 unsigned len
= object_list_length(trees
);
363 struct tree_entry_list
**posns
;
365 struct object_list
*posn
= trees
;
366 struct tree_entry_list df_conflict_list
;
367 struct cache_entry df_conflict_entry
;
369 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
370 df_conflict_list
.next
= &df_conflict_list
;
374 state
.refresh_cache
= 1;
377 o
->df_conflict_entry
= &df_conflict_entry
;
380 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
381 for (i
= 0; i
< len
; i
++) {
382 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
385 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
386 o
, &indpos
, &df_conflict_list
))
390 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
391 die("Merge requires file-level merging");
393 check_updates(active_cache
, active_nr
, o
);