From 1c93f00212b37bf480bc7f30d7ac4576a74d8303 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 28 Dec 2021 13:57:33 -0800 Subject: [PATCH] cmd/init- ask about .gitattributes, fixes #42 --- src/cmd/init.zig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/cmd/init.zig b/src/cmd/init.zig index bea2ab01a0a927903ca970de5a62c1f58a6e2e47..dcb48525a2cb78124f3b14a784d64ebec8286323 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -120,6 +120,28 @@ pub fn execute(args: [][]u8) !void { try w.writeAll("deps.zig\n"); } } + + // ask about .gitattributes + if (try u.does_folder_exist(".git")) { + const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa); + if (do) { + const exists = try u.does_file_exist(null, ".gitattributes"); + const file: std.fs.File = try (if (exists) cwd.openFile(".gitattributes", .{ .read = true, .write = true }) else cwd.createFile(".gitattributes", .{})); + defer file.close(); + const len = try file.getEndPos(); + if (len > 0) try file.seekTo(len - 1); + const w = file.writer(); + if (len > 0 and (try file.reader().readByte()) != '\n') { + try w.writeAll("\n"); + } + if (!exists) try w.writeAll("* text=auto\n"); + if (!exists) try w.writeAll("*.zig text eol=lf # See https://github.com/ziglang/zig-spec/issues/38\n"); + try w.writeAll("zig.mod text eol=lf\n"); + try w.writeAll("zigmod.* text eol=lf\n"); + try w.writeAll("zig.mod linguist-language=YAML\n"); + try w.writeAll("zig.mod gitlab-language=yaml\n"); + } + } } pub fn writeExeManifest(w: std.fs.File.Writer, id: string, name: string, license: ?string, description: ?string) !void { -- 2.54.0