move platform independent files into common folder
[git-cheetah/kirill.git] / columns.c
blobfd026b4514813a4617052738ffd0b9e57990447c
2 #include "common/cache.h"
3 #include "common/exec.h"
5 #include <shlobj.h>
6 #include "common/menuengine.h"
7 #include "ext.h"
8 #include "columns.h"
10 #ifndef _MSC_VER
12 * this flag, not defined in mingw's ShlObj.h, is a hint that the file
13 * has changed since the last call to GetItemData
15 #define SHCDF_UPDATEITEM 0x00000001
17 /* convenience macro, not defined in mingw's OleAuto.h */
18 #define V_I1REF(X) V_UNION(X, pcVal)
19 #endif
21 #define CHEETAH_COLUMN_FLAGS (SHCOLSTATE_TYPE_STR)
24 * If a non-standard FMT is specified, the column will be accessible
25 * only in repo folders. But if FMTID_SummaryInformation is used,
26 * the column's info will be "merged" into CHEETAH_PID_* and may
27 * be visible in the Details on folder's task pane
29 #define CHEETAH_FMTID IID_git_columns
32 * column's id, that can be matched to one of standard FMTs
33 * for example, if FMTID is FMTID_SummaryInformation than
34 * PID can be PIDSI_AUTHOR, so the status is provided in the
35 * Author column and visible in the Details
37 #define CHEETAH_STATUS_PID 0
39 /* names and descriptions MUST be wchar * */
40 #define CHEETAH_STATUS_NAME L"Git Status"
41 #define CHEETAH_STATUS_DESC L"Status of the file in a Git repository"
43 STDMETHODIMP initialize_columns(void *p, LPCSHCOLUMNINIT psci)
45 struct git_data *this_ = ((struct git_columns *)p)->git_data;
46 int status;
48 wcstombs(this_->name, psci->wszFolder, MAX_PATH);
51 * don't try to do anything about cache here, because
52 * we're called even if get_item_data will not be called
54 status = exec_program(this_->name, NULL, NULL, WAITMODE,
55 "git", "rev-parse", "--show-prefix", NULL);
58 * if something went terribly wrong or not a repo,
59 * return E_FAIL to prevent calls to get_column_info
61 return status ? E_FAIL : S_OK;
64 STDMETHODIMP get_column_info(void *p, DWORD dwIndex, SHCOLUMNINFO *psci)
66 struct git_data *this_ = ((struct git_columns *)p)->git_data;
68 /* do NOT FORGET to increase this when adding more columns */
69 if (0 < dwIndex)
70 return S_FALSE;
72 psci->scid.fmtid = CHEETAH_FMTID;
73 psci->scid.pid = CHEETAH_STATUS_PID;
74 psci->vt = VT_LPSTR;
75 psci->fmt = LVCFMT_LEFT;
76 psci->cChars = 15;
77 psci->csFlags = CHEETAH_COLUMN_FLAGS;
79 lstrcpynW(psci->wszTitle,
80 CHEETAH_STATUS_NAME, MAX_COLUMN_NAME_LEN);
81 lstrcpynW(psci->wszDescription,
82 CHEETAH_STATUS_DESC, MAX_COLUMN_DESC_LEN);
84 return S_OK;
87 static int cache_others(char *wd, struct strbuf *cache)
89 int status;
91 if (cache->alloc)
92 return 0;
94 strbuf_init(cache, 0);
95 status = exec_program(wd, cache, NULL, WAITMODE,
96 "git", "ls-files", "-z", "--others", "--exclude-standard",
97 NULL);
99 /* something went terribly wrong or not a repo */
100 if (status)
101 strbuf_release(cache);
102 return status;
106 * cache files' status, and return non-zero on the first failure
107 * because if something goes wrong, there is no need to try other statuses
109 static int cache(struct git_data *this_)
111 int status;
112 if (status = cache_others(this_->name, &this_->other_files))
113 return status;
115 /* if everything succeeded, return success */
116 return 0;
119 static char *parse_others(struct strbuf *cache, char *file)
121 char *start = cache->buf;
122 while (*start) {
123 if (!stricmp(file, start))
124 return "other";
126 start += strlen(start) + 1;
129 return 0;
132 static char *parse_status(struct git_data *this_, char *file)
134 char *result = NULL;
136 if (result = parse_others(&this_->other_files, file))
137 return result;
139 /* if no status was found, return NULL string */
140 return NULL;
144 STDMETHODIMP get_item_data(void *p,
145 LPCSHCOLUMNID pscid,
146 LPCSHCOLUMNDATA pscd,
147 VARIANT *pvarData)
149 struct git_data *this_ = ((struct git_columns *)p)->git_data;
150 char file_path[MAX_PATH], *file = file_path;
151 char *result;
153 /* directories don't have status */
154 if (pscd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
155 return S_FALSE;
157 /* update cache and bail out on failure */
158 if (cache(this_))
159 return S_FALSE;
161 if (pscd->dwFlags & SHCDF_UPDATEITEM) {
163 * we can add the file to the usual ls-files
164 * or we can refresh the whole cache
168 wcstombs(file, pscd->wszFile, MAX_PATH);
169 file += strlen(this_->name) + 1;
171 /* get the status from cache and return if any */
172 if (result = parse_status(this_, file)) {
173 V_VT(pvarData) = VT_LPSTR;
174 V_I1REF(pvarData) = result;
175 return S_OK;
178 return S_FALSE;
181 DEFINE_STANDARD_METHODS(git_columns)
183 struct git_columns_virtual_table git_columns_virtual_table = {
184 query_interface_git_columns,
185 add_ref_git_columns,
186 release_git_columns,
187 initialize_columns,
188 get_column_info,
189 get_item_data