Rewrite TGitCache status basic okay
[TortoiseGit.git] / src / Git / GitStatus.h
blob0ad216107a1b72688ded4fa31b59cb834fc7f4b0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 #ifdef _MFC_VER
23 //# include "SVNPrompt.h"
24 #endif
25 #include "TGitPath.h"
27 #pragma warning (push,1)
28 typedef std::basic_string<wchar_t> wide_string;
29 #ifdef UNICODE
30 # define stdstring wide_string
31 #else
32 # define stdstring std::string
33 #endif
34 #pragma warning (pop)
36 #include "TGitPath.h"
37 #include "GitHash.h"
39 typedef enum type_git_wc_status_kind
41 git_wc_status_none,
42 git_wc_status_unversioned,
43 git_wc_status_ignored,
44 git_wc_status_normal,
45 git_wc_status_external,
46 git_wc_status_incomplete,
47 git_wc_status_missing,
48 git_wc_status_deleted,
49 git_wc_status_replaced,
50 git_wc_status_modified,
51 git_wc_status_merged,
52 git_wc_status_added,
53 git_wc_status_conflicted,
54 git_wc_status_obstructed,
55 git_wc_status_unknown,
57 }git_wc_status_kind;
59 typedef enum
61 git_depth_empty,
62 git_depth_infinity,
63 git_depth_unknown,
64 git_depth_files,
65 git_depth_immediates,
66 }git_depth_t;
69 #define GIT_REV_ZERO _T("0000000000000000000000000000000000000000")
70 #define GIT_INVALID_REVNUM _T("")
71 typedef CString git_revnum_t;
72 typedef int git_error_t;
74 typedef struct git_wc_entry_t
76 // url in repository
77 const char *url;
79 TCHAR cmt_rev[41];
80 } git_wc_entry_t;
83 typedef struct git_wc_status2_t
85 /** The status of the entries text. */
86 git_wc_status_kind text_status;
88 /** The status of the entries properties. */
89 git_wc_status_kind prop_status;
91 //git_wc_entry_t *entry;
92 } git_wc_status2;
94 #define MAX_STATUS_STRING_LENGTH 256
97 /////////////////////////////////////////////////////////////////////
98 // WINGIT API (replaced by commandline tool, but defs and data types kept so old code still works)
100 // Flags for wgEnumFiles
101 enum WGENUMFILEFLAGS
103 WGEFF_NoRecurse = (1<<0), // only enumerate files directly in the specified path
104 WGEFF_FullPath = (1<<1), // enumerated filenames are specified with full path (instead of relative to proj root)
105 WGEFF_DirStatusDelta= (1<<2), // include directories, in enumeration, that have a recursive status != WGFS_Normal (may have a slightly better performance than WGEFF_DirStatusAll)
106 WGEFF_DirStatusAll = (1<<3), // include directories, in enumeration, with recursive status
107 WGEFF_EmptyAsNormal = (1<<4), // report sub-directories, with no versioned files, as WGFS_Normal instead of WGFS_Empty
108 WGEFF_SingleFile = (1<<5) // indicates that the status of a single file or dir, specified by pszSubPath, is wanted
111 // File status
112 enum WGFILESTATUS
114 WGFS_Normal,
115 WGFS_Modified,
116 WGFS_Staged,
117 WGFS_Added,
118 WGFS_Conflicted,
119 WGFS_Deleted,
121 WGFS_Ignored = -1,
122 WGFS_Unversioned = -2,
123 WGFS_Empty = -3,
124 WGFS_Unknown = -4
127 // File flags
128 enum WGFILEFLAGS
130 WGFF_Directory = (1<<0) // enumerated file is a directory
133 struct wgFile_s
135 LPCTSTR sFileName; // filename or directory relative to project root (using forward slashes)
136 int nStatus; // the WGFILESTATUS of the file
137 int nFlags; // a combination of WGFILEFLAGS
139 const BYTE* sha1; // points to the BYTE[20] sha1 (NULL for directories, WGFF_Directory)
142 // Application-defined callback function for wgEnumFiles, returns TRUE to abort enumeration
143 // NOTE: do NOT store the pFile pointer or any pointers in wgFile_s for later use, the data is only valid for a single callback call
144 typedef BOOL (__cdecl WGENUMFILECB)(const struct wgFile_s *pFile, void *pUserData);
147 /////////////////////////////////////////////////////////////////////
150 // convert wingit.dll status to git_wc_status_kind
151 inline static git_wc_status_kind GitStatusFromWingit(int nStatus)
153 switch (nStatus)
155 case WGFS_Normal: return git_wc_status_normal;
156 case WGFS_Modified: return git_wc_status_modified;
157 case WGFS_Staged: return git_wc_status_merged;
158 case WGFS_Added: return git_wc_status_added;
159 case WGFS_Conflicted: return git_wc_status_conflicted;
160 case WGFS_Deleted: return git_wc_status_deleted;
162 case WGFS_Ignored: return git_wc_status_ignored;
163 case WGFS_Unversioned: return git_wc_status_unversioned;
164 case WGFS_Empty: return git_wc_status_unversioned;
167 return git_wc_status_none;
170 // convert 20 byte sha1 hash to the git_revnum_t type
171 inline static git_revnum_t ConvertHashToRevnum(const BYTE *sha1)
173 if (!sha1)
174 return GIT_INVALID_REVNUM;
176 char s[41];
177 char *p = s;
178 for (int i=0; i<20; i++)
180 #pragma warning(push)
181 #pragma warning(disable: 4996)
182 sprintf(p, "%02x", (UINT)*sha1);
183 #pragma warning(pop)
184 p += 2;
185 sha1++;
188 return CString(s);
191 typedef BOOL (*FIll_STATUS_CALLBACK)(const CString &path,git_wc_status_kind status,bool isDir, void *pdata);
194 * \ingroup Git
195 * Handles Subversion status of working copies.
197 class GitStatus
199 public:
201 #define GIT_MODE_INDEX 0x1
202 #define GIT_MODE_HEAD 0x2
203 #define GIT_MODE_IGNORE 0x4
204 #define GIT_MODE_ALL (GIT_MODE_INDEX|GIT_MODE_HEAD|GIT_MODE_IGNORE)
206 static int GetFileStatus(const CString &gitdir,const CString &path,git_wc_status_kind * status,BOOL IsFull=false, BOOL IsRecursive=false, BOOL isIgnore=true, FIll_STATUS_CALLBACK callback=NULL,void *pData=NULL);
207 static int GetDirStatus(const CString &gitdir,const CString &path,git_wc_status_kind * status,BOOL IsFull=false, BOOL IsRecursive=false, BOOL isIgnore=true, FIll_STATUS_CALLBACK callback=NULL, void *pData=NULL);
208 static int EnumDirStatus(const CString &gitdir,const CString &path,git_wc_status_kind * status,BOOL IsFull=false, BOOL IsRecursive=false, BOOL isIgnore=true, FIll_STATUS_CALLBACK callback=NULL, void *pData=NULL);
209 static int GetFileList(const CString &gitdir, const CString &path, std::vector<CString> &list);
210 static bool IsGitReposChanged(const CString &gitdir, const CString &subpaths, int mode=GIT_MODE_ALL);
211 static int LoadIgnoreFile(const CString &gitdir, const CString &subpaths);
212 static int IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir,bool *isVersion);
213 static int IsIgnore(const CString &gitdir, const CString &path, bool *isIgnore);
214 static __int64 GetIndexFileTime(const CString &gitdir);
215 static bool IsExistIndexLockFile(const CString &gitdir);
216 static int GetIgnoreFileChangeTimeList(const CString &path, std::vector<__int64> &timelist);
218 static int GetHeadHash(const CString &gitdir, CGitHash &hash);
220 public:
221 GitStatus(bool * pbCanceled = NULL);
222 ~GitStatus(void);
226 * Reads the Subversion status of the working copy entry. No
227 * recurse is done, even if the entry is a directory.
228 * If the status of the text and property part are different
229 * then the more important status is returned.
231 static git_wc_status_kind GetAllStatus(const CTGitPath& path, git_depth_t depth = git_depth_empty);
234 * Reads the Subversion status of the working copy entry and all its
235 * subitems. The resulting status is determined by using priorities for
236 * each status. The status with the highest priority is then returned.
237 * If the status of the text and property part are different then
238 * the more important status is returned.
240 static git_wc_status_kind GetAllStatusRecursive(const CTGitPath& path);
243 * Returns the status which is more "important" of the two statuses specified.
244 * This is used for the "recursive" status functions on folders - i.e. which status
245 * should be returned for a folder which has several files with different statuses
246 * in it.
248 static git_wc_status_kind GetMoreImportant(git_wc_status_kind status1, git_wc_status_kind status2);
251 * Checks if a status is "important", i.e. if the status indicates that the user should know about it.
252 * E.g. a "normal" status is not important, but "modified" is.
253 * \param status the status to check
255 static BOOL IsImportant(git_wc_status_kind status) {return (GetMoreImportant(git_wc_status_added, status)==status);}
258 * Reads the Subversion text status of the working copy entry. No
259 * recurse is done, even if the entry is a directory.
260 * The result is stored in the public member variable status.
261 * Use this method if you need detailed information about a file/folder, not just the raw status (like "normal", "modified").
263 * \param path the pathname of the entry
264 * \param update true if the status should be updated with the repository. Default is false.
265 * \return If update is set to true the HEAD revision of the repository is returned. If update is false then -1 is returned.
266 * \remark If the return value is -2 then the status could not be obtained.
268 git_revnum_t GetStatus(const CTGitPath& path, bool update = false, bool noignore = false, bool noexternals = false);
271 * Returns a string representation of a Subversion status.
272 * \param status the status enum
273 * \param string a string representation
275 static void GetStatusString(git_wc_status_kind status, size_t buflen, TCHAR * string);
276 static void GetStatusString(HINSTANCE hInst, git_wc_status_kind status, TCHAR * string, int size, WORD lang);
279 * Returns the string representation of a depth.
281 #ifdef _MFC_VER
282 static CString GetDepthString(git_depth_t depth);
283 #endif
284 static void GetDepthString(HINSTANCE hInst, git_depth_t depth, TCHAR * string, int size, WORD lang);
287 * Returns the status of the first file of the given path. Use GetNextFileStatus() to obtain
288 * the status of the next file in the list.
289 * \param path the path of the folder from where the status list should be obtained
290 * \param retPath the path of the file for which the status was returned
291 * \param update set this to true if you want the status to be updated with the repository (needs network access)
292 * \param recurse true to fetch the status recursively
293 * \param bNoIgnore true to not fetch the ignored files
294 * \param bNoExternals true to not fetch the status of included Git:externals
295 * \return the status
297 git_wc_status2_t * GetFirstFileStatus(const CTGitPath& path, CTGitPath& retPath, bool update = false, git_depth_t depth = git_depth_infinity, bool bNoIgnore = true, bool bNoExternals = false);
298 unsigned int GetFileCount() const {return /*apr_hash_count(m_statushash);*/0;}
299 unsigned int GetVersionedCount() const;
301 * Returns the status of the next file in the file list. If no more files are in the list then NULL is returned.
302 * See GetFirstFileStatus() for details.
304 git_wc_status2_t * GetNextFileStatus(CTGitPath& retPath);
306 * Checks if a path is an external folder.
307 * This is necessary since Subversion returns two entries for external folders: one with the status Git_wc_status_external
308 * and one with the 'real' status of that folder. GetFirstFileStatus() and GetNextFileStatus() only return the 'real'
309 * status, so with this method it's possible to check if the status also is Git_wc_status_external.
311 bool IsExternal(const CTGitPath& path) const;
313 * Checks if a path is in an external folder.
315 bool IsInExternal(const CTGitPath& path) const;
318 * Clears the memory pool.
320 void ClearPool();
323 * This member variable hold the status of the last call to GetStatus().
325 git_wc_status2_t * status; ///< the status result of GetStatus()
327 git_revnum_t headrev; ///< the head revision fetched with GetFirstStatus()
329 bool * m_pbCanceled;
330 #ifdef _MFC_VER
331 friend class Git; // So that Git can get to our m_err
333 * Returns the last error message as a CString object.
335 CString GetLastErrorMsg() const;
338 * Set a list of paths which will be considered when calling GetFirstFileStatus.
339 * If a filter is set, then GetFirstFileStatus/GetNextFileStatus will only return items which are in the filter list
341 void SetFilter(const CTGitPathList& fileList);
342 void ClearFilter();
344 #else
346 * Returns the last error message as a CString object.
348 stdstring GetLastErrorMsg() const;
349 #endif
352 protected:
353 // apr_pool_t * m_pool; ///< the memory pool
354 private:
355 typedef struct sort_item
357 const void *key;
358 // apr_ssize_t klen;
359 void *value;
360 } sort_item;
362 typedef struct hashbaton_t
364 GitStatus* pThis;
365 // apr_hash_t * hash;
366 // apr_hash_t * exthash;
367 } hash_baton_t;
369 // git_client_ctx_t * ctx;
370 git_wc_status_kind m_allstatus; ///< used by GetAllStatus and GetAllStatusRecursive
371 // git_error_t * m_err; ///< Subversion error baton
372 git_error_t m_err;
374 git_wc_status2_t m_status; // used for GetStatus
376 #ifdef _MFC_VER
377 // GitPrompt m_prompt;
378 #endif
381 * Returns a numeric value indicating the importance of a status.
382 * A higher number indicates a more important status.
384 static int GetStatusRanking(git_wc_status_kind status);
387 * Callback function which collects the raw status from a Git_client_status() function call
389 //static git_error_t * getallstatus (void *baton, const char *path, git_wc_status2_t *status, apr_pool_t *pool);
390 static BOOL getallstatus(const struct wgFile_s *pFile, void *pUserData);
391 static BOOL getstatus(const struct wgFile_s *pFile, void *pUserData);
394 * Callback function which stores the raw status from a Git_client_status() function call
395 * in a hash table.
397 // static git_error_t * getstatushash (void *baton, const char *path, git_wc_status2_t *status, apr_pool_t *pool);
400 * helper function to sort a hash to an array
402 // static apr_array_header_t * sort_hash (apr_hash_t *ht, int (*comparison_func) (const sort_item *,
403 // const sort_item *), apr_pool_t *pool);
406 * Callback function used by qsort() which does the comparison of two elements
408 static int __cdecl sort_compare_items_as_paths (const sort_item *a, const sort_item *b);
410 //for GetFirstFileStatus and GetNextFileStatus
411 // apr_hash_t * m_statushash;
412 // apr_array_header_t * m_statusarray;
413 unsigned int m_statushashindex;
414 // apr_hash_t * m_externalhash;
416 #pragma warning(push)
417 #pragma warning(disable: 4200)
418 struct STRINGRESOURCEIMAGE
420 WORD nLength;
421 WCHAR achString[];
423 #pragma warning(pop) // C4200
425 static int LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage);
426 static git_error_t* cancel(void *baton);
428 // A sorted list of filenames (in Git format, in lowercase)
429 // when this list is set, we only pick-up files during a GetStatus which are found in this list
430 typedef std::vector<std::string> StdStrAVector;
431 StdStrAVector m_filterFileList;